From 106c2689d6529c58dbe9684f10cd6d1797eea459 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 15 Nov 2023 10:13:58 +0300 Subject: [PATCH 1/5] gh-108303: Move `double_const` to `test_import` where it belongs --- Lib/test/test_import/__init__.py | 2 +- Lib/test/{ => test_import/data}/double_const.py | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename Lib/test/{ => test_import/data}/double_const.py (100%) diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index aa465c70dfbcd0..9f67a7793b6a23 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -411,7 +411,7 @@ def test_case_sensitivity(self): def test_double_const(self): # Another brief digression to test the accuracy of manifest float # constants. - from test import double_const # don't blink -- that *was* the test + from test.test_import.data import double_const # don't blink -- that *was* the test def test_import(self): def test_with_extension(ext): diff --git a/Lib/test/double_const.py b/Lib/test/test_import/data/double_const.py similarity index 100% rename from Lib/test/double_const.py rename to Lib/test/test_import/data/double_const.py From 23319d90fe0489466543b6c816b7f96f714ba244 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 15 Nov 2023 21:13:02 +0300 Subject: [PATCH 2/5] Address review --- Lib/test/test_import/__init__.py | 34 ++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index 9f67a7793b6a23..1206e5609f3aa0 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -25,7 +25,7 @@ from test.support import os_helper from test.support import ( - STDLIB_DIR, swap_attr, swap_item, cpython_only, is_emscripten, + STDLIB_DIR, TEST_HOME_DIR, swap_attr, swap_item, cpython_only, is_emscripten, is_wasi, run_in_subinterp, run_in_subinterp_with_config, Py_TRACE_REFS) from test.support.import_helper import ( forget, make_legacy_pyc, unlink, unload, ready_to_import, @@ -408,11 +408,6 @@ def test_case_sensitivity(self): with self.assertRaises(ImportError): import RAnDoM - def test_double_const(self): - # Another brief digression to test the accuracy of manifest float - # constants. - from test.test_import.data import double_const # don't blink -- that *was* the test - def test_import(self): def test_with_extension(ext): # The extension is normally ".py", perhaps ".pyw". @@ -877,6 +872,33 @@ def test_pyc_always_writable(self): m = __import__(name) self.assertEqual(m.x, 'rewritten') + def test_double_const(self): + # Importing double_const checks that float constants + # serialiazed by marshal as PYC files don't lose precision + # (SF bug 422177). + filepath = os.path.join( + TEST_HOME_DIR, + 'test_import', + 'data', + 'double_const.py', + ) + + with open(filepath, 'r', encoding='utf8') as f: + source = f.read() + + with ready_to_import(source=source) as (name, path): + # Initial import should be fine: + __import__(name) + + # Now, delete source file, only keep `.pyc` file and import again: + unlink(path) + unload(name) + importlib.invalidate_caches() + + bytecode_only = path + 'c' + os.rename(importlib.util.cache_from_source(path), bytecode_only) + __import__(name) + class PycRewritingTests(unittest.TestCase): # Test that the `co_filename` attribute on code objects always points From 0af9fb6ff78ebe7dcda004aff4ee235f8c6d54d2 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Wed, 15 Nov 2023 23:47:34 +0300 Subject: [PATCH 3/5] Simplify import --- Lib/test/test_import/__init__.py | 25 +++---------------------- 1 file changed, 3 insertions(+), 22 deletions(-) diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index 1206e5609f3aa0..f1ccb66225ca18 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -876,28 +876,9 @@ def test_double_const(self): # Importing double_const checks that float constants # serialiazed by marshal as PYC files don't lose precision # (SF bug 422177). - filepath = os.path.join( - TEST_HOME_DIR, - 'test_import', - 'data', - 'double_const.py', - ) - - with open(filepath, 'r', encoding='utf8') as f: - source = f.read() - - with ready_to_import(source=source) as (name, path): - # Initial import should be fine: - __import__(name) - - # Now, delete source file, only keep `.pyc` file and import again: - unlink(path) - unload(name) - importlib.invalidate_caches() - - bytecode_only = path + 'c' - os.rename(importlib.util.cache_from_source(path), bytecode_only) - __import__(name) + from test.test_import.data import double_const + unload('test.test_import.data.double_const') + from test.test_import.data import double_const class PycRewritingTests(unittest.TestCase): From 80be93a11ff770d1a1637d3cfb656ad739896a32 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Sat, 18 Nov 2023 09:43:50 +0300 Subject: [PATCH 4/5] Update __init__.py --- Lib/test/test_import/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index f1ccb66225ca18..254c328e6bafff 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -25,7 +25,7 @@ from test.support import os_helper from test.support import ( - STDLIB_DIR, TEST_HOME_DIR, swap_attr, swap_item, cpython_only, is_emscripten, + STDLIB_DIR, swap_attr, swap_item, cpython_only, is_emscripten, is_wasi, run_in_subinterp, run_in_subinterp_with_config, Py_TRACE_REFS) from test.support.import_helper import ( forget, make_legacy_pyc, unlink, unload, ready_to_import, From 6d5b4f70336026bd31c3b24e67fba946a78acf66 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Sat, 2 Dec 2023 13:00:31 +0300 Subject: [PATCH 5/5] Address review --- Lib/test/test_import/__init__.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_import/__init__.py b/Lib/test/test_import/__init__.py index f1ccb66225ca18..029a7666ffe0a1 100644 --- a/Lib/test/test_import/__init__.py +++ b/Lib/test/test_import/__init__.py @@ -408,6 +408,14 @@ def test_case_sensitivity(self): with self.assertRaises(ImportError): import RAnDoM + def test_double_const(self): + # Importing double_const checks that float constants + # serialiazed by marshal as PYC files don't lose precision + # (SF bug 422177). + from test.test_import.data import double_const + unload('test.test_import.data.double_const') + from test.test_import.data import double_const + def test_import(self): def test_with_extension(ext): # The extension is normally ".py", perhaps ".pyw". @@ -872,14 +880,6 @@ def test_pyc_always_writable(self): m = __import__(name) self.assertEqual(m.x, 'rewritten') - def test_double_const(self): - # Importing double_const checks that float constants - # serialiazed by marshal as PYC files don't lose precision - # (SF bug 422177). - from test.test_import.data import double_const - unload('test.test_import.data.double_const') - from test.test_import.data import double_const - class PycRewritingTests(unittest.TestCase): # Test that the `co_filename` attribute on code objects always points