@@ -48,8 +48,10 @@ instr_size(instruction *instr)
4848}
4949
5050struct assembler {
51- PyObject * a_bytecode ; /* bytes containing bytecode */
51+ PyBytesWriter * a_bytecode_writer ; /* writer containing bytecode */
52+ PyObject * a_bytecode ; /* bytes containing bytecode */
5253 int a_offset ; /* offset into bytecode */
54+ PyBytesWriter * a_except_table_writer ; /* writer containing exception table */
5355 PyObject * a_except_table ; /* bytes containing exception table */
5456 int a_except_table_off ; /* offset into exception table */
5557 /* Location Info */
@@ -64,38 +66,40 @@ assemble_init(struct assembler *a, int firstlineno)
6466{
6567 memset (a , 0 , sizeof (struct assembler ));
6668 a -> a_lineno = firstlineno ;
67- a -> a_bytecode = PyBytes_FromStringAndSize ( NULL , DEFAULT_CODE_SIZE );
68- if (a -> a_bytecode == NULL ) {
69+ a -> a_bytecode_writer = PyBytesWriter_Create ( DEFAULT_CODE_SIZE );
70+ if (a -> a_bytecode_writer == NULL ) {
6971 goto error ;
7072 }
7173 a -> a_linetable_writer = PyBytesWriter_Create (DEFAULT_CNOTAB_SIZE );
7274 if (a -> a_linetable_writer == NULL ) {
7375 goto error ;
7476 }
75- a -> a_except_table = PyBytes_FromStringAndSize ( NULL , DEFAULT_LNOTAB_SIZE );
76- if (a -> a_except_table == NULL ) {
77+ a -> a_except_table_writer = PyBytesWriter_Create ( DEFAULT_LNOTAB_SIZE );
78+ if (a -> a_except_table_writer == NULL ) {
7779 goto error ;
7880 }
7981 return SUCCESS ;
8082error :
81- Py_CLEAR (a -> a_bytecode );
83+ PyBytesWriter_Discard (a -> a_bytecode_writer );
8284 PyBytesWriter_Discard (a -> a_linetable_writer );
83- Py_CLEAR (a -> a_except_table );
85+ PyBytesWriter_Discard (a -> a_except_table_writer );
8486 return ERROR ;
8587}
8688
8789static void
8890assemble_free (struct assembler * a )
8991{
90- Py_XDECREF (a -> a_bytecode );
92+ PyBytesWriter_Discard (a -> a_bytecode_writer );
9193 PyBytesWriter_Discard (a -> a_linetable_writer );
94+ PyBytesWriter_Discard (a -> a_except_table_writer );
95+ Py_XDECREF (a -> a_bytecode );
9296 Py_XDECREF (a -> a_linetable );
9397 Py_XDECREF (a -> a_except_table );
9498}
9599
96100static inline void
97101write_except_byte (struct assembler * a , int byte ) {
98- unsigned char * p = (unsigned char * ) PyBytes_AS_STRING (a -> a_except_table );
102+ unsigned char * p = (unsigned char * ) PyBytesWriter_GetData (a -> a_except_table_writer );
99103 p [a -> a_except_table_off ++ ] = byte ;
100104}
101105
@@ -133,9 +137,9 @@ assemble_emit_exception_table_entry(struct assembler *a, int start, int end,
133137 int handler_offset ,
134138 _PyExceptHandlerInfo * handler )
135139{
136- Py_ssize_t len = PyBytes_GET_SIZE (a -> a_except_table );
140+ Py_ssize_t len = PyBytesWriter_GetSize (a -> a_except_table_writer );
137141 if (a -> a_except_table_off + MAX_SIZE_OF_ENTRY >= len ) {
138- RETURN_IF_ERROR (_PyBytes_Resize ( & a -> a_except_table , len * 2 ));
142+ RETURN_IF_ERROR (PyBytesWriter_Resize ( a -> a_except_table_writer , len * 2 ));
139143 }
140144 int size = end - start ;
141145 assert (end > start );
@@ -412,7 +416,7 @@ write_instr(_Py_CODEUNIT *codestr, instruction *instr, int ilen)
412416static int
413417assemble_emit_instr (struct assembler * a , instruction * instr )
414418{
415- Py_ssize_t len = PyBytes_GET_SIZE (a -> a_bytecode );
419+ Py_ssize_t len = PyBytesWriter_GetSize (a -> a_bytecode_writer );
416420 _Py_CODEUNIT * code ;
417421
418422 int size = instr_size (instr );
@@ -421,9 +425,9 @@ assemble_emit_instr(struct assembler *a, instruction *instr)
421425 PyErr_NoMemory ();
422426 return ERROR ;
423427 }
424- RETURN_IF_ERROR (_PyBytes_Resize ( & a -> a_bytecode , len * 2 ));
428+ RETURN_IF_ERROR (PyBytesWriter_Resize ( a -> a_bytecode_writer , len * 2 ));
425429 }
426- code = (_Py_CODEUNIT * )PyBytes_AS_STRING (a -> a_bytecode ) + a -> a_offset ;
430+ code = (_Py_CODEUNIT * )PyBytesWriter_GetData (a -> a_bytecode_writer ) + a -> a_offset ;
427431 a -> a_offset += size ;
428432 write_instr (code , instr , size );
429433 return SUCCESS ;
@@ -444,7 +448,12 @@ assemble_emit(struct assembler *a, instr_sequence *instrs,
444448
445449 RETURN_IF_ERROR (assemble_exception_table (a , instrs ));
446450
447- RETURN_IF_ERROR (_PyBytes_Resize (& a -> a_except_table , a -> a_except_table_off ));
451+ a -> a_except_table = PyBytesWriter_FinishWithSize (a -> a_except_table_writer ,
452+ a -> a_except_table_off );
453+ a -> a_except_table_writer = NULL ;
454+ if (a -> a_except_table == NULL ) {
455+ return ERROR ;
456+ }
448457 RETURN_IF_ERROR (_PyCompile_ConstCacheMergeOne (const_cache , & a -> a_except_table ));
449458
450459 a -> a_linetable = PyBytesWriter_FinishWithSize (a -> a_linetable_writer ,
@@ -455,7 +464,12 @@ assemble_emit(struct assembler *a, instr_sequence *instrs,
455464 }
456465 RETURN_IF_ERROR (_PyCompile_ConstCacheMergeOne (const_cache , & a -> a_linetable ));
457466
458- RETURN_IF_ERROR (_PyBytes_Resize (& a -> a_bytecode , a -> a_offset * sizeof (_Py_CODEUNIT )));
467+ a -> a_bytecode = PyBytesWriter_FinishWithSize (a -> a_bytecode_writer ,
468+ a -> a_offset * sizeof (_Py_CODEUNIT ));
469+ a -> a_bytecode_writer = NULL ;
470+ if (a -> a_bytecode == NULL ) {
471+ return ERROR ;
472+ }
459473 RETURN_IF_ERROR (_PyCompile_ConstCacheMergeOne (const_cache , & a -> a_bytecode ));
460474 return SUCCESS ;
461475}
0 commit comments