OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Special implementations of built-in functions and methods. |
| 3 * |
| 4 * Optional optimisations for builtins are in Optimize.c. |
| 5 * |
| 6 * General object operations and protocols are in ObjectHandling.c. |
| 7 */ |
| 8 |
| 9 //////////////////// Globals.proto //////////////////// |
| 10 |
| 11 static PyObject* __Pyx_Globals(void); /*proto*/ |
| 12 |
| 13 //////////////////// Globals //////////////////// |
| 14 //@substitute: naming |
| 15 //@requires: ObjectHandling.c::GetAttr |
| 16 |
| 17 // This is a stub implementation until we have something more complete. |
| 18 // Currently, we only handle the most common case of a read-only dict |
| 19 // of Python names. Supporting cdef names in the module and write |
| 20 // access requires a rewrite as a dedicated class. |
| 21 |
| 22 static PyObject* __Pyx_Globals(void) { |
| 23 Py_ssize_t i; |
| 24 //PyObject *d; |
| 25 PyObject *names = NULL; |
| 26 PyObject *globals = PyObject_GetAttr($module_cname, PYIDENT("__dict__")); |
| 27 if (!globals) { |
| 28 PyErr_SetString(PyExc_TypeError, |
| 29 "current module must have __dict__ attribute"); |
| 30 goto bad; |
| 31 } |
| 32 names = PyObject_Dir($module_cname); |
| 33 if (!names) |
| 34 goto bad; |
| 35 for (i = PyList_GET_SIZE(names)-1; i >= 0; i--) { |
| 36 #if CYTHON_COMPILING_IN_PYPY |
| 37 PyObject* name = PySequence_GetItem(names, i); |
| 38 if (!name) |
| 39 goto bad; |
| 40 #else |
| 41 PyObject* name = PyList_GET_ITEM(names, i); |
| 42 #endif |
| 43 if (!PyDict_Contains(globals, name)) { |
| 44 PyObject* value = __Pyx_GetAttr($module_cname, name); |
| 45 if (!value) { |
| 46 #if CYTHON_COMPILING_IN_PYPY |
| 47 Py_DECREF(name); |
| 48 #endif |
| 49 goto bad; |
| 50 } |
| 51 if (PyDict_SetItem(globals, name, value) < 0) { |
| 52 #if CYTHON_COMPILING_IN_PYPY |
| 53 Py_DECREF(name); |
| 54 #endif |
| 55 Py_DECREF(value); |
| 56 goto bad; |
| 57 } |
| 58 } |
| 59 #if CYTHON_COMPILING_IN_PYPY |
| 60 Py_DECREF(name); |
| 61 #endif |
| 62 } |
| 63 Py_DECREF(names); |
| 64 return globals; |
| 65 // d = PyDictProxy_New(globals); |
| 66 // Py_DECREF(globals); |
| 67 // return d; |
| 68 bad: |
| 69 Py_XDECREF(names); |
| 70 Py_XDECREF(globals); |
| 71 return NULL; |
| 72 } |
| 73 |
| 74 //////////////////// PyExecGlobals.proto //////////////////// |
| 75 |
| 76 static PyObject* __Pyx_PyExecGlobals(PyObject*); |
| 77 |
| 78 //////////////////// PyExecGlobals //////////////////// |
| 79 //@requires: Globals |
| 80 //@requires: PyExec |
| 81 |
| 82 static PyObject* __Pyx_PyExecGlobals(PyObject* code) { |
| 83 PyObject* result; |
| 84 PyObject* globals = __Pyx_Globals(); |
| 85 if (unlikely(!globals)) |
| 86 return NULL; |
| 87 result = __Pyx_PyExec2(code, globals); |
| 88 Py_DECREF(globals); |
| 89 return result; |
| 90 } |
| 91 |
| 92 //////////////////// PyExec.proto //////////////////// |
| 93 |
| 94 static PyObject* __Pyx_PyExec3(PyObject*, PyObject*, PyObject*); |
| 95 static CYTHON_INLINE PyObject* __Pyx_PyExec2(PyObject*, PyObject*); |
| 96 |
| 97 //////////////////// PyExec //////////////////// |
| 98 //@substitute: naming |
| 99 |
| 100 static CYTHON_INLINE PyObject* __Pyx_PyExec2(PyObject* o, PyObject* globals) { |
| 101 return __Pyx_PyExec3(o, globals, NULL); |
| 102 } |
| 103 |
| 104 static PyObject* __Pyx_PyExec3(PyObject* o, PyObject* globals, PyObject* locals)
{ |
| 105 PyObject* result; |
| 106 PyObject* s = 0; |
| 107 char *code = 0; |
| 108 |
| 109 if (!globals || globals == Py_None) { |
| 110 globals = PyModule_GetDict($module_cname); |
| 111 if (!globals) |
| 112 goto bad; |
| 113 } else if (!PyDict_Check(globals)) { |
| 114 PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.200s", |
| 115 Py_TYPE(globals)->tp_name); |
| 116 goto bad; |
| 117 } |
| 118 if (!locals || locals == Py_None) { |
| 119 locals = globals; |
| 120 } |
| 121 |
| 122 if (PyDict_GetItem(globals, PYIDENT("__builtins__")) == NULL) { |
| 123 if (PyDict_SetItem(globals, PYIDENT("__builtins__"), PyEval_GetBuiltins(
)) < 0) |
| 124 goto bad; |
| 125 } |
| 126 |
| 127 if (PyCode_Check(o)) { |
| 128 if (PyCode_GetNumFree((PyCodeObject *)o) > 0) { |
| 129 PyErr_SetString(PyExc_TypeError, |
| 130 "code object passed to exec() may not contain free variables"); |
| 131 goto bad; |
| 132 } |
| 133 #if PY_VERSION_HEX < 0x030200B1 |
| 134 result = PyEval_EvalCode((PyCodeObject *)o, globals, locals); |
| 135 #else |
| 136 result = PyEval_EvalCode(o, globals, locals); |
| 137 #endif |
| 138 } else { |
| 139 PyCompilerFlags cf; |
| 140 cf.cf_flags = 0; |
| 141 if (PyUnicode_Check(o)) { |
| 142 cf.cf_flags = PyCF_SOURCE_IS_UTF8; |
| 143 s = PyUnicode_AsUTF8String(o); |
| 144 if (!s) goto bad; |
| 145 o = s; |
| 146 #if PY_MAJOR_VERSION >= 3 |
| 147 } else if (!PyBytes_Check(o)) { |
| 148 #else |
| 149 } else if (!PyString_Check(o)) { |
| 150 #endif |
| 151 PyErr_Format(PyExc_TypeError, |
| 152 "exec: arg 1 must be string, bytes or code object, got %.200s", |
| 153 Py_TYPE(o)->tp_name); |
| 154 goto bad; |
| 155 } |
| 156 #if PY_MAJOR_VERSION >= 3 |
| 157 code = PyBytes_AS_STRING(o); |
| 158 #else |
| 159 code = PyString_AS_STRING(o); |
| 160 #endif |
| 161 if (PyEval_MergeCompilerFlags(&cf)) { |
| 162 result = PyRun_StringFlags(code, Py_file_input, globals, locals, &cf
); |
| 163 } else { |
| 164 result = PyRun_String(code, Py_file_input, globals, locals); |
| 165 } |
| 166 Py_XDECREF(s); |
| 167 } |
| 168 |
| 169 return result; |
| 170 bad: |
| 171 Py_XDECREF(s); |
| 172 return 0; |
| 173 } |
| 174 |
| 175 //////////////////// GetAttr3.proto //////////////////// |
| 176 |
| 177 static CYTHON_INLINE PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *
); /*proto*/ |
| 178 |
| 179 //////////////////// GetAttr3 //////////////////// |
| 180 //@requires: ObjectHandling.c::GetAttr |
| 181 |
| 182 static CYTHON_INLINE PyObject *__Pyx_GetAttr3(PyObject *o, PyObject *n, PyObject
*d) { |
| 183 PyObject *r = __Pyx_GetAttr(o, n); |
| 184 if (unlikely(!r)) { |
| 185 if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 186 goto bad; |
| 187 PyErr_Clear(); |
| 188 r = d; |
| 189 Py_INCREF(d); |
| 190 } |
| 191 return r; |
| 192 bad: |
| 193 return NULL; |
| 194 } |
| 195 |
| 196 //////////////////// Intern.proto //////////////////// |
| 197 |
| 198 static PyObject* __Pyx_Intern(PyObject* s); /* proto */ |
| 199 |
| 200 //////////////////// Intern //////////////////// |
| 201 |
| 202 static PyObject* __Pyx_Intern(PyObject* s) { |
| 203 if (!(likely(PyString_CheckExact(s)))) { |
| 204 PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TY
PE(s)->tp_name); |
| 205 return 0; |
| 206 } |
| 207 Py_INCREF(s); |
| 208 #if PY_MAJOR_VERSION >= 3 |
| 209 PyUnicode_InternInPlace(&s); |
| 210 #else |
| 211 PyString_InternInPlace(&s); |
| 212 #endif |
| 213 return s; |
| 214 } |
| 215 |
| 216 //////////////////// abs_int.proto //////////////////// |
| 217 |
| 218 static CYTHON_INLINE unsigned int __Pyx_abs_int(int x) { |
| 219 if (unlikely(x == -INT_MAX-1)) |
| 220 return ((unsigned int)INT_MAX) + 1U; |
| 221 return (unsigned int) abs(x); |
| 222 } |
| 223 |
| 224 //////////////////// abs_long.proto //////////////////// |
| 225 |
| 226 static CYTHON_INLINE unsigned long __Pyx_abs_long(long x) { |
| 227 if (unlikely(x == -LONG_MAX-1)) |
| 228 return ((unsigned long)LONG_MAX) + 1U; |
| 229 return (unsigned long) labs(x); |
| 230 } |
| 231 |
| 232 //////////////////// abs_longlong.proto //////////////////// |
| 233 |
| 234 static CYTHON_INLINE unsigned PY_LONG_LONG __Pyx_abs_longlong(PY_LONG_LONG x) { |
| 235 #ifndef PY_LLONG_MAX |
| 236 #ifdef LLONG_MAX |
| 237 const PY_LONG_LONG PY_LLONG_MAX = LLONG_MAX; |
| 238 #else |
| 239 // copied from pyport.h in CPython 3.3, missing in 2.4 |
| 240 const PY_LONG_LONG PY_LLONG_MAX = (1 + 2 * ((1LL << (CHAR_BIT * sizeof(PY_LO
NG_LONG) - 2)) - 1)); |
| 241 #endif |
| 242 #endif |
| 243 if (unlikely(x == -PY_LLONG_MAX-1)) |
| 244 return ((unsigned PY_LONG_LONG)PY_LLONG_MAX) + 1U; |
| 245 #if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L |
| 246 return (unsigned PY_LONG_LONG) llabs(x); |
| 247 #else |
| 248 return (x<0) ? (unsigned PY_LONG_LONG)-x : (unsigned PY_LONG_LONG)x; |
| 249 #endif |
| 250 } |
| 251 |
| 252 //////////////////// pow2.proto //////////////////// |
| 253 |
| 254 #define __Pyx_PyNumber_Power2(a, b) PyNumber_Power(a, b, Py_None) |
| 255 |
| 256 //////////////////// py_dict_keys.proto //////////////////// |
| 257 |
| 258 static CYTHON_INLINE PyObject* __Pyx_PyDict_Keys(PyObject* d); /*proto*/ |
| 259 |
| 260 //////////////////// py_dict_keys //////////////////// |
| 261 //@requires: ObjectHandling.c::PyObjectCallMethod |
| 262 |
| 263 static CYTHON_INLINE PyObject* __Pyx_PyDict_Keys(PyObject* d) { |
| 264 if (PY_MAJOR_VERSION >= 3) |
| 265 return __Pyx_PyObject_CallMethod1((PyObject*)&PyDict_Type, PYIDENT("keys
"), d); |
| 266 else |
| 267 return PyDict_Keys(d); |
| 268 } |
| 269 |
| 270 //////////////////// py_dict_values.proto //////////////////// |
| 271 |
| 272 static CYTHON_INLINE PyObject* __Pyx_PyDict_Values(PyObject* d); /*proto*/ |
| 273 |
| 274 //////////////////// py_dict_values //////////////////// |
| 275 //@requires: ObjectHandling.c::PyObjectCallMethod |
| 276 |
| 277 static CYTHON_INLINE PyObject* __Pyx_PyDict_Values(PyObject* d) { |
| 278 if (PY_MAJOR_VERSION >= 3) |
| 279 return __Pyx_PyObject_CallMethod1((PyObject*)&PyDict_Type, PYIDENT("valu
es"), d); |
| 280 else |
| 281 return PyDict_Values(d); |
| 282 } |
| 283 |
| 284 //////////////////// py_dict_items.proto //////////////////// |
| 285 |
| 286 static CYTHON_INLINE PyObject* __Pyx_PyDict_Items(PyObject* d); /*proto*/ |
| 287 |
| 288 //////////////////// py_dict_items //////////////////// |
| 289 //@requires: ObjectHandling.c::PyObjectCallMethod |
| 290 |
| 291 static CYTHON_INLINE PyObject* __Pyx_PyDict_Items(PyObject* d) { |
| 292 if (PY_MAJOR_VERSION >= 3) |
| 293 return __Pyx_PyObject_CallMethod1((PyObject*)&PyDict_Type, PYIDENT("item
s"), d); |
| 294 else |
| 295 return PyDict_Items(d); |
| 296 } |
| 297 |
| 298 //////////////////// py_dict_iterkeys.proto //////////////////// |
| 299 |
| 300 static CYTHON_INLINE PyObject* __Pyx_PyDict_IterKeys(PyObject* d); /*proto*/ |
| 301 |
| 302 //////////////////// py_dict_iterkeys //////////////////// |
| 303 //@requires: ObjectHandling.c::PyObjectCallMethod |
| 304 |
| 305 static CYTHON_INLINE PyObject* __Pyx_PyDict_IterKeys(PyObject* d) { |
| 306 return __Pyx_PyObject_CallMethod0(d, (PY_MAJOR_VERSION >= 3) ? PYIDENT("keys
") : PYIDENT("iterkeys")); |
| 307 } |
| 308 |
| 309 //////////////////// py_dict_itervalues.proto //////////////////// |
| 310 |
| 311 static CYTHON_INLINE PyObject* __Pyx_PyDict_IterValues(PyObject* d); /*proto*/ |
| 312 |
| 313 //////////////////// py_dict_itervalues //////////////////// |
| 314 //@requires: ObjectHandling.c::PyObjectCallMethod |
| 315 |
| 316 static CYTHON_INLINE PyObject* __Pyx_PyDict_IterValues(PyObject* d) { |
| 317 return __Pyx_PyObject_CallMethod0(d, (PY_MAJOR_VERSION >= 3) ? PYIDENT("valu
es") : PYIDENT("itervalues")); |
| 318 } |
| 319 |
| 320 //////////////////// py_dict_iteritems.proto //////////////////// |
| 321 |
| 322 static CYTHON_INLINE PyObject* __Pyx_PyDict_IterItems(PyObject* d); /*proto*/ |
| 323 |
| 324 //////////////////// py_dict_iteritems //////////////////// |
| 325 //@requires: ObjectHandling.c::PyObjectCallMethod |
| 326 |
| 327 static CYTHON_INLINE PyObject* __Pyx_PyDict_IterItems(PyObject* d) { |
| 328 return __Pyx_PyObject_CallMethod0(d, (PY_MAJOR_VERSION >= 3) ? PYIDENT("item
s") : PYIDENT("iteritems")); |
| 329 } |
| 330 |
| 331 //////////////////// py_dict_viewkeys.proto //////////////////// |
| 332 |
| 333 #if PY_VERSION_HEX < 0x02070000 |
| 334 #error This module uses dict views, which require Python 2.7 or later |
| 335 #endif |
| 336 static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewKeys(PyObject* d); /*proto*/ |
| 337 |
| 338 //////////////////// py_dict_viewkeys //////////////////// |
| 339 //@requires: ObjectHandling.c::PyObjectCallMethod |
| 340 |
| 341 static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewKeys(PyObject* d) { |
| 342 return __Pyx_PyObject_CallMethod0(d, (PY_MAJOR_VERSION >= 3) ? PYIDENT("keys
") : PYIDENT("viewkeys")); |
| 343 } |
| 344 |
| 345 //////////////////// py_dict_viewvalues.proto //////////////////// |
| 346 |
| 347 #if PY_VERSION_HEX < 0x02070000 |
| 348 #error This module uses dict views, which require Python 2.7 or later |
| 349 #endif |
| 350 static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewValues(PyObject* d); /*proto*/ |
| 351 |
| 352 //////////////////// py_dict_viewvalues //////////////////// |
| 353 //@requires: ObjectHandling.c::PyObjectCallMethod |
| 354 |
| 355 static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewValues(PyObject* d) { |
| 356 return __Pyx_PyObject_CallMethod0(d, (PY_MAJOR_VERSION >= 3) ? PYIDENT("valu
es") : PYIDENT("viewvalues")); |
| 357 } |
| 358 |
| 359 //////////////////// py_dict_viewitems.proto //////////////////// |
| 360 |
| 361 #if PY_VERSION_HEX < 0x02070000 |
| 362 #error This module uses dict views, which require Python 2.7 or later |
| 363 #endif |
| 364 static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewItems(PyObject* d); /*proto*/ |
| 365 |
| 366 //////////////////// py_dict_viewitems //////////////////// |
| 367 //@requires: ObjectHandling.c::PyObjectCallMethod |
| 368 |
| 369 static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewItems(PyObject* d) { |
| 370 return __Pyx_PyObject_CallMethod0(d, (PY_MAJOR_VERSION >= 3) ? PYIDENT("item
s") : PYIDENT("viewitems")); |
| 371 } |
| 372 |
| 373 //////////////////// pyset_compat.proto //////////////////// |
| 374 |
| 375 #if PY_VERSION_HEX < 0x02050000 |
| 376 #ifndef PyAnySet_CheckExact |
| 377 |
| 378 #define PyAnySet_CheckExact(ob) \ |
| 379 ((ob)->ob_type == &PySet_Type || \ |
| 380 (ob)->ob_type == &PyFrozenSet_Type) |
| 381 |
| 382 #define PySet_New(iterable) \ |
| 383 PyObject_CallFunctionObjArgs((PyObject *)&PySet_Type, (iterable), NULL) |
| 384 |
| 385 #define PyFrozenSet_New(iterable) \ |
| 386 PyObject_CallFunctionObjArgs((PyObject *)&PyFrozenSet_Type, (iterable), NULL
) |
| 387 |
| 388 #define PySet_Size(anyset) \ |
| 389 PyObject_Size((anyset)) |
| 390 |
| 391 #define PySet_GET_SIZE(anyset) \ |
| 392 PyObject_Size((anyset)) |
| 393 |
| 394 #define PySet_Contains(anyset, key) \ |
| 395 PySequence_Contains((anyset), (key)) |
| 396 |
| 397 #define PySet_Pop(set) \ |
| 398 PyObject_CallMethod((set), (char*)"pop", NULL) |
| 399 |
| 400 static CYTHON_INLINE int PySet_Clear(PyObject *set) { |
| 401 PyObject *ret = PyObject_CallMethod(set, (char*)"clear", NULL); |
| 402 if (!ret) return -1; |
| 403 Py_DECREF(ret); return 0; |
| 404 } |
| 405 |
| 406 static CYTHON_INLINE int PySet_Discard(PyObject *set, PyObject *key) { |
| 407 PyObject *ret = PyObject_CallMethod(set, (char*)"discard", (char*)"(O)", key
); |
| 408 if (!ret) return -1; |
| 409 Py_DECREF(ret); return 0; |
| 410 } |
| 411 |
| 412 static CYTHON_INLINE int PySet_Add(PyObject *set, PyObject *key) { |
| 413 PyObject *ret = PyObject_CallMethod(set, (char*)"add", (char*)"(O)", key); |
| 414 if (!ret) return -1; |
| 415 Py_DECREF(ret); return 0; |
| 416 } |
| 417 |
| 418 #endif /* PyAnySet_CheckExact (<= Py2.4) */ |
| 419 #endif /* < Py2.5 */ |
| 420 |
| 421 //////////////////// pyfrozenset_new.proto //////////////////// |
| 422 //@substitute: naming |
| 423 //@requires: pyset_compat |
| 424 |
| 425 static CYTHON_INLINE PyObject* __Pyx_PyFrozenSet_New(PyObject* it) { |
| 426 if (it) { |
| 427 PyObject* result; |
| 428 if (PyFrozenSet_CheckExact(it)) { |
| 429 Py_INCREF(it); |
| 430 return it; |
| 431 } |
| 432 result = PyFrozenSet_New(it); |
| 433 if (unlikely(!result)) |
| 434 return NULL; |
| 435 if (likely(PySet_GET_SIZE(result))) |
| 436 return result; |
| 437 // empty frozenset is a singleton |
| 438 // seems wasteful, but CPython does the same |
| 439 Py_DECREF(result); |
| 440 } |
| 441 #if CYTHON_COMPILING_IN_CPYTHON |
| 442 return PyFrozenSet_Type.tp_new(&PyFrozenSet_Type, $empty_tuple, NULL); |
| 443 #else |
| 444 return PyObject_Call((PyObject*)&PyFrozenSet_Type, $empty_tuple, NULL); |
| 445 #endif |
| 446 } |
OLD | NEW |