public
Description: Clone of SVN of PyObjC (not affiliated)
Homepage: http://pyobjc.sourceforge.net/
Clone URL: git://github.com/orestis/pyobjc.git
- copy.deepcopy support is not possible after all, implementation probably 
needs
pickle support first.

- add custom implementations of -copyWithZone: and -mutableCopyWithZone: 
for
  OC_PythonArray and OC_PythonDictionary (with tests)



git-svn-id: http://svn.red-bean.com/pyobjc/trunk@2001 
f55f28a5-9edb-0310-a011-a803cfcd5d25
ronaldoussoren (author)
Mon May 05 09:53:33 -0700 2008
commit  234037ef816ef24f676bc140ca3f6e70c97b98ef
tree    f6927121255d55d3ca6b240c7ee052b1f154e0cb
parent  fd3327ca5b2ea7429c59f7ca344372298d56d431
...
561
562
563
564
565
566
567
568
569
570
571
572
573
 
 
 
 
 
 
 
 
 
 
 
574
575
576
...
561
562
563
 
 
 
 
 
 
 
 
 
 
564
565
566
567
568
569
570
571
572
573
574
575
576
577
0
@@ -561,16 +561,17 @@ CONVENIENCE_METHODS['copyWithZone:'] = (
0
     ('__copy__', lambda self: self.copyWithZone_(None)),
0
 )
0
 
0
-NSKeyedArchiver = lookUpClass('NSKeyedArchiver')
0
-NSKeyedUnarchiver = lookUpClass('NSKeyedUnarchiver')
0
-def coder_deepcopy(self):
0
- buf = NSKeyedArchiver.archivedDataWithRootObject_(self)
0
- result = NSKeyedUnarchiver.unarchiveObjectWithData_(buf)
0
- return result
0
-
0
-CONVENIENCE_METHODS['encodeWithCoder:'] = (
0
- ('__deepcopy__', coder_deepcopy ),
0
-)
0
+# This won't work:
0
+#NSKeyedArchiver = lookUpClass('NSKeyedArchiver')
0
+#NSKeyedUnarchiver = lookUpClass('NSKeyedUnarchiver')
0
+#def coder_deepcopy(self, memo):
0
+# buf = NSKeyedArchiver.archivedDataWithRootObject_(self)
0
+# result = NSKeyedUnarchiver.unarchiveObjectWithData_(buf)
0
+# return result
0
+#
0
+#CONVENIENCE_METHODS['encodeWithCoder:'] = (
0
+# ('__deepcopy__', coder_deepcopy ),
0
+#)
0
 
0
 CLASS_METHODS['NSNull'] = (
0
     ('__nonzero__', lambda self: False ),
...
221
222
223
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
224
225
...
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
0
@@ -221,5 +221,22 @@ class TestNSCopying (objc.test.TestCase):
0
         self.assertEquals(o.x, 42)
0
         self.assertEquals(o.intVal(), 40)
0
 
0
+
0
+NSMutableArray = objc.lookUpClass("NSMutableArray")
0
+import copy
0
+
0
+class TestPyCopyObjC (objc.test.TestCase):
0
+ # Testcases that ensure that copy.copy works
0
+ # with Objective-C objects as well.
0
+
0
+ def testCopyArray(self):
0
+ a = NSMutableArray.arrayWithArray_(['a', 'b', 'c'])
0
+ self.assert_(isinstance(a, NSMutableArray))
0
+
0
+ b = copy.copy(a)
0
+ self.assert_(isinstance(b, NSMutableArray))
0
+ self.assert_(list(a) == list(b))
0
+
0
+
0
 if __name__ == "__main__":
0
     objc.test.main()
...
449
450
451
452
453
 
454
455
456
457
 
 
 
 
458
459
460
461
462
463
 
 
 
464
465
466
467
468
469
 
 
470
471
472
473
474
475
 
 
 
476
477
478
479
480
 
481
482
483
484
485
 
486
487
488
489
 
 
 
 
490
491
492
 
493
494
495
496
 
 
 
 
 
 
 
 
 
497
 
 
 
 
 
 
 
 
 
 
 
 
498
...
449
450
451
 
 
452
453
 
 
 
454
455
456
457
458
 
 
 
 
 
459
460
461
462
 
 
 
 
 
463
464
465
 
 
 
 
 
466
467
468
469
 
 
 
 
470
471
 
 
 
 
472
473
 
 
 
474
475
476
477
478
479
 
480
481
 
 
 
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
0
@@ -449,50 +449,56 @@ static PyObject* mapTypes = NULL;
0
 }
