| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#include "Python.h" |
| 5 |
#include "frameobject.h" |
| 6 |
|
| 7 |
#ifdef __cplusplus |
| 8 |
extern "C" { |
| 9 |
#endif |
| 10 |
|
| 11 |
#ifdef Py_REF_DEBUG |
| 12 |
Py_ssize_t _Py_RefTotal; |
| 13 |
|
| 14 |
Py_ssize_t |
| 15 |
_Py_GetRefTotal(void) |
| 16 |
{ |
| 17 |
PyObject *o; |
| 18 |
Py_ssize_t total = _Py_RefTotal; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
o = _PyDict_Dummy(); |
| 23 |
if (o != NULL) |
| 24 |
total -= o->ob_refcnt; |
| 25 |
o = _PySet_Dummy(); |
| 26 |
if (o != NULL) |
| 27 |
total -= o->ob_refcnt; |
| 28 |
return total; |
| 29 |
} |
| 30 |
#endif |
| 31 |
|
| 32 |
int Py_DivisionWarningFlag; |
| 33 |
int Py_Py3kWarningFlag; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
#ifdef Py_TRACE_REFS |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
static PyObject refchain = {&refchain, &refchain}; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
void |
| 58 |
_Py_AddToAllObjects(PyObject *op, int force) |
| 59 |
{ |
| 60 |
#ifdef Py_DEBUG |
| 61 |
if (!force) { |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
assert((op->_ob_prev == NULL) == (op->_ob_next == NULL)); |
| 66 |
} |
| 67 |
#endif |
| 68 |
if (force || op->_ob_prev == NULL) { |
| 69 |
op->_ob_next = refchain._ob_next; |
| 70 |
op->_ob_prev = &refchain; |
| 71 |
refchain._ob_next->_ob_prev = op; |
| 72 |
refchain._ob_next = op; |
| 73 |
} |
| 74 |
} |
| 75 |
#endif |
| 76 |
|
| 77 |
#ifdef COUNT_ALLOCS |
| 78 |
static PyTypeObject *type_list; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
static int unlist_types_without_objects; |
| 86 |
extern Py_ssize_t tuple_zero_allocs, fast_tuple_allocs; |
| 87 |
extern Py_ssize_t quick_int_allocs, quick_neg_int_allocs; |
| 88 |
extern Py_ssize_t null_strings, one_strings; |
| 89 |
void |
| 90 |
dump_counts(FILE* f) |
| 91 |
{ |
| 92 |
PyTypeObject *tp; |
| 93 |
|
| 94 |
for (tp = type_list; tp; tp = tp->tp_next) |
| 95 |
fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, " |
| 96 |
"freed: %" PY_FORMAT_SIZE_T "d, " |
| 97 |
"max in use: %" PY_FORMAT_SIZE_T "d\n", |
| 98 |
tp->tp_name, tp->tp_allocs, tp->tp_frees, |
| 99 |
tp->tp_maxalloc); |
| 100 |
fprintf(f, "fast tuple allocs: %" PY_FORMAT_SIZE_T "d, " |
| 101 |
"empty: %" PY_FORMAT_SIZE_T "d\n", |
| 102 |
fast_tuple_allocs, tuple_zero_allocs); |
| 103 |
fprintf(f, "fast int allocs: pos: %" PY_FORMAT_SIZE_T "d, " |
| 104 |
"neg: %" PY_FORMAT_SIZE_T "d\n", |
| 105 |
quick_int_allocs, quick_neg_int_allocs); |
| 106 |
fprintf(f, "null strings: %" PY_FORMAT_SIZE_T "d, " |
| 107 |
"1-strings: %" PY_FORMAT_SIZE_T "d\n", |
| 108 |
null_strings, one_strings); |
| 109 |
} |
| 110 |
|
| 111 |
PyObject * |
| 112 |
get_counts(void) |
| 113 |
{ |
| 114 |
PyTypeObject *tp; |
| 115 |
PyObject *result; |
| 116 |
PyObject *v; |
| 117 |
|
| 118 |
result = PyList_New(0); |
| 119 |
if (result == NULL) |
| 120 |
return NULL; |
| 121 |
for (tp = type_list; tp; tp = tp->tp_next) { |
| 122 |
v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs, |
| 123 |
tp->tp_frees, tp->tp_maxalloc); |
| 124 |
if (v == NULL) { |
| 125 |
Py_DECREF(result); |
| 126 |
return NULL; |
| 127 |
} |
| 128 |
if (PyList_Append(result, v) < 0) { |
| 129 |
Py_DECREF(v); |
| 130 |
Py_DECREF(result); |
| 131 |
return NULL; |
| 132 |
} |
| 133 |
Py_DECREF(v); |
| 134 |
} |
| 135 |
return result; |
| 136 |
} |
| 137 |
|
| 138 |
void |
| 139 |
inc_count(PyTypeObject *tp) |
| 140 |
{ |
| 141 |
if (tp->tp_next == NULL && tp->tp_prev == NULL) { |
| 142 |
|
| 143 |
if (tp->tp_next != NULL) |
| 144 |
Py_FatalError("XXX inc_count sanity check"); |
| 145 |
if (type_list) |
| 146 |
type_list->tp_prev = tp; |
| 147 |
tp->tp_next = type_list; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
Py_INCREF(tp); |
| 157 |
type_list = tp; |
| 158 |
#ifdef Py_TRACE_REFS |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
_Py_AddToAllObjects((PyObject *)tp, 0); |
| 163 |
#endif |
| 164 |
} |
| 165 |
tp->tp_allocs++; |
| 166 |
if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc) |
| 167 |
tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees; |
| 168 |
} |
| 169 |
|
| 170 |
void dec_count(PyTypeObject *tp) |
| 171 |
{ |
| 172 |
tp->tp_frees++; |
| 173 |
if (unlist_types_without_objects && |
| 174 |
tp->tp_allocs == tp->tp_frees) { |
| 175 |
|
| 176 |
if (tp->tp_prev) |
| 177 |
tp->tp_prev->tp_next = tp->tp_next; |
| 178 |
else |
| 179 |
type_list = tp->tp_next; |
| 180 |
if (tp->tp_next) |
| 181 |
tp->tp_next->tp_prev = tp->tp_prev; |
| 182 |
tp->tp_next = tp->tp_prev = NULL; |
| 183 |
Py_DECREF(tp); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
#endif |
| 188 |
|
| 189 |
#ifdef Py_REF_DEBUG |
| 190 |
|
| 191 |
void |
| 192 |
_Py_NegativeRefcount(const char *fname, int lineno, PyObject *op) |
| 193 |
{ |
| 194 |
char buf[300]; |
| 195 |
|
| 196 |
PyOS_snprintf(buf, sizeof(buf), |
| 197 |
"%s:%i object at %p has negative ref count " |
| 198 |
"%" PY_FORMAT_SIZE_T "d", |
| 199 |
fname, lineno, op, op->ob_refcnt); |
| 200 |
Py_FatalError(buf); |
| 201 |
} |
| 202 |
|
| 203 |
#endif |
| 204 |
|
| 205 |
void |
| 206 |
Py_IncRef(PyObject *o) |
| 207 |
{ |
| 208 |
Py_XINCREF(o); |
| 209 |
} |
| 210 |
|
| 211 |
void |
| 212 |
Py_DecRef(PyObject *o) |
| 213 |
{ |
| 214 |
Py_XDECREF(o); |
| 215 |
} |
| 216 |
|
| 217 |
PyObject * |
| 218 |
PyObject_Init(PyObject *op, PyTypeObject *tp) |
| 219 |
{ |
| 220 |
if (op == NULL) |
| 221 |
return PyErr_NoMemory(); |
| 222 |
|
| 223 |
Py_TYPE(op) = tp; |
| 224 |
_Py_NewReference(op); |
| 225 |
return op; |
| 226 |
} |
| 227 |
|
| 228 |
PyVarObject * |
| 229 |
PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size) |
| 230 |
{ |
| 231 |
if (op == NULL) |
| 232 |
return (PyVarObject *) PyErr_NoMemory(); |
| 233 |
|
| 234 |
op->ob_size = size; |
| 235 |
Py_TYPE(op) = tp; |
| 236 |
_Py_NewReference((PyObject *)op); |
| 237 |
return op; |
| 238 |
} |
| 239 |
|
| 240 |
PyObject * |
| 241 |
_PyObject_New(PyTypeObject *tp) |
| 242 |
{ |
| 243 |
PyObject *op; |
| 244 |
op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp)); |
| 245 |
if (op == NULL) |
| 246 |
return PyErr_NoMemory(); |
| 247 |
return PyObject_INIT(op, tp); |
| 248 |
} |
| 249 |
|
| 250 |
PyVarObject * |
| 251 |
_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems) |
| 252 |
{ |
| 253 |
PyVarObject *op; |
| 254 |
const size_t size = _PyObject_VAR_SIZE(tp, nitems); |
| 255 |
op = (PyVarObject *) PyObject_MALLOC(size); |
| 256 |
if (op == NULL) |
| 257 |
return (PyVarObject *)PyErr_NoMemory(); |
| 258 |
return PyObject_INIT_VAR(op, tp, nitems); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
#undef _PyObject_Del |
| 263 |
void |
| 264 |
_PyObject_Del(PyObject *op) |
| 265 |
{ |
| 266 |
PyObject_FREE(op); |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
static int |
| 271 |
internal_print(PyObject *op, FILE *fp, int flags, int nesting) |
| 272 |
{ |
| 273 |
int ret = 0; |
| 274 |
if (nesting > 10) { |
| 275 |
PyErr_SetString(PyExc_RuntimeError, "print recursion"); |
| 276 |
return -1; |
| 277 |
} |
| 278 |
if (PyErr_CheckSignals()) |
| 279 |
return -1; |
| 280 |
#ifdef USE_STACKCHECK |
| 281 |
if (PyOS_CheckStack()) { |
| 282 |
PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
| 283 |
return -1; |
| 284 |
} |
| 285 |
#endif |
| 286 |
clearerr(fp); |
| 287 |
if (op == NULL) { |
| 288 |
Py_BEGIN_ALLOW_THREADS |
| 289 |
fprintf(fp, "<nil>"); |
| 290 |
Py_END_ALLOW_THREADS |
| 291 |
} |
| 292 |
else { |
| 293 |
if (op->ob_refcnt <= 0) |
| 294 |
|
| 295 |
|
| 296 |
Py_BEGIN_ALLOW_THREADS |
| 297 |
fprintf(fp, "<refcnt %ld at %p>", |
| 298 |
(long)op->ob_refcnt, op); |
| 299 |
Py_END_ALLOW_THREADS |
| 300 |
else if (Py_TYPE(op)->tp_print == NULL) { |
| 301 |
PyObject *s; |
| 302 |
if (flags & Py_PRINT_RAW) |
| 303 |
s = PyObject_Str(op); |
| 304 |
else |
| 305 |
s = PyObject_Repr(op); |
| 306 |
if (s == NULL) |
| 307 |
ret = -1; |
| 308 |
else { |
| 309 |
ret = internal_print(s, fp, Py_PRINT_RAW, |
| 310 |
nesting+1); |
| 311 |
} |
| 312 |
Py_XDECREF(s); |
| 313 |
} |
| 314 |
else |
| 315 |
ret = (*Py_TYPE(op)->tp_print)(op, fp, flags); |
| 316 |
} |
| 317 |
if (ret == 0) { |
| 318 |
if (ferror(fp)) { |
| 319 |
PyErr_SetFromErrno(PyExc_IOError); |
| 320 |
clearerr(fp); |
| 321 |
ret = -1; |
| 322 |
} |
| 323 |
} |
| 324 |
return ret; |
| 325 |
} |
| 326 |
|
| 327 |
int |
| 328 |
PyObject_Print(PyObject *op, FILE *fp, int flags) |
| 329 |
{ |
| 330 |
return internal_print(op, fp, flags, 0); |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
void _PyObject_Dump(PyObject* op) |
| 336 |
{ |
| 337 |
if (op == NULL) |
| 338 |
fprintf(stderr, "NULL\n"); |
| 339 |
else { |
| 340 |
#ifdef WITH_THREAD |
| 341 |
PyGILState_STATE gil; |
| 342 |
#endif |
| 343 |
fprintf(stderr, "object : "); |
| 344 |
#ifdef WITH_THREAD |
| 345 |
gil = PyGILState_Ensure(); |
| 346 |
#endif |
| 347 |
(void)PyObject_Print(op, stderr, 0); |
| 348 |
#ifdef WITH_THREAD |
| 349 |
PyGILState_Release(gil); |
| 350 |
#endif |
| 351 |
|
| 352 |
|
| 353 |
fprintf(stderr, "\n" |
| 354 |
"type : %s\n" |
| 355 |
"refcount: %ld\n" |
| 356 |
"address : %p\n", |
| 357 |
Py_TYPE(op)==NULL ? "NULL" : Py_TYPE(op)->tp_name, |
| 358 |
(long)op->ob_refcnt, |
| 359 |
op); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
PyObject * |
| 364 |
PyObject_Repr(PyObject *v) |
| 365 |
{ |
| 366 |
if (PyErr_CheckSignals()) |
| 367 |
return NULL; |
| 368 |
#ifdef USE_STACKCHECK |
| 369 |
if (PyOS_CheckStack()) { |
| 370 |
PyErr_SetString(PyExc_MemoryError, "stack overflow"); |
| 371 |
return NULL; |
| 372 |
} |
| 373 |
#endif |
| 374 |
if (v == NULL) |
| 375 |
return PyString_FromString("<NULL>"); |
| 376 |
else if (Py_TYPE(v)->tp_repr == NULL) |
| 377 |
return PyString_FromFormat("<%s object at %p>", |
| 378 |
Py_TYPE(v)->tp_name, v); |
| 379 |
else { |
| 380 |
PyObject *res; |
| 381 |
|
| 382 |
|
| 383 |
if (Py_EnterRecursiveCall(" while getting the repr of an object")) |
| 384 |
return NULL; |
| 385 |
res = (*Py_TYPE(v)->tp_repr)(v); |
| 386 |
Py_LeaveRecursiveCall(); |
| 387 |
if (res == NULL) |
| 388 |
return NULL; |
| 389 |
#ifdef Py_USING_UNICODE |
| 390 |
if (PyUnicode_Check(res)) { |
| 391 |
PyObject* str; |
| 392 |
str = PyUnicode_AsEncodedString(res, NULL, NULL); |
| 393 |
Py_DECREF(res); |
| 394 |
if (str) |
| 395 |
res = str; |
| 396 |
else |
| 397 |
return NULL; |
| 398 |
} |
| 399 |
#endif |
| 400 |
if (!PyString_Check(res)) { |
| 401 |
PyErr_Format(PyExc_TypeError, |
| 402 |
"__repr__ returned non-string (type %.200s)", |
| 403 |
Py_TYPE(res)->tp_name); |
| 404 |
Py_DECREF(res); |
| 405 |
return NULL; |
| 406 |
} |
| 407 |
return res; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
PyObject * |
| 412 |
_PyObject_Str(PyObject *v) |
| 413 |
{ |
| 414 |
PyObject *res; |
| 415 |
int type_ok; |
| 416 |
if (v == NULL) |
| 417 |
return PyString_FromString("<NULL>"); |
| 418 |
if (PyString_CheckExact(v)) { |
| 419 |
Py_INCREF(v); |
| 420 |
return v; |
| 421 |
} |
| 422 |
#ifdef Py_USING_UNICODE |
| 423 |
if (PyUnicode_CheckExact(v)) { |
| 424 |
Py_INCREF(v); |
| 425 |
return v; |
| 426 |
} |
| 427 |
#endif |
| 428 |
if (Py_TYPE(v)->tp_str == NULL) |
| 429 |
return PyObject_Repr(v); |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
if (Py_EnterRecursiveCall(" while getting the str of an object")) |
| 434 |
return NULL; |
| 435 |
res = (*Py_TYPE(v)->tp_str)(v); |
| 436 |
Py_LeaveRecursiveCall(); |
| 437 |
if (res == NULL) |
| 438 |
return NULL; |
| 439 |
type_ok = PyString_Check(res); |
| 440 |
#ifdef Py_USING_UNICODE |
| 441 |
type_ok = type_ok || PyUnicode_Check(res); |
| 442 |
#endif |
| 443 |
if (!type_ok) { |
| 444 |
PyErr_Format(PyExc_TypeError, |
| 445 |
"__str__ returned non-string (type %.200s)", |
| 446 |
Py_TYPE(res)->tp_name); |
| 447 |
Py_DECREF(res); |
| 448 |
return NULL; |
| 449 |
} |
| 450 |
return res; |
| 451 |
} |
| 452 |
|
| 453 |
PyObject * |
| 454 |
PyObject_Str(PyObject *v) |
| 455 |
{ |
| 456 |
PyObject *res = _PyObject_Str(v); |
| 457 |
if (res == NULL) |
| 458 |
return NULL; |
| 459 |
#ifdef Py_USING_UNICODE |
| 460 |
if (PyUnicode_Check(res)) { |
| 461 |
PyObject* str; |
| 462 |
str = PyUnicode_AsEncodedString(res, NULL, NULL); |
| 463 |
Py_DECREF(res); |
| 464 |
if (str) |
| 465 |
res = str; |
| 466 |
else |
| 467 |
return NULL; |
| 468 |
} |
| 469 |
#endif |
| 470 |
assert(PyString_Check(res)); |
| 471 |
return res; |
| 472 |
} |
| 473 |
|
| 474 |
#ifdef Py_USING_UNICODE |
| 475 |
PyObject * |
| 476 |
PyObject_Unicode(PyObject *v) |
| 477 |
{ |
| 478 |
PyObject *res; |
| 479 |
PyObject *func; |
| 480 |
PyObject *str; |
| 481 |
int unicode_method_found = 0; |
| 482 |
static PyObject *unicodestr = NULL; |
| 483 |
|
| 484 |
if (v == NULL) { |
| 485 |
res = PyString_FromString("<NULL>"); |
| 486 |
if (res == NULL) |
| 487 |
return NULL; |
| 488 |
str = PyUnicode_FromEncodedObject(res, NULL, "strict"); |
| 489 |
Py_DECREF(res); |
| 490 |
return str; |
| 491 |
} else if (PyUnicode_CheckExact(v)) { |
| 492 |
Py_INCREF(v); |
| 493 |
return v; |
| 494 |
} |
| 495 |
|
| 496 |
if (PyInstance_Check(v)) { |
| 497 |
|
| 498 |
|
| 499 |
if (!unicodestr) { |
| 500 |
unicodestr = PyString_InternFromString("__unicode__"); |
| 501 |
if (!unicodestr) |
| 502 |
return NULL; |
| 503 |
} |
| 504 |
func = PyObject_GetAttr(v, unicodestr); |
| 505 |
if (func != NULL) { |
| 506 |
unicode_method_found = 1; |
| 507 |
res = PyObject_CallFunctionObjArgs(func, NULL); |
| 508 |
Py_DECREF(func); |
| 509 |
} |
| 510 |
else { |
| 511 |
PyErr_Clear(); |
| 512 |
} |
| 513 |
} |
| 514 |
else { |
| 515 |
|
| 516 |
func = _PyObject_LookupSpecial(v, "__unicode__", &unicodestr); |
| 517 |
if (func != NULL) { |
| 518 |
unicode_method_found = 1; |
| 519 |
res = PyObject_CallFunctionObjArgs(func, NULL); |
| 520 |
Py_DECREF(func); |
| 521 |
} |
| 522 |
else if (PyErr_Occurred()) |
| 523 |
return NULL; |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
if (!unicode_method_found) { |
| 528 |
if (PyUnicode_Check(v)) { |
| 529 |
|
| 530 |
|
| 531 |
return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v), |
| 532 |
PyUnicode_GET_SIZE(v)); |
| 533 |
} |
| 534 |
if (PyString_CheckExact(v)) { |
| 535 |
Py_INCREF(v); |
| 536 |
res = v; |
| 537 |
} |
| 538 |
else { |
| 539 |
if (Py_TYPE(v)->tp_str != NULL) |
| 540 |
res = (*Py_TYPE(v)->tp_str)(v); |
| 541 |
else |
| 542 |
res = PyObject_Repr(v); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
if (res == NULL) |
| 547 |
return NULL; |
| 548 |
if (!PyUnicode_Check(res)) { |
| 549 |
str = PyUnicode_FromEncodedObject(res, NULL, "strict"); |
| 550 |
Py_DECREF(res); |
| 551 |
res = str; |
| 552 |
} |
| 553 |
return res; |
| 554 |
} |
| 555 |
#endif |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
static int |
| 566 |
adjust_tp_compare(int c) |
| 567 |
{ |
| 568 |
if (PyErr_Occurred()) { |
| 569 |
if (c != -1 && c != -2) { |
| 570 |
PyObject *t, *v, *tb; |
| 571 |
PyErr_Fetch(&t, &v, &tb); |
| 572 |
if (PyErr_Warn(PyExc_RuntimeWarning, |
| 573 |
"tp_compare didn't return -1 or -2 " |
| 574 |
"for exception") < 0) { |
| 575 |
Py_XDECREF(t); |
| 576 |
Py_XDECREF(v); |
| 577 |
Py_XDECREF(tb); |
| 578 |
} |
| 579 |
else |
| 580 |
PyErr_Restore(t, v, tb); |
| 581 |
} |
| 582 |
return -2; |
| 583 |
} |
| 584 |
else if (c < -1 || c > 1) { |
| 585 |
if (PyErr_Warn(PyExc_RuntimeWarning, |
| 586 |
"tp_compare didn't return -1, 0 or 1") < 0) |
| 587 |
return -2; |
| 588 |
else |
| 589 |
return c < -1 ? -1 : 1; |
| 590 |
} |
| 591 |
else { |
| 592 |
assert(c >= -1 && c <= 1); |
| 593 |
return c; |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \ |
| 600 |
? (t)->tp_richcompare : NULL) |
| 601 |
|
| 602 |
|
| 603 |
int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
static PyObject * |
| 613 |
try_rich_compare(PyObject *v, PyObject *w, int op) |
| 614 |
{ |
| 615 |
richcmpfunc f; |
| 616 |
PyObject *res; |
| 617 |
|
| 618 |
if (v->ob_type != w->ob_type && |
| 619 |
PyType_IsSubtype(w->ob_type, v->ob_type) && |
| 620 |
(f = RICHCOMPARE(w->ob_type)) != NULL) { |
| 621 |
res = (*f)(w, v, _Py_SwappedOp[op]); |
| 622 |
if (res != Py_NotImplemented) |
| 623 |
return res; |
| 624 |
Py_DECREF(res); |
| 625 |
} |
| 626 |
if ((f = RICHCOMPARE(v->ob_type)) != NULL) { |
| 627 |
res = (*f)(v, w, op); |
| 628 |
if (res != Py_NotImplemented) |
| 629 |
return res; |
| 630 |
Py_DECREF(res); |
| 631 |
} |
| 632 |
if ((f = RICHCOMPARE(w->ob_type)) != NULL) { |
| 633 |
return (*f)(w, v, _Py_SwappedOp[op]); |
| 634 |
} |
| 635 |
res = Py_NotImplemented; |
| 636 |
Py_INCREF(res); |
| 637 |
return res; |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
static int |
| 648 |
try_rich_compare_bool(PyObject *v, PyObject *w, int op) |
| 649 |
{ |
| 650 |
PyObject *res; |
| 651 |
int ok; |
| 652 |
|
| 653 |
if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL) |
| 654 |
return 2; |
| 655 |
res = try_rich_compare(v, w, op); |
| 656 |
if (res == NULL) |
| 657 |
return -1; |
| 658 |
if (res == Py_NotImplemented) { |
| 659 |
Py_DECREF(res); |
| 660 |
return 2; |
| 661 |
} |
| 662 |
ok = PyObject_IsTrue(res); |
| 663 |
Py_DECREF(res); |
| 664 |
return ok; |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
static int |
| 675 |
try_rich_to_3way_compare(PyObject *v, PyObject *w) |
| 676 |
{ |
| 677 |
static struct { int op; int outcome; } tries[3] = { |
| 678 |
|
| 679 |
{Py_EQ, 0}, |
| 680 |
{Py_LT, -1}, |
| 681 |
{Py_GT, 1}, |
| 682 |
}; |
| 683 |
int i; |
| 684 |
|
| 685 |
if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL) |
| 686 |
return 2; |
| 687 |
|
| 688 |
for (i = 0; i < 3; i++) { |
| 689 |
switch (try_rich_compare_bool(v, w, tries[i].op)) { |
| 690 |
case -1: |
| 691 |
return -2; |
| 692 |
case 1: |
| 693 |
return tries[i].outcome; |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
return 2; |
| 698 |
} |
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
static int |
| 708 |
try_3way_compare(PyObject *v, PyObject *w) |
| 709 |
{ |
| 710 |
int c; |
| 711 |
cmpfunc f; |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
f = v->ob_type->tp_compare; |
| 717 |
if (PyInstance_Check(v)) |
| 718 |
return (*f)(v, w); |
| 719 |
if (PyInstance_Check(w)) |
| 720 |
return (*w->ob_type->tp_compare)(v, w); |
| 721 |
|
| 722 |
|
| 723 |
if (f != NULL && f == w->ob_type->tp_compare) { |
| 724 |
c = (*f)(v, w); |
| 725 |
return adjust_tp_compare(c); |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
if (f == _PyObject_SlotCompare || |
| 730 |
w->ob_type->tp_compare == _PyObject_SlotCompare) |
| 731 |
return _PyObject_SlotCompare(v, w); |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
c = PyNumber_CoerceEx(&v, &w); |
| 743 |
if (c < 0) |
| 744 |
return -2; |
| 745 |
if (c > 0) |
| 746 |
return 2; |
| 747 |
f = v->ob_type->tp_compare; |
| 748 |
if (f != NULL && f == w->ob_type->tp_compare) { |
| 749 |
c = (*f)(v, w); |
| 750 |
Py_DECREF(v); |
| 751 |
Py_DECREF(w); |
| 752 |
return adjust_tp_compare(c); |
| 753 |
} |
| 754 |
|
| 755 |
|
| 756 |
Py_DECREF(v); |
| 757 |
Py_DECREF(w); |
| 758 |
return 2; |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
static int |
| 768 |
default_3way_compare(PyObject *v, PyObject *w) |
| 769 |
{ |
| 770 |
int c; |
| 771 |
const char *vname, *wname; |
| 772 |
|
| 773 |
if (v->ob_type == w->ob_type) { |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
Py_uintptr_t vv = (Py_uintptr_t)v; |
| 780 |
Py_uintptr_t ww = (Py_uintptr_t)w; |
| 781 |
return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; |
| 782 |
} |
| 783 |
|
| 784 |
|
| 785 |
if (v == Py_None) |
| 786 |
return -1; |
| 787 |
if (w == Py_None) |
| 788 |
return 1; |
| 789 |
|
| 790 |
|
| 791 |
if (PyNumber_Check(v)) |
| 792 |
vname = ""; |
| 793 |
else |
| 794 |
vname = v->ob_type->tp_name; |
| 795 |
if (PyNumber_Check(w)) |
| 796 |
wname = ""; |
| 797 |
else |
| 798 |
wname = w->ob_type->tp_name; |
| 799 |
c = strcmp(vname, wname); |
| 800 |
if (c < 0) |
| 801 |
return -1; |
| 802 |
if (c > 0) |
| 803 |
return 1; |
| 804 |
|
| 805 |
return ((Py_uintptr_t)(v->ob_type) < ( |
| 806 |
Py_uintptr_t)(w->ob_type)) ? -1 : 1; |
| 807 |
} |
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
static int |
| 818 |
do_cmp(PyObject *v, PyObject *w) |
| 819 |
{ |
| 820 |
int c; |
| 821 |
cmpfunc f; |
| 822 |
|
| 823 |
if (v->ob_type == w->ob_type |
| 824 |
&& (f = v->ob_type->tp_compare) != NULL) { |
| 825 |
c = (*f)(v, w); |
| 826 |
if (PyInstance_Check(v)) { |
| 827 |
|
| 828 |
|
| 829 |
if (c != 2) |
| 830 |
return c; |
| 831 |
|
| 832 |
} |
| 833 |
else |
| 834 |
return adjust_tp_compare(c); |
| 835 |
} |
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
c = try_rich_to_3way_compare(v, w); |
| 843 |
if (c < 2) |
| 844 |
return c; |
| 845 |
c = try_3way_compare(v, w); |
| 846 |
if (c < 2) |
| 847 |
return c; |
| 848 |
return default_3way_compare(v, w); |
| 849 |
} |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
int |
| 859 |
PyObject_Compare(PyObject *v, PyObject *w) |
| 860 |
{ |
| 861 |
int result; |
| 862 |
|
| 863 |
if (v == NULL || w == NULL) { |
| 864 |
PyErr_BadInternalCall(); |
| 865 |
return -1; |
| 866 |
} |
| 867 |
if (v == w) |
| 868 |
return 0; |
| 869 |
if (Py_EnterRecursiveCall(" in cmp")) |
| 870 |
return -1; |
| 871 |
result = do_cmp(v, w); |
| 872 |
Py_LeaveRecursiveCall(); |
| 873 |
return result < 0 ? -1 : result; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
static PyObject * |
| 878 |
convert_3way_to_object(int op, int c) |
| 879 |
{ |
| 880 |
PyObject *result; |
| 881 |
switch (op) { |
| 882 |
case Py_LT: c = c < 0; break; |
| 883 |
case Py_LE: c = c <= 0; break; |
| 884 |
case Py_EQ: c = c == 0; break; |
| 885 |
case Py_NE: c = c != 0; break; |
| 886 |
case Py_GT: c = c > 0; break; |
| 887 |
case Py_GE: c = c >= 0; break; |
| 888 |
} |
| 889 |
result = c ? Py_True : Py_False; |
| 890 |
Py_INCREF(result); |
| 891 |
return result; |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
static PyObject * |
| 901 |
try_3way_to_rich_compare(PyObject *v, PyObject *w, int op) |
| 902 |
{ |
| 903 |
int c; |
| 904 |
|
| 905 |
c = try_3way_compare(v, w); |
| 906 |
if (c >= 2) { |
| 907 |
|
| 908 |
|
| 909 |
if (Py_Py3kWarningFlag && |
| 910 |
v->ob_type != w->ob_type && op != Py_EQ && op != Py_NE && |
| 911 |
PyErr_WarnEx(PyExc_DeprecationWarning, |
| 912 |
"comparing unequal types not supported " |
| 913 |
"in 3.x", 1) < 0) { |
| 914 |
return NULL; |
| 915 |
} |
| 916 |
|
| 917 |
c = default_3way_compare(v, w); |
| 918 |
} |
| 919 |
if (c <= -2) |
| 920 |
return NULL; |
| 921 |
return convert_3way_to_object(op, c); |
| 922 |
} |
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
static PyObject * |
| 931 |
do_richcmp(PyObject *v, PyObject *w, int op) |
| 932 |
{ |
| 933 |
PyObject *res; |
| 934 |
|
| 935 |
res = try_rich_compare(v, w, op); |
| 936 |
if (res != Py_NotImplemented) |
| 937 |
return res; |
| 938 |
Py_DECREF(res); |
| 939 |
|
| 940 |
return try_3way_to_rich_compare(v, w, op); |
| 941 |
} |
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
PyObject * |
| 949 |
PyObject_RichCompare(PyObject *v, PyObject *w, int op) |
| 950 |
{ |
| 951 |
PyObject *res; |
| 952 |
|
| 953 |
assert(Py_LT <= op && op <= Py_GE); |
| 954 |
if (Py_EnterRecursiveCall(" in cmp")) |
| 955 |
return NULL; |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
if (v->ob_type == w->ob_type && !PyInstance_Check(v)) { |
| 960 |
cmpfunc fcmp; |
| 961 |
richcmpfunc frich = RICHCOMPARE(v->ob_type); |
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
if (frich != NULL) { |
| 966 |
res = (*frich)(v, w, op); |
| 967 |
if (res != Py_NotImplemented) |
| 968 |
goto Done; |
| 969 |
Py_DECREF(res); |
| 970 |
} |
| 971 |
|
| 972 |
|
| 973 |
fcmp = v->ob_type->tp_compare; |
| 974 |
if (fcmp != NULL) { |
| 975 |
int c = (*fcmp)(v, w); |
| 976 |
c = adjust_tp_compare(c); |
| 977 |
if (c == -2) { |
| 978 |
res = NULL; |
| 979 |
goto Done; |
| 980 |
} |
| 981 |
res = convert_3way_to_object(op, c); |
| 982 |
goto Done; |
| 983 |
} |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
res = do_richcmp(v, w, op); |
| 988 |
Done: |
| 989 |
Py_LeaveRecursiveCall(); |
| 990 |
return res; |
| 991 |
} |
| 992 |
|
| 993 |
|
| 994 |
int |
| 995 |
PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) |
| 996 |
{ |
| 997 |
PyObject *res; |
| 998 |
int ok; |
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
if (v == w) { |
| 1003 |
if (op == Py_EQ) |
| 1004 |
return 1; |
| 1005 |
else if (op == Py_NE) |
| 1006 |
return 0; |
| 1007 |
} |
| 1008 |
|
| 1009 |
res = PyObject_RichCompare(v, w, op); |
| 1010 |
if (res == NULL) |
| 1011 |
return -1; |
| 1012 |
if (PyBool_Check(res)) |
| 1013 |
ok = (res == Py_True); |
| 1014 |
else |
| 1015 |
ok = PyObject_IsTrue(res); |
| 1016 |
Py_DECREF(res); |
| 1017 |
return ok; |
| 1018 |
} |
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
long |
| 1027 |
_Py_HashDouble(double v) |
| 1028 |
{ |
| 1029 |
double intpart, fractpart; |
| 1030 |
int expo; |
| 1031 |
long hipart; |
| 1032 |
long x; |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
if (!Py_IS_FINITE(v)) { |
| 1039 |
if (Py_IS_INFINITY(v)) |
| 1040 |
return v < 0 ? -271828 : 314159; |
| 1041 |
else |
| 1042 |
return 0; |
| 1043 |
} |
| 1044 |
fractpart = modf(v, &intpart); |
| 1045 |
if (fractpart == 0.0) { |
| 1046 |
|
| 1047 |
if (intpart > LONG_MAX/2 || -intpart > LONG_MAX/2) { |
| 1048 |
|
| 1049 |
PyObject *plong; |
| 1050 |
plong = PyLong_FromDouble(v); |
| 1051 |
if (plong == NULL) |
| 1052 |
return -1; |
| 1053 |
x = PyObject_Hash(plong); |
| 1054 |
Py_DECREF(plong); |
| 1055 |
return x; |
| 1056 |
} |
| 1057 |
|
| 1058 |
x = (long)intpart; |
| 1059 |
if (x == -1) |
| 1060 |
x = -2; |
| 1061 |
return x; |
| 1062 |
} |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
v = frexp(v, &expo); |
| 1076 |
v *= 2147483648.0; |
| 1077 |
hipart = (long)v; |
| 1078 |
v = (v - (double)hipart) * 2147483648.0; |
| 1079 |
x = hipart + (long)v + (expo << 15); |
| 1080 |
if (x == -1) |
| 1081 |
x = -2; |
| 1082 |
return x; |
| 1083 |
} |
| 1084 |
|
| 1085 |
long |
| 1086 |
_Py_HashPointer(void *p) |
| 1087 |
{ |
| 1088 |
long x; |
| 1089 |
size_t y = (size_t)p; |
| 1090 |
|
| 1091 |
|
| 1092 |
y = (y >> 4) | (y << (8 * SIZEOF_VOID_P - 4)); |
| 1093 |
x = (long)y; |
| 1094 |
if (x == -1) |
| 1095 |
x = -2; |
| 1096 |
return x; |
| 1097 |
} |
| 1098 |
|
| 1099 |
long |
| 1100 |
PyObject_HashNotImplemented(PyObject *self) |
| 1101 |
{ |
| 1102 |
PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", |
| 1103 |
self->ob_type->tp_name); |
| 1104 |
return -1; |
| 1105 |
} |
| 1106 |
|
| 1107 |
_Py_HashSecret_t _Py_HashSecret; |
| 1108 |
|
| 1109 |
long |
| 1110 |
PyObject_Hash(PyObject *v) |
| 1111 |
{ |
| 1112 |
PyTypeObject *tp = v->ob_type; |
| 1113 |
if (tp->tp_hash != NULL) |
| 1114 |
return (*tp->tp_hash)(v); |
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
if (tp->tp_dict == NULL) { |
| 1121 |
if (PyType_Ready(tp) < 0) |
| 1122 |
return -1; |
| 1123 |
if (tp->tp_hash != NULL) |
| 1124 |
return (*tp->tp_hash)(v); |
| 1125 |
} |
| 1126 |
if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) { |
| 1127 |
return _Py_HashPointer(v); |
| 1128 |
} |
| 1129 |
|
| 1130 |
return PyObject_HashNotImplemented(v); |
| 1131 |
} |
| 1132 |
|
| 1133 |
PyObject * |
| 1134 |
PyObject_GetAttrString(PyObject *v, const char *name) |
| 1135 |
{ |
| 1136 |
PyObject *w, *res; |
| 1137 |
|
| 1138 |
if (Py_TYPE(v)->tp_getattr != NULL) |
| 1139 |
return (*Py_TYPE(v)->tp_getattr)(v, (char*)name); |
| 1140 |
w = PyString_InternFromString(name); |
| 1141 |
if (w == NULL) |
| 1142 |
return NULL; |
| 1143 |
res = PyObject_GetAttr(v, w); |
| 1144 |
Py_XDECREF(w); |
| 1145 |
return res; |
| 1146 |
} |
| 1147 |
|
| 1148 |
int |
| 1149 |
PyObject_HasAttrString(PyObject *v, const char *name) |
| 1150 |
{ |
| 1151 |
PyObject *res = PyObject_GetAttrString(v, name); |
| 1152 |
if (res != NULL) { |
| 1153 |
Py_DECREF(res); |
| 1154 |
return 1; |
| 1155 |
} |
| 1156 |
PyErr_Clear(); |
| 1157 |
return 0; |
| 1158 |
} |
| 1159 |
|
| 1160 |
int |
| 1161 |
PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w) |
| 1162 |
{ |
| 1163 |
PyObject *s; |
| 1164 |
int res; |
| 1165 |
|
| 1166 |
if (Py_TYPE(v)->tp_setattr != NULL) |
| 1167 |
return (*Py_TYPE(v)->tp_setattr)(v, (char*)name, w); |
| 1168 |
s = PyString_InternFromString(name); |
| 1169 |
if (s == NULL) |
| 1170 |
return -1; |
| 1171 |
res = PyObject_SetAttr(v, s, w); |
| 1172 |
Py_XDECREF(s); |
| 1173 |
return res; |
| 1174 |
} |
| 1175 |
|
| 1176 |
PyObject * |
| 1177 |
PyObject_GetAttr(PyObject *v, PyObject *name) |
| 1178 |
{ |
| 1179 |
PyTypeObject *tp = Py_TYPE(v); |
| 1180 |
|
| 1181 |
if (!PyString_Check(name)) { |
| 1182 |
#ifdef Py_USING_UNICODE |
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
if (PyUnicode_Check(name)) { |
| 1187 |
name = _PyUnicode_AsDefaultEncodedString(name, NULL); |
| 1188 |
if (name == NULL) |
| 1189 |
return NULL; |
| 1190 |
} |
| 1191 |
else |
| 1192 |
#endif |
| 1193 |
{ |
| 1194 |
PyErr_Format(PyExc_TypeError, |
| 1195 |
"attribute name must be string, not '%.200s'", |
| 1196 |
Py_TYPE(name)->tp_name); |
| 1197 |
return NULL; |
| 1198 |
} |
| 1199 |
} |
| 1200 |
if (tp->tp_getattro != NULL) |
| 1201 |
return (*tp->tp_getattro)(v, name); |
| 1202 |
if (tp->tp_getattr != NULL) |
| 1203 |
return (*tp->tp_getattr)(v, PyString_AS_STRING(name)); |
| 1204 |
PyErr_Format(PyExc_AttributeError, |
| 1205 |
"'%.50s' object has no attribute '%.400s'", |
| 1206 |
tp->tp_name, PyString_AS_STRING(name)); |
| 1207 |
return NULL; |
| 1208 |
} |
| 1209 |
|
| 1210 |
int |
| 1211 |
PyObject_HasAttr(PyObject *v, PyObject *name) |
| 1212 |
{ |
| 1213 |
PyObject *res = PyObject_GetAttr(v, name); |
| 1214 |
if (res != NULL) { |
| 1215 |
Py_DECREF(res); |
| 1216 |
return 1; |
| 1217 |
} |
| 1218 |
PyErr_Clear(); |
| 1219 |
return 0; |
| 1220 |
} |
| 1221 |
|
| 1222 |
int |
| 1223 |
PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) |
| 1224 |
{ |
| 1225 |
PyTypeObject *tp = Py_TYPE(v); |
| 1226 |
int err; |
| 1227 |
|
| 1228 |
if (!PyString_Check(name)){ |
| 1229 |
#ifdef Py_USING_UNICODE |
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
if (PyUnicode_Check(name)) { |
| 1234 |
name = PyUnicode_AsEncodedString(name, NULL, NULL); |
| 1235 |
if (name == NULL) |
| 1236 |
return -1; |
| 1237 |
} |
| 1238 |
else |
| 1239 |
#endif |
| 1240 |
{ |
| 1241 |
PyErr_Format(PyExc_TypeError, |
| 1242 |
"attribute name must be string, not '%.200s'", |
| 1243 |
Py_TYPE(name)->tp_name); |
| 1244 |
return -1; |
| 1245 |
} |
| 1246 |
} |
| 1247 |
else |
| 1248 |
Py_INCREF(name); |
| 1249 |
|
| 1250 |
PyString_InternInPlace(&name); |
| 1251 |
if (tp->tp_setattro != NULL) { |
| 1252 |
err = (*tp->tp_setattro)(v, name, value); |
| 1253 |
Py_DECREF(name); |
| 1254 |
return err; |
| 1255 |
} |
| 1256 |
if (tp->tp_setattr != NULL) { |
| 1257 |
err = (*tp->tp_setattr)(v, PyString_AS_STRING(name), value); |
| 1258 |
Py_DECREF(name); |
| 1259 |
return err; |
| 1260 |
} |
| 1261 |
Py_DECREF(name); |
| 1262 |
if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) |
| 1263 |
PyErr_Format(PyExc_TypeError, |
| 1264 |
"'%.100s' object has no attributes " |
| 1265 |
"(%s .%.100s)", |
| 1266 |
tp->tp_name, |
| 1267 |
value==NULL ? "del" : "assign to", |
| 1268 |
PyString_AS_STRING(name)); |
| 1269 |
else |
| 1270 |
PyErr_Format(PyExc_TypeError, |
| 1271 |
"'%.100s' object has only read-only attributes " |
| 1272 |
"(%s .%.100s)", |
| 1273 |
tp->tp_name, |
| 1274 |
value==NULL ? "del" : "assign to", |
| 1275 |
PyString_AS_STRING(name)); |
| 1276 |
return -1; |
| 1277 |
} |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
PyObject ** |
| 1282 |
_PyObject_GetDictPtr(PyObject *obj) |
| 1283 |
{ |
| 1284 |
Py_ssize_t dictoffset; |
| 1285 |
PyTypeObject *tp = Py_TYPE(obj); |
| 1286 |
|
| 1287 |
if (!(tp->tp_flags & Py_TPFLAGS_HAVE_CLASS)) |
| 1288 |
return NULL; |
| 1289 |
dictoffset = tp->tp_dictoffset; |
| 1290 |
if (dictoffset == 0) |
| 1291 |
return NULL; |
| 1292 |
if (dictoffset < 0) { |
| 1293 |
Py_ssize_t tsize; |
| 1294 |
size_t size; |
| 1295 |
|
| 1296 |
tsize = ((PyVarObject *)obj)->ob_size; |
| 1297 |
if (tsize < 0) |
| 1298 |
tsize = -tsize; |
| 1299 |
size = _PyObject_VAR_SIZE(tp, tsize); |
| 1300 |
|
| 1301 |
dictoffset += (long)size; |
| 1302 |
assert(dictoffset > 0); |
| 1303 |
assert(dictoffset % SIZEOF_VOID_P == 0); |
| 1304 |
} |
| 1305 |
return (PyObject **) ((char *)obj + dictoffset); |
| 1306 |
} |
| 1307 |
|
| 1308 |
PyObject * |
| 1309 |
PyObject_SelfIter(PyObject *obj) |
| 1310 |
{ |
| 1311 |
Py_INCREF(obj); |
| 1312 |
return obj; |
| 1313 |
} |
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
PyObject * |
| 1321 |
_PyObject_NextNotImplemented(PyObject *self) |
| 1322 |
{ |
| 1323 |
PyErr_Format(PyExc_TypeError, |
| 1324 |
"'%.200s' object is not iterable", |
| 1325 |
Py_TYPE(self)->tp_name); |
| 1326 |
return NULL; |
| 1327 |
} |
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
PyObject * |
| 1332 |
_PyObject_GenericGetAttrWithDict(PyObject *obj, PyObject *name, PyObject *dict) |
| 1333 |
{ |
| 1334 |
PyTypeObject *tp = Py_TYPE(obj); |
| 1335 |
PyObject *descr = NULL; |
| 1336 |
PyObject *res = NULL; |
| 1337 |
descrgetfunc f; |
| 1338 |
Py_ssize_t dictoffset; |
| 1339 |
PyObject **dictptr; |
| 1340 |
|
| 1341 |
if (!PyString_Check(name)){ |
| 1342 |
#ifdef Py_USING_UNICODE |
| 1343 |
|
| 1344 |
|
| 1345 |
|
| 1346 |
if (PyUnicode_Check(name)) { |
| 1347 |
name = PyUnicode_AsEncodedString(name, NULL, NULL); |
| 1348 |
if (name == NULL) |
| 1349 |
return NULL; |
| 1350 |
} |
| 1351 |
else |
| 1352 |
#endif |
| 1353 |
{ |
| 1354 |
PyErr_Format(PyExc_TypeError, |
| 1355 |
"attribute name must be string, not '%.200s'", |
| 1356 |
Py_TYPE(name)->tp_name); |
| 1357 |
return NULL; |
| 1358 |
} |
| 1359 |
} |
| 1360 |
else |
| 1361 |
Py_INCREF(name); |
| 1362 |
|
| 1363 |
if (tp->tp_dict == NULL) { |
| 1364 |
if (PyType_Ready(tp) < 0) |
| 1365 |
goto done; |
| 1366 |
} |
| 1367 |
|
| 1368 |
#if 0 |
| 1369 |
|
| 1370 |
{ |
| 1371 |
Py_ssize_t i, n; |
| 1372 |
PyObject *mro, *base, *dict; |
| 1373 |
|
| 1374 |
|
| 1375 |
mro = tp->tp_mro; |
| 1376 |
assert(mro != NULL); |
| 1377 |
assert(PyTuple_Check(mro)); |
| 1378 |
n = PyTuple_GET_SIZE(mro); |
| 1379 |
for (i = 0; i < n; i++) { |
| 1380 |
base = PyTuple_GET_ITEM(mro, i); |
| 1381 |
if (PyClass_Check(base)) |
| 1382 |
dict = ((PyClassObject *)base)->cl_dict; |
| 1383 |
else { |
| 1384 |
assert(PyType_Check(base)); |
| 1385 |
dict = ((PyTypeObject *)base)->tp_dict; |
| 1386 |
} |
| 1387 |
assert(dict && PyDict_Check(dict)); |
| 1388 |
descr = PyDict_GetItem(dict, name); |
| 1389 |
if (descr != NULL) |
| 1390 |
break; |
| 1391 |
} |
| 1392 |
} |
| 1393 |
#else |
| 1394 |
descr = _PyType_Lookup(tp, name); |
| 1395 |
#endif |
| 1396 |
|
| 1397 |
Py_XINCREF(descr); |
| 1398 |
|
| 1399 |
f = NULL; |
| 1400 |
if (descr != NULL && |
| 1401 |
PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) { |
| 1402 |
f = descr->ob_type->tp_descr_get; |
| 1403 |
if (f != NULL && PyDescr_IsData(descr)) { |
| 1404 |
res = f(descr, obj, (PyObject *)obj->ob_type); |
| 1405 |
Py_DECREF(descr); |
| 1406 |
goto done; |
| 1407 |
} |
| 1408 |
} |
| 1409 |
|
| 1410 |
if (dict == NULL) { |
| 1411 |
|
| 1412 |
dictoffset = tp->tp_dictoffset; |
| 1413 |
if (dictoffset != 0) { |
| 1414 |
if (dictoffset < 0) { |
| 1415 |
Py_ssize_t tsize; |
| 1416 |
size_t size; |
| 1417 |
|
| 1418 |
tsize = ((PyVarObject *)obj)->ob_size; |
| 1419 |
if (tsize < 0) |
| 1420 |
tsize = -tsize; |
| 1421 |
size = _PyObject_VAR_SIZE(tp, tsize); |
| 1422 |
|
| 1423 |
dictoffset += (long)size; |
| 1424 |
assert(dictoffset > 0); |
| 1425 |
assert(dictoffset % SIZEOF_VOID_P == 0); |
| 1426 |
} |
| 1427 |
dictptr = (PyObject **) ((char *)obj + dictoffset); |
| 1428 |
dict = *dictptr; |
| 1429 |
} |
| 1430 |
} |
| 1431 |
if (dict != NULL) { |
| 1432 |
Py_INCREF(dict); |
| 1433 |
res = PyDict_GetItem(dict, name); |
| 1434 |
if (res != NULL) { |
| 1435 |
Py_INCREF(res); |
| 1436 |
Py_XDECREF(descr); |
| 1437 |
Py_DECREF(dict); |
| 1438 |
goto done; |
| 1439 |
} |
| 1440 |
Py_DECREF(dict); |
| 1441 |
} |
| 1442 |
|
| 1443 |
if (f != NULL) { |
| 1444 |
res = f(descr, obj, (PyObject *)Py_TYPE(obj)); |
| 1445 |
Py_DECREF(descr); |
| 1446 |
goto done; |
| 1447 |
} |
| 1448 |
|
| 1449 |
if (descr != NULL) { |
| 1450 |
res = descr; |
| 1451 |
|
| 1452 |
goto done; |
| 1453 |
} |
| 1454 |
|
| 1455 |
PyErr_Format(PyExc_AttributeError, |
| 1456 |
"'%.50s' object has no attribute '%.400s'", |
| 1457 |
tp->tp_name, PyString_AS_STRING(name)); |
| 1458 |
done: |
| 1459 |
Py_DECREF(name); |
| 1460 |
return res; |
| 1461 |
} |
| 1462 |
|
| 1463 |
PyObject * |
| 1464 |
PyObject_GenericGetAttr(PyObject *obj, PyObject *name) |
| 1465 |
{ |
| 1466 |
return _PyObject_GenericGetAttrWithDict(obj, name, NULL); |
| 1467 |
} |
| 1468 |
|
| 1469 |
int |
| 1470 |
_PyObject_GenericSetAttrWithDict(PyObject *obj, PyObject *name, |
| 1471 |
PyObject *value, PyObject *dict) |
| 1472 |
{ |
| 1473 |
PyTypeObject *tp = Py_TYPE(obj); |
| 1474 |
PyObject *descr; |
| 1475 |
descrsetfunc f; |
| 1476 |
PyObject **dictptr; |
| 1477 |
int res = -1; |
| 1478 |
|
| 1479 |
if (!PyString_Check(name)){ |
| 1480 |
#ifdef Py_USING_UNICODE |
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
if (PyUnicode_Check(name)) { |
| 1485 |
name = PyUnicode_AsEncodedString(name, NULL, NULL); |
| 1486 |
if (name == NULL) |
| 1487 |
return -1; |
| 1488 |
} |
| 1489 |
else |
| 1490 |
#endif |
| 1491 |
{ |
| 1492 |
PyErr_Format(PyExc_TypeError, |
| 1493 |
"attribute name must be string, not '%.200s'", |
| 1494 |
Py_TYPE(name)->tp_name); |
| 1495 |
return -1; |
| 1496 |
} |
| 1497 |
} |
| 1498 |
else |
| 1499 |
Py_INCREF(name); |
| 1500 |
|
| 1501 |
if (tp->tp_dict == NULL) { |
| 1502 |
if (PyType_Ready(tp) < 0) |
| 1503 |
goto done; |
| 1504 |
} |
| 1505 |
|
| 1506 |
descr = _PyType_Lookup(tp, name); |
| 1507 |
f = NULL; |
| 1508 |
if (descr != NULL && |
| 1509 |
PyType_HasFeature(descr->ob_type, Py_TPFLAGS_HAVE_CLASS)) { |
| 1510 |
f = descr->ob_type->tp_descr_set; |
| 1511 |
if (f != NULL && PyDescr_IsData(descr)) { |
| 1512 |
res = f(descr, obj, value); |
| 1513 |
goto done; |
| 1514 |
} |
| 1515 |
} |
| 1516 |
|
| 1517 |
if (dict == NULL) { |
| 1518 |
dictptr = _PyObject_GetDictPtr(obj); |
| 1519 |
if (dictptr != NULL) { |
| 1520 |
dict = *dictptr; |
| 1521 |
if (dict == NULL && value != NULL) { |
| 1522 |
dict = PyDict_New(); |
| 1523 |
if (dict == NULL) |
| 1524 |
goto done; |
| 1525 |
*dictptr = dict; |
| 1526 |
} |
| 1527 |
} |
| 1528 |
} |
| 1529 |
if (dict != NULL) { |
| 1530 |
Py_INCREF(dict); |
| 1531 |
if (value == NULL) |
| 1532 |
res = PyDict_DelItem(dict, name); |
| 1533 |
else |
| 1534 |
res = PyDict_SetItem(dict, name, value); |
| 1535 |
if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) |
| 1536 |
PyErr_SetObject(PyExc_AttributeError, name); |
| 1537 |
Py_DECREF(dict); |
| 1538 |
goto done; |
| 1539 |
} |
| 1540 |
|
| 1541 |
if (f != NULL) { |
| 1542 |
res = f(descr, obj, value); |
| 1543 |
goto done; |
| 1544 |
} |
| 1545 |
|
| 1546 |
if (descr == NULL) { |
| 1547 |
PyErr_Format(PyExc_AttributeError, |
| 1548 |
"'%.100s' object has no attribute '%.200s'", |
| 1549 |
tp->tp_name, PyString_AS_STRING(name)); |
| 1550 |
goto done; |
| 1551 |
} |
| 1552 |
|
| 1553 |
PyErr_Format(PyExc_AttributeError, |
| 1554 |
"'%.50s' object attribute '%.400s' is read-only", |
| 1555 |
tp->tp_name, PyString_AS_STRING(name)); |
| 1556 |
done: |
| 1557 |
Py_DECREF(name); |
| 1558 |
return res; |
| 1559 |
} |
| 1560 |
|
| 1561 |
int |
| 1562 |
PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) |
| 1563 |
{ |
| 1564 |
return _PyObject_GenericSetAttrWithDict(obj, name, value, NULL); |
| 1565 |
} |
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
int |
| 1572 |
PyObject_IsTrue(PyObject *v) |
| 1573 |
{ |
| 1574 |
Py_ssize_t res; |
| 1575 |
if (v == Py_True) |
| 1576 |
return 1; |
| 1577 |
if (v == Py_False) |
| 1578 |
return 0; |
| 1579 |
if (v == Py_None) |
| 1580 |
return 0; |
| 1581 |
else if (v->ob_type->tp_as_number != NULL && |
| 1582 |
v->ob_type->tp_as_number->nb_nonzero != NULL) |
| 1583 |
res = (*v->ob_type->tp_as_number->nb_nonzero)(v); |
| 1584 |
else if (v->ob_type->tp_as_mapping != NULL && |
| 1585 |
v->ob_type->tp_as_mapping->mp_length != NULL) |
| 1586 |
res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
| 1587 |
else if (v->ob_type->tp_as_sequence != NULL && |
| 1588 |
v->ob_type->tp_as_sequence->sq_length != NULL) |
| 1589 |
res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
| 1590 |
else |
| 1591 |
return 1; |
| 1592 |
|
| 1593 |
return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int); |
| 1594 |
} |
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
int |
| 1600 |
PyObject_Not(PyObject *v) |
| 1601 |
{ |
| 1602 |
int res; |
| 1603 |
res = PyObject_IsTrue(v); |
| 1604 |
if (res < 0) |
| 1605 |
return res; |
| 1606 |
return res == 0; |
| 1607 |
} |
| 1608 |
|
| 1609 |
|
| 1610 |
|
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
int |
| 1617 |
PyNumber_CoerceEx(PyObject **pv, PyObject **pw) |
| 1618 |
{ |
| 1619 |
register PyObject *v = *pv; |
| 1620 |
register PyObject *w = *pw; |
| 1621 |
int res; |
| 1622 |
|
| 1623 |
|
| 1624 |
if (v->ob_type == w->ob_type && |
| 1625 |
!PyType_HasFeature(v->ob_type, Py_TPFLAGS_CHECKTYPES)) |
| 1626 |
{ |
| 1627 |
Py_INCREF(v); |
| 1628 |
Py_INCREF(w); |
| 1629 |
return 0; |
| 1630 |
} |
| 1631 |
if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { |
| 1632 |
res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw); |
| 1633 |
if (res <= 0) |
| 1634 |
return res; |
| 1635 |
} |
| 1636 |
if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) { |
| 1637 |
res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv); |
| 1638 |
if (res <= 0) |
| 1639 |
return res; |
| 1640 |
} |
| 1641 |
return 1; |
| 1642 |
} |
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
int |
| 1650 |
PyNumber_Coerce(PyObject **pv, PyObject **pw) |
| 1651 |
{ |
| 1652 |
int err = PyNumber_CoerceEx(pv, pw); |
| 1653 |
if (err <= 0) |
| 1654 |
return err; |
| 1655 |
PyErr_SetString(PyExc_TypeError, "number coercion failed"); |
| 1656 |
return -1; |
| 1657 |
} |
| 1658 |
|
| 1659 |
|
| 1660 |
|
| 1661 |
|
| 1662 |
int |
| 1663 |
PyCallable_Check(PyObject *x) |
| 1664 |
{ |
| 1665 |
if (x == NULL) |
| 1666 |
return 0; |
| 1667 |
if (PyInstance_Check(x)) { |
| 1668 |
PyObject *call = PyObject_GetAttrString(x, "__call__"); |
| 1669 |
if (call == NULL) { |
| 1670 |
PyErr_Clear(); |
| 1671 |
return 0; |
| 1672 |
} |
| 1673 |
|
| 1674 |
|
| 1675 |
Py_DECREF(call); |
| 1676 |
return 1; |
| 1677 |
} |
| 1678 |
else { |
| 1679 |
return x->ob_type->tp_call != NULL; |
| 1680 |
} |
| 1681 |
} |
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
|
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
static int |
| 1694 |
merge_class_dict(PyObject* dict, PyObject* aclass) |
| 1695 |
{ |
| 1696 |
PyObject *classdict; |
| 1697 |
PyObject *bases; |
| 1698 |
|
| 1699 |
assert(PyDict_Check(dict)); |
| 1700 |
assert(aclass); |
| 1701 |
|
| 1702 |
|
| 1703 |
classdict = PyObject_GetAttrString(aclass, "__dict__"); |
| 1704 |
if (classdict == NULL) |
| 1705 |
PyErr_Clear(); |
| 1706 |
else { |
| 1707 |
int status = PyDict_Update(dict, classdict); |
| 1708 |
Py_DECREF(classdict); |
| 1709 |
if (status < 0) |
| 1710 |
return -1; |
| 1711 |
} |
| 1712 |
|
| 1713 |
|
| 1714 |
bases = PyObject_GetAttrString(aclass, "__bases__"); |
| 1715 |
if (bases == NULL) |
| 1716 |
PyErr_Clear(); |
| 1717 |
else { |
| 1718 |
|
| 1719 |
Py_ssize_t i, n; |
| 1720 |
n = PySequence_Size(bases); |
| 1721 |
if (n < 0) |
| 1722 |
PyErr_Clear(); |
| 1723 |
else { |
| 1724 |
for (i = 0; i < n; i++) { |
| 1725 |
int status; |
| 1726 |
PyObject *base = PySequence_GetItem(bases, i); |
| 1727 |
if (base == NULL) { |
| 1728 |
Py_DECREF(bases); |
| 1729 |
return -1; |
| 1730 |
} |
| 1731 |
status = merge_class_dict(dict, base); |
| 1732 |
Py_DECREF(base); |
| 1733 |
if (status < 0) { |
| 1734 |
Py_DECREF(bases); |
| 1735 |
return -1; |
| 1736 |
} |
| 1737 |
} |
| 1738 |
} |
| 1739 |
Py_DECREF(bases); |
| 1740 |
} |
| 1741 |
return 0; |
| 1742 |
} |
| 1743 |
|
| 1744 |
|
| 1745 |
|
| 1746 |
|
| 1747 |
|
| 1748 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
static int |
| 1752 |
merge_list_attr(PyObject* dict, PyObject* obj, const char *attrname) |
| 1753 |
{ |
| 1754 |
PyObject *list; |
| 1755 |
int result = 0; |
| 1756 |
|
| 1757 |
assert(PyDict_Check(dict)); |
| 1758 |
assert(obj); |
| 1759 |
assert(attrname); |
| 1760 |
|
| 1761 |
list = PyObject_GetAttrString(obj, attrname); |
| 1762 |
if (list == NULL) |
| 1763 |
PyErr_Clear(); |
| 1764 |
|
| 1765 |
else if (PyList_Check(list)) { |
| 1766 |
int i; |
| 1767 |
for (i = 0; i < PyList_GET_SIZE(list); ++i) { |
| 1768 |
PyObject *item = PyList_GET_ITEM(list, i); |
| 1769 |
if (PyString_Check(item)) { |
| 1770 |
result = PyDict_SetItem(dict, item, Py_None); |
| 1771 |
if (result < 0) |
| 1772 |
break; |
| 1773 |
} |
| 1774 |
} |
| 1775 |
if (Py_Py3kWarningFlag && |
| 1776 |
(strcmp(attrname, "__members__") == 0 || |
| 1777 |
strcmp(attrname, "__methods__") == 0)) { |
| 1778 |
if (PyErr_WarnEx(PyExc_DeprecationWarning, |
| 1779 |
"__members__ and __methods__ not " |
| 1780 |
"supported in 3.x", 1) < 0) { |
| 1781 |
Py_XDECREF(list); |
| 1782 |
return -1; |
| 1783 |
} |
| 1784 |
} |
| 1785 |
} |
| 1786 |
|
| 1787 |
Py_XDECREF(list); |
| 1788 |
return result; |
| 1789 |
} |
| 1790 |
|
| 1791 |
|
| 1792 |
static PyObject * |
| 1793 |
_dir_locals(void) |
| 1794 |
{ |
| 1795 |
PyObject *names; |
| 1796 |
PyObject *locals = PyEval_GetLocals(); |
| 1797 |
|
| 1798 |
if (locals == NULL) { |
| 1799 |
PyErr_SetString(PyExc_SystemError, "frame does not exist"); |
| 1800 |
return NULL; |
| 1801 |
} |
| 1802 |
|
| 1803 |
names = PyMapping_Keys(locals); |
| 1804 |
if (!names) |
| 1805 |
return NULL; |
| 1806 |
if (!PyList_Check(names)) { |
| 1807 |
PyErr_Format(PyExc_TypeError, |
| 1808 |
"dir(): expected keys() of locals to be a list, " |
| 1809 |
"not '%.200s'", Py_TYPE(names)->tp_name); |
| 1810 |
Py_DECREF(names); |
| 1811 |
return NULL; |
| 1812 |
} |
| 1813 |
|
| 1814 |
return names; |
| 1815 |
} |
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
static PyObject * |
| 1822 |
_specialized_dir_type(PyObject *obj) |
| 1823 |
{ |
| 1824 |
PyObject *result = NULL; |
| 1825 |
PyObject *dict = PyDict_New(); |
| 1826 |
|
| 1827 |
if (dict != NULL && merge_class_dict(dict, obj) == 0) |
| 1828 |
result = PyDict_Keys(dict); |
| 1829 |
|
| 1830 |
Py_XDECREF(dict); |
| 1831 |
return result; |
| 1832 |
} |
| 1833 |
|
| 1834 |
|
| 1835 |
static PyObject * |
| 1836 |
_specialized_dir_module(PyObject *obj) |
| 1837 |
{ |
| 1838 |
PyObject *result = NULL; |
| 1839 |
PyObject *dict = PyObject_GetAttrString(obj, "__dict__"); |
| 1840 |
|
| 1841 |
if (dict != NULL) { |
| 1842 |
if (PyDict_Check(dict)) |
| 1843 |
result = PyDict_Keys(dict); |
| 1844 |
else { |
| 1845 |
char *name = PyModule_GetName(obj); |
| 1846 |
if (name) |
| 1847 |
PyErr_Format(PyExc_TypeError, |
| 1848 |
"%.200s.__dict__ is not a dictionary", |
| 1849 |
name); |
| 1850 |
} |
| 1851 |
} |
| 1852 |
|
| 1853 |
Py_XDECREF(dict); |
| 1854 |
return result; |
| 1855 |
} |
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
static PyObject * |
| 1861 |
_generic_dir(PyObject *obj) |
| 1862 |
{ |
| 1863 |
PyObject *result = NULL; |
| 1864 |
PyObject *dict = NULL; |
| 1865 |
PyObject *itsclass = NULL; |
| 1866 |
|
| 1867 |
|
| 1868 |
dict = PyObject_GetAttrString(obj, "__dict__"); |
| 1869 |
if (dict == NULL) { |
| 1870 |
PyErr_Clear(); |
| 1871 |
dict = PyDict_New(); |
| 1872 |
} |
| 1873 |
else if (!PyDict_Check(dict)) { |
| 1874 |
Py_DECREF(dict); |
| 1875 |
dict = PyDict_New(); |
| 1876 |
} |
| 1877 |
else { |
| 1878 |
|
| 1879 |
PyObject *temp = PyDict_Copy(dict); |
| 1880 |
Py_DECREF(dict); |
| 1881 |
dict = temp; |
| 1882 |
} |
| 1883 |
|
| 1884 |
if (dict == NULL) |
| 1885 |
goto error; |
| 1886 |
|
| 1887 |
|
| 1888 |
|
| 1889 |
if (merge_list_attr(dict, obj, "__members__") < 0) |
| 1890 |
goto error; |
| 1891 |
if (merge_list_attr(dict, obj, "__methods__") < 0) |
| 1892 |
goto error; |
| 1893 |
|
| 1894 |
|
| 1895 |
itsclass = PyObject_GetAttrString(obj, "__class__"); |
| 1896 |
if (itsclass == NULL) |
| 1897 |
|
| 1898 |
|
| 1899 |
PyErr_Clear(); |
| 1900 |
else { |
| 1901 |
if (merge_class_dict(dict, itsclass) != 0) |
| 1902 |
goto error; |
| 1903 |
} |
| 1904 |
|
| 1905 |
result = PyDict_Keys(dict); |
| 1906 |
|
| 1907 |
error: |
| 1908 |
Py_XDECREF(itsclass); |
| 1909 |
Py_XDECREF(dict); |
| 1910 |
return result; |
| 1911 |
} |
| 1912 |
|
| 1913 |
|
| 1914 |
|
| 1915 |
|
| 1916 |
static PyObject * |
| 1917 |
_dir_object(PyObject *obj) |
| 1918 |
{ |
| 1919 |
PyObject *result = NULL; |
| 1920 |
static PyObject *dir_str = NULL; |
| 1921 |
PyObject *dirfunc; |
| 1922 |
|
| 1923 |
assert(obj); |
| 1924 |
if (PyInstance_Check(obj)) { |
| 1925 |
dirfunc = PyObject_GetAttrString(obj, "__dir__"); |
| 1926 |
if (dirfunc == NULL) { |
| 1927 |
if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1928 |
PyErr_Clear(); |
| 1929 |
else |
| 1930 |
return NULL; |
| 1931 |
} |
| 1932 |
} |
| 1933 |
else { |
| 1934 |
dirfunc = _PyObject_LookupSpecial(obj, "__dir__", &dir_str); |
| 1935 |
if (PyErr_Occurred()) |
| 1936 |
return NULL; |
| 1937 |
} |
| 1938 |
if (dirfunc == NULL) { |
| 1939 |
|
| 1940 |
if (PyModule_Check(obj)) |
| 1941 |
result = _specialized_dir_module(obj); |
| 1942 |
else if (PyType_Check(obj) || PyClass_Check(obj)) |
| 1943 |
result = _specialized_dir_type(obj); |
| 1944 |
else |
| 1945 |
result = _generic_dir(obj); |
| 1946 |
} |
| 1947 |
else { |
| 1948 |
|
| 1949 |
result = PyObject_CallFunctionObjArgs(dirfunc, NULL); |
| 1950 |
Py_DECREF(dirfunc); |
| 1951 |
if (result == NULL) |
| 1952 |
return NULL; |
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
if (!PyList_Check(result)) { |
| 1957 |
PyErr_Format(PyExc_TypeError, |
| 1958 |
"__dir__() must return a list, not %.200s", |
| 1959 |
Py_TYPE(result)->tp_name); |
| 1960 |
Py_DECREF(result); |
| 1961 |
result = NULL; |
| 1962 |
} |
| 1963 |
} |
| 1964 |
|
| 1965 |
return result; |
| 1966 |
} |
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
PyObject * |
| 1973 |
PyObject_Dir(PyObject *obj) |
| 1974 |
{ |
| 1975 |
PyObject * result; |
| 1976 |
|
| 1977 |
if (obj == NULL) |
| 1978 |
|
| 1979 |
result = _dir_locals(); |
| 1980 |
else |
| 1981 |
|
| 1982 |
result = _dir_object(obj); |
| 1983 |
|
| 1984 |
assert(result == NULL || PyList_Check(result)); |
| 1985 |
|
| 1986 |
if (result != NULL && PyList_Sort(result) != 0) { |
| 1987 |
|
| 1988 |
Py_DECREF(result); |
| 1989 |
result = NULL; |
| 1990 |
} |
| 1991 |
|
| 1992 |
return result; |
| 1993 |
} |
| 1994 |
|
| 1995 |
|
| 1996 |
|
| 1997 |
|
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
static PyObject * |
| 2004 |
none_repr(PyObject *op) |
| 2005 |
{ |
| 2006 |
return PyString_FromString("None"); |
| 2007 |
} |
| 2008 |
|
| 2009 |
|
| 2010 |
static void |
| 2011 |
none_dealloc(PyObject* ignore) |
| 2012 |
{ |
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
Py_FatalError("deallocating None"); |
| 2017 |
} |
| 2018 |
|
| 2019 |
|
| 2020 |
static PyTypeObject PyNone_Type = { |
| 2021 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2022 |
"NoneType", |
| 2023 |
0, |
| 2024 |
0, |
| 2025 |
none_dealloc, |
| 2026 |
0, |
| 2027 |
0, |
| 2028 |
0, |
| 2029 |
0, |
| 2030 |
none_repr, |
| 2031 |
0, |
| 2032 |
0, |
| 2033 |
0, |
| 2034 |
(hashfunc)_Py_HashPointer, |
| 2035 |
}; |
| 2036 |
|
| 2037 |
PyObject _Py_NoneStruct = { |
| 2038 |
_PyObject_EXTRA_INIT |
| 2039 |
1, &PyNone_Type |
| 2040 |
}; |
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
static PyObject * |
| 2046 |
NotImplemented_repr(PyObject *op) |
| 2047 |
{ |
| 2048 |
return PyString_FromString("NotImplemented"); |
| 2049 |
} |
| 2050 |
|
| 2051 |
static PyTypeObject PyNotImplemented_Type = { |
| 2052 |
PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 2053 |
"NotImplementedType", |
| 2054 |
0, |
| 2055 |
0, |
| 2056 |
none_dealloc, |
| 2057 |
0, |
| 2058 |
0, |
| 2059 |
0, |
| 2060 |
0, |
| 2061 |
NotImplemented_repr, |
| 2062 |
0, |
| 2063 |
0, |
| 2064 |
0, |
| 2065 |
0, |
| 2066 |
}; |
| 2067 |
|
| 2068 |
PyObject _Py_NotImplementedStruct = { |
| 2069 |
_PyObject_EXTRA_INIT |
| 2070 |
1, &PyNotImplemented_Type |
| 2071 |
}; |
| 2072 |
|
| 2073 |
void |
| 2074 |
_Py_ReadyTypes(void) |
| 2075 |
{ |
| 2076 |
if (PyType_Ready(&PyType_Type) < 0) |
| 2077 |
Py_FatalError("Can't initialize type type"); |
| 2078 |
|
| 2079 |
if (PyType_Ready(&_PyWeakref_RefType) < 0) |
| 2080 |
Py_FatalError("Can't initialize weakref type"); |
| 2081 |
|
| 2082 |
if (PyType_Ready(&_PyWeakref_CallableProxyType) < 0) |
| 2083 |
Py_FatalError("Can't initialize callable weakref proxy type"); |
| 2084 |
|
| 2085 |
if (PyType_Ready(&_PyWeakref_ProxyType) < 0) |
| 2086 |
Py_FatalError("Can't initialize weakref proxy type"); |
| 2087 |
|
| 2088 |
if (PyType_Ready(&PyBool_Type) < 0) |
| 2089 |
Py_FatalError("Can't initialize bool type"); |
| 2090 |
|
| 2091 |
if (PyType_Ready(&PyString_Type) < 0) |
| 2092 |
Py_FatalError("Can't initialize str type"); |
| 2093 |
|
| 2094 |
if (PyType_Ready(&PyByteArray_Type) < 0) |
| 2095 |
Py_FatalError("Can't initialize bytearray type"); |
| 2096 |
|
| 2097 |
if (PyType_Ready(&PyList_Type) < 0) |
| 2098 |
Py_FatalError("Can't initialize list type"); |
| 2099 |
|
| 2100 |
if (PyType_Ready(&PyNone_Type) < 0) |
| 2101 |
Py_FatalError("Can't initialize None type"); |
| 2102 |
|
| 2103 |
if (PyType_Ready(&PyNotImplemented_Type) < 0) |
| 2104 |
Py_FatalError("Can't initialize NotImplemented type"); |
| 2105 |
|
| 2106 |
if (PyType_Ready(&PyTraceBack_Type) < 0) |
| 2107 |
Py_FatalError("Can't initialize traceback type"); |
| 2108 |
|
| 2109 |
if (PyType_Ready(&PySuper_Type) < 0) |
| 2110 |
Py_FatalError("Can't initialize super type"); |
| 2111 |
|
| 2112 |
if (PyType_Ready(&PyBaseObject_Type) < 0) |
| 2113 |
Py_FatalError("Can't initialize object type"); |
| 2114 |
|
| 2115 |
if (PyType_Ready(&PyRange_Type) < 0) |
| 2116 |
Py_FatalError("Can't initialize xrange type"); |
| 2117 |
|
| 2118 |
if (PyType_Ready(&PyDict_Type) < 0) |
| 2119 |
Py_FatalError("Can't initialize dict type"); |
| 2120 |
|
| 2121 |
if (PyType_Ready(&PySet_Type) < 0) |
| 2122 |
Py_FatalError("Can't initialize set type"); |
| 2123 |
|
| 2124 |
#ifdef Py_USING_UNICODE |
| 2125 |
if (PyType_Ready(&PyUnicode_Type) < 0) |
| 2126 |
Py_FatalError("Can't initialize unicode type"); |
| 2127 |
#endif |
| 2128 |
|
| 2129 |
if (PyType_Ready(&PySlice_Type) < 0) |
| 2130 |
Py_FatalError("Can't initialize slice type"); |
| 2131 |
|
| 2132 |
if (PyType_Ready(&PyStaticMethod_Type) < 0) |
| 2133 |
Py_FatalError("Can't initialize static method type"); |
| 2134 |
|
| 2135 |
#ifndef WITHOUT_COMPLEX |
| 2136 |
if (PyType_Ready(&PyComplex_Type) < 0) |
| 2137 |
Py_FatalError("Can't initialize complex type"); |
| 2138 |
#endif |
| 2139 |
|
| 2140 |
if (PyType_Ready(&PyFloat_Type) < 0) |
| 2141 |
Py_FatalError("Can't initialize float type"); |
| 2142 |
|
| 2143 |
if (PyType_Ready(&PyBuffer_Type) < 0) |
| 2144 |
Py_FatalError("Can't initialize buffer type"); |
| 2145 |
|
| 2146 |
if (PyType_Ready(&PyLong_Type) < 0) |
| 2147 |
Py_FatalError("Can't initialize long type"); |
| 2148 |
|
| 2149 |
if (PyType_Ready(&PyInt_Type) < 0) |
| 2150 |
Py_FatalError("Can't initialize int type"); |
| 2151 |
|
| 2152 |
if (PyType_Ready(&PyFrozenSet_Type) < 0) |
| 2153 |
Py_FatalError("Can't initialize frozenset type"); |
| 2154 |
|
| 2155 |
if (PyType_Ready(&PyProperty_Type) < 0) |
| 2156 |
Py_FatalError("Can't initialize property type"); |
| 2157 |
|
| 2158 |
if (PyType_Ready(&PyMemoryView_Type) < 0) |
| 2159 |
Py_FatalError("Can't initialize memoryview type"); |
| 2160 |
|
| 2161 |
if (PyType_Ready(&PyTuple_Type) < 0) |
| 2162 |
Py_FatalError("Can't initialize tuple type"); |
| 2163 |
|
| 2164 |
if (PyType_Ready(&PyEnum_Type) < 0) |
| 2165 |
Py_FatalError("Can't initialize enumerate type"); |
| 2166 |
|
| 2167 |
if (PyType_Ready(&PyReversed_Type) < 0) |
| 2168 |
Py_FatalError("Can't initialize reversed type"); |
| 2169 |
|
| 2170 |
if (PyType_Ready(&PyCode_Type) < 0) |
| 2171 |
Py_FatalError("Can't initialize code type"); |
| 2172 |
|
| 2173 |
if (PyType_Ready(&PyFrame_Type) < 0) |
| 2174 |
Py_FatalError("Can't initialize frame type"); |
| 2175 |
|
| 2176 |
if (PyType_Ready(&PyCFunction_Type) < 0) |
| 2177 |
Py_FatalError("Can't initialize builtin function type"); |
| 2178 |
|
| 2179 |
if (PyType_Ready(&PyMethod_Type) < 0) |
| 2180 |
Py_FatalError("Can't initialize method type"); |
| 2181 |
|
| 2182 |
if (PyType_Ready(&PyFunction_Type) < 0) |
| 2183 |
Py_FatalError("Can't initialize function type"); |
| 2184 |
|
| 2185 |
if (PyType_Ready(&PyClass_Type) < 0) |
| 2186 |
Py_FatalError("Can't initialize class type"); |
| 2187 |
|
| 2188 |
if (PyType_Ready(&PyDictProxy_Type) < 0) |
| 2189 |
Py_FatalError("Can't initialize dict proxy type"); |
| 2190 |
|
| 2191 |
if (PyType_Ready(&PyGen_Type) < 0) |
| 2192 |
Py_FatalError("Can't initialize generator type"); |
| 2193 |
|
| 2194 |
if (PyType_Ready(&PyGetSetDescr_Type) < 0) |
| 2195 |
Py_FatalError("Can't initialize get-set descriptor type"); |
| 2196 |
|
| 2197 |
if (PyType_Ready(&PyWrapperDescr_Type) < 0) |
| 2198 |
Py_FatalError("Can't initialize wrapper type"); |
| 2199 |
|
| 2200 |
if (PyType_Ready(&PyInstance_Type) < 0) |
| 2201 |
Py_FatalError("Can't initialize instance type"); |
| 2202 |
|
| 2203 |
if (PyType_Ready(&PyEllipsis_Type) < 0) |
| 2204 |
Py_FatalError("Can't initialize ellipsis type"); |
| 2205 |
|
| 2206 |
if (PyType_Ready(&PyMemberDescr_Type) < 0) |
| 2207 |
Py_FatalError("Can't initialize member descriptor type"); |
| 2208 |
|
| 2209 |
if (PyType_Ready(&PyFile_Type) < 0) |
| 2210 |
Py_FatalError("Can't initialize file type"); |
| 2211 |
|
| 2212 |
if (PyType_Ready(&PyCapsule_Type) < 0) |
| 2213 |
Py_FatalError("Can't initialize capsule type"); |
| 2214 |
|
| 2215 |
if (PyType_Ready(&PyCell_Type) < 0) |
| 2216 |
Py_FatalError("Can't initialize cell type"); |
| 2217 |
|
| 2218 |
if (PyType_Ready(&PyCallIter_Type) < 0) |
| 2219 |
Py_FatalError("Can't initialize call iter type"); |
| 2220 |
|
| 2221 |
if (PyType_Ready(&PySeqIter_Type) < 0) |
| 2222 |
Py_FatalError("Can't initialize sequence iterator type"); |
| 2223 |
} |
| 2224 |
|
| 2225 |
|
| 2226 |
#ifdef Py_TRACE_REFS |
| 2227 |
|
| 2228 |
void |
| 2229 |
_Py_NewReference(PyObject *op) |
| 2230 |
{ |
| 2231 |
_Py_INC_REFTOTAL; |
| 2232 |
op->ob_refcnt = 1; |
| 2233 |
_Py_AddToAllObjects(op, 1); |
| 2234 |
_Py_INC_TPALLOCS(op); |
| 2235 |
} |
| 2236 |
|
| 2237 |
void |
| 2238 |
_Py_ForgetReference(register PyObject *op) |
| 2239 |
{ |
| 2240 |
#ifdef SLOW_UNREF_CHECK |
| 2241 |
register PyObject *p; |
| 2242 |
#endif |
| 2243 |
if (op->ob_refcnt < 0) |
| 2244 |
Py_FatalError("UNREF negative refcnt"); |
| 2245 |
if (op == &refchain || |
| 2246 |
op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) |
| 2247 |
Py_FatalError("UNREF invalid object"); |
| 2248 |
#ifdef SLOW_UNREF_CHECK |
| 2249 |
for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 2250 |
if (p == op) |
| 2251 |
break; |
| 2252 |
} |
| 2253 |
if (p == &refchain) |
| 2254 |
Py_FatalError("UNREF unknown object"); |
| 2255 |
#endif |
| 2256 |
op->_ob_next->_ob_prev = op->_ob_prev; |
| 2257 |
op->_ob_prev->_ob_next = op->_ob_next; |
| 2258 |
op->_ob_next = op->_ob_prev = NULL; |
| 2259 |
_Py_INC_TPFREES(op); |
| 2260 |
} |
| 2261 |
|
| 2262 |
void |
| 2263 |
_Py_Dealloc(PyObject *op) |
| 2264 |
{ |
| 2265 |
destructor dealloc = Py_TYPE(op)->tp_dealloc; |
| 2266 |
_Py_ForgetReference(op); |
| 2267 |
(*dealloc)(op); |
| 2268 |
} |
| 2269 |
|
| 2270 |
|
| 2271 |
|
| 2272 |
|
| 2273 |
void |
| 2274 |
_Py_PrintReferences(FILE *fp) |
| 2275 |
{ |
| 2276 |
PyObject *op; |
| 2277 |
fprintf(fp, "Remaining objects:\n"); |
| 2278 |
for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
| 2279 |
fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt); |
| 2280 |
if (PyObject_Print(op, fp, 0) != 0) |
| 2281 |
PyErr_Clear(); |
| 2282 |
putc('\n', fp); |
| 2283 |
} |
| 2284 |
} |
| 2285 |
|
| 2286 |
|
| 2287 |
|
| 2288 |
|
| 2289 |
void |
| 2290 |
_Py_PrintReferenceAddresses(FILE *fp) |
| 2291 |
{ |
| 2292 |
PyObject *op; |
| 2293 |
fprintf(fp, "Remaining object addresses:\n"); |
| 2294 |
for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) |
| 2295 |
fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op, |
| 2296 |
op->ob_refcnt, Py_TYPE(op)->tp_name); |
| 2297 |
} |
| 2298 |
|
| 2299 |
PyObject * |
| 2300 |
_Py_GetObjects(PyObject *self, PyObject *args) |
| 2301 |
{ |
| 2302 |
int i, n; |
| 2303 |
PyObject *t = NULL; |
| 2304 |
PyObject *res, *op; |
| 2305 |
|
| 2306 |
if (!PyArg_ParseTuple(args, "i|O", &n, &t)) |
| 2307 |
return NULL; |
| 2308 |
op = refchain._ob_next; |
| 2309 |
res = PyList_New(0); |
| 2310 |
if (res == NULL) |
| 2311 |
return NULL; |
| 2312 |
for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { |
| 2313 |
while (op == self || op == args || op == res || op == t || |
| 2314 |
(t != NULL && Py_TYPE(op) != (PyTypeObject *) t)) { |
| 2315 |
op = op->_ob_next; |
| 2316 |
if (op == &refchain) |
| 2317 |
return res; |
| 2318 |
} |
| 2319 |
if (PyList_Append(res, op) < 0) { |
| 2320 |
Py_DECREF(res); |
| 2321 |
return NULL; |
| 2322 |
} |
| 2323 |
op = op->_ob_next; |
| 2324 |
} |
| 2325 |
return res; |
| 2326 |
} |
| 2327 |
|
| 2328 |
#endif |
| 2329 |
|
| 2330 |
|
| 2331 |
|
| 2332 |
PyTypeObject *_Py_capsule_hack = &PyCapsule_Type; |
| 2333 |
|
| 2334 |
|
| 2335 |
|
| 2336 |
PyTypeObject *_Py_cobject_hack = &PyCObject_Type; |
| 2337 |
|
| 2338 |
|
| 2339 |
|
| 2340 |
Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; |
| 2341 |
|
| 2342 |
|
| 2343 |
|
| 2344 |
|
| 2345 |
void * |
| 2346 |
PyMem_Malloc(size_t nbytes) |
| 2347 |
{ |
| 2348 |
return PyMem_MALLOC(nbytes); |
| 2349 |
} |
| 2350 |
|
| 2351 |
void * |
| 2352 |
PyMem_Realloc(void *p, size_t nbytes) |
| 2353 |
{ |
| 2354 |
return PyMem_REALLOC(p, nbytes); |
| 2355 |
} |
| 2356 |
|
| 2357 |
void |
| 2358 |
PyMem_Free(void *p) |
| 2359 |
{ |
| 2360 |
PyMem_FREE(p); |
| 2361 |
} |
| 2362 |
|
| 2363 |
|
| 2364 |
|
| 2365 |
|
| 2366 |
|
| 2367 |
|
| 2368 |
|
| 2369 |
|
| 2370 |
|
| 2371 |
|
| 2372 |
|
| 2373 |
|
| 2374 |
|
| 2375 |
|
| 2376 |
#define KEY "Py_Repr" |
| 2377 |
|
| 2378 |
int |
| 2379 |
Py_ReprEnter(PyObject *obj) |
| 2380 |
{ |
| 2381 |
PyObject *dict; |
| 2382 |
PyObject *list; |
| 2383 |
Py_ssize_t i; |
| 2384 |
|
| 2385 |
dict = PyThreadState_GetDict(); |
| 2386 |
if (dict == NULL) |
| 2387 |
return 0; |
| 2388 |
list = PyDict_GetItemString(dict, KEY); |
| 2389 |
if (list == NULL) { |
| 2390 |
list = PyList_New(0); |
| 2391 |
if (list == NULL) |
| 2392 |
return -1; |
| 2393 |
if (PyDict_SetItemString(dict, KEY, list) < 0) |
| 2394 |
return -1; |
| 2395 |
Py_DECREF(list); |
| 2396 |
} |
| 2397 |
i = PyList_GET_SIZE(list); |
| 2398 |
while (--i >= 0) { |
| 2399 |
if (PyList_GET_ITEM(list, i) == obj) |
| 2400 |
return 1; |
| 2401 |
} |
| 2402 |
PyList_Append(list, obj); |
| 2403 |
return 0; |
| 2404 |
} |
| 2405 |
|
| 2406 |
void |
| 2407 |
Py_ReprLeave(PyObject *obj) |
| 2408 |
{ |
| 2409 |
PyObject *dict; |
| 2410 |
PyObject *list; |
| 2411 |
Py_ssize_t i; |
| 2412 |
|
| 2413 |
dict = PyThreadState_GetDict(); |
| 2414 |
if (dict == NULL) |
| 2415 |
return; |
| 2416 |
list = PyDict_GetItemString(dict, KEY); |
| 2417 |
if (list == NULL || !PyList_Check(list)) |
| 2418 |
return; |
| 2419 |
i = PyList_GET_SIZE(list); |
| 2420 |
|
| 2421 |
while (--i >= 0) { |
| 2422 |
if (PyList_GET_ITEM(list, i) == obj) { |
| 2423 |
PyList_SetSlice(list, i, i + 1, NULL); |
| 2424 |
break; |
| 2425 |
} |
| 2426 |
} |
| 2427 |
} |
| 2428 |
|
| 2429 |
|
| 2430 |
|
| 2431 |
|
| 2432 |
int _PyTrash_delete_nesting = 0; |
| 2433 |
|
| 2434 |
|
| 2435 |
|
| 2436 |
|
| 2437 |
PyObject *_PyTrash_delete_later = NULL; |
| 2438 |
|
| 2439 |
|
| 2440 |
|
| 2441 |
|
| 2442 |
|
| 2443 |
void |
| 2444 |
_PyTrash_deposit_object(PyObject *op) |
| 2445 |
{ |
| 2446 |
assert(PyObject_IS_GC(op)); |
| 2447 |
assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED); |
| 2448 |
assert(op->ob_refcnt == 0); |
| 2449 |
_Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later; |
| 2450 |
_PyTrash_delete_later = op; |
| 2451 |
} |
| 2452 |
|
| 2453 |
|
| 2454 |
void |
| 2455 |
_PyTrash_thread_deposit_object(PyObject *op) |
| 2456 |
{ |
| 2457 |
PyThreadState *tstate = PyThreadState_GET(); |
| 2458 |
assert(PyObject_IS_GC(op)); |
| 2459 |
assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED); |
| 2460 |
assert(op->ob_refcnt == 0); |
| 2461 |
_Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *) tstate->trash_delete_later; |
| 2462 |
tstate->trash_delete_later = op; |
| 2463 |
} |
| 2464 |
|
| 2465 |
|
| 2466 |
|
| 2467 |
|
| 2468 |
void |
| 2469 |
_PyTrash_destroy_chain(void) |
| 2470 |
{ |
| 2471 |
while (_PyTrash_delete_later) { |
| 2472 |
PyObject *op = _PyTrash_delete_later; |
| 2473 |
destructor dealloc = Py_TYPE(op)->tp_dealloc; |
| 2474 |
|
| 2475 |
_PyTrash_delete_later = |
| 2476 |
(PyObject*) _Py_AS_GC(op)->gc.gc_prev; |
| 2477 |
|
| 2478 |
|
| 2479 |
|
| 2480 |
|
| 2481 |
|
| 2482 |
|
| 2483 |
|
| 2484 |
assert(op->ob_refcnt == 0); |
| 2485 |
++_PyTrash_delete_nesting; |
| 2486 |
(*dealloc)(op); |
| 2487 |
--_PyTrash_delete_nesting; |
| 2488 |
} |
| 2489 |
} |
| 2490 |
|
| 2491 |
|
| 2492 |
void |
| 2493 |
_PyTrash_thread_destroy_chain(void) |
| 2494 |
{ |
| 2495 |
PyThreadState *tstate = PyThreadState_GET(); |
| 2496 |
while (tstate->trash_delete_later) { |
| 2497 |
PyObject *op = tstate->trash_delete_later; |
| 2498 |
destructor dealloc = Py_TYPE(op)->tp_dealloc; |
| 2499 |
|
| 2500 |
tstate->trash_delete_later = |
| 2501 |
(PyObject*) _Py_AS_GC(op)->gc.gc_prev; |
| 2502 |
|
| 2503 |
|
| 2504 |
|
| 2505 |
|
| 2506 |
|
| 2507 |
|
| 2508 |
|
| 2509 |
assert(op->ob_refcnt == 0); |
| 2510 |
++tstate->trash_delete_nesting; |
| 2511 |
(*dealloc)(op); |
| 2512 |
--tstate->trash_delete_nesting; |
| 2513 |
} |
| 2514 |
} |
| 2515 |
|
| 2516 |
#ifdef __cplusplus |
| 2517 |
} |
| 2518 |
#endif |