@@ -2075,6 +2075,39 @@ def test_detect_overflow(self):
20752075 # strategy which depends on the operating system
20762076 self .assertIn (f'at position ' .encode (), proc .err )
20772077
2078+ @support .nomemtest
2079+ def test_memory_error (self ):
2080+ # Inject MemoryError in PyUnicodeWriter_WriteStr()
2081+ writer = self .create_writer (0 )
2082+ writer .write_str ("start" )
2083+ with self .assertRaises (MemoryError ):
2084+ with support .inject_memory_error_cm ():
2085+ # Resize the internal str object
2086+ writer .write_str ("s" * 1024 )
2087+ writer .write_str (" end" )
2088+ self .assertEqual (writer .finish (), "start end" )
2089+
2090+ # Inject MemoryError in PyUnicodeWriter_Finish()
2091+ writer = self .create_writer (1024 )
2092+ writer .write_str ("abc" )
2093+ with self .assertRaises (MemoryError ):
2094+ with support .inject_memory_error_cm ():
2095+ # Need to truncate the internal str object
2096+ writer .finish ()
2097+
2098+ def test_change_kind (self ):
2099+ writer = self .create_writer (0 )
2100+ # Create an ASCII buffer
2101+ writer .write_str ('ascii ' )
2102+ # Change the buffer to UCS1
2103+ writer .write_str ('latin1:\xe9 ' )
2104+ # Change the buffer to UCS2
2105+ writer .write_str ('ucs2:\u20ac ' )
2106+ # Change the buffer to UCS4
2107+ writer .write_str ('ucs4:\U0010ffff ' )
2108+ self .assertEqual (writer .finish (),
2109+ 'ascii latin1:\xe9 ucs2:\u20ac ucs4:\U0010ffff ' )
2110+
20782111
20792112# Test PyUnicodeWriter_Format()
20802113@unittest .skipIf (ctypes is None , 'need ctypes' )
0 commit comments