0
 
0
 
0
-#if 1
0
--(NSObject*)replacementObjectForArchiver:(NSArchiver*)archiver
0
+-(id)copyWithZone:(NSZone*)zone
0
 {
0
- (void)(archiver);
0
- return self;
0
-}
0
+ if (PyObjC_CopyFunc) {
0
+ PyObjC_BEGIN_WITH_GIL
0
+ PyObject* copy = PyObject_CallFunctionObjArgs(PyObjC_CopyFunc,
0
+ value, NULL);
0
 
0
--(NSObject*)replacementObjectForKeyedArchiver:(NSKeyedArchiver*)archiver
0
-{
0
- (void)(archiver);
0
- return self;
0
-}
0
+ if (copy == NULL) {
0
+ PyObjC_GIL_FORWARD_EXC();
0
+ }
0
 
0
--(NSObject*)replacementObjectForCoder:(NSKeyedArchiver*)archiver
0
-{
0
- (void)(archiver);
0
- return self;
0
-}
0
+ NSObject* result = PyObjC_PythonToId(copy);
0
+ Py_DECREF(copy);
0
 
0
--(NSObject*)replacementObjectForPortCoder:(NSKeyedArchiver*)archiver
0
-{
0
- (void)(archiver);
0
- return self;
0
-}
0
+ if (PyErr_Occurred()) {
0
+ PyObjC_GIL_FORWARD_EXC();
0
+ }
0
 
0
--(Class)classForArchiver
0
-{
0
- return [OC_PythonArray class];
0
-}
0
+ [result retain];
0
 
0
--(Class)classForKeyedArchiver
0
-{
0
- return [OC_PythonArray class];
0
-}
0
+ PyObjC_GIL_RETURN(result);
0
 
0
--(Class)classForCoder
0
-{
0
- return [OC_PythonArray class];
0
+ PyObjC_END_WITH_GIL
0
+ } else {
0
+ return [super copyWithZone:zone];
0
+ }
0
 }
0
 
0
--(Class)classForPortCoder
0
+-(id)mutableCopyWithZone:(NSZone*)zone
0
 {
0
- return [OC_PythonArray class];
0
-}
0
-#endif
0
+ if (PyObjC_CopyFunc) {
0
+ PyObjC_BEGIN_WITH_GIL
0
+ PyObject* copy = PySequence_List(value);
0
+ if (copy == NULL) {
0
+ PyObjC_GIL_FORWARD_EXC();
0
+ }
0
+
0
+ NSObject* result = PyObjC_PythonToId(copy);
0
+ Py_DECREF(copy);
0
 
0
+ if (PyErr_Occurred()) {
0
+ PyObjC_GIL_FORWARD_EXC();
0
+ }
0
+
0
+ [result retain];
0
+ PyObjC_GIL_RETURN(result);
0
+
0
+ PyObjC_END_WITH_GIL
0
+ } else {
0
+ return [super mutableCopyWithZone:zone];
0
+ }
0
+}
0
 @end /* implementation OC_PythonArray */
...
523
524
525
 
 
 
 
 
 
 
 
 
526
527
 
 
528
529
530
531
532
533
 
 
 
534
535
536
537
538
539
 
540
 
541
542
543
544
 
 
 
 
545
546
547
 
548
549
550
 
 
 
 
 
 
551
552
553
554
555
 
 
 
 
556
557
558
559
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
560
561
562
563
564
...
523
524
525
526
527
528
529
530
531
532
533
534
535
 
536
537
538
 
 
 
 
 
539
540
541
542
 
 
 
 
 
543
544
545
546
 
 
 
547
548
549
550
551
552
 
553
554
 
 
555
556
557
558
559
560
561
 
 
 
 
562
563
564
565
566
 
 
 
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
 
584
585
0
@@ -523,42 +523,63 @@ static PyObject* mapTypes = NULL;
0
   }
0
 }
0
 
0
+-(id)copyWithZone:(NSZone*)zone
0
+{
0
+ if (PyObjC_CopyFunc) {
0
+ PyObjC_BEGIN_WITH_GIL
0
+ PyObject* copy = PyObject_CallFunctionObjArgs(PyObjC_CopyFunc,
0
+ value, NULL);
0
+ if (copy == NULL) {
0
+ PyObjC_GIL_FORWARD_EXC();
0
+ }
0
 
0
-#if 1
0
+ NSObject* result = PyObjC_PythonToId(copy);
0
+ Py_DECREF(copy);
0
 
0
--(NSObject*)replacementObjectForArchiver:(NSArchiver*)archiver
0
-{
0
- (void)archiver;
0
- return self;
0
-}
0
+ if (PyErr_Occurred()) {
0
+ PyObjC_GIL_FORWARD_EXC();
0
+ }
0
 
0
--(NSObject*)replacementObjectForKeyedArchiver:(NSKeyedArchiver*)archiver
0
-{
0
- (void)archiver;
0
- return self;
0
-}
0
+ [result retain];
0
 
0
+ PyObjC_GIL_RETURN(result);
0
 
0
--(Class)classForArchiver
0
-{
0
- return [OC_PythonDictionary class];
0
+ PyObjC_END_WITH_GIL
0
+ } else {
0
+ return [super copyWithZone:zone];
0
+ }
0
 }
0
 
0
--(Class)classForKeyedArchiver
0
+-(id)mutableCopyWithZone:(NSZone*)zone
0
 {
0
- return [OC_PythonDictionary class];
0
-}
0
+ if (PyObjC_CopyFunc) {
0
+ PyObjC_BEGIN_WITH_GIL
0
+ PyObject* copy = PyDict_New();
0
+ if (copy == NULL) {
0
+ PyObjC_GIL_FORWARD_EXC();
0
+ }
0
 
0
--(Class)classForCoder
0
-{
0
- return [OC_PythonDictionary class];
0
-}
0
+ int r = PyDict_Update(copy, value);
0
+ if (r == -1) {
0
+ PyObjC_GIL_FORWARD_EXC();
0
+ }
0
 
0
--(Class)classForPortCoder
0
-{
0
- return [OC_PythonDictionary class];
0
+ NSObject* result = PyObjC_PythonToId(copy);
0
+ Py_DECREF(copy);
0
+
0
+ if (PyErr_Occurred()) {
0
+ PyObjC_GIL_FORWARD_EXC();
0
+ }
0
+
0
+ [result retain];
0
+
0
+ PyObjC_GIL_RETURN(result);
0
+
0
+ PyObjC_END_WITH_GIL
0
+ } else {
0
+ return [super mutableCopyWithZone:zone];
0
+ }
0
 }
0
 
0
-#endif
0
 
0
 @end // interface OC_PythonDictionary
...
399
400
401
402
 
403
404
405
...
399
400
401
 
402
403
404
405
0
@@ -399,7 +399,7 @@ end:
0
 
0
   } else {
0
     PyObjC_BEGIN_WITH_GIL
0
- copy = PyObject_CallFunction(PyObjC_CopyFunc, "O", pyObject);
0
+ copy = PyObject_CallFunctionObjArgs(PyObjC_CopyFunc, pyObject, NULL);
0
       if (copy == NULL) {
0
         PyObjC_GIL_FORWARD_EXC();
0
       }
...
35
36
37
38
39
 
 
 
 
 
 
 
 
40
41
42
...
35
36
37
 
 
38
39
40
41
42
43
44
45
46
47
48
0
@@ -35,8 +35,14 @@ Version 2.1 (...)
0
 - Objective-C classes that support the ``NSCopying`` protocol can now be
0
   copied using ``copy.copy`` as well.
0
 
0
-- Objective-C classes that support the ``NSCoding`` protocol can now be
0
- copied using ``copy.deepcopy``.
0
+..
0
+ it would be nice to have the following, but that's not easy to achieve::
0
+ - Objective-C classes that support the ``NSCoding`` protocol can now be
0
+ copied using ``copy.deepcopy``.
0
+
0
+- ``OC_PythonArray`` and ``OC_PythonDictionary`` now explicitly implement
0
+ ``copyWithZone:`` and ``mutableCopyWithZone:``, copies will now be
0
+ Python objects instead of regular ``NSDictionary`` instances.
0
 
0
 - Pure Python objects now support the ``NSCopying`` protocol.
0
 

Comments

    No one has commented yet.