Skip to content
Open
Show file tree
Hide file tree
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
88 changes: 55 additions & 33 deletions codetransformer/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ def _freevar_argname(arg, cellvars, freevars):


def pycode(argcount,
posonlyargcount,
kwonlyargcount,
nlocals,
stacksize,
Expand All @@ -238,23 +239,43 @@ def pycode(argcount,
--------
types.CodeType
"""
return CodeType(
argcount,
kwonlyargcount,
nlocals,
stacksize,
flags,
codestring,
constants,
names,
varnames,
filename,
name,
firstlineno,
lnotab,
freevars,
cellvars,
)
if hasattr(CodeType, "co_posonlyargcount"):
return CodeType(
argcount,
posonlyargcount, # new in 3.8
kwonlyargcount,
nlocals,
stacksize,
flags,
codestring,
constants,
names,
varnames,
filename,
name,
firstlineno,
lnotab,
freevars,
cellvars,
)
else:
return CodeType(
argcount,
kwonlyargcount,
nlocals,
stacksize,
flags,
codestring,
constants,
names,
varnames,
filename,
name,
firstlineno,
lnotab,
freevars,
cellvars,
)


class Code:
Expand Down Expand Up @@ -581,22 +602,23 @@ def to_pycode(self):
# with wordcode, all instructions are padded to 2 bytes
bc.append(0)

return CodeType(
self.argcount,
self.kwonlyargcount,
len(varnames),
self.stacksize,
self.py_flags,
bytes(bc),
consts,
names,
varnames,
self.filename,
self.name,
self.firstlineno,
self.py_lnotab,
freevars,
cellvars,
return pycode(
argcount=self.argcount,
posonlyargcount=0,
kwonlyargcount=self.kwonlyargcount,
nlocals=len(varnames),
stacksize=self.stacksize,
flags=self.py_flags,
codestring=bytes(bc),
constants=consts,
names=names,
varnames=varnames,
filename=self.filename,
name=self.name,
firstlineno=self.firstlineno,
lnotab=self.py_lnotab,
freevars=freevars,
cellvars=cellvars
)

@property
Expand Down
1 change: 1 addition & 0 deletions codetransformer/tests/test_code.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ def test_code_flags(sample_flags):

code = Code.from_pycode(pycode(
argcount=0,
posonlyargcount=0,
kwonlyargcount=0,
nlocals=2,
stacksize=0,
Expand Down