Skip to content

Commit cc4ebe6

Browse files
authored
Update{runpy,numbers}.py from 3.13.7 (RustPython#6141)
* Update number.py from 3.13.7 * Update `runpy.py` from 3.13.7
1 parent f429ac4 commit cc4ebe6

File tree

3 files changed

+63
-44
lines changed

3 files changed

+63
-44
lines changed

Lib/numbers.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,27 @@ def conjugate(self):
290290

291291

292292
class Rational(Real):
293-
""".numerator and .denominator should be in lowest terms."""
293+
"""To Real, Rational adds numerator and denominator properties.
294+
295+
The numerator and denominator values should be in lowest terms,
296+
with a positive denominator.
297+
"""
294298

295299
__slots__ = ()
296300

297301
@property
298302
@abstractmethod
299303
def numerator(self):
304+
"""The numerator of a rational number in lowest terms."""
300305
raise NotImplementedError
301306

302307
@property
303308
@abstractmethod
304309
def denominator(self):
310+
"""The denominator of a rational number in lowest terms.
311+
312+
This denominator should be positive.
313+
"""
305314
raise NotImplementedError
306315

307316
# Concrete implementation of Real's conversion to float.

Lib/runpy.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414
import importlib.machinery # importlib first so we can test #15386 via -m
1515
import importlib.util
1616
import io
17-
import types
1817
import os
1918

2019
__all__ = [
2120
"run_module", "run_path",
2221
]
2322

23+
# avoid 'import types' just for ModuleType
24+
ModuleType = type(sys)
25+
2426
class _TempModule(object):
2527
"""Temporarily replace a module in sys.modules with an empty namespace"""
2628
def __init__(self, mod_name):
2729
self.mod_name = mod_name
28-
self.module = types.ModuleType(mod_name)
30+
self.module = ModuleType(mod_name)
2931
self._saved_module = []
3032

3133
def __enter__(self):
@@ -245,17 +247,17 @@ def _get_main_module_details(error=ImportError):
245247
sys.modules[main_name] = saved_main
246248

247249

248-
def _get_code_from_file(run_name, fname):
250+
def _get_code_from_file(fname):
249251
# Check for a compiled file first
250252
from pkgutil import read_code
251-
decoded_path = os.path.abspath(os.fsdecode(fname))
252-
with io.open_code(decoded_path) as f:
253+
code_path = os.path.abspath(fname)
254+
with io.open_code(code_path) as f:
253255
code = read_code(f)
254256
if code is None:
255257
# That didn't work, so try it as normal source code
256-
with io.open_code(decoded_path) as f:
258+
with io.open_code(code_path) as f:
257259
code = compile(f.read(), fname, 'exec')
258-
return code, fname
260+
return code
259261

260262
def run_path(path_name, init_globals=None, run_name=None):
261263
"""Execute code located at the specified filesystem location.
@@ -277,17 +279,13 @@ def run_path(path_name, init_globals=None, run_name=None):
277279
pkg_name = run_name.rpartition(".")[0]
278280
from pkgutil import get_importer
279281
importer = get_importer(path_name)
280-
# Trying to avoid importing imp so as to not consume the deprecation warning.
281-
is_NullImporter = False
282-
if type(importer).__module__ == 'imp':
283-
if type(importer).__name__ == 'NullImporter':
284-
is_NullImporter = True
285-
if isinstance(importer, type(None)) or is_NullImporter:
282+
path_name = os.fsdecode(path_name)
283+
if isinstance(importer, type(None)):
286284
# Not a valid sys.path entry, so run the code directly
287285
# execfile() doesn't help as we want to allow compiled files
288-
code, fname = _get_code_from_file(run_name, path_name)
286+
code = _get_code_from_file(path_name)
289287
return _run_module_code(code, init_globals, run_name,
290-
pkg_name=pkg_name, script_name=fname)
288+
pkg_name=pkg_name, script_name=path_name)
291289
else:
292290
# Finder is defined for path, so add it to
293291
# the start of sys.path

Lib/test/test_runpy.py

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,16 @@
1212
import textwrap
1313
import unittest
1414
import warnings
15-
from test.support import no_tracing, verbose
15+
from test.support import (
16+
force_not_colorized_test_class,
17+
infinite_recursion,
18+
no_tracing,
19+
requires_resource,
20+
requires_subprocess,
21+
verbose,
22+
)
1623
from test.support.import_helper import forget, make_legacy_pyc, unload
17-
from test.support.os_helper import create_empty_file, temp_dir
24+
from test.support.os_helper import create_empty_file, temp_dir, FakePath
1825
from test.support.script_helper import make_script, make_zip_script
1926

2027

@@ -656,13 +663,14 @@ def test_basic_script(self):
656663
self._check_script(script_name, "<run_path>", script_name,
657664
script_name, expect_spec=False)
658665

659-
def test_basic_script_with_path_object(self):
666+
def test_basic_script_with_pathlike_object(self):
660667
with temp_dir() as script_dir:
661668
mod_name = 'script'
662-
script_name = pathlib.Path(self._make_test_script(script_dir,
663-
mod_name))
664-
self._check_script(script_name, "<run_path>", script_name,
665-
script_name, expect_spec=False)
669+
script_name = self._make_test_script(script_dir, mod_name)
670+
self._check_script(FakePath(script_name), "<run_path>",
671+
script_name,
672+
script_name,
673+
expect_spec=False)
666674

667675
def test_basic_script_no_suffix(self):
668676
with temp_dir() as script_dir:
@@ -734,17 +742,18 @@ def test_zipfile_error(self):
734742
self._check_import_error(zip_name, msg)
735743

736744
@no_tracing
745+
@requires_resource('cpu')
737746
def test_main_recursion_error(self):
738747
with temp_dir() as script_dir, temp_dir() as dummy_dir:
739748
mod_name = '__main__'
740749
source = ("import runpy\n"
741750
"runpy.run_path(%r)\n") % dummy_dir
742751
script_name = self._make_test_script(script_dir, mod_name, source)
743752
zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
744-
self.assertRaises(RecursionError, run_path, zip_name)
753+
with infinite_recursion(25):
754+
self.assertRaises(RecursionError, run_path, zip_name)
745755

746-
# TODO: RUSTPYTHON, detect encoding comments in files
747-
@unittest.expectedFailure
756+
@unittest.expectedFailure # TODO: RUSTPYTHON; detect encoding comments in files
748757
def test_encoding(self):
749758
with temp_dir() as script_dir:
750759
filename = os.path.join(script_dir, 'script.py')
@@ -757,6 +766,7 @@ def test_encoding(self):
757766
self.assertEqual(result['s'], "non-ASCII: h\xe9")
758767

759768

769+
@force_not_colorized_test_class
760770
class TestExit(unittest.TestCase):
761771
STATUS_CONTROL_C_EXIT = 0xC000013A
762772
EXPECTED_CODE = (
@@ -783,16 +793,19 @@ def run(self, *args, **kwargs):
783793
)
784794
super().run(*args, **kwargs)
785795

786-
def assertSigInt(self, *args, **kwargs):
787-
proc = subprocess.run(*args, **kwargs, text=True, stderr=subprocess.PIPE)
788-
self.assertTrue(proc.stderr.endswith("\nKeyboardInterrupt\n"))
796+
@requires_subprocess()
797+
def assertSigInt(self, cmd, *args, **kwargs):
798+
# Use -E to ignore PYTHONSAFEPATH
799+
cmd = [sys.executable, '-E', *cmd]
800+
proc = subprocess.run(cmd, *args, **kwargs, text=True, stderr=subprocess.PIPE)
801+
self.assertTrue(proc.stderr.endswith("\nKeyboardInterrupt\n"), proc.stderr)
789802
self.assertEqual(proc.returncode, self.EXPECTED_CODE)
790803

791-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
804+
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
792805
def test_pymain_run_file(self):
793-
self.assertSigInt([sys.executable, self.ham])
806+
self.assertSigInt([self.ham])
794807

795-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
808+
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
796809
def test_pymain_run_file_runpy_run_module(self):
797810
tmp = self.ham.parent
798811
run_module = tmp / "run_module.py"
@@ -804,9 +817,9 @@ def test_pymain_run_file_runpy_run_module(self):
804817
"""
805818
)
806819
)
807-
self.assertSigInt([sys.executable, run_module], cwd=tmp)
820+
self.assertSigInt([run_module], cwd=tmp)
808821

