@@ -54,7 +54,8 @@ struct assembler {
5454 int a_except_table_off ; /* offset into exception table */
5555 /* Location Info */
5656 int a_lineno ; /* lineno of last emitted instruction */
57- PyObject * a_linetable ; /* bytes containing location info */
57+ PyBytesWriter * a_linetable ; /* bytes writer containing location info */
58+ PyObject * a_linetable_obj ; /* bytes object containing location info */
5859 int a_location_off ; /* offset of last written location info frame */
5960};
6061
@@ -63,14 +64,11 @@ assemble_init(struct assembler *a, int firstlineno)
6364{
6465 memset (a , 0 , sizeof (struct assembler ));
6566 a -> a_lineno = firstlineno ;
66- a -> a_linetable = NULL ;
67- a -> a_location_off = 0 ;
68- a -> a_except_table = NULL ;
6967 a -> a_bytecode = PyBytes_FromStringAndSize (NULL , DEFAULT_CODE_SIZE );
7068 if (a -> a_bytecode == NULL ) {
7169 goto error ;
7270 }
73- a -> a_linetable = PyBytes_FromStringAndSize ( NULL , DEFAULT_CNOTAB_SIZE );
71+ a -> a_linetable = PyBytesWriter_Create ( DEFAULT_CNOTAB_SIZE );
7472 if (a -> a_linetable == NULL ) {
7573 goto error ;
7674 }
@@ -81,7 +79,7 @@ assemble_init(struct assembler *a, int firstlineno)
8179 return SUCCESS ;
8280error :
8381 Py_CLEAR (a -> a_bytecode );
84- Py_CLEAR (a -> a_linetable );
82+ PyBytesWriter_Discard (a -> a_linetable );
8583 Py_CLEAR (a -> a_except_table );
8684 return ERROR ;
8785}
@@ -90,7 +88,8 @@ static void
9088assemble_free (struct assembler * a )
9189{
9290 Py_XDECREF (a -> a_bytecode );
93- Py_XDECREF (a -> a_linetable );
91+ PyBytesWriter_Discard (a -> a_linetable );
92+ Py_XDECREF (a -> a_linetable_obj );
9493 Py_XDECREF (a -> a_except_table );
9594}
9695
@@ -195,16 +194,17 @@ assemble_exception_table(struct assembler *a, instr_sequence *instrs)
195194static void
196195write_location_byte (struct assembler * a , int val )
197196{
198- PyBytes_AS_STRING (a -> a_linetable )[a -> a_location_off ] = val & 255 ;
197+ uint8_t * a_linetable = PyBytesWriter_GetData (a -> a_linetable );
198+ a_linetable [a -> a_location_off ] = val & 255 ;
199199 a -> a_location_off ++ ;
200200}
201201
202202
203203static uint8_t *
204204location_pointer (struct assembler * a )
205205{
206- return ( uint8_t * ) PyBytes_AS_STRING (a -> a_linetable ) +
207- a -> a_location_off ;
206+ uint8_t * a_linetable = PyBytesWriter_GetData (a -> a_linetable );
207+ return a_linetable + a -> a_location_off ;
208208}
209209
210210static void
@@ -285,10 +285,10 @@ write_location_info_no_column(struct assembler* a, int length, int line_delta)
285285static int
286286write_location_info_entry (struct assembler * a , location loc , int isize )
287287{
288- Py_ssize_t len = PyBytes_GET_SIZE (a -> a_linetable );
288+ Py_ssize_t len = PyBytesWriter_GetSize (a -> a_linetable );
289289 if (a -> a_location_off + THEORETICAL_MAX_ENTRY_SIZE >= len ) {
290290 assert (len > THEORETICAL_MAX_ENTRY_SIZE );
291- RETURN_IF_ERROR (_PyBytes_Resize ( & a -> a_linetable , len * 2 ));
291+ RETURN_IF_ERROR (PyBytesWriter_Resize ( a -> a_linetable , len * 2 ));
292292 }
293293 if (loc .lineno == NO_LOCATION .lineno ) {
294294 write_location_info_none (a , isize );
@@ -447,8 +447,13 @@ assemble_emit(struct assembler *a, instr_sequence *instrs,
447447 RETURN_IF_ERROR (_PyBytes_Resize (& a -> a_except_table , a -> a_except_table_off ));
448448 RETURN_IF_ERROR (_PyCompile_ConstCacheMergeOne (const_cache , & a -> a_except_table ));
449449
450- RETURN_IF_ERROR (_PyBytes_Resize (& a -> a_linetable , a -> a_location_off ));
451- RETURN_IF_ERROR (_PyCompile_ConstCacheMergeOne (const_cache , & a -> a_linetable ));
450+ a -> a_linetable_obj = PyBytesWriter_FinishWithSize (a -> a_linetable ,
451+ a -> a_location_off );
452+ a -> a_linetable = NULL ;
453+ if (a -> a_linetable_obj == NULL ) {
454+ return ERROR ;
455+ }
456+ RETURN_IF_ERROR (_PyCompile_ConstCacheMergeOne (const_cache , & a -> a_linetable_obj ));
452457
453458 RETURN_IF_ERROR (_PyBytes_Resize (& a -> a_bytecode , a -> a_offset * sizeof (_Py_CODEUNIT )));
454459 RETURN_IF_ERROR (_PyCompile_ConstCacheMergeOne (const_cache , & a -> a_bytecode ));
@@ -629,7 +634,7 @@ makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_
629634
630635 .code = a -> a_bytecode ,
631636 .firstlineno = umd -> u_firstlineno ,
632- .linetable = a -> a_linetable ,
637+ .linetable = a -> a_linetable_obj ,
633638
634639 .consts = consts ,
635640 .names = names ,
0 commit comments