6868 long = None # Avoid NameError when evaluating "long"
6969
7070
71+ def is_class (dat ):
72+ """Return whether dat is a class."""
73+ if is_python3 :
74+ return isinstance (dat , type )
75+ else :
76+ return type (dat ) in (types .ClassType , types .TypeType )
77+
78+
79+ def is_instance (dat ):
80+ """Return whether dat is an instance of a class."""
81+ if is_python3 :
82+ return isinstance (type (dat ), type ) and not isinstance (dat , type )
83+ else :
84+ # ugh, classRE match is a bit of a hack :(
85+ return type (dat ) == types .InstanceType or classRE .match (str (type (dat )))
86+
87+
7188def get_name (obj ):
7289 """Return the name of an object."""
7390 return obj .__name__ if hasattr (obj , '__name__' ) else get_name (type (obj ))
@@ -182,7 +199,7 @@ def encode(self, dat):
182199 elif typ is types .BuiltinFunctionType :
183200 pretty_name = get_name (dat ) + '(...)'
184201 new_obj .extend (['FUNCTION' , pretty_name , None ])
185- elif self . is_class (dat ) or self . is_instance (dat ):
202+ elif is_class (dat ) or is_instance (dat ):
186203 self .encode_class_or_instance (dat , new_obj )
187204 elif typ is types .ModuleType :
188205 new_obj .extend (['module' , dat .__name__ ])
@@ -199,26 +216,9 @@ def encode(self, dat):
199216 return ret
200217
201218
202- def is_class (self , dat ):
203- """Return whether dat is a class."""
204- if is_python3 :
205- return isinstance (dat , type )
206- else :
207- return type (dat ) in (types .ClassType , types .TypeType )
208-
209-
210- def is_instance (self , dat ):
211- """Return whether dat is an instance of a class."""
212- if is_python3 :
213- return isinstance (type (dat ), type ) and not isinstance (dat , type )
214- else :
215- # ugh, classRE match is a bit of a hack :(
216- return type (dat ) == types .InstanceType or classRE .match (str (type (dat )))
217-
218-
219219 def encode_class_or_instance (self , dat , new_obj ):
220220 """Encode dat as a class or instance."""
221- if self . is_instance (dat ):
221+ if is_instance (dat ):
222222 class_name = get_name (dat .__class__ )
223223 new_obj .extend (['INSTANCE' , class_name ])
224224 # don't traverse inside modules, or else risk EXPLODING the visualization
0 commit comments