Skip to content

Commit 700b15f

Browse files
freakboy3742timrid
andcommitted
Add a test of .pth handling.
Co-authored-by: Tim Rid <6593626+timrid@users.noreply.github.com>
1 parent 76c16e6 commit 700b15f

6 files changed

Lines changed: 79 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
__pycache__/
22
.idea
33
*.dist-info/
4+
*.egg-info/
45
build/
56
logs/
67
.DS_Store

pth-tester/pth_tester.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
initialized = False
2+
3+
4+
# The pth_tester module should be initalized by processing the `.pth` file
5+
# created on installation.
6+
def init():
7+
global initialized
8+
initialized = True

pth-tester/pyproject.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[build-system]
2+
requires = ["setuptools==78.0.2", "wheel"]
3+
build-backend = "setuptools.build_meta"
4+
5+
[project]
6+
name = "x-pth-tester"
7+
version = "1.0.0"
8+
classifiers = ["Private :: Do Not Upload"]

pth-tester/setup.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
3+
import setuptools
4+
from setuptools.command.install import install
5+
6+
7+
# Copied from setuptools:
8+
# (https://github.com/pypa/setuptools/blob/7c859e017368360ba66c8cc591279d8964c031bc/setup.py#L40C6-L82)
9+
class install_with_pth(install):
10+
"""
11+
Custom install command to install a .pth file.
12+
13+
This hack is necessary because there's no standard way to install behavior
14+
on startup (and it's debatable if there should be one). This hack (ab)uses
15+
the `extra_path` behavior in Setuptools to install a `.pth` file with
16+
implicit behavior on startup.
17+
18+
The original source strongly recommends against using this behavior.
19+
"""
20+
21+
_pth_name = "_pth_tester"
22+
_pth_contents = "import pth_tester; pth_tester.init()"
23+
24+
def initialize_options(self):
25+
install.initialize_options(self)
26+
self.extra_path = self._pth_name, self._pth_contents
27+
28+
def finalize_options(self):
29+
install.finalize_options(self)
30+
self._restore_install_lib()
31+
32+
def _restore_install_lib(self):
33+
"""
34+
Undo secondary effect of `extra_path` adding to `install_lib`
35+
"""
36+
suffix = os.path.relpath(self.install_lib, self.install_libbase)
37+
38+
if suffix.strip() == self._pth_contents.strip():
39+
self.install_lib = self.install_libbase
40+
41+
42+
setuptools.setup(
43+
cmdclass={"install": install_with_pth},
44+
)

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ sources = ["src/testbed"]
1919
test_sources = ["tests"]
2020

2121
requires = [
22+
# Android can't process .pth files (see chaquo/chaquopy#1338). This also
23+
# means we can only test Linux on Python3.13+, because Android reports as
24+
# "Linux" on Python3.12 and earlier.
25+
"""./pth-tester; \
26+
(platform_system != 'Linux' and platform_system != 'Android') \
27+
or (platform_system == 'Linux' and python_version >= '3.13')""",
2228
# Cryptography provides an ABI3 wheel for all desktop platforms, but requires cffi which doesn't.
2329
"""cryptography; \
2430
(platform_system != 'iOS' and platform_system != 'Android' and python_version < '3.14') \

tests/test_common.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
import pytest
99

10+
from .test_thirdparty import xfail_if_not_installed
11+
1012

1113
def test_bootstrap_modules():
1214
"All the bootstrap modules are importable"
@@ -444,3 +446,13 @@ def test_zoneinfo():
444446

445447
dt = datetime(2022, 5, 4, 13, 40, 42, tzinfo=ZoneInfo("Australia/Perth"))
446448
assert str(dt) == "2022-05-04 13:40:42+08:00"
449+
450+
451+
@xfail_if_not_installed("x-pth-tester")
452+
def test_pth_handling():
453+
".pth files installed by a package are processed"
454+
import pth_tester
455+
456+
# The pth_tester module should be "initialized" as a result of
457+
# processing the .pth file created when the package is installed.
458+
assert pth_tester.initialized

0 commit comments

Comments
 (0)