1. Ronald Oussoren
  2. pyobjc

Source

pyobjc / Modules / objc / objc-object.m

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 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
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 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
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
/*
 * Implementation of objective-C object wrapper
 */
#include "pyobjc.h"

#include <stddef.h>

#include <objc/Object.h>



/*
 * Basic freelist. 
 * - to delete an object: obj_freelist[obj_freelist_top++] = OBJ
 * - to create an object: OBJ = obj_freelist[--obj_freelist_top];
 */
#if 0
#define FREELIST_SIZE 1024

static PyObject* obj_freelist[FREELIST_SIZE];
static int obj_freelist_top = 0;
#endif


static NSMapTable* proxy_dict = NULL;

static PyObject* 
find_existing_proxy(id objc_obj)
{
	PyObject* v;

	if (proxy_dict == NULL) return NULL;

	v = NSMapGet(proxy_dict, objc_obj);
	Py_XINCREF(v);
	return v;
}

static void 
unregister_proxy(id objc_obj)
{
	if (proxy_dict == NULL) return;
	if (objc_obj == nil) return;

	NSMapRemove(proxy_dict, objc_obj);
}

static int
register_proxy(PyObject* proxy_obj) 
{
	id objc_obj;

	if (PyObjCObject_Check(proxy_obj)) {
		objc_obj = PyObjCObject_GetObject(proxy_obj);
	} else if (PyObjCClass_Check(proxy_obj)) {
		objc_obj = PyObjCClass_GetClass(proxy_obj);
	} else if (PyObjCUnicode_Check(proxy_obj)) {
		objc_obj = PyObjCUnicode_Extract(proxy_obj);
	} else {
		PyErr_SetString(PyExc_TypeError, 
			"bad argument for register_proxy");
		return -1;
	}
	assert(objc_obj != nil);

	if (proxy_dict == NULL)  {
		proxy_dict =  NSCreateMapTable(
			PyObjCUtil_PointerKeyCallBacks,
			PyObjCUtil_PointerValueCallBacks, 
			500);

		if (proxy_dict == NULL) {
			PyErr_SetString(PyExc_RuntimeError,
					"Cannot create NSMapTable");
			return -1;
		}
	}


	NSMapInsert(proxy_dict, objc_obj, proxy_obj);

	return 0;
}



static PyObject*
object_new(
	PyTypeObject*  type __attribute__((__unused__)),
	PyObject* args __attribute__((__unused__)), 
	PyObject* kwds __attribute__((__unused__)))
{
	PyErr_SetString(PyExc_TypeError, 
		"Use class methods to instantiate new Objective-C objects");
	return NULL;
}

static PyObject*
object_repr(PyObjCObject* self)
{
	char buffer[256];
	PyObject* res;

	if ((self->flags & PyObjCObject_kUNINITIALIZED) == 0 && !PyObjCObject_IsClassic(self)) {
		/* Try to call the method 'description', which is the ObjC
		 * equivalent of __repr__. If that fails we'll fall back to
		 * the default repr.
		 * Don't call 'description' for uninitialized objects, that
		 * crashes the interpreter for several classes.
		 */
		res = PyObject_CallMethod((PyObject*)self, "description", NULL);
		if (res == NULL) {
			PyErr_Clear();
		} else {
			return res;
		}
	}
	snprintf(buffer, sizeof(buffer), "<%s objective-c instance %p>",
		self->ob_type->tp_name, self->objc_object);

	return PyString_FromString(buffer);
}

#if PY_VERSION_HEX >= 0x020300A2
static void
object_del(PyObject* obj __attribute__((__unused__)))
{
	/* Dummy function, we do not want the default implementation */
}
#endif


