Reputation: 1
I changed the definition of the builtin function len, then I tried to import the ctypes module.But I don't know why it occurs an error when I did that.Here is my code:
import builtins
def func(obj):
print("Length: %d" % len(obj))
builtins.len = lambda obj: "Bad!!!"
import ctypes
Then here is the tracback of the error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 661, in exec_module
File "<frozen importlib._bootstrap_external>", line 772, in get_code
File "<frozen importlib._bootstrap_external>", line 491, in _code_to_bytecode
File "<frozen importlib._bootstrap_external>", line 42, in _w_long
ValueError: invalid literal for int() with base 10: 'Bad!!!'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 53, in apport_excepthook
if not enabled():
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 24, in enabled
import re
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 661, in exec_module
File "<frozen importlib._bootstrap_external>", line 772, in get_code
File "<frozen importlib._bootstrap_external>", line 491, in _code_to_bytecode
File "<frozen importlib._bootstrap_external>", line 42, in _w_long
ValueError: invalid literal for int() with base 10: 'Bad!!!'
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 661, in exec_module
File "<frozen importlib._bootstrap_external>", line 772, in get_code
File "<frozen importlibbootstrap_external>", line 491, in _code_to_bytecode
File "<frozen importlib._bootstrap_external>", line 42, in _w_long
ValueError: invalid literal for int() with base 10: 'Bad!!!'
This puzzles me a lot. Thanks very much if someone can help me!
Upvotes: 0
Views: 232
Reputation: 10431
Because your len
function returns non integer string.
You just ran in the exact same behavior you could have with:
>>> int("I should know playing with builtins is always a bad thing")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: 'lol'
Try to update your code like:
builtins.len = lambda obj: "123"
To see it.
Upvotes: 2