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 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
|
/*
mxProxy -- Proxy wrapper type
Copyright (c) 1998-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com
Copyright (c) 2000-2011, eGenix.com Software GmbH; mailto:info@egenix.com
See the documentation for further copyright information or contact
the author (mailto:mal@lemburg.com).
*/
/* Define this to aid in finding memory leaks */
/*#define MAL_MEM_DEBUG*/
/*#define MAL_DEBUG*/
/* Logging file used by debugging facility */
#ifndef MAL_DEBUG_OUTPUTFILE
# define MAL_DEBUG_OUTPUTFILE "mxProxy.log"
#endif
/* We want all our symbols to be exported */
#define MX_BUILDING_MXPROXY
/* Mark the module as Py_ssize_t clean. */
#define PY_SSIZE_T_CLEAN 1
/* Setup mxstdlib memory management */
#if 1
# define MAL_USE_PYMALLOC
#else
# define MAL_USE_C_MALLOC
#endif
#include "mx.h"
#include "mxProxy.h"
/* Version number: Major.Minor.Patchlevel */
#define MXPROXY_VERSION "3.2.1"
/* Define these to have the module use free lists (saves malloc calls) */
#define MXPROXY_FREELIST
/* --- module doc-string -------------------------------------------------- */
static char *Module_docstring =
MXPROXY_MODULE" -- Generic proxy wrapper type. Version "MXPROXY_VERSION"\n\n"
"Copyright (c) 1998-2000, Marc-Andre Lemburg; mailto:mal@lemburg.com\n"
"Copyright (c) 2000-2011, eGenix.com Software GmbH; mailto:info@egenix.com\n\n"
" All Rights Reserved\n\n"
"See the documentation for further information on copyrights,\n"
"or contact the author."
;
/* --- module globals ----------------------------------------------------- */
static PyObject *mxProxy_AccessError; /* AccessError Exception
object */
static PyObject *mxProxy_LostReferenceError; /* LostReferenceError
Exception object */
static PyObject *mxProxy_InternalError; /* InternalError Exception
object */
/* Free lists for Proxy and ProxyDelta objects */
#ifdef MXPROXY_FREELIST
static mxProxyObject *mxProxy_FreeList = NULL;
#endif
/* Dictionary mapping id integers to (object, [proxy1,proxy2,...])
tuples. See WeakReference APIs below for details. */
static PyObject *mxProxy_WeakReferences;
/* Flag telling us whether the module was initialized or not. */
static int mxProxy_Initialized = 0;
/* --- forward declarations ----------------------------------------------- */
staticforward PyTypeObject mxProxy_Type;
staticforward PyMethodDef mxProxy_Methods[];
static int mxProxy_DefuncObjectReference(mxProxyObject *self);
static int mxProxy_FinalizeWeakReferences(void);
/* --- internal macros ---------------------------------------------------- */
#define _mxProxy_Check(v) \
(((mxProxyObject *)(v))->ob_type == &mxProxy_Type)
/* --- module helpers ----------------------------------------------------- */
/* Create an exception object, insert it into the module dictionary
under the given name and return the object pointer; this is NULL in
case an error occurred. base can be given to indicate the base
object to be used by the exception object. It should be NULL
otherwise */
static
PyObject *insexc(PyObject *moddict,
char *name,
PyObject *base)
{
PyObject *v;
char fullname[256];
char *modname;
char *dot;
v = PyDict_GetItemString(moddict, "__name__");
if (v == NULL)
modname = NULL;
else
modname = PyString_AsString(v);
if (modname == NULL) {
PyErr_Clear();
modname = MXPROXY_MODULE;
}
/* The symbols from this extension are imported into
mx.<packagename>. We trim the name to not confuse the user with
an overly long package path. */
strcpy(fullname, modname);
dot = strchr(fullname, '.');
if (dot)
dot = strchr(dot+1, '.');
if (dot)
strcpy(dot+1, name);
else
sprintf(fullname, "%s.%s", modname, name);
v = PyErr_NewException(fullname, base, NULL);
if (v == NULL)
return NULL;
if (PyDict_SetItemString(moddict,name,v))
return NULL;
return v;
}
#if 0
/* Helper for adding integer constants to a dictionary. Check for
errors with PyErr_Occurred() */
static
void insint(PyObject *dict,
char *name,
int value)
{
PyObject *v = PyInt_FromLong((long)value);
PyDict_SetItemString(dict, name, v);
Py_XDECREF(v);
}
#endif
/* Helper for adding integer constants to a dictionary. Check for
errors with PyErr_Occurred() */
static
void insstr(PyObject *dict,
char *name,
char *value)
{
PyObject *v = PyString_FromString(value);
PyDict_SetItemString(dict, name, v);
Py_XDECREF(v);
}
#if 0
/* Helper for adding objects to dictionaries. Check for errors with
PyErr_Occurred() */
static
void insobj(PyObject *dict,
char *name,
PyObject *v)
{
PyDict_SetItemString(dict, name, v);
Py_XDECREF(v);
}
#endif
/* Converts a sequence to a lookup dictionary with entries
key:None. For strings the string is taken as key, for other types
the object's __name__ attribute. */
static
PyObject *seq2dict(PyObject *sequence)
{
Py_ssize_t i,len;
PyObject *v,*w;
len = PySequence_Length(sequence);
if (len < 0)
goto onError;
v = PyDict_New();
for (i = 0; i < len; i++) {
w = PySequence_GetItem(sequence,i);
if (!w) {
Py_DECREF(v);
goto onError;
}
/* Not a string, so take the object's name instead */
if (!PyString_Check(w)) {
PyObject *name;
name = PyObject_GetAttrString(w,"__name__");
if (!name) {
Py_DECREF(w);
Py_DECREF(v);
goto onError;
}
Py_DECREF(w);
w = name;
}
PyDict_SetItem(v,w,Py_None);
Py_DECREF(w);
}
return v;
onError:
return NULL;
}
/* (Re)Init the weak reference implementation. */
static
int mxProxy_InitWeakReferences(void)
{
/* Finalize first... */
if (mxProxy_WeakReferences != NULL &&
mxProxy_FinalizeWeakReferences())
goto onError;
/* Create weak references dictionary */
mxProxy_WeakReferences = PyDict_New();
if (mxProxy_WeakReferences == NULL)
goto onError;
return 0;
onError:
return -1;
}
/* Store the object in the mxProxy_WeakReferences dict to keep it
alive. Returns a new index object for the dictionary. */
static
int mxProxy_RegisterWeakReference(mxProxyObject *proxy,
PyObject *object)
{
PyObject *id = 0,*v,*w;
int rc;
id = PyInt_FromLong((long)object);
if (id == NULL)
goto onError;
DPRINTF("mxProxy_RegisterWeakReference: "
"proxy at %0lx, object at %0lx -> id at %0lx\n",
(long)proxy,(long)object,(long)id);
Py_Assert(mxProxy_WeakReferences != NULL &&
mxProxy_WeakReferences->ob_refcnt > 0,
mxProxy_InternalError,
"mxProxy_WeakReferences dict is not available");
v = PyDict_GetItem(mxProxy_WeakReferences,id);
if (v && PyTuple_Check(v)) {
mxProxyObject *p;
/* Existing entry... add the proxy object to the linked list
of weak proxies for this object */
Py_Assert(PyTuple_GET_ITEM(v,0) == object,
mxProxy_InternalError,
"inconsistency in mxProxy_WeakReferences dict");
p = (mxProxyObject *)PyCObject_AsVoidPtr(PyTuple_GET_ITEM(v,1));
if (!p)
goto onError;
while (p->next_weak_proxy != NULL)
p = p->next_weak_proxy;
p->next_weak_proxy = proxy;
DPRINTF("mxProxy_RegisterWeakReference: "
"added a new weak proxy to the list\n");
DPRINTF(" "
"refcounts... id:%i\n",
id->ob_refcnt);
}
else {
/* New entry: id:(object,CObject -> proxy) */
w = PyCObject_FromVoidPtr((void*)proxy,NULL);
if (!w)
goto onError;
v = PyTuple_New(2);
if (!v) {
Py_DECREF(w);
goto onError;
}
Py_INCREF(object);
PyTuple_SET_ITEM(v,0,object);
PyTuple_SET_ITEM(v,1,w);
rc = PyDict_SetItem(mxProxy_WeakReferences,id,v);
Py_DECREF(v);
if (rc != 0)
goto onError;
DPRINTF("mxProxy_RegisterWeakReference: "
"added a new weak dict entry id %0lx in dict %0lx\n",
(long)id,(long)mxProxy_WeakReferences);
DPRINTF(" "
"refcounts... id:%i, dict:%i\n",
id->ob_refcnt,mxProxy_WeakReferences->ob_refcnt);
}
proxy->object = id;
proxy->next_weak_proxy = NULL;
return 0;
onError:
Py_XDECREF(id);
return -1;
}
/* Walks along the weak proxy linked list rooted at proxy and defuncs
all proxies by notifying them via mxProxy_DefuncObjectReference().
*/
static
int mxProxy_DefuncWeakProxies(mxProxyObject *proxy)
{
DPRINTF("mxProxy_DefuncWeakProxies: proxy at %0lx\n",(long)proxy);
do {
mxProxy_DefuncObjectReference(proxy);
proxy = proxy->next_weak_proxy;
} while (proxy != NULL);
if (PyErr_Occurred()) {
DPRINTF("mxProxy_DefuncWeakProxies: "
"errors occurred during notification\n");
goto onError;
}
return 0;
onError:
return -1;
}
/* Removes the object reference from mxProxy_WeakReferences.
This calls mxProxy_DefuncWeakProxies() to defunc all weak proxies
using the object.
*/
static
int mxProxy_CollectWeakReference(mxProxyObject *proxy)
{
PyObject *id,*v;
id = proxy->object;
DPRINTF("mxProxy_CollectWeakReference: "
"proxy at %0lx, id at %0lx\n",
(long)proxy,(long)id);
Py_Assert(mxProxy_WeakReferences != NULL &&
mxProxy_WeakReferences->ob_refcnt > 0,
mxProxy_InternalError,
"mxProxy_WeakReferences dict is not available");
if (id == NULL) {
DPRINTF("mxProxy_CollectWeakReference: "
"nothing to do -- proxy is defunct\n");
return 0;
}
DPRINTF(" "
"refcounts... id:%i, dict:%i\n",
id->ob_refcnt,mxProxy_WeakReferences->ob_refcnt);
v = PyDict_GetItem(mxProxy_WeakReferences,id);
if (v && PyTuple_Check(v)) {
mxProxyObject *p;
int rc;
/* Keep the id object alive until the dict entry is removed. */
Py_INCREF(id);
/* Call the hook on all weak proxies in the linked list */
DPRINTF("mxProxy_CollectWeakReference: "
"notifying all weak proxies for this object\n");
p = PyCObject_AsVoidPtr(PyTuple_GET_ITEM(v,1));
if (!p)
goto onError;
if (mxProxy_DefuncWeakProxies(p))
goto onError;
/* Remove the mxProxy_WeakReferences entry for this object */
DPRINTF("mxProxy_CollectWeakReference: "
"removing the weak dict entry id %0lx from dict %0lx\n",
(long)id,(long)mxProxy_WeakReferences);
DPRINTF(" "
"refcounts... id:%i, dict:%i\n",
id->ob_refcnt,mxProxy_WeakReferences->ob_refcnt);
rc = PyDict_DelItem(mxProxy_WeakReferences,id);
/* Dereference the id object, possibly GCing it and return. */
Py_DECREF(id);
return rc;
}
else
Py_Error(mxProxy_InternalError,
"object not found in mxProxy_WeakReferences dict");
return 0;
onError:
return -1;
}
/* Remove the reference to object in mxProxy_WeakReferences.
This can cause the object to be garbage collected. The index object
is not DECREFed. */
static
int mxProxy_DeregisterWeakReference(mxProxyObject *proxy)
{
PyObject *id,*v,*w;
id = proxy->object;
DPRINTF("mxProxy_DeregisterWeakReference: proxy at %0lx, id at %0lx\n",
(long)proxy,(long)id);
Py_Assert(mxProxy_WeakReferences != NULL &&
mxProxy_WeakReferences->ob_refcnt > 0,
mxProxy_InternalError,
"mxProxy_WeakReferences dict is not available");
if (id == NULL) {
DPRINTF("mxProxy_DeregisterWeakReference: "
"nothing to do -- proxy is defunct\n");
return 0;
}
v = PyDict_GetItem(mxProxy_WeakReferences,id);
if (v && PyTuple_Check(v)) {
if (PyTuple_GET_ITEM(v,0)->ob_refcnt == 1)
/* GC the object along with the entry in
mxProxy_WeakReferences */
return mxProxy_CollectWeakReference(proxy);
else {
/* Remove the proxy object from the list */
mxProxyObject *p;
p = (mxProxyObject *)PyCObject_AsVoidPtr(PyTuple_GET_ITEM(v,1));
if (!p)
goto onError;
if (p == proxy) {
/* Change root of list */
if (p->next_weak_proxy == NULL) {
/* No more entries in the list: remove the
mxProxy_WeakReferences entry altogether */
DPRINTF("mxProxy_DeregisterWeakReference: "
"removing weak reference dict entry\n");
return PyDict_DelItem(mxProxy_WeakReferences,id);
}
DPRINTF("mxProxy_DeregisterWeakReference: "
"removing proxy entry at root of list\n");
w = PyCObject_FromVoidPtr((void*)p->next_weak_proxy,NULL);
if (!w)
goto onError;
Py_DECREF(PyTuple_GET_ITEM(v,1));
PyTuple_SET_ITEM(v,1,w);
return 0;
}
else {
/* Delink from list (contains at least 2 entries) */
mxProxyObject *q;
do {
q = p;
p = p->next_weak_proxy;
} while (p && (p != proxy));
Py_Assert(p != NULL,
mxProxy_InternalError,
"proxy object no longer in linked list");
q->next_weak_proxy = p->next_weak_proxy;
DPRINTF("mxProxy_DeregisterWeakReference: "
"removed proxy entry from list\n");
}
}
}
else {
DPRINTF("mxProxy_DeregisterWeakReference: id not found !\n");
Py_Error(mxProxy_InternalError,
"object not found in mxProxy_WeakReferences dict");
}
return 0;
onError:
return -1;
}
/* Returns a new reference to the weak referenced object indexed by
id.
This also checks the refcount on the object. In case it has dropped
to 1, the object is garbage collected, all proxies are informed of
this and a LostReferenceError is raised.
If id is NULL a LostReferenceError is raised.
*/
static
PyObject *mxProxy_GetWeakReferenceObject(mxProxyObject *proxy)
{
PyObject *id,*v,*w;
id = proxy->object;
DPRINTF("mxProxy_GetWeakReferenceObject: proxy at %0lx, id at %0lx\n",
(long)proxy,(long)id);
Py_Assert(mxProxy_WeakReferences != NULL &&
mxProxy_WeakReferences->ob_refcnt > 0,
mxProxy_InternalError,
"mxProxy_WeakReferences dict is not available");
Py_Assert(id != NULL,
mxProxy_LostReferenceError,
"object already garbage collected");
v = PyDict_GetItem(mxProxy_WeakReferences,id);
if (v && PyTuple_Check(v)) {
if (PyTuple_GET_ITEM(v,0)->ob_refcnt == 1) {
/* GC the object along with the entry in
mxProxy_WeakReferences and return an error */
DPRINTF("mxProxy_GetWeakReferenceObject: "
"garbage collecting object\n");
mxProxy_CollectWeakReference(proxy);
Py_Error(mxProxy_LostReferenceError,
"object already garbage collected");
}
w = PyTuple_GET_ITEM(v,0);
Py_INCREF(w);
DPRINTF("mxProxy_GetWeakReferenceObject: returning object at %0lx\n",
(long)w);
return w;
}
else
Py_Error(mxProxy_InternalError,
"object not found in mxProxy_WeakReferences dict");
onError:
return NULL;
}
static
int _mxProxy_CollectWeakReferences(int force)
{
PyObject *id,*v,*collect = 0;
mxProxyObject *proxy;
Py_ssize_t i;
Py_Assert(mxProxy_WeakReferences != NULL &&
mxProxy_WeakReferences->ob_refcnt > 0,
mxProxy_InternalError,
"mxProxy_WeakReferences dict is not available");
collect = PyList_New(0);
if (!collect)
goto onError;
/* Find and mark the root proxies in mxProxy_WeakReferences
pointing to (phantom) objects with refcount 1 (these are only
artificially kept alive). */
DPRINTF("mxProxy_CheckWeakReferenceDict: "
"checking for phantom objects\n");
i = 0;
while (PyDict_Next(mxProxy_WeakReferences, &i, &id, &v)) {
if (PyTuple_Check(v) &&
(force || PyTuple_GET_ITEM(v,0)->ob_refcnt == 1)) {
proxy = (mxProxyObject *)PyCObject_AsVoidPtr(
PyTuple_GET_ITEM(v,1));
if (!proxy)
goto onError;
DPRINTF("mxProxy_CheckWeakReferenceDict: "
"found proxy at %0lx, object at %0lx -> id at %0lx\n",
(long)proxy,(long)PyTuple_GET_ITEM(v,0),(long)id);
PyList_Append(collect,(PyObject *)proxy);
}
}
/* Collect the weak references via the root proxies */
for (i = 0; i < PyList_GET_SIZE(collect); i++) {
proxy = (mxProxyObject *)PyList_GET_ITEM(collect,i);
id = proxy->object;
/* Defunc the weak proxies in the linked list */
if (mxProxy_DefuncWeakProxies(proxy))
goto onError;
/* Remove the mxProxy_WeakReferences entry for this object */
DPRINTF("mxProxy_CheckWeakReferenceDict: "
"removing the weak dict entry for id at %0x\n",id);
if (PyDict_DelItem(mxProxy_WeakReferences,id))
goto onError;
}
Py_DECREF(collect);
return 0;
onError:
Py_XDECREF(collect);
return -1;
}
/* Checks the mxProxy_WeakReferences dict for entries which are only
artificially kept alive. */
static
int mxProxy_CheckWeakReferenceDict(void)
{
return _mxProxy_CollectWeakReferences(0);
}
/* Remove the weak reference dict entries and then try to collect the
mxProxy_WeakReferences dict itself. Sets mxProxy_WeakReferences to
NULL, so all subsequent weak reference actions cause errors.
It is not an error to call this API again after completed
finalization.
*/
static
int mxProxy_FinalizeWeakReferences(void)
{
if (mxProxy_WeakReferences == NULL ||
mxProxy_WeakReferences->ob_refcnt <= 0)
return 0;
if (_mxProxy_CollectWeakReferences(1))
goto onError;
Py_DECREF(mxProxy_WeakReferences);
mxProxy_WeakReferences = NULL;
return 0;
onError:
return -1;
}
/* --- Proxy Object -------------------------------------------------*/
/* --- allocation --- */
/* Create a new Proxy instance for object. interface may be a lookup
dictionary (values are not used), a sequence (which is converted to
a lookup dictionary by this function) or NULL (to disable the interface
check). passobj can be used to reextract the wrapped object.
If weak is true, a weak reference to the object will be created.
The object itself will be stored in the global
mxProxy_WeakReferences dictionary and the reference count on the
object be checked prior to every operation.
*/
static
mxProxyObject *mxProxy_New(PyObject *object,
PyObject *interface,
PyObject *passobj,
int weak)
{
mxProxyObject *proxy;
/* Convert the interface object into a lookup dictionary */
if (interface) {
if (PyDict_Check(interface))
Py_INCREF(interface);
/* Convert sequence to a dictionary */
else if (PySequence_Check(interface)) {
interface = seq2dict(interface);
if (!interface)
goto onError;
}
else
Py_Error(PyExc_TypeError,
"interface must be a dictionary, a sequence or not given");
}
/* Allocate the object */
#ifdef MXPROXY_FREELIST
if (mxProxy_FreeList) {
proxy = mxProxy_FreeList;
mxProxy_FreeList = *(mxProxyObject **)mxProxy_FreeList;
proxy->ob_type = &mxProxy_Type;
_Py_NewReference(proxy);
}
else
#endif
{
proxy = PyObject_NEW(mxProxyObject,&mxProxy_Type);
if (proxy == NULL) {
Py_XDECREF(interface);
goto onError;
}
}
/* Keep a (weak) reference to the object */
proxy->isWeak = (weak > 0);
if (!weak) {
Py_INCREF(object);
proxy->object = object;
proxy->next_weak_proxy = NULL;
}
else {
/* Store the index object instead of the object itself. */
if (mxProxy_RegisterWeakReference(proxy, object)) {
mxPy_UNREF(proxy);
PyObject_Del(proxy);
goto onError;
}
}
/* Errors may not occur after this line, or the onError-mechanism
must be changed. */
/* Init vars */
proxy->interface = interface;
Py_XINCREF(passobj);
proxy->passobj = passobj;
/* Cache some methods (this is only done for strong referencing
proxies since the method object contain a string reference to
the object) */
if (!weak && !PyMethod_Check(object) && !PyCFunction_Check(object)) {
PyObject *v;
v = PyObject_GetAttrString(object,"__public_getattr__");
if (!v)
PyErr_Clear();
proxy->public_getattr = v;
v = PyObject_GetAttrString(object,"__public_setattr__");
if (!v)
PyErr_Clear();
proxy->public_setattr = v;
v = PyObject_GetAttrString(object,"__cleanup__");
if (!v)
PyErr_Clear();
proxy->cleanup = v;
}
else {
proxy->public_getattr = NULL;
proxy->public_setattr = NULL;
proxy->cleanup = NULL;
}
return proxy;
onError:
return NULL;
}
/* --- deallocation --- */
static
void mxProxy_Free(mxProxyObject *proxy)
{
PyObject *error_type, *error_value, *error_traceback;
DPRINTF("mxProxy_Free: instance at %0lx\n",(long)proxy);
/* Allow cleanup of the wrapped object; errors are printed to stderr */
if (proxy->cleanup) {
PyObject *v;
/* Reanimate the Proxy */
Py_INCREF(proxy);
/* Save the current exception, since the call could produce a
new one which will get cleared. */
PyErr_Fetch(&error_type, &error_value, &error_traceback);
DPRINTF("mxProxy_Free: __cleanup__\n");
v = PyEval_CallObject(proxy->cleanup,(PyObject *)NULL);
if (!v) {
/* We print the error only if Python is run in debug mode
since this sometimes causes strange core dumps... */
if (PyErr_Occurred() && Py_DebugFlag) {
fprintf(stderr,"Error in ");
PyObject_Print(proxy->cleanup,stderr,Py_PRINT_RAW);
fprintf(stderr," ignored:\n");
PyErr_Print();
}
else if (Py_VerboseFlag) {
fprintf(stderr,"Error in ");
PyObject_Print(proxy->cleanup,stderr,Py_PRINT_RAW);
fprintf(stderr,
" ignored.\n"
"(run in debug mode to have the error printed)\n");
}
PyErr_Clear();
}
else
Py_DECREF(v);
/* Restore the previous exception */
PyErr_Restore(error_type, error_value, error_traceback);
/* We might be referenced again somewhere... */
if (proxy->ob_refcnt > 1) {
DPRINTF("mxProxy_Free: kept alive; refcnt=%i\n",proxy->ob_refcnt);
Py_DECREF(proxy);
return;
}
}
/* Remove the object reference */
DPRINTF("mxProxy_Free: removing object reference\n");
if (proxy->isWeak) {
/* Reanimate the Proxy */
Py_INCREF(proxy);
/* Save the current exception, since the call could produce a
new one which will get cleared. */
PyErr_Fetch(&error_type, &error_value, &error_traceback);
/* Deregister the weak reference. XXX Errors are ignored. */
if (mxProxy_DeregisterWeakReference(proxy))
PyErr_Clear();
/* Restore the previous exception */
PyErr_Restore(error_type, error_value, error_traceback);
/* We might be referenced again somewhere... */
if (proxy->ob_refcnt > 1) {
DPRINTF("mxProxy_Free: kept alive; refcnt=%i\n",proxy->ob_refcnt);
Py_DECREF(proxy);
return;
}
}
/* proxy->object could have already be defunct by a call to
mxProxy_DefuncObjectReference(). */
Py_XDECREF(proxy->object);
/* Now remove all references */
DPRINTF("mxProxy_Free: removing other references\n");
Py_XDECREF(proxy->interface);
Py_XDECREF(proxy->passobj);
Py_XDECREF(proxy->public_getattr);
Py_XDECREF(proxy->public_setattr);
Py_XDECREF(proxy->cleanup);
DPRINTF("mxProxy_Free: refcnt=%i\n",proxy->ob_refcnt);
#ifdef MXPROXY_FREELIST
/* Append to free list */
*(mxProxyObject **)proxy = mxProxy_FreeList;
mxProxy_FreeList = proxy;
#else
PyObject_Del(proxy);
#endif
DPRINTF("mxProxy_Free: dealloced\n");
}
/* --- internal functions --- */
/* Is direct access to slot name allowed ? Returns 1/0.
Slots are named just like the hooks that are available for them on
instance objects, e.g. '__call__' for tp_call. name has to be an
object; this is typically an interned string object.
*/
static
int mxProxy_SlotAccessAllowed(mxProxyObject *self,
PyObject *name)
{
PyObject *v;
DPRINTF("mxProxy_SlotAccessAllowed: "
"proxy at %0lx, object/index at %0lx, slot '%.200s' -- ",
(long)self,(long)self->object,PyString_AS_STRING(name));
/* Is interface access restricted ? */
if (!self->interface) {
DPRINTF("yes (unrestricted)\n");
return 1;
}
/* Check if the interface dict allows access (if given) */
v = PyDict_GetItem(self->interface,name);
if (!v) {
PyErr_Clear();
DPRINTF("no\n");
return 0;
}
DPRINTF("yes\n");
return 1;
}
/* This hook is called to inform the weak referencing Proxy of the
fact that his object reference is about to be garbage collected.
The hook DECREFs index object stored in self->object and then sets
self->object to NULL. */
static
int mxProxy_DefuncObjectReference(mxProxyObject *self)
{
DPRINTF("mxProxy_DefuncObjectReference: "
"proxy at %0lx, object/index at %0lx\n",
(long)self,(long)self->object);
Py_XDECREF(self->object);
self->object = NULL;
return 0;
}
/* Get attribute name from the wrapped object */
static
PyObject *mxProxy_GetattrObject(PyObject *obj,
PyObject *name)
{
mxProxyObject *self = (mxProxyObject *)obj;
PyObject *v;
DPRINTF("mxProxy_GetattrObject: %.200s\n",
PyString_AsString(PyObject_Str(name)));
/* Access to Proxy methods is never restricted */
if (PyString_Check(name)) {
char *sname = PyString_AS_STRING(name);
if (sname[0] == 'p' && sname[1] == 'r' &&
sname[2] == 'o' && sname[3] == 'x' &&
sname[4] == 'y' && sname[5] == '_')
return Py_FindMethod(mxProxy_Methods,
(PyObject *)self,sname);
}
/* Check if the interface dict allows access (if given) */
if (self->interface) {
v = PyDict_GetItem(self->interface,name);
if (!v) {
PyErr_Clear();
goto noAccess;
}
}
/* Get the attribute via the __public_getattr__ hook (if given),
or use the standard method; note that weak Proxies do not cache
the __public_getattr__ hook. */
if (self->public_getattr) {
PyObject *arg;
arg = PyTuple_New(1);
if (!arg)
goto onError;
Py_INCREF(name);
PyTuple_SET_ITEM(arg,0,name);
v = PyEval_CallObject(self->public_getattr,arg);
Py_DECREF(arg);
}
else {
if (self->isWeak) {
PyObject *object;
object = mxProxy_GetWeakReferenceObject(self);
if (!object)
goto onError;
v = PyObject_GetAttr(object,name);
Py_DECREF(object);
}
else
v = PyObject_GetAttr(self->object,name);
}
if (!v)
goto onError;
/* Check if we have to wrap a method (which carries a reference to
the wrapped object) */
if (PyMethod_Check(v) || PyCFunction_Check(v)) {
static PyObject *callinterface;
PyObject *w;
DPRINTF("mxProxy_GetattrObject: wrapping method %.200s\n",
PyString_AsString(PyObject_Str(v)));
if (callinterface == NULL)
callinterface = Py_BuildValue("{s:O}","__call__",Py_None);
w = (PyObject *)mxProxy_New(v,callinterface,NULL,0);
Py_DECREF(v);
v = w;
}
return v;
noAccess:
if (PyString_Check(name)) {
Py_ErrorWithArg(mxProxy_AccessError,
"attribute read access (%.200s) denied",
PyString_AS_STRING(name));
}
else {
Py_Error(mxProxy_AccessError,
"attribute read access denied");
}
onError:
return NULL;
}
/* Set attribute name of the wrapped object to value; return -1 in
case of error, 0 otherwise. */
static
int mxProxy_SetattrObject(PyObject *obj,
PyObject *name,
PyObject *value)
{
mxProxyObject *self = (mxProxyObject *)obj;
PyObject *v;
DPRINTF("mxProxy_SetattrObject: %.200s to %.200s\n",
PyString_AsString(PyObject_Str(name)),
PyString_AsString(PyObject_Repr(value)));
/* Check if the interface dict allows access (if given) */
if (self->interface) {
v = PyDict_GetItem(self->interface,name);
if (!v) {
PyErr_Clear();
goto noAccess;
}
}
/* Use the __public_setattr__ hook (if given), or the standard
method; note that weak Proxies do not cache the
__public_setattr__ hook. */
if (self->public_setattr) {
PyObject *arg;
arg = PyTuple_New(2);
if (!arg)
goto onError;
Py_INCREF(name);
PyTuple_SET_ITEM(arg,0,name);
Py_INCREF(value);
PyTuple_SET_ITEM(arg,1,value);
v = PyEval_CallObject(self->public_setattr,arg);
Py_DECREF(arg);
if (!v)
goto onError;
Py_DECREF(v);
return 0;
}
else {
if (self->isWeak) {
PyObject *object;
int rc;
object = mxProxy_GetWeakReferenceObject(self);
if (!object)
goto onError;
rc = PyObject_SetAttr(object,name,value);
Py_DECREF(object);
return rc;
}
else
return PyObject_SetAttr(self->object,name,value);
}
noAccess:
if (PyString_Check(name)) {
Py_ErrorWithArg(mxProxy_AccessError,
"attribute write access (%.200s) denied",
PyString_AS_STRING(name));
}
else {
Py_Error(mxProxy_AccessError,
"attribute write access denied");
}
onError:
return -1;
}
/* --- API functions --- */
/* --- methods --- */
#define proxy ((mxProxyObject*)self)
Py_C_Function( mxProxy_proxy_object,
"proxy_object(passobj)\n\n"
"Returns the wrapped object as-is provided the passobj\n"
"matches the one given at creation time."
)
{
PyObject *passobj;
Py_GetArg("O",passobj);
if (passobj == proxy->passobj) {
if (proxy->isWeak) {
PyObject *object;
object = mxProxy_GetWeakReferenceObject(proxy);
if (!object)
goto onError;
return object;
}
else {
Py_INCREF(proxy->object);
return proxy->object;
}
}
Py_Error(mxProxy_AccessError,
"wrong pass-object");
onError:
return NULL;
}
Py_C_Function( mxProxy_proxy_defunct,
"proxy_defunct()\n\n"
"Returns 1 iff the referenced object has already been\n"
"garbage collected."
)
{
Py_NoArgsCheck();
if (proxy->object == NULL)
return PyInt_FromLong(1);
else
return PyInt_FromLong(0);
onError:
return NULL;
}
Py_C_Function( mxProxy_proxy_getattr,
"proxy_getattr(name)\n\n"
"Tries to get the attribute name from the wrapped object\n"
"and returns it. The normal access restriction apply."
)
{
PyObject *name;
Py_GetArg("O",name);
return mxProxy_GetattrObject(self, name);
onError:
return NULL;
}
Py_C_Function( mxProxy_proxy_setattr,
"proxy_setattr(name,value)\n\n"
"Tries to set the attribute name of the wrapped object\n"
"to value. The normal access restriction apply."
)
{
PyObject *name,*value;
Py_Get2Args("OO",name,value);
if (mxProxy_SetattrObject(self, name, value))
goto onError;
Py_ReturnNone();
onError:
return NULL;
}
#undef proxy
/* --- slots --- */
static
PyObject *mxProxy_Repr(PyObject *obj)
{
mxProxyObject *self = (mxProxyObject *)obj;
char t[100];
if (self->isWeak) {
if (self->object)
sprintf(t,"<WeakProxy object at %lx>",(long)self);
else
sprintf(t,"<defunct WeakProxy object at %lx>",(long)self);
}
else
sprintf(t,"<Proxy object at %lx>",(long)self);
return PyString_FromString(t);
}
/* Macros for defining slot APIs with 0,1,2 and 3 arguments */
#define SLOT_API_PPP(name,slot,function,arg1type,arg2type,arg3type,rctype,errorrc) \
static \
rctype name(PyObject *obj, arg1type v, arg2type w, arg3type x) \
{ \
mxProxyObject *self = (mxProxyObject *)obj; \
static PyObject *slotstr; \
\
if (slotstr == NULL) \
slotstr = PyString_InternFromString(#slot); \
Py_Assert(mxProxy_SlotAccessAllowed(self,slotstr), \
mxProxy_AccessError, \
#slot" access denied"); \
if (self->isWeak) { \
PyObject *object; \
rctype rc; \
object = mxProxy_GetWeakReferenceObject(self); \
if (!object) \
goto onError; \
rc = function(object,v,w,x); \
Py_DECREF(object); \
return rc; \
} \
else \
return function(self->object,v,w,x); \
\
onError: \
return errorrc; \
}
#define SLOT_API_PP(name,slot,function,arg1type,arg2type,rctype,errorrc) \
static \
rctype name(PyObject *obj, arg1type v, arg2type w) \
{ \
mxProxyObject *self = (mxProxyObject *)obj; \
static PyObject *slotstr; \
\
if (slotstr == NULL) \
slotstr = PyString_InternFromString(#slot); \
Py_Assert(mxProxy_SlotAccessAllowed(self,slotstr), \
mxProxy_AccessError, \
#slot" access denied"); \
if (self->isWeak) { \
PyObject *object; \
rctype rc; \
object = mxProxy_GetWeakReferenceObject(self); \
if (!object) \
goto onError; \
rc = function(object,v,w); \
Py_DECREF(object); \
return rc; \
} \
else \
return function(self->object,v,w); \
\
onError: \
return errorrc; \
}
#define SLOT_API_P(name,slot,function,arg1type,rctype,errorrc) \
static \
rctype name(PyObject *obj, arg1type v) \
{ \
mxProxyObject *self = (mxProxyObject *)obj; \
static PyObject *slotstr; \
\
if (slotstr == NULL) \
slotstr = PyString_InternFromString(#slot); \
Py_Assert(mxProxy_SlotAccessAllowed(self,slotstr), \
mxProxy_AccessError, \
#slot" access denied"); \
if (self->isWeak) { \
PyObject *object; \
rctype rc; \
object = mxProxy_GetWeakReferenceObject(self); \
if (!object) \
goto onError; \
rc = function(object,v); \
Py_DECREF(object); \
return rc; \
} \
else \
return function(self->object,v); \
\
onError: \
return errorrc; \
}
#define SLOT_API(name,slot,function,rctype,errorrc) \
static \
rctype name(PyObject *obj) \
{ \
mxProxyObject *self = (mxProxyObject *)obj; \
static PyObject *slotstr; \
\
if (slotstr == NULL) \
slotstr = PyString_InternFromString(#slot); \
Py_Assert(mxProxy_SlotAccessAllowed(self,slotstr), \
mxProxy_AccessError, \
#slot" access denied"); \
if (self->isWeak) { \
PyObject *object; \
rctype rc; \
object = mxProxy_GetWeakReferenceObject(self); \
if (!object) \
goto onError; \
rc = function(object); \
Py_DECREF(object); \
return rc; \
} \
else \
return function(self->object); \
\
onError: \
return errorrc; \
}
/* Basic slots */
SLOT_API_PP(mxProxy_Call,__call__,PyEval_CallObjectWithKeywords,PyObject*,PyObject*,PyObject*,NULL)
SLOT_API(mxProxy_Hash,__hash__,PyObject_Hash,long,-1)
SLOT_API(mxProxy_Str,__str__,PyObject_Str,PyObject*,NULL)
SLOT_API_P(mxProxy_Compare,__cmp__,PyObject_Compare,PyObject*,int,-1)
/* Mapping */
SLOT_API_P(mxProxy_GetItem,__getitem__,PyObject_GetItem,PyObject*,PyObject*,NULL)
SLOT_API_PP(mxProxy_SetItem,__setitem__,PyObject_SetItem,PyObject*,PyObject*,int,-1)
SLOT_API(mxProxy_Length,__len__,PyObject_Length,Py_ssize_t,-1)
/* Sequence */
SLOT_API_P(mxProxy_Concat,__add__,PySequence_Concat,PyObject*,PyObject*,NULL)
SLOT_API_P(mxProxy_Repeat,__repeat__,PySequence_Repeat,Py_ssize_t,PyObject*,NULL)
SLOT_API_P(mxProxy_GetIndex,__getitem__,PySequence_GetItem,Py_ssize_t,PyObject*,NULL)
SLOT_API_PP(mxProxy_GetSlice,__getslice__,PySequence_GetSlice,Py_ssize_t,Py_ssize_t,PyObject*,NULL)
SLOT_API_PP(mxProxy_SetIndex,__setitem__,PySequence_SetItem,Py_ssize_t,PyObject*,int,-1)
SLOT_API_PPP(mxProxy_SetSlice,__getitem__,PySequence_SetSlice,Py_ssize_t,Py_ssize_t,PyObject*,int,-1)
/* Number
Note: number coercion does not work yet, so most of these are
currently useless !
*/
SLOT_API_P(mxProxy_Add,__add__,PyNumber_Add,PyObject*,PyObject*,NULL)
SLOT_API_P(mxProxy_Sub,__sub__,PyNumber_Subtract,PyObject*,PyObject*,NULL)
SLOT_API_P(mxProxy_Multiply,__mul__,PyNumber_Multiply,PyObject*,PyObject*,NULL)
SLOT_API_P(mxProxy_Divide,__div__,PyNumber_Divide,PyObject*,PyObject*,NULL)
SLOT_API_P(mxProxy_Remainder,__mod__,PyNumber_Remainder,PyObject*,PyObject*,NULL)
SLOT_API_P(mxProxy_Divmod,__divmod__,PyNumber_Divmod,PyObject*,PyObject*,NULL)
SLOT_API_PP(mxProxy_Power,__pow__,PyNumber_Power,PyObject*,PyObject*,PyObject*,NULL)
SLOT_API(mxProxy_Negative,__neg__,PyNumber_Negative,PyObject*,NULL)
SLOT_API(mxProxy_Positive,__pos__,PyNumber_Positive,PyObject*,NULL)
SLOT_API(mxProxy_Absolute,__abs__,PyNumber_Absolute,PyObject*,NULL)
SLOT_API(mxProxy_IsTrue,__true__,PyObject_IsTrue,int,-1)
SLOT_API(mxProxy_Invert,__invert__,PyNumber_Invert,PyObject*,NULL)
SLOT_API_P(mxProxy_Lshift,__lshift__,PyNumber_Lshift,PyObject*,PyObject*,NULL)
SLOT_API_P(mxProxy_Rshift,__rshift__,PyNumber_Rshift,PyObject*,PyObject*,NULL)
SLOT_API_P(mxProxy_And,__and__,PyNumber_And,PyObject*,PyObject*,NULL)
SLOT_API_P(mxProxy_Xor,__str__,PyNumber_Xor,PyObject*,PyObject*,NULL)
SLOT_API_P(mxProxy_Or,__or__,PyNumber_Or,PyObject*,PyObject*,NULL)
/* coerce ? */
SLOT_API(mxProxy_Int,__int__,PyNumber_Int,PyObject*,NULL)
SLOT_API(mxProxy_Long,__long__,PyNumber_Long,PyObject*,NULL)
SLOT_API(mxProxy_Float,__float__,PyNumber_Float,PyObject*,NULL)
/* oct, hex ? */
/* Python Type Tables */
static PyNumberMethods mxProxy_NumberSlots = {
mxProxy_Add, /*nb_add*/
mxProxy_Sub, /*nb_subtract*/
mxProxy_Multiply, /*nb_multiply*/
mxProxy_Divide, /*nb_divide*/
mxProxy_Remainder, /*nb_remainder*/
mxProxy_Divmod, /*nb_divmod*/
mxProxy_Power, /*nb_power*/
mxProxy_Negative, /*nb_negative*/
mxProxy_Positive, /*nb_positive*/
mxProxy_Absolute, /*nb_absolute*/
mxProxy_IsTrue, /*nb_nonzero*/
mxProxy_Invert, /*nb_invert*/
mxProxy_Lshift, /*nb_lshift*/
mxProxy_Rshift, /*nb_rshift*/
mxProxy_And, /*nb_and*/
mxProxy_Xor, /*nb_xor*/
mxProxy_Or, /*nb_or*/
0, /*nb_coerce*/
mxProxy_Int, /*nb_int*/
mxProxy_Long, /*nb_long*/
mxProxy_Float, /*nb_float*/
0, /*nb_oct*/
0, /*nb_hex*/
};
static PySequenceMethods mxProxy_SequenceSlots = {
mxProxy_Length, /*sq_length*/
mxProxy_Concat, /*sq_concat*/
mxProxy_Repeat, /*sq_repeat*/
mxProxy_GetIndex, /*sq_item*/
mxProxy_GetSlice, /*sq_slice*/
mxProxy_SetIndex, /*sq_ass_item*/
mxProxy_SetSlice, /*sq_ass_slice*/
};
static PyMappingMethods mxProxy_MappingSlots = {
mxProxy_Length, /*mp_length*/
mxProxy_GetItem, /*mp_subscript*/
mxProxy_SetItem, /*mp_ass_subscript*/
};
statichere
PyTypeObject mxProxy_Type = {
PyObject_HEAD_INIT(0) /* init at startup ! */
0, /*ob_size*/
"Proxy", /*tp_name*/
sizeof(mxProxyObject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* slots */
(destructor)mxProxy_Free, /*tp_dealloc*/
0, /*tp_print*/
0, /*tp_getattr -- see tp_getattro */
0, /*tp_setattr -- see tp_setattro */
mxProxy_Compare, /*tp_compare*/
mxProxy_Repr, /*tp_repr*/
&mxProxy_NumberSlots, /*tp_as_number*/
&mxProxy_SequenceSlots, /*tp_as_sequence*/
&mxProxy_MappingSlots, /*tp_as_mapping*/
mxProxy_Hash, /*tp_hash*/
mxProxy_Call, /*tp_call*/
mxProxy_Str, /*tp_str*/
mxProxy_GetattrObject, /*tp_getattro*/
mxProxy_SetattrObject, /*tp_setattro*/
0, /*tp_as_buffer*/
0, /*tp_xxx4*/
0 /*tp_doc*/
};
/* Python Method Table
Note: Proxy method names must start with 'proxy_' and cannot be
access restricted.
*/
statichere
PyMethodDef mxProxy_Methods[] =
{
Py_MethodListEntry("proxy_getattr",mxProxy_proxy_getattr),
Py_MethodListEntry("proxy_setattr",mxProxy_proxy_setattr),
Py_MethodListEntry("proxy_object",mxProxy_proxy_object),
Py_MethodListEntryNoArgs("proxy_defunct",mxProxy_proxy_defunct),
{NULL,NULL} /* end of list */
};
/* --- module interface ---------------------------------------------------- */
Py_C_Function( mxProxy_Proxy,
"Proxy(object,interface=None,passobj=None)\n"
)
{
PyObject *object,*interface = NULL,*passobj=NULL;
Py_Get3Args("O|OO",object,interface,passobj);
if (interface == Py_None)
interface = NULL;
if (passobj == Py_None)
passobj = NULL;
return (PyObject *)mxProxy_New(object,interface,passobj,0);
onError:
return NULL;
}
Py_C_Function( mxProxy_WeakProxy,
"WeakProxy(object,interface=None,passobj=None)\n"
)
{
PyObject *object,*interface = NULL,*passobj=NULL;
Py_Get3Args("O|OO",object,interface,passobj);
if (interface == Py_None)
interface = NULL;
if (passobj == Py_None)
passobj = NULL;
return (PyObject *)mxProxy_New(object,interface,passobj,1);
onError:
return NULL;
}
Py_C_Function( mxProxy_checkweakrefs,
"checkweakrefs()\n"
)
{
Py_NoArgsCheck();
if (mxProxy_CheckWeakReferenceDict())
goto onError;
Py_ReturnNone();
onError:
return NULL;
}
Py_C_Function( mxProxy_initweakrefs,
"initweakrefs()\n"
)
{
Py_NoArgsCheck();
if (mxProxy_InitWeakReferences())
goto onError;
Py_ReturnNone();
onError:
return NULL;
}
Py_C_Function( mxProxy_finalizeweakrefs,
"finalizeweakrefs()\n"
)
{
Py_NoArgsCheck();
if (mxProxy_FinalizeWeakReferences())
goto onError;
Py_ReturnNone();
onError:
return NULL;
}
/* Python Method Table */
static
PyMethodDef Module_methods[] =
{
Py_MethodListEntry("Proxy",mxProxy_Proxy),
Py_MethodListEntry("WeakProxy",mxProxy_WeakProxy),
Py_MethodListEntryNoArgs("checkweakrefs",mxProxy_checkweakrefs),
Py_MethodListEntryNoArgs("initweakrefs",mxProxy_initweakrefs),
Py_MethodListEntryNoArgs("finalizeweakrefs",mxProxy_finalizeweakrefs),
{NULL,NULL} /* end of list */
};
/* Cleanup function */
static
void mxProxyModule_Cleanup(void)
{
#ifdef MXPROXY_FREELIST
{
mxProxyObject *d = mxProxy_FreeList;
while (d != NULL) {
mxProxyObject *v = d;
d = *(mxProxyObject **)d;
PyObject_Del(v);
}
mxProxy_FreeList = NULL;
}
#endif
/* mxProxy_WeakReferences should have a refcount of zero by now;
if it doesn't, then someone got hold of the dictionary
available through the module dictionary... too bad: weak
references won't work then. */
if (mxProxy_WeakReferences != NULL &&
mxProxy_WeakReferences->ob_refcnt > 0) {
DPRINTF("mxProxyModule_Cleanup: "
"memory leak: weakref dictionary still alive\n");
}
mxProxy_WeakReferences = NULL;
/* Reset mxProxy_Initialized flag */
mxProxy_Initialized = 0;
}
/* Create PyMethodObjects and register them in the module's dict */
MX_EXPORT(void)
initmxProxy(void)
{
PyObject *module, *moddict;
if (mxProxy_Initialized)
Py_Error(PyExc_SystemError,
"can't initialize "MXPROXY_MODULE" more than once");
/* Init type objects */
PyType_Init(mxProxy_Type);
/* Create module */
module = Py_InitModule4(MXPROXY_MODULE, /* Module name */
Module_methods, /* Method list */
Module_docstring, /* Module doc-string */
(PyObject *)NULL, /* always pass this as *self */
PYTHON_API_VERSION); /* API Version */
if (module == NULL)
goto onError;
/* Init globals */
#ifdef MXPROXY_FREELIST
mxProxy_FreeList = NULL;
#endif
/* Register cleanup function */
if (Py_AtExit(mxProxyModule_Cleanup))
/* XXX what to do if we can't register that function ??? */;
/* Init weak reference implementation */
if (mxProxy_InitWeakReferences())
goto onError;
/* Add some constants to the module's dict */
moddict = PyModule_GetDict(module);
if (moddict == NULL)
goto onError;
insstr(moddict,"__version__",MXPROXY_VERSION);
/* Errors */
if (!(mxProxy_AccessError = insexc(moddict,
"AccessError",
PyExc_AttributeError)))
goto onError;
if (!(mxProxy_LostReferenceError = insexc(moddict,
"LostReferenceError",
mxProxy_AccessError)))
goto onError;
if (!(mxProxy_InternalError = insexc(moddict,
"InternalError",
PyExc_StandardError)))
goto onError;
/* Type objects */
Py_INCREF(&mxProxy_Type);
PyDict_SetItemString(moddict,"ProxyType",
(PyObject *)&mxProxy_Type);
DPRINTF("initmxProxy: module dict refcnt = %i\n",
moddict->ob_refcnt);
/* We are now initialized */
mxProxy_Initialized = 1;
onError:
/* Check for errors and report them */
if (PyErr_Occurred())
Py_ReportModuleInitError(MXPROXY_MODULE);
return;
}
|