static void
object_dealloc(PyObject* obj)
{
	/* XXX: This should not be necessary, but if we don't do this we
	 * sometimes loose exception information...
	 */
	PyObject* ptype, *pvalue, *ptraceback;
	PyErr_Fetch(&ptype, &pvalue, &ptraceback);

	if (PyObjCObject_GetFlags(obj) != PyObjCObject_kDEALLOC_HELPER 
			&& PyObjCObject_GetObject(obj) != nil) {
		/* Release the proxied object, we don't have to do this when
		 * there is no proxied object!
		 */
		unregister_proxy(PyObjCObject_GetObject(obj));


		/* If the object is not yet initialized we try to initialize 
		 * it before releasing the reference. This is necessary 
		 * because of a misfeature of MacOS X: 
		 * [[NSTextView alloc] release] crashes (at least upto 10.2)
		 * and this is not a bug according to Apple.
		 */
		if (PyObjCObject_IsClassic(obj)) {
			/* pass */

		} else if (((PyObjCObject*)obj)->flags 
				& PyObjCObject_kUNINITIALIZED) {
			/* Freeing of an unitialized object, just leak because 
			 * there is no reliable manner to free such objects.
			 *
			 * - [obj release] doesn't work because some classes 
			 *   cause crashes for unitialized objects
			 * - [[obj init] release] also doesn't work because 
			 *   not all classes implement -init
			 * - [obj dealloc] also doesn't work for class 
			 *   clusters like NSArray.
			 */
			char buf[256];

			snprintf(buf, sizeof(buf), 
				"leaking an unitialized object of type %s",
				obj->ob_type->tp_name);
			PyErr_Warn(PyObjCExc_UnInitDeallocWarning, buf);
			((PyObjCObject*)obj)->objc_object = nil;
		} else {
			PyObjC_DURING
				[((PyObjCObject*)obj)->objc_object release];

			PyObjC_HANDLER
				NSLog(@"PyObjC: Exception during dealloc of proxy: %@",
					localException);

			PyObjC_ENDHANDLER
			((PyObjCObject*)obj)->objc_object = nil;
		}
	}

#ifdef FREELIST_SIZE
	/* Push self onto the freelist */
	if (obj_freelist_top == FREELIST_SIZE) {
		obj->ob_type->tp_free(obj);
	} else {
		obj_freelist[obj_freelist_top++] = obj;
		obj->ob_refcnt = 0xDEADBEEF;
	}
#else
	obj->ob_type->tp_free(obj);
#endif

	PyErr_Restore(ptype, pvalue, ptraceback);
}


static inline PyObject*
_type_lookup(PyTypeObject* tp, PyObject* name)
{
	int i, n;
	PyObject *mro, *base, *dict;
	PyObject *descr = NULL;

	/* Look in tp_dict of types in MRO */
	mro = tp->tp_mro;
	assert(mro != NULL);
	assert(PyTuple_Check(mro));
	n = PyTuple_GET_SIZE(mro);
	for (i = 0; i < n; i++) {
		base = PyTuple_GET_ITEM(mro, i);

		if (PyClass_Check(base)) {
			dict = ((PyClassObject*)base)->cl_dict;
		} else {
			assert(PyType_Check(base));
			if (PyObjCClass_Check(base)) {
				PyObjCClass_CheckMethodList(base, 0);
			}

			dict = ((PyTypeObject *)base)->tp_dict;
		}
		assert(dict && PyDict_Check(dict));
		descr = PyDict_GetItem(dict, name);
		if (descr != NULL) {
			break;
		}
	}

	return descr;
}

static PyObject** _get_dictptr(PyObject* obj)
{
	int dictoffset;
	id obj_object;
	dictoffset = PyObjCClass_DictOffset((PyObject*)obj->ob_type);
	if (dictoffset == 0) return NULL;
	obj_object = PyObjCObject_GetObject(obj);
	assert(obj_object != nil);
	return (PyObject**)(((char*)obj_object) + dictoffset);
}


static PyObject *
object_getattro(PyObject *obj, PyObject * volatile name)
{
	PyTypeObject *tp;
	PyObject *descr = NULL;
	PyObject *res = NULL;
	descrgetfunc f;
	PyObject **dictptr;
	char*      namestr;
	Class obj_class;
	id obj_inst;

	if (!PyString_Check(name)){
#ifdef Py_USING_UNICODE
		/* The Unicode to string conversion is done here because the
		   existing tp_setattro slots expect a string object as name
		   and we wouldn't want to break those. */
		if (PyUnicode_Check(name)) {
			name = PyUnicode_AsEncodedString(name, NULL, NULL);
			if (name == NULL)
				return NULL;
		}
		else
#endif
		{
			PyErr_Format(PyExc_TypeError,
				"attribute name must be string, got %s",
				name->ob_type->tp_name);
			return NULL;
		}
	}
	else
		Py_INCREF(name);


	namestr = PyString_AS_STRING(name);

	/* Special hack for KVO on MacOS X, when an object is observed it's 
	 * ISA is changed by the runtime. We change the python type as well.
	 */
	obj_inst = PyObjCObject_GetObject(obj);
	if (obj_inst == nil) {
		PyErr_Format(PyExc_AttributeError,
		     "cannot access attribute '%.400s' of NIL '%.50s' object",
		     PyString_AS_STRING(name),
		     obj->ob_type->tp_name);
		goto done;
	}


	obj_class = GETISA(obj_inst);
	tp = (PyTypeObject*)PyObjCClass_New(obj_class);

	descr = NULL;
	if (tp != obj->ob_type) {
		/* Workaround for KVO implementation feature */
		PyObject* dict;

		if (tp->tp_dict == NULL) {
			if (PyType_Ready(tp) < 0)
				goto done;
		}

		PyObjCClass_CheckMethodList((PyObject*)tp, 0);
		dict = tp->tp_dict;

		assert(dict && PyDict_Check(dict));
		descr = PyDict_GetItem(dict, name);
	}
	Py_DECREF(tp);

	tp = obj->ob_type;
	if (tp->tp_dict == NULL) {
		if (PyType_Ready(tp) < 0)
			goto done;
	}

	/* replace _PyType_Lookup */
	if (descr == NULL) {
		descr = _type_lookup(tp, name);
	}

	f = NULL;
	if (descr != NULL &&
	    PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {
		f = descr->ob_type->tp_descr_get;
		if (f != NULL && PyDescr_IsData(descr)) {
			res = f(descr, obj, (PyObject*)obj->ob_type);
			goto done;
		}
	}

	if (strcmp(PyString_AS_STRING(name), "__del__") == 0) {
		res = PyObjCClass_GetDelMethod((PyObject*)obj->ob_type);
		if (res != NULL) {
			/* TODO: bind self */	
		}
		goto done;
	}

	/* First try the __dict__ */
	dictptr = _get_dictptr(obj);

	if (dictptr != NULL) {
		PyObject *dict;

		if (strcmp(PyString_AS_STRING(name), "__dict__") == 0) {
			res = *dictptr;
			if (res == NULL) {
				*dictptr = PyDict_New();
				if (*dictptr == NULL) {
					PyErr_Clear();
				}
				res = *dictptr;
			}
			if (res != NULL) {
				Py_INCREF(res);
				goto done;
			}
		} else {
			dict = *dictptr;
			if (dict != NULL) {
				res = PyDict_GetItem(dict, name);
				if (res != NULL) {
					Py_INCREF(res);
					goto done;
				}
			}
		}
	}

	if (f != NULL) {
		res = f(descr, obj, (PyObject*)obj->ob_type);
		goto done;
	}

	if (descr != NULL) {
		Py_INCREF(descr);
		res = descr;
		goto done;
	}

	if (!PyObjCObject_IsClassic(obj)) {
		res = PyObjCSelector_FindNative(obj, namestr);
		if (res) goto done;
	}

	PyErr_Format(PyExc_AttributeError,
	     "'%.50s' object has no attribute '%.400s'",
	     tp->tp_name, namestr);

  done:
	if (res != NULL) {
		/* class methods cannot be accessed through instances */
		if (PyObjCSelector_Check(res) 
				&& PyObjCSelector_IsClassMethod(res)) {
			Py_DECREF(res);
			PyErr_Format(PyExc_AttributeError,
			     "'%.50s' object has no attribute '%.400s'",
			     tp->tp_name, PyString_AS_STRING(name));
			res = NULL;
		}
	}

	Py_DECREF(name);
	return res;
}


static int
object_setattro(PyObject *obj, PyObject *name, PyObject *value)
{
	PyTypeObject *tp = obj->ob_type;
	PyObject *descr;
	descrsetfunc f;
	PyObject** dictptr;
	int res = -1;
	


	if (!PyString_Check(name)){
#ifdef Py_USING_UNICODE
		/* The Unicode to string conversion is done here because the
		   existing tp_setattro slots expect a string object as name
		   and we wouldn't want to break those. */
		if (PyUnicode_Check(name)) {
			name = PyUnicode_AsEncodedString(name, NULL, NULL);
			if (name == NULL)
				return -1;
		}
		else
#endif
		{
			PyErr_Format(PyExc_TypeError,
				"attribute name must be string, got %s",
				name->ob_type->tp_name);
			return -1;
		}
	}
	else
		Py_INCREF(name);

	if (PyObjCObject_GetObject(obj) == nil) {
		PyErr_Format(PyExc_AttributeError,
		     "Cannot set '%s.400s' on NIL '%.50s' object",
		     PyString_AS_STRING(name),
		     tp->tp_name);
		goto done;
	}

	if (tp->tp_dict == NULL) {
		if (PyType_Ready(tp) < 0)
			goto done;
	}

	descr = _type_lookup(tp, name);
	f = NULL;
	if (descr != NULL &&
	    PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) {
		f = descr->ob_type->tp_descr_set;
		if (f != NULL && PyDescr_IsData(descr)) {
			res = f(descr, obj, value);
			goto done;
		}
	}

	dictptr = _get_dictptr(obj);
	if (dictptr != NULL) {
		PyObject *dict;

		dict = *dictptr;
		
		if (dict == NULL && value != NULL) {
			dict = PyDict_New();
			if (dict == NULL)
				goto done;
			
			*dictptr = dict;
		}
		if (dict != NULL) {
			if (value == NULL)
				res = PyDict_DelItem(dict, name);
			else
				res = PyDict_SetItem(dict, name, value);
			if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError))
				PyErr_SetObject(PyExc_AttributeError, name);
			goto done;
		}
	}

	if (f != NULL) {
		res = f(descr, obj, value);
		goto done;
	}

	if (descr == NULL) {
		PyErr_Format(PyExc_AttributeError,
			     "'%.50s' object has no attribute '%.400s'",
			     tp->tp_name, PyString_AS_STRING(name));
		goto done;
	}

	PyErr_Format(PyExc_AttributeError,
		     "'%.50s' object attribute '%.400s' is read-only",
		     tp->tp_name, PyString_AS_STRING(name));
  done:

