|
25 | 25 |
|
26 | 26 | from test.support import os_helper
|
27 | 27 | from test.support import (
|
28 |
| - STDLIB_DIR, swap_attr, swap_item, cpython_only, is_emscripten, |
| 28 | + STDLIB_DIR, TEST_HOME_DIR, swap_attr, swap_item, cpython_only, is_emscripten, |
29 | 29 | is_wasi, run_in_subinterp, run_in_subinterp_with_config, Py_TRACE_REFS)
|
30 | 30 | from test.support.import_helper import (
|
31 | 31 | forget, make_legacy_pyc, unlink, unload, ready_to_import,
|
@@ -408,11 +408,6 @@ def test_case_sensitivity(self):
|
408 | 408 | with self.assertRaises(ImportError):
|
409 | 409 | import RAnDoM
|
410 | 410 |
|
411 |
| - def test_double_const(self): |
412 |
| - # Another brief digression to test the accuracy of manifest float |
413 |
| - # constants. |
414 |
| - from test.test_import.data import double_const # don't blink -- that *was* the test |
415 |
| - |
416 | 411 | def test_import(self):
|
417 | 412 | def test_with_extension(ext):
|
418 | 413 | # The extension is normally ".py", perhaps ".pyw".
|
@@ -877,6 +872,33 @@ def test_pyc_always_writable(self):
|
877 | 872 | m = __import__(name)
|
878 | 873 | self.assertEqual(m.x, 'rewritten')
|
879 | 874 |
|
| 875 | + def test_double_const(self): |
| 876 | + # Importing double_const checks that float constants |
| 877 | + # serialiazed by marshal as PYC files don't lose precision |
| 878 | + # (SF bug 422177). |
| 879 | + filepath = os.path.join( |
| 880 | + TEST_HOME_DIR, |
| 881 | + 'test_import', |
| 882 | + 'data', |
| 883 | + 'double_const.py', |
| 884 | + ) |
| 885 | + |
| 886 | + with open(filepath, 'r', encoding='utf8') as f: |
| 887 | + source = f.read() |
| 888 | + |
| 889 | + with ready_to_import(source=source) as (name, path): |
| 890 | + # Initial import should be fine: |
| 891 | + __import__(name) |
| 892 | + |
| 893 | + # Now, delete source file, only keep `.pyc` file and import again: |
| 894 | + unlink(path) |
| 895 | + unload(name) |
| 896 | + importlib.invalidate_caches() |
| 897 | + |
| 898 | + bytecode_only = path + 'c' |
| 899 | + os.rename(importlib.util.cache_from_source(path), bytecode_only) |
| 900 | + __import__(name) |
| 901 | + |
880 | 902 |
|
881 | 903 | class PycRewritingTests(unittest.TestCase):
|
882 | 904 | # Test that the `co_filename` attribute on code objects always points
|
|
0 commit comments