Skip to content

Define all Pdb's instance variable in __init__ #140790

@Carreau

Description

@Carreau

Pdb currently have a few instances variables that are not defined in __init__.

Mainly as far as I can tell, :

self.lineno
self.stack
self.curindex
self.curframe
self.currentbp
self._user_requested_quit

(not set in Bdb, or Cmd superclass either)

This means that accessing these requires a hasattr/getattr, which we can see in the Pdb's implementation in a few places. This is a footgun for a subclasses, and in particular for IPython, in some circumstances, the gettattr start to have a non-negligible perf impact (We support things like having __debuggerskip__ = True|False in a frame, so the getattr(self, 'currentframe',...) start to get expensive.

https://github.com/ipython/ipython/blob/f74b500b0f3061452898ed51cb44e05ebda01cd6/IPython/core/debugger.py#L595-L615

It would be great if all those were initialized in __init__; Yes I can initialize in my __init__ subclass, but having it in CPython should benefit everybody, plus it would allow the removal of a few getattr() in CPython source directly.

Personally I care mostly about curframe, but while at it maybe do the other ones.

I am not aware of anything that uses the fact the attribute is missing.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions