Skip to content

test_runpy causes running all Python tests when run directly #75221

Open

Description

@serhiy-storchaka
BPO 31038
Nosy @brettcannon, @ncoghlan, @vstinner, @ericsnowcurrently, @serhiy-storchaka, @iritkatriel

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = None
created_at = <Date 2017-07-25.16:17:29.593>
labels = ['type-bug', 'tests', '3.9', '3.10', '3.11']
title = 'test_runpy causes running all Python tests when run directly'
updated_at = <Date 2022-01-16.18:38:48.803>
user = 'https://github.com/serhiy-storchaka'

bugs.python.org fields:

activity = <Date 2022-01-16.18:38:48.803>
actor = 'iritkatriel'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Tests']
creation = <Date 2017-07-25.16:17:29.593>
creator = 'serhiy.storchaka'
dependencies = []
files = []
hgrepos = []
issue_num = 31038
keywords = []
message_count = 4.0
messages = ['299118', '299119', '299236', '410710']
nosy_count = 6.0
nosy_names = ['brett.cannon', 'ncoghlan', 'vstinner', 'eric.snow', 'serhiy.storchaka', 'iritkatriel']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue31038'
versions = ['Python 3.9', 'Python 3.10', 'Python 3.11']

Linked PRs

Activity

  1. serhiy-storchaka commented on Jul 25, 2017

    @serhiy-storchaka
    MemberAuthor
    $ ./python Lib/test/test_runpy.py
    .......................== 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/test_runpy.py
    ..............................

    Ran 30 tests in 2.776s

    OK

  2. serhiy-storchaka commented on Jul 25, 2017

    @serhiy-storchaka
    MemberAuthor

    After commenting out methods test_directory_error, test_zipfile_error and test_main_recursion_error, test_runpy is passed.

  3. ncoghlan commented on Jul 26, 2017

    @ncoghlan
    Contributor

    This is a result of a known quirk in the way sys.path entry execution works: the search for "__main__.py" 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 __main__.py 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.

  4. iritkatriel commented on Jan 16, 2022

    @iritkatriel
    Member

    Reproduced on 3.11.

  5. added
    3.11only security fixes
    and removed on Jan 16, 2022
  6. transferred this issue fromon Apr 10, 2022
  7. added a commit that references this issue on Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    testsTests in the Lib/test dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions