diff options
author | Damien George <damien.p.george@gmail.com> | 2014-12-09 16:19:48 +0000 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2014-12-09 16:19:48 +0000 |
commit | 78d702c300ae9f175e6f47f805a37cdfe5b81898 (patch) | |
tree | 034b42ea789dc38c629a2f0dd8a48001a32cd838 /tests/basics/builtin_override.py | |
parent | e6e8ad8ab238cd596a3eedf8f4dd635e2e84f46e (diff) | |
download | micropython-78d702c300ae9f175e6f47f805a37cdfe5b81898.tar.gz micropython-78d702c300ae9f175e6f47f805a37cdfe5b81898.zip |
py: Allow builtins to be overridden.
This patch adds a configuration option (MICROPY_CAN_OVERRIDE_BUILTINS)
which, when enabled, allows to override all names within the builtins
module. A builtins override dict is created the first time the user
assigns to a name in the builtins model, and then that dict is searched
first on subsequent lookups. Note that this implementation doesn't
allow deleting of names.
This patch also does some refactoring of builtins code, creating the
modbuiltins.c file.
Addresses issue #959.
Diffstat (limited to 'tests/basics/builtin_override.py')
-rw-r--r-- | tests/basics/builtin_override.py | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/tests/basics/builtin_override.py b/tests/basics/builtin_override.py new file mode 100644 index 0000000000..e245985ad9 --- /dev/null +++ b/tests/basics/builtin_override.py @@ -0,0 +1,13 @@ +# test overriding builtins + +import builtins + +# override generic builtin +builtins.abs = lambda x: x + 1 +print(abs(1)) + +# __build_class__ is handled in a special way +builtins.__build_class__ = lambda x, y: ('class', y) +class A: + pass +print(A) |