Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError when using try/except with "as" in Enaml code #521

Closed
bburan opened this issue Apr 14, 2023 · 4 comments · Fixed by #528
Closed

TypeError when using try/except with "as" in Enaml code #521

bburan opened this issue Apr 14, 2023 · 4 comments · Fixed by #528

Comments

@bburan
Copy link
Contributor

bburan commented Apr 14, 2023

The following code block in any Enaml file:

try:
    pass
except Exception as e:
    pass

Results in the following traceback:

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "c:\Users\lbhb\anaconda3\envs\psi-nafc\Scripts\enaml-run.exe\__main__.py", line 7, in <module>
  File "c:\Users\lbhb\anaconda3\envs\psi-nafc\Lib\site-packages\enaml\runner.py", line 44, in main
    code = EnamlCompiler.compile(ast, enaml_file_path)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\lbhb\anaconda3\envs\psi-nafc\Lib\site-packages\enaml\core\enaml_compiler.py", line 179, in compile
    return compiler.visit(node)
           ^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\lbhb\anaconda3\envs\psi-nafc\Lib\site-packages\enaml\core\enaml_ast.py", line 418, in visit
    result = visitor(node, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\lbhb\anaconda3\envs\psi-nafc\Lib\site-packages\enaml\core\enaml_compiler.py", line 195, in visit_Module
    self.visit(item)
  File "c:\Users\lbhb\anaconda3\envs\psi-nafc\Lib\site-packages\enaml\core\enaml_ast.py", line 418, in visit
    result = visitor(node, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\Users\lbhb\anaconda3\envs\psi-nafc\Lib\site-packages\enaml\core\enaml_compiler.py", line 213, in visit_PythonModule
    cg.insert_python_block(node.ast)
  File "c:\Users\lbhb\anaconda3\envs\psi-nafc\Lib\site-packages\enaml\core\code_generator.py", line 573, in insert_python_block
    bc_code = cfg.to_bytecode()
              ^^^^^^^^^^^^^^^^^
  File "c:\Users\lbhb\anaconda3\envs\psi-nafc\Lib\site-packages\bytecode\cfg.py", line 992, in to_bytecode
    byt_te.entry.stack_depth = min(
                               ^^^^
TypeError: '<' not supported between instances of '_UNSET' and '_UNSET'

Other variations such as:

try:
    pass
except:
    pass

and:

try:
    pass
except Exception:
    pass

are fine. It seems the "as" triggers an error. I have only seen this on Python 3.11. I believe Python 3.10 is fine.

@bburan
Copy link
Contributor Author

bburan commented Apr 14, 2023

Closing in favor of #bytecode/4

@MatthieuDartiailh
Copy link
Member

The enaml compiler is doing something wrong at https://github.com/nucleic/enaml/blob/main/enaml/core/code_generator.py#L547 since before the cfg modification the CFG round trip properly

@MatthieuDartiailh
Copy link
Member

Good news I believe I have a fix for this. It turns out I need a tiny fix in bytecode and a fix in enaml. I hope to land both by the end of the week.

MatthieuDartiailh added a commit that referenced this issue May 24, 2023
* core: only link the last block of a cfg to an artificial end block if it is a true terminal block

By terminal block I mean a block ending on LOAD_CONST None RETURN_VALUE, in Python 3.11 we can often have exception handling block last.

* tests: add a test for the code generation issue identified in #521

* cis: test PR branch of bytecode

* revert ci modification

* pyproject: request latest bytecode release
@bburan
Copy link
Contributor Author

bburan commented May 29, 2023 via email

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 a pull request may close this issue.

2 participants