#if 0
	/* XXX: This would introduce some form of support for KeyValueObserving
	 * but, I'm not sure if this is the right approach. If this is, this 
	 * code needs more work: the name must be transformed to 
	 * a KeyValueCoding key (e.g. '_name' -> 'name')
	 */

#if defined(MACOSX) && MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_3
	/* KeyValueObserving support. If an attribute is changed by assignment,
	 * send out the notification.
	 */
	if (!PyErr_Occurred()) {
		id key = PyObjC_PythonToId(name);
		if (key == NULL && PyErr_Occurred()) {
			/* Cannot convert the name, ignore this */
			PyErr_Clear();
		} else {
			[PyObjCObject_GetObject(obj) didChangeValueForKey:key];
		}
	}
#endif

#endif

	Py_DECREF(name);
	return res;
}

#if 0 /* No longer used */
PyDoc_STRVAR(obj_get_classMethods_doc,
"The attributes of this field are the class methods of this object. This can\n"
"be used to force access to a class method."
);
static PyObject*
obj_get_classMethods(PyObjCObject* self, void* closure __attribute__((__unused__)))
{
	return ObjCMethodAccessor_New((PyObject*)self, 1);
}
#endif

PyDoc_STRVAR(objc_get_real_class_doc, "Return the current ISA of the object");
static PyObject* objc_get_real_class(PyObject* self, void* closure __attribute__((__unused__)))
{
	id obj_object;
	PyObject* ret;
	obj_object = PyObjCObject_GetObject(self);
	assert(obj_object != nil);
	ret = PyObjCClass_New(GETISA(obj_object));
	if (ret != (PyObject*)self->ob_type) {
		/* XXX doesn't this leak a reference to the original ob_type? */
		self->ob_type = (PyTypeObject*)ret;
		Py_INCREF(ret);
	}
	return ret;
}

