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

TryBegin object has no attribute name with python 3.12 in rewrite_to_fast_locals #548

Closed
frmdstryr opened this issue Apr 13, 2024 · 4 comments · Fixed by #550
Closed

TryBegin object has no attribute name with python 3.12 in rewrite_to_fast_locals #548

frmdstryr opened this issue Apr 13, 2024 · 4 comments · Fixed by #550

Comments

@frmdstryr
Copy link
Contributor

frmdstryr commented Apr 13, 2024

Not sure how to reproduce in a test yet but it seems like an isinstance(instr, bc.Instr) check is needed in rewrite_to_fast_locals.

  File "/app/lib/python3.12/site-packages/enaml/core/import_hooks.py", line 139, in exec_module
    code, _ = self.get_code()
              ^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/enaml/core/import_hooks.py", line 400, in get_code
    return self.compile_code()
           ^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/enaml/core/import_hooks.py", line 365, in compile_code
    code = EnamlCompiler.compile(ast, file_info.src_path)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/enaml/core/enaml_compiler.py", line 179, in compile
    return compiler.visit(node)
           ^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/enaml/core/enaml_ast.py", line 420, in visit
    result = visitor(node, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/enaml/core/enaml_compiler.py", line 195, in visit_Module
    self.visit(item)
  File "/app/lib/python3.12/site-packages/enaml/core/enaml_ast.py", line 420, in visit
    result = visitor(node, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/enaml/core/enaml_compiler.py", line 264, in visit_Template
    code = TemplateCompiler.compile(node, cg.filename)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/enaml/core/template_compiler.py", line 303, in compile
    return compiler.visit(node)
           ^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/enaml/core/enaml_ast.py", line 420, in visit
    result = visitor(node, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/enaml/core/template_compiler.py", line 315, in visit_Template
    first_code, index_map = FirstPassTemplateCompiler.compile(
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/enaml/core/template_compiler.py", line 97, in compile
    compiler.visit(node)
  File "/app/lib/python3.12/site-packages/enaml/core/enaml_ast.py", line 420, in visit
    result = visitor(node, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/enaml/core/template_compiler.py", line 134, in visit_Template
    self.visit(item)
  File "/app/lib/python3.12/site-packages/enaml/core/enaml_ast.py", line 420, in visit
    result = visitor(node, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/lib/python3.12/site-packages/enaml/core/block_compiler.py", line 82, in visit_TemplateInst
    cmn.gen_template_inst_node(cg, node, self.local_names)
  File "/app/lib/python3.12/site-packages/enaml/core/compiler_common.py", line 629, in gen_template_inst_node
    safe_eval_ast(cg, arg.ast, node.name, arg.lineno, local_names)
  File "/app/lib/python3.12/site-packages/enaml/core/compiler_common.py", line 399, in safe_eval_ast
    expr_cg.rewrite_to_fast_locals(local_names)
  File "/app/lib/python3.12/site-packages/enaml/core/code_generator.py", line 591, in rewrite_to_fast_locals
    if instr.name == "STORE_NAME":
       ^^^^^^^^^^
AttributeError: 'TryBegin' object has no attribute 'name'

It is when generating templates.

enaml==0.17.0
bytecode==0.15.1

@MatthieuDartiailh
Copy link
Member

Out of curiosity can you reproduce on 3.11 ? TryBegin was not introduced for 3.12 so I suspect the bug exists also on 3.11.

@MatthieuDartiailh
Copy link
Member

I agree that a simple isinstance to check if we are dealing with an Instr is the way to go here. Could you provide a reproducer even if it is not minimal.

@frmdstryr
Copy link
Contributor Author

frmdstryr commented Apr 15, 2024

I was able to reproduce it by inlining the Spec argument in the AutoFormBody from the templates/advanced.enaml example

Eg replace

template AutoFormBody(ModelType):
    const Spec: tuple = form_spec(ModelType)
    ForEach(Spec, FormItem):
        pass

with

template AutoFormBody(ModelType):
    const Spec: tuple = tuple([
        (name, type(member))
        for name, member in ModelType.members().items()
        if not name.startswith('_')
    ])
    ForEach(Spec, FormItem):
        pass

@frmdstryr
Copy link
Contributor Author

I cannot reproduce it with 3.11

frmdstryr added a commit to frmdstryr/enaml that referenced this issue Apr 15, 2024
MatthieuDartiailh added a commit that referenced this issue Apr 24, 2024
Fix potential error in rewrite fast locals on 3.12 for #548
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