Update{runpy,numbers}.py from 3.13.7 (#6141)

* Update number.py from 3.13.7

* Update `runpy.py` from 3.13.7
This commit is contained in:
Shahar Naveh authored and GitHub committed 2025-09-11 22:43:11 +09:00
1 parent f429ac4939
commit cc4ebe6256
3 files changed
+63 -44

No files matched your search

+10 -1
View File
@@ -290,18 +290,27 @@ Real.register(float)
class Rational(Real):
""".numerator and .denominator should be in lowest terms."""
"""To Real, Rational adds numerator and denominator properties.
The numerator and denominator values should be in lowest terms,
with a positive denominator.
"""
__slots__ = ()
@property
@abstractmethod
def numerator(self):
"""The numerator of a rational number in lowest terms."""
raise NotImplementedError
@property
@abstractmethod
def denominator(self):
"""The denominator of a rational number in lowest terms.
This denominator should be positive.
"""
raise NotImplementedError
# Concrete implementation of Real's conversion to float.
+13 -15
View File
@@ -14,18 +14,20 @@ import sys
import importlib.machinery # importlib first so we can test #15386 via -m
import importlib.util
import io
import types
import os
__all__ = [
"run_module", "run_path",
]
# avoid 'import types' just for ModuleType
ModuleType = type(sys)
class _TempModule(object):
"""Temporarily replace a module in sys.modules with an empty namespace"""
def __init__(self, mod_name):
self.mod_name = mod_name
self.module = types.ModuleType(mod_name)
self.module = ModuleType(mod_name)
self._saved_module = []
def __enter__(self):
@@ -245,17 +247,17 @@ def _get_main_module_details(error=ImportError):
sys.modules[main_name] = saved_main
def _get_code_from_file(run_name, fname):
def _get_code_from_file(fname):
# Check for a compiled file first
from pkgutil import read_code
decoded_path = os.path.abspath(os.fsdecode(fname))
with io.open_code(decoded_path) as f:
code_path = os.path.abspath(fname)
with io.open_code(code_path) as f:
code = read_code(f)
if code is None:
# That didn't work, so try it as normal source code
with io.open_code(decoded_path) as f:
with io.open_code(code_path) as f:
code = compile(f.read(), fname, 'exec')
return code, fname
return code
def run_path(path_name, init_globals=None, run_name=None):
"""Execute code located at the specified filesystem location.
@@ -277,17 +279,13 @@ def run_path(path_name, init_globals=None, run_name=None):
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
if type(importer).__module__ == 'imp':
if type(importer).__name__ == 'NullImporter':
is_NullImporter = True
if isinstance(importer, type(None)) or is_NullImporter:
path_name = os.fsdecode(path_name)
if isinstance(importer, type(None)):
# Not a valid sys.path entry, so run the code directly
# execfile() doesn't help as we want to allow compiled files
code, fname = _get_code_from_file(run_name, path_name)
code = _get_code_from_file(path_name)
return _run_module_code(code, init_globals, run_name,
pkg_name=pkg_name, script_name=fname)
pkg_name=pkg_name, script_name=path_name)
else:
# Finder is defined for path, so add it to
# the start of sys.path
+40 -28
View File
@@ -12,9 +12,16 @@ import tempfile
import textwrap
import unittest
import warnings
from test.support import no_tracing, verbose
from test.support import (
force_not_colorized_test_class,
infinite_recursion,
no_tracing,
requires_resource,
requires_subprocess,
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.os_helper import create_empty_file, temp_dir, FakePath
from test.support.script_helper import make_script, make_zip_script
@@ -656,13 +663,14 @@ class RunPathTestCase(unittest.TestCase, CodeExecutionMixin):
self._check_script(script_name, "<run_path>", script_name,
script_name, expect_spec=False)
def test_basic_script_with_path_object(self):
def test_basic_script_with_pathlike_object(self):
with temp_dir() as script_dir:
mod_name = 'script'
script_name = pathlib.Path(self._make_test_script(script_dir,
mod_name))
self._check_script(script_name, "<run_path>", script_name,
script_name, expect_spec=False)
script_name = self._make_test_script(script_dir, mod_name)
self._check_script(FakePath(script_name), "<run_path>",
script_name,
script_name,
expect_spec=False)
def test_basic_script_no_suffix(self):
with temp_dir() as script_dir:
@@ -734,6 +742,7 @@ class RunPathTestCase(unittest.TestCase, CodeExecutionMixin):
self._check_import_error(zip_name, msg)
@no_tracing
@requires_resource('cpu')
def test_main_recursion_error(self):
with temp_dir() as script_dir, temp_dir() as dummy_dir:
mod_name = '__main__'
@@ -741,10 +750,10 @@ class RunPathTestCase(unittest.TestCase, CodeExecutionMixin):
"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)
self.assertRaises(RecursionError, run_path, zip_name)
with infinite_recursion(25):
self.assertRaises(RecursionError, run_path, zip_name)
# TODO: RUSTPYTHON, detect encoding comments in files
@unittest.expectedFailure
@unittest.expectedFailure # TODO: RUSTPYTHON; detect encoding comments in files
def test_encoding(self):
with temp_dir() as script_dir:
filename = os.path.join(script_dir, 'script.py')
@@ -757,6 +766,7 @@ s = "non-ASCII: h\xe9"
self.assertEqual(result['s'], "non-ASCII: h\xe9")
@force_not_colorized_test_class
class TestExit(unittest.TestCase):
STATUS_CONTROL_C_EXIT = 0xC000013A
EXPECTED_CODE = (
@@ -783,16 +793,19 @@ class TestExit(unittest.TestCase):
)
super().run(*args, **kwargs)
def assertSigInt(self, *args, **kwargs):
proc = subprocess.run(*args, **kwargs, text=True, stderr=subprocess.PIPE)
self.assertTrue(proc.stderr.endswith("\nKeyboardInterrupt\n"))
@requires_subprocess()
def assertSigInt(self, cmd, *args, **kwargs):
# Use -E to ignore PYTHONSAFEPATH
cmd = [sys.executable, '-E', *cmd]
proc = subprocess.run(cmd, *args, **kwargs, text=True, stderr=subprocess.PIPE)
self.assertTrue(proc.stderr.endswith("\nKeyboardInterrupt\n"), proc.stderr)
self.assertEqual(proc.returncode, self.EXPECTED_CODE)
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
def test_pymain_run_file(self):
self.assertSigInt([sys.executable, self.ham])
self.assertSigInt([self.ham])
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
def test_pymain_run_file_runpy_run_module(self):
tmp = self.ham.parent
run_module = tmp / "run_module.py"
@@ -804,9 +817,9 @@ class TestExit(unittest.TestCase):
"""
)
)
self.assertSigInt([sys.executable, run_module], cwd=tmp)
self.assertSigInt([run_module], cwd=tmp)
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
def test_pymain_run_file_runpy_run_module_as_main(self):
tmp = self.ham.parent
run_module_as_main = tmp / "run_module_as_main.py"
@@ -818,28 +831,27 @@ class TestExit(unittest.TestCase):
"""
)
)
self.assertSigInt([sys.executable, run_module_as_main], cwd=tmp)
self.assertSigInt([run_module_as_main], cwd=tmp)
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
def test_pymain_run_command_run_module(self):
self.assertSigInt(
[sys.executable, "-c", "import runpy; runpy.run_module('ham')"],
["-c", "import runpy; runpy.run_module('ham')"],
cwd=self.ham.parent,
)
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
def test_pymain_run_command(self):
self.assertSigInt([sys.executable, "-c", "import ham"], cwd=self.ham.parent)
self.assertSigInt(["-c", "import ham"], cwd=self.ham.parent)
# TODO: RUSTPYTHON
@unittest.expectedFailure
@unittest.expectedFailure # TODO: RUSTPYTHON
def test_pymain_run_stdin(self):
self.assertSigInt([sys.executable], input="import ham", cwd=self.ham.parent)
self.assertSigInt([], input="import ham", cwd=self.ham.parent)
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
def test_pymain_run_module(self):
ham = self.ham
self.assertSigInt([sys.executable, "-m", ham.stem], cwd=ham.parent)
self.assertSigInt(["-m", ham.stem], cwd=ham.parent)
if __name__ == "__main__":