intrinsics.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #define _PY_INTERPRETER
  2. #include "Python.h"
  3. #include "pycore_frame.h"
  4. #include "pycore_function.h"
  5. #include "pycore_runtime.h"
  6. #include "pycore_global_objects.h"
  7. #include "pycore_intrinsics.h"
  8. #include "pycore_pyerrors.h"
  9. #include "pycore_typevarobject.h"
  10. /******** Unary functions ********/
  11. static PyObject *
  12. no_intrinsic(PyThreadState* tstate, PyObject *unused)
  13. {
  14. _PyErr_SetString(tstate, PyExc_SystemError, "invalid intrinsic function");
  15. return NULL;
  16. }
  17. static PyObject *
  18. print_expr(PyThreadState* tstate, PyObject *value)
  19. {
  20. PyObject *hook = _PySys_GetAttr(tstate, &_Py_ID(displayhook));
  21. // Can't use ERROR_IF here.
  22. if (hook == NULL) {
  23. _PyErr_SetString(tstate, PyExc_RuntimeError,
  24. "lost sys.displayhook");
  25. return NULL;
  26. }
  27. return PyObject_CallOneArg(hook, value);
  28. }
  29. static int
  30. import_all_from(PyThreadState *tstate, PyObject *locals, PyObject *v)
  31. {
  32. PyObject *all, *dict, *name, *value;
  33. int skip_leading_underscores = 0;
  34. int pos, err;
  35. if (_PyObject_LookupAttr(v, &_Py_ID(__all__), &all) < 0) {
  36. return -1; /* Unexpected error */
  37. }
  38. if (all == NULL) {
  39. if (_PyObject_LookupAttr(v, &_Py_ID(__dict__), &dict) < 0) {
  40. return -1;
  41. }
  42. if (dict == NULL) {
  43. _PyErr_SetString(tstate, PyExc_ImportError,
  44. "from-import-* object has no __dict__ and no __all__");
  45. return -1;
  46. }
  47. all = PyMapping_Keys(dict);
  48. Py_DECREF(dict);
  49. if (all == NULL)
  50. return -1;
  51. skip_leading_underscores = 1;
  52. }
  53. for (pos = 0, err = 0; ; pos++) {
  54. name = PySequence_GetItem(all, pos);
  55. if (name == NULL) {
  56. if (!_PyErr_ExceptionMatches(tstate, PyExc_IndexError)) {
  57. err = -1;
  58. }
  59. else {
  60. _PyErr_Clear(tstate);
  61. }
  62. break;
  63. }
  64. if (!PyUnicode_Check(name)) {
  65. PyObject *modname = PyObject_GetAttr(v, &_Py_ID(__name__));
  66. if (modname == NULL) {
  67. Py_DECREF(name);
  68. err = -1;
  69. break;
  70. }
  71. if (!PyUnicode_Check(modname)) {
  72. _PyErr_Format(tstate, PyExc_TypeError,
  73. "module __name__ must be a string, not %.100s",
  74. Py_TYPE(modname)->tp_name);
  75. }
  76. else {
  77. _PyErr_Format(tstate, PyExc_TypeError,
  78. "%s in %U.%s must be str, not %.100s",
  79. skip_leading_underscores ? "Key" : "Item",
  80. modname,
  81. skip_leading_underscores ? "__dict__" : "__all__",
  82. Py_TYPE(name)->tp_name);
  83. }
  84. Py_DECREF(modname);
  85. Py_DECREF(name);
  86. err = -1;
  87. break;
  88. }
  89. if (skip_leading_underscores) {
  90. if (PyUnicode_READY(name) == -1) {
  91. Py_DECREF(name);
  92. err = -1;
  93. break;
  94. }
  95. if (PyUnicode_READ_CHAR(name, 0) == '_') {
  96. Py_DECREF(name);
  97. continue;
  98. }
  99. }
  100. value = PyObject_GetAttr(v, name);
  101. if (value == NULL)
  102. err = -1;
  103. else if (PyDict_CheckExact(locals))
  104. err = PyDict_SetItem(locals, name, value);
  105. else
  106. err = PyObject_SetItem(locals, name, value);
  107. Py_DECREF(name);
  108. Py_XDECREF(value);
  109. if (err < 0)
  110. break;
  111. }
  112. Py_DECREF(all);
  113. return err;
  114. }
  115. static PyObject *
  116. import_star(PyThreadState* tstate, PyObject *from)
  117. {
  118. _PyInterpreterFrame *frame = tstate->cframe->current_frame;
  119. if (_PyFrame_FastToLocalsWithError(frame) < 0) {
  120. return NULL;
  121. }
  122. PyObject *locals = frame->f_locals;
  123. if (locals == NULL) {
  124. _PyErr_SetString(tstate, PyExc_SystemError,
  125. "no locals found during 'import *'");
  126. return NULL;
  127. }
  128. int err = import_all_from(tstate, locals, from);
  129. _PyFrame_LocalsToFast(frame, 0);
  130. if (err < 0) {
  131. return NULL;
  132. }
  133. Py_RETURN_NONE;
  134. }
  135. static PyObject *
  136. stopiteration_error(PyThreadState* tstate, PyObject *exc)
  137. {
  138. _PyInterpreterFrame *frame = tstate->cframe->current_frame;
  139. assert(frame->owner == FRAME_OWNED_BY_GENERATOR);
  140. assert(PyExceptionInstance_Check(exc));
  141. const char *msg = NULL;
  142. if (PyErr_GivenExceptionMatches(exc, PyExc_StopIteration)) {
  143. msg = "generator raised StopIteration";
  144. if (frame->f_code->co_flags & CO_ASYNC_GENERATOR) {
  145. msg = "async generator raised StopIteration";
  146. }
  147. else if (frame->f_code->co_flags & CO_COROUTINE) {
  148. msg = "coroutine raised StopIteration";
  149. }
  150. }
  151. else if ((frame->f_code->co_flags & CO_ASYNC_GENERATOR) &&
  152. PyErr_GivenExceptionMatches(exc, PyExc_StopAsyncIteration))
  153. {
  154. /* code in `gen` raised a StopAsyncIteration error:
  155. raise a RuntimeError.
  156. */
  157. msg = "async generator raised StopAsyncIteration";
  158. }
  159. if (msg != NULL) {
  160. PyObject *message = _PyUnicode_FromASCII(msg, strlen(msg));
  161. if (message == NULL) {
  162. return NULL;
  163. }
  164. PyObject *error = PyObject_CallOneArg(PyExc_RuntimeError, message);
  165. if (error == NULL) {
  166. Py_DECREF(message);
  167. return NULL;
  168. }
  169. assert(PyExceptionInstance_Check(error));
  170. PyException_SetCause(error, Py_NewRef(exc));
  171. // Steal exc reference, rather than Py_NewRef+Py_DECREF
  172. PyException_SetContext(error, Py_NewRef(exc));
  173. Py_DECREF(message);
  174. return error;
  175. }
  176. return Py_NewRef(exc);
  177. }
  178. static PyObject *
  179. unary_pos(PyThreadState* unused, PyObject *value)
  180. {
  181. return PyNumber_Positive(value);
  182. }
  183. static PyObject *
  184. list_to_tuple(PyThreadState* unused, PyObject *v)
  185. {
  186. assert(PyList_Check(v));
  187. return _PyTuple_FromArray(((PyListObject *)v)->ob_item, Py_SIZE(v));
  188. }
  189. static PyObject *
  190. make_typevar(PyThreadState* Py_UNUSED(ignored), PyObject *v)
  191. {
  192. assert(PyUnicode_Check(v));
  193. return _Py_make_typevar(v, NULL, NULL);
  194. }
  195. const instrinsic_func1
  196. _PyIntrinsics_UnaryFunctions[] = {
  197. [0] = no_intrinsic,
  198. [INTRINSIC_PRINT] = print_expr,
  199. [INTRINSIC_IMPORT_STAR] = import_star,
  200. [INTRINSIC_STOPITERATION_ERROR] = stopiteration_error,
  201. [INTRINSIC_ASYNC_GEN_WRAP] = _PyAsyncGenValueWrapperNew,
  202. [INTRINSIC_UNARY_POSITIVE] = unary_pos,
  203. [INTRINSIC_LIST_TO_TUPLE] = list_to_tuple,
  204. [INTRINSIC_TYPEVAR] = make_typevar,
  205. [INTRINSIC_PARAMSPEC] = _Py_make_paramspec,
  206. [INTRINSIC_TYPEVARTUPLE] = _Py_make_typevartuple,
  207. [INTRINSIC_SUBSCRIPT_GENERIC] = _Py_subscript_generic,
  208. [INTRINSIC_TYPEALIAS] = _Py_make_typealias,
  209. };
  210. /******** Binary functions ********/
  211. static PyObject *
  212. prep_reraise_star(PyThreadState* unused, PyObject *orig, PyObject *excs)
  213. {
  214. assert(PyList_Check(excs));
  215. return _PyExc_PrepReraiseStar(orig, excs);
  216. }
  217. static PyObject *
  218. make_typevar_with_bound(PyThreadState* Py_UNUSED(ignored), PyObject *name,
  219. PyObject *evaluate_bound)
  220. {
  221. assert(PyUnicode_Check(name));
  222. return _Py_make_typevar(name, evaluate_bound, NULL);
  223. }
  224. static PyObject *
  225. make_typevar_with_constraints(PyThreadState* Py_UNUSED(ignored), PyObject *name,
  226. PyObject *evaluate_constraints)
  227. {
  228. assert(PyUnicode_Check(name));
  229. return _Py_make_typevar(name, NULL, evaluate_constraints);
  230. }
  231. const instrinsic_func2
  232. _PyIntrinsics_BinaryFunctions[] = {
  233. [INTRINSIC_PREP_RERAISE_STAR] = prep_reraise_star,
  234. [INTRINSIC_TYPEVAR_WITH_BOUND] = make_typevar_with_bound,
  235. [INTRINSIC_TYPEVAR_WITH_CONSTRAINTS] = make_typevar_with_constraints,
  236. [INTRINSIC_SET_FUNCTION_TYPE_PARAMS] = _Py_set_function_type_params,
  237. };