Commits

jason kirtland committed ed91bec

Set the __module__ of @class_cloner-generated types like Python syntactic subclassing does, on Pythons that support frame inspection.

  • Participants
  • Parent commits 089f7ad

Comments (0)

Files changed (2)

flatland/util/base.py

 # -*- coding: utf-8; fill-column: 78 -*-
 import re
 import string
+import sys
 
 try:
     import threading
                 return instance.__dict__[self.name]
             except KeyError:
                 raise AttributeError(self.name)
-        members = {}
+        members = {'__doc__': getattr(cls, '__doc__', '')}
         try:
-            members['__doc__'] = getattr(cls, '__doc__', '')
+            members['__module__'] = \
+              sys._getframe(1).f_globals['__name__']
+        except (AttributeError, KeyError, TypeError):  # pragma: nocover
             members['__module__'] = cls.__module__
-        except KeyError:
-            pass
         clone = type(cls.__name__, (cls,), members)
         return self.cloner.__get__(None, clone)
 

tests/schema/test_base.py

 from tests._util import assert_raises, eq_, requires_unicode_coercion
 
 
+def test_cloning():
+    new_element = Element.named(u'x')
+    assert isinstance(new_element, type)
+    assert new_element.__module__ != Element.__module__
+    assert 'test_base' in new_element.__module__
+
+
 @requires_unicode_coercion
 def test_naming():
     for arg in (u'unicode', 'sysencoding', None):