Skip to content
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

building: add .py suffix to entry-point scripts without suffix #8049

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions PyInstaller/building/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,15 @@ def get_code_object(modname, filename):
with open(filename, 'rb') as f:
source = f.read()

# If entry-point script has no suffix, append .py when compiling the source. In POSIX builds, the executable
# has no suffix either; this causes issues with `traceback` module, as it tries to read the executable file
# when trying to look up the code for the entry-point script (when current working directory contains the
# executable).
_, ext = os.path.splitext(filename)
if not ext:
logger.debug("Appending .py to compiled entry-point name...")
filename += '.py'

try:
code_object = compile(source, filename, 'exec')
except SyntaxError:
Expand Down
10 changes: 10 additions & 0 deletions news/8046.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
If the entry-point script has no suffix, append the ``.py`` suffix
to the filename passed to the ``compile`` function when byte-compiling
the script for collection. This ensures that the entry-point script
filename never coincides with executable filename, especially in POSIX
builds, where executables have no suffix either (and their name is based
on the entry-point script basename by default). Entry-point script having
the same filename as the executable causes issues when ``traceback``
(and ``linecache``) try to access source code for it, an in the process
end up reading the executable file if it happens to be in the current
working directory.