PyDoc_STRVAR(obj_get_instanceMethods_doc,
"The attributes of this field are the instance methods of this object. This\n"
"can be used to force access to a class method."
);
static PyObject*
obj_get_instanceMethods(PyObjCObject* self, void* closure __attribute__((__unused__)))
{
	return ObjCMethodAccessor_New((PyObject*)self, 0);
}

static PyGetSetDef obj_getset[] = {
#if 0
	{
		"pyobjc_classMethods",
		(getter)obj_get_classMethods,
		NULL,
		obj_get_classMethods_doc,
		0
	},
#endif
	{
		"pyobjc_ISA",
		(getter)objc_get_real_class,
		NULL,
		objc_get_real_class_doc,
		0
	},
	{
		"pyobjc_instanceMethods",
		(getter)obj_get_instanceMethods,
		NULL,
		obj_get_instanceMethods_doc,
		0
	},
	{ 0, 0, 0, 0, 0 }
};

/*
 * We don't support pickling of Objective-C objects at the moment. The new
 * version 2 of the pickle protocol has a default pickle method for new-style
 * classes that doesn't work for us (it will write incomplete values to the
 * pickle). This method forces a failure during pickling.
 */
static PyObject*
meth_reduce(PyObject* self __attribute__((__unused__)))
{
	PyErr_SetString(PyExc_TypeError,
		"Cannot pickle Objective-C objects");
	return NULL;
}

static PyMethodDef obj_methods[] = {
	{
		"__reduce__",
		(PyCFunction)meth_reduce,
		METH_NOARGS,
		"Used for pickling"
	},
	{
		NULL,
		NULL,
		0,
		NULL
	}
};


PyObjCClassObject PyObjCObject_Type = {
#ifdef PyObjC_CLASS_INFO_IN_TYPE
   {
     {
#else
   {
#endif
	PyObject_HEAD_INIT(&PyObjCClass_Type)
	0,					/* ob_size */
	"objc_object",				/* tp_name */
	sizeof(PyObjCObject),			/* tp_basicsize */
	0,					/* tp_itemsize */
	/* methods */
	object_dealloc,		 		/* tp_dealloc */
	0,					/* tp_print */
	0,					/* tp_getattr */
	0,					/* tp_setattr */
	0,					/* tp_compare */
	(reprfunc)object_repr,			/* tp_repr */
	0,					/* tp_as_number */
	0,					/* tp_as_sequence */
	0,		       			/* tp_as_mapping */
	0,					/* tp_hash */
	0,					/* tp_call */
	0,					/* tp_str */
	object_getattro,			/* tp_getattro */
	object_setattro,			/* tp_setattro */
	0,					/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT 
		| Py_TPFLAGS_BASETYPE,          /* tp_flags */
 	0,					/* tp_doc */
 	0,					/* tp_traverse */
 	0,					/* tp_clear */
	0,					/* tp_richcompare */
	0,					/* tp_weaklistoffset */
	0,					/* tp_iter */
	0,					/* tp_iternext */
	obj_methods,				/* tp_methods */
	0,					/* tp_members */
	obj_getset,				/* tp_getset */
	0,					/* tp_base */
	0,					/* tp_dict */
	0,					/* tp_descr_get */
	0,					/* tp_descr_set */
	0,					/* tp_dictoffset */
	0,					/* tp_init */
	PyType_GenericAlloc,			/* tp_alloc */
	object_new,				/* tp_new */
	0,		        		/* tp_free */
	0,					/* tp_is_gc */
	0,					/* tp_bases */
	0,					/* tp_mro */
	0,					/* tp_cache */
	0, 					/* tp_subclasses */
	0					/* tp_weaklist */
#ifdef PyObjC_CLASS_INFO_IN_TYPE
	, (destructor)object_del,		/* tp_del */
     }
     ,
     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
       0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* as_number */
     { 0, 0, 0 },			/* as_mapping */
     { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },	/* as_sequence */
     { 0, 0, 0, 0 },			/* as_buffer */
     0,					/* name */
     0,					/* slots */
   }
   , 0, 0, 0, 0, 0, 0, 0
#else
   }
#endif
};

