mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
Add dir __base__ recursively; Add dir tests to 70_builtins.py
This commit is contained in:
parent
0031e87db4
commit
a7d83934c1
@ -85,18 +85,18 @@ def dir(obj=None):
|
|||||||
if obj is None:
|
if obj is None:
|
||||||
return list(globals().keys())
|
return list(globals().keys())
|
||||||
|
|
||||||
if hasattr(obj, "__dir__"):
|
|
||||||
return obj.__dir__()
|
|
||||||
|
|
||||||
attributes = set()
|
attributes = set()
|
||||||
# Set object attributes.
|
|
||||||
|
if not isinstance(obj, type):
|
||||||
|
if hasattr(obj, "__dir__"):
|
||||||
|
return sorted(obj.__dir__())
|
||||||
|
attributes.update(dir(type(obj)))
|
||||||
|
|
||||||
if hasattr(obj, "__dict__"):
|
if hasattr(obj, "__dict__"):
|
||||||
attributes.update(obj.__dict__.keys())
|
attributes.update(obj.__dict__.keys())
|
||||||
|
|
||||||
# Set type attributes.
|
if hasattr(obj, "__base__") and obj.__base__ is not None:
|
||||||
if not isinstance(obj, type):
|
attributes.update(dir(obj.__base__))
|
||||||
if hasattr(type(obj), "__dict__"):
|
|
||||||
attributes.update(type(obj).__dict__.keys())
|
|
||||||
|
|
||||||
return sorted(attributes)
|
return sorted(attributes)
|
||||||
|
|
||||||
|
@ -49,4 +49,21 @@ assert not all([True, False])
|
|||||||
assert not all([False, False])
|
assert not all([False, False])
|
||||||
|
|
||||||
assert list(enumerate([1,2,3])) == [(0,1), (1,2), (2,3)]
|
assert list(enumerate([1,2,3])) == [(0,1), (1,2), (2,3)]
|
||||||
assert list(enumerate([1,2,3], 1)) == [(1,1), (2,2), (3,3)]
|
assert list(enumerate([1,2,3], 1)) == [(1,1), (2,2), (3,3)]
|
||||||
|
|
||||||
|
class C1:
|
||||||
|
def c(): ...
|
||||||
|
|
||||||
|
class C2(C1):
|
||||||
|
a = 'a'
|
||||||
|
def __init__(self):
|
||||||
|
self.b = 1
|
||||||
|
|
||||||
|
class C3:
|
||||||
|
def __dir__(self):
|
||||||
|
return ["custom"]
|
||||||
|
|
||||||
|
assert "__name__" in dir()
|
||||||
|
assert "a", "b" in dir(C2())
|
||||||
|
assert "c" in dir(C2())
|
||||||
|
assert ["custom"] == dir(C3())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user