Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Update libraries and tests from CPython 3.10.6 #4064

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 23 commits into from
Aug 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update runpy from CPython 3.10.6
  • Loading branch information
CPython Developers authored and youknowone committed Aug 14, 2022
commit 628c4c0edf7a76d03749f6dd4c772e40bd3803cf
49 changes: 38 additions & 11 deletions 49 Lib/runpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import sys
import importlib.machinery # importlib first so we can test #15386 via -m
import importlib.util
import io
import types
from pkgutil import read_code, get_importer
import os

__all__ = [
"run_module", "run_path",
Expand Down Expand Up @@ -131,6 +132,9 @@ def _get_module_details(mod_name, error=ImportError):
# importlib, where the latter raises other errors for cases where
# pkgutil previously raised ImportError
msg = "Error while finding module specification for {!r} ({}: {})"
if mod_name.endswith(".py"):
msg += (f". Try using '{mod_name[:-3]}' instead of "
f"'{mod_name}' as the module name.")
raise error(msg.format(mod_name, type(ex).__name__, ex)) from ex
if spec is None:
raise error("No module named %s" % mod_name)
Expand Down Expand Up @@ -194,9 +198,24 @@ def _run_module_as_main(mod_name, alter_argv=True):

def run_module(mod_name, init_globals=None,
run_name=None, alter_sys=False):
"""Execute a module's code without importing it
"""Execute a module's code without importing it.

Returns the resulting top level namespace dictionary
mod_name -- an absolute module name or package name.

Optional arguments:
init_globals -- dictionary used to pre-populate the module’s
globals dictionary before the code is executed.

run_name -- if not None, this will be used for setting __name__;
otherwise, __name__ will be set to mod_name + '__main__' if the
named module is a package and to just mod_name otherwise.

alter_sys -- if True, sys.argv[0] is updated with the value of
__file__ and sys.modules[__name__] is updated with a temporary
module object for the module being executed. Both are
restored to their original values before the function returns.

Returns the resulting module globals dictionary.
"""
mod_name, mod_spec, code = _get_module_details(mod_name)
if run_name is None:
Expand Down Expand Up @@ -228,27 +247,35 @@ def _get_main_module_details(error=ImportError):

def _get_code_from_file(run_name, fname):
# Check for a compiled file first
with open(fname, "rb") as f:
from pkgutil import read_code
decoded_path = os.path.abspath(os.fsdecode(fname))
with io.open_code(decoded_path) as f:
code = read_code(f)
if code is None:
# That didn't work, so try it as normal source code
with open(fname, "rb") as f:
with io.open_code(decoded_path) as f:
code = compile(f.read(), fname, 'exec')
return code, fname

def run_path(path_name, init_globals=None, run_name=None):
"""Execute code located at the specified filesystem location
"""Execute code located at the specified filesystem location.

path_name -- filesystem location of a Python script, zipfile,
or directory containing a top level __main__.py script.

Optional arguments:
init_globals -- dictionary used to pre-populate the module’s
globals dictionary before the code is executed.

Returns the resulting top level namespace dictionary
run_name -- if not None, this will be used to set __name__;
otherwise, '<run_path>' will be used for __name__.

The file path may refer directly to a Python script (i.e.
one that could be directly executed with execfile) or else
it may refer to a zipfile or directory containing a top
level __main__.py script.
Returns the resulting module globals dictionary.
"""
if run_name is None:
run_name = "<run_path>"
pkg_name = run_name.rpartition(".")[0]
from pkgutil import get_importer
importer = get_importer(path_name)
# Trying to avoid importing imp so as to not consume the deprecation warning.
is_NullImporter = False
Expand Down
2 changes: 0 additions & 2 deletions 2 Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,6 @@ def test_dash_m_bad_pyc(self):
self.assertNotIn(b'is a package', err)
self.assertNotIn(b'Traceback', err)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_hint_when_triying_to_import_a_py_file(self):
with os_helper.temp_dir() as script_dir, \
os_helper.change_cwd(path=script_dir):
Expand Down
7 changes: 3 additions & 4 deletions 7 Lib/test/test_runpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
import textwrap
import unittest
import warnings
from test.support import verbose, no_tracing
from test.support import no_tracing, verbose
from test.support.import_helper import forget, make_legacy_pyc, unload
from test.support.os_helper import create_empty_file, temp_dir
from test.support.script_helper import make_script, make_zip_script
from test.support.import_helper import unload, forget, make_legacy_pyc


import runpy
Expand Down Expand Up @@ -745,8 +745,7 @@ def test_main_recursion_error(self):
"runpy.run_path(%r)\n") % dummy_dir
script_name = self._make_test_script(script_dir, mod_name, source)
zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
msg = "recursion depth exceeded"
self.assertRaisesRegex(RecursionError, msg, run_path, zip_name)
self.assertRaises(RecursionError, run_path, zip_name)

# TODO: RUSTPYTHON, detect encoding comments in files
@unittest.expectedFailure
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.