| 1 |
""" |
| 2 |
Plain Python definition of the builtin functions related to run-time |
| 3 |
program introspection. |
| 4 |
""" |
| 5 |
|
| 6 |
import sys |
| 7 |
|
| 8 |
from __pypy__ import lookup_special |
| 9 |
|
| 10 |
def _caller_locals(): |
| 11 |
return sys._getframe(0).f_locals |
| 12 |
|
| 13 |
def vars(*obj): |
| 14 |
"""Return a dictionary of all the attributes currently bound in obj. If |
| 15 |
called with no argument, return the variables bound in local scope.""" |
| 16 |
|
| 17 |
if len(obj) == 0: |
| 18 |
return _caller_locals() |
| 19 |
elif len(obj) != 1: |
| 20 |
raise TypeError("vars() takes at most 1 argument.") |
| 21 |
try: |
| 22 |
return obj[0].__dict__ |
| 23 |
except AttributeError: |
| 24 |
raise TypeError("vars() argument must have __dict__ attribute") |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
class types(object): |
| 30 |
class _C: |
| 31 |
def _m(self): pass |
| 32 |
ModuleType = type(sys) |
| 33 |
ClassType = type(_C) |
| 34 |
TypeType = type |
| 35 |
_x = _C() |
| 36 |
InstanceType = type(_x) |
| 37 |
|
| 38 |
def dir(*args): |
| 39 |
"""dir([object]) -> list of strings |
| 40 |
|
| 41 |
Return an alphabetized list of names comprising (some of) the attributes |
| 42 |
of the given object, and of attributes reachable from it: |
| 43 |
|
| 44 |
No argument: the names in the current scope. |
| 45 |
Module object: the module attributes. |
| 46 |
Type or class object: its attributes, and recursively the attributes of |
| 47 |
its bases. |
| 48 |
Otherwise: its attributes, its class's attributes, and recursively the |
| 49 |
attributes of its class's base classes. |
| 50 |
""" |
| 51 |
if len(args) > 1: |
| 52 |
raise TypeError("dir expected at most 1 arguments, got %d" % len(args)) |
| 53 |
if len(args) == 0: |
| 54 |
return sorted(_caller_locals().keys()) |
| 55 |
|
| 56 |
obj = args[0] |
| 57 |
dir_meth = lookup_special(obj, '__dir__') |
| 58 |
if dir_meth is not None: |
| 59 |
|
| 60 |
result = dir_meth(obj) if obj is None else dir_meth() |
| 61 |
|
| 62 |
return sorted(result) |
| 63 |
|
| 64 |
return [] |