This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: inspect.getsource() raises a wrong exception type
Type: behavior Stage:
Components: Documentation, Library (Lib) Versions: Python 3.3, Python 3.4
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: docs@python Nosy List: BreamoreBoy, docs@python, marco.buttu, yselivanov
Priority: normal Keywords:

Created on 2013-11-01 11:54 by marco.buttu, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (3)
msg201891 - (view) Author: Marco Buttu (marco.buttu) * Date: 2013-11-01 11:54
I was looking at inspect.getsource(). In Python 3.3 and 3.4 either the docstring and the online doc say it raises a OSError, and in fact: 

>>> import inspect
>>> def foo():
...     pass
>>> inspect.getsource(foo)
Traceback (most recent call last):
OSError: could not get source code

However, getsource() calls getfile(), and this one raises a TypeError:

>>> inspect.getsource(0)
Traceback (most recent call last):
TypeError: 0 is not a module, class, method, function, traceback, frame, or code object
msg228281 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-10-02 23:49
@Yury do you agree with this?
msg228382 - (view) Author: Yury Selivanov (yselivanov) * (Python committer) Date: 2014-10-03 21:17
> @Yury do you agree with this?

I think it's a perfectly normal behaviour. OSError is raised for valid kind of objects, and TypeError is raised when you're passing something weird. That's a pretty common practice is Python.
Date User Action Args
2022-04-11 14:57:53adminsetgithub: 63671
2014-10-03 21:17:18yselivanovsetstatus: open -> closed
resolution: not a bug
2014-10-03 21:17:09yselivanovsetmessages: + msg228382
2014-10-02 23:49:37BreamoreBoysetnosy: + yselivanov, BreamoreBoy
messages: + msg228281
2013-11-01 11:54:23marco.buttucreate