Builtins.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 PY_VERSION_HEX >= 0x030800A3
  115. cf.cf_feature_version = PY_MINOR_VERSION;
  116. #endif
  117. if (PyUnicode_Check(o)) {
  118. cf.cf_flags = PyCF_SOURCE_IS_UTF8;
  119. s = PyUnicode_AsUTF8String(o);
  120. if (!s) goto bad;
  121. o = s;
  122. #if PY_MAJOR_VERSION >= 3
  123. } else if (!PyBytes_Check(o)) {
  124. #else
  125. } else if (!PyString_Check(o)) {
  126. #endif
  127. PyErr_Format(PyExc_TypeError,
  128. "exec: arg 1 must be string, bytes or code object, got %.200s",
  129. Py_TYPE(o)->tp_name);
  130. goto bad;
  131. }
  132. #if PY_MAJOR_VERSION >= 3
  133. code = PyBytes_AS_STRING(o);
  134. #else
  135. code = PyString_AS_STRING(o);
  136. #endif
  137. if (PyEval_MergeCompilerFlags(&cf)) {
  138. result = PyRun_StringFlags(code, Py_file_input, globals, locals, &cf);
  139. } else {
  140. result = PyRun_String(code, Py_file_input, globals, locals);
  141. }
  142. Py_XDECREF(s);
  143. }
  144. return result;
  145. bad:
  146. Py_XDECREF(s);
  147. return 0;
  148. }
  149. //////////////////// GetAttr3.proto ////////////////////
  150. static CYTHON_INLINE PyObject *__Pyx_GetAttr3(PyObject *, PyObject *, PyObject *); /*proto*/
  151. //////////////////// GetAttr3 ////////////////////
  152. //@requires: ObjectHandling.c::GetAttr
  153. //@requires: Exceptions.c::PyThreadStateGet
  154. //@requires: Exceptions.c::PyErrFetchRestore
  155. //@requires: Exceptions.c::PyErrExceptionMatches
  156. static PyObject *__Pyx_GetAttr3Default(PyObject *d) {
  157. __Pyx_PyThreadState_declare
  158. __Pyx_PyThreadState_assign
  159. if (unlikely(!__Pyx_PyErr_ExceptionMatches(PyExc_AttributeError)))
  160. return NULL;
  161. __Pyx_PyErr_Clear();
  162. Py_INCREF(d);
  163. return d;
  164. }
  165. static CYTHON_INLINE PyObject *__Pyx_GetAttr3(PyObject *o, PyObject *n, PyObject *d) {
  166. PyObject *r = __Pyx_GetAttr(o, n);
  167. return (likely(r)) ? r : __Pyx_GetAttr3Default(d);
  168. }
  169. //////////////////// HasAttr.proto ////////////////////
  170. static CYTHON_INLINE int __Pyx_HasAttr(PyObject *, PyObject *); /*proto*/
  171. //////////////////// HasAttr ////////////////////
  172. //@requires: ObjectHandling.c::GetAttr
  173. static CYTHON_INLINE int __Pyx_HasAttr(PyObject *o, PyObject *n) {
  174. PyObject *r;
  175. if (unlikely(!__Pyx_PyBaseString_Check(n))) {
  176. PyErr_SetString(PyExc_TypeError,
  177. "hasattr(): attribute name must be string");
  178. return -1;
  179. }
  180. r = __Pyx_GetAttr(o, n);
  181. if (unlikely(!r)) {
  182. PyErr_Clear();
  183. return 0;
  184. } else {
  185. Py_DECREF(r);
  186. return 1;
  187. }
  188. }
  189. //////////////////// Intern.proto ////////////////////
  190. static PyObject* __Pyx_Intern(PyObject* s); /* proto */
  191. //////////////////// Intern ////////////////////
  192. static PyObject* __Pyx_Intern(PyObject* s) {
  193. if (!(likely(PyString_CheckExact(s)))) {
  194. PyErr_Format(PyExc_TypeError, "Expected %.16s, got %.200s", "str", Py_TYPE(s)->tp_name);
  195. return 0;
  196. }
  197. Py_INCREF(s);
  198. #if PY_MAJOR_VERSION >= 3
  199. PyUnicode_InternInPlace(&s);
  200. #else
  201. PyString_InternInPlace(&s);
  202. #endif
  203. return s;
  204. }
  205. //////////////////// abs_longlong.proto ////////////////////
  206. static CYTHON_INLINE PY_LONG_LONG __Pyx_abs_longlong(PY_LONG_LONG x) {
  207. #if defined (__cplusplus) && __cplusplus >= 201103L
  208. return std::abs(x);
  209. #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  210. return llabs(x);
  211. #elif defined (_MSC_VER)
  212. // abs() is defined for long, but 64-bits type on MSVC is long long.
  213. // Use MS-specific _abs64() instead, which returns the original (negative) value for abs(-MAX-1)
  214. return _abs64(x);
  215. #elif defined (__GNUC__)
  216. // gcc or clang on 64 bit windows.
  217. return __builtin_llabs(x);
  218. #else
  219. if (sizeof(PY_LONG_LONG) <= sizeof(Py_ssize_t))
  220. return __Pyx_sst_abs(x);
  221. return (x<0) ? -x : x;
  222. #endif
  223. }
  224. //////////////////// py_abs.proto ////////////////////
  225. #if CYTHON_USE_PYLONG_INTERNALS
  226. static PyObject *__Pyx_PyLong_AbsNeg(PyObject *num);/*proto*/
  227. #define __Pyx_PyNumber_Absolute(x) \
  228. ((likely(PyLong_CheckExact(x))) ? \
  229. (likely(Py_SIZE(x) >= 0) ? (Py_INCREF(x), (x)) : __Pyx_PyLong_AbsNeg(x)) : \
  230. PyNumber_Absolute(x))
  231. #else
  232. #define __Pyx_PyNumber_Absolute(x) PyNumber_Absolute(x)
  233. #endif
  234. //////////////////// py_abs ////////////////////
  235. #if CYTHON_USE_PYLONG_INTERNALS
  236. static PyObject *__Pyx_PyLong_AbsNeg(PyObject *n) {
  237. if (likely(Py_SIZE(n) == -1)) {
  238. // digits are unsigned
  239. return PyLong_FromLong(((PyLongObject*)n)->ob_digit[0]);
  240. }
  241. #if CYTHON_COMPILING_IN_CPYTHON
  242. {
  243. PyObject *copy = _PyLong_Copy((PyLongObject*)n);
  244. if (likely(copy)) {
  245. // negate the size to swap the sign
  246. __Pyx_SET_SIZE(copy, -Py_SIZE(copy));
  247. }
  248. return copy;
  249. }
  250. #else
  251. return PyNumber_Negative(n);
  252. #endif
  253. }
  254. #endif
  255. //////////////////// pow2.proto ////////////////////
  256. #define __Pyx_PyNumber_Power2(a, b) PyNumber_Power(a, b, Py_None)
  257. //////////////////// object_ord.proto ////////////////////
  258. //@requires: TypeConversion.c::UnicodeAsUCS4
  259. #if PY_MAJOR_VERSION >= 3
  260. #define __Pyx_PyObject_Ord(c) \
  261. (likely(PyUnicode_Check(c)) ? (long)__Pyx_PyUnicode_AsPy_UCS4(c) : __Pyx__PyObject_Ord(c))
  262. #else
  263. #define __Pyx_PyObject_Ord(c) __Pyx__PyObject_Ord(c)
  264. #endif
  265. static long __Pyx__PyObject_Ord(PyObject* c); /*proto*/
  266. //////////////////// object_ord ////////////////////
  267. static long __Pyx__PyObject_Ord(PyObject* c) {
  268. Py_ssize_t size;
  269. if (PyBytes_Check(c)) {
  270. size = PyBytes_GET_SIZE(c);
  271. if (likely(size == 1)) {
  272. return (unsigned char) PyBytes_AS_STRING(c)[0];
  273. }
  274. #if PY_MAJOR_VERSION < 3
  275. } else if (PyUnicode_Check(c)) {
  276. return (long)__Pyx_PyUnicode_AsPy_UCS4(c);
  277. #endif
  278. #if (!CYTHON_COMPILING_IN_PYPY) || (defined(PyByteArray_AS_STRING) && defined(PyByteArray_GET_SIZE))
  279. } else if (PyByteArray_Check(c)) {
  280. size = PyByteArray_GET_SIZE(c);
  281. if (likely(size == 1)) {
  282. return (unsigned char) PyByteArray_AS_STRING(c)[0];
  283. }
  284. #endif
  285. } else {
  286. // FIXME: support character buffers - but CPython doesn't support them either
  287. PyErr_Format(PyExc_TypeError,
  288. "ord() expected string of length 1, but %.200s found", Py_TYPE(c)->tp_name);
  289. return (long)(Py_UCS4)-1;
  290. }
  291. PyErr_Format(PyExc_TypeError,
  292. "ord() expected a character, but string of length %zd found", size);
  293. return (long)(Py_UCS4)-1;
  294. }
  295. //////////////////// py_dict_keys.proto ////////////////////
  296. static CYTHON_INLINE PyObject* __Pyx_PyDict_Keys(PyObject* d); /*proto*/
  297. //////////////////// py_dict_keys ////////////////////
  298. static CYTHON_INLINE PyObject* __Pyx_PyDict_Keys(PyObject* d) {
  299. if (PY_MAJOR_VERSION >= 3)
  300. return CALL_UNBOUND_METHOD(PyDict_Type, "keys", d);
  301. else
  302. return PyDict_Keys(d);
  303. }
  304. //////////////////// py_dict_values.proto ////////////////////
  305. static CYTHON_INLINE PyObject* __Pyx_PyDict_Values(PyObject* d); /*proto*/
  306. //////////////////// py_dict_values ////////////////////
  307. static CYTHON_INLINE PyObject* __Pyx_PyDict_Values(PyObject* d) {
  308. if (PY_MAJOR_VERSION >= 3)
  309. return CALL_UNBOUND_METHOD(PyDict_Type, "values", d);
  310. else
  311. return PyDict_Values(d);
  312. }
  313. //////////////////// py_dict_items.proto ////////////////////
  314. static CYTHON_INLINE PyObject* __Pyx_PyDict_Items(PyObject* d); /*proto*/
  315. //////////////////// py_dict_items ////////////////////
  316. static CYTHON_INLINE PyObject* __Pyx_PyDict_Items(PyObject* d) {
  317. if (PY_MAJOR_VERSION >= 3)
  318. return CALL_UNBOUND_METHOD(PyDict_Type, "items", d);
  319. else
  320. return PyDict_Items(d);
  321. }
  322. //////////////////// py_dict_iterkeys.proto ////////////////////
  323. static CYTHON_INLINE PyObject* __Pyx_PyDict_IterKeys(PyObject* d); /*proto*/
  324. //////////////////// py_dict_iterkeys ////////////////////
  325. static CYTHON_INLINE PyObject* __Pyx_PyDict_IterKeys(PyObject* d) {
  326. if (PY_MAJOR_VERSION >= 3)
  327. return CALL_UNBOUND_METHOD(PyDict_Type, "keys", d);
  328. else
  329. return CALL_UNBOUND_METHOD(PyDict_Type, "iterkeys", d);
  330. }
  331. //////////////////// py_dict_itervalues.proto ////////////////////
  332. static CYTHON_INLINE PyObject* __Pyx_PyDict_IterValues(PyObject* d); /*proto*/
  333. //////////////////// py_dict_itervalues ////////////////////
  334. static CYTHON_INLINE PyObject* __Pyx_PyDict_IterValues(PyObject* d) {
  335. if (PY_MAJOR_VERSION >= 3)
  336. return CALL_UNBOUND_METHOD(PyDict_Type, "values", d);
  337. else
  338. return CALL_UNBOUND_METHOD(PyDict_Type, "itervalues", d);
  339. }
  340. //////////////////// py_dict_iteritems.proto ////////////////////
  341. static CYTHON_INLINE PyObject* __Pyx_PyDict_IterItems(PyObject* d); /*proto*/
  342. //////////////////// py_dict_iteritems ////////////////////
  343. static CYTHON_INLINE PyObject* __Pyx_PyDict_IterItems(PyObject* d) {
  344. if (PY_MAJOR_VERSION >= 3)
  345. return CALL_UNBOUND_METHOD(PyDict_Type, "items", d);
  346. else
  347. return CALL_UNBOUND_METHOD(PyDict_Type, "iteritems", d);
  348. }
  349. //////////////////// py_dict_viewkeys.proto ////////////////////
  350. #if PY_VERSION_HEX < 0x02070000
  351. #error This module uses dict views, which require Python 2.7 or later
  352. #endif
  353. static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewKeys(PyObject* d); /*proto*/
  354. //////////////////// py_dict_viewkeys ////////////////////
  355. static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewKeys(PyObject* d) {
  356. if (PY_MAJOR_VERSION >= 3)
  357. return CALL_UNBOUND_METHOD(PyDict_Type, "keys", d);
  358. else
  359. return CALL_UNBOUND_METHOD(PyDict_Type, "viewkeys", d);
  360. }
  361. //////////////////// py_dict_viewvalues.proto ////////////////////
  362. #if PY_VERSION_HEX < 0x02070000
  363. #error This module uses dict views, which require Python 2.7 or later
  364. #endif
  365. static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewValues(PyObject* d); /*proto*/
  366. //////////////////// py_dict_viewvalues ////////////////////
  367. static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewValues(PyObject* d) {
  368. if (PY_MAJOR_VERSION >= 3)
  369. return CALL_UNBOUND_METHOD(PyDict_Type, "values", d);
  370. else
  371. return CALL_UNBOUND_METHOD(PyDict_Type, "viewvalues", d);
  372. }
  373. //////////////////// py_dict_viewitems.proto ////////////////////
  374. #if PY_VERSION_HEX < 0x02070000
  375. #error This module uses dict views, which require Python 2.7 or later
  376. #endif
  377. static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewItems(PyObject* d); /*proto*/
  378. //////////////////// py_dict_viewitems ////////////////////
  379. static CYTHON_INLINE PyObject* __Pyx_PyDict_ViewItems(PyObject* d) {
  380. if (PY_MAJOR_VERSION >= 3)
  381. return CALL_UNBOUND_METHOD(PyDict_Type, "items", d);
  382. else
  383. return CALL_UNBOUND_METHOD(PyDict_Type, "viewitems", d);
  384. }
  385. //////////////////// pyfrozenset_new.proto ////////////////////
  386. static CYTHON_INLINE PyObject* __Pyx_PyFrozenSet_New(PyObject* it);
  387. //////////////////// pyfrozenset_new ////////////////////
  388. //@substitute: naming
  389. static CYTHON_INLINE PyObject* __Pyx_PyFrozenSet_New(PyObject* it) {
  390. if (it) {
  391. PyObject* result;
  392. #if CYTHON_COMPILING_IN_PYPY
  393. // PyPy currently lacks PyFrozenSet_CheckExact() and PyFrozenSet_New()
  394. PyObject* args;
  395. args = PyTuple_Pack(1, it);
  396. if (unlikely(!args))
  397. return NULL;
  398. result = PyObject_Call((PyObject*)&PyFrozenSet_Type, args, NULL);
  399. Py_DECREF(args);
  400. return result;
  401. #else
  402. if (PyFrozenSet_CheckExact(it)) {
  403. Py_INCREF(it);
  404. return it;
  405. }
  406. result = PyFrozenSet_New(it);
  407. if (unlikely(!result))
  408. return NULL;
  409. if ((PY_VERSION_HEX >= 0x031000A1) || likely(PySet_GET_SIZE(result)))
  410. return result;
  411. // empty frozenset is a singleton (on Python <3.10)
  412. // seems wasteful, but CPython does the same
  413. Py_DECREF(result);
  414. #endif
  415. }
  416. #if CYTHON_USE_TYPE_SLOTS
  417. return PyFrozenSet_Type.tp_new(&PyFrozenSet_Type, $empty_tuple, NULL);
  418. #else
  419. return PyObject_Call((PyObject*)&PyFrozenSet_Type, $empty_tuple, NULL);
  420. #endif
  421. }
  422. //////////////////// PySet_Update.proto ////////////////////
  423. static CYTHON_INLINE int __Pyx_PySet_Update(PyObject* set, PyObject* it); /*proto*/
  424. //////////////////// PySet_Update ////////////////////
  425. static CYTHON_INLINE int __Pyx_PySet_Update(PyObject* set, PyObject* it) {
  426. PyObject *retval;
  427. #if CYTHON_USE_TYPE_SLOTS && !CYTHON_COMPILING_IN_PYPY
  428. if (PyAnySet_Check(it)) {
  429. if (PySet_GET_SIZE(it) == 0)
  430. return 0;
  431. // fast and safe case: CPython will update our result set and return it
  432. retval = PySet_Type.tp_as_number->nb_inplace_or(set, it);
  433. if (likely(retval == set)) {
  434. Py_DECREF(retval);
  435. return 0;
  436. }
  437. if (unlikely(!retval))
  438. return -1;
  439. // unusual result, fall through to set.update() call below
  440. Py_DECREF(retval);
  441. }
  442. #endif
  443. retval = CALL_UNBOUND_METHOD(PySet_Type, "update", set, it);
  444. if (unlikely(!retval)) return -1;
  445. Py_DECREF(retval);
  446. return 0;
  447. }