Builtins.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  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. //////////////////// Globals.proto ////////////////////
  9. static PyObject* __Pyx_Globals(void); /*proto*/
  10. //////////////////// Globals ////////////////////
  11. //@substitute: naming
  12. //@requires: ObjectHandling.c::GetAttr
  13. // This is a stub implementation until we have something more complete.
  14. // Currently, we only handle the most common case of a read-only dict
  15. // of Python names. Supporting cdef names in the module and write
  16. // access requires a rewrite as a dedicated class.
  17. static PyObject* __Pyx_Globals(void) {
  18. Py_ssize_t i;
  19. PyObject *names;
  20. PyObject *globals = $moddict_cname;
  21. Py_INCREF(globals);
  22. names = PyObject_Dir($module_cname);
  23. if (!names)
  24. goto bad;
  25. for (i = PyList_GET_SIZE(names)-1; i >= 0; i--) {
  26. #if CYTHON_COMPILING_IN_PYPY
  27. PyObject* name = PySequence_ITEM(names, i);
  28. if (!name)
  29. goto bad;
  30. #else
  31. PyObject* name = PyList_GET_ITEM(names, i);
  32. #endif
  33. if (!PyDict_Contains(globals, name)) {
  34. PyObject* value = __Pyx_GetAttr($module_cname, name);
  35. if (!value) {
  36. #if CYTHON_COMPILING_IN_PYPY
  37. Py_DECREF(name);
  38. #endif
  39. goto bad;
  40. }
  41. if (PyDict_SetItem(globals, name, value) < 0) {
  42. #if CYTHON_COMPILING_IN_PYPY
  43. Py_DECREF(name);
  44. #endif
  45. Py_DECREF(value);
  46. goto bad;
  47. }
  48. }
  49. #if CYTHON_COMPILING_IN_PYPY
  50. Py_DECREF(name);
  51. #endif
  52. }
  53. Py_DECREF(names);
  54. return globals;
  55. bad:
  56. Py_XDECREF(names);
  57. Py_XDECREF(globals);
  58. return NULL;
  59. }
  60. //////////////////// PyExecGlobals.proto ////////////////////
  61. static PyObject* __Pyx_PyExecGlobals(PyObject*);
  62. //////////////////// PyExecGlobals ////////////////////
  63. //@requires: Globals
  64. //@requires: PyExec
  65. static PyObject* __Pyx_PyExecGlobals(PyObject* code) {
  66. PyObject* result;
  67. PyObject* globals = __Pyx_Globals();
  68. if (unlikely(!globals))
  69. return NULL;
  70. result = __Pyx_PyExec2(code, globals);
  71. Py_DECREF(globals);
  72. return result;
  73. }
  74. //////////////////// PyExec.proto ////////////////////
  75. static PyObject* __Pyx_PyExec3(PyObject*, PyObject*, PyObject*);
  76. static CYTHON_INLINE PyObject* __Pyx_PyExec2(PyObject*, PyObject*);
  77. //////////////////// PyExec ////////////////////
  78. //@substitute: naming
  79. static CYTHON_INLINE PyObject* __Pyx_PyExec2(PyObject* o, PyObject* globals) {
  80. return __Pyx_PyExec3(o, globals, NULL);
  81. }
  82. static PyObject* __Pyx_PyExec3(PyObject* o, PyObject* globals, PyObject* locals) {
  83. PyObject* result;
  84. PyObject* s = 0;
  85. char *code = 0;
  86. if (!globals || globals == Py_None) {
  87. globals = $moddict_cname;
  88. } else if (!PyDict_Check(globals)) {
  89. PyErr_Format(PyExc_TypeError, "exec() arg 2 must be a dict, not %.200s",
  90. Py_TYPE(globals)->tp_name);
  91. goto bad;
  92. }
  93. if (!locals || locals == Py_None) {
  94. locals = globals;
  95. }
  96. if (__Pyx_PyDict_GetItemStr(globals, PYIDENT("__builtins__")) == NULL) {
  97. if (PyDict_SetItem(globals, PYIDENT("__builtins__"), PyEval_GetBuiltins()) < 0)
  98. goto bad;
  99. }
  100. if (PyCode_Check(o)) {
  101. if (__Pyx_PyCode_HasFreeVars((PyCodeObject *)o)) {
  102. PyErr_SetString(PyExc_TypeError,
  103. "code object passed to exec() may not contain free variables");
  104. goto bad;
  105. }
  106. #if CYTHON_COMPILING_IN_PYPY || PY_VERSION_HEX < 0x030200B1
  107. result = PyEval_EvalCode((PyCodeObject *)o, globals, locals);
  108. #else
  109. result = PyEval_EvalCode(o, globals, locals);
  110. #endif
  111. } else {
  112. PyCompilerFlags cf;
  113. cf.cf_flags = 0;
  114. if (PyUnicode_Check(o)) {
  115. cf.cf_flags = PyCF_SOURCE_IS_UTF8;
  116. s = PyUnicode_AsUTF8String(o);
  117. if (!s) goto bad;
  118. o = s;
  119. #if PY_MAJOR_VERSION >= 3
  120. } else if (!PyBytes_Check(o)) {
  121. #else
  122. } else if (!PyString_Check(o)) {
  123. #endif
  124. PyErr_Format(PyExc_TypeError,
  125. "exec: arg 1 must be string, bytes or code object, got %.200s",
  126. Py_TYPE(o)->tp_name);
  127. goto bad;
  128. }
  129. #if PY_MAJOR_VERSION >= 3
  130. code = PyBytes_AS_STRING(o);
  131. #else
  132. code = PyString_AS_STRING(o);
  133. #endif
  134. if (PyEval_MergeCompilerFlags(&cf)) {
  135. result = PyRun_StringFlags(code, Py_file_input, globals, locals, &cf);
  136. } else {
  137. result = PyRun_String(code, Py_file_input, globals, locals);
  138. }
  139. Py_XDECREF(s);
  140. }
  141. return result;
  142. bad:
  143. Py_XDECREF(s);
  144. return 0;
  145. }
  146. //////////////////// GetAttr3.proto ////////////////////
  147. static CYTHON_INLINE PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *); /*proto*/
  148. //////////////////// GetAttr3 ////////////////////
  149. //@requires: ObjectHandling.c::GetAttr
  150. //@requires: Exceptions.c::PyThreadStateGet
  151. //@requires: Exceptions.c::PyErrFetchRestore
  152. //@requires: Exceptions.c::PyErrExceptionMatches
  153. static PyObject *__Pyx_GetAttr3Default(PyObject *d) {
  154. __Pyx_PyThreadState_declare
  155. __Pyx_PyThreadState_assign
  156. if (unlikely(!__Pyx_PyErr_ExceptionMatches(PyExc_AttributeError)))
  157. return NULL;
  158. __Pyx_PyErr_Clear();
  159. Py_INCREF(d);
  160. return d;
  161. }
  162. static CYTHON_INLINE PyObject *__Pyx_GetAttr3(PyObject *o, PyObject *n, PyObject *d) {
  163. PyObject *r = __Pyx_GetAttr(o, n);
  164. return (likely(r)) ? r : __Pyx_GetAttr3Default(d);
  165. }
  166. //////////////////// HasAttr.proto ////////////////////
  167. static CYTHON_INLINE int __Pyx_HasAttr(PyObject *, PyObject *); /*proto*/
  168. //////////////////// HasAttr ////////////////////
  169. //@requires: ObjectHandling.c::GetAttr
  170. static CYTHON_INLINE int __Pyx_HasAttr(PyObject *o, PyObject *n) {
  171. PyObject *r;
  172. if (unlikely(!__Pyx_PyBaseString_Check(n))) {
  173. PyErr_SetString(PyExc_TypeError,
  174. "hasattr(): attribute name must be string");
  175. return -1;
  176. }
  177. r = __Pyx_GetAttr(o, n);
  178. if (unlikely(!r)) {
  179. PyErr_Clear();
  180. return 0;
  181. } else {
  182. Py_DECREF(r);
  183. return 1;
  184. }
  185. }
  186. //////////////////// Intern.proto ////////////////////
  187. static PyObject* __Pyx_Intern(PyObject* s); /* proto */
  188. //////////////////// Intern ////////////////////
  189. static PyObject* __Pyx_Intern(PyObject* s) {
  190. if (!(likely(PyString_CheckExact(s)))) {
  191. PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(s)->tp_name);
  192. return 0;
  193. }
  194. Py_INCREF(s);
  195. #if PY_MAJOR_VERSION >= 3
  196. PyUnicode_InternInPlace(&s);
  197. #else
  198. PyString_InternInPlace(&s);
  199. #endif
  200. return s;
  201. }
  202. //////////////////// abs_longlong.proto ////////////////////
  203. static CYTHON_INLINE PY_LONG_LONG __Pyx_abs_longlong(PY_LONG_LONG x) {
  204. #if defined (__cplusplus) && __cplusplus >= 201103L
  205. return std::abs(x);
  206. #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  207. return llabs(x);
  208. #elif defined (_MSC_VER)
  209. // abs() is defined for long, but 64-bits type on MSVC is long long.
  210. // Use MS-specific _abs64() instead, which returns the original (negative) value for abs(-MAX-1)
  211. return _abs64(x);
  212. #elif defined (__GNUC__)
  213. // gcc or clang on 64 bit windows.
  214. return __builtin_llabs(x);
  215. #else
  216. if (sizeof(PY_LONG_LONG) <= sizeof(Py_ssize_t))
  217. return __Pyx_sst_abs(x);
  218. return (x<0) ? -x : x;
  219. #endif
  220. }
  221. //////////////////// py_abs.proto ////////////////////
  222. #if CYTHON_USE_PYLONG_INTERNALS
  223. static PyObject *__Pyx_PyLong_AbsNeg(PyObject *num);/*proto*/
  224. #define __Pyx_PyNumber_Absolute(x) \
  225. ((likely(PyLong_CheckExact(x))) ? \
  226. (likely(Py_SIZE(x) >= 0) ? (Py_INCREF(x), (x)) : __Pyx_PyLong_AbsNeg(x)) : \
  227. PyNumber_Absolute(x))
  228. #else
  229. #define __Pyx_PyNumber_Absolute(x) PyNumber_Absolute(x)
  230. #endif
  231. //////////////////// py_abs ////////////////////
  232. #if CYTHON_USE_PYLONG_INTERNALS
  233. static PyObject *__Pyx_PyLong_AbsNeg(PyObject *n) {
  234. if (likely(Py_SIZE(n) == -1)) {
  235. // digits are unsigned
  236. return PyLong_FromLong(((PyLongObject*)n)->ob_digit[0]);
  237. }
  238. #if CYTHON_COMPILING_IN_CPYTHON
  239. {
  240. PyObject *copy = _PyLong_Copy((PyLongObject*)n);
  241. if (likely(copy)) {
  242. Py_SIZE(copy) = -(Py_SIZE(copy));
  243. }
  244. return copy;
  245. }
  246. #else
  247. return PyNumber_Negative(n);
  248. #endif
  249. }
  250. #endif
  251. //////////////////// pow2.proto ////////////////////
  252. #define __Pyx_PyNumber_Power2(a, b) PyNumber_Power(a, b, Py_None)
  253. //////////////////// object_ord.proto ////////////////////
  254. //@requires: TypeConversion.c::UnicodeAsUCS4
  255. #if PY_MAJOR_VERSION >= 3
  256. #define __Pyx_PyObject_Ord(c) \
  257. (likely(PyUnicode_Check(c)) ? (long)__Pyx_PyUnicode_AsPy_UCS4(c) : __Pyx__PyObject_Ord(c))
  258. #else
  259. #define __Pyx_PyObject_Ord(c) __Pyx__PyObject_Ord(c)
  260. #endif
  261. static long __Pyx__PyObject_Ord(PyObject* c); /*proto*/
  262. //////////////////// object_ord ////////////////////
  263. static long __Pyx__PyObject_Ord(PyObject* c) {
  264. Py_ssize_t size;
  265. if (PyBytes_Check(c)) {
  266. size = PyBytes_GET_SIZE(c);
  267. if (likely(size == 1)) {
  268. return (unsigned char) PyBytes_AS_STRING(c)[0];
  269. }
  270. #if PY_MAJOR_VERSION < 3
  271. } else if (PyUnicode_Check(c)) {
  272. return (long)__Pyx_PyUnicode_AsPy_UCS4(c);
  273. #endif
  274. #if (!CYTHON_COMPILING_IN_PYPY) || (defined(PyByteArray_AS_STRING) && defined(PyByteArray_GET_SIZE))
  275. } else if (PyByteArray_Check(c)) {
  276. size = PyByteArray_GET_SIZE(c);
  277. if (likely(size == 1)) {
  278. return (unsigned char) PyByteArray_AS_STRING(c)[0];
  279. }
  280. #endif
  281. } else {
  282. // FIXME: support character buffers - but CPython doesn't support them either
  283. PyErr_Format(PyExc_TypeError,
  284. "ord() expected string of length 1, but %.200s found", c->ob_type->tp_name);
  285. return (long)(Py_UCS4)-1;
  286. }
  287. PyErr_Format(PyExc_TypeError,
  288. "ord() expected a character, but string of length %zd found", size);
  289. return (long)(Py_UCS4)-1;
  290. }
  291. //////////////////// py_dict_keys.proto ////////////////////
  292. static CYTHON_INLINE PyObject* __Pyx_PyDict_Keys(PyObject* d); /*proto*/
  293. //////////////////// py_dict_keys ////////////////////
  294. static CYTHON_INLINE PyObject* __Pyx_PyDict_Keys(PyObject* d) {
  295. if (PY_MAJOR_VERSION >= 3)
  296. return CALL_UNBOUND_METHOD(PyDict_Type, "keys", d);
  297. else
  298. return PyDict_Keys(d);
  299. }
  300. //////////////////// py_dict_values.proto ////////////////////
  301. static CYTHON_INLINE PyObject* __Pyx_PyDict_Values(PyObject* d); /*proto*/
  302. //////////////////// py_dict_values ////////////////////
  303. static CYTHON_INLINE PyObject* __Pyx_PyDict_Values(PyObject* d) {
  304. if (PY_MAJOR_VERSION >= 3)
  305. return CALL_UNBOUND_METHOD(PyDict_Type, "values", d);
  306. else
  307. return PyDict_Values(d);
  308. }
  309. //////////////////// py_dict_items.proto ////////////////////
  310. static CYTHON_INLINE PyObject* __Pyx_PyDict_Items(PyObject* d); /*proto*/
  311. //////////////////// py_dict_items ////////////////////
  312. static CYTHON_INLINE PyObject* __Pyx_PyDict_Items(PyObject* d) {
  313. if (PY_MAJOR_VERSION >= 3)
  314. return CALL_UNBOUND_METHOD(PyDict_Type, "items", d);
  315. else
  316. return PyDict_Items(d);
  317. }
  318. //////////////////// py_dict_iterkeys.proto ////////////////////
  319. static CYTHON_INLINE PyObject* __Pyx_PyDict_IterKeys(PyObject* d); /*proto*/
  320. //////////////////// py_dict_iterkeys ////////////////////
  321. static CYTHON_INLINE PyObject* __Pyx_PyDict_IterKeys(PyObject* d) {
  322. if (PY_MAJOR_VERSION >= 3)
  323. return CALL_UNBOUND_METHOD(PyDict_Type, "keys", d);
  324. else
  325. return CALL_UNBOUND_METHOD(PyDict_Type, "iterkeys", d);
  326. }
  327. //////////////////// py_dict_itervalues.proto ////////////////////
  328. static CYTHON_INLINE PyObject* __Pyx_PyDict_IterValues(PyObject* d); /*proto*/
  329. //////////////////// py_dict_itervalues ////////////////////
  330. static CYTHON_INLINE PyObject* __Pyx_PyDict_IterValues(PyObject* d) {
  331. if (PY_MAJOR_VERSION >= 3)
  332. return CALL_UNBOUND_METHOD(PyDict_Type, "values", d);
  333. else
  334. return CALL_UNBOUND_METHOD(PyDict_Type, "itervalues", d);
  335. }
  336. //////////////////// py_dict_iteritems.proto ////////////////////
  337. static CYTHON_INLINE PyObject* __Pyx_PyDict_IterItems(PyObject* d); /*proto*/
  338. //////////////////// py_dict_iteritems ////////////////////
  339. static CYTHON_INLINE PyObject* __Pyx_PyDict_IterItems(PyObject* d) {
  340. if (PY_MAJOR_VERSION >= 3)
  341. return CALL_UNBOUND_METHOD(PyDict_Type, "items", d);
  342. else
  343. return CALL_UNBOUND_METHOD(PyDict_Type, "iteritems", d);
  344. }
  345. //////////////////// py_dict_viewkeys.proto ////////////////////
  346. #if PY_VERSION_HEX < 0x02070000
  347. #error This module uses dict views, which require Python 2.7 or later
  348. #endif
  349. static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewKeys(PyObject* d); /*proto*/
  350. //////////////////// py_dict_viewkeys ////////////////////
  351. static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewKeys(PyObject* d) {
  352. if (PY_MAJOR_VERSION >= 3)
  353. return CALL_UNBOUND_METHOD(PyDict_Type, "keys", d);
  354. else
  355. return CALL_UNBOUND_METHOD(PyDict_Type, "viewkeys", d);
  356. }
  357. //////////////////// py_dict_viewvalues.proto ////////////////////
  358. #if PY_VERSION_HEX < 0x02070000
  359. #error This module uses dict views, which require Python 2.7 or later
  360. #endif
  361. static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewValues(PyObject* d); /*proto*/
  362. //////////////////// py_dict_viewvalues ////////////////////
  363. static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewValues(PyObject* d) {
  364. if (PY_MAJOR_VERSION >= 3)
  365. return CALL_UNBOUND_METHOD(PyDict_Type, "values", d);
  366. else
  367. return CALL_UNBOUND_METHOD(PyDict_Type, "viewvalues", d);
  368. }
  369. //////////////////// py_dict_viewitems.proto ////////////////////
  370. #if PY_VERSION_HEX < 0x02070000
  371. #error This module uses dict views, which require Python 2.7 or later
  372. #endif
  373. static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewItems(PyObject* d); /*proto*/
  374. //////////////////// py_dict_viewitems ////////////////////
  375. static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewItems(PyObject* d) {
  376. if (PY_MAJOR_VERSION >= 3)
  377. return CALL_UNBOUND_METHOD(PyDict_Type, "items", d);
  378. else
  379. return CALL_UNBOUND_METHOD(PyDict_Type, "viewitems", d);
  380. }
  381. //////////////////// pyfrozenset_new.proto ////////////////////
  382. static CYTHON_INLINE PyObject* __Pyx_PyFrozenSet_New(PyObject* it);
  383. //////////////////// pyfrozenset_new ////////////////////
  384. //@substitute: naming
  385. static CYTHON_INLINE PyObject* __Pyx_PyFrozenSet_New(PyObject* it) {
  386. if (it) {
  387. PyObject* result;
  388. #if CYTHON_COMPILING_IN_PYPY
  389. // PyPy currently lacks PyFrozenSet_CheckExact() and PyFrozenSet_New()
  390. PyObject* args;
  391. args = PyTuple_Pack(1, it);
  392. if (unlikely(!args))
  393. return NULL;
  394. result = PyObject_Call((PyObject*)&PyFrozenSet_Type, args, NULL);
  395. Py_DECREF(args);
  396. return result;
  397. #else
  398. if (PyFrozenSet_CheckExact(it)) {
  399. Py_INCREF(it);
  400. return it;
  401. }
  402. result = PyFrozenSet_New(it);
  403. if (unlikely(!result))
  404. return NULL;
  405. if (likely(PySet_GET_SIZE(result)))
  406. return result;
  407. // empty frozenset is a singleton
  408. // seems wasteful, but CPython does the same
  409. Py_DECREF(result);
  410. #endif
  411. }
  412. #if CYTHON_USE_TYPE_SLOTS
  413. return PyFrozenSet_Type.tp_new(&PyFrozenSet_Type, $empty_tuple, NULL);
  414. #else
  415. return PyObject_Call((PyObject*)&PyFrozenSet_Type, $empty_tuple, NULL);
  416. #endif
  417. }
  418. //////////////////// PySet_Update.proto ////////////////////
  419. static CYTHON_INLINE int __Pyx_PySet_Update(PyObject* set, PyObject* it); /*proto*/
  420. //////////////////// PySet_Update ////////////////////
  421. static CYTHON_INLINE int __Pyx_PySet_Update(PyObject* set, PyObject* it) {
  422. PyObject *retval;
  423. #if CYTHON_USE_TYPE_SLOTS && !CYTHON_COMPILING_IN_PYPY
  424. if (PyAnySet_Check(it)) {
  425. if (PySet_GET_SIZE(it) == 0)
  426. return 0;
  427. // fast and safe case: CPython will update our result set and return it
  428. retval = PySet_Type.tp_as_number->nb_inplace_or(set, it);
  429. if (likely(retval == set)) {
  430. Py_DECREF(retval);
  431. return 0;
  432. }
  433. if (unlikely(!retval))
  434. return -1;
  435. // unusual result, fall through to set.update() call below
  436. Py_DECREF(retval);
  437. }
  438. #endif
  439. retval = CALL_UNBOUND_METHOD(PySet_Type, "update", set, it);
  440. if (unlikely(!retval)) return -1;
  441. Py_DECREF(retval);
  442. return 0;
  443. }