Commits

Kirill Simonov committed baec861

Use the types module instead of constructing type objects by hand. Fix #41. Thanks to v.haisman@sh.cvut.cz for the patch.

  • Participants
  • Parent commits eb81eab

Comments (0)

Files changed (3)

File lib/yaml/constructor.py

 except NameError:
     from sets import Set as set
 
-import binascii, re, sys
+import binascii, re, sys, types
 
 class ConstructorError(MarkedYAMLError):
     pass
         if self.check_node():
             return self.construct_document(self.get_node())
 
-    def g(): yield None
-    generator_type = type(g())
-    del g
-
     def construct_document(self, node):
         data = self.construct_object(node)
         while self.state_generators:
             data = constructor(self, node)
         else:
             data = constructor(self, tag_suffix, node)
-        if isinstance(data, self.generator_type):
+        if isinstance(data, types.GeneratorType):
             generator = data
             data = generator.next()
             if self.deep_construct:

File lib/yaml/parser.py

                     token = self.get_token()
                     end_mark = token.end_mark
                     anchor = token.value
-            if tag is not None and tag != u'!':
+            if tag is not None:
                 handle, suffix = tag
                 if handle is not None:
                     if handle not in self.tag_handles:

File lib/yaml/representer.py

 except NameError:
     from sets import Set as set
 
-import sys, copy_reg
+import sys, copy_reg, types
 
 class RepresenterError(YAMLError):
     pass
         self.object_keeper = []
         self.alias_key = None
 
-    class C: pass
-    c = C()
-    def f(): pass
-    def g(): yield None
-    classobj_type = type(C)
-    instance_type = type(c)
-    function_type = type(f)
-    generator_type = type(g())
-    builtin_function_type = type(abs)
-    module_type = type(sys)
-    del C, c, f, g
-
     def get_classobj_bases(self, cls):
         bases = [cls]
         for base in cls.__bases__:
             #self.represented_objects[alias_key] = None
             self.object_keeper.append(data)
         data_types = type(data).__mro__
-        if type(data) is self.instance_type:
+        if type(data) is types.InstanceType:
             data_types = self.get_classobj_bases(data.__class__)+list(data_types)
         if data_types[0] in self.yaml_representers:
             node = self.yaml_representers[data_types[0]](self, data)
 Representer.add_representer(type,
         Representer.represent_name)
 
-Representer.add_representer(Representer.classobj_type,
+Representer.add_representer(types.ClassType,
         Representer.represent_name)
 
-Representer.add_representer(Representer.function_type,
+Representer.add_representer(types.FunctionType,
         Representer.represent_name)
 
-Representer.add_representer(Representer.builtin_function_type,
+Representer.add_representer(types.BuiltinFunctionType,
         Representer.represent_name)
 
-Representer.add_representer(Representer.module_type,
+Representer.add_representer(types.ModuleType,
         Representer.represent_module)
 
-Representer.add_multi_representer(Representer.instance_type,
+Representer.add_multi_representer(types.InstanceType,
         Representer.represent_instance)
 
 Representer.add_multi_representer(object,