Last active
May 17, 2018 20:10
-
-
Save rs2/303684b2e98e09b085b2f2c8ffc23f29 to your computer and use it in GitHub Desktop.
PyDojoE9S9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sitecustomize | |
foobar = 42 | |
print(foo) | |
# Prints | |
# name 'foo' is not defined | |
# Did you mean foobar? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import re | |
RX = re.compile(r"name \'(?P<variable>\w+)\' is not defined") | |
original_hook = sys.excepthook | |
def custom_exception_handler(etype, value, tb): | |
if issubclass(etype, NameError): | |
print(value) | |
name = RX.match(str(value)).groupdict()['variable'] | |
for sym in tb.tb_frame.f_globals: | |
if sym.startswith(name): | |
print("Did you mean {}?".format(sym)) | |
return | |
original_hook(etype, value, tb) | |
def install_custom_hook(): | |
sys.excepthook = custom_exception_handler | |
install_custom_hook() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment