Remove obsolete entries from .gitignore.
[chromium-blink-merge.git] / third_party / cython / src / Cython / Utility / Builtins.c
blob537669d1a7de35db1b37035a44807df5ec38de97
1 /*
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.
7 */
9 //////////////////// Globals.proto ////////////////////
11 static PyObject* __Pyx_Globals(void); /*proto*/
13 //////////////////// Globals ////////////////////
14 //@substitute: naming
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) {
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;
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;
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;
59 #if CYTHON_COMPILING_IN_PYPY
60 Py_DECREF(name);
61 #endif
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;
74 //////////////////// PyExecGlobals.proto ////////////////////
76 static PyObject* __Pyx_PyExecGlobals(PyObject*);
78 //////////////////// PyExecGlobals ////////////////////
79 //@requires: Globals
80 //@requires: PyExec
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;
92 //////////////////// PyExec.proto ////////////////////
94 static PyObject* __Pyx_PyExec3(PyObject*, PyObject*, PyObject*);
95 static CYTHON_INLINE PyObject* __Pyx_PyExec2(PyObject*, PyObject*);
97 //////////////////// PyExec ////////////////////
98 //@substitute: naming
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) {
105 PyObject* result;
106 PyObject* s = 0;
107 char *code = 0;
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;
118 if (!locals || locals == Py_None) {
119 locals = globals;
122 if (PyDict_GetItem(globals, PYIDENT("__builtins__")) == NULL) {
123 if (PyDict_SetItem(globals, PYIDENT("__builtins__"), PyEval_GetBuiltins()) < 0)
124 goto bad;
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;
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;
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);
166 Py_XDECREF(s);
169 return result;
170 bad:
171 Py_XDECREF(s);
172 return 0;
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);
184 if (unlikely(!r)) {
185 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
186 goto bad;
187 PyErr_Clear();
188 r = d;
189 Py_INCREF(d);
191 return r;
192 bad:
193 return NULL;
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);
205 return 0;
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;
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) {
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_LONG_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
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);
266 else
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);
280 else
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);
294 else
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
335 #endif
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
349 #endif
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
363 #endif
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);
402 if (!ret) return -1;
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);
408 if (!ret) return -1;
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);
414 if (!ret) return -1;
415 Py_DECREF(ret); return 0;
418 #endif /* PyAnySet_CheckExact (<= Py2.4) */
419 #endif /* < Py2.5 */
421 //////////////////// pyfrozenset_new.proto ////////////////////
422 //@substitute: naming
423 //@requires: pyset_compat
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;
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);
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