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.

classification
Title: inspect.getouterframes() tracebacks when $CWD does not exists
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: iritkatriel, lukasz.langa, miss-islington, msuchy@redhat.com, raulcd
Priority: normal Keywords: patch

Created on 2021-10-07 13:57 by msuchy@redhat.com, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28824 merged iritkatriel, 2021-10-08 18:56
PR 29377 merged miss-islington, 2021-11-02 21:55
PR 29378 merged miss-islington, 2021-11-02 21:56
Messages (7)
msg403410 - (view) Author: Miroslav Suchý (msuchy@redhat.com) Date: 2021-10-07 13:57
When you exec inspect.getouterframes() in directory, which no longer exists, then you get traceback, because the code traverse to os.getcwd() which fails.

Reproducer:

$ mkdir /tmp/bar
$ cd /tmp/bar
$ rmdir /tmp/bar
$ python3
Python 3.10.0rc2 (default, Sep  8 2021, 00:00:00) [GCC 11.2.1 20210728 (Red Hat 11.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> inspect.getouterframes(inspect.currentframe())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python3.10/inspect.py", line 1653, in getouterframes
    frameinfo = (frame,) + getframeinfo(frame, context)
  File "/usr/lib64/python3.10/inspect.py", line 1623, in getframeinfo
    filename = getsourcefile(frame) or getfile(frame)
  File "/usr/lib64/python3.10/inspect.py", line 829, in getsourcefile
    module = getmodule(object, filename)
  File "/usr/lib64/python3.10/inspect.py", line 861, in getmodule
    file = getabsfile(object, _filename)
  File "/usr/lib64/python3.10/inspect.py", line 845, in getabsfile
    return os.path.normcase(os.path.abspath(_filename))
  File "/usr/lib64/python3.10/posixpath.py", line 383, in abspath
    cwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory
msg403461 - (view) Author: Raúl Cumplido (raulcd) * Date: 2021-10-08 08:13
I have reproduced on python3.10 and validated this is also happening for other python versions:

# PYTHON 3.9
$ mkdir /tmp/bar
$ cd /tmp/bar
$ rmdir /tmp/bar
$ python3.9
Python 3.9.5 (default, May 19 2021, 11:32:47) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> inspect.getouterframes(inspect.currentframe())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/inspect.py", line 1529, in getouterframes
    frameinfo = (frame,) + getframeinfo(frame, context)
  File "/usr/lib/python3.9/inspect.py", line 1499, in getframeinfo
    filename = getsourcefile(frame) or getfile(frame)
  File "/usr/lib/python3.9/inspect.py", line 709, in getsourcefile
    if getattr(getmodule(object, filename), '__loader__', None) is not None:
  File "/usr/lib/python3.9/inspect.py", line 738, in getmodule
    file = getabsfile(object, _filename)
  File "/usr/lib/python3.9/inspect.py", line 722, in getabsfile
    return os.path.normcase(os.path.abspath(_filename))
  File "/usr/lib/python3.9/posixpath.py", line 379, in abspath
    cwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory

# PYTHON 3.8
$ mkdir /tmp/bar
$ cd /tmp/bar
$ rmdir /tmp/bar
$ python3.8
Python 3.8.10 (default, Sep 28 2021, 16:10:42) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inspect
>>> inspect.getouterframes(inspect.currentframe())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/inspect.py", line 1503, in getouterframes
    frameinfo = (frame,) + getframeinfo(frame, context)
  File "/usr/lib/python3.8/inspect.py", line 1473, in getframeinfo
    filename = getsourcefile(frame) or getfile(frame)
  File "/usr/lib/python3.8/inspect.py", line 708, in getsourcefile
    if getattr(getmodule(object, filename), '__loader__', None) is not None:
  File "/usr/lib/python3.8/inspect.py", line 737, in getmodule
    file = getabsfile(object, _filename)
  File "/usr/lib/python3.8/inspect.py", line 721, in getabsfile
    return os.path.normcase(os.path.abspath(_filename))
  File "/usr/lib/python3.8/posixpath.py", line 379, in abspath
    cwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 76, in apport_excepthook
    binary = os.path.realpath(os.path.join(os.getcwd(), sys.argv[0]))
FileNotFoundError: [Errno 2] No such file or directory

Original exception was:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/inspect.py", line 1503, in getouterframes
    frameinfo = (frame,) + getframeinfo(frame, context)
  File "/usr/lib/python3.8/inspect.py", line 1473, in getframeinfo
    filename = getsourcefile(frame) or getfile(frame)
  File "/usr/lib/python3.8/inspect.py", line 708, in getsourcefile
    if getattr(getmodule(object, filename), '__loader__', None) is not None:
  File "/usr/lib/python3.8/inspect.py", line 737, in getmodule
    file = getabsfile(object, _filename)
  File "/usr/lib/python3.8/inspect.py", line 721, in getabsfile
    return os.path.normcase(os.path.abspath(_filename))
  File "/usr/lib/python3.8/posixpath.py", line 379, in abspath
    cwd = os.getcwd()
FileNotFoundError: [Errno 2] No such file or directory
>>>
msg403491 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-10-08 18:55
Thanks for the report, crash typically refers to segfaults rather than exceptions being raised. Updated versions.
msg405560 - (view) Author: Łukasz Langa (lukasz.langa) * (Python committer) Date: 2021-11-02 21:55
New changeset a459a81530de700b3d3faeb827b22ed1c9985812 by Irit Katriel in branch 'main':
bpo-45406: make inspect.getmodule() return None when getabsfile() raises FileNotFoundError (GH-28824)
https://github.com/python/cpython/commit/a459a81530de700b3d3faeb827b22ed1c9985812
msg405563 - (view) Author: miss-islington (miss-islington) Date: 2021-11-02 22:23
New changeset cfdd7d26a72e7ae523039e40c11001c2d7ef36ba by Miss Islington (bot) in branch '3.10':
bpo-45406: make inspect.getmodule() return None when getabsfile() raises FileNotFoundError (GH-28824)
https://github.com/python/cpython/commit/cfdd7d26a72e7ae523039e40c11001c2d7ef36ba
msg405565 - (view) Author: miss-islington (miss-islington) Date: 2021-11-02 22:26
New changeset 5f527caf15c52acc3f6e865721cdf781209f11ca by Miss Islington (bot) in branch '3.9':
bpo-45406: make inspect.getmodule() return None when getabsfile() raises FileNotFoundError (GH-28824)
https://github.com/python/cpython/commit/5f527caf15c52acc3f6e865721cdf781209f11ca
msg405567 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2021-11-02 23:15
Thanks Lukasz.
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89569
2021-11-02 23:15:32iritkatrielsetstatus: open -> closed
resolution: fixed
messages: + msg405567

stage: patch review -> resolved
2021-11-02 22:26:44miss-islingtonsetmessages: + msg405565
2021-11-02 22:23:57miss-islingtonsetmessages: + msg405563
2021-11-02 21:56:05miss-islingtonsetpull_requests: + pull_request27636
2021-11-02 21:55:59miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request27635
2021-11-02 21:55:55lukasz.langasetnosy: + lukasz.langa
messages: + msg405560
2021-10-08 18:56:23iritkatrielsetkeywords: + patch
stage: patch review
pull_requests: + pull_request27141
2021-10-08 18:55:43iritkatrielsetversions: + Python 3.9, Python 3.11
nosy: + iritkatriel

messages: + msg403491

type: crash -> behavior
2021-10-08 08:13:30raulcdsetnosy: + raulcd
messages: + msg403461
2021-10-07 13:58:18msuchy@redhat.comsettype: crash
2021-10-07 13:57:49msuchy@redhat.comcreate