/*
 *  Allocate a proxy object for use during the call of __del__,
 *  this isn't a full-featured proxy object.
 */
PyObject* 
_PyObjCObject_NewDeallocHelper(id objc_object)
{
	Class cls; 
	PyObject* res;
	PyTypeObject* cls_type;

	assert(objc_object != nil);
	cls = GETISA(objc_object);
	cls_type = (PyTypeObject*)PyObjCClass_New(cls);
	if (cls_type == NULL) {
		return NULL;
	}

#ifdef FREELIST_SIZE
	if (obj_freelist_top == 0) {
		res = cls_type->tp_alloc(cls_type, 0);
		if (res == NULL) {
			return NULL;
		}
	} else {
		res = obj_freelist[obj_freelist_top-1];
		obj_freelist_top -= 1;
		if (res->ob_refcnt != 0xDEADBEEF) abort();
		res->ob_refcnt = 1;
		res->ob_type = cls_type;
	}
#else
	res = cls_type->tp_alloc(cls_type, 0);
	if (res == NULL) {
		return NULL;
	}
#endif

	PyObjCClass_CheckMethodList((PyObject*)res->ob_type, 1);
	
	assert(objc_object != nil);
	((PyObjCObject*)res)->objc_object = objc_object;
	((PyObjCObject*)res)->flags = PyObjCObject_kDEALLOC_HELPER;
	return res;
}

/*
 * Free the object allocated using '_PyObCObject_NewDeallocHelper'. If the
 * object has a refcnt > 1 when calling this function, the object is 
 * promoted to a full proxy object. This should only happen when someone
 * revives the object, it is unclear whether the ObjC runtime will accept
 * reviveing.
 */
void
_PyObjCObject_FreeDeallocHelper(PyObject* obj)
{
	if (obj->ob_refcnt != 1) {
		/* Someone revived this object, hopefully 
		 * Objective-C can deal with this.
		 */
		id objc_object = PyObjCObject_GetObject(obj);

		Py_DECREF(obj);

		if (strcmp(GETISA(objc_object)->name, 
						"NSAutoreleasePool") != 0) {
			/* NSAutoreleasePool doesn't like retain */
			[objc_object retain];
		}

		if (register_proxy(obj) < 0) {
			NSLog(@"Couldn't register revived proxy object!");
		}
		return;
	}
	Py_DECREF(obj);
}


PyObject* 
PyObjCObject_New(id objc_object)
{
	Class cls = [objc_object class];
	PyTypeObject* cls_type;
	PyObject*     res;

	res = find_existing_proxy(objc_object);
	if (res) return res;

	if (objc_object == NULL) {
		Py_INCREF(Py_None);
		return Py_None;
	}

	cls_type = (PyTypeObject*)PyObjCClass_New(cls);
	if (cls_type == NULL) {
		return NULL;
	}

#ifdef FREELIST_SIZE
	if (obj_freelist_top == 0) {
		res = cls_type->tp_alloc(cls_type, 0);
		if (res == NULL) {
			return NULL;
		}
	} else {
		res = obj_freelist[obj_freelist_top-1];
		obj_freelist_top -= 1;
		if (res->ob_refcnt != 0xDEADBEEF) abort();
		res->ob_refcnt = 1;
		res->ob_type = cls_type;
	}
#else
	res = cls_type->tp_alloc(cls_type, 0);
	if (res == NULL) {
		return NULL;
	}
#endif

	/* This should be in the tp_alloc for the new class, but 
	 * adding a tp_alloc to PyObjCClass_Type doesn't seem to help
	 */
	PyObjCClass_CheckMethodList((PyObject*)res->ob_type, 1);
	
	assert(objc_object != nil);
	((PyObjCObject*)res)->objc_object = objc_object;
	((PyObjCObject*)res)->flags = 0;



	if (strcmp(GETISA(objc_object)->name, "NSAutoreleasePool") != 0) {
		/* NSAutoreleasePool doesn't like retain */
		/* XXX: Technicly we shouldn't call retain either if this
		 * is an uninitialized object.
		 */
		[objc_object retain];
	}

	if (register_proxy(res) < 0) {
		Py_DECREF(res);
		return NULL;
	}

	return res;
}

