Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 20 additions & 15 deletions Python/assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ struct assembler {
int a_except_table_off; /* offset into exception table */
/* Location Info */
int a_lineno; /* lineno of last emitted instruction */
PyObject* a_linetable; /* bytes containing location info */
PyBytesWriter *a_linetable; /* bytes writer containing location info */
PyObject *a_linetable_obj; /* bytes object containing location info */
int a_location_off; /* offset of last written location info frame */
};

Expand All @@ -63,14 +64,11 @@ assemble_init(struct assembler *a, int firstlineno)
{
memset(a, 0, sizeof(struct assembler));
a->a_lineno = firstlineno;
a->a_linetable = NULL;
a->a_location_off = 0;
a->a_except_table = NULL;
a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
if (a->a_bytecode == NULL) {
goto error;
}
a->a_linetable = PyBytes_FromStringAndSize(NULL, DEFAULT_CNOTAB_SIZE);
a->a_linetable = PyBytesWriter_Create(DEFAULT_CNOTAB_SIZE);
if (a->a_linetable == NULL) {
goto error;
}
Expand All @@ -81,7 +79,7 @@ assemble_init(struct assembler *a, int firstlineno)
return SUCCESS;
error:
Py_CLEAR(a->a_bytecode);
Py_CLEAR(a->a_linetable);
PyBytesWriter_Discard(a->a_linetable);
Py_CLEAR(a->a_except_table);
return ERROR;
}
Expand All @@ -90,7 +88,8 @@ static void
assemble_free(struct assembler *a)
{
Py_XDECREF(a->a_bytecode);
Py_XDECREF(a->a_linetable);
PyBytesWriter_Discard(a->a_linetable);
Py_XDECREF(a->a_linetable_obj);
Py_XDECREF(a->a_except_table);
}

Expand Down Expand Up @@ -195,16 +194,17 @@ assemble_exception_table(struct assembler *a, instr_sequence *instrs)
static void
write_location_byte(struct assembler* a, int val)
{
PyBytes_AS_STRING(a->a_linetable)[a->a_location_off] = val&255;
uint8_t *a_linetable = PyBytesWriter_GetData(a->a_linetable);
a_linetable[a->a_location_off] = val & 255;
a->a_location_off++;
}


static uint8_t *
location_pointer(struct assembler* a)
{
return (uint8_t *)PyBytes_AS_STRING(a->a_linetable) +
a->a_location_off;
uint8_t *a_linetable = PyBytesWriter_GetData(a->a_linetable);
return a_linetable + a->a_location_off;
}

static void
Expand Down Expand Up @@ -285,10 +285,10 @@ write_location_info_no_column(struct assembler* a, int length, int line_delta)
static int
write_location_info_entry(struct assembler* a, location loc, int isize)
{
Py_ssize_t len = PyBytes_GET_SIZE(a->a_linetable);
Py_ssize_t len = PyBytesWriter_GetSize(a->a_linetable);
if (a->a_location_off + THEORETICAL_MAX_ENTRY_SIZE >= len) {
assert(len > THEORETICAL_MAX_ENTRY_SIZE);
RETURN_IF_ERROR(_PyBytes_Resize(&a->a_linetable, len*2));
RETURN_IF_ERROR(PyBytesWriter_Resize(a->a_linetable, len * 2));
}
if (loc.lineno == NO_LOCATION.lineno) {
write_location_info_none(a, isize);
Expand Down Expand Up @@ -447,8 +447,13 @@ assemble_emit(struct assembler *a, instr_sequence *instrs,
RETURN_IF_ERROR(_PyBytes_Resize(&a->a_except_table, a->a_except_table_off));
RETURN_IF_ERROR(_PyCompile_ConstCacheMergeOne(const_cache, &a->a_except_table));

RETURN_IF_ERROR(_PyBytes_Resize(&a->a_linetable, a->a_location_off));
RETURN_IF_ERROR(_PyCompile_ConstCacheMergeOne(const_cache, &a->a_linetable));
a->a_linetable_obj = PyBytesWriter_FinishWithSize(a->a_linetable,
a->a_location_off);
a->a_linetable = NULL;
if (a->a_linetable_obj == NULL) {
return ERROR;
}
RETURN_IF_ERROR(_PyCompile_ConstCacheMergeOne(const_cache, &a->a_linetable_obj));

RETURN_IF_ERROR(_PyBytes_Resize(&a->a_bytecode, a->a_offset * sizeof(_Py_CODEUNIT)));
RETURN_IF_ERROR(_PyCompile_ConstCacheMergeOne(const_cache, &a->a_bytecode));
Expand Down Expand Up @@ -629,7 +634,7 @@ makecode(_PyCompile_CodeUnitMetadata *umd, struct assembler *a, PyObject *const_

.code = a->a_bytecode,
.firstlineno = umd->u_firstlineno,
.linetable = a->a_linetable,
.linetable = a->a_linetable_obj,

.consts = consts,
.names = names,
Expand Down
Loading