[pypy-dev] Re: New __builtin__
Pedro Rodriguez
pedro_rodriguez at club-internet.fr
Mon Jan 27 18:28:23 CET 2003
On Mon, 27 Jan 2003 17:07:17 +0100, Michael Hudson wrote:
> Scott Fenton <scott at fenton.baltimore.md.us> writes:
>
>> Stuff I think can be easily done in python that haven't been yet: dir,
>> raw_input, reload, round, and xrange.
>
> dir is probably possible but a bit tedious.
>
Don't know if this suits you, but some time ago I posted a dir version
I rewrote dir based on C code to emulate 2.2 behaviour in 1.5.2.
Didn't check if 'dir' code evolved since then.
OTH,
Pedro
# new_dir.py
""" Restranscript of python 2.2 dir() builtin.
"""
import types
import sys
def merge_class_dict(dict, aclass):
classdict = getattr(aclass, "__dict__", None)
if classdict is not None:
dict.update(classdict)
bases = getattr(aclass, "__bases__", None)
if bases is not None:
for base in bases:
merge_class_dict(dict, base)
def merge_list_attr(dict, obj, attrname):
list = getattr(obj, attrname, None)
if list is not None:
for item in list:
if type(item) is types.StringType:
dict[item] = None
def new_dir(obj=None):
objType = type(obj)
if obj is None:
result = locals().keys()
elif objType is types.ModuleType:
result = obj.__dict__.keys()
elif objType is types.ClassType or objType is types.TypeType:
dict = {}
merge_class_dict(dict, obj)
result = dict.keys()
else:
dict = getattr(obj, "__dict__", None)
if dict is None:
dict = {}
elif type(dict) is not types.DictType:
dict = {}
else:
dict = dict.copy()
merge_list_attr(dict, obj, "__members__")
merge_list_attr(dict, obj, "__methods__")
itsclass = getattr(obj, "__class__", None)
if itsclass is not None:
merge_class_dict(dict, itsclass)
result = dict.keys()
return result
try:
major, minor, micro, releaselevel, serial = sys.version_info
if (major, minor) >= (2, 2):
new_dir = dir
except:
pass
More information about the Pypy-dev
mailing list