Skip to content

Commit f0c4d6d

Browse files
committed
Add canonical test-method imports and ImportError repr coverage
Extend copy-test with an explicit, hash-pinned absent-only tail insertion mode. Require unchanged class context and dependencies, preserve unrelated bytes, and acknowledge local imports rather than guessing their availability. Use the official importer for two unchanged CPython 3.15 ImportError repr tests covering existing RustPython behavior. Preserve all existing markers and unrelated test definitions. Assisted-by: OpenAI Codex: model version unavailable
1 parent ef6e2db commit f0c4d6d

4 files changed

Lines changed: 719 additions & 3 deletions

File tree

‎Lib/test/test_exceptions.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,50 @@ def test_copy_pickle(self):
21172117
self.assertEqual(exc.name, orig.name)
21182118
self.assertEqual(exc.path, orig.path)
21192119

2120+
def test_repr(self):
2121+
exc = ImportError()
2122+
self.assertEqual(repr(exc), "ImportError()")
2123+
2124+
exc = ImportError('test')
2125+
self.assertEqual(repr(exc), "ImportError('test')")
2126+
2127+
exc = ImportError('test', 'case')
2128+
self.assertEqual(repr(exc), "ImportError('test', 'case')")
2129+
2130+
exc = ImportError(name='somemodule')
2131+
self.assertEqual(repr(exc), "ImportError(name='somemodule')")
2132+
2133+
exc = ImportError('test', name='somemodule')
2134+
self.assertEqual(repr(exc), "ImportError('test', name='somemodule')")
2135+
2136+
exc = ImportError(path='somepath')
2137+
self.assertEqual(repr(exc), "ImportError(path='somepath')")
2138+
2139+
exc = ImportError('test', path='somepath')
2140+
self.assertEqual(repr(exc), "ImportError('test', path='somepath')")
2141+
2142+
exc = ImportError(name='somename', path='somepath')
2143+
self.assertEqual(repr(exc),
2144+
"ImportError(name='somename', path='somepath')")
2145+
2146+
exc = ImportError('test', name='somename', path='somepath')
2147+
self.assertEqual(repr(exc),
2148+
"ImportError('test', name='somename', path='somepath')")
2149+
2150+
exc = ModuleNotFoundError('test', name='somename', path='somepath')
2151+
self.assertEqual(repr(exc),
2152+
"ModuleNotFoundError('test', name='somename', path='somepath')")
2153+
2154+
def test_ModuleNotFoundError_repr_with_failed_import(self):
2155+
with self.assertRaises(ModuleNotFoundError) as cm:
2156+
import does_not_exist # type: ignore[import] # noqa: F401
2157+
2158+
self.assertEqual(cm.exception.name, "does_not_exist")
2159+
self.assertIsNone(cm.exception.path)
2160+
2161+
self.assertEqual(repr(cm.exception),
2162+
"ModuleNotFoundError(\"No module named 'does_not_exist'\", name='does_not_exist')")
2163+
21202164

21212165
def run_script(source):
21222166
if isinstance(source, str):

‎scripts/update_lib/cmd_copy_test.py‎

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,15 @@ def main(argv: list[str] | None = None) -> int:
9898
parser.add_argument("source", type=Path)
9999
parser.add_argument("destination", type=Path)
100100
parser.add_argument("--test", required=True, help="Exact ClassName.method_name")
101+
parser.add_argument(
102+
"--add-after", help="Append an absent method after this exact ClassName.anchor"
103+
)
104+
parser.add_argument(
105+
"--allow-local-import",
106+
action="append",
107+
default=[],
108+
help="Acknowledge one exact local import statement in an added method",
109+
)
101110
parser.add_argument("--source-sha256", required=True)
102111
parser.add_argument("--destination-sha256", required=True)
103112
args = parser.parse_args(argv)
@@ -110,9 +119,32 @@ def main(argv: list[str] | None = None) -> int:
110119
):
111120
if hashlib.sha256(contents).hexdigest() != expected:
112121
raise ValueError(f"{label} SHA256 does not match the pinned input")
113-
result = copy_test(
114-
source.decode("utf-8"), destination.decode("utf-8"), args.test
115-
).encode("utf-8")
122+
receipt = {}
123+
if args.add_after:
124+
from update_lib.copy_test_addition import _local_imports, add_test
125+
126+
result_text = add_test(
127+
source.decode("utf-8"),
128+
destination.decode("utf-8"),
129+
args.test,
130+
args.add_after,
131+
local_imports=args.allow_local_import,
132+
)
133+
method, _, first, last = _definition(source.decode("utf-8"), args.test)
134+
definition = b"".join(source.splitlines(keepends=True)[first:last])
135+
receipt = {
136+
"mode": "add",
137+
"anchor": args.add_after,
138+
"definition_sha256": hashlib.sha256(definition).hexdigest(),
139+
"local_imports": _local_imports(method, args.allow_local_import),
140+
}
141+
else:
142+
if args.allow_local_import:
143+
raise ValueError("Local import acknowledgment requires --add-after")
144+
result_text = copy_test(
145+
source.decode("utf-8"), destination.decode("utf-8"), args.test
146+
)
147+
result = result_text.encode("utf-8")
116148
# Recheck the destination immediately before writing so stale invocations
117149
# fail instead of overwriting an intervening edit.
118150
if args.destination.read_bytes() != destination:
@@ -127,6 +159,7 @@ def main(argv: list[str] | None = None) -> int:
127159
"source_sha256": args.source_sha256,
128160
"destination_before_sha256": args.destination_sha256,
129161
"destination_after_sha256": hashlib.sha256(result).hexdigest(),
162+
**receipt,
130163
},
131164
sort_keys=True,
132165
)

0 commit comments

Comments
 (0)