0

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!

3
  • 1
    Why are you puzzled? Changing the functionality of a heavily-relied upon basic builtin then trying to use it (indirectly, through importing) as it was intended should cause an error, no? Commented Sep 9, 2017 at 10:19
  • Do you mean the changed len function is excuted when I tried to import ctypes? @jedwards Commented Sep 9, 2017 at 10:28
  • The changed len fun is definitely called when you tried to import ctypes (see ValueError: invalid literal for int() with base 10: 'Bad!!!'). I'm not sure why though, unless a version of ctypes has a dependency on the future package. Commented Sep 9, 2017 at 10:56

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.