nucleic / enaml

Declarative User Interfaces for Python
http://enaml.readthedocs.io/en/latest/
Other
1.52k stars 130 forks source link

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

Closed bburan closed 1 year ago

bburan commented 1 year ago

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 commented 1 year ago

Closing in favor of #bytecode/4

MatthieuDartiailh commented 1 year ago

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 commented 1 year ago

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.

bburan commented 1 year ago

Great, thank you for fixing this!

On Wed, May 24, 2023 at 11:39 AM Matthieu Dartiailh < @.***> wrote:

Closed #521 https://github.com/nucleic/enaml/issues/521 as completed via #528 https://github.com/nucleic/enaml/pull/528.

— Reply to this email directly, view it on GitHub https://github.com/nucleic/enaml/issues/521#event-9331435687, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACPSQWOUGYXYBVJATIZGDLXHZIVTANCNFSM6AAAAAAW6ZF5OM . You are receiving this because you modified the open/close state.Message ID: @.***>