1import sys 2from PyObjCTest.fnd import NSNumber 3from PyObjCTools.TestSupport import * 4from objc._pythonify import OC_PythonLong, OC_PythonFloat 5if sys.version_info[0] == 2: 6 from objc._pythonify import OC_PythonInt 7import pickle 8 9try: 10 import cPickle 11except ImportError: 12 cPickle = None 13 14try: 15 long 16except NameError: 17 # Python 3.x 18 long = int 19 20class TestPickleNumber (TestCase): 21 22 def testPickleInt(self): 23 if sys.version_info[0] == 2: 24 number_type = OC_PythonInt 25 else: 26 number_type = OC_PythonLong 27 v = NSNumber.numberWithInt_(42) 28 self.assertIsInstance(v, number_type) 29 30 # First python pickle 31 s = pickle.dumps(v) 32 v2 = pickle.loads(s) 33 self.assertEqual(v2, v) 34 self.assertIsNotInstance(v2, number_type) 35 self.assertIsInstance(v2, int) 36 37 if cPickle is not None: 38 # Then C pickle 39 s = cPickle.dumps(v) 40 v2 = cPickle.loads(s) 41 self.assertEqual(v2, v) 42 self.assertIsNotInstance(v2, number_type) 43 self.assertIsInstance(v2, int) 44 45 def testPickleFloat(self): 46 v = NSNumber.numberWithFloat_(42) 47 self.assertIsInstance(v, OC_PythonFloat) 48 49 # First python pickle 50 s = pickle.dumps(v) 51 v2 = pickle.loads(s) 52 self.assertEqual(v2, v) 53 self.assertIsNotInstance(v2, OC_PythonFloat) 54 self.assertIsInstance(v2, float) 55 56 if cPickle is not None: 57 # Then C pickle 58 s = cPickle.dumps(v) 59 v2 = cPickle.loads(s) 60 self.assertEqual(v2, v) 61 self.assertIsNotInstance(v2, OC_PythonFloat) 62 self.assertIsInstance(v2, float) 63 64 @onlyOn32Bit 65 def testPickleLongLong(self): 66 v = NSNumber.numberWithLongLong_(sys.maxsize + 3) 67 self.assertIsInstance(v, OC_PythonLong) 68 69 # First python pickle 70 s = pickle.dumps(v) 71 v2 = pickle.loads(s) 72 self.assertEqual(v2, v) 73 self.assertIsNotInstance(v2, OC_PythonLong) 74 self.assertIsInstance(v2, long) 75 76 if cPickle is not None: 77 # Then C pickle 78 s = cPickle.dumps(v) 79 v2 = cPickle.loads(s) 80 self.assertEqual(v2, v) 81 self.assertIsNotInstance(v2, OC_PythonLong) 82 self.assertIsInstance(v2, long) 83 84 85 86 87if __name__ == "__main__": 88 main() 89