This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: test_runpy causes running all Python tests when run directly
Type: behavior Stage:
Components: Tests Versions: Python 3.11, Python 3.10, Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: brett.cannon, eric.snow, iritkatriel, ncoghlan, serhiy.storchaka, vstinner
Priority: normal Keywords:

Created on 2017-07-25 16:17 by serhiy.storchaka, last changed 2022-04-11 14:58 by admin.

Messages (4)
msg299118 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-07-25 16:17
$ ./python Lib/test/
.......................== CPython 3.7.0a0 (heads/master-dirty:39243779f4, Jul 25 2017, 14:32:21) [GCC 6.3.0 20170406]
== Linux-4.10.0-28-generic-i686-athlon-with-debian-stretch-sid little-endian
== hash algorithm: siphash24 32bit
== cwd: /home/serhiy/py/cpython/build/test_python_31873
== CPU count: 2
== encodings: locale=UTF-8, FS=utf-8
Testing with flags: sys.flags(debug=0, inspect=0, interactive=0, optimize=0, dont_write_bytecode=0, no_user_site=0, no_site=0, ignore_environment=0, verbose=0, bytes_warning=0, quiet=0, hash_randomization=1, isolated=0)
Run tests sequentially
0:00:00 load avg: 0.85 [  1/406] test_grammar
0:00:00 load avg: 0.85 [  2/406] test_opcodes
0:00:00 load avg: 0.85 [  3/406] test_dict
0:00:01 load avg: 0.85 [  4/406] test_builtin
0:00:02 load avg: 0.85 [  5/406] test_exceptions
0:00:02 load avg: 0.85 [  6/406] test_types

In isolated mode it runs normally.

$ ./python -I Lib/test/
Ran 30 tests in 2.776s

msg299119 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-07-25 16:49
After commenting out methods test_directory_error, test_zipfile_error and test_main_recursion_error, test_runpy is passed.
msg299236 - (view) Author: Nick Coghlan (ncoghlan) * (Python committer) Date: 2017-07-26 14:45
This is a result of a known quirk in the way sys.path entry execution works: the search for "" isn't constrained specifically to sys.path[0].

That's almost entirely a bad thing, but I'd been ignoring it because I hadn't thought of a nice way of fixing it that didn't require some substantial changes to the import system APIs.

However, it just occurred to me that I may have been overcomplicating matters: we don't need to keep runpy from *finding* a from outside sys.path[0] in this case, we just need to keep it from *running* it.

That means that after we find the candidate module spec for __main__, we can introduce a new constraint:

    if os.path.commonpath([sys.path[0], spec.origin]) != sys.path[0]:
        raise RuntimeError(...)

It might still be a little fiddly to decide exactly when to enforce the constraint, but it should still be much easier than attempting to constrain the search for the spec directly.
msg410710 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-16 18:38
Reproduced on 3.11.
Date User Action Args
2022-04-11 14:58:49adminsetgithub: 75221
2022-01-16 18:38:48iritkatrielsetnosy: + iritkatriel

messages: + msg410710
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 2.7, Python 3.6, Python 3.7
2017-07-26 14:45:35ncoghlansetnosy: + brett.cannon, eric.snow
messages: + msg299236
2017-07-25 16:49:09serhiy.storchakasetnosy: + ncoghlan
messages: + msg299119
2017-07-25 16:17:29serhiy.storchakacreate