Skip to content

Insert co_posonlyargcount into args#34

Closed
TabulateJarl8 wants to merge 2 commits intosnoack:masterfrom
TabulateJarl8:patch-2
Closed

Insert co_posonlyargcount into args#34
TabulateJarl8 wants to merge 2 commits intosnoack:masterfrom
TabulateJarl8:patch-2

Conversation

@TabulateJarl8
Copy link

Until this is done you get this error:

Traceback (most recent call last):
  File "/home/tabulate/snek.py", line 4, in <module>
    def main():
  File "/home/tabulate/.local/lib/python3.9/site-packages/goto.py", line 240, in with_goto
    _patch_code(func_or_code.__code__),
  File "/home/tabulate/.local/lib/python3.9/site-packages/goto.py", line 231, in _patch_code
    return _make_code(code, _array_to_bytes(buf))
  File "/home/tabulate/.local/lib/python3.9/site-packages/goto.py", line 58, in _make_code
    return types.CodeType(*args)
TypeError: an integer is required (got type bytes)

This is because types.CodeType started requiring another argument, posonlyargcount. If you use the inspect module, you can see the arguments needed for this function.

>>> import types
>>> import inspect
>>> print(inspect.getdoc(types.CodeType))
code(argcount, posonlyargcount, kwonlyargcount, nlocals, stacksize,
      flags, codestring, constants, names, varnames, filename, name,
      firstlineno, lnotab[, freevars[, cellvars]])

Create a code object.  Not for the faint of heart.

I got this error using Python 3.9, and after adding this it works again

@progval
Copy link

progval commented Apr 30, 2021

Alternatively, this works too:

def _make_code(code, codestring):
    return code.replace(co_code=codestring)

@TabulateJarl8
Copy link
Author

Closing because of #22

@wdyyoij
Copy link

wdyyoij commented May 12, 2022

code.replace(co_code=codestring)

u r so fking awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants