def test_reduce_invalid(self): class invalid_reduce (object): def __reduce__(self): return 42 data = NSMutableData.alloc().init() archiver = self.archiverClass.alloc().initForWritingWithMutableData_(data) self.assertRaises(pickle.PicklingError, archiver.encodeRootObject_, invalid_reduce()) if self.archiverClass is NSKeyedArchiver: archiver.finishEncoding() class invalid_reduce (object): def __reduce__(self): return (1,) data = NSMutableData.alloc().init() archiver = self.archiverClass.alloc().initForWritingWithMutableData_(data) self.assertRaises(pickle.PicklingError, archiver.encodeRootObject_, invalid_reduce()) if self.archiverClass is NSKeyedArchiver: archiver.finishEncoding() class invalid_reduce (object): def __reduce__(self): return (1,2,3,4,5,6) data = NSMutableData.alloc().init() archiver = self.archiverClass.alloc().initForWritingWithMutableData_(data) self.assertRaises(pickle.PicklingError, archiver.encodeRootObject_, invalid_reduce()) if self.archiverClass is NSKeyedArchiver: archiver.finishEncoding()
def test_reducing_issues(self): class Error1(object): def __reduce__(self): return dir, 'foo' object1 = Error1() data = NSMutableData.alloc().init() archiver = self.archiverClass.alloc().initForWritingWithMutableData_( data) self.assertRaises(pickle.PicklingError, archiver.encodeRootObject_, object1) if self.archiverClass is NSKeyedArchiver: archiver.finishEncoding() class Error2(object): def __reduce__(self): return 'foo', (1, 2) object2 = Error2() data = NSMutableData.alloc().init() archiver = self.archiverClass.alloc().initForWritingWithMutableData_( data) self.assertRaises(pickle.PicklingError, archiver.encodeRootObject_, object2) if self.archiverClass is NSKeyedArchiver: archiver.finishEncoding()
def test_reducing_issues(self): class Error1 (object): def __reduce__(self): return dir, 'foo' object1 = Error1() data = NSMutableData.alloc().init() archiver = self.archiverClass.alloc().initForWritingWithMutableData_(data) self.assertRaises(pickle.PicklingError, archiver.encodeRootObject_, object1) if self.archiverClass is NSKeyedArchiver: archiver.finishEncoding() class Error2 (object): def __reduce__(self): return 'foo', (1, 2) object2 = Error2() data = NSMutableData.alloc().init() archiver = self.archiverClass.alloc().initForWritingWithMutableData_(data) self.assertRaises(pickle.PicklingError, archiver.encodeRootObject_, object2) if self.archiverClass is NSKeyedArchiver: archiver.finishEncoding()
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') 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