[Python-checkins] python/dist/src/Mac/Tools/IDE PyBrowser.py,1.17,1.18

jvr@users.sourceforge.net jvr@users.sourceforge.net
Sun, 01 Dec 2002 14:10:39 -0800


Update of /cvsroot/python/python/dist/src/Mac/Tools/IDE
In directory sc8-pr-cvs1:/tmp/cvs-serv18532

Modified Files:
	PyBrowser.py 
Log Message:
- reworked the object unpacking code, now supports new-style objects more
  or less decently/completely.
- cleaned up a little.



Index: PyBrowser.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Mac/Tools/IDE/PyBrowser.py,v
retrieving revision 1.17
retrieving revision 1.18
diff -C2 -d -r1.17 -r1.18
*** PyBrowser.py	30 Nov 2002 00:01:25 -0000	1.17
--- PyBrowser.py	1 Dec 2002 22:10:36 -0000	1.18
***************
*** 503,533 ****
  
  SIMPLE_TYPES = (
! 	types.NoneType,
! 	types.IntType,
! 	types.LongType,
! 	types.FloatType,
! 	types.ComplexType,
! 	types.StringType
  )
  
! INDEXING_TYPES = (
! 	types.TupleType,
! 	types.ListType,
! 	types.DictionaryType
! )
  
  def unpack_object(object, indent = 0):
  	tp = type(object)
! 	if tp in SIMPLE_TYPES and tp is not types.NoneType:
  		raise TypeError, "can't browse simple type: %s" % tp.__name__
! 	elif tp == types.DictionaryType:
  		return unpack_dict(object, indent)
! 	elif tp in (types.TupleType, types.ListType):
  		return unpack_sequence(object, indent)
! 	elif tp == types.InstanceType:
! 		return unpack_instance(object, indent)
! 	elif tp == types.ClassType:
! 		return unpack_class(object, indent)
! 	elif tp == types.ModuleType:
  		return unpack_dict(object.__dict__, indent)
  	else:
--- 503,553 ----
  
  SIMPLE_TYPES = (
! 	type(None),
! 	int,
! 	long,
! 	float,
! 	complex,
! 	str,
! 	unicode,
  )
  
! def get_ivars(obj):
! 	"""Return a list the names of all (potential) instance variables."""
! 	# __mro__ recipe from Guido
! 	slots = {}
! 	# old-style C objects
! 	if hasattr(obj, "__members__"):
! 		for name in obj.__members__:
! 			slots[name] = None
! 	if hasattr(obj, "__methods__"):
! 		for name in obj.__methods__:
! 			slots[name] = None
! 	# generic type
! 	if hasattr(obj, "__dict__"):
! 		slots.update(obj.__dict__)
! 	cls = type(obj)
! 	if hasattr(cls, "__mro__"):
! 		# new-style class, use descriptors
! 		for base in cls.__mro__:
! 			for name, value in base.__dict__.items():
! 				# XXX using callable() is a heuristic which isn't 100%
! 				# foolproof.
! 				if hasattr(value, "__get__") and not callable(value):
! 					slots[name] = None
! 	if "__dict__" in slots:
! 		del slots["__dict__"]
! 	slots = slots.keys()
! 	slots.sort()
! 	return slots
  
  def unpack_object(object, indent = 0):
  	tp = type(object)
! 	if isinstance(object, SIMPLE_TYPES) and object is not None:
  		raise TypeError, "can't browse simple type: %s" % tp.__name__
! 	elif isinstance(object, dict):
  		return unpack_dict(object, indent)
! 	elif isinstance(object, (tuple, list)):
  		return unpack_sequence(object, indent)
! 	elif isinstance(object, types.ModuleType):
  		return unpack_dict(object.__dict__, indent)
  	else:
***************
*** 556,576 ****
  
  def unpack_other(object, indent = 0):
! 	attrs = []
! 	if hasattr(object, '__members__'):
! 		attrs = attrs + object.__members__
! 	if hasattr(object, '__methods__'):
! 		attrs = attrs + object.__methods__
! 	if hasattr(object, '__dict__'):
! 		attrs = attrs + object.__dict__.keys()
! 	if hasattr(object, '__slots__'):
! 		# XXX??
! 		attrs = attrs + object.__slots__
! 	if hasattr(object, "__class__") and "__class__" not in attrs:
! 		attrs.append("__class__")
! 	if hasattr(object, "__doc__") and "__doc__" not in attrs:
! 		attrs.append("__doc__")
  	items = []
  	for attr in attrs:
! 		items.append((attr, getattr(object, attr)))
  	return pack_items(items, indent)
  
--- 576,588 ----
  
  def unpack_other(object, indent = 0):
! 	attrs = get_ivars(object)
  	items = []
  	for attr in attrs:
! 		try:
! 			value = getattr(object, attr)
! 		except:
! 			pass
! 		else:
! 			items.append((attr, value))
  	return pack_items(items, indent)