-
-
Notifications
You must be signed in to change notification settings - Fork 32k
Wrong inspect.getsource for datetime #76494
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
Comments
inspect.getsource(datetime) shows the Python source code for datetime, even when it is the C extension. This is very confusing. I believe it's because _datetime is used to override everything in datetime at the end of the file (here Line 2285 in 11a247d
|
I think this is mostly the correct behavior (though it may indeed be confusing). Note that If you try and get the source of individual methods that were imported from >>> inspect.getsource(datetime.datetime.fromisoformat)
-- Long traceback --
TypeError: module, class, method, function, traceback, frame, or code object was expected, got builtin_function_or_method That said, This is consistent with the behavior of Not sure if this is something that should be a warning, an exception or if the behavior should simply be documented in the I'll note that both |
The documentation for getfile says "This will fail with a TypeError if the object is a built-in module, class, or function." https://docs.python.org/3/library/inspect.html#inspect.getfile But it doesn't:
>>> inspect.getfile(datetime.datetime)
'C:\\Users\\User\\src\\cpython\\lib\\datetime.py' |
Looks like for import inspect, datetime
print(inspect.getfile(datetime.datetime)) outputs, /usr/local/Cellar/python@2/2.7.17_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload/datetime.so It seems a long time overdue? |
_datetime.datetime's tp_name is datetime.datetime so If the _datetime.datetime's tp_name is updated to _datetime.datetime it could be fixed but I don't know this is the right approach and there might be some reason for choosing tp_name as datetime.datetime instead of _datetime.datetime. @p-ganssle is the best expert for datetime module :) |
I think that we should re-examine this issue after #64671 is merged. I'm not really sure how that will affect this and indeed *how* it should affect this. I am not sure whether people are relying on the current behavior, or what use cases would be improved if we had a different behavior. With regards to this:
This is a bit unclear to me, and I'm not entirely sure if We should probably clarify the wording of |
In many places (source code or docs), built-in is used not to mean «global function in the builtins module» but «defined in an extension module». You can see Guido referring to datetime classes as built-in here for example: #88784 (comment) |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: