Python __builtin__.__name__() Examples

The following are 27 code examples of __builtin__.__name__(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module __builtin__ , or try the search function .
Example #1
Source File: test_class_jy.py    From medicare-demo with Apache License 2.0 6 votes vote down vote up
def test_builtin_attributes(self):
        for attr, val in dict(__name__='foo', __module__='bar', __dict__={},
                              __flags__=1, __base__=object,
                              __bases__=(unicode, object),
                              __mro__=(unicode, object)).iteritems():
            try:
                setattr(str, attr, val)
            except TypeError, te:
                self.assertEqual(str(te), self.TE_MSG)
            else:
                self.assert_(False,
                             'setattr str.%s expected a TypeError' % attr)
            try:
                delattr(str, attr)
            except TypeError, te:
                self.assertEqual(str(te), self.TE_MSG) 
Example #2
Source File: test_class_jy.py    From CTFCrackTools with GNU General Public License v3.0 6 votes vote down vote up
def test_builtin_attributes(self):
        for attr, val in dict(__name__='foo', __module__='bar', __dict__={},
                              __flags__=1, __base__=object,
                              __bases__=(unicode, object),
                              __mro__=(unicode, object)).iteritems():
            try:
                setattr(str, attr, val)
            except TypeError, te:
                self.assertEqual(str(te), self.TE_MSG)
            else:
                self.assert_(False,
                             'setattr str.%s expected a TypeError' % attr)
            try:
                delattr(str, attr)
            except TypeError, te:
                self.assertEqual(str(te), self.TE_MSG) 
Example #3
Source File: test_class_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 6 votes vote down vote up
def test_builtin_attributes(self):
        for attr, val in dict(__name__='foo', __module__='bar', __dict__={},
                              __flags__=1, __base__=object,
                              __bases__=(unicode, object),
                              __mro__=(unicode, object)).iteritems():
            try:
                setattr(str, attr, val)
            except TypeError, te:
                self.assertEqual(str(te), self.TE_MSG)
            else:
                self.assert_(False,
                             'setattr str.%s expected a TypeError' % attr)
            try:
                delattr(str, attr)
            except TypeError, te:
                self.assertEqual(str(te), self.TE_MSG) 
Example #4
Source File: test_util.py    From pex with Apache License 2.0 6 votes vote down vote up
def test_access_zipped_assets(
    mock_resource_string,
    mock_resource_isdir,
    mock_resource_listdir,
    mock_safe_mkdir,
    mock_safe_mkdtemp):

  mock_open = mock.mock_open()
  mock_safe_mkdtemp.side_effect = iter(['tmpJIMMEH', 'faketmpDir'])
  mock_resource_listdir.side_effect = iter([['./__init__.py', './directory/'], ['file.py']])
  mock_resource_isdir.side_effect = iter([False, True, False])
  mock_resource_string.return_value = 'testing'

  with mock.patch('%s.open' % python_builtins.__name__, mock_open, create=True):
    temp_dir = DistributionHelper.access_zipped_assets('twitter.common', 'dirutil')
    assert mock_resource_listdir.call_count == 2
    assert mock_open.call_count == 2
    file_handle = mock_open.return_value.__enter__.return_value
    assert file_handle.write.call_count == 2
    assert mock_safe_mkdtemp.mock_calls == [mock.call()]
    assert temp_dir == 'tmpJIMMEH'
    assert mock_safe_mkdir.mock_calls == [mock.call(os.path.join('tmpJIMMEH', 'directory'))] 
Example #5
Source File: test_class_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_attributes(self):
        class Foo(object):
            pass

        Foo.__name__ = 'Bar'
        self.assertEqual(Foo.__name__, 'Bar')
        try:
            del Foo.__name__
        except TypeError, te:
            self.assertEqual(str(te), "can't delete Bar.__name__") 
Example #6
Source File: test_class_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_repr_with_metaclass(self):
        # http://bugs.jython.org/issue1131
        class FooMetaclass(type):
            def __new__(cls, name, bases, attrs):
                return super(FooMetaclass, cls).__new__(cls, name, bases, attrs)

        class Foo(object):
            __metaclass__ = FooMetaclass
        self.assertEqual("<class '%s.Foo'>" % __name__, repr(Foo)) 
Example #7
Source File: test_class_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_java_class_name(self):
        # The __name__ and __module__ attributes of Java classes should
        # be set according to the same convention that Python uses.
        from java.lang import String
        self.assertEqual(String.__name__, "String")
        self.assertEqual(String.__module__, "java.lang") 
Example #8
Source File: test_class_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def tearDown(self):
        global __name__
        __builtin__.__name__ = self.builtin_name
        __name__ = self.name 
Example #9
Source File: test_class_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def tearDown(self):
        global __name__
        __name__ = self.name 
Example #10
Source File: test_class_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        global __name__
        self.name = __name__
        del __name__ 
Example #11
Source File: test_class_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_attributes(self):
        class Foo(object):
            pass

        Foo.__name__ = 'Bar'
        self.assertEqual(Foo.__name__, 'Bar')
        try:
            del Foo.__name__
        except TypeError, te:
            self.assertEqual(str(te), "can't delete Bar.__name__") 
Example #12
Source File: test_class_jy.py    From CTFCrackTools with GNU General Public License v3.0 5 votes vote down vote up
def test_dunder_module(self):
        self.assertEqual(str.__module__, '__builtin__')
        class Foo:
            pass
        Fu = types.ClassType('Fu', (), {})
        for cls in Foo, Fu:
            self.assert_('__module__' in cls.__dict__)
            self.assertEqual(cls.__module__, __name__)
            self.assertEqual(str(cls), '%s.%s' % (__name__, cls.__name__))
            self.assert_(repr(cls).startswith('<class %s.%s at' %
                                              (__name__, cls.__name__)))
            obj = cls()
            self.assert_(str(obj).startswith('<%s.%s instance at' %
                                             (__name__, cls.__name__)))

        class Bar(object):
            pass
        class Baz(Object):
            pass
        Bang = type('Bang', (), {})
        for cls in Bar, Baz, Bang:
            self.assert_('__module__' in cls.__dict__)
            self.assertEqual(cls.__module__, __name__)
            self.assertEqual(str(cls), "<class '%s.%s'>" % (__name__, cls.__name__))
            self.assertEqual(repr(cls), "<class '%s.%s'>" % (__name__, cls.__name__))
        self.assert_(str(Bar()).startswith('<%s.Bar object at' % __name__))
        self.assert_(str(Baz()).startswith("org.python.proxies.%s$Baz" % __name__)) 
Example #13
Source File: test_class_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_repr_with_metaclass(self):
        # http://bugs.jython.org/issue1131
        class FooMetaclass(type):
            def __new__(cls, name, bases, attrs):
                return super(FooMetaclass, cls).__new__(cls, name, bases, attrs)

        class Foo(object):
            __metaclass__ = FooMetaclass
        self.assertEqual("<class '%s.Foo'>" % __name__, repr(Foo)) 
Example #14
Source File: test_class_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_java_class_name(self):
        # The __name__ and __module__ attributes of Java classes should
        # be set according to the same convention that Python uses.
        from java.lang import String
        self.assertEqual(String.__name__, "String")
        self.assertEqual(String.__module__, "java.lang") 
Example #15
Source File: test_class_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def tearDown(self):
        global __name__
        __builtin__.__name__ = self.builtin_name
        __name__ = self.name 
Example #16
Source File: test_class_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def tearDown(self):
        global __name__
        __name__ = self.name 
Example #17
Source File: test_class_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def setUp(self):
        global __name__
        self.name = __name__
        del __name__ 
Example #18
Source File: test_class_jy.py    From CTFCrackTools-V2 with GNU General Public License v3.0 5 votes vote down vote up
def test_dunder_module(self):
        self.assertEqual(str.__module__, '__builtin__')
        class Foo:
            pass
        Fu = types.ClassType('Fu', (), {})
        for cls in Foo, Fu:
            self.assert_('__module__' in cls.__dict__)
            self.assertEqual(cls.__module__, __name__)
            self.assertEqual(str(cls), '%s.%s' % (__name__, cls.__name__))
            self.assert_(repr(cls).startswith('<class %s.%s at' %
                                              (__name__, cls.__name__)))
            obj = cls()
            self.assert_(str(obj).startswith('<%s.%s instance at' %
                                             (__name__, cls.__name__)))

        class Bar(object):
            pass
        class Baz(Object):
            pass
        Bang = type('Bang', (), {})
        for cls in Bar, Baz, Bang:
            self.assert_('__module__' in cls.__dict__)
            self.assertEqual(cls.__module__, __name__)
            self.assertEqual(str(cls), "<class '%s.%s'>" % (__name__, cls.__name__))
            self.assertEqual(repr(cls), "<class '%s.%s'>" % (__name__, cls.__name__))
        self.assert_(str(Bar()).startswith('<%s.Bar object at' % __name__))
        self.assert_(str(Baz()).startswith("org.python.proxies.%s$Baz" % __name__)) 
Example #19
Source File: test_class_jy.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_repr_with_metaclass(self):
        # http://bugs.jython.org/issue1131
        class FooMetaclass(type):
            def __new__(cls, name, bases, attrs):
                return super(FooMetaclass, cls).__new__(cls, name, bases, attrs)

        class Foo(object):
            __metaclass__ = FooMetaclass
        self.assertEqual("<class '%s.Foo'>" % __name__, repr(Foo)) 
Example #20
Source File: test_class_jy.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_java_class_name(self):
        # The __name__ and __module__ attributes of Java classes should
        # be set according to the same convention that Python uses.
        from java.lang import String
        self.assertEqual(String.__name__, "String")
        self.assertEqual(String.__module__, "java.lang") 
Example #21
Source File: test_class_jy.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def tearDown(self):
        global __name__
        __builtin__.__name__ = self.builtin_name
        __name__ = self.name 
Example #22
Source File: test_class_jy.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def tearDown(self):
        global __name__
        __name__ = self.name 
Example #23
Source File: test_class_jy.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def setUp(self):
        global __name__
        self.name = __name__
        del __name__ 
Example #24
Source File: test_class_jy.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_attributes(self):
        class Foo(object):
            pass

        Foo.__name__ = 'Bar'
        self.assertEqual(Foo.__name__, 'Bar')
        try:
            del Foo.__name__
        except TypeError, te:
            self.assertEqual(str(te), "can't delete Bar.__name__") 
Example #25
Source File: test_class_jy.py    From medicare-demo with Apache License 2.0 5 votes vote down vote up
def test_dunder_module(self):
        self.assertEqual(str.__module__, '__builtin__')
        class Foo:
            pass
        Fu = types.ClassType('Fu', (), {})
        for cls in Foo, Fu:
            self.assert_('__module__' in cls.__dict__)
            self.assertEqual(cls.__module__, __name__)
            self.assertEqual(str(cls), '%s.%s' % (__name__, cls.__name__))
            self.assert_(repr(cls).startswith('<class %s.%s at' %
                                              (__name__, cls.__name__)))
            obj = cls()
            self.assert_(str(obj).startswith('<%s.%s instance at' %
                                             (__name__, cls.__name__)))

        class Bar(object):
            pass
        class Baz(Object):
            pass
        Bang = type('Bang', (), {})
        for cls in Bar, Baz, Bang:
            self.assert_('__module__' in cls.__dict__)
            self.assertEqual(cls.__module__, __name__)
            self.assertEqual(str(cls), "<class '%s.%s'>" % (__name__, cls.__name__))
            self.assertEqual(repr(cls), "<class '%s.%s'>" % (__name__, cls.__name__))
        self.assert_(str(Bar()).startswith('<%s.Bar object at' % __name__))
        self.assert_(str(Baz()).startswith("org.python.proxies.%s$Baz" % __name__)) 
Example #26
Source File: util.py    From python-hunter with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __get__(self, obj, cls):
        if obj is None:
            return self
        value = obj.__dict__[self.func.__name__] = self.func(obj)
        return value 
Example #27
Source File: util.py    From python-hunter with BSD 2-Clause "Simplified" License 4 votes vote down vote up
def safe_repr(obj, maxdepth=5):
    if not maxdepth:
        return '...'
    obj_type = type(obj)
    obj_type_type = type(obj_type)
    newdepth = maxdepth - 1

    # only represent exact builtins
    # (subclasses can have side-effects due to __class__ as a property, __instancecheck__, __subclasscheck__ etc)
    if obj_type is dict:
        return '{%s}' % ', '.join('%s: %s' % (
            safe_repr(k, maxdepth),
            safe_repr(v, newdepth)
        ) for k, v in obj.items())
    elif obj_type is list:
        return '[%s]' % ', '.join(safe_repr(i, newdepth) for i in obj)
    elif obj_type is tuple:
        return '(%s%s)' % (', '.join(safe_repr(i, newdepth) for i in obj), ',' if len(obj) == 1 else '')
    elif obj_type is set:
        return '{%s}' % ', '.join(safe_repr(i, newdepth) for i in obj)
    elif obj_type is frozenset:
        return '%s({%s})' % (obj_type.__name__, ', '.join(safe_repr(i, newdepth) for i in obj))
    elif obj_type is deque:
        return '%s([%s])' % (obj_type.__name__, ', '.join(safe_repr(i, newdepth) for i in obj))
    elif obj_type in (Counter, OrderedDict, defaultdict):
        return '%s({%s})' % (
            obj_type.__name__,
            ', '.join('%s: %s' % (
                safe_repr(k, maxdepth),
                safe_repr(v, newdepth)
            ) for k, v in obj.items())
        )
    elif obj_type is types.MethodType:  # noqa
        self = obj.__self__
        name = getattr(obj, '__qualname__', None)
        if name is None:
            name = obj.__name__
        return '<%sbound method %s of %s>' % ('un' if self is None else '', name, safe_repr(self, newdepth))
    elif obj_type_type is type and BaseException in obj_type.__mro__:
        return '%s(%s)' % (obj_type.__name__, ', '.join(safe_repr(i, newdepth) for i in obj.args))
    elif obj_type_type is type and obj_type is not InstanceType and obj_type.__module__ in (builtins.__name__, 'io', 'socket', '_socket'):
        # hardcoded list of safe things. note that isinstance ain't used
        # (we don't trust subclasses to do the right thing in __repr__)
        return repr(obj)
    else:
        # if the object has a __dict__ then it's probably an instance of a pure python class, assume bad things
        #  with side-effects will be going on in __repr__ - use the default instead (object.__repr__)
        return object.__repr__(obj)