PyObject* 
PyObjCObject_NewClassic(id objc_object)
{
	Class cls = [objc_object class];
	PyTypeObject* cls_type;
	PyObject*     res;

	res = find_existing_proxy(objc_object);
	if (res) return res;

	if (objc_object == NULL) {
		Py_INCREF(Py_None);
		return Py_None;
	}

	cls_type = (PyTypeObject*)PyObjCClass_New(cls);
	if (cls_type == NULL) {
		return NULL;
	}

#ifdef FREELIST_SIZE
	if (obj_freelist_top == 0) {
		res = cls_type->tp_alloc(cls_type, 0);
		if (res == NULL) {
			return NULL;
		}
	} else {
		res = obj_freelist[obj_freelist_top-1];
		obj_freelist_top -= 1;
		if (res->ob_refcnt != 0xDEADBEEF) abort();
		res->ob_refcnt = 1;
		res->ob_type = cls_type;
	}
#else
	res = cls_type->tp_alloc(cls_type, 0);
	if (res == NULL) {
		return NULL;
	}
#endif

	/* This should be in the tp_alloc for the new class, but 
	 * adding a tp_alloc to PyObjCClass_Type doesn't seem to help
	 */
	PyObjCClass_CheckMethodList((PyObject*)res->ob_type, 1);
	
	assert(objc_object != nil);
	((PyObjCObject*)res)->objc_object = objc_object;
	((PyObjCObject*)res)->flags = PyObjCObject_kCLASSIC;

	if (register_proxy(res) < 0) {
		Py_DECREF(res);
		return NULL;
	}

	return res;
}

PyObject* 
PyObjCObject_NewUnitialized(id objc_object)
{
	Class cls = GETISA(objc_object);
	PyTypeObject* cls_type;
	PyObject*     res;


	res = find_existing_proxy(objc_object);
	if (res) return res;

	if (objc_object == NULL) {
		Py_INCREF(Py_None);
		return Py_None;
	}

	cls_type = (PyTypeObject*)PyObjCClass_New(cls);
	if (cls_type == NULL) {
		return NULL;
	}

	res = cls_type->tp_alloc(cls_type, 0);
	if (res == NULL) {
		return NULL;
	}

	/* This should be in the tp_alloc for the new class, but 
	 * adding a tp_alloc to PyObjCClass_Type doesn't seem to help
	 */
	PyObjCClass_CheckMethodList((PyObject*)res->ob_type, 1);
	
	assert(objc_object != nil);
	((PyObjCObject*)res)->objc_object = objc_object;
	((PyObjCObject*)res)->flags = 0;

	if (register_proxy(res) < 0) {
		Py_DECREF(res);
		return NULL;
	}

	return res;
}

PyObject* 
PyObjCObject_FindSelector(PyObject* object, SEL selector)
{
	PyObject* meth;
	
	meth = PyObjCClass_FindSelector((PyObject*)object->ob_type, selector);

	if (meth == NULL) {
		return NULL; 
	} else {
		return meth;
	}	
}

id
(PyObjCObject_GetObject)(PyObject* object)
{
	if (!PyObjCObject_Check(object)) {
		PyErr_Format(PyExc_TypeError,
			"'objc.objc_object' expected, got '%s'",
			object->ob_type->tp_name);
		
	}
	return PyObjCObject_GetObject(object);
}

void        
PyObjCObject_ClearObject(PyObject* object)
{
    if (object == NULL) abort();
	if (!PyObjCObject_Check(object)) {
		PyErr_Format(PyExc_TypeError,
			"'objc.objc_object' expected, got '%s'",
			object->ob_type->tp_name);
		
	}
	unregister_proxy(((PyObjCObject*)object)->objc_object);
	((PyObjCObject*)object)->objc_object = nil;
}