[New-bugs-announce] [issue46547] `pydoc.Helper` leaks several `for` loop variables

Nikita Sobolev report at bugs.python.org
Thu Jan 27 06:40:53 EST 2022

New submission from Nikita Sobolev <mail at sobolevn.me>:

Here's the problem. `pydoc.Helper` is defined as:

class Helper:
    for topic, symbols_ in _symbols_inverse.items():
        for symbol in symbols_:
            topics = symbols.get(symbol, topic)
            if topic not in topics:
                topics = topics + ' ' + topic
            symbols[symbol] = topics

Link: https://github.com/python/cpython/blob/08c0ed2d9c0d01ad1a5adc0787bc75e4e90cbb85/Lib/pydoc.py#L1877-L1882

It causes some unwanted consequences: `topic, symbols_, symbol` are leaking to the class's namespace. Example:

>>> import pydoc
>>> pydoc.Helper.symbol
>>> pydoc.Helper.topic
>>> pydoc.Helper.symbols_
('j', 'J')

There's also `topics` var, but it is shadowed later.

So, I propose deleting all intermediate variables right after the `for` loop.

components: Library (Lib)
messages: 411856
nosy: sobolevn
priority: normal
severity: normal
status: open
title: `pydoc.Helper` leaks several `for` loop variables
type: behavior
versions: Python 3.11

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list