@@ -232,6 +232,33 @@ def test_error_offset_continuation_characters(self):
232232 check = self .check
233233 check ('"\\ \n "(1 for c in I,\\ \n \\ ' , 2 , 2 )
234234
235+ def testSyntaxErrorRange (self ):
236+ # gh-156894: the position was reported in bytes, not in characters,
237+ # for the errors which cover a range
238+ for source , offset , end_offset in [
239+ ('abcd = 00010' , 8 , 11 ),
240+ ('\u03b1 \u03b2 \u03b3 \u03b4 = 00010' , 8 , 11 ),
241+ ('a\u0301 b\u0308 c\u20d7 d\u1ab0 = 00010' , 12 , 15 ),
242+ ("abcd = ub'a'" , 8 , 10 ),
243+ ("\u03b1 \u03b2 \u03b3 \u03b4 = ub'a'" , 8 , 10 ),
244+ ("a\u0301 b\u0308 c\u20d7 d\u1ab0 = ub'a'" , 12 , 14 ),
245+ ]:
246+ with self .subTest (source = source ):
247+ with self .assertRaises (SyntaxError ) as cm :
248+ compile (source , '<testcase>' , 'exec' )
249+ self .assertEqual (cm .exception .offset , offset )
250+ self .assertEqual (cm .exception .end_offset , end_offset )
251+
252+ def testSyntaxErrorNonUTF8Offset (self ):
253+ # gh-157378: the position was reported one column short for each
254+ # multi-byte character preceding the invalid byte on the same line
255+ check = self .check
256+ check (b'X\x80 ' , 1 , 2 , 1 , 2 )
257+ check (b'\xc3 \xa9 X\x80 ' , 1 , 3 , 1 , 3 )
258+ check (b'\t \xc3 \xa9 X\x80 ' , 1 , 4 , 1 , 4 )
259+ check (b'a\xc3 \xa9 b\x80 c' , 1 , 4 , 1 , 4 )
260+ check (b'a\n \xc3 \xa9 X\x80 ' , 2 , 3 , 2 , 3 )
261+
235262 def testSyntaxErrorOffset (self ):
236263 check = self .check
237264 check ('def fact(x):\n \t return x!\n ' , 2 , 10 )
@@ -252,7 +279,16 @@ def testSyntaxErrorOffset(self):
252279 check ('[\n file\n for str(file)\n in\n []\n ]' , 3 , 5 )
253280 check ('[file for\n str(file) in []]' , 2 , 2 )
254281 check ("ages = {'Alice'=22, 'Bob'=23}" , 1 , 9 )
255- check ('match ...:\n case {**rest, "key": value}:\n ...' , 2 , 19 )
282+ check (dedent ("""\
283+ match ...:
284+ case {**rest1, "after": after}:
285+ ...
286+ """ ), 2 , 11 )
287+ check (dedent ("""\
288+ match ...:
289+ case {"before": before, **rest2, "after": after}:
290+ ...
291+ """ ), 2 , 29 )
256292 check ("[a b c d e f]" , 1 , 2 )
257293 check ("for x yfff:" , 1 , 7 )
258294 check ("f(a for a in b, c)" , 1 , 3 , 1 , 15 )
@@ -328,7 +364,6 @@ def baz():
328364 check ('x=1\n from __future__ import division' , 2 , 1 )
329365 check ('foo(1=2)' , 1 , 5 )
330366 check ('def f():\n x, y: int' , 2 , 3 )
331- check ('[*x for x in xs]' , 1 , 2 )
332367 check ('foo(x for x in range(10), 100)' , 1 , 5 )
333368 check ('for 1 in []: pass' , 1 , 5 )
334369 check ('(yield i) = 2' , 1 , 2 )
@@ -1510,6 +1545,7 @@ def gen():
15101545 @cpython_only
15111546 @unittest .skipIf (_testcapi is None , "requires _testcapi" )
15121547 @force_not_colorized
1548+ @support .skip_if_huge_c_stack ()
15131549 def test_recursion_normalizing_infinite_exception (self ):
15141550 # Issue #30697. Test that a RecursionError is raised when
15151551 # maximum recursion depth has been exceeded when creating
@@ -1576,11 +1612,7 @@ def recurse_in_body_and_except():
15761612 sys .setrecursionlimit (recursionlimit )
15771613
15781614
1579- @cpython_only
1580- # Python built with Py_TRACE_REFS fail with a fatal error in
1581- # _PyRefchain_Trace() on memory allocation error.
1582- @unittest .skipIf (support .Py_TRACE_REFS , 'cannot test Py_TRACE_REFS build' )
1583- @unittest .skipIf (_testcapi is None , "requires _testcapi" )
1615+ @support .nomemtest
15841616 def test_recursion_normalizing_with_no_memory (self ):
15851617 # Issue #30697. Test that in the abort that occurs when there is no
15861618 # memory left and the size of the Python frames stack is greater than
@@ -1707,6 +1739,20 @@ def inner():
17071739 gc_collect () # For PyPy or other GCs.
17081740 self .assertEqual (wr (), None )
17091741
1742+ def test_oserror_reinit_leak (self ):
1743+ # gh-150988: Check for memory leak when re-initializing OSError.
1744+ # Previously, setting OSError attributes in a subclass
1745+ # before calling super().__init__() leaked memory.
1746+ class LeakingOSError (OSError ):
1747+ def __init__ (self , code , message , filename , filename2 ):
1748+ self .strerror = message
1749+ self .filename = filename
1750+ self .filename2 = filename2
1751+ super ().__init__ (code , message , filename , None , filename2 )
1752+
1753+ exc = LeakingOSError (1 , "some message" , "filename.py" , "filename2.py" )
1754+ exc .__init__ (2 , "another message" , "filename3.py" , "filename4.py" )
1755+
17101756 def test_errno_ENOTDIR (self ):
17111757 # Issue #12802: "not a directory" errors are ENOTDIR even on Windows
17121758 with self .assertRaises (OSError ) as cm :
@@ -1753,11 +1799,7 @@ def test_unhandled(self):
17531799 self .assertIn ("test message" , report )
17541800 self .assertEndsWith (report , "\n " )
17551801
1756- @cpython_only
1757- # Python built with Py_TRACE_REFS fail with a fatal error in
1758- # _PyRefchain_Trace() on memory allocation error.
1759- @unittest .skipIf (support .Py_TRACE_REFS , 'cannot test Py_TRACE_REFS build' )
1760- @unittest .skipIf (_testcapi is None , "requires _testcapi" )
1802+ @support .nomemtest
17611803 def test_memory_error_in_PyErr_PrintEx (self ):
17621804 code = """if 1:
17631805 import _testcapi
@@ -1915,12 +1957,8 @@ def test_keyerror_context(self):
19151957 exc2 = None
19161958
19171959
1918- @cpython_only
1919- # Python built with Py_TRACE_REFS fail with a fatal error in
1920- # _PyRefchain_Trace() on memory allocation error.
1921- @unittest .skipIf (support .Py_TRACE_REFS , 'cannot test Py_TRACE_REFS build' )
1960+ @support .nomemtest
19221961 def test_exec_set_nomemory_hang (self ):
1923- import_module ("_testcapi" )
19241962 # gh-134163: A MemoryError inside code that was wrapped by a try/except
19251963 # block would lead to an infinite loop.
19261964
@@ -2117,6 +2155,50 @@ def test_copy_pickle(self):
21172155 self .assertEqual (exc .name , orig .name )
21182156 self .assertEqual (exc .path , orig .path )
21192157
2158+ def test_repr (self ):
2159+ exc = ImportError ()
2160+ self .assertEqual (repr (exc ), "ImportError()" )
2161+
2162+ exc = ImportError ('test' )
2163+ self .assertEqual (repr (exc ), "ImportError('test')" )
2164+
2165+ exc = ImportError ('test' , 'case' )
2166+ self .assertEqual (repr (exc ), "ImportError('test', 'case')" )
2167+
2168+ exc = ImportError (name = 'somemodule' )
2169+ self .assertEqual (repr (exc ), "ImportError(name='somemodule')" )
2170+
2171+ exc = ImportError ('test' , name = 'somemodule' )
2172+ self .assertEqual (repr (exc ), "ImportError('test', name='somemodule')" )
2173+
2174+ exc = ImportError (path = 'somepath' )
2175+ self .assertEqual (repr (exc ), "ImportError(path='somepath')" )
2176+
2177+ exc = ImportError ('test' , path = 'somepath' )
2178+ self .assertEqual (repr (exc ), "ImportError('test', path='somepath')" )
2179+
2180+ exc = ImportError (name = 'somename' , path = 'somepath' )
2181+ self .assertEqual (repr (exc ),
2182+ "ImportError(name='somename', path='somepath')" )
2183+
2184+ exc = ImportError ('test' , name = 'somename' , path = 'somepath' )
2185+ self .assertEqual (repr (exc ),
2186+ "ImportError('test', name='somename', path='somepath')" )
2187+
2188+ exc = ModuleNotFoundError ('test' , name = 'somename' , path = 'somepath' )
2189+ self .assertEqual (repr (exc ),
2190+ "ModuleNotFoundError('test', name='somename', path='somepath')" )
2191+
2192+ def test_ModuleNotFoundError_repr_with_failed_import (self ):
2193+ with self .assertRaises (ModuleNotFoundError ) as cm :
2194+ import does_not_exist # type: ignore[import] # noqa: F401
2195+
2196+ self .assertEqual (cm .exception .name , "does_not_exist" )
2197+ self .assertIsNone (cm .exception .path )
2198+
2199+ self .assertEqual (repr (cm .exception ),
2200+ "ModuleNotFoundError(\" No module named 'does_not_exist'\" , name='does_not_exist')" )
2201+
21202202
21212203def run_script (source ):
21222204 if isinstance (source , str ):
@@ -2373,7 +2455,6 @@ class MySyntaxError(SyntaxError):
23732455 ^^^^^
23742456""" , err .getvalue ())
23752457
2376- @unittest .expectedFailure # TODO: RUSTPYTHON
23772458 def test_encodings (self ):
23782459 self .addCleanup (unlink , TESTFN )
23792460 source = (
@@ -2382,7 +2463,8 @@ def test_encodings(self):
23822463 )
23832464 err = run_script (source .encode ('cp437' ))
23842465 self .assertEqual (err [- 3 ], ' "¢¢¢¢¢¢" + f(4, x for x in range(1))' )
2385- self .assertEqual (err [- 2 ], ' ^^^' )
2466+ self .assertEqual (err [- 2 ], ' ^^^^^^^^^^^^^^^^^^^' )
2467+ self .assertEqual (err [- 1 ], 'SyntaxError: Generator expression must be parenthesized' )
23862468
23872469 # Check backwards tokenizer errors
23882470 source = '# -*- coding: ascii -*-\n \n (\n '
0 commit comments