2 * Special implementations of built-in functions and methods.
4 * Optional optimisations for builtins are in Optimize.c.
6 * General object operations and protocols are in ObjectHandling.c.
9 //////////////////// Globals.proto ////////////////////
11 static PyObject
* __Pyx_Globals(void); /*proto*/
13 //////////////////// Globals ////////////////////
15 //@requires: ObjectHandling.c::GetAttr
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.
22 static PyObject
* __Pyx_Globals(void) {
25 PyObject
*names
= NULL
;
26 PyObject
*globals
= PyObject_GetAttr($module_cname
, PYIDENT("__dict__"));
28 PyErr_SetString(PyExc_TypeError
,
29 "current module must have __dict__ attribute");
32 names
= PyObject_Dir($module_cname
);
35 for (i
= PyList_GET_SIZE(names
)-1; i
>= 0; i
--) {
36 #if CYTHON_COMPILING_IN_PYPY
37 PyObject
* name
= PySequence_GetItem(names
, i
);
41 PyObject
* name
= PyList_GET_ITEM(names
, i
);
43 if (!PyDict_Contains(globals
, name
)) {
44 PyObject
* value
= __Pyx_GetAttr($module_cname
, name
);
46 #if CYTHON_COMPILING_IN_PYPY
51 if (PyDict_SetItem(globals
, name
, value
) < 0) {
52 #if CYTHON_COMPILING_IN_PYPY
59 #if CYTHON_COMPILING_IN_PYPY
65 // d = PyDictProxy_New(globals);
66 // Py_DECREF(globals);
74 //////////////////// PyExecGlobals.proto ////////////////////
76 static PyObject
* __Pyx_PyExecGlobals(PyObject
*);
78 //////////////////// PyExecGlobals ////////////////////
82 static PyObject
* __Pyx_PyExecGlobals(PyObject
* code
) {
84 PyObject
* globals
= __Pyx_Globals();
85 if (unlikely(!globals
))
87 result
= __Pyx_PyExec2(code
, globals
);
92 //////////////////// PyExec.proto ////////////////////
94 static PyObject
* __Pyx_PyExec3(PyObject
*, PyObject
*, PyObject
*);
95 static CYTHON_INLINE PyObject
* __Pyx_PyExec2(PyObject
*, PyObject
*);
97 //////////////////// PyExec ////////////////////
100 static CYTHON_INLINE PyObject
* __Pyx_PyExec2(PyObject
* o
, PyObject
* globals
) {
101 return __Pyx_PyExec3(o
, globals
, NULL
);
104 static PyObject
* __Pyx_PyExec3(PyObject
* o
, PyObject
* globals
, PyObject
* locals
) {
109 if (!globals
|| globals
== Py_None
) {
110 globals
= PyModule_GetDict($module_cname
);
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
);
118 if (!locals
|| locals
== Py_None
) {
122 if (PyDict_GetItem(globals
, PYIDENT("__builtins__")) == NULL
) {
123 if (PyDict_SetItem(globals
, PYIDENT("__builtins__"), PyEval_GetBuiltins()) < 0)
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");
133 #if PY_VERSION_HEX < 0x030200B1
134 result
= PyEval_EvalCode((PyCodeObject
*)o
, globals
, locals
);
136 result
= PyEval_EvalCode(o
, globals
, locals
);
141 if (PyUnicode_Check(o
)) {
142 cf
.cf_flags
= PyCF_SOURCE_IS_UTF8
;
143 s
= PyUnicode_AsUTF8String(o
);
146 #if PY_MAJOR_VERSION >= 3
147 } else if (!PyBytes_Check(o
)) {
149 } else if (!PyString_Check(o
)) {
151 PyErr_Format(PyExc_TypeError
,
152 "exec: arg 1 must be string, bytes or code object, got %.200s",
153 Py_TYPE(o
)->tp_name
);
156 #if PY_MAJOR_VERSION >= 3
157 code
= PyBytes_AS_STRING(o
);
159 code
= PyString_AS_STRING(o
);
161 if (PyEval_MergeCompilerFlags(&cf
)) {
162 result
= PyRun_StringFlags(code
, Py_file_input
, globals
, locals
, &cf
);
164 result
= PyRun_String(code
, Py_file_input
, globals
, locals
);
175 //////////////////// GetAttr3.proto ////////////////////
177 static CYTHON_INLINE PyObject
*__Pyx_GetAttr3(PyObject
*, PyObject
*, PyObject
*); /*proto*/
179 //////////////////// GetAttr3 ////////////////////
180 //@requires: ObjectHandling.c::GetAttr
182 static CYTHON_INLINE PyObject
*__Pyx_GetAttr3(PyObject
*o
, PyObject
*n
, PyObject
*d
) {
183 PyObject
*r
= __Pyx_GetAttr(o
, n
);
185 if (!PyErr_ExceptionMatches(PyExc_AttributeError
))
196 //////////////////// Intern.proto ////////////////////
198 static PyObject
* __Pyx_Intern(PyObject
* s
); /* proto */
200 //////////////////// Intern ////////////////////
202 static PyObject
* __Pyx_Intern(PyObject
* s
) {
203 if (!(likely(PyString_CheckExact(s
)))) {
204 PyErr_Format(PyExc_TypeError
, "Expected %.16s, got %.200s", "str", Py_TYPE(s
)->tp_name
);
208 #if PY_MAJOR_VERSION >= 3
209 PyUnicode_InternInPlace(&s
);
211 PyString_InternInPlace(&s
);
216 //////////////////// abs_int.proto ////////////////////
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
);
224 //////////////////// abs_long.proto ////////////////////
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
);
232 //////////////////// abs_longlong.proto ////////////////////
234 static CYTHON_INLINE
unsigned PY_LONG_LONG
__Pyx_abs_longlong(PY_LONG_LONG x
) {
237 const PY_LONG_LONG PY_LLONG_MAX
= LLONG_MAX
;
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_LONG_LONG
) - 2)) - 1));
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
);
248 return (x
<0) ? (unsigned PY_LONG_LONG
)-x
: (unsigned PY_LONG_LONG
)x
;
252 //////////////////// pow2.proto ////////////////////
254 #define __Pyx_PyNumber_Power2(a, b) PyNumber_Power(a, b, Py_None)
256 //////////////////// py_dict_keys.proto ////////////////////
258 static CYTHON_INLINE PyObject
* __Pyx_PyDict_Keys(PyObject
* d
); /*proto*/
260 //////////////////// py_dict_keys ////////////////////
261 //@requires: ObjectHandling.c::PyObjectCallMethod
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
);
267 return PyDict_Keys(d
);
270 //////////////////// py_dict_values.proto ////////////////////
272 static CYTHON_INLINE PyObject
* __Pyx_PyDict_Values(PyObject
* d
); /*proto*/
274 //////////////////// py_dict_values ////////////////////
275 //@requires: ObjectHandling.c::PyObjectCallMethod
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("values"), d
);
281 return PyDict_Values(d
);
284 //////////////////// py_dict_items.proto ////////////////////
286 static CYTHON_INLINE PyObject
* __Pyx_PyDict_Items(PyObject
* d
); /*proto*/
288 //////////////////// py_dict_items ////////////////////
289 //@requires: ObjectHandling.c::PyObjectCallMethod
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("items"), d
);
295 return PyDict_Items(d
);
298 //////////////////// py_dict_iterkeys.proto ////////////////////
300 static CYTHON_INLINE PyObject
* __Pyx_PyDict_IterKeys(PyObject
* d
); /*proto*/
302 //////////////////// py_dict_iterkeys ////////////////////
303 //@requires: ObjectHandling.c::PyObjectCallMethod
305 static CYTHON_INLINE PyObject
* __Pyx_PyDict_IterKeys(PyObject
* d
) {
306 return __Pyx_PyObject_CallMethod0(d
, (PY_MAJOR_VERSION
>= 3) ? PYIDENT("keys") : PYIDENT("iterkeys"));
309 //////////////////// py_dict_itervalues.proto ////////////////////
311 static CYTHON_INLINE PyObject
* __Pyx_PyDict_IterValues(PyObject
* d
); /*proto*/
313 //////////////////// py_dict_itervalues ////////////////////
314 //@requires: ObjectHandling.c::PyObjectCallMethod
316 static CYTHON_INLINE PyObject
* __Pyx_PyDict_IterValues(PyObject
* d
) {
317 return __Pyx_PyObject_CallMethod0(d
, (PY_MAJOR_VERSION
>= 3) ? PYIDENT("values") : PYIDENT("itervalues"));
320 //////////////////// py_dict_iteritems.proto ////////////////////
322 static CYTHON_INLINE PyObject
* __Pyx_PyDict_IterItems(PyObject
* d
); /*proto*/
324 //////////////////// py_dict_iteritems ////////////////////
325 //@requires: ObjectHandling.c::PyObjectCallMethod
327 static CYTHON_INLINE PyObject
* __Pyx_PyDict_IterItems(PyObject
* d
) {
328 return __Pyx_PyObject_CallMethod0(d
, (PY_MAJOR_VERSION
>= 3) ? PYIDENT("items") : PYIDENT("iteritems"));
331 //////////////////// py_dict_viewkeys.proto ////////////////////
333 #if PY_VERSION_HEX < 0x02070000
334 #error This module uses dict views, which require Python 2.7 or later
336 static CYTHON_INLINE PyObject
* __Pyx_PyDict_ViewKeys(PyObject
* d
); /*proto*/
338 //////////////////// py_dict_viewkeys ////////////////////
339 //@requires: ObjectHandling.c::PyObjectCallMethod
341 static CYTHON_INLINE PyObject
* __Pyx_PyDict_ViewKeys(PyObject
* d
) {
342 return __Pyx_PyObject_CallMethod0(d
, (PY_MAJOR_VERSION
>= 3) ? PYIDENT("keys") : PYIDENT("viewkeys"));
345 //////////////////// py_dict_viewvalues.proto ////////////////////
347 #if PY_VERSION_HEX < 0x02070000
348 #error This module uses dict views, which require Python 2.7 or later
350 static CYTHON_INLINE PyObject
* __Pyx_PyDict_ViewValues(PyObject
* d
); /*proto*/
352 //////////////////// py_dict_viewvalues ////////////////////
353 //@requires: ObjectHandling.c::PyObjectCallMethod
355 static CYTHON_INLINE PyObject
* __Pyx_PyDict_ViewValues(PyObject
* d
) {
356 return __Pyx_PyObject_CallMethod0(d
, (PY_MAJOR_VERSION
>= 3) ? PYIDENT("values") : PYIDENT("viewvalues"));
359 //////////////////// py_dict_viewitems.proto ////////////////////
361 #if PY_VERSION_HEX < 0x02070000
362 #error This module uses dict views, which require Python 2.7 or later
364 static CYTHON_INLINE PyObject
* __Pyx_PyDict_ViewItems(PyObject
* d
); /*proto*/
366 //////////////////// py_dict_viewitems ////////////////////
367 //@requires: ObjectHandling.c::PyObjectCallMethod
369 static CYTHON_INLINE PyObject
* __Pyx_PyDict_ViewItems(PyObject
* d
) {
370 return __Pyx_PyObject_CallMethod0(d
, (PY_MAJOR_VERSION
>= 3) ? PYIDENT("items") : PYIDENT("viewitems"));
373 //////////////////// pyset_compat.proto ////////////////////
375 #if PY_VERSION_HEX < 0x02050000
376 #ifndef PyAnySet_CheckExact
378 #define PyAnySet_CheckExact(ob) \
379 ((ob)->ob_type == &PySet_Type || \
380 (ob)->ob_type == &PyFrozenSet_Type)
382 #define PySet_New(iterable) \
383 PyObject_CallFunctionObjArgs((PyObject *)&PySet_Type, (iterable), NULL)
385 #define PyFrozenSet_New(iterable) \
386 PyObject_CallFunctionObjArgs((PyObject *)&PyFrozenSet_Type, (iterable), NULL)
388 #define PySet_Size(anyset) \
389 PyObject_Size((anyset))
391 #define PySet_GET_SIZE(anyset) \
392 PyObject_Size((anyset))
394 #define PySet_Contains(anyset, key) \
395 PySequence_Contains((anyset), (key))
397 #define PySet_Pop(set) \
398 PyObject_CallMethod((set), (char*)"pop", NULL)
400 static CYTHON_INLINE
int PySet_Clear(PyObject
*set
) {
401 PyObject
*ret
= PyObject_CallMethod(set
, (char*)"clear", NULL
);
403 Py_DECREF(ret
); return 0;
406 static CYTHON_INLINE
int PySet_Discard(PyObject
*set
, PyObject
*key
) {
407 PyObject
*ret
= PyObject_CallMethod(set
, (char*)"discard", (char*)"(O)", key
);
409 Py_DECREF(ret
); return 0;
412 static CYTHON_INLINE
int PySet_Add(PyObject
*set
, PyObject
*key
) {
413 PyObject
*ret
= PyObject_CallMethod(set
, (char*)"add", (char*)"(O)", key
);
415 Py_DECREF(ret
); return 0;
418 #endif /* PyAnySet_CheckExact (<= Py2.4) */
421 //////////////////// pyfrozenset_new.proto ////////////////////
422 //@substitute: naming
423 //@requires: pyset_compat
425 static CYTHON_INLINE PyObject
* __Pyx_PyFrozenSet_New(PyObject
* it
) {
428 if (PyFrozenSet_CheckExact(it
)) {
432 result
= PyFrozenSet_New(it
);
433 if (unlikely(!result
))
435 if (likely(PySet_GET_SIZE(result
)))
437 // empty frozenset is a singleton
438 // seems wasteful, but CPython does the same
441 #if CYTHON_COMPILING_IN_CPYTHON
442 return PyFrozenSet_Type
.tp_new(&PyFrozenSet_Type
, $empty_tuple
, NULL
);
444 return PyObject_Call((PyObject
*)&PyFrozenSet_Type
, $empty_tuple
, NULL
);