You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mitogen/mitogen
Alex Willmer 3a31a7d886 mitogen: Workaround CPython importlib PermissionError when cwd is unreadable
On macOS when using a become plugin as an unprivileged user, to another
unprivileged user it is likely that the current working directory can't be
read. In this case os.cwd() raises PermissionError.

On versions of Python currently in the wild (March 2024, CPython <= 3.13) if
any non-builtin or non-frozen module (e.g. zlib, base64) is imported then
`importlib._bootstrap_external.PathFinder._path_importer_cache()` attempts to
call os.cwd() without catching PermissionError.

The previous comment about needing an extra .encode() appears to be wrong,
atleast for Python 3.x >= 3.6.

Command size increased by 54 bytes, bootstrap by 804 bytes. Changed from
codecs module to binascii & zlib because they're extensions, and importing
them triggers fewer supporting imports (e.g. encodings module).

Before

```
✗ ./preamble_size.py
SSH command size: 705
Bootstrap (mitogen.core) size: 17078 (16.68KiB)

                              Original          Minimized           Compressed
mitogen.parent            97884 95.6KiB  50515 49.3KiB 51.6%  12727 12.4KiB
13.0%
mitogen.fork               8436  8.2KiB   4130  4.0KiB 49.0%   1648  1.6KiB
19.5%
mitogen.ssh               10892 10.6KiB   6952  6.8KiB 63.8%   2113  2.1KiB
19.4%
mitogen.sudo              12089 11.8KiB   5924  5.8KiB 49.0%   2249  2.2KiB
18.6%
mitogen.select            12325 12.0KiB   2929  2.9KiB 23.8%    964  0.9KiB
7.8%
mitogen.service           41699 40.7KiB  22477 22.0KiB 53.9%   5885  5.7KiB
14.1%
mitogen.fakessh           15577 15.2KiB   7989  7.8KiB 51.3%   2623  2.6KiB
16.8%
mitogen.master            51398 50.2KiB  25715 25.1KiB 50.0%   6886  6.7KiB
13.4%
```

After

```
✗ ./preamble_size.py
SSH command size: 759
Bootstrap (mitogen.core) size: 17882 (17.46KiB)

                              Original          Minimized           Compressed
mitogen.parent            98173 95.9KiB  50571 49.4KiB 51.5%  12747 12.4KiB
13.0%
mitogen.fork               8436  8.2KiB   4130  4.0KiB 49.0%   1648  1.6KiB
19.5%
mitogen.ssh               10892 10.6KiB   6952  6.8KiB 63.8%   2113  2.1KiB
19.4%
mitogen.sudo              12089 11.8KiB   5924  5.8KiB 49.0%   2249  2.2KiB
18.6%
mitogen.select            12325 12.0KiB   2929  2.9KiB 23.8%    964  0.9KiB
7.8%
mitogen.service           41699 40.7KiB  22477 22.0KiB 53.9%   5885  5.7KiB
14.1%
mitogen.fakessh           15577 15.2KiB   7989  7.8KiB 51.3%   2623  2.6KiB
16.8%
mitogen.master            56116 54.8KiB  29427 28.7KiB 52.4%   7627  7.4KiB
13.6%
```

Fixes #885
Refs https://github.com/python/cpython/issues/115911
3 months ago
..
compat compat: fix Py2.4 SyntaxError 5 years ago
__init__.py Bump version 11 months ago
buildah.py Remove unused module imports 2 years ago
core.py mitogen: Workaround CPython importlib PermissionError when cwd is unreadable 3 months ago
debug.py [stream-refactor] fix flake8 errors 5 years ago
doas.py remove unused imports flagged by lgtm 5 years ago
docker.py Refactor Stream, introduce quasi-asynchronous connect, much more 5 years ago
fakessh.py Fix lints found by flake8 2 years ago
fork.py Merge devel/290 @ 79b979ec8544ef5d8620c64068d4a42fabf50415 5 years ago
jail.py Stop using mitogen root logger in more modules, remove unused loggers 5 years ago
kubectl.py Remove unused module imports 2 years ago
lxc.py Remove unused module imports 2 years ago
lxd.py Remove unused module imports 2 years ago
master.py non functional: Add comments about imp module removal in Python 3.12 4 months ago
minify.py Normalize docstring formatting 5 years ago
os_fork.py mitogen: Handle Python 3.10 threading depreactions 2 years ago
parent.py mitogen: Workaround CPython importlib PermissionError when cwd is unreadable 3 months ago
podman.py Remove unused module imports 2 years ago
profiler.py Fix stdlib typos that would cause NameError or AttributeError exceptions 3 years ago
select.py Merge devel/290 @ 79b979ec8544ef5d8620c64068d4a42fabf50415 5 years ago
service.py docs: Fix generation of static website 10 months ago
setns.py Refactor Stream, introduce quasi-asynchronous connect, much more 5 years ago
ssh.py ssh: Match newer ssh host key prompt that accepts the fingerprint 4 years ago
su.py Stop using mitogen root logger in more modules, remove unused loggers 5 years ago
sudo.py reverted autolinted code 4 years ago
unix.py core: MitogenProtocol.is_privileged was not set in children 5 years ago
utils.py mitogen.utils: Preserve docstring of functions decorated @with_router 2 years ago