Skip to content

Bug: define_macro & embed doesn't work as expected inside a method vs __main__ #12333

@kirankotari

Description

@kirankotari

If I have a macro inside a function doesn't work as expected, the same is working fine on __main__ condition. Why?

Define Macro in function throws an error..!

import IPython as ipy
from IPython.terminal.interactiveshell import TerminalInteractiveShell

def run():
    shell = TerminalInteractiveShell.instance()
    shell.define_macro('foo', """a,b=10,20""")
    ipy.embed(display_banner=False)

if __name__ == "__main__":
    run()
⋊> ~/c/i/i/ipycli on master ⨯ ipython test
In [1]: foo
Out[1]: IPython.macro.Macro('a,b=10,20\n')
In [2]: a,b
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-3-2fbdd1b4fa70> in <module>
----> 1 a,b
NameError: name 'a' is not defined

Define Macro in main works fine..!

import IPython as ipy
from IPython.terminal.interactiveshell import TerminalInteractiveShell

if __name__ == "__main__":
    shell = TerminalInteractiveShell.instance()
    shell.define_macro('foo', """a,b=10,20""")
    ipy.embed(display_banner=False)
⋊> ~/c/i/i/ipycli on master ⨯ ipython test
In [1]: foo
In [2]: a,b
Out[2]: (10, 20)

IPython version 7.14.0
OS: macOS Catalina
Terminal: iTerm2 version 3.3.9

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions