def produce_global_ext(self, extcode, opcode): e = ExtensionSaver(extcode) try: copyreg.add_extension(__name__, "MyList", extcode) x = MyList([1, 2, 3]) x.foo = 42 x.bar = "hello" # Dump using protocol 1 for comparison. s1 = self.dumps(x, 1) self.assertIn(__name__.encode("utf-8"), s1) self.assertIn(b"MyList", s1) self.assertEqual(opcode_in_pickle(opcode, s1), False) y = self.loads(s1) self.assertEqual(list(x), list(y)) self.assertEqual(x.__dict__, y.__dict__) # Dump using protocol 2 for test. s2 = self.dumps(x, 2) self.assertNotIn(__name__.encode("utf-8"), s2) self.assertNotIn(b"MyList", s2) self.assertEqual(opcode_in_pickle(opcode, s2), True, repr(s2)) y = self.loads(s2) self.assertEqual(list(x), list(y)) self.assertEqual(x.__dict__, y.__dict__) finally: e.restore()
def test_extension_registry(self): #test getattr of the attribute and how the value of this attribute affects other method copyreg.add_extension('a', 'b', 123) key = copyreg._inverted_registry[123] result = copyreg._extension_registry code = result[key] self.assertTrue( code == 123, "The _extension_registry attribute did not return the correct value" ) copyreg.add_extension('1', '2', 999) result = copyreg._extension_registry code = result[('1', '2')] self.assertTrue( code == 999, "The _extension_registry attribute did not return the correct value" ) #general test, try to set the attribute then to get it myvalue = 3885 copyreg._extension_registry["key"] = myvalue result = copyreg._extension_registry["key"] self.assertTrue(result == myvalue, "The set or the get of the attribute failed")
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_inverted_registry(self): copyreg.add_extension('obj1','obj2',64) #get result = copyreg._inverted_registry[64] self.assertTrue(result == ('obj1','obj2'), "The _inverted_registry attribute did not return the correct value") #set value = ('newmodule','newobj') copyreg._inverted_registry[10001] = value result = copyreg._inverted_registry[10001] self.assertTrue(result == value, "The setattr of _inverted_registry attribute failed")
def produce_global_ext(self, extcode, opcode): e = test.pickletester.ExtensionSaver(extcode) try: copyreg.add_extension(__name__, "MyList", extcode) x = MyList([1, 2, 3]) x.foo = 42 x.bar = "hello" s1 = self.dumps(x, 1) y = self.loads(s1) self.assertEqual(list(x), list(y)) self.assertEqual(x.__dict__, y.__dict__) finally: e.restore()
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 produce_global_ext(self, extcode): e = ExtensionSaver(extcode) try: copyreg.add_extension(__name__, "MyList", extcode) x = MyList([1, 2, 3]) x.foo = 42 x.bar = "hello" # Dump using protocol 2 for test. s2 = self.dumps(x, 2) self.assertNotIn(__name__.encode("utf-8"), s2) self.assertNotIn(b"MyList", s2) y = self.loads(s2) self.assertEqual(list(x), list(y)) self.assertEqual(x.__dict__, y.__dict__) finally: e.restore()
def test_add_extension(self): global obj obj = object() #The module is system defined module:random copyreg.add_extension(random,obj,100) #The module is a custom mudole or the module argument is not a type of module global mod mod = imp.new_module('module') sys.modules['argu1'] = mod import argu1 copyreg.add_extension(argu1,obj,1) module = True copyreg.add_extension(module,obj,6) # the value is zero or less than zero module = "module" self.assertRaises(ValueError,copyreg.add_extension,module,obj,0) self.assertRaises(ValueError,copyreg.add_extension,module,object(),-987654) # the key is already registered with code self.assertRaises(ValueError,copyreg.add_extension,argu1,object(),100) # the code is already in use for key self.assertRaises(ValueError,copyreg.add_extension,random,obj,100009)
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', 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', 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_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') 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') 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) 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.add_extension(self.input(0), self.input(1), self.input(2)))
'THINKING WITH TIME MACHINE': '286080', 'MEL': '317400', # Note - no workshop 'DEST_AP': '433970', 'DESTROYED_APERTURE': '433970', # Others: # 841: P2 Beta # 213630: Educational # 247120: Sixense # 211480: 'In Motion' } # Add core srctools types into the pickle registry, so they can be more directly # loaded. # IDs 240 - 255 are available for application uses. copyreg.add_extension('srctools.math', '_mk_vec', 240) copyreg.add_extension('srctools.math', '_mk_ang', 241) copyreg.add_extension('srctools.math', '_mk_mat', 242) copyreg.add_extension('srctools.math', 'Vec_tuple', 243) copyreg.add_extension('srctools.property_parser', 'Property', 244) # Appropriate locations to store config options for each OS. if WIN: _SETTINGS_ROOT = Path(os.environ['APPDATA']) elif MAC: _SETTINGS_ROOT = Path('~/Library/Preferences/').expanduser() elif LINUX: _SETTINGS_ROOT = Path('~/.config').expanduser() else: # Defer the error until used, so it goes in logs and whatnot. # Utils is early, so it'll get lost in stderr.