809-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
822+
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
810823
def test_pymain_run_file_runpy_run_module_as_main(self):
811824
tmp = self.ham.parent
812825
run_module_as_main = tmp / "run_module_as_main.py"
@@ -818,28 +831,27 @@ def test_pymain_run_file_runpy_run_module_as_main(self):
818831
"""
819832
)
820833
)
821-
self.assertSigInt([sys.executable, run_module_as_main], cwd=tmp)
834+
self.assertSigInt([run_module_as_main], cwd=tmp)
822835

823-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
836+
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
824837
def test_pymain_run_command_run_module(self):
825838
self.assertSigInt(
826-
[sys.executable, "-c", "import runpy; runpy.run_module('ham')"],
839+
["-c", "import runpy; runpy.run_module('ham')"],
827840
cwd=self.ham.parent,
828841
)
829842

830-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
843+
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
831844
def test_pymain_run_command(self):
832-
self.assertSigInt([sys.executable, "-c", "import ham"], cwd=self.ham.parent)
845+
self.assertSigInt(["-c", "import ham"], cwd=self.ham.parent)
833846

834-
# TODO: RUSTPYTHON
835-
@unittest.expectedFailure
847+
@unittest.expectedFailure # TODO: RUSTPYTHON
836848
def test_pymain_run_stdin(self):
837-
self.assertSigInt([sys.executable], input="import ham", cwd=self.ham.parent)
849+
self.assertSigInt([], input="import ham", cwd=self.ham.parent)
838850

839-
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON")
851+
@unittest.expectedFailureIfWindows("TODO: RUSTPYTHON; ")
840852
def test_pymain_run_module(self):
841853
ham = self.ham
842-
self.assertSigInt([sys.executable, "-m", ham.stem], cwd=ham.parent)
854+
self.assertSigInt(["-m", ham.stem], cwd=ham.parent)
843855

844856

845857
if __name__ == "__main__":

0 commit comments

Comments
 (0)