def __init__(self, code): self.code = code if code in copyreg._inverted_registry: self.pair = copyreg._inverted_registry[code] copyreg.remove_extension(self.pair[0], self.pair[1], code) else: self.pair = None
def test_misc_globals(self): global mystr orig = mystr try: del mystr o = orig('hello') self.assertRaises(pickle.PicklingError, self.archiverClass.archivedDataWithRootObject_, o) finally: mystr = orig try: mystr = None o = orig('hello') self.assertRaises(pickle.PicklingError, self.archiverClass.archivedDataWithRootObject_, o) finally: mystr = orig try: copyreg.add_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) self.assertIn((a_newstyle_class.__module__, a_newstyle_class.__name__), copyreg._extension_registry) o = a_newstyle_class buf = self.archiverClass.archivedDataWithRootObject_(o) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, o) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, o) copyreg.remove_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) self.assertRaises(ValueError, self.unarchiverClass.unarchiveObjectWithData_, buf) finally: mystr = orig try: copyreg.remove_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) except ValueError: pass def f(): pass del f.__module__ try: sys.f = f buf = self.archiverClass.archivedDataWithRootObject_(f) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, f) finally: del f
def restore(self): code = self.code curpair = copyreg._inverted_registry.get(code) if curpair is not None: copyreg.remove_extension(curpair[0], curpair[1], code) pair = self.pair if pair is not None: copyreg.add_extension(pair[0], pair[1], code)
def test_extension_registry(self): mod, func, code = 'junk1 ', ' junk2', 0xabcd e = ExtensionSaver(code) try: # Shouldn't be in registry now. self.assertRaises(ValueError, copyreg.remove_extension, mod, func, code) copyreg.add_extension(mod, func, code) # Should be in the registry. self.assertTrue(copyreg._extension_registry[mod, func] == code) self.assertTrue(copyreg._inverted_registry[code] == (mod, func)) # Shouldn't be in the cache. self.assertNotIn(code, copyreg._extension_cache) # Redundant registration should be OK. copyreg.add_extension(mod, func, code) # shouldn't blow up # Conflicting code. self.assertRaises(ValueError, copyreg.add_extension, mod, func, code + 1) self.assertRaises(ValueError, copyreg.remove_extension, mod, func, code + 1) # Conflicting module name. self.assertRaises(ValueError, copyreg.add_extension, mod[1:], func, code ) self.assertRaises(ValueError, copyreg.remove_extension, mod[1:], func, code ) # Conflicting function name. self.assertRaises(ValueError, copyreg.add_extension, mod, func[1:], code) self.assertRaises(ValueError, copyreg.remove_extension, mod, func[1:], code) # Can't remove one that isn't registered at all. if code + 1 not in copyreg._inverted_registry: self.assertRaises(ValueError, copyreg.remove_extension, mod[1:], func[1:], code + 1) finally: e.restore() # Shouldn't be there anymore. self.assertNotIn((mod, func), copyreg._extension_registry) # The code *may* be in copyreg._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: copyreg.add_extension(mod, func, code) copyreg.remove_extension(mod, func, code) finally: e.restore() # Ensure invalid codes blow up. for code in -1, 0, 0x80000000: self.assertRaises(ValueError, copyreg.add_extension, mod, func, code)
def test_extension_registry(self): mod, func, code = 'junk1 ', ' junk2', 0xabcd e = ExtensionSaver(code) try: # Shouldn't be in registry now. self.assertRaises(ValueError, copyreg.remove_extension, mod, func, code) copyreg.add_extension(mod, func, code) # Should be in the registry. self.assertTrue(copyreg._extension_registry[mod, func] == code) self.assertTrue(copyreg._inverted_registry[code] == (mod, func)) # Shouldn't be in the cache. self.assertTrue(code not in copyreg._extension_cache) # Redundant registration should be OK. copyreg.add_extension(mod, func, code) # shouldn't blow up # Conflicting code. self.assertRaises(ValueError, copyreg.add_extension, mod, func, code + 1) self.assertRaises(ValueError, copyreg.remove_extension, mod, func, code + 1) # Conflicting module name. self.assertRaises(ValueError, copyreg.add_extension, mod[1:], func, code ) self.assertRaises(ValueError, copyreg.remove_extension, mod[1:], func, code ) # Conflicting function name. self.assertRaises(ValueError, copyreg.add_extension, mod, func[1:], code) self.assertRaises(ValueError, copyreg.remove_extension, mod, func[1:], code) # Can't remove one that isn't registered at all. if code + 1 not in copyreg._inverted_registry: self.assertRaises(ValueError, copyreg.remove_extension, mod[1:], func[1:], code + 1) finally: e.restore() # Shouldn't be there anymore. self.assertTrue((mod, func) not in copyreg._extension_registry) # The code *may* be in copyreg._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: copyreg.add_extension(mod, func, code) copyreg.remove_extension(mod, func, code) finally: e.restore() # Ensure invalid codes blow up. for code in -1, 0, 0x80000000: self.assertRaises(ValueError, copyreg.add_extension, mod, func, code)
def test_extension_registry(self): mod, func, code = 'junk1 ', ' junk2', 43981 e = ExtensionSaver(code) try: self.assertRaises(ValueError, copyreg.remove_extension, mod, func, code) copyreg.add_extension(mod, func, code) self.assertTrue(copyreg._extension_registry[mod, func] == code) self.assertTrue(copyreg._inverted_registry[code] == (mod, func)) self.assertNotIn(code, copyreg._extension_cache) copyreg.add_extension(mod, func, code) self.assertRaises(ValueError, copyreg.add_extension, mod, func, code + 1) self.assertRaises(ValueError, copyreg.remove_extension, mod, func, code + 1) self.assertRaises(ValueError, copyreg.add_extension, mod[1:], func, code) self.assertRaises(ValueError, copyreg.remove_extension, mod[1:], func, code) self.assertRaises(ValueError, copyreg.add_extension, mod, func[1:], code) self.assertRaises(ValueError, copyreg.remove_extension, mod, func[1:], code) if code + 1 not in copyreg._inverted_registry: self.assertRaises(ValueError, copyreg.remove_extension, mod[1:], func[1:], code + 1) finally: e.restore() self.assertNotIn((mod, func), copyreg._extension_registry) for code in (1, 2147483647): e = ExtensionSaver(code) try: copyreg.add_extension(mod, func, code) copyreg.remove_extension(mod, func, code) finally: e.restore() for code in (-1, 0, 2147483648): self.assertRaises(ValueError, copyreg.add_extension, mod, func, code)
def test_remove_extension(self): #delete extension copyreg.remove_extension(random,obj,100) import argu1 copyreg.remove_extension(argu1,obj,1) module = True copyreg.remove_extension(module,obj,6) #remove extension which has not been registed self.assertRaises(ValueError,copyreg.remove_extension,random,obj,2) self.assertRaises(ValueError,copyreg.remove_extension,random,object(),100) self.assertRaises(ValueError,copyreg.remove_extension,argu1,obj,1) copyreg.add_extension(argu1,obj,1) self.assertRaises(ValueError,copyreg.remove_extension,argu1,obj,0)
def test_remove_extension(): #delete extension copyreg.remove_extension(_random,obj,100) import argu1 copyreg.remove_extension(argu1,obj,1) module = True copyreg.remove_extension(module,obj,6) #remove extension which has not been registed AssertError(ValueError,copyreg.remove_extension,_random,obj,2) AssertError(ValueError,copyreg.remove_extension,_random,object(),100) AssertError(ValueError,copyreg.remove_extension,argu1,obj,1) copyreg.add_extension(argu1,obj,1) AssertError(ValueError,copyreg.remove_extension,argu1,obj,0)
def test_misc_globals(self): global mystr orig = mystr try: del mystr o = orig('hello') data = NSMutableData.alloc().init() archiver = self.archiverClass.alloc().initForWritingWithMutableData_(data) self.assertRaises(pickle.PicklingError, archiver.encodeRootObject_, o) if self.archiverClass is NSKeyedArchiver: archiver.finishEncoding() finally: mystr = orig try: mystr = None o = orig('hello') data = NSMutableData.alloc().init() archiver = self.archiverClass.alloc().initForWritingWithMutableData_(data) self.assertRaises(pickle.PicklingError, archiver.encodeRootObject_, o) if self.archiverClass is NSKeyedArchiver: archiver.finishEncoding() finally: mystr = orig try: copyreg.add_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) self.assertIn((a_newstyle_class.__module__, a_newstyle_class.__name__), copyreg._extension_registry) o = a_newstyle_class buf = self.archiverClass.archivedDataWithRootObject_(o) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, o) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, o) copyreg.remove_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) self.assertRaises(ValueError, self.unarchiverClass.unarchiveObjectWithData_, buf) finally: mystr = orig try: copyreg.remove_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) except ValueError: pass def f(): pass del f.__module__ if hasattr(f, '__qualname__'): f.__qualname__ = f.__name__ try: sys.f = f buf = self.archiverClass.archivedDataWithRootObject_(f) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, f) finally: del f
def test_misc_globals(self): global mystr orig = mystr try: del mystr o = orig('hello') self.assertRaises(pickle.PicklingError, self.archiverClass.archivedDataWithRootObject_, o) finally: mystr = orig try: mystr = None o = orig('hello') self.assertRaises(pickle.PicklingError, self.archiverClass.archivedDataWithRootObject_, o) finally: mystr = orig try: copyreg.add_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) self.assertIn( (a_newstyle_class.__module__, a_newstyle_class.__name__), copyreg._extension_registry) o = a_newstyle_class buf = self.archiverClass.archivedDataWithRootObject_(o) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, o) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, o) copyreg.remove_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) self.assertRaises(ValueError, self.unarchiverClass.unarchiveObjectWithData_, buf) finally: mystr = orig try: copyreg.remove_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) except ValueError: pass def f(): pass del f.__module__ try: sys.f = f buf = self.archiverClass.archivedDataWithRootObject_(f) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, f) finally: del f
def test_misc_globals(self): global mystr orig = mystr try: del mystr o = orig('hello') data = NSMutableData.alloc().init() archiver = self.archiverClass.alloc().initForWritingWithMutableData_(data) self.assertRaises(pickle.PicklingError, archiver.encodeRootObject_, o) if self.archiverClass is NSKeyedArchiver: archiver.finishEncoding() finally: mystr = orig try: mystr = None o = orig('hello') data = NSMutableData.alloc().init() archiver = self.archiverClass.alloc().initForWritingWithMutableData_(data) self.assertRaises(pickle.PicklingError, archiver.encodeRootObject_, o) if self.archiverClass is NSKeyedArchiver: archiver.finishEncoding() finally: mystr = orig try: copyreg.add_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) self.assertIn((a_newstyle_class.__module__, a_newstyle_class.__name__), copyreg._extension_registry) o = a_newstyle_class buf = self.archiverClass.archivedDataWithRootObject_(o) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, o) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, o) copyreg.remove_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) if os_level_key(os_release()) >= os_level_key('10.11'): # On OSX 10.11 (un)archivers modify exceptions, which looses # enough information that PyObjC can no longer reconstruct # the correct Python exception exception = (objc.error, ValueError) else: exception = ValueError self.assertRaises(exception, self.unarchiverClass.unarchiveObjectWithData_, buf) finally: mystr = orig try: copyreg.remove_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) except ValueError: pass def f(): pass del f.__module__ if hasattr(f, '__qualname__'): f.__qualname__ = f.__name__ try: sys.f = f buf = self.archiverClass.archivedDataWithRootObject_(f) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, f) finally: del f
def test_misc_globals(self): global mystr orig = mystr try: del mystr o = orig('hello') data = NSMutableData.alloc().init() archiver = self.archiverClass.alloc( ).initForWritingWithMutableData_(data) self.assertRaises(pickle.PicklingError, archiver.encodeRootObject_, o) if self.archiverClass is NSKeyedArchiver: archiver.finishEncoding() finally: mystr = orig try: mystr = None o = orig('hello') data = NSMutableData.alloc().init() archiver = self.archiverClass.alloc( ).initForWritingWithMutableData_(data) self.assertRaises(pickle.PicklingError, archiver.encodeRootObject_, o) if self.archiverClass is NSKeyedArchiver: archiver.finishEncoding() finally: mystr = orig try: copyreg.add_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) self.assertIn( (a_newstyle_class.__module__, a_newstyle_class.__name__), copyreg._extension_registry) o = a_newstyle_class buf = self.archiverClass.archivedDataWithRootObject_(o) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, o) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, o) copyreg.remove_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) self.assertRaises(ValueError, self.unarchiverClass.unarchiveObjectWithData_, buf) finally: mystr = orig try: copyreg.remove_extension(a_newstyle_class.__module__, a_newstyle_class.__name__, 42) except ValueError: pass def f(): pass del f.__module__ if hasattr(f, '__qualname__'): f.__qualname__ = f.__name__ try: sys.f = f buf = self.archiverClass.archivedDataWithRootObject_(f) self.assertIsInstance(buf, NSData) v = self.unarchiverClass.unarchiveObjectWithData_(buf) self.assertIs(v, f) finally: del f
def update_event(self, inp=-1): self.set_output_val(0, copyreg.remove_extension(self.input(0), self.input(1), self.input(2)))