You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.2 KiB
49 lines
1.2 KiB
import inspect
|
|
import types
|
|
from io import StringIO
|
|
import sys
|
|
import re
|
|
|
|
did_you_mean = re.compile(r'Did you mean (.*)\?', re.IGNORECASE)
|
|
what = 'what?'
|
|
|
|
class Yes(types.ModuleType):
|
|
def __init__(self, other):
|
|
for attr in dir(other):
|
|
setattr(self, attr, getattr(other, attr))
|
|
|
|
def __repr__(self):
|
|
try:
|
|
last_error = sys.last_value
|
|
except:
|
|
return what
|
|
|
|
if not isinstance(last_error, Exception):
|
|
return what
|
|
|
|
error_message = str(last_error)
|
|
|
|
try:
|
|
suggestion = did_you_mean.search(error_message).group(1)
|
|
except Exception as e:
|
|
return what
|
|
|
|
if not suggestion.startswith('print('):
|
|
suggestion_ = f'print(({suggestion}).__repr__())'
|
|
else:
|
|
suggestion_ = suggestion
|
|
|
|
frame = inspect.currentframe()
|
|
locals = frame.f_back.f_locals
|
|
del frame
|
|
|
|
old_stdout = sys.stdout
|
|
eval_output = sys.stdout = StringIO()
|
|
|
|
eval(suggestion_, globals(), locals)
|
|
sys.stdout = old_stdout
|
|
|
|
return(f'{sys.ps1}{suggestion} \n{eval_output.getvalue()[:-1]}')
|
|
|
|
sys.modules[__name__] = Yes(sys.modules[__name__])
|