Is there a way to restore a builtin function after deleting it?
Question:
After deleting a builtin function like this, I want to restore it without restarting the interpreter.
>>> import builtins
>>> del builtins.eval
>>> builtins.eval = None
I tried reloading the builtin module using importlib
, that didn’t restore eval.
>>> import importlib
>>> importlib.reload(builtins)
<module 'builtins' (built-in)>
>>> eval("5 + 5")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
I also tried to assign a __builtins__
variable from another module. That didn’t work as well.
>>> import os
>>> __builtins__ = os.__builtins__
>>> eval()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
Is there a way to restore a builtin function after deleting it?
Answers:
After posting the question I figured out a way to restore it using the BuiltinImporter.
>>> import builtins
>>> del builtins.eval
>>> builtins.eval = None
>>> eval()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
>>> import importlib
>>> bi = importlib.machinery.BuiltinImporter
>>> bi.load_module("builtins")
<module 'builtins' (built-in)>
>>> import sys
>>> __builtins__ = bi.load_module.__globals__['module_from_spec'](sys.modules['builtins'].__spec__)
>>> eval("5 + 5")
10
I think the usage pattern of builtins
is different from what you suggest.
What you typically do is that you re-bind a built-in name for your purpose and then use builtins
to restore the functionality:
eval = None
eval('5 + 5')
# TypeError: 'NoneType' object is not callable
import builtins
eval = builtins.eval
eval('5 + 5')
# 10
or (as commented by @ShadowRanger), even more simply in this specific case:
eval = None
eval('5 + 5')
# TypeError: 'NoneType' object is not callable
del eval
eval('5 + 5')
# 10
After deleting a builtin function like this, I want to restore it without restarting the interpreter.
>>> import builtins
>>> del builtins.eval
>>> builtins.eval = None
I tried reloading the builtin module using importlib
, that didn’t restore eval.
>>> import importlib
>>> importlib.reload(builtins)
<module 'builtins' (built-in)>
>>> eval("5 + 5")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
I also tried to assign a __builtins__
variable from another module. That didn’t work as well.
>>> import os
>>> __builtins__ = os.__builtins__
>>> eval()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
Is there a way to restore a builtin function after deleting it?
After posting the question I figured out a way to restore it using the BuiltinImporter.
>>> import builtins
>>> del builtins.eval
>>> builtins.eval = None
>>> eval()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object is not callable
>>> import importlib
>>> bi = importlib.machinery.BuiltinImporter
>>> bi.load_module("builtins")
<module 'builtins' (built-in)>
>>> import sys
>>> __builtins__ = bi.load_module.__globals__['module_from_spec'](sys.modules['builtins'].__spec__)
>>> eval("5 + 5")
10
I think the usage pattern of builtins
is different from what you suggest.
What you typically do is that you re-bind a built-in name for your purpose and then use builtins
to restore the functionality:
eval = None
eval('5 + 5')
# TypeError: 'NoneType' object is not callable
import builtins
eval = builtins.eval
eval('5 + 5')
# 10
or (as commented by @ShadowRanger), even more simply in this specific case:
eval = None
eval('5 + 5')
# TypeError: 'NoneType' object is not callable
del eval
eval('5 + 5')
# 10