Skip to content

Remove shebang and executable bit from most modules #118673

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

Closed
jaraco opened this issue May 6, 2024 · 9 comments · Fixed by #119658
Closed

Remove shebang and executable bit from most modules #118673

jaraco opened this issue May 6, 2024 · 9 comments · Fixed by #119658
Assignees
Labels
type-feature A feature request or enhancement

Comments

@jaraco
Copy link
Member

jaraco commented May 6, 2024

Feature or enhancement

Proposal:

In jaraco/backports.tarfile#4, I discovered that the presence of a shebang and executable bit on the tarfile module signals to downstream packagers and users that the module can be treated like a script and executed directly. That expectation seems to stem from an incidental artifact of the original contribution happening to have been written as a script. In fact, back in those days, many developers including myself would add #!/usr/bin/env python to a Python file just to signal that the syntax was Python, so it may never have been intended to be executed directly.

Nevertheless, the presence of the shebang led to it being marked as executable in #64135.

Marking this module as executable prevents the module from ever being ported to a package (such as backports.tarfile does) without breaking that assumption. It also tempts users to execute a module with the incorrect Python version. Instead, users should be directed to runpy invocation (i.e. python -m tarfile).

This proposal only includes tarfile in the scope, but it also implies that other modules changed in #64135 should also have their shebangs removed except where explicitly desired (and presumably documented or tested as such).

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

@jaraco jaraco added the type-feature A feature request or enhancement label May 6, 2024
@jaraco
Copy link
Member Author

jaraco commented May 6, 2024

In the prior bug, gpshead indicated that these shebangs should probably be removed.

@serhiy-storchaka Do you have any objections to removing executable bits and shebang from tarfile and maybe others?

@serhiy-storchaka
Copy link
Member

I think it made more sense in the past, when:

  • python -m did not exist.
  • There was usually only one system Python.
  • Python module (like pydoc) could be turned into a command by a symlink.
  • In general, the stdlib code supported compatibility with several Python versions.

Today this is perhaps no longer relevant or less relevant. But it needs to be discussed with a wider audience to shape general policy.

@jaraco jaraco changed the title Remove shebang and executable bit from tarfile module Remove shebang and executable bit from most modules May 16, 2024
@jaraco
Copy link
Member Author

jaraco commented May 16, 2024

Summarized and posted in discourse.

@merwok
Copy link
Member

merwok commented May 16, 2024

Previously reported in #85792 (comment) :

For stdlib modules, I think the intended usage is python3 -m encodings.rot_13; I don’t expect anyone to run python3 /usr/lib/python3.10/encodings/rot_13.py to do a decoding in the terminal!
So for these easter eggs / hidden gems in the stdlib, I would delete the shebang and remove the executable bit. There are 18 such modules, ignoring test, lib2to3, turtledemo and a few others named below.

There are 15 modules left now (+ 14 in turtledemo); I think there should be 1 PR rather than 15, and it would not have to be backported (let’s avoid the work + debating the definition of bugfix + xkcd 1172)

@jaraco
Copy link
Member Author

jaraco commented May 28, 2024

Sounds good. The consensus in the discourse is that we can remove them. I'll start a new PR as such.

@jaraco jaraco self-assigned this May 28, 2024
@jaraco
Copy link
Member Author

jaraco commented May 28, 2024

I note that there are only 14 modules in Lib, now that cgi.py is gone.

jaraco added a commit to jaraco/backports.tarfile that referenced this issue May 28, 2024
jaraco added a commit that referenced this issue May 29, 2024
…119658)

* gh-118673: Remove shebang and executable bits from stdlib modules.

* Removed shebangs and exe bits on turtledemo scripts.

The setting was inappropriate for '__main__' and inconsistent across the other modules. The scripts can still be executed directly by invoking with the desired interpreter.
@XuehaiPan
Copy link
Contributor

How about adding pre-commit checks for this?

  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v4.6.0
    hooks:
      - id: check-executables-have-shebangs
      - id: check-shebang-scripts-are-executable
      - id: check-symlinks
      - id: destroyed-symlinks

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: check-case-conflict
- id: check-merge-conflict
- id: check-toml
exclude: ^Lib/test/test_tomllib/
- id: check-yaml
- id: end-of-file-fixer
types: [python]
exclude: Lib/test/tokenizedata/coding20731.py
- id: trailing-whitespace
types_or: [c, inc, python, rst]

@jaraco
Copy link
Member Author

jaraco commented May 31, 2024

That seems reasonable. Probably relates more to the earlier PR to make things consistent than to this one (as the checks would pass both before and after). Feel free to prepare a PR.

noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
…es. (python#119658)

* pythongh-118673: Remove shebang and executable bits from stdlib modules.

* Removed shebangs and exe bits on turtledemo scripts.

The setting was inappropriate for '__main__' and inconsistent across the other modules. The scripts can still be executed directly by invoking with the desired interpreter.
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
…es. (python#119658)

* pythongh-118673: Remove shebang and executable bits from stdlib modules.

* Removed shebangs and exe bits on turtledemo scripts.

The setting was inappropriate for '__main__' and inconsistent across the other modules. The scripts can still be executed directly by invoking with the desired interpreter.
@terryjreedy
Copy link
Member

Not backporting this to 3.13 and 3.12 at least for turtledemo examples made it impossible to backport fixes to their docstrings without getting a merge conflict. They all (most?) start with something like the following, with an unneeded first line and a now incorrect file name.

"""     turtle-example-suite:

             tdemo_bytedesign.py

I considered asking about backporting just the turtledemo changes, but it might be easier to write a program to make the changes and apply it to both main and 3.13 branches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-feature A feature request or enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants