Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/PyInstaller/hooks/rthooks/pyi_rth_inspect.py: 0%
18 statements
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +0000
« prev ^ index » next coverage.py v7.0.1, created at 2022-12-25 06:11 +0000
1#-----------------------------------------------------------------------------
2# Copyright (c) 2021-2022, PyInstaller Development Team.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6#
7# The full license is in the file COPYING.txt, distributed with this software.
8#
9# SPDX-License-Identifier: Apache-2.0
10#-----------------------------------------------------------------------------
12import inspect
13import os
14import sys
16_orig_inspect_getsourcefile = inspect.getsourcefile
19# Provide custom implementation of inspect.getsourcefile() for frozen applications that properly resolves relative
20# filenames obtained from object (e.g., inspect stack-frames). See #5963.
21def _pyi_getsourcefile(object):
22 filename = inspect.getfile(object)
23 if not os.path.isabs(filename):
24 # Check if given filename matches the basename of __main__'s __file__.
25 main_file = sys.modules['__main__'].__file__
26 if filename == os.path.basename(main_file):
27 return main_file
29 # If filename ends with .py suffix and does not correspond to frozen entry-point script, convert it to
30 # corresponding .pyc in sys._MEIPASS.
31 if filename.endswith('.py'):
32 filename = os.path.normpath(os.path.join(sys._MEIPASS, filename + 'c'))
33 # Ensure the relative path did not try to jump out of sys._MEIPASS, just in case...
34 if filename.startswith(sys._MEIPASS):
35 return filename
36 elif filename.startswith(sys._MEIPASS) and filename.endswith('.pyc'):
37 # If filename is already PyInstaller-compatible, prevent any further processing (i.e., with original
38 # implementation).
39 return filename
40 # Use original implementation as a fallback.
41 return _orig_inspect_getsourcefile(object)
44inspect.getsourcefile = _pyi_getsourcefile