skorokithakis / tbvaccine

A small utility to pretty-print Python tracebacks. ⛺
MIT License
377 stars 14 forks source link

adding __main__ as a variable #9

Closed szabolcsdombi closed 7 years ago

szabolcsdombi commented 7 years ago
print('__name__ is %r' % __name__)

if __name__ == '__main__':
    raise Exception('hello')
skorokithakis commented 7 years ago

Hmm, can you elaborate on what this fixes exactly?

szabolcsdombi commented 7 years ago

Running python -m tbvaccine test_main.py prints __name__ is builtins and quits without an exception.

It is useful when running a single unittest. ( example )

test_main.py:

print('__name__ is %r' % __name__)

if __name__ == '__main__':
    raise Exception('hello')
skorokithakis commented 7 years ago

Ah, I see, thanks. Hopefully it doesn't break anything else by overwriting __name__ when it shouldn't.

szabolcsdombi commented 7 years ago

Not sure if it will override the __name__ for further imports. those get a different globals() and locals()

szabolcsdombi commented 7 years ago

Tested import with a random module:

__name__ is 'K'             # print from K.py
__name__ is '__main__'      # print from test_main.py
skorokithakis commented 7 years ago

Sounds good, thanks!