From 3678a6bdc6a925f2bce6423c41f911229836f946 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 10 May 2018 23:10:46 +1000 Subject: py/modbuiltins: Make built-in dir support the __dir__ special method. If MICROPY_PY_ALL_SPECIAL_METHODS is enabled then dir() will now delegate to the special method __dir__ if the object it is listing has this method. --- tests/basics/special_methods2.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'tests/basics') diff --git a/tests/basics/special_methods2.py b/tests/basics/special_methods2.py index ba7cf27cd..1a38a250c 100644 --- a/tests/basics/special_methods2.py +++ b/tests/basics/special_methods2.py @@ -94,6 +94,9 @@ class Cud(): print("__isub__ called") return self + def __dir__(self): + return ['a', 'b', 'c'] + cud1 = Cud() cud2 = Cud() @@ -113,6 +116,12 @@ cud2 // cud1 cud1 += cud2 cud1 -= cud2 +# test that dir() delegates to __dir__ special method +print(dir(cud1)) + +# test that dir() does not delegate to __dir__ for the type +print('a' in dir(Cud)) + # TODO: the following operations are not supported on every ports # # ne is not supported, !(eq) is called instead -- cgit v1.2.3