Title: Wrong inspect.getsource for datetime
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6, Python 3.4, Python 3.5
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Aaron.Meurer, belopolsky, corona10, iritkatriel, p-ganssle, thatiparthy, xtreak, yselivanov
Priority: normal Keywords:

Created on 2017-12-13 21:46 by Aaron.Meurer, last changed 2020-08-27 21:16 by p-ganssle.

Messages (6)
msg308255 - (view) Author: Aaron Meurer (Aaron.Meurer) Date: 2017-12-13 21:46
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, but __file__ is not imported.
msg325495 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2018-09-16 17:42
I think this is *mostly* the correct behavior (though it may indeed be confusing). Note that `` *is* the source of the module `datetime`, it's just that most of the code in there is shadowed by the line you've linked.

If you try and get the source of individual methods that were imported from `_datetime`, you'll get the expected failure:

>>> 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, `inspect.getsource` does seem to be erroneously using the Python source for classes, e.g. `print(inspect.getsource(`

This is consistent with the behavior of `functools`, where the Python code for `functools.partial`, even when the C implementation is used.

Not sure if this is something that should be a warning, an exception or if the behavior should simply be documented in the `inspect` documentation.

I'll note that both `inspect.getsource(datetime)` and `inspect.getsource(` were IOError exceptions in Python 2.7, but have been returning the Python source code since at least Python 3.4.
msg375821 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2020-08-23 18:37
The documentation for getfile says "This will fail with a TypeError if the object is a built-in module, class, or function."

But it doesn't:
>>> inspect.getfile(datetime.datetime)
msg375912 - (view) Author: Srinivas Reddy Thatiparthy(శ్రీనివాస్ రెడ్డి తాటిపర్తి) (thatiparthy) * Date: 2020-08-26 06:54
Looks like for `getfile` docs should be updated. On python2.7.17_1, 

  import inspect, datetime
  print(inspect.getfile(datetime.datetime)) outputs, 


It seems a long time overdue?
msg375932 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2020-08-26 14:11
_datetime.datetime's tp_name is datetime.datetime so
inspect module guess that _datetime.datetime's module name is datetime, not _datetime.
I don't think we have to fix this issue from inspect module side.

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 he could give the answer to this.
msg376014 - (view) Author: Paul Ganssle (p-ganssle) * (Python committer) Date: 2020-08-27 21:16
I think that we should re-examine this issue after GH-20472 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:

> The documentation for getfile says "This will fail with a TypeError if the object is a built-in module, class, or function."


This is a bit unclear to me, and I'm not entirely sure if `datetime` qualifies. I think of built-in classes as things like `int` and `float`, and built-in functions as things like `abs` and `sum`, and `datetime` is an extension module — albeit one with a C implementation, and one that is in the standard library.

We should probably clarify the wording of `inspect.getsource` and determine what the intended semantics are for PEP-399-style modules, with both a C and pure Python implementation and the C implementation is what's being used. Error? Point to the Python implementation?
Date User Action Args
2020-08-27 21:16:51p-gansslesetmessages: + msg376014
2020-08-26 14:11:22corona10setnosy: + corona10
messages: + msg375932
2020-08-26 06:54:30thatiparthysetnosy: + thatiparthy
messages: + msg375912
2020-08-23 18:37:36iritkatrielsetnosy: + iritkatriel
messages: + msg375821
2018-09-16 17:42:53p-gansslesetversions: + Python 3.4, Python 3.5, Python 3.6, Python 3.7, Python 3.8
nosy: + belopolsky, yselivanov

messages: + msg325495

components: + Library (Lib)
type: behavior
2018-09-14 19:09:19p-gansslesetnosy: + p-ganssle
2018-09-14 17:28:58xtreaksetnosy: + xtreak
2017-12-13 21:46:39Aaron.Meurercreate