-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-108901: Add inspect.Signature.from_frame
#116537
base: main
Are you sure you want to change the base?
Conversation
Misc/NEWS.d/next/Library/2024-03-09-12-55-44.gh-issue-108901.s9VClL.rst
Outdated
Show resolved
Hide resolved
Misc/NEWS.d/next/Library/2024-03-09-12-55-44.gh-issue-108901.s9VClL.rst
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I haven't really got the bandwidth to look at this right now, sorry :) |
I agree, we should drop the defaults part. >>> import sys, inspect
>>> def f(x=1):
... print(inspect.getargvalues(sys._getframe()))
...
>>> f()
ArgInfo(args=['x'], varargs=None, keywords=None, locals={'x': 1}) Current API does not show defaults, only locals. But, locals are stored as Showing defaults is also not safe in this case by default, for example we can leak things like passwords or secret keys. |
I looked at tests where the frame is captured whereas frame locals are not modified, so "it just works" magically. But if I look at the test which modify a variable, I now understand how wrong it is. A frame local is not a function default parameter value: they are two different things. signature() is the signature, before the function is called. Frame locals is the "live state" of a frame, it's unrelated. Moreover, frame locals can contain sensitive information (like a password) which should not be exposed in a signature. |
Updated, now defaults are not shown. Right now this is very close to >>> import sys, inspect
>>> def f(x=1):
... print(inspect.getargvalues(sys._getframe()))
...
>>> f()
ArgInfo(args=['x'], varargs=None, keywords=None, locals={'x': 1}) And we are trying to replace this old function, so I guess it is fine :) |
(This has a merge conflict) |
@sobolevn: Can you please try to fix the merge conflict? |
in a function inside ``__defaults__``, ``__kwdefaults__``, | ||
and ``__annotations__`` attributes. | ||
|
||
.. versionadded:: 3.13 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.. versionadded:: 3.13 | |
.. versionadded:: next |
inspect | ||
------- | ||
|
||
* Add :meth:`inspect.Signature.from_frame` to get signatures from frame objects. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't we need the "Contributed by" part? (to have the issue number)
I had to go with a bigger diff, but easier code and probably more optimal one.
The main reason was that creating
types.FunctionType
is not trivial, for example, it required the correct amount of__closure__
vars for a code object. So, let's not create it: it will reduce the amount of possible errors.First PR: #112639
inspect
module, deprecate old incorrect APIs #108901📚 Documentation preview 📚: https://cpython-previews--116537.org.readthedocs.build/