Skip to content

test_site _pth file tests fail when stdlib path is very long #113628

Closed
@itamaro

Description

@itamaro

Bug report

Bug description:

The getpath module tries to read certain _pth files during initialization).

This is tested in test_site with generated _pth files that include the stdlib path 200 times.

The getpath module disallows reading files over 32KB during initialization.

If the test suite runs from a very long base path, 200 repetitions of the stdlib path in the _pth file would be enough to exceed the 32KB limit.

To demonstrate, artificially increase the number of repetitions to some high number that would exceed 32KB, e.g. this patch:

diff --git a/Lib/test/test_site.py b/Lib/test/test_site.py
index 33d0975bda8..ba2f7d90a80 100644
--- a/Lib/test/test_site.py
+++ b/Lib/test/test_site.py
@@ -668,7 +668,7 @@ def test_underpth_nosite_file(self):
         exe_prefix = os.path.dirname(sys.executable)
         pth_lines = [
             'fake-path-name',
-            *[libpath for _ in range(200)],
+            *[libpath for _ in range(5000)],
             '',
             '# comment',
         ]

and observe the test failing with the following error:

$ ./python.exe -m test test_site -v -m '*_pthFileTests.test_underpth_nosite_file'
== CPython 3.13.0a2+ (heads/main-dirty:b4b2cc10121, Jan 1 2024, 11:38:27) [Clang 15.0.0 (clang-1500.1.0.2.5)]
== macOS-14.2.1-arm64-arm-64bit little-endian
== Python build: debug
== cwd: /Users/itamaro/work/pyexe/main-dbg/build/test_python_worker_96404æ
== CPU count: 12
== encodings: locale=UTF-8 FS=utf-8
== resources: all test resources are disabled, use -u option to unskip tests

Using random seed: 4200167786
0:00:00 load avg: 8.99 Run 1 test sequentially
0:00:00 load avg: 8.99 [1/1] test_site
test_underpth_nosite_file (test.test_site._pthFileTests.test_underpth_nosite_file) ... Exception ignored in running getpath:
Traceback (most recent call last):
  File "<frozen getpath>", line 462, in <module>
MemoryError: cannot read file larger than 32KB during initialization
Fatal Python error: error evaluating path
Python runtime state: core initialized

Current thread 0x00000001e0e39000 (most recent call first):
  <no Python frame>
ERROR

======================================================================
ERROR: test_underpth_nosite_file (test.test_site._pthFileTests.test_underpth_nosite_file)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/itamaro/work/cpython/Lib/test/test_site.py", line 683, in test_underpth_nosite_file
    output = subprocess.check_output([exe_file, '-c',
             ~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^
        'import sys; print("\\n".join(sys.path) if sys.flags.no_site else "")'
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ], env=env, encoding='utf-8', errors='surrogateescape')
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/itamaro/work/cpython/Lib/subprocess.py", line 470, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
           ~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
               **kwargs).stdout
               ^^^^^^^^^
  File "/Users/itamaro/work/cpython/Lib/subprocess.py", line 575, in run
    raise CalledProcessError(retcode, process.args,
                             output=stdout, stderr=stderr)
subprocess.CalledProcessError: Command '['/var/folders/ng/hmf622ys3wnc3c21prtx7nd80000gn/T/tmpkc8z54mu/python.exe', '-c', 'import sys; print("\\n".join(sys.path) if sys.flags.no_site else "")']' returned non-zero exit status 1.

----------------------------------------------------------------------
Ran 1 test in 0.032s

FAILED (errors=1)
test test_site failed
test_site failed (1 error)

== Tests result: FAILURE ==

1 test failed:
    test_site

Total duration: 150 ms
Total tests: run=1 (filtered)
Total test files: run=1/1 (filtered) failed=1
Result: FAILURE

CPython versions tested on:

3.12, 3.13, CPython main branch

Operating systems tested on:

Linux, macOS

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixesstdlibPython modules in the Lib dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions