Python copy_reg._extension_cache() Examples

The following are 9 code examples of copy_reg._extension_cache(). 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 copy_reg , or try the search function .
Example #1
Source File: _pycoder.py    From MIA-Dictionary-Addon with GNU General Public License v3.0 6 votes vote down vote up
def load_global_ext(coder, setValue):
    if coder.allowsKeyedCoding():
        code = coder.decodeIntForKey_(kCODE)
    else:
        code = coder.decodeValueOfObjCType_at_(objc._C_INT, None)
    nil = []
    obj = copyreg._extension_cache.get(code, nil)
    if obj is not nil:
        return obj
    key = copyreg._inverted_registry.get(code)
    if not key:
        raise ValueError("unregistered extension code %d" % code)

    module, name = key
    mod = import_module(module)
    klass = _getattribute(mod, name)
    copyreg._extension_cache[code] = klass
    return klass 
Example #2
Source File: test_copy_reg.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_extension_cache(self):
        #set and get the attribute
        rand = random.Random()
        value = rand.getrandbits(8)
        copy_reg._extension_cache['cache1'] = value
        result = copy_reg._extension_cache['cache1']
        self.assertTrue(result == value,
            "The get and set of the attribute failed")
        
        value = rand.getrandbits(16)
        copy_reg._extension_cache['cache2'] = value
        result = copy_reg._extension_cache['cache2']
        self.assertTrue(result == value,
            "The get and set of the attribute failed")

        #change the value of the attribue
        value2 = rand.getrandbits(4)
        copy_reg._extension_cache['cache1'] = value2
        result = copy_reg._extension_cache['cache1']
        self.assertTrue(result == value2,
            "The get and set of the attribute failed")
        
        if not copy_reg._extension_cache.has_key('cache1') or  not copy_reg._extension_cache.has_key('cache2'):
            Fail("Set of the attribute failed")
            
        copy_reg.clear_extension_cache()
        if  copy_reg._extension_cache.has_key('cache1') or copy_reg._extension_cache.has_key('cache2'):
            Fail("The method clear_extension_cache did not work correctly ") 
Example #3
Source File: test_copy_reg.py    From ironpython2 with Apache License 2.0 4 votes vote down vote up
def test_extension_registry(self):
        mod, func, code = 'junk1 ', ' junk2', 0xabcd
        e = ExtensionSaver(code)
        try:
            # Shouldn't be in registry now.
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code)
            copy_reg.add_extension(mod, func, code)
            # Should be in the registry.
            self.assertTrue(copy_reg._extension_registry[mod, func] == code)
            self.assertTrue(copy_reg._inverted_registry[code] == (mod, func))
            # Shouldn't be in the cache.
            self.assertNotIn(code, copy_reg._extension_cache)
            # Redundant registration should be OK.
            copy_reg.add_extension(mod, func, code)  # shouldn't blow up
            # Conflicting code.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code + 1)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code + 1)
            # Conflicting module name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod[1:], func, code )
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod[1:], func, code )
            # Conflicting function name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func[1:], code)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func[1:], code)
            # Can't remove one that isn't registered at all.
            if code + 1 not in copy_reg._inverted_registry:
                self.assertRaises(ValueError, copy_reg.remove_extension,
                                  mod[1:], func[1:], code + 1)

        finally:
            e.restore()

        # Shouldn't be there anymore.
        self.assertNotIn((mod, func), copy_reg._extension_registry)
        # The code *may* be in copy_reg._extension_registry, though, if
        # we happened to pick on a registered code.  So don't check for
        # that.

        # Check valid codes at the limits.
        for code in 1, 0x7fffffff:
            e = ExtensionSaver(code)
            try:
                copy_reg.add_extension(mod, func, code)
                copy_reg.remove_extension(mod, func, code)
            finally:
                e.restore()

        # Ensure invalid codes blow up.
        for code in -1, 0, 0x80000000L:
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code) 
Example #4
Source File: test_copy_reg.py    From BinderFilter with MIT License 4 votes vote down vote up
def test_extension_registry(self):
        mod, func, code = 'junk1 ', ' junk2', 0xabcd
        e = ExtensionSaver(code)
        try:
            # Shouldn't be in registry now.
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code)
            copy_reg.add_extension(mod, func, code)
            # Should be in the registry.
            self.assertTrue(copy_reg._extension_registry[mod, func] == code)
            self.assertTrue(copy_reg._inverted_registry[code] == (mod, func))
            # Shouldn't be in the cache.
            self.assertNotIn(code, copy_reg._extension_cache)
            # Redundant registration should be OK.
            copy_reg.add_extension(mod, func, code)  # shouldn't blow up
            # Conflicting code.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code + 1)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code + 1)
            # Conflicting module name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod[1:], func, code )
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod[1:], func, code )
            # Conflicting function name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func[1:], code)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func[1:], code)
            # Can't remove one that isn't registered at all.
            if code + 1 not in copy_reg._inverted_registry:
                self.assertRaises(ValueError, copy_reg.remove_extension,
                                  mod[1:], func[1:], code + 1)

        finally:
            e.restore()

        # Shouldn't be there anymore.
        self.assertNotIn((mod, func), copy_reg._extension_registry)
        # The code *may* be in copy_reg._extension_registry, though, if
        # we happened to pick on a registered code.  So don't check for
        # that.

        # Check valid codes at the limits.
        for code in 1, 0x7fffffff:
            e = ExtensionSaver(code)
            try:
                copy_reg.add_extension(mod, func, code)
                copy_reg.remove_extension(mod, func, code)
            finally:
                e.restore()

        # Ensure invalid codes blow up.
        for code in -1, 0, 0x80000000L:
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code) 
Example #5
Source File: test_copy_reg.py    From oss-ftp with MIT License 4 votes vote down vote up
def test_extension_registry(self):
        mod, func, code = 'junk1 ', ' junk2', 0xabcd
        e = ExtensionSaver(code)
        try:
            # Shouldn't be in registry now.
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code)
            copy_reg.add_extension(mod, func, code)
            # Should be in the registry.
            self.assertTrue(copy_reg._extension_registry[mod, func] == code)
            self.assertTrue(copy_reg._inverted_registry[code] == (mod, func))
            # Shouldn't be in the cache.
            self.assertNotIn(code, copy_reg._extension_cache)
            # Redundant registration should be OK.
            copy_reg.add_extension(mod, func, code)  # shouldn't blow up
            # Conflicting code.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code + 1)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code + 1)
            # Conflicting module name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod[1:], func, code )
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod[1:], func, code )
            # Conflicting function name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func[1:], code)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func[1:], code)
            # Can't remove one that isn't registered at all.
            if code + 1 not in copy_reg._inverted_registry:
                self.assertRaises(ValueError, copy_reg.remove_extension,
                                  mod[1:], func[1:], code + 1)

        finally:
            e.restore()

        # Shouldn't be there anymore.
        self.assertNotIn((mod, func), copy_reg._extension_registry)
        # The code *may* be in copy_reg._extension_registry, though, if
        # we happened to pick on a registered code.  So don't check for
        # that.

        # Check valid codes at the limits.
        for code in 1, 0x7fffffff:
            e = ExtensionSaver(code)
            try:
                copy_reg.add_extension(mod, func, code)
                copy_reg.remove_extension(mod, func, code)
            finally:
                e.restore()

        # Ensure invalid codes blow up.
        for code in -1, 0, 0x80000000L:
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code) 
Example #6
Source File: test_copy_reg.py    From gcblue with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def test_extension_registry(self):
        mod, func, code = 'junk1 ', ' junk2', 0xabcd
        e = ExtensionSaver(code)
        try:
            # Shouldn't be in registry now.
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code)
            copy_reg.add_extension(mod, func, code)
            # Should be in the registry.
            self.assertTrue(copy_reg._extension_registry[mod, func] == code)
            self.assertTrue(copy_reg._inverted_registry[code] == (mod, func))
            # Shouldn't be in the cache.
            self.assertNotIn(code, copy_reg._extension_cache)
            # Redundant registration should be OK.
            copy_reg.add_extension(mod, func, code)  # shouldn't blow up
            # Conflicting code.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code + 1)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code + 1)
            # Conflicting module name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod[1:], func, code )
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod[1:], func, code )
            # Conflicting function name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func[1:], code)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func[1:], code)
            # Can't remove one that isn't registered at all.
            if code + 1 not in copy_reg._inverted_registry:
                self.assertRaises(ValueError, copy_reg.remove_extension,
                                  mod[1:], func[1:], code + 1)

        finally:
            e.restore()

        # Shouldn't be there anymore.
        self.assertNotIn((mod, func), copy_reg._extension_registry)
        # The code *may* be in copy_reg._extension_registry, though, if
        # we happened to pick on a registered code.  So don't check for
        # that.

        # Check valid codes at the limits.
        for code in 1, 0x7fffffff:
            e = ExtensionSaver(code)
            try:
                copy_reg.add_extension(mod, func, code)
                copy_reg.remove_extension(mod, func, code)
            finally:
                e.restore()

        # Ensure invalid codes blow up.
        for code in -1, 0, 0x80000000L:
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code) 
Example #7
Source File: test_copy_reg.py    From medicare-demo with Apache License 2.0 4 votes vote down vote up
def test_extension_registry(self):
        mod, func, code = 'junk1 ', ' junk2', 0xabcd
        e = ExtensionSaver(code)
        try:
            # Shouldn't be in registry now.
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code)
            copy_reg.add_extension(mod, func, code)
            # Should be in the registry.
            self.assert_(copy_reg._extension_registry[mod, func] == code)
            self.assert_(copy_reg._inverted_registry[code] == (mod, func))
            # Shouldn't be in the cache.
            self.assert_(code not in copy_reg._extension_cache)
            # Redundant registration should be OK.
            copy_reg.add_extension(mod, func, code)  # shouldn't blow up
            # Conflicting code.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code + 1)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code + 1)
            # Conflicting module name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod[1:], func, code )
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod[1:], func, code )
            # Conflicting function name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func[1:], code)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func[1:], code)
            # Can't remove one that isn't registered at all.
            if code + 1 not in copy_reg._inverted_registry:
                self.assertRaises(ValueError, copy_reg.remove_extension,
                                  mod[1:], func[1:], code + 1)

        finally:
            e.restore()

        # Shouldn't be there anymore.
        self.assert_((mod, func) not in copy_reg._extension_registry)
        # The code *may* be in copy_reg._extension_registry, though, if
        # we happened to pick on a registered code.  So don't check for
        # that.

        # Check valid codes at the limits.
        for code in 1, 0x7fffffff:
            e = ExtensionSaver(code)
            try:
                copy_reg.add_extension(mod, func, code)
                copy_reg.remove_extension(mod, func, code)
            finally:
                e.restore()

        # Ensure invalid codes blow up.
        for code in -1, 0, 0x80000000L:
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code) 
Example #8
Source File: test_copy_reg.py    From CTFCrackTools-V2 with GNU General Public License v3.0 4 votes vote down vote up
def test_extension_registry(self):
        mod, func, code = 'junk1 ', ' junk2', 0xabcd
        e = ExtensionSaver(code)
        try:
            # Shouldn't be in registry now.
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code)
            copy_reg.add_extension(mod, func, code)
            # Should be in the registry.
            self.assertTrue(copy_reg._extension_registry[mod, func] == code)
            self.assertTrue(copy_reg._inverted_registry[code] == (mod, func))
            # Shouldn't be in the cache.
            self.assertNotIn(code, copy_reg._extension_cache)
            # Redundant registration should be OK.
            copy_reg.add_extension(mod, func, code)  # shouldn't blow up
            # Conflicting code.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code + 1)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code + 1)
            # Conflicting module name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod[1:], func, code )
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod[1:], func, code )
            # Conflicting function name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func[1:], code)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func[1:], code)
            # Can't remove one that isn't registered at all.
            if code + 1 not in copy_reg._inverted_registry:
                self.assertRaises(ValueError, copy_reg.remove_extension,
                                  mod[1:], func[1:], code + 1)

        finally:
            e.restore()

        # Shouldn't be there anymore.
        self.assertNotIn((mod, func), copy_reg._extension_registry)
        # The code *may* be in copy_reg._extension_registry, though, if
        # we happened to pick on a registered code.  So don't check for
        # that.

        # Check valid codes at the limits.
        for code in 1, 0x7fffffff:
            e = ExtensionSaver(code)
            try:
                copy_reg.add_extension(mod, func, code)
                copy_reg.remove_extension(mod, func, code)
            finally:
                e.restore()

        # Ensure invalid codes blow up.
        for code in -1, 0, 0x80000000L:
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code) 
Example #9
Source File: test_copy_reg.py    From CTFCrackTools with GNU General Public License v3.0 4 votes vote down vote up
def test_extension_registry(self):
        mod, func, code = 'junk1 ', ' junk2', 0xabcd
        e = ExtensionSaver(code)
        try:
            # Shouldn't be in registry now.
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code)
            copy_reg.add_extension(mod, func, code)
            # Should be in the registry.
            self.assertTrue(copy_reg._extension_registry[mod, func] == code)
            self.assertTrue(copy_reg._inverted_registry[code] == (mod, func))
            # Shouldn't be in the cache.
            self.assertNotIn(code, copy_reg._extension_cache)
            # Redundant registration should be OK.
            copy_reg.add_extension(mod, func, code)  # shouldn't blow up
            # Conflicting code.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code + 1)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func, code + 1)
            # Conflicting module name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod[1:], func, code )
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod[1:], func, code )
            # Conflicting function name.
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func[1:], code)
            self.assertRaises(ValueError, copy_reg.remove_extension,
                              mod, func[1:], code)
            # Can't remove one that isn't registered at all.
            if code + 1 not in copy_reg._inverted_registry:
                self.assertRaises(ValueError, copy_reg.remove_extension,
                                  mod[1:], func[1:], code + 1)

        finally:
            e.restore()

        # Shouldn't be there anymore.
        self.assertNotIn((mod, func), copy_reg._extension_registry)
        # The code *may* be in copy_reg._extension_registry, though, if
        # we happened to pick on a registered code.  So don't check for
        # that.

        # Check valid codes at the limits.
        for code in 1, 0x7fffffff:
            e = ExtensionSaver(code)
            try:
                copy_reg.add_extension(mod, func, code)
                copy_reg.remove_extension(mod, func, code)
            finally:
                e.restore()

        # Ensure invalid codes blow up.
        for code in -1, 0, 0x80000000L:
            self.assertRaises(ValueError, copy_reg.add_extension,
                              mod, func, code)