pythonrun.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020
  1. /* Top level execution of Python code (including in __main__) */
  2. /* To help control the interfaces between the startup, execution and
  3. * shutdown code, the phases are split across separate modules (bootstrap,
  4. * pythonrun, shutdown)
  5. */
  6. /* TODO: Cull includes following phase split */
  7. #include <stdbool.h>
  8. #include "Python.h"
  9. #include "pycore_ast.h" // PyAST_mod2obj
  10. #include "pycore_ceval.h" // _Py_EnterRecursiveCall
  11. #include "pycore_compile.h" // _PyAST_Compile()
  12. #include "pycore_interp.h" // PyInterpreterState.importlib
  13. #include "pycore_object.h" // _PyDebug_PrintTotalRefs()
  14. #include "pycore_parser.h" // _PyParser_ASTFromString()
  15. #include "pycore_pyerrors.h" // _PyErr_GetRaisedException, _Py_Offer_Suggestions
  16. #include "pycore_pylifecycle.h" // _Py_UnhandledKeyboardInterrupt
  17. #include "pycore_pystate.h" // _PyInterpreterState_GET()
  18. #include "pycore_sysmodule.h" // _PySys_Audit()
  19. #include "pycore_traceback.h" // _PyTraceBack_Print_Indented()
  20. #include "errcode.h" // E_EOF
  21. #include "marshal.h" // PyMarshal_ReadLongFromFile()
  22. #ifdef MS_WINDOWS
  23. # include "malloc.h" // alloca()
  24. #endif
  25. #ifdef MS_WINDOWS
  26. # undef BYTE
  27. # include "windows.h"
  28. #endif
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /* Forward */
  33. static void flush_io(void);
  34. static PyObject *run_mod(mod_ty, PyObject *, PyObject *, PyObject *,
  35. PyCompilerFlags *, PyArena *);
  36. static PyObject *run_pyc_file(FILE *, PyObject *, PyObject *,
  37. PyCompilerFlags *);
  38. static int PyRun_InteractiveOneObjectEx(FILE *, PyObject *, PyCompilerFlags *);
  39. static PyObject* pyrun_file(FILE *fp, PyObject *filename, int start,
  40. PyObject *globals, PyObject *locals, int closeit,
  41. PyCompilerFlags *flags);
  42. int
  43. _PyRun_AnyFileObject(FILE *fp, PyObject *filename, int closeit,
  44. PyCompilerFlags *flags)
  45. {
  46. int decref_filename = 0;
  47. if (filename == NULL) {
  48. filename = PyUnicode_FromString("???");
  49. if (filename == NULL) {
  50. PyErr_Print();
  51. return -1;
  52. }
  53. decref_filename = 1;
  54. }
  55. int res;
  56. if (_Py_FdIsInteractive(fp, filename)) {
  57. res = _PyRun_InteractiveLoopObject(fp, filename, flags);
  58. if (closeit) {
  59. fclose(fp);
  60. }
  61. }
  62. else {
  63. res = _PyRun_SimpleFileObject(fp, filename, closeit, flags);
  64. }
  65. if (decref_filename) {
  66. Py_DECREF(filename);
  67. }
  68. return res;
  69. }
  70. /* Parse input from a file and execute it */
  71. int
  72. PyRun_AnyFileExFlags(FILE *fp, const char *filename, int closeit,
  73. PyCompilerFlags *flags)
  74. {
  75. PyObject *filename_obj;
  76. if (filename != NULL) {
  77. filename_obj = PyUnicode_DecodeFSDefault(filename);
  78. if (filename_obj == NULL) {
  79. PyErr_Print();
  80. return -1;
  81. }
  82. }
  83. else {
  84. filename_obj = NULL;
  85. }
  86. int res = _PyRun_AnyFileObject(fp, filename_obj, closeit, flags);
  87. Py_XDECREF(filename_obj);
  88. return res;
  89. }
  90. int
  91. _PyRun_InteractiveLoopObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
  92. {
  93. PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
  94. if (flags == NULL) {
  95. flags = &local_flags;
  96. }
  97. PyThreadState *tstate = _PyThreadState_GET();
  98. PyObject *v = _PySys_GetAttr(tstate, &_Py_ID(ps1));
  99. if (v == NULL) {
  100. _PySys_SetAttr(&_Py_ID(ps1), v = PyUnicode_FromString(">>> "));
  101. Py_XDECREF(v);
  102. }
  103. v = _PySys_GetAttr(tstate, &_Py_ID(ps2));
  104. if (v == NULL) {
  105. _PySys_SetAttr(&_Py_ID(ps2), v = PyUnicode_FromString("... "));
  106. Py_XDECREF(v);
  107. }
  108. #ifdef Py_REF_DEBUG
  109. int show_ref_count = _Py_GetConfig()->show_ref_count;
  110. #endif
  111. int err = 0;
  112. int ret;
  113. int nomem_count = 0;
  114. do {
  115. ret = PyRun_InteractiveOneObjectEx(fp, filename, flags);
  116. if (ret == -1 && PyErr_Occurred()) {
  117. /* Prevent an endless loop after multiple consecutive MemoryErrors
  118. * while still allowing an interactive command to fail with a
  119. * MemoryError. */
  120. if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
  121. if (++nomem_count > 16) {
  122. PyErr_Clear();
  123. err = -1;
  124. break;
  125. }
  126. } else {
  127. nomem_count = 0;
  128. }
  129. PyErr_Print();
  130. flush_io();
  131. } else {
  132. nomem_count = 0;
  133. }
  134. #ifdef Py_REF_DEBUG
  135. if (show_ref_count) {
  136. _PyDebug_PrintTotalRefs();
  137. }
  138. #endif
  139. } while (ret != E_EOF);
  140. return err;
  141. }
  142. int
  143. PyRun_InteractiveLoopFlags(FILE *fp, const char *filename, PyCompilerFlags *flags)
  144. {
  145. PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
  146. if (filename_obj == NULL) {
  147. PyErr_Print();
  148. return -1;
  149. }
  150. int err = _PyRun_InteractiveLoopObject(fp, filename_obj, flags);
  151. Py_DECREF(filename_obj);
  152. return err;
  153. }
  154. /* A PyRun_InteractiveOneObject() auxiliary function that does not print the
  155. * error on failure. */
  156. static int
  157. PyRun_InteractiveOneObjectEx(FILE *fp, PyObject *filename,
  158. PyCompilerFlags *flags)
  159. {
  160. PyObject *m, *d, *v, *w, *oenc = NULL;
  161. mod_ty mod;
  162. PyArena *arena;
  163. const char *ps1 = "", *ps2 = "", *enc = NULL;
  164. int errcode = 0;
  165. PyThreadState *tstate = _PyThreadState_GET();
  166. if (fp == stdin) {
  167. /* Fetch encoding from sys.stdin if possible. */
  168. v = _PySys_GetAttr(tstate, &_Py_ID(stdin));
  169. if (v && v != Py_None) {
  170. oenc = PyObject_GetAttr(v, &_Py_ID(encoding));
  171. if (oenc)
  172. enc = PyUnicode_AsUTF8(oenc);
  173. if (!enc)
  174. PyErr_Clear();
  175. }
  176. }
  177. v = _PySys_GetAttr(tstate, &_Py_ID(ps1));
  178. if (v != NULL) {
  179. v = PyObject_Str(v);
  180. if (v == NULL)
  181. PyErr_Clear();
  182. else if (PyUnicode_Check(v)) {
  183. ps1 = PyUnicode_AsUTF8(v);
  184. if (ps1 == NULL) {
  185. PyErr_Clear();
  186. ps1 = "";
  187. }
  188. }
  189. }
  190. w = _PySys_GetAttr(tstate, &_Py_ID(ps2));
  191. if (w != NULL) {
  192. w = PyObject_Str(w);
  193. if (w == NULL)
  194. PyErr_Clear();
  195. else if (PyUnicode_Check(w)) {
  196. ps2 = PyUnicode_AsUTF8(w);
  197. if (ps2 == NULL) {
  198. PyErr_Clear();
  199. ps2 = "";
  200. }
  201. }
  202. }
  203. arena = _PyArena_New();
  204. if (arena == NULL) {
  205. Py_XDECREF(v);
  206. Py_XDECREF(w);
  207. Py_XDECREF(oenc);
  208. return -1;
  209. }
  210. mod = _PyParser_ASTFromFile(fp, filename, enc, Py_single_input,
  211. ps1, ps2, flags, &errcode, arena);
  212. Py_XDECREF(v);
  213. Py_XDECREF(w);
  214. Py_XDECREF(oenc);
  215. if (mod == NULL) {
  216. _PyArena_Free(arena);
  217. if (errcode == E_EOF) {
  218. PyErr_Clear();
  219. return E_EOF;
  220. }
  221. return -1;
  222. }
  223. m = PyImport_AddModuleObject(&_Py_ID(__main__));
  224. if (m == NULL) {
  225. _PyArena_Free(arena);
  226. return -1;
  227. }
  228. d = PyModule_GetDict(m);
  229. v = run_mod(mod, filename, d, d, flags, arena);
  230. _PyArena_Free(arena);
  231. if (v == NULL) {
  232. return -1;
  233. }
  234. Py_DECREF(v);
  235. flush_io();
  236. return 0;
  237. }
  238. int
  239. PyRun_InteractiveOneObject(FILE *fp, PyObject *filename, PyCompilerFlags *flags)
  240. {
  241. int res;
  242. res = PyRun_InteractiveOneObjectEx(fp, filename, flags);
  243. if (res == -1) {
  244. PyErr_Print();
  245. flush_io();
  246. }
  247. return res;
  248. }
  249. int
  250. PyRun_InteractiveOneFlags(FILE *fp, const char *filename_str, PyCompilerFlags *flags)
  251. {
  252. PyObject *filename;
  253. int res;
  254. filename = PyUnicode_DecodeFSDefault(filename_str);
  255. if (filename == NULL) {
  256. PyErr_Print();
  257. return -1;
  258. }
  259. res = PyRun_InteractiveOneObject(fp, filename, flags);
  260. Py_DECREF(filename);
  261. return res;
  262. }
  263. /* Check whether a file maybe a pyc file: Look at the extension,
  264. the file type, and, if we may close it, at the first few bytes. */
  265. static int
  266. maybe_pyc_file(FILE *fp, PyObject *filename, int closeit)
  267. {
  268. PyObject *ext = PyUnicode_FromString(".pyc");
  269. if (ext == NULL) {
  270. return -1;
  271. }
  272. Py_ssize_t endswith = PyUnicode_Tailmatch(filename, ext, 0, PY_SSIZE_T_MAX, +1);
  273. Py_DECREF(ext);
  274. if (endswith) {
  275. return 1;
  276. }
  277. /* Only look into the file if we are allowed to close it, since
  278. it then should also be seekable. */
  279. if (!closeit) {
  280. return 0;
  281. }
  282. /* Read only two bytes of the magic. If the file was opened in
  283. text mode, the bytes 3 and 4 of the magic (\r\n) might not
  284. be read as they are on disk. */
  285. unsigned int halfmagic = PyImport_GetMagicNumber() & 0xFFFF;
  286. unsigned char buf[2];
  287. /* Mess: In case of -x, the stream is NOT at its start now,
  288. and ungetc() was used to push back the first newline,
  289. which makes the current stream position formally undefined,
  290. and a x-platform nightmare.
  291. Unfortunately, we have no direct way to know whether -x
  292. was specified. So we use a terrible hack: if the current
  293. stream position is not 0, we assume -x was specified, and
  294. give up. Bug 132850 on SourceForge spells out the
  295. hopelessness of trying anything else (fseek and ftell
  296. don't work predictably x-platform for text-mode files).
  297. */
  298. int ispyc = 0;
  299. if (ftell(fp) == 0) {
  300. if (fread(buf, 1, 2, fp) == 2 &&
  301. ((unsigned int)buf[1]<<8 | buf[0]) == halfmagic)
  302. ispyc = 1;
  303. rewind(fp);
  304. }
  305. return ispyc;
  306. }
  307. static int
  308. set_main_loader(PyObject *d, PyObject *filename, const char *loader_name)
  309. {
  310. PyInterpreterState *interp = _PyInterpreterState_GET();
  311. PyObject *loader_type = _PyImport_GetImportlibExternalLoader(interp,
  312. loader_name);
  313. if (loader_type == NULL) {
  314. return -1;
  315. }
  316. PyObject *loader = PyObject_CallFunction(loader_type,
  317. "sO", "__main__", filename);
  318. Py_DECREF(loader_type);
  319. if (loader == NULL) {
  320. return -1;
  321. }
  322. if (PyDict_SetItemString(d, "__loader__", loader) < 0) {
  323. Py_DECREF(loader);
  324. return -1;
  325. }
  326. Py_DECREF(loader);
  327. return 0;
  328. }
  329. int
  330. _PyRun_SimpleFileObject(FILE *fp, PyObject *filename, int closeit,
  331. PyCompilerFlags *flags)
  332. {
  333. PyObject *m, *d, *v;
  334. int set_file_name = 0, ret = -1;
  335. m = PyImport_AddModule("__main__");
  336. if (m == NULL)
  337. return -1;
  338. Py_INCREF(m);
  339. d = PyModule_GetDict(m);
  340. if (_PyDict_GetItemStringWithError(d, "__file__") == NULL) {
  341. if (PyErr_Occurred()) {
  342. goto done;
  343. }
  344. if (PyDict_SetItemString(d, "__file__", filename) < 0) {
  345. goto done;
  346. }
  347. if (PyDict_SetItemString(d, "__cached__", Py_None) < 0) {
  348. goto done;
  349. }
  350. set_file_name = 1;
  351. }
  352. int pyc = maybe_pyc_file(fp, filename, closeit);
  353. if (pyc < 0) {
  354. goto done;
  355. }
  356. if (pyc) {
  357. FILE *pyc_fp;
  358. /* Try to run a pyc file. First, re-open in binary */
  359. if (closeit) {
  360. fclose(fp);
  361. }
  362. pyc_fp = _Py_fopen_obj(filename, "rb");
  363. if (pyc_fp == NULL) {
  364. fprintf(stderr, "python: Can't reopen .pyc file\n");
  365. goto done;
  366. }
  367. if (set_main_loader(d, filename, "SourcelessFileLoader") < 0) {
  368. fprintf(stderr, "python: failed to set __main__.__loader__\n");
  369. ret = -1;
  370. fclose(pyc_fp);
  371. goto done;
  372. }
  373. v = run_pyc_file(pyc_fp, d, d, flags);
  374. } else {
  375. /* When running from stdin, leave __main__.__loader__ alone */
  376. if (PyUnicode_CompareWithASCIIString(filename, "<stdin>") != 0 &&
  377. set_main_loader(d, filename, "SourceFileLoader") < 0) {
  378. fprintf(stderr, "python: failed to set __main__.__loader__\n");
  379. ret = -1;
  380. goto done;
  381. }
  382. v = pyrun_file(fp, filename, Py_file_input, d, d,
  383. closeit, flags);
  384. }
  385. flush_io();
  386. if (v == NULL) {
  387. Py_CLEAR(m);
  388. PyErr_Print();
  389. goto done;
  390. }
  391. Py_DECREF(v);
  392. ret = 0;
  393. done:
  394. if (set_file_name) {
  395. if (PyDict_DelItemString(d, "__file__")) {
  396. PyErr_Clear();
  397. }
  398. if (PyDict_DelItemString(d, "__cached__")) {
  399. PyErr_Clear();
  400. }
  401. }
  402. Py_XDECREF(m);
  403. return ret;
  404. }
  405. int
  406. PyRun_SimpleFileExFlags(FILE *fp, const char *filename, int closeit,
  407. PyCompilerFlags *flags)
  408. {
  409. PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
  410. if (filename_obj == NULL) {
  411. return -1;
  412. }
  413. int res = _PyRun_SimpleFileObject(fp, filename_obj, closeit, flags);
  414. Py_DECREF(filename_obj);
  415. return res;
  416. }
  417. int
  418. PyRun_SimpleStringFlags(const char *command, PyCompilerFlags *flags)
  419. {
  420. PyObject *m, *d, *v;
  421. m = PyImport_AddModule("__main__");
  422. if (m == NULL)
  423. return -1;
  424. d = PyModule_GetDict(m);
  425. v = PyRun_StringFlags(command, Py_file_input, d, d, flags);
  426. if (v == NULL) {
  427. PyErr_Print();
  428. return -1;
  429. }
  430. Py_DECREF(v);
  431. return 0;
  432. }
  433. static int
  434. parse_syntax_error(PyObject *err, PyObject **message, PyObject **filename,
  435. Py_ssize_t *lineno, Py_ssize_t *offset,
  436. Py_ssize_t* end_lineno, Py_ssize_t* end_offset,
  437. PyObject **text)
  438. {
  439. Py_ssize_t hold;
  440. PyObject *v;
  441. *message = NULL;
  442. *filename = NULL;
  443. /* new style errors. `err' is an instance */
  444. *message = PyObject_GetAttr(err, &_Py_ID(msg));
  445. if (!*message)
  446. goto finally;
  447. v = PyObject_GetAttr(err, &_Py_ID(filename));
  448. if (!v)
  449. goto finally;
  450. if (v == Py_None) {
  451. Py_DECREF(v);
  452. _Py_DECLARE_STR(anon_string, "<string>");
  453. *filename = Py_NewRef(&_Py_STR(anon_string));
  454. }
  455. else {
  456. *filename = v;
  457. }
  458. v = PyObject_GetAttr(err, &_Py_ID(lineno));
  459. if (!v)
  460. goto finally;
  461. hold = PyLong_AsSsize_t(v);
  462. Py_DECREF(v);
  463. if (hold < 0 && PyErr_Occurred())
  464. goto finally;
  465. *lineno = hold;
  466. v = PyObject_GetAttr(err, &_Py_ID(offset));
  467. if (!v)
  468. goto finally;
  469. if (v == Py_None) {
  470. *offset = -1;
  471. Py_DECREF(v);
  472. } else {
  473. hold = PyLong_AsSsize_t(v);
  474. Py_DECREF(v);
  475. if (hold < 0 && PyErr_Occurred())
  476. goto finally;
  477. *offset = hold;
  478. }
  479. if (Py_TYPE(err) == (PyTypeObject*)PyExc_SyntaxError) {
  480. v = PyObject_GetAttr(err, &_Py_ID(end_lineno));
  481. if (!v) {
  482. PyErr_Clear();
  483. *end_lineno = *lineno;
  484. }
  485. else if (v == Py_None) {
  486. *end_lineno = *lineno;
  487. Py_DECREF(v);
  488. } else {
  489. hold = PyLong_AsSsize_t(v);
  490. Py_DECREF(v);
  491. if (hold < 0 && PyErr_Occurred())
  492. goto finally;
  493. *end_lineno = hold;
  494. }
  495. v = PyObject_GetAttr(err, &_Py_ID(end_offset));
  496. if (!v) {
  497. PyErr_Clear();
  498. *end_offset = -1;
  499. }
  500. else if (v == Py_None) {
  501. *end_offset = -1;
  502. Py_DECREF(v);
  503. } else {
  504. hold = PyLong_AsSsize_t(v);
  505. Py_DECREF(v);
  506. if (hold < 0 && PyErr_Occurred())
  507. goto finally;
  508. *end_offset = hold;
  509. }
  510. } else {
  511. // SyntaxError subclasses
  512. *end_lineno = *lineno;
  513. *end_offset = -1;
  514. }
  515. v = PyObject_GetAttr(err, &_Py_ID(text));
  516. if (!v)
  517. goto finally;
  518. if (v == Py_None) {
  519. Py_DECREF(v);
  520. *text = NULL;
  521. }
  522. else {
  523. *text = v;
  524. }
  525. return 1;
  526. finally:
  527. Py_XDECREF(*message);
  528. Py_XDECREF(*filename);
  529. return 0;
  530. }
  531. static int
  532. print_error_text(PyObject *f, Py_ssize_t offset, Py_ssize_t end_offset,
  533. PyObject *text_obj)
  534. {
  535. size_t caret_repetitions = (end_offset > 0 && end_offset > offset) ?
  536. end_offset - offset : 1;
  537. /* Convert text to a char pointer; return if error */
  538. const char *text = PyUnicode_AsUTF8(text_obj);
  539. if (text == NULL) {
  540. return -1;
  541. }
  542. /* Convert offset from 1-based to 0-based */
  543. offset--;
  544. /* Strip leading whitespace from text, adjusting offset as we go */
  545. while (*text == ' ' || *text == '\t' || *text == '\f') {
  546. text++;
  547. offset--;
  548. }
  549. /* Calculate text length excluding trailing newline */
  550. Py_ssize_t len = strlen(text);
  551. if (len > 0 && text[len-1] == '\n') {
  552. len--;
  553. }
  554. /* Clip offset to at most len */
  555. if (offset > len) {
  556. offset = len;
  557. }
  558. /* Skip past newlines embedded in text */
  559. for (;;) {
  560. const char *nl = strchr(text, '\n');
  561. if (nl == NULL) {
  562. break;
  563. }
  564. Py_ssize_t inl = nl - text;
  565. if (inl >= offset) {
  566. break;
  567. }
  568. inl += 1;
  569. text += inl;
  570. len -= inl;
  571. offset -= (int)inl;
  572. }
  573. /* Print text */
  574. if (PyFile_WriteString(" ", f) < 0) {
  575. return -1;
  576. }
  577. if (PyFile_WriteString(text, f) < 0) {
  578. return -1;
  579. }
  580. /* Make sure there's a newline at the end */
  581. if (text[len] != '\n') {
  582. if (PyFile_WriteString("\n", f) < 0) {
  583. return -1;
  584. }
  585. }
  586. /* Don't print caret if it points to the left of the text */
  587. if (offset < 0) {
  588. return 0;
  589. }
  590. /* Write caret line */
  591. if (PyFile_WriteString(" ", f) < 0) {
  592. return -1;
  593. }
  594. while (--offset >= 0) {
  595. if (PyFile_WriteString(" ", f) < 0) {
  596. return -1;
  597. }
  598. }
  599. for (size_t caret_iter=0; caret_iter < caret_repetitions ; caret_iter++) {
  600. if (PyFile_WriteString("^", f) < 0) {
  601. return -1;
  602. }
  603. }
  604. if (PyFile_WriteString("\n", f) < 0) {
  605. return -1;
  606. }
  607. return 0;
  608. }
  609. int
  610. _Py_HandleSystemExit(int *exitcode_p)
  611. {
  612. int inspect = _Py_GetConfig()->inspect;
  613. if (inspect) {
  614. /* Don't exit if -i flag was given. This flag is set to 0
  615. * when entering interactive mode for inspecting. */
  616. return 0;
  617. }
  618. if (!PyErr_ExceptionMatches(PyExc_SystemExit)) {
  619. return 0;
  620. }
  621. fflush(stdout);
  622. int exitcode = 0;
  623. PyObject *exc = PyErr_GetRaisedException();
  624. if (exc == NULL) {
  625. goto done;
  626. }
  627. assert(PyExceptionInstance_Check(exc));
  628. /* The error code should be in the `code' attribute. */
  629. PyObject *code = PyObject_GetAttr(exc, &_Py_ID(code));
  630. if (code) {
  631. Py_SETREF(exc, code);
  632. if (exc == Py_None) {
  633. goto done;
  634. }
  635. }
  636. /* If we failed to dig out the 'code' attribute,
  637. * just let the else clause below print the error.
  638. */
  639. if (PyLong_Check(exc)) {
  640. exitcode = (int)PyLong_AsLong(exc);
  641. }
  642. else {
  643. PyThreadState *tstate = _PyThreadState_GET();
  644. PyObject *sys_stderr = _PySys_GetAttr(tstate, &_Py_ID(stderr));
  645. /* We clear the exception here to avoid triggering the assertion
  646. * in PyObject_Str that ensures it won't silently lose exception
  647. * details.
  648. */
  649. PyErr_Clear();
  650. if (sys_stderr != NULL && sys_stderr != Py_None) {
  651. PyFile_WriteObject(exc, sys_stderr, Py_PRINT_RAW);
  652. } else {
  653. PyObject_Print(exc, stderr, Py_PRINT_RAW);
  654. fflush(stderr);
  655. }
  656. PySys_WriteStderr("\n");
  657. exitcode = 1;
  658. }
  659. done:
  660. Py_CLEAR(exc);
  661. *exitcode_p = exitcode;
  662. return 1;
  663. }
  664. static void
  665. handle_system_exit(void)
  666. {
  667. int exitcode;
  668. if (_Py_HandleSystemExit(&exitcode)) {
  669. Py_Exit(exitcode);
  670. }
  671. }
  672. static void
  673. _PyErr_PrintEx(PyThreadState *tstate, int set_sys_last_vars)
  674. {
  675. PyObject *typ = NULL, *tb = NULL;
  676. handle_system_exit();
  677. PyObject *exc = _PyErr_GetRaisedException(tstate);
  678. if (exc == NULL) {
  679. goto done;
  680. }
  681. assert(PyExceptionInstance_Check(exc));
  682. typ = Py_NewRef(Py_TYPE(exc));
  683. tb = PyException_GetTraceback(exc);
  684. if (tb == NULL) {
  685. tb = Py_NewRef(Py_None);
  686. }
  687. if (set_sys_last_vars) {
  688. if (_PySys_SetAttr(&_Py_ID(last_exc), exc) < 0) {
  689. _PyErr_Clear(tstate);
  690. }
  691. /* Legacy version: */
  692. if (_PySys_SetAttr(&_Py_ID(last_type), typ) < 0) {
  693. _PyErr_Clear(tstate);
  694. }
  695. if (_PySys_SetAttr(&_Py_ID(last_value), exc) < 0) {
  696. _PyErr_Clear(tstate);
  697. }
  698. if (_PySys_SetAttr(&_Py_ID(last_traceback), tb) < 0) {
  699. _PyErr_Clear(tstate);
  700. }
  701. }
  702. PyObject *hook = _PySys_GetAttr(tstate, &_Py_ID(excepthook));
  703. if (_PySys_Audit(tstate, "sys.excepthook", "OOOO", hook ? hook : Py_None,
  704. typ, exc, tb) < 0) {
  705. if (PyErr_ExceptionMatches(PyExc_RuntimeError)) {
  706. PyErr_Clear();
  707. goto done;
  708. }
  709. _PyErr_WriteUnraisableMsg("in audit hook", NULL);
  710. }
  711. if (hook) {
  712. PyObject* stack[3];
  713. stack[0] = typ;
  714. stack[1] = exc;
  715. stack[2] = tb;
  716. PyObject *result = _PyObject_FastCall(hook, stack, 3);
  717. if (result == NULL) {
  718. handle_system_exit();
  719. PyObject *exc2 = _PyErr_GetRaisedException(tstate);
  720. assert(exc2 && PyExceptionInstance_Check(exc2));
  721. fflush(stdout);
  722. PySys_WriteStderr("Error in sys.excepthook:\n");
  723. PyErr_DisplayException(exc2);
  724. PySys_WriteStderr("\nOriginal exception was:\n");
  725. PyErr_DisplayException(exc);
  726. Py_DECREF(exc2);
  727. }
  728. else {
  729. Py_DECREF(result);
  730. }
  731. }
  732. else {
  733. PySys_WriteStderr("sys.excepthook is missing\n");
  734. PyErr_DisplayException(exc);
  735. }
  736. done:
  737. Py_XDECREF(typ);
  738. Py_XDECREF(exc);
  739. Py_XDECREF(tb);
  740. }
  741. void
  742. _PyErr_Print(PyThreadState *tstate)
  743. {
  744. _PyErr_PrintEx(tstate, 1);
  745. }
  746. void
  747. PyErr_PrintEx(int set_sys_last_vars)
  748. {
  749. PyThreadState *tstate = _PyThreadState_GET();
  750. _PyErr_PrintEx(tstate, set_sys_last_vars);
  751. }
  752. void
  753. PyErr_Print(void)
  754. {
  755. PyErr_PrintEx(1);
  756. }
  757. struct exception_print_context
  758. {
  759. PyObject *file;
  760. PyObject *seen; // Prevent cycles in recursion
  761. int exception_group_depth; // nesting level of current exception group
  762. bool need_close; // Need a closing bottom frame
  763. int max_group_width; // Maximum number of children of each EG
  764. int max_group_depth; // Maximum nesting level of EGs
  765. };
  766. #define EXC_MARGIN(ctx) ((ctx)->exception_group_depth ? "| " : "")
  767. #define EXC_INDENT(ctx) (2 * (ctx)->exception_group_depth)
  768. static int
  769. write_indented_margin(struct exception_print_context *ctx, PyObject *f)
  770. {
  771. return _Py_WriteIndentedMargin(EXC_INDENT(ctx), EXC_MARGIN(ctx), f);
  772. }
  773. static int
  774. print_exception_invalid_type(struct exception_print_context *ctx,
  775. PyObject *value)
  776. {
  777. PyObject *f = ctx->file;
  778. if (_Py_WriteIndent(EXC_INDENT(ctx), f) < 0) {
  779. return -1;
  780. }
  781. const char *const msg = "TypeError: print_exception(): Exception expected "
  782. "for value, ";
  783. if (PyFile_WriteString(msg, f) < 0) {
  784. return -1;
  785. }
  786. if (PyFile_WriteString(Py_TYPE(value)->tp_name, f) < 0) {
  787. return -1;
  788. }
  789. if (PyFile_WriteString(" found\n", f) < 0) {
  790. return -1;
  791. }
  792. return 0;
  793. }
  794. static int
  795. print_exception_traceback(struct exception_print_context *ctx, PyObject *value)
  796. {
  797. PyObject *f = ctx->file;
  798. int err = 0;
  799. PyObject *tb = PyException_GetTraceback(value);
  800. if (tb && tb != Py_None) {
  801. const char *header = EXCEPTION_TB_HEADER;
  802. const char *header_margin = EXC_MARGIN(ctx);
  803. if (_PyBaseExceptionGroup_Check(value)) {
  804. header = EXCEPTION_GROUP_TB_HEADER;
  805. if (ctx->exception_group_depth == 1) {
  806. header_margin = "+ ";
  807. }
  808. }
  809. err = _PyTraceBack_Print_Indented(
  810. tb, EXC_INDENT(ctx), EXC_MARGIN(ctx), header_margin, header, f);
  811. }
  812. Py_XDECREF(tb);
  813. return err;
  814. }
  815. static int
  816. print_exception_file_and_line(struct exception_print_context *ctx,
  817. PyObject **value_p)
  818. {
  819. PyObject *f = ctx->file;
  820. PyObject *tmp;
  821. int res = _PyObject_LookupAttr(*value_p, &_Py_ID(print_file_and_line), &tmp);
  822. if (res <= 0) {
  823. if (res < 0) {
  824. PyErr_Clear();
  825. }
  826. return 0;
  827. }
  828. Py_DECREF(tmp);
  829. PyObject *message, *filename, *text;
  830. Py_ssize_t lineno, offset, end_lineno, end_offset;
  831. if (!parse_syntax_error(*value_p, &message, &filename,
  832. &lineno, &offset,
  833. &end_lineno, &end_offset, &text)) {
  834. PyErr_Clear();
  835. return 0;
  836. }
  837. Py_SETREF(*value_p, message);
  838. PyObject *line = PyUnicode_FromFormat(" File \"%S\", line %zd\n",
  839. filename, lineno);
  840. Py_DECREF(filename);
  841. if (line == NULL) {
  842. goto error;
  843. }
  844. if (write_indented_margin(ctx, f) < 0) {
  845. goto error;
  846. }
  847. if (PyFile_WriteObject(line, f, Py_PRINT_RAW) < 0) {
  848. goto error;
  849. }
  850. Py_CLEAR(line);
  851. if (text != NULL) {
  852. Py_ssize_t line_size;
  853. const char *error_line = PyUnicode_AsUTF8AndSize(text, &line_size);
  854. // If the location of the error spawn multiple lines, we want
  855. // to just print the first one and highlight everything until
  856. // the end of that one since we don't support multi-line error
  857. // messages.
  858. if (end_lineno > lineno) {
  859. end_offset = (error_line != NULL) ? line_size : -1;
  860. }
  861. // Limit the amount of '^' that we can display to
  862. // the size of the text in the source line.
  863. if (error_line != NULL && end_offset > line_size + 1) {
  864. end_offset = line_size + 1;
  865. }
  866. if (print_error_text(f, offset, end_offset, text) < 0) {
  867. goto error;
  868. }
  869. Py_DECREF(text);
  870. }
  871. assert(!PyErr_Occurred());
  872. return 0;
  873. error:
  874. Py_XDECREF(line);
  875. Py_XDECREF(text);
  876. return -1;
  877. }
  878. /* Prints the message line: module.qualname[: str(exc)] */
  879. static int
  880. print_exception_message(struct exception_print_context *ctx, PyObject *type,
  881. PyObject *value)
  882. {
  883. PyObject *f = ctx->file;
  884. assert(PyExceptionClass_Check(type));
  885. if (write_indented_margin(ctx, f) < 0) {
  886. return -1;
  887. }
  888. PyObject *modulename = PyObject_GetAttr(type, &_Py_ID(__module__));
  889. if (modulename == NULL || !PyUnicode_Check(modulename)) {
  890. Py_XDECREF(modulename);
  891. PyErr_Clear();
  892. if (PyFile_WriteString("<unknown>.", f) < 0) {
  893. return -1;
  894. }
  895. }
  896. else {
  897. if (!_PyUnicode_Equal(modulename, &_Py_ID(builtins)) &&
  898. !_PyUnicode_Equal(modulename, &_Py_ID(__main__)))
  899. {
  900. int res = PyFile_WriteObject(modulename, f, Py_PRINT_RAW);
  901. Py_DECREF(modulename);
  902. if (res < 0) {
  903. return -1;
  904. }
  905. if (PyFile_WriteString(".", f) < 0) {
  906. return -1;
  907. }
  908. }
  909. else {
  910. Py_DECREF(modulename);
  911. }
  912. }
  913. PyObject *qualname = PyType_GetQualName((PyTypeObject *)type);
  914. if (qualname == NULL || !PyUnicode_Check(qualname)) {
  915. Py_XDECREF(qualname);
  916. PyErr_Clear();
  917. if (PyFile_WriteString("<unknown>", f) < 0) {
  918. return -1;
  919. }
  920. }
  921. else {
  922. int res = PyFile_WriteObject(qualname, f, Py_PRINT_RAW);
  923. Py_DECREF(qualname);
  924. if (res < 0) {
  925. return -1;
  926. }
  927. }
  928. if (Py_IsNone(value)) {
  929. return 0;
  930. }
  931. PyObject *s = PyObject_Str(value);
  932. if (s == NULL) {
  933. PyErr_Clear();
  934. if (PyFile_WriteString(": <exception str() failed>", f) < 0) {
  935. return -1;
  936. }
  937. }
  938. else {
  939. /* only print colon if the str() of the
  940. object is not the empty string
  941. */
  942. if (!PyUnicode_Check(s) || PyUnicode_GetLength(s) != 0) {
  943. if (PyFile_WriteString(": ", f) < 0) {
  944. Py_DECREF(s);
  945. return -1;
  946. }
  947. }
  948. int res = PyFile_WriteObject(s, f, Py_PRINT_RAW);
  949. Py_DECREF(s);
  950. if (res < 0) {
  951. return -1;
  952. }
  953. }
  954. return 0;
  955. }
  956. static int
  957. print_exception_suggestions(struct exception_print_context *ctx,
  958. PyObject *value)
  959. {
  960. PyObject *f = ctx->file;
  961. PyObject *suggestions = _Py_Offer_Suggestions(value);
  962. if (suggestions) {
  963. if (PyFile_WriteObject(suggestions, f, Py_PRINT_RAW) < 0) {
  964. goto error;
  965. }
  966. Py_DECREF(suggestions);
  967. }
  968. else if (PyErr_Occurred()) {
  969. PyErr_Clear();
  970. }
  971. return 0;
  972. error:
  973. Py_XDECREF(suggestions);
  974. return -1;
  975. }
  976. static int
  977. print_exception_notes(struct exception_print_context *ctx, PyObject *notes)
  978. {
  979. PyObject *f = ctx->file;
  980. if (notes == NULL) {
  981. return 0;
  982. }
  983. if (!PySequence_Check(notes) || PyUnicode_Check(notes) || PyBytes_Check(notes)) {
  984. int res = 0;
  985. if (write_indented_margin(ctx, f) < 0) {
  986. res = -1;
  987. }
  988. PyObject *s = PyObject_Repr(notes);
  989. if (s == NULL) {
  990. PyErr_Clear();
  991. res = PyFile_WriteString("<__notes__ repr() failed>", f);
  992. }
  993. else {
  994. res = PyFile_WriteObject(s, f, Py_PRINT_RAW);
  995. Py_DECREF(s);
  996. }
  997. if (PyFile_WriteString("\n", f) < 0) {
  998. res = -1;
  999. }
  1000. return res;
  1001. }
  1002. Py_ssize_t num_notes = PySequence_Length(notes);
  1003. PyObject *lines = NULL;
  1004. for (Py_ssize_t ni = 0; ni < num_notes; ni++) {
  1005. PyObject *note = PySequence_GetItem(notes, ni);
  1006. PyObject *note_str = PyObject_Str(note);
  1007. Py_DECREF(note);
  1008. if (note_str == NULL) {
  1009. PyErr_Clear();
  1010. if (PyFile_WriteString("<note str() failed>", f) < 0) {
  1011. goto error;
  1012. }
  1013. }
  1014. else {
  1015. lines = PyUnicode_Splitlines(note_str, 1);
  1016. Py_DECREF(note_str);
  1017. if (lines == NULL) {
  1018. goto error;
  1019. }
  1020. Py_ssize_t n = PyList_GET_SIZE(lines);
  1021. for (Py_ssize_t i = 0; i < n; i++) {
  1022. PyObject *line = PyList_GET_ITEM(lines, i);
  1023. assert(PyUnicode_Check(line));
  1024. if (write_indented_margin(ctx, f) < 0) {
  1025. goto error;
  1026. }
  1027. if (PyFile_WriteObject(line, f, Py_PRINT_RAW) < 0) {
  1028. goto error;
  1029. }
  1030. }
  1031. Py_CLEAR(lines);
  1032. }
  1033. if (PyFile_WriteString("\n", f) < 0) {
  1034. goto error;
  1035. }
  1036. }
  1037. return 0;
  1038. error:
  1039. Py_XDECREF(lines);
  1040. return -1;
  1041. }
  1042. static int
  1043. get_exception_notes(struct exception_print_context *ctx, PyObject *value, PyObject **notes) {
  1044. PyObject *note = NULL;
  1045. if (_PyObject_LookupAttr(value, &_Py_ID(__notes__), notes) < 0) {
  1046. PyObject *type, *errvalue, *tback;
  1047. PyErr_Fetch(&type, &errvalue, &tback);
  1048. PyErr_NormalizeException(&type, &errvalue, &tback);
  1049. note = PyUnicode_FromFormat("Ignored error getting __notes__: %R", errvalue);
  1050. Py_XDECREF(type);
  1051. Py_XDECREF(errvalue);
  1052. Py_XDECREF(tback);
  1053. if (!note) {
  1054. goto error;
  1055. }
  1056. *notes = PyList_New(1);
  1057. if (!*notes) {
  1058. goto error;
  1059. }
  1060. if (PyList_SetItem(*notes, 0, note) < 0) {
  1061. Py_DECREF(*notes);
  1062. goto error;
  1063. }
  1064. }
  1065. return 0;
  1066. error:
  1067. Py_XDECREF(note);
  1068. return -1;
  1069. }
  1070. static int
  1071. print_exception(struct exception_print_context *ctx, PyObject *value)
  1072. {
  1073. PyObject *notes = NULL;
  1074. PyObject *f = ctx->file;
  1075. if (!PyExceptionInstance_Check(value)) {
  1076. return print_exception_invalid_type(ctx, value);
  1077. }
  1078. Py_INCREF(value);
  1079. fflush(stdout);
  1080. if (print_exception_traceback(ctx, value) < 0) {
  1081. goto error;
  1082. }
  1083. /* grab the type and notes now because value can change below */
  1084. PyObject *type = (PyObject *) Py_TYPE(value);
  1085. if (get_exception_notes(ctx, value, &notes) < 0) {
  1086. goto error;
  1087. }
  1088. if (print_exception_file_and_line(ctx, &value) < 0) {
  1089. goto error;
  1090. }
  1091. if (print_exception_message(ctx, type, value) < 0) {
  1092. goto error;
  1093. }
  1094. if (print_exception_suggestions(ctx, value) < 0) {
  1095. goto error;
  1096. }
  1097. if (PyFile_WriteString("\n", f) < 0) {
  1098. goto error;
  1099. }
  1100. if (print_exception_notes(ctx, notes) < 0) {
  1101. goto error;
  1102. }
  1103. Py_XDECREF(notes);
  1104. Py_DECREF(value);
  1105. assert(!PyErr_Occurred());
  1106. return 0;
  1107. error:
  1108. Py_XDECREF(notes);
  1109. Py_DECREF(value);
  1110. return -1;
  1111. }
  1112. static const char cause_message[] =
  1113. "The above exception was the direct cause "
  1114. "of the following exception:\n";
  1115. static const char context_message[] =
  1116. "During handling of the above exception, "
  1117. "another exception occurred:\n";
  1118. static int
  1119. print_exception_recursive(struct exception_print_context*, PyObject*);
  1120. static int
  1121. print_chained(struct exception_print_context* ctx, PyObject *value,
  1122. const char * message, const char *tag)
  1123. {
  1124. PyObject *f = ctx->file;
  1125. if (_Py_EnterRecursiveCall(" in print_chained")) {
  1126. return -1;
  1127. }
  1128. bool need_close = ctx->need_close;
  1129. int res = print_exception_recursive(ctx, value);
  1130. ctx->need_close = need_close;
  1131. _Py_LeaveRecursiveCall();
  1132. if (res < 0) {
  1133. return -1;
  1134. }
  1135. if (write_indented_margin(ctx, f) < 0) {
  1136. return -1;
  1137. }
  1138. if (PyFile_WriteString("\n", f) < 0) {
  1139. return -1;
  1140. }
  1141. if (write_indented_margin(ctx, f) < 0) {
  1142. return -1;
  1143. }
  1144. if (PyFile_WriteString(message, f) < 0) {
  1145. return -1;
  1146. }
  1147. if (write_indented_margin(ctx, f) < 0) {
  1148. return -1;
  1149. }
  1150. if (PyFile_WriteString("\n", f) < 0) {
  1151. return -1;
  1152. }
  1153. return 0;
  1154. }
  1155. /* Return true if value is in seen or there was a lookup error.
  1156. * Return false if lookup succeeded and the item was not found.
  1157. * We suppress errors because this makes us err on the side of
  1158. * under-printing which is better than over-printing irregular
  1159. * exceptions (e.g., unhashable ones).
  1160. */
  1161. static bool
  1162. print_exception_seen_lookup(struct exception_print_context *ctx,
  1163. PyObject *value)
  1164. {
  1165. PyObject *check_id = PyLong_FromVoidPtr(value);
  1166. if (check_id == NULL) {
  1167. PyErr_Clear();
  1168. return true;
  1169. }
  1170. int in_seen = PySet_Contains(ctx->seen, check_id);
  1171. Py_DECREF(check_id);
  1172. if (in_seen == -1) {
  1173. PyErr_Clear();
  1174. return true;
  1175. }
  1176. if (in_seen == 1) {
  1177. /* value is in seen */
  1178. return true;
  1179. }
  1180. return false;
  1181. }
  1182. static int
  1183. print_exception_cause_and_context(struct exception_print_context *ctx,
  1184. PyObject *value)
  1185. {
  1186. PyObject *value_id = PyLong_FromVoidPtr(value);
  1187. if (value_id == NULL || PySet_Add(ctx->seen, value_id) == -1) {
  1188. PyErr_Clear();
  1189. Py_XDECREF(value_id);
  1190. return 0;
  1191. }
  1192. Py_DECREF(value_id);
  1193. if (!PyExceptionInstance_Check(value)) {
  1194. return 0;
  1195. }
  1196. PyObject *cause = PyException_GetCause(value);
  1197. if (cause) {
  1198. int err = 0;
  1199. if (!print_exception_seen_lookup(ctx, cause)) {
  1200. err = print_chained(ctx, cause, cause_message, "cause");
  1201. }
  1202. Py_DECREF(cause);
  1203. return err;
  1204. }
  1205. if (((PyBaseExceptionObject *)value)->suppress_context) {
  1206. return 0;
  1207. }
  1208. PyObject *context = PyException_GetContext(value);
  1209. if (context) {
  1210. int err = 0;
  1211. if (!print_exception_seen_lookup(ctx, context)) {
  1212. err = print_chained(ctx, context, context_message, "context");
  1213. }
  1214. Py_DECREF(context);
  1215. return err;
  1216. }
  1217. return 0;
  1218. }
  1219. static int
  1220. print_exception_group(struct exception_print_context *ctx, PyObject *value)
  1221. {
  1222. PyObject *f = ctx->file;
  1223. if (ctx->exception_group_depth > ctx->max_group_depth) {
  1224. /* depth exceeds limit */
  1225. if (write_indented_margin(ctx, f) < 0) {
  1226. return -1;
  1227. }
  1228. PyObject *line = PyUnicode_FromFormat("... (max_group_depth is %d)\n",
  1229. ctx->max_group_depth);
  1230. if (line == NULL) {
  1231. return -1;
  1232. }
  1233. int err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
  1234. Py_DECREF(line);
  1235. return err;
  1236. }
  1237. if (ctx->exception_group_depth == 0) {
  1238. ctx->exception_group_depth += 1;
  1239. }
  1240. if (print_exception(ctx, value) < 0) {
  1241. return -1;
  1242. }
  1243. PyObject *excs = ((PyBaseExceptionGroupObject *)value)->excs;
  1244. assert(excs && PyTuple_Check(excs));
  1245. Py_ssize_t num_excs = PyTuple_GET_SIZE(excs);
  1246. assert(num_excs > 0);
  1247. Py_ssize_t n;
  1248. if (num_excs <= ctx->max_group_width) {
  1249. n = num_excs;
  1250. }
  1251. else {
  1252. n = ctx->max_group_width + 1;
  1253. }
  1254. ctx->need_close = false;
  1255. for (Py_ssize_t i = 0; i < n; i++) {
  1256. bool last_exc = (i == n - 1);
  1257. if (last_exc) {
  1258. // The closing frame may be added in a recursive call
  1259. ctx->need_close = true;
  1260. }
  1261. if (_Py_WriteIndent(EXC_INDENT(ctx), f) < 0) {
  1262. return -1;
  1263. }
  1264. bool truncated = (i >= ctx->max_group_width);
  1265. PyObject *line;
  1266. if (!truncated) {
  1267. line = PyUnicode_FromFormat(
  1268. "%s+---------------- %zd ----------------\n",
  1269. (i == 0) ? "+-" : " ", i + 1);
  1270. }
  1271. else {
  1272. line = PyUnicode_FromFormat(
  1273. "%s+---------------- ... ----------------\n",
  1274. (i == 0) ? "+-" : " ");
  1275. }
  1276. if (line == NULL) {
  1277. return -1;
  1278. }
  1279. int err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
  1280. Py_DECREF(line);
  1281. if (err < 0) {
  1282. return -1;
  1283. }
  1284. ctx->exception_group_depth += 1;
  1285. PyObject *exc = PyTuple_GET_ITEM(excs, i);
  1286. if (!truncated) {
  1287. if (_Py_EnterRecursiveCall(" in print_exception_group")) {
  1288. return -1;
  1289. }
  1290. int res = print_exception_recursive(ctx, exc);
  1291. _Py_LeaveRecursiveCall();
  1292. if (res < 0) {
  1293. return -1;
  1294. }
  1295. }
  1296. else {
  1297. Py_ssize_t excs_remaining = num_excs - ctx->max_group_width;
  1298. if (write_indented_margin(ctx, f) < 0) {
  1299. return -1;
  1300. }
  1301. PyObject *line = PyUnicode_FromFormat(
  1302. "and %zd more exception%s\n",
  1303. excs_remaining, excs_remaining > 1 ? "s" : "");
  1304. if (line == NULL) {
  1305. return -1;
  1306. }
  1307. int err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
  1308. Py_DECREF(line);
  1309. if (err < 0) {
  1310. return -1;
  1311. }
  1312. }
  1313. if (last_exc && ctx->need_close) {
  1314. if (_Py_WriteIndent(EXC_INDENT(ctx), f) < 0) {
  1315. return -1;
  1316. }
  1317. if (PyFile_WriteString(
  1318. "+------------------------------------\n", f) < 0) {
  1319. return -1;
  1320. }
  1321. ctx->need_close = false;
  1322. }
  1323. ctx->exception_group_depth -= 1;
  1324. }
  1325. if (ctx->exception_group_depth == 1) {
  1326. ctx->exception_group_depth -= 1;
  1327. }
  1328. return 0;
  1329. }
  1330. static int
  1331. print_exception_recursive(struct exception_print_context *ctx, PyObject *value)
  1332. {
  1333. if (_Py_EnterRecursiveCall(" in print_exception_recursive")) {
  1334. return -1;
  1335. }
  1336. if (ctx->seen != NULL) {
  1337. /* Exception chaining */
  1338. if (print_exception_cause_and_context(ctx, value) < 0) {
  1339. goto error;
  1340. }
  1341. }
  1342. if (!_PyBaseExceptionGroup_Check(value)) {
  1343. if (print_exception(ctx, value) < 0) {
  1344. goto error;
  1345. }
  1346. }
  1347. else if (print_exception_group(ctx, value) < 0) {
  1348. goto error;
  1349. }
  1350. assert(!PyErr_Occurred());
  1351. _Py_LeaveRecursiveCall();
  1352. return 0;
  1353. error:
  1354. _Py_LeaveRecursiveCall();
  1355. return -1;
  1356. }
  1357. #define PyErr_MAX_GROUP_WIDTH 15
  1358. #define PyErr_MAX_GROUP_DEPTH 10
  1359. void
  1360. _PyErr_Display(PyObject *file, PyObject *unused, PyObject *value, PyObject *tb)
  1361. {
  1362. assert(file != NULL && file != Py_None);
  1363. if (PyExceptionInstance_Check(value)
  1364. && tb != NULL && PyTraceBack_Check(tb)) {
  1365. /* Put the traceback on the exception, otherwise it won't get
  1366. displayed. See issue #18776. */
  1367. PyObject *cur_tb = PyException_GetTraceback(value);
  1368. if (cur_tb == NULL) {
  1369. PyException_SetTraceback(value, tb);
  1370. }
  1371. else {
  1372. Py_DECREF(cur_tb);
  1373. }
  1374. }
  1375. struct exception_print_context ctx;
  1376. ctx.file = file;
  1377. ctx.exception_group_depth = 0;
  1378. ctx.need_close = false;
  1379. ctx.max_group_width = PyErr_MAX_GROUP_WIDTH;
  1380. ctx.max_group_depth = PyErr_MAX_GROUP_DEPTH;
  1381. /* We choose to ignore seen being possibly NULL, and report
  1382. at least the main exception (it could be a MemoryError).
  1383. */
  1384. ctx.seen = PySet_New(NULL);
  1385. if (ctx.seen == NULL) {
  1386. PyErr_Clear();
  1387. }
  1388. if (print_exception_recursive(&ctx, value) < 0) {
  1389. PyErr_Clear();
  1390. _PyObject_Dump(value);
  1391. fprintf(stderr, "lost sys.stderr\n");
  1392. }
  1393. Py_XDECREF(ctx.seen);
  1394. /* Call file.flush() */
  1395. PyObject *res = _PyObject_CallMethodNoArgs(file, &_Py_ID(flush));
  1396. if (!res) {
  1397. /* Silently ignore file.flush() error */
  1398. PyErr_Clear();
  1399. }
  1400. else {
  1401. Py_DECREF(res);
  1402. }
  1403. }
  1404. void
  1405. PyErr_Display(PyObject *unused, PyObject *value, PyObject *tb)
  1406. {
  1407. PyThreadState *tstate = _PyThreadState_GET();
  1408. PyObject *file = _PySys_GetAttr(tstate, &_Py_ID(stderr));
  1409. if (file == NULL) {
  1410. _PyObject_Dump(value);
  1411. fprintf(stderr, "lost sys.stderr\n");
  1412. return;
  1413. }
  1414. if (file == Py_None) {
  1415. return;
  1416. }
  1417. Py_INCREF(file);
  1418. _PyErr_Display(file, NULL, value, tb);
  1419. Py_DECREF(file);
  1420. }
  1421. void _PyErr_DisplayException(PyObject *file, PyObject *exc)
  1422. {
  1423. _PyErr_Display(file, NULL, exc, NULL);
  1424. }
  1425. void PyErr_DisplayException(PyObject *exc)
  1426. {
  1427. PyErr_Display(NULL, exc, NULL);
  1428. }
  1429. PyObject *
  1430. PyRun_StringFlags(const char *str, int start, PyObject *globals,
  1431. PyObject *locals, PyCompilerFlags *flags)
  1432. {
  1433. PyObject *ret = NULL;
  1434. mod_ty mod;
  1435. PyArena *arena;
  1436. arena = _PyArena_New();
  1437. if (arena == NULL)
  1438. return NULL;
  1439. _Py_DECLARE_STR(anon_string, "<string>");
  1440. mod = _PyParser_ASTFromString(
  1441. str, &_Py_STR(anon_string), start, flags, arena);
  1442. if (mod != NULL)
  1443. ret = run_mod(mod, &_Py_STR(anon_string), globals, locals, flags, arena);
  1444. _PyArena_Free(arena);
  1445. return ret;
  1446. }
  1447. static PyObject *
  1448. pyrun_file(FILE *fp, PyObject *filename, int start, PyObject *globals,
  1449. PyObject *locals, int closeit, PyCompilerFlags *flags)
  1450. {
  1451. PyArena *arena = _PyArena_New();
  1452. if (arena == NULL) {
  1453. return NULL;
  1454. }
  1455. mod_ty mod;
  1456. mod = _PyParser_ASTFromFile(fp, filename, NULL, start, NULL, NULL,
  1457. flags, NULL, arena);
  1458. if (closeit) {
  1459. fclose(fp);
  1460. }
  1461. PyObject *ret;
  1462. if (mod != NULL) {
  1463. ret = run_mod(mod, filename, globals, locals, flags, arena);
  1464. }
  1465. else {
  1466. ret = NULL;
  1467. }
  1468. _PyArena_Free(arena);
  1469. return ret;
  1470. }
  1471. PyObject *
  1472. PyRun_FileExFlags(FILE *fp, const char *filename, int start, PyObject *globals,
  1473. PyObject *locals, int closeit, PyCompilerFlags *flags)
  1474. {
  1475. PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
  1476. if (filename_obj == NULL) {
  1477. return NULL;
  1478. }
  1479. PyObject *res = pyrun_file(fp, filename_obj, start, globals,
  1480. locals, closeit, flags);
  1481. Py_DECREF(filename_obj);
  1482. return res;
  1483. }
  1484. static void
  1485. flush_io_stream(PyThreadState *tstate, PyObject *name)
  1486. {
  1487. PyObject *f = _PySys_GetAttr(tstate, name);
  1488. if (f != NULL) {
  1489. PyObject *r = _PyObject_CallMethodNoArgs(f, &_Py_ID(flush));
  1490. if (r) {
  1491. Py_DECREF(r);
  1492. }
  1493. else {
  1494. PyErr_Clear();
  1495. }
  1496. }
  1497. }
  1498. static void
  1499. flush_io(void)
  1500. {
  1501. PyThreadState *tstate = _PyThreadState_GET();
  1502. PyObject *exc = _PyErr_GetRaisedException(tstate);
  1503. flush_io_stream(tstate, &_Py_ID(stderr));
  1504. flush_io_stream(tstate, &_Py_ID(stdout));
  1505. _PyErr_SetRaisedException(tstate, exc);
  1506. }
  1507. static PyObject *
  1508. run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, PyObject *locals)
  1509. {
  1510. PyObject *v;
  1511. /*
  1512. * We explicitly re-initialize _Py_UnhandledKeyboardInterrupt every eval
  1513. * _just in case_ someone is calling into an embedded Python where they
  1514. * don't care about an uncaught KeyboardInterrupt exception (why didn't they
  1515. * leave config.install_signal_handlers set to 0?!?) but then later call
  1516. * Py_Main() itself (which _checks_ this flag and dies with a signal after
  1517. * its interpreter exits). We don't want a previous embedded interpreter's
  1518. * uncaught exception to trigger an unexplained signal exit from a future
  1519. * Py_Main() based one.
  1520. */
  1521. // XXX Isn't this dealt with by the move to _PyRuntimeState?
  1522. _PyRuntime.signals.unhandled_keyboard_interrupt = 0;
  1523. /* Set globals['__builtins__'] if it doesn't exist */
  1524. if (globals != NULL && _PyDict_GetItemStringWithError(globals, "__builtins__") == NULL) {
  1525. if (PyErr_Occurred() ||
  1526. PyDict_SetItemString(globals, "__builtins__",
  1527. tstate->interp->builtins) < 0)
  1528. {
  1529. return NULL;
  1530. }
  1531. }
  1532. v = PyEval_EvalCode((PyObject*)co, globals, locals);
  1533. if (!v && _PyErr_Occurred(tstate) == PyExc_KeyboardInterrupt) {
  1534. _PyRuntime.signals.unhandled_keyboard_interrupt = 1;
  1535. }
  1536. return v;
  1537. }
  1538. static PyObject *
  1539. run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
  1540. PyCompilerFlags *flags, PyArena *arena)
  1541. {
  1542. PyThreadState *tstate = _PyThreadState_GET();
  1543. PyCodeObject *co = _PyAST_Compile(mod, filename, flags, -1, arena);
  1544. if (co == NULL)
  1545. return NULL;
  1546. if (_PySys_Audit(tstate, "exec", "O", co) < 0) {
  1547. Py_DECREF(co);
  1548. return NULL;
  1549. }
  1550. PyObject *v = run_eval_code_obj(tstate, co, globals, locals);
  1551. Py_DECREF(co);
  1552. return v;
  1553. }
  1554. static PyObject *
  1555. run_pyc_file(FILE *fp, PyObject *globals, PyObject *locals,
  1556. PyCompilerFlags *flags)
  1557. {
  1558. PyThreadState *tstate = _PyThreadState_GET();
  1559. PyCodeObject *co;
  1560. PyObject *v;
  1561. long magic;
  1562. long PyImport_GetMagicNumber(void);
  1563. magic = PyMarshal_ReadLongFromFile(fp);
  1564. if (magic != PyImport_GetMagicNumber()) {
  1565. if (!PyErr_Occurred())
  1566. PyErr_SetString(PyExc_RuntimeError,
  1567. "Bad magic number in .pyc file");
  1568. goto error;
  1569. }
  1570. /* Skip the rest of the header. */
  1571. (void) PyMarshal_ReadLongFromFile(fp);
  1572. (void) PyMarshal_ReadLongFromFile(fp);
  1573. (void) PyMarshal_ReadLongFromFile(fp);
  1574. if (PyErr_Occurred()) {
  1575. goto error;
  1576. }
  1577. v = PyMarshal_ReadLastObjectFromFile(fp);
  1578. if (v == NULL || !PyCode_Check(v)) {
  1579. Py_XDECREF(v);
  1580. PyErr_SetString(PyExc_RuntimeError,
  1581. "Bad code object in .pyc file");
  1582. goto error;
  1583. }
  1584. fclose(fp);
  1585. co = (PyCodeObject *)v;
  1586. v = run_eval_code_obj(tstate, co, globals, locals);
  1587. if (v && flags)
  1588. flags->cf_flags |= (co->co_flags & PyCF_MASK);
  1589. Py_DECREF(co);
  1590. return v;
  1591. error:
  1592. fclose(fp);
  1593. return NULL;
  1594. }
  1595. PyObject *
  1596. Py_CompileStringObject(const char *str, PyObject *filename, int start,
  1597. PyCompilerFlags *flags, int optimize)
  1598. {
  1599. PyCodeObject *co;
  1600. mod_ty mod;
  1601. PyArena *arena = _PyArena_New();
  1602. if (arena == NULL)
  1603. return NULL;
  1604. mod = _PyParser_ASTFromString(str, filename, start, flags, arena);
  1605. if (mod == NULL) {
  1606. _PyArena_Free(arena);
  1607. return NULL;
  1608. }
  1609. if (flags && (flags->cf_flags & PyCF_ONLY_AST)) {
  1610. PyObject *result = PyAST_mod2obj(mod);
  1611. _PyArena_Free(arena);
  1612. return result;
  1613. }
  1614. co = _PyAST_Compile(mod, filename, flags, optimize, arena);
  1615. _PyArena_Free(arena);
  1616. return (PyObject *)co;
  1617. }
  1618. PyObject *
  1619. Py_CompileStringExFlags(const char *str, const char *filename_str, int start,
  1620. PyCompilerFlags *flags, int optimize)
  1621. {
  1622. PyObject *filename, *co;
  1623. filename = PyUnicode_DecodeFSDefault(filename_str);
  1624. if (filename == NULL)
  1625. return NULL;
  1626. co = Py_CompileStringObject(str, filename, start, flags, optimize);
  1627. Py_DECREF(filename);
  1628. return co;
  1629. }
  1630. const char *
  1631. _Py_SourceAsString(PyObject *cmd, const char *funcname, const char *what, PyCompilerFlags *cf, PyObject **cmd_copy)
  1632. {
  1633. const char *str;
  1634. Py_ssize_t size;
  1635. Py_buffer view;
  1636. *cmd_copy = NULL;
  1637. if (PyUnicode_Check(cmd)) {
  1638. cf->cf_flags |= PyCF_IGNORE_COOKIE;
  1639. str = PyUnicode_AsUTF8AndSize(cmd, &size);
  1640. if (str == NULL)
  1641. return NULL;
  1642. }
  1643. else if (PyBytes_Check(cmd)) {
  1644. str = PyBytes_AS_STRING(cmd);
  1645. size = PyBytes_GET_SIZE(cmd);
  1646. }
  1647. else if (PyByteArray_Check(cmd)) {
  1648. str = PyByteArray_AS_STRING(cmd);
  1649. size = PyByteArray_GET_SIZE(cmd);
  1650. }
  1651. else if (PyObject_GetBuffer(cmd, &view, PyBUF_SIMPLE) == 0) {
  1652. /* Copy to NUL-terminated buffer. */
  1653. *cmd_copy = PyBytes_FromStringAndSize(
  1654. (const char *)view.buf, view.len);
  1655. PyBuffer_Release(&view);
  1656. if (*cmd_copy == NULL) {
  1657. return NULL;
  1658. }
  1659. str = PyBytes_AS_STRING(*cmd_copy);
  1660. size = PyBytes_GET_SIZE(*cmd_copy);
  1661. }
  1662. else {
  1663. PyErr_Format(PyExc_TypeError,
  1664. "%s() arg 1 must be a %s object",
  1665. funcname, what);
  1666. return NULL;
  1667. }
  1668. if (strlen(str) != (size_t)size) {
  1669. PyErr_SetString(PyExc_SyntaxError,
  1670. "source code string cannot contain null bytes");
  1671. Py_CLEAR(*cmd_copy);
  1672. return NULL;
  1673. }
  1674. return str;
  1675. }
  1676. #if defined(USE_STACKCHECK)
  1677. #if defined(WIN32) && defined(_MSC_VER)
  1678. /* Stack checking for Microsoft C */
  1679. #include <malloc.h>
  1680. #include <excpt.h>
  1681. /*
  1682. * Return non-zero when we run out of memory on the stack; zero otherwise.
  1683. */
  1684. int
  1685. PyOS_CheckStack(void)
  1686. {
  1687. __try {
  1688. /* alloca throws a stack overflow exception if there's
  1689. not enough space left on the stack */
  1690. alloca(PYOS_STACK_MARGIN * sizeof(void*));
  1691. return 0;
  1692. } __except (GetExceptionCode() == STATUS_STACK_OVERFLOW ?
  1693. EXCEPTION_EXECUTE_HANDLER :
  1694. EXCEPTION_CONTINUE_SEARCH) {
  1695. int errcode = _resetstkoflw();
  1696. if (errcode == 0)
  1697. {
  1698. Py_FatalError("Could not reset the stack!");
  1699. }
  1700. }
  1701. return 1;
  1702. }
  1703. #endif /* WIN32 && _MSC_VER */
  1704. /* Alternate implementations can be added here... */
  1705. #endif /* USE_STACKCHECK */
  1706. /* Deprecated C API functions still provided for binary compatibility */
  1707. #undef PyRun_AnyFile
  1708. PyAPI_FUNC(int)
  1709. PyRun_AnyFile(FILE *fp, const char *name)
  1710. {
  1711. return PyRun_AnyFileExFlags(fp, name, 0, NULL);
  1712. }
  1713. #undef PyRun_AnyFileEx
  1714. PyAPI_FUNC(int)
  1715. PyRun_AnyFileEx(FILE *fp, const char *name, int closeit)
  1716. {
  1717. return PyRun_AnyFileExFlags(fp, name, closeit, NULL);
  1718. }
  1719. #undef PyRun_AnyFileFlags
  1720. PyAPI_FUNC(int)
  1721. PyRun_AnyFileFlags(FILE *fp, const char *name, PyCompilerFlags *flags)
  1722. {
  1723. return PyRun_AnyFileExFlags(fp, name, 0, flags);
  1724. }
  1725. #undef PyRun_File
  1726. PyAPI_FUNC(PyObject *)
  1727. PyRun_File(FILE *fp, const char *p, int s, PyObject *g, PyObject *l)
  1728. {
  1729. return PyRun_FileExFlags(fp, p, s, g, l, 0, NULL);
  1730. }
  1731. #undef PyRun_FileEx
  1732. PyAPI_FUNC(PyObject *)
  1733. PyRun_FileEx(FILE *fp, const char *p, int s, PyObject *g, PyObject *l, int c)
  1734. {
  1735. return PyRun_FileExFlags(fp, p, s, g, l, c, NULL);
  1736. }
  1737. #undef PyRun_FileFlags
  1738. PyAPI_FUNC(PyObject *)
  1739. PyRun_FileFlags(FILE *fp, const char *p, int s, PyObject *g, PyObject *l,
  1740. PyCompilerFlags *flags)
  1741. {
  1742. return PyRun_FileExFlags(fp, p, s, g, l, 0, flags);
  1743. }
  1744. #undef PyRun_SimpleFile
  1745. PyAPI_FUNC(int)
  1746. PyRun_SimpleFile(FILE *f, const char *p)
  1747. {
  1748. return PyRun_SimpleFileExFlags(f, p, 0, NULL);
  1749. }
  1750. #undef PyRun_SimpleFileEx
  1751. PyAPI_FUNC(int)
  1752. PyRun_SimpleFileEx(FILE *f, const char *p, int c)
  1753. {
  1754. return PyRun_SimpleFileExFlags(f, p, c, NULL);
  1755. }
  1756. #undef PyRun_String
  1757. PyAPI_FUNC(PyObject *)
  1758. PyRun_String(const char *str, int s, PyObject *g, PyObject *l)
  1759. {
  1760. return PyRun_StringFlags(str, s, g, l, NULL);
  1761. }
  1762. #undef PyRun_SimpleString
  1763. PyAPI_FUNC(int)
  1764. PyRun_SimpleString(const char *s)
  1765. {
  1766. return PyRun_SimpleStringFlags(s, NULL);
  1767. }
  1768. #undef Py_CompileString
  1769. PyAPI_FUNC(PyObject *)
  1770. Py_CompileString(const char *str, const char *p, int s)
  1771. {
  1772. return Py_CompileStringExFlags(str, p, s, NULL, -1);
  1773. }
  1774. #undef Py_CompileStringFlags
  1775. PyAPI_FUNC(PyObject *)
  1776. Py_CompileStringFlags(const char *str, const char *p, int s,
  1777. PyCompilerFlags *flags)
  1778. {
  1779. return Py_CompileStringExFlags(str, p, s, flags, -1);
  1780. }
  1781. #undef PyRun_InteractiveOne
  1782. PyAPI_FUNC(int)
  1783. PyRun_InteractiveOne(FILE *f, const char *p)
  1784. {
  1785. return PyRun_InteractiveOneFlags(f, p, NULL);
  1786. }
  1787. #undef PyRun_InteractiveLoop
  1788. PyAPI_FUNC(int)
  1789. PyRun_InteractiveLoop(FILE *f, const char *p)
  1790. {
  1791. return PyRun_InteractiveLoopFlags(f, p, NULL);
  1792. }
  1793. #ifdef __cplusplus
  1794. }
  1795. #endif