errors.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960
  1. /* Error handling */
  2. #include "Python.h"
  3. #include "pycore_call.h" // _PyObject_CallNoArgs()
  4. #include "pycore_initconfig.h" // _PyStatus_ERR()
  5. #include "pycore_pyerrors.h" // _PyErr_Format()
  6. #include "pycore_pystate.h" // _PyThreadState_GET()
  7. #include "pycore_structseq.h" // _PyStructSequence_FiniBuiltin()
  8. #include "pycore_sysmodule.h" // _PySys_Audit()
  9. #include "pycore_traceback.h" // _PyTraceBack_FromFrame()
  10. #include <ctype.h>
  11. #ifdef MS_WINDOWS
  12. # include <windows.h>
  13. # include <winbase.h>
  14. # include <stdlib.h> // _sys_nerr
  15. #endif
  16. #ifdef __cplusplus
  17. extern "C" {
  18. #endif
  19. /* Forward declarations */
  20. static PyObject *
  21. _PyErr_FormatV(PyThreadState *tstate, PyObject *exception,
  22. const char *format, va_list vargs);
  23. void
  24. _PyErr_SetRaisedException(PyThreadState *tstate, PyObject *exc)
  25. {
  26. PyObject *old_exc = tstate->current_exception;
  27. tstate->current_exception = exc;
  28. Py_XDECREF(old_exc);
  29. }
  30. static PyObject*
  31. _PyErr_CreateException(PyObject *exception_type, PyObject *value)
  32. {
  33. PyObject *exc;
  34. if (value == NULL || value == Py_None) {
  35. exc = _PyObject_CallNoArgs(exception_type);
  36. }
  37. else if (PyTuple_Check(value)) {
  38. exc = PyObject_Call(exception_type, value, NULL);
  39. }
  40. else {
  41. exc = PyObject_CallOneArg(exception_type, value);
  42. }
  43. if (exc != NULL && !PyExceptionInstance_Check(exc)) {
  44. PyErr_Format(PyExc_TypeError,
  45. "calling %R should have returned an instance of "
  46. "BaseException, not %s",
  47. exception_type, Py_TYPE(exc)->tp_name);
  48. Py_CLEAR(exc);
  49. }
  50. return exc;
  51. }
  52. void
  53. _PyErr_Restore(PyThreadState *tstate, PyObject *type, PyObject *value,
  54. PyObject *traceback)
  55. {
  56. if (type == NULL) {
  57. assert(value == NULL);
  58. assert(traceback == NULL);
  59. _PyErr_SetRaisedException(tstate, NULL);
  60. return;
  61. }
  62. assert(PyExceptionClass_Check(type));
  63. if (value != NULL && type == (PyObject *)Py_TYPE(value)) {
  64. /* Already normalized */
  65. assert(((PyBaseExceptionObject *)value)->traceback != Py_None);
  66. }
  67. else {
  68. PyObject *exc = _PyErr_CreateException(type, value);
  69. Py_XDECREF(value);
  70. if (exc == NULL) {
  71. Py_DECREF(type);
  72. Py_XDECREF(traceback);
  73. return;
  74. }
  75. value = exc;
  76. }
  77. assert(PyExceptionInstance_Check(value));
  78. if (traceback != NULL && !PyTraceBack_Check(traceback)) {
  79. if (traceback == Py_None) {
  80. Py_DECREF(Py_None);
  81. traceback = NULL;
  82. }
  83. else {
  84. PyErr_SetString(PyExc_TypeError, "traceback must be a Traceback or None");
  85. Py_XDECREF(value);
  86. Py_DECREF(type);
  87. Py_XDECREF(traceback);
  88. return;
  89. }
  90. }
  91. PyObject *old_traceback = ((PyBaseExceptionObject *)value)->traceback;
  92. ((PyBaseExceptionObject *)value)->traceback = traceback;
  93. Py_XDECREF(old_traceback);
  94. _PyErr_SetRaisedException(tstate, value);
  95. Py_DECREF(type);
  96. }
  97. void
  98. PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
  99. {
  100. PyThreadState *tstate = _PyThreadState_GET();
  101. _PyErr_Restore(tstate, type, value, traceback);
  102. }
  103. void
  104. PyErr_SetRaisedException(PyObject *exc)
  105. {
  106. PyThreadState *tstate = _PyThreadState_GET();
  107. _PyErr_SetRaisedException(tstate, exc);
  108. }
  109. _PyErr_StackItem *
  110. _PyErr_GetTopmostException(PyThreadState *tstate)
  111. {
  112. _PyErr_StackItem *exc_info = tstate->exc_info;
  113. assert(exc_info);
  114. while ((exc_info->exc_value == NULL || exc_info->exc_value == Py_None) &&
  115. exc_info->previous_item != NULL)
  116. {
  117. exc_info = exc_info->previous_item;
  118. }
  119. return exc_info;
  120. }
  121. static PyObject *
  122. get_normalization_failure_note(PyThreadState *tstate, PyObject *exception, PyObject *value)
  123. {
  124. PyObject *args = PyObject_Repr(value);
  125. if (args == NULL) {
  126. _PyErr_Clear(tstate);
  127. args = PyUnicode_FromFormat("<unknown>");
  128. }
  129. PyObject *note;
  130. const char *tpname = ((PyTypeObject*)exception)->tp_name;
  131. if (args == NULL) {
  132. _PyErr_Clear(tstate);
  133. note = PyUnicode_FromFormat("Normalization failed: type=%s", tpname);
  134. }
  135. else {
  136. note = PyUnicode_FromFormat("Normalization failed: type=%s args=%S",
  137. tpname, args);
  138. Py_DECREF(args);
  139. }
  140. return note;
  141. }
  142. void
  143. _PyErr_SetObject(PyThreadState *tstate, PyObject *exception, PyObject *value)
  144. {
  145. PyObject *exc_value;
  146. PyObject *tb = NULL;
  147. if (exception != NULL &&
  148. !PyExceptionClass_Check(exception)) {
  149. _PyErr_Format(tstate, PyExc_SystemError,
  150. "_PyErr_SetObject: "
  151. "exception %R is not a BaseException subclass",
  152. exception);
  153. return;
  154. }
  155. /* Normalize the exception */
  156. int is_subclass = 0;
  157. if (value != NULL && PyExceptionInstance_Check(value)) {
  158. is_subclass = PyObject_IsSubclass((PyObject *)Py_TYPE(value), exception);
  159. if (is_subclass < 0) {
  160. return;
  161. }
  162. }
  163. Py_XINCREF(value);
  164. if (!is_subclass) {
  165. /* We must normalize the value right now */
  166. /* Issue #23571: functions must not be called with an
  167. exception set */
  168. _PyErr_Clear(tstate);
  169. PyObject *fixed_value = _PyErr_CreateException(exception, value);
  170. if (fixed_value == NULL) {
  171. PyObject *exc = _PyErr_GetRaisedException(tstate);
  172. assert(PyExceptionInstance_Check(exc));
  173. PyObject *note = get_normalization_failure_note(tstate, exception, value);
  174. Py_XDECREF(value);
  175. if (note != NULL) {
  176. /* ignore errors in _PyException_AddNote - they will be overwritten below */
  177. _PyException_AddNote(exc, note);
  178. Py_DECREF(note);
  179. }
  180. _PyErr_SetRaisedException(tstate, exc);
  181. return;
  182. }
  183. Py_XSETREF(value, fixed_value);
  184. }
  185. exc_value = _PyErr_GetTopmostException(tstate)->exc_value;
  186. if (exc_value != NULL && exc_value != Py_None) {
  187. /* Implicit exception chaining */
  188. Py_INCREF(exc_value);
  189. /* Avoid creating new reference cycles through the
  190. context chain, while taking care not to hang on
  191. pre-existing ones.
  192. This is O(chain length) but context chains are
  193. usually very short. Sensitive readers may try
  194. to inline the call to PyException_GetContext. */
  195. if (exc_value != value) {
  196. PyObject *o = exc_value, *context;
  197. PyObject *slow_o = o; /* Floyd's cycle detection algo */
  198. int slow_update_toggle = 0;
  199. while ((context = PyException_GetContext(o))) {
  200. Py_DECREF(context);
  201. if (context == value) {
  202. PyException_SetContext(o, NULL);
  203. break;
  204. }
  205. o = context;
  206. if (o == slow_o) {
  207. /* pre-existing cycle - all exceptions on the
  208. path were visited and checked. */
  209. break;
  210. }
  211. if (slow_update_toggle) {
  212. slow_o = PyException_GetContext(slow_o);
  213. Py_DECREF(slow_o);
  214. }
  215. slow_update_toggle = !slow_update_toggle;
  216. }
  217. PyException_SetContext(value, exc_value);
  218. }
  219. else {
  220. Py_DECREF(exc_value);
  221. }
  222. }
  223. assert(value != NULL);
  224. if (PyExceptionInstance_Check(value))
  225. tb = PyException_GetTraceback(value);
  226. _PyErr_Restore(tstate, Py_NewRef(Py_TYPE(value)), value, tb);
  227. }
  228. void
  229. PyErr_SetObject(PyObject *exception, PyObject *value)
  230. {
  231. PyThreadState *tstate = _PyThreadState_GET();
  232. _PyErr_SetObject(tstate, exception, value);
  233. }
  234. /* Set a key error with the specified argument, wrapping it in a
  235. * tuple automatically so that tuple keys are not unpacked as the
  236. * exception arguments. */
  237. void
  238. _PyErr_SetKeyError(PyObject *arg)
  239. {
  240. PyThreadState *tstate = _PyThreadState_GET();
  241. PyObject *tup = PyTuple_Pack(1, arg);
  242. if (!tup) {
  243. /* caller will expect error to be set anyway */
  244. return;
  245. }
  246. _PyErr_SetObject(tstate, PyExc_KeyError, tup);
  247. Py_DECREF(tup);
  248. }
  249. void
  250. _PyErr_SetNone(PyThreadState *tstate, PyObject *exception)
  251. {
  252. _PyErr_SetObject(tstate, exception, (PyObject *)NULL);
  253. }
  254. void
  255. PyErr_SetNone(PyObject *exception)
  256. {
  257. PyThreadState *tstate = _PyThreadState_GET();
  258. _PyErr_SetNone(tstate, exception);
  259. }
  260. void
  261. _PyErr_SetString(PyThreadState *tstate, PyObject *exception,
  262. const char *string)
  263. {
  264. PyObject *value = PyUnicode_FromString(string);
  265. if (value != NULL) {
  266. _PyErr_SetObject(tstate, exception, value);
  267. Py_DECREF(value);
  268. }
  269. }
  270. void
  271. PyErr_SetString(PyObject *exception, const char *string)
  272. {
  273. PyThreadState *tstate = _PyThreadState_GET();
  274. _PyErr_SetString(tstate, exception, string);
  275. }
  276. PyObject* _Py_HOT_FUNCTION
  277. PyErr_Occurred(void)
  278. {
  279. /* The caller must hold the GIL. */
  280. assert(PyGILState_Check());
  281. PyThreadState *tstate = _PyThreadState_GET();
  282. return _PyErr_Occurred(tstate);
  283. }
  284. int
  285. PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
  286. {
  287. if (err == NULL || exc == NULL) {
  288. /* maybe caused by "import exceptions" that failed early on */
  289. return 0;
  290. }
  291. if (PyTuple_Check(exc)) {
  292. Py_ssize_t i, n;
  293. n = PyTuple_Size(exc);
  294. for (i = 0; i < n; i++) {
  295. /* Test recursively */
  296. if (PyErr_GivenExceptionMatches(
  297. err, PyTuple_GET_ITEM(exc, i)))
  298. {
  299. return 1;
  300. }
  301. }
  302. return 0;
  303. }
  304. /* err might be an instance, so check its class. */
  305. if (PyExceptionInstance_Check(err))
  306. err = PyExceptionInstance_Class(err);
  307. if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
  308. return PyType_IsSubtype((PyTypeObject *)err, (PyTypeObject *)exc);
  309. }
  310. return err == exc;
  311. }
  312. int
  313. _PyErr_ExceptionMatches(PyThreadState *tstate, PyObject *exc)
  314. {
  315. return PyErr_GivenExceptionMatches(_PyErr_Occurred(tstate), exc);
  316. }
  317. int
  318. PyErr_ExceptionMatches(PyObject *exc)
  319. {
  320. PyThreadState *tstate = _PyThreadState_GET();
  321. return _PyErr_ExceptionMatches(tstate, exc);
  322. }
  323. #ifndef Py_NORMALIZE_RECURSION_LIMIT
  324. #define Py_NORMALIZE_RECURSION_LIMIT 32
  325. #endif
  326. /* Used in many places to normalize a raised exception, including in
  327. eval_code2(), do_raise(), and PyErr_Print()
  328. XXX: should PyErr_NormalizeException() also call
  329. PyException_SetTraceback() with the resulting value and tb?
  330. */
  331. void
  332. _PyErr_NormalizeException(PyThreadState *tstate, PyObject **exc,
  333. PyObject **val, PyObject **tb)
  334. {
  335. int recursion_depth = 0;
  336. tstate->recursion_headroom++;
  337. PyObject *type, *value, *initial_tb;
  338. restart:
  339. type = *exc;
  340. if (type == NULL) {
  341. /* There was no exception, so nothing to do. */
  342. tstate->recursion_headroom--;
  343. return;
  344. }
  345. value = *val;
  346. /* If PyErr_SetNone() was used, the value will have been actually
  347. set to NULL.
  348. */
  349. if (!value) {
  350. value = Py_NewRef(Py_None);
  351. }
  352. /* Normalize the exception so that if the type is a class, the
  353. value will be an instance.
  354. */
  355. if (PyExceptionClass_Check(type)) {
  356. PyObject *inclass = NULL;
  357. int is_subclass = 0;
  358. if (PyExceptionInstance_Check(value)) {
  359. inclass = PyExceptionInstance_Class(value);
  360. is_subclass = PyObject_IsSubclass(inclass, type);
  361. if (is_subclass < 0) {
  362. goto error;
  363. }
  364. }
  365. /* If the value was not an instance, or is not an instance
  366. whose class is (or is derived from) type, then use the
  367. value as an argument to instantiation of the type
  368. class.
  369. */
  370. if (!is_subclass) {
  371. PyObject *fixed_value = _PyErr_CreateException(type, value);
  372. if (fixed_value == NULL) {
  373. goto error;
  374. }
  375. Py_SETREF(value, fixed_value);
  376. }
  377. /* If the class of the instance doesn't exactly match the
  378. class of the type, believe the instance.
  379. */
  380. else if (inclass != type) {
  381. Py_SETREF(type, Py_NewRef(inclass));
  382. }
  383. }
  384. *exc = type;
  385. *val = value;
  386. tstate->recursion_headroom--;
  387. return;
  388. error:
  389. Py_DECREF(type);
  390. Py_DECREF(value);
  391. recursion_depth++;
  392. if (recursion_depth == Py_NORMALIZE_RECURSION_LIMIT) {
  393. _PyErr_SetString(tstate, PyExc_RecursionError,
  394. "maximum recursion depth exceeded "
  395. "while normalizing an exception");
  396. }
  397. /* If the new exception doesn't set a traceback and the old
  398. exception had a traceback, use the old traceback for the
  399. new exception. It's better than nothing.
  400. */
  401. initial_tb = *tb;
  402. _PyErr_Fetch(tstate, exc, val, tb);
  403. assert(*exc != NULL);
  404. if (initial_tb != NULL) {
  405. if (*tb == NULL)
  406. *tb = initial_tb;
  407. else
  408. Py_DECREF(initial_tb);
  409. }
  410. /* Abort when Py_NORMALIZE_RECURSION_LIMIT has been exceeded, and the
  411. corresponding RecursionError could not be normalized, and the
  412. MemoryError raised when normalize this RecursionError could not be
  413. normalized. */
  414. if (recursion_depth >= Py_NORMALIZE_RECURSION_LIMIT + 2) {
  415. if (PyErr_GivenExceptionMatches(*exc, PyExc_MemoryError)) {
  416. Py_FatalError("Cannot recover from MemoryErrors "
  417. "while normalizing exceptions.");
  418. }
  419. else {
  420. Py_FatalError("Cannot recover from the recursive normalization "
  421. "of an exception.");
  422. }
  423. }
  424. goto restart;
  425. }
  426. void
  427. PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
  428. {
  429. PyThreadState *tstate = _PyThreadState_GET();
  430. _PyErr_NormalizeException(tstate, exc, val, tb);
  431. }
  432. PyObject *
  433. _PyErr_GetRaisedException(PyThreadState *tstate) {
  434. PyObject *exc = tstate->current_exception;
  435. tstate->current_exception = NULL;
  436. return exc;
  437. }
  438. PyObject *
  439. PyErr_GetRaisedException(void)
  440. {
  441. PyThreadState *tstate = _PyThreadState_GET();
  442. return _PyErr_GetRaisedException(tstate);
  443. }
  444. void
  445. _PyErr_Fetch(PyThreadState *tstate, PyObject **p_type, PyObject **p_value,
  446. PyObject **p_traceback)
  447. {
  448. PyObject *exc = _PyErr_GetRaisedException(tstate);
  449. *p_value = exc;
  450. if (exc == NULL) {
  451. *p_type = NULL;
  452. *p_traceback = NULL;
  453. }
  454. else {
  455. *p_type = Py_NewRef(Py_TYPE(exc));
  456. *p_traceback = Py_XNewRef(((PyBaseExceptionObject *)exc)->traceback);
  457. }
  458. }
  459. void
  460. PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
  461. {
  462. PyThreadState *tstate = _PyThreadState_GET();
  463. _PyErr_Fetch(tstate, p_type, p_value, p_traceback);
  464. }
  465. void
  466. _PyErr_Clear(PyThreadState *tstate)
  467. {
  468. _PyErr_Restore(tstate, NULL, NULL, NULL);
  469. }
  470. void
  471. PyErr_Clear(void)
  472. {
  473. PyThreadState *tstate = _PyThreadState_GET();
  474. _PyErr_Clear(tstate);
  475. }
  476. static PyObject*
  477. get_exc_type(PyObject *exc_value) /* returns a borrowed ref */
  478. {
  479. if (exc_value == NULL || exc_value == Py_None) {
  480. return Py_None;
  481. }
  482. else {
  483. assert(PyExceptionInstance_Check(exc_value));
  484. PyObject *type = PyExceptionInstance_Class(exc_value);
  485. assert(type != NULL);
  486. return type;
  487. }
  488. }
  489. static PyObject*
  490. get_exc_traceback(PyObject *exc_value) /* returns a borrowed ref */
  491. {
  492. if (exc_value == NULL || exc_value == Py_None) {
  493. return Py_None;
  494. }
  495. else {
  496. assert(PyExceptionInstance_Check(exc_value));
  497. PyObject *tb = PyException_GetTraceback(exc_value);
  498. Py_XDECREF(tb);
  499. return tb ? tb : Py_None;
  500. }
  501. }
  502. void
  503. _PyErr_GetExcInfo(PyThreadState *tstate,
  504. PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
  505. {
  506. _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
  507. *p_type = Py_XNewRef(get_exc_type(exc_info->exc_value));
  508. *p_value = Py_XNewRef(exc_info->exc_value);
  509. *p_traceback = Py_XNewRef(get_exc_traceback(exc_info->exc_value));
  510. }
  511. PyObject*
  512. _PyErr_GetHandledException(PyThreadState *tstate)
  513. {
  514. _PyErr_StackItem *exc_info = _PyErr_GetTopmostException(tstate);
  515. PyObject *exc = exc_info->exc_value;
  516. if (exc == NULL || exc == Py_None) {
  517. return NULL;
  518. }
  519. return Py_NewRef(exc);
  520. }
  521. PyObject*
  522. PyErr_GetHandledException(void)
  523. {
  524. PyThreadState *tstate = _PyThreadState_GET();
  525. return _PyErr_GetHandledException(tstate);
  526. }
  527. void
  528. _PyErr_SetHandledException(PyThreadState *tstate, PyObject *exc)
  529. {
  530. Py_XSETREF(tstate->exc_info->exc_value, Py_XNewRef(exc));
  531. }
  532. void
  533. PyErr_SetHandledException(PyObject *exc)
  534. {
  535. PyThreadState *tstate = _PyThreadState_GET();
  536. _PyErr_SetHandledException(tstate, exc);
  537. }
  538. void
  539. PyErr_GetExcInfo(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
  540. {
  541. PyThreadState *tstate = _PyThreadState_GET();
  542. _PyErr_GetExcInfo(tstate, p_type, p_value, p_traceback);
  543. }
  544. void
  545. PyErr_SetExcInfo(PyObject *type, PyObject *value, PyObject *traceback)
  546. {
  547. PyErr_SetHandledException(value);
  548. Py_XDECREF(value);
  549. /* These args are no longer used, but we still need to steal a ref */
  550. Py_XDECREF(type);
  551. Py_XDECREF(traceback);
  552. }
  553. PyObject*
  554. _PyErr_StackItemToExcInfoTuple(_PyErr_StackItem *err_info)
  555. {
  556. PyObject *exc_value = err_info->exc_value;
  557. assert(exc_value == NULL ||
  558. exc_value == Py_None ||
  559. PyExceptionInstance_Check(exc_value));
  560. PyObject *exc_type = get_exc_type(exc_value);
  561. PyObject *exc_traceback = get_exc_traceback(exc_value);
  562. return Py_BuildValue(
  563. "(OOO)",
  564. exc_type ? exc_type : Py_None,
  565. exc_value ? exc_value : Py_None,
  566. exc_traceback ? exc_traceback : Py_None);
  567. }
  568. /* Like PyErr_Restore(), but if an exception is already set,
  569. set the context associated with it.
  570. The caller is responsible for ensuring that this call won't create
  571. any cycles in the exception context chain. */
  572. void
  573. _PyErr_ChainExceptions(PyObject *typ, PyObject *val, PyObject *tb)
  574. {
  575. if (typ == NULL)
  576. return;
  577. PyThreadState *tstate = _PyThreadState_GET();
  578. if (!PyExceptionClass_Check(typ)) {
  579. _PyErr_Format(tstate, PyExc_SystemError,
  580. "_PyErr_ChainExceptions: "
  581. "exception %R is not a BaseException subclass",
  582. typ);
  583. return;
  584. }
  585. if (_PyErr_Occurred(tstate)) {
  586. _PyErr_NormalizeException(tstate, &typ, &val, &tb);
  587. if (tb != NULL) {
  588. PyException_SetTraceback(val, tb);
  589. Py_DECREF(tb);
  590. }
  591. Py_DECREF(typ);
  592. PyObject *exc2 = _PyErr_GetRaisedException(tstate);
  593. PyException_SetContext(exc2, val);
  594. _PyErr_SetRaisedException(tstate, exc2);
  595. }
  596. else {
  597. _PyErr_Restore(tstate, typ, val, tb);
  598. }
  599. }
  600. /* Like PyErr_SetRaisedException(), but if an exception is already set,
  601. set the context associated with it.
  602. The caller is responsible for ensuring that this call won't create
  603. any cycles in the exception context chain. */
  604. void
  605. _PyErr_ChainExceptions1(PyObject *exc)
  606. {
  607. if (exc == NULL) {
  608. return;
  609. }
  610. PyThreadState *tstate = _PyThreadState_GET();
  611. if (_PyErr_Occurred(tstate)) {
  612. PyObject *exc2 = _PyErr_GetRaisedException(tstate);
  613. PyException_SetContext(exc2, exc);
  614. _PyErr_SetRaisedException(tstate, exc2);
  615. }
  616. else {
  617. _PyErr_SetRaisedException(tstate, exc);
  618. }
  619. }
  620. /* Set the currently set exception's context to the given exception.
  621. If the provided exc_info is NULL, then the current Python thread state's
  622. exc_info will be used for the context instead.
  623. This function can only be called when _PyErr_Occurred() is true.
  624. Also, this function won't create any cycles in the exception context
  625. chain to the extent that _PyErr_SetObject ensures this. */
  626. void
  627. _PyErr_ChainStackItem(_PyErr_StackItem *exc_info)
  628. {
  629. PyThreadState *tstate = _PyThreadState_GET();
  630. assert(_PyErr_Occurred(tstate));
  631. int exc_info_given;
  632. if (exc_info == NULL) {
  633. exc_info_given = 0;
  634. exc_info = tstate->exc_info;
  635. } else {
  636. exc_info_given = 1;
  637. }
  638. if (exc_info->exc_value == NULL || exc_info->exc_value == Py_None) {
  639. return;
  640. }
  641. _PyErr_StackItem *saved_exc_info;
  642. if (exc_info_given) {
  643. /* Temporarily set the thread state's exc_info since this is what
  644. _PyErr_SetObject uses for implicit exception chaining. */
  645. saved_exc_info = tstate->exc_info;
  646. tstate->exc_info = exc_info;
  647. }
  648. PyObject *typ, *val, *tb;
  649. _PyErr_Fetch(tstate, &typ, &val, &tb);
  650. /* _PyErr_SetObject sets the context from PyThreadState. */
  651. _PyErr_SetObject(tstate, typ, val);
  652. Py_DECREF(typ); // since _PyErr_Occurred was true
  653. Py_XDECREF(val);
  654. Py_XDECREF(tb);
  655. if (exc_info_given) {
  656. tstate->exc_info = saved_exc_info;
  657. }
  658. }
  659. static PyObject *
  660. _PyErr_FormatVFromCause(PyThreadState *tstate, PyObject *exception,
  661. const char *format, va_list vargs)
  662. {
  663. assert(_PyErr_Occurred(tstate));
  664. PyObject *exc = _PyErr_GetRaisedException(tstate);
  665. assert(!_PyErr_Occurred(tstate));
  666. _PyErr_FormatV(tstate, exception, format, vargs);
  667. PyObject *exc2 = _PyErr_GetRaisedException(tstate);
  668. PyException_SetCause(exc2, Py_NewRef(exc));
  669. PyException_SetContext(exc2, Py_NewRef(exc));
  670. Py_DECREF(exc);
  671. _PyErr_SetRaisedException(tstate, exc2);
  672. return NULL;
  673. }
  674. PyObject *
  675. _PyErr_FormatFromCauseTstate(PyThreadState *tstate, PyObject *exception,
  676. const char *format, ...)
  677. {
  678. va_list vargs;
  679. va_start(vargs, format);
  680. _PyErr_FormatVFromCause(tstate, exception, format, vargs);
  681. va_end(vargs);
  682. return NULL;
  683. }
  684. PyObject *
  685. _PyErr_FormatFromCause(PyObject *exception, const char *format, ...)
  686. {
  687. PyThreadState *tstate = _PyThreadState_GET();
  688. va_list vargs;
  689. va_start(vargs, format);
  690. _PyErr_FormatVFromCause(tstate, exception, format, vargs);
  691. va_end(vargs);
  692. return NULL;
  693. }
  694. /* Convenience functions to set a type error exception and return 0 */
  695. int
  696. PyErr_BadArgument(void)
  697. {
  698. PyThreadState *tstate = _PyThreadState_GET();
  699. _PyErr_SetString(tstate, PyExc_TypeError,
  700. "bad argument type for built-in operation");
  701. return 0;
  702. }
  703. PyObject *
  704. PyErr_NoMemory(void)
  705. {
  706. PyThreadState *tstate = _PyThreadState_GET();
  707. return _PyErr_NoMemory(tstate);
  708. }
  709. PyObject *
  710. PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
  711. {
  712. return PyErr_SetFromErrnoWithFilenameObjects(exc, filenameObject, NULL);
  713. }
  714. PyObject *
  715. PyErr_SetFromErrnoWithFilenameObjects(PyObject *exc, PyObject *filenameObject, PyObject *filenameObject2)
  716. {
  717. PyThreadState *tstate = _PyThreadState_GET();
  718. PyObject *message;
  719. PyObject *v, *args;
  720. int i = errno;
  721. #ifdef MS_WINDOWS
  722. WCHAR *s_buf = NULL;
  723. #endif /* Unix/Windows */
  724. #ifdef EINTR
  725. if (i == EINTR && PyErr_CheckSignals())
  726. return NULL;
  727. #endif
  728. #ifndef MS_WINDOWS
  729. if (i != 0) {
  730. const char *s = strerror(i);
  731. message = PyUnicode_DecodeLocale(s, "surrogateescape");
  732. }
  733. else {
  734. /* Sometimes errno didn't get set */
  735. message = PyUnicode_FromString("Error");
  736. }
  737. #else
  738. if (i == 0)
  739. message = PyUnicode_FromString("Error"); /* Sometimes errno didn't get set */
  740. else
  741. {
  742. /* Note that the Win32 errors do not lineup with the
  743. errno error. So if the error is in the MSVC error
  744. table, we use it, otherwise we assume it really _is_
  745. a Win32 error code
  746. */
  747. if (i > 0 && i < _sys_nerr) {
  748. message = PyUnicode_FromString(_sys_errlist[i]);
  749. }
  750. else {
  751. int len = FormatMessageW(
  752. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  753. FORMAT_MESSAGE_FROM_SYSTEM |
  754. FORMAT_MESSAGE_IGNORE_INSERTS,
  755. NULL, /* no message source */
  756. i,
  757. MAKELANGID(LANG_NEUTRAL,
  758. SUBLANG_DEFAULT),
  759. /* Default language */
  760. (LPWSTR) &s_buf,
  761. 0, /* size not used */
  762. NULL); /* no args */
  763. if (len==0) {
  764. /* Only ever seen this in out-of-mem
  765. situations */
  766. s_buf = NULL;
  767. message = PyUnicode_FromFormat("Windows Error 0x%x", i);
  768. } else {
  769. /* remove trailing cr/lf and dots */
  770. while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
  771. s_buf[--len] = L'\0';
  772. message = PyUnicode_FromWideChar(s_buf, len);
  773. }
  774. }
  775. }
  776. #endif /* Unix/Windows */
  777. if (message == NULL)
  778. {
  779. #ifdef MS_WINDOWS
  780. LocalFree(s_buf);
  781. #endif
  782. return NULL;
  783. }
  784. if (filenameObject != NULL) {
  785. if (filenameObject2 != NULL)
  786. args = Py_BuildValue("(iOOiO)", i, message, filenameObject, 0, filenameObject2);
  787. else
  788. args = Py_BuildValue("(iOO)", i, message, filenameObject);
  789. } else {
  790. assert(filenameObject2 == NULL);
  791. args = Py_BuildValue("(iO)", i, message);
  792. }
  793. Py_DECREF(message);
  794. if (args != NULL) {
  795. v = PyObject_Call(exc, args, NULL);
  796. Py_DECREF(args);
  797. if (v != NULL) {
  798. _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(v), v);
  799. Py_DECREF(v);
  800. }
  801. }
  802. #ifdef MS_WINDOWS
  803. LocalFree(s_buf);
  804. #endif
  805. return NULL;
  806. }
  807. PyObject *
  808. PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
  809. {
  810. PyObject *name = NULL;
  811. if (filename) {
  812. int i = errno;
  813. name = PyUnicode_DecodeFSDefault(filename);
  814. if (name == NULL) {
  815. return NULL;
  816. }
  817. errno = i;
  818. }
  819. PyObject *result = PyErr_SetFromErrnoWithFilenameObjects(exc, name, NULL);
  820. Py_XDECREF(name);
  821. return result;
  822. }
  823. PyObject *
  824. PyErr_SetFromErrno(PyObject *exc)
  825. {
  826. return PyErr_SetFromErrnoWithFilenameObjects(exc, NULL, NULL);
  827. }
  828. #ifdef MS_WINDOWS
  829. /* Windows specific error code handling */
  830. PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
  831. PyObject *exc,
  832. int ierr,
  833. PyObject *filenameObject)
  834. {
  835. return PyErr_SetExcFromWindowsErrWithFilenameObjects(exc, ierr,
  836. filenameObject, NULL);
  837. }
  838. PyObject *PyErr_SetExcFromWindowsErrWithFilenameObjects(
  839. PyObject *exc,
  840. int ierr,
  841. PyObject *filenameObject,
  842. PyObject *filenameObject2)
  843. {
  844. PyThreadState *tstate = _PyThreadState_GET();
  845. int len;
  846. WCHAR *s_buf = NULL; /* Free via LocalFree */
  847. PyObject *message;
  848. PyObject *args, *v;
  849. DWORD err = (DWORD)ierr;
  850. if (err==0) {
  851. err = GetLastError();
  852. }
  853. len = FormatMessageW(
  854. /* Error API error */
  855. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  856. FORMAT_MESSAGE_FROM_SYSTEM |
  857. FORMAT_MESSAGE_IGNORE_INSERTS,
  858. NULL, /* no message source */
  859. err,
  860. MAKELANGID(LANG_NEUTRAL,
  861. SUBLANG_DEFAULT), /* Default language */
  862. (LPWSTR) &s_buf,
  863. 0, /* size not used */
  864. NULL); /* no args */
  865. if (len==0) {
  866. /* Only seen this in out of mem situations */
  867. message = PyUnicode_FromFormat("Windows Error 0x%x", err);
  868. s_buf = NULL;
  869. } else {
  870. /* remove trailing cr/lf and dots */
  871. while (len > 0 && (s_buf[len-1] <= L' ' || s_buf[len-1] == L'.'))
  872. s_buf[--len] = L'\0';
  873. message = PyUnicode_FromWideChar(s_buf, len);
  874. }
  875. if (message == NULL)
  876. {
  877. LocalFree(s_buf);
  878. return NULL;
  879. }
  880. if (filenameObject == NULL) {
  881. assert(filenameObject2 == NULL);
  882. filenameObject = filenameObject2 = Py_None;
  883. }
  884. else if (filenameObject2 == NULL)
  885. filenameObject2 = Py_None;
  886. /* This is the constructor signature for OSError.
  887. The POSIX translation will be figured out by the constructor. */
  888. args = Py_BuildValue("(iOOiO)", 0, message, filenameObject, err, filenameObject2);
  889. Py_DECREF(message);
  890. if (args != NULL) {
  891. v = PyObject_Call(exc, args, NULL);
  892. Py_DECREF(args);
  893. if (v != NULL) {
  894. _PyErr_SetObject(tstate, (PyObject *) Py_TYPE(v), v);
  895. Py_DECREF(v);
  896. }
  897. }
  898. LocalFree(s_buf);
  899. return NULL;
  900. }
  901. PyObject *PyErr_SetExcFromWindowsErrWithFilename(
  902. PyObject *exc,
  903. int ierr,
  904. const char *filename)
  905. {
  906. PyObject *name = NULL;
  907. if (filename) {
  908. if ((DWORD)ierr == 0) {
  909. ierr = (int)GetLastError();
  910. }
  911. name = PyUnicode_DecodeFSDefault(filename);
  912. if (name == NULL) {
  913. return NULL;
  914. }
  915. }
  916. PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObjects(exc,
  917. ierr,
  918. name,
  919. NULL);
  920. Py_XDECREF(name);
  921. return ret;
  922. }
  923. PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
  924. {
  925. return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
  926. }
  927. PyObject *PyErr_SetFromWindowsErr(int ierr)
  928. {
  929. return PyErr_SetExcFromWindowsErrWithFilename(PyExc_OSError,
  930. ierr, NULL);
  931. }
  932. PyObject *PyErr_SetFromWindowsErrWithFilename(
  933. int ierr,
  934. const char *filename)
  935. {
  936. PyObject *name = NULL;
  937. if (filename) {
  938. if ((DWORD)ierr == 0) {
  939. ierr = (int)GetLastError();
  940. }
  941. name = PyUnicode_DecodeFSDefault(filename);
  942. if (name == NULL) {
  943. return NULL;
  944. }
  945. }
  946. PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObjects(
  947. PyExc_OSError,
  948. ierr, name, NULL);
  949. Py_XDECREF(name);
  950. return result;
  951. }
  952. #endif /* MS_WINDOWS */
  953. static PyObject *
  954. _PyErr_SetImportErrorSubclassWithNameFrom(
  955. PyObject *exception, PyObject *msg,
  956. PyObject *name, PyObject *path, PyObject* from_name)
  957. {
  958. PyThreadState *tstate = _PyThreadState_GET();
  959. int issubclass;
  960. PyObject *kwargs, *error;
  961. issubclass = PyObject_IsSubclass(exception, PyExc_ImportError);
  962. if (issubclass < 0) {
  963. return NULL;
  964. }
  965. else if (!issubclass) {
  966. _PyErr_SetString(tstate, PyExc_TypeError,
  967. "expected a subclass of ImportError");
  968. return NULL;
  969. }
  970. if (msg == NULL) {
  971. _PyErr_SetString(tstate, PyExc_TypeError,
  972. "expected a message argument");
  973. return NULL;
  974. }
  975. if (name == NULL) {
  976. name = Py_None;
  977. }
  978. if (path == NULL) {
  979. path = Py_None;
  980. }
  981. if (from_name == NULL) {
  982. from_name = Py_None;
  983. }
  984. kwargs = PyDict_New();
  985. if (kwargs == NULL) {
  986. return NULL;
  987. }
  988. if (PyDict_SetItemString(kwargs, "name", name) < 0) {
  989. goto done;
  990. }
  991. if (PyDict_SetItemString(kwargs, "path", path) < 0) {
  992. goto done;
  993. }
  994. if (PyDict_SetItemString(kwargs, "name_from", from_name) < 0) {
  995. goto done;
  996. }
  997. error = PyObject_VectorcallDict(exception, &msg, 1, kwargs);
  998. if (error != NULL) {
  999. _PyErr_SetObject(tstate, (PyObject *)Py_TYPE(error), error);
  1000. Py_DECREF(error);
  1001. }
  1002. done:
  1003. Py_DECREF(kwargs);
  1004. return NULL;
  1005. }
  1006. PyObject *
  1007. PyErr_SetImportErrorSubclass(PyObject *exception, PyObject *msg,
  1008. PyObject *name, PyObject *path)
  1009. {
  1010. return _PyErr_SetImportErrorSubclassWithNameFrom(exception, msg, name, path, NULL);
  1011. }
  1012. PyObject *
  1013. _PyErr_SetImportErrorWithNameFrom(PyObject *msg, PyObject *name, PyObject *path, PyObject* from_name)
  1014. {
  1015. return _PyErr_SetImportErrorSubclassWithNameFrom(PyExc_ImportError, msg, name, path, from_name);
  1016. }
  1017. PyObject *
  1018. PyErr_SetImportError(PyObject *msg, PyObject *name, PyObject *path)
  1019. {
  1020. return PyErr_SetImportErrorSubclass(PyExc_ImportError, msg, name, path);
  1021. }
  1022. void
  1023. _PyErr_BadInternalCall(const char *filename, int lineno)
  1024. {
  1025. PyThreadState *tstate = _PyThreadState_GET();
  1026. _PyErr_Format(tstate, PyExc_SystemError,
  1027. "%s:%d: bad argument to internal function",
  1028. filename, lineno);
  1029. }
  1030. /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
  1031. export the entry point for existing object code: */
  1032. #undef PyErr_BadInternalCall
  1033. void
  1034. PyErr_BadInternalCall(void)
  1035. {
  1036. assert(0 && "bad argument to internal function");
  1037. PyThreadState *tstate = _PyThreadState_GET();
  1038. _PyErr_SetString(tstate, PyExc_SystemError,
  1039. "bad argument to internal function");
  1040. }
  1041. #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
  1042. static PyObject *
  1043. _PyErr_FormatV(PyThreadState *tstate, PyObject *exception,
  1044. const char *format, va_list vargs)
  1045. {
  1046. PyObject* string;
  1047. /* Issue #23571: PyUnicode_FromFormatV() must not be called with an
  1048. exception set, it calls arbitrary Python code like PyObject_Repr() */
  1049. _PyErr_Clear(tstate);
  1050. string = PyUnicode_FromFormatV(format, vargs);
  1051. if (string != NULL) {
  1052. _PyErr_SetObject(tstate, exception, string);
  1053. Py_DECREF(string);
  1054. }
  1055. return NULL;
  1056. }
  1057. PyObject *
  1058. PyErr_FormatV(PyObject *exception, const char *format, va_list vargs)
  1059. {
  1060. PyThreadState *tstate = _PyThreadState_GET();
  1061. return _PyErr_FormatV(tstate, exception, format, vargs);
  1062. }
  1063. PyObject *
  1064. _PyErr_Format(PyThreadState *tstate, PyObject *exception,
  1065. const char *format, ...)
  1066. {
  1067. va_list vargs;
  1068. va_start(vargs, format);
  1069. _PyErr_FormatV(tstate, exception, format, vargs);
  1070. va_end(vargs);
  1071. return NULL;
  1072. }
  1073. PyObject *
  1074. PyErr_Format(PyObject *exception, const char *format, ...)
  1075. {
  1076. PyThreadState *tstate = _PyThreadState_GET();
  1077. va_list vargs;
  1078. va_start(vargs, format);
  1079. _PyErr_FormatV(tstate, exception, format, vargs);
  1080. va_end(vargs);
  1081. return NULL;
  1082. }
  1083. /* Adds a note to the current exception (if any) */
  1084. void
  1085. _PyErr_FormatNote(const char *format, ...)
  1086. {
  1087. PyObject *exc = PyErr_GetRaisedException();
  1088. if (exc == NULL) {
  1089. return;
  1090. }
  1091. va_list vargs;
  1092. va_start(vargs, format);
  1093. PyObject *note = PyUnicode_FromFormatV(format, vargs);
  1094. va_end(vargs);
  1095. if (note == NULL) {
  1096. goto error;
  1097. }
  1098. int res = _PyException_AddNote(exc, note);
  1099. Py_DECREF(note);
  1100. if (res < 0) {
  1101. goto error;
  1102. }
  1103. PyErr_SetRaisedException(exc);
  1104. return;
  1105. error:
  1106. _PyErr_ChainExceptions1(exc);
  1107. }
  1108. PyObject *
  1109. PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
  1110. {
  1111. PyThreadState *tstate = _PyThreadState_GET();
  1112. PyObject *modulename = NULL;
  1113. PyObject *mydict = NULL;
  1114. PyObject *bases = NULL;
  1115. PyObject *result = NULL;
  1116. #if defined(__has_feature)
  1117. # if __has_feature(memory_sanitizer)
  1118. __msan_unpoison_string(name);
  1119. # endif
  1120. #endif
  1121. const char *dot = strrchr(name, '.');
  1122. if (dot == NULL) {
  1123. _PyErr_SetString(tstate, PyExc_SystemError,
  1124. "PyErr_NewException: name must be module.class");
  1125. return NULL;
  1126. }
  1127. if (base == NULL) {
  1128. base = PyExc_Exception;
  1129. }
  1130. if (dict == NULL) {
  1131. dict = mydict = PyDict_New();
  1132. if (dict == NULL)
  1133. goto failure;
  1134. }
  1135. int r = PyDict_Contains(dict, &_Py_ID(__module__));
  1136. if (r < 0) {
  1137. goto failure;
  1138. }
  1139. if (r == 0) {
  1140. modulename = PyUnicode_FromStringAndSize(name,
  1141. (Py_ssize_t)(dot-name));
  1142. if (modulename == NULL)
  1143. goto failure;
  1144. if (PyDict_SetItem(dict, &_Py_ID(__module__), modulename) != 0)
  1145. goto failure;
  1146. }
  1147. if (PyTuple_Check(base)) {
  1148. bases = Py_NewRef(base);
  1149. } else {
  1150. bases = PyTuple_Pack(1, base);
  1151. if (bases == NULL)
  1152. goto failure;
  1153. }
  1154. /* Create a real class. */
  1155. result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
  1156. dot+1, bases, dict);
  1157. failure:
  1158. Py_XDECREF(bases);
  1159. Py_XDECREF(mydict);
  1160. Py_XDECREF(modulename);
  1161. return result;
  1162. }
  1163. /* Create an exception with docstring */
  1164. PyObject *
  1165. PyErr_NewExceptionWithDoc(const char *name, const char *doc,
  1166. PyObject *base, PyObject *dict)
  1167. {
  1168. int result;
  1169. PyObject *ret = NULL;
  1170. PyObject *mydict = NULL; /* points to the dict only if we create it */
  1171. PyObject *docobj;
  1172. if (dict == NULL) {
  1173. dict = mydict = PyDict_New();
  1174. if (dict == NULL) {
  1175. return NULL;
  1176. }
  1177. }
  1178. if (doc != NULL) {
  1179. docobj = PyUnicode_FromString(doc);
  1180. if (docobj == NULL)
  1181. goto failure;
  1182. result = PyDict_SetItemString(dict, "__doc__", docobj);
  1183. Py_DECREF(docobj);
  1184. if (result < 0)
  1185. goto failure;
  1186. }
  1187. ret = PyErr_NewException(name, base, dict);
  1188. failure:
  1189. Py_XDECREF(mydict);
  1190. return ret;
  1191. }
  1192. PyDoc_STRVAR(UnraisableHookArgs__doc__,
  1193. "UnraisableHookArgs\n\
  1194. \n\
  1195. Type used to pass arguments to sys.unraisablehook.");
  1196. static PyTypeObject UnraisableHookArgsType;
  1197. static PyStructSequence_Field UnraisableHookArgs_fields[] = {
  1198. {"exc_type", "Exception type"},
  1199. {"exc_value", "Exception value"},
  1200. {"exc_traceback", "Exception traceback"},
  1201. {"err_msg", "Error message"},
  1202. {"object", "Object causing the exception"},
  1203. {0}
  1204. };
  1205. static PyStructSequence_Desc UnraisableHookArgs_desc = {
  1206. .name = "UnraisableHookArgs",
  1207. .doc = UnraisableHookArgs__doc__,
  1208. .fields = UnraisableHookArgs_fields,
  1209. .n_in_sequence = 5
  1210. };
  1211. PyStatus
  1212. _PyErr_InitTypes(PyInterpreterState *interp)
  1213. {
  1214. if (_PyStructSequence_InitBuiltin(interp, &UnraisableHookArgsType,
  1215. &UnraisableHookArgs_desc) < 0)
  1216. {
  1217. return _PyStatus_ERR("failed to initialize UnraisableHookArgs type");
  1218. }
  1219. return _PyStatus_OK();
  1220. }
  1221. void
  1222. _PyErr_FiniTypes(PyInterpreterState *interp)
  1223. {
  1224. _PyStructSequence_FiniBuiltin(interp, &UnraisableHookArgsType);
  1225. }
  1226. static PyObject *
  1227. make_unraisable_hook_args(PyThreadState *tstate, PyObject *exc_type,
  1228. PyObject *exc_value, PyObject *exc_tb,
  1229. PyObject *err_msg, PyObject *obj)
  1230. {
  1231. PyObject *args = PyStructSequence_New(&UnraisableHookArgsType);
  1232. if (args == NULL) {
  1233. return NULL;
  1234. }
  1235. Py_ssize_t pos = 0;
  1236. #define ADD_ITEM(exc_type) \
  1237. do { \
  1238. if (exc_type == NULL) { \
  1239. exc_type = Py_None; \
  1240. } \
  1241. PyStructSequence_SET_ITEM(args, pos++, Py_NewRef(exc_type)); \
  1242. } while (0)
  1243. ADD_ITEM(exc_type);
  1244. ADD_ITEM(exc_value);
  1245. ADD_ITEM(exc_tb);
  1246. ADD_ITEM(err_msg);
  1247. ADD_ITEM(obj);
  1248. #undef ADD_ITEM
  1249. if (_PyErr_Occurred(tstate)) {
  1250. Py_DECREF(args);
  1251. return NULL;
  1252. }
  1253. return args;
  1254. }
  1255. /* Default implementation of sys.unraisablehook.
  1256. It can be called to log the exception of a custom sys.unraisablehook.
  1257. Do nothing if sys.stderr attribute doesn't exist or is set to None. */
  1258. static int
  1259. write_unraisable_exc_file(PyThreadState *tstate, PyObject *exc_type,
  1260. PyObject *exc_value, PyObject *exc_tb,
  1261. PyObject *err_msg, PyObject *obj, PyObject *file)
  1262. {
  1263. if (obj != NULL && obj != Py_None) {
  1264. if (err_msg != NULL && err_msg != Py_None) {
  1265. if (PyFile_WriteObject(err_msg, file, Py_PRINT_RAW) < 0) {
  1266. return -1;
  1267. }
  1268. if (PyFile_WriteString(": ", file) < 0) {
  1269. return -1;
  1270. }
  1271. }
  1272. else {
  1273. if (PyFile_WriteString("Exception ignored in: ", file) < 0) {
  1274. return -1;
  1275. }
  1276. }
  1277. if (PyFile_WriteObject(obj, file, 0) < 0) {
  1278. _PyErr_Clear(tstate);
  1279. if (PyFile_WriteString("<object repr() failed>", file) < 0) {
  1280. return -1;
  1281. }
  1282. }
  1283. if (PyFile_WriteString("\n", file) < 0) {
  1284. return -1;
  1285. }
  1286. }
  1287. else if (err_msg != NULL && err_msg != Py_None) {
  1288. if (PyFile_WriteObject(err_msg, file, Py_PRINT_RAW) < 0) {
  1289. return -1;
  1290. }
  1291. if (PyFile_WriteString(":\n", file) < 0) {
  1292. return -1;
  1293. }
  1294. }
  1295. if (exc_tb != NULL && exc_tb != Py_None) {
  1296. if (PyTraceBack_Print(exc_tb, file) < 0) {
  1297. /* continue even if writing the traceback failed */
  1298. _PyErr_Clear(tstate);
  1299. }
  1300. }
  1301. if (exc_type == NULL || exc_type == Py_None) {
  1302. return -1;
  1303. }
  1304. assert(PyExceptionClass_Check(exc_type));
  1305. PyObject *modulename = PyObject_GetAttr(exc_type, &_Py_ID(__module__));
  1306. if (modulename == NULL || !PyUnicode_Check(modulename)) {
  1307. Py_XDECREF(modulename);
  1308. _PyErr_Clear(tstate);
  1309. if (PyFile_WriteString("<unknown>", file) < 0) {
  1310. return -1;
  1311. }
  1312. }
  1313. else {
  1314. if (!_PyUnicode_Equal(modulename, &_Py_ID(builtins)) &&
  1315. !_PyUnicode_Equal(modulename, &_Py_ID(__main__))) {
  1316. if (PyFile_WriteObject(modulename, file, Py_PRINT_RAW) < 0) {
  1317. Py_DECREF(modulename);
  1318. return -1;
  1319. }
  1320. Py_DECREF(modulename);
  1321. if (PyFile_WriteString(".", file) < 0) {
  1322. return -1;
  1323. }
  1324. }
  1325. else {
  1326. Py_DECREF(modulename);
  1327. }
  1328. }
  1329. PyObject *qualname = PyType_GetQualName((PyTypeObject *)exc_type);
  1330. if (qualname == NULL || !PyUnicode_Check(qualname)) {
  1331. Py_XDECREF(qualname);
  1332. _PyErr_Clear(tstate);
  1333. if (PyFile_WriteString("<unknown>", file) < 0) {
  1334. return -1;
  1335. }
  1336. }
  1337. else {
  1338. if (PyFile_WriteObject(qualname, file, Py_PRINT_RAW) < 0) {
  1339. Py_DECREF(qualname);
  1340. return -1;
  1341. }
  1342. Py_DECREF(qualname);
  1343. }
  1344. if (exc_value && exc_value != Py_None) {
  1345. if (PyFile_WriteString(": ", file) < 0) {
  1346. return -1;
  1347. }
  1348. if (PyFile_WriteObject(exc_value, file, Py_PRINT_RAW) < 0) {
  1349. _PyErr_Clear(tstate);
  1350. if (PyFile_WriteString("<exception str() failed>", file) < 0) {
  1351. return -1;
  1352. }
  1353. }
  1354. }
  1355. if (PyFile_WriteString("\n", file) < 0) {
  1356. return -1;
  1357. }
  1358. /* Explicitly call file.flush() */
  1359. PyObject *res = _PyObject_CallMethodNoArgs(file, &_Py_ID(flush));
  1360. if (!res) {
  1361. return -1;
  1362. }
  1363. Py_DECREF(res);
  1364. return 0;
  1365. }
  1366. static int
  1367. write_unraisable_exc(PyThreadState *tstate, PyObject *exc_type,
  1368. PyObject *exc_value, PyObject *exc_tb, PyObject *err_msg,
  1369. PyObject *obj)
  1370. {
  1371. PyObject *file = _PySys_GetAttr(tstate, &_Py_ID(stderr));
  1372. if (file == NULL || file == Py_None) {
  1373. return 0;
  1374. }
  1375. /* Hold a strong reference to ensure that sys.stderr doesn't go away
  1376. while we use it */
  1377. Py_INCREF(file);
  1378. int res = write_unraisable_exc_file(tstate, exc_type, exc_value, exc_tb,
  1379. err_msg, obj, file);
  1380. Py_DECREF(file);
  1381. return res;
  1382. }
  1383. PyObject*
  1384. _PyErr_WriteUnraisableDefaultHook(PyObject *args)
  1385. {
  1386. PyThreadState *tstate = _PyThreadState_GET();
  1387. if (!Py_IS_TYPE(args, &UnraisableHookArgsType)) {
  1388. _PyErr_SetString(tstate, PyExc_TypeError,
  1389. "sys.unraisablehook argument type "
  1390. "must be UnraisableHookArgs");
  1391. return NULL;
  1392. }
  1393. /* Borrowed references */
  1394. PyObject *exc_type = PyStructSequence_GET_ITEM(args, 0);
  1395. PyObject *exc_value = PyStructSequence_GET_ITEM(args, 1);
  1396. PyObject *exc_tb = PyStructSequence_GET_ITEM(args, 2);
  1397. PyObject *err_msg = PyStructSequence_GET_ITEM(args, 3);
  1398. PyObject *obj = PyStructSequence_GET_ITEM(args, 4);
  1399. if (write_unraisable_exc(tstate, exc_type, exc_value, exc_tb, err_msg, obj) < 0) {
  1400. return NULL;
  1401. }
  1402. Py_RETURN_NONE;
  1403. }
  1404. /* Call sys.unraisablehook().
  1405. This function can be used when an exception has occurred but there is no way
  1406. for Python to handle it. For example, when a destructor raises an exception
  1407. or during garbage collection (gc.collect()).
  1408. If err_msg_str is non-NULL, the error message is formatted as:
  1409. "Exception ignored %s" % err_msg_str. Otherwise, use "Exception ignored in"
  1410. error message.
  1411. An exception must be set when calling this function. */
  1412. void
  1413. _PyErr_WriteUnraisableMsg(const char *err_msg_str, PyObject *obj)
  1414. {
  1415. PyThreadState *tstate = _PyThreadState_GET();
  1416. _Py_EnsureTstateNotNULL(tstate);
  1417. PyObject *err_msg = NULL;
  1418. PyObject *exc_type, *exc_value, *exc_tb;
  1419. _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
  1420. assert(exc_type != NULL);
  1421. if (exc_type == NULL) {
  1422. /* sys.unraisablehook requires that at least exc_type is set */
  1423. goto default_hook;
  1424. }
  1425. if (exc_tb == NULL) {
  1426. PyFrameObject *frame = PyThreadState_GetFrame(tstate);
  1427. if (frame != NULL) {
  1428. exc_tb = _PyTraceBack_FromFrame(NULL, frame);
  1429. if (exc_tb == NULL) {
  1430. _PyErr_Clear(tstate);
  1431. }
  1432. Py_DECREF(frame);
  1433. }
  1434. }
  1435. _PyErr_NormalizeException(tstate, &exc_type, &exc_value, &exc_tb);
  1436. if (exc_tb != NULL && exc_tb != Py_None && PyTraceBack_Check(exc_tb)) {
  1437. if (PyException_SetTraceback(exc_value, exc_tb) < 0) {
  1438. _PyErr_Clear(tstate);
  1439. }
  1440. }
  1441. if (err_msg_str != NULL) {
  1442. err_msg = PyUnicode_FromFormat("Exception ignored %s", err_msg_str);
  1443. if (err_msg == NULL) {
  1444. PyErr_Clear();
  1445. }
  1446. }
  1447. PyObject *hook_args = make_unraisable_hook_args(
  1448. tstate, exc_type, exc_value, exc_tb, err_msg, obj);
  1449. if (hook_args == NULL) {
  1450. err_msg_str = ("Exception ignored on building "
  1451. "sys.unraisablehook arguments");
  1452. goto error;
  1453. }
  1454. PyObject *hook = _PySys_GetAttr(tstate, &_Py_ID(unraisablehook));
  1455. if (hook == NULL) {
  1456. Py_DECREF(hook_args);
  1457. goto default_hook;
  1458. }
  1459. if (_PySys_Audit(tstate, "sys.unraisablehook", "OO", hook, hook_args) < 0) {
  1460. Py_DECREF(hook_args);
  1461. err_msg_str = "Exception ignored in audit hook";
  1462. obj = NULL;
  1463. goto error;
  1464. }
  1465. if (hook == Py_None) {
  1466. Py_DECREF(hook_args);
  1467. goto default_hook;
  1468. }
  1469. PyObject *res = PyObject_CallOneArg(hook, hook_args);
  1470. Py_DECREF(hook_args);
  1471. if (res != NULL) {
  1472. Py_DECREF(res);
  1473. goto done;
  1474. }
  1475. /* sys.unraisablehook failed: log its error using default hook */
  1476. obj = hook;
  1477. err_msg_str = NULL;
  1478. error:
  1479. /* err_msg_str and obj have been updated and we have a new exception */
  1480. Py_XSETREF(err_msg, PyUnicode_FromString(err_msg_str ?
  1481. err_msg_str : "Exception ignored in sys.unraisablehook"));
  1482. Py_XDECREF(exc_type);
  1483. Py_XDECREF(exc_value);
  1484. Py_XDECREF(exc_tb);
  1485. _PyErr_Fetch(tstate, &exc_type, &exc_value, &exc_tb);
  1486. default_hook:
  1487. /* Call the default unraisable hook (ignore failure) */
  1488. (void)write_unraisable_exc(tstate, exc_type, exc_value, exc_tb,
  1489. err_msg, obj);
  1490. done:
  1491. Py_XDECREF(exc_type);
  1492. Py_XDECREF(exc_value);
  1493. Py_XDECREF(exc_tb);
  1494. Py_XDECREF(err_msg);
  1495. _PyErr_Clear(tstate); /* Just in case */
  1496. }
  1497. void
  1498. PyErr_WriteUnraisable(PyObject *obj)
  1499. {
  1500. _PyErr_WriteUnraisableMsg(NULL, obj);
  1501. }
  1502. void
  1503. PyErr_SyntaxLocation(const char *filename, int lineno)
  1504. {
  1505. PyErr_SyntaxLocationEx(filename, lineno, -1);
  1506. }
  1507. /* Set file and line information for the current exception.
  1508. If the exception is not a SyntaxError, also sets additional attributes
  1509. to make printing of exceptions believe it is a syntax error. */
  1510. static void
  1511. PyErr_SyntaxLocationObjectEx(PyObject *filename, int lineno, int col_offset,
  1512. int end_lineno, int end_col_offset)
  1513. {
  1514. PyThreadState *tstate = _PyThreadState_GET();
  1515. /* add attributes for the line number and filename for the error */
  1516. PyObject *exc = _PyErr_GetRaisedException(tstate);
  1517. /* XXX check that it is, indeed, a syntax error. It might not
  1518. * be, though. */
  1519. PyObject *tmp = PyLong_FromLong(lineno);
  1520. if (tmp == NULL) {
  1521. _PyErr_Clear(tstate);
  1522. }
  1523. else {
  1524. if (PyObject_SetAttr(exc, &_Py_ID(lineno), tmp)) {
  1525. _PyErr_Clear(tstate);
  1526. }
  1527. Py_DECREF(tmp);
  1528. }
  1529. tmp = NULL;
  1530. if (col_offset >= 0) {
  1531. tmp = PyLong_FromLong(col_offset);
  1532. if (tmp == NULL) {
  1533. _PyErr_Clear(tstate);
  1534. }
  1535. }
  1536. if (PyObject_SetAttr(exc, &_Py_ID(offset), tmp ? tmp : Py_None)) {
  1537. _PyErr_Clear(tstate);
  1538. }
  1539. Py_XDECREF(tmp);
  1540. tmp = NULL;
  1541. if (end_lineno >= 0) {
  1542. tmp = PyLong_FromLong(end_lineno);
  1543. if (tmp == NULL) {
  1544. _PyErr_Clear(tstate);
  1545. }
  1546. }
  1547. if (PyObject_SetAttr(exc, &_Py_ID(end_lineno), tmp ? tmp : Py_None)) {
  1548. _PyErr_Clear(tstate);
  1549. }
  1550. Py_XDECREF(tmp);
  1551. tmp = NULL;
  1552. if (end_col_offset >= 0) {
  1553. tmp = PyLong_FromLong(end_col_offset);
  1554. if (tmp == NULL) {
  1555. _PyErr_Clear(tstate);
  1556. }
  1557. }
  1558. if (PyObject_SetAttr(exc, &_Py_ID(end_offset), tmp ? tmp : Py_None)) {
  1559. _PyErr_Clear(tstate);
  1560. }
  1561. Py_XDECREF(tmp);
  1562. tmp = NULL;
  1563. if (filename != NULL) {
  1564. if (PyObject_SetAttr(exc, &_Py_ID(filename), filename)) {
  1565. _PyErr_Clear(tstate);
  1566. }
  1567. tmp = PyErr_ProgramTextObject(filename, lineno);
  1568. if (tmp) {
  1569. if (PyObject_SetAttr(exc, &_Py_ID(text), tmp)) {
  1570. _PyErr_Clear(tstate);
  1571. }
  1572. Py_DECREF(tmp);
  1573. }
  1574. else {
  1575. _PyErr_Clear(tstate);
  1576. }
  1577. }
  1578. if ((PyObject *)Py_TYPE(exc) != PyExc_SyntaxError) {
  1579. if (_PyObject_LookupAttr(exc, &_Py_ID(msg), &tmp) < 0) {
  1580. _PyErr_Clear(tstate);
  1581. }
  1582. else if (tmp) {
  1583. Py_DECREF(tmp);
  1584. }
  1585. else {
  1586. tmp = PyObject_Str(exc);
  1587. if (tmp) {
  1588. if (PyObject_SetAttr(exc, &_Py_ID(msg), tmp)) {
  1589. _PyErr_Clear(tstate);
  1590. }
  1591. Py_DECREF(tmp);
  1592. }
  1593. else {
  1594. _PyErr_Clear(tstate);
  1595. }
  1596. }
  1597. if (_PyObject_LookupAttr(exc, &_Py_ID(print_file_and_line), &tmp) < 0) {
  1598. _PyErr_Clear(tstate);
  1599. }
  1600. else if (tmp) {
  1601. Py_DECREF(tmp);
  1602. }
  1603. else {
  1604. if (PyObject_SetAttr(exc, &_Py_ID(print_file_and_line), Py_None)) {
  1605. _PyErr_Clear(tstate);
  1606. }
  1607. }
  1608. }
  1609. _PyErr_SetRaisedException(tstate, exc);
  1610. }
  1611. void
  1612. PyErr_SyntaxLocationObject(PyObject *filename, int lineno, int col_offset) {
  1613. PyErr_SyntaxLocationObjectEx(filename, lineno, col_offset, lineno, -1);
  1614. }
  1615. void
  1616. PyErr_RangedSyntaxLocationObject(PyObject *filename, int lineno, int col_offset,
  1617. int end_lineno, int end_col_offset) {
  1618. PyErr_SyntaxLocationObjectEx(filename, lineno, col_offset, end_lineno, end_col_offset);
  1619. }
  1620. void
  1621. PyErr_SyntaxLocationEx(const char *filename, int lineno, int col_offset)
  1622. {
  1623. PyThreadState *tstate = _PyThreadState_GET();
  1624. PyObject *fileobj;
  1625. if (filename != NULL) {
  1626. fileobj = PyUnicode_DecodeFSDefault(filename);
  1627. if (fileobj == NULL) {
  1628. _PyErr_Clear(tstate);
  1629. }
  1630. }
  1631. else {
  1632. fileobj = NULL;
  1633. }
  1634. PyErr_SyntaxLocationObject(fileobj, lineno, col_offset);
  1635. Py_XDECREF(fileobj);
  1636. }
  1637. /* Attempt to load the line of text that the exception refers to. If it
  1638. fails, it will return NULL but will not set an exception.
  1639. XXX The functionality of this function is quite similar to the
  1640. functionality in tb_displayline() in traceback.c. */
  1641. static PyObject *
  1642. err_programtext(PyThreadState *tstate, FILE *fp, int lineno, const char* encoding)
  1643. {
  1644. int i;
  1645. char linebuf[1000];
  1646. if (fp == NULL) {
  1647. return NULL;
  1648. }
  1649. for (i = 0; i < lineno; i++) {
  1650. char *pLastChar = &linebuf[sizeof(linebuf) - 2];
  1651. do {
  1652. *pLastChar = '\0';
  1653. if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf,
  1654. fp, NULL) == NULL) {
  1655. goto after_loop;
  1656. }
  1657. /* fgets read *something*; if it didn't get as
  1658. far as pLastChar, it must have found a newline
  1659. or hit the end of the file; if pLastChar is \n,
  1660. it obviously found a newline; else we haven't
  1661. yet seen a newline, so must continue */
  1662. } while (*pLastChar != '\0' && *pLastChar != '\n');
  1663. }
  1664. after_loop:
  1665. fclose(fp);
  1666. if (i == lineno) {
  1667. PyObject *res;
  1668. if (encoding != NULL) {
  1669. res = PyUnicode_Decode(linebuf, strlen(linebuf), encoding, "replace");
  1670. } else {
  1671. res = PyUnicode_FromString(linebuf);
  1672. }
  1673. if (res == NULL)
  1674. _PyErr_Clear(tstate);
  1675. return res;
  1676. }
  1677. return NULL;
  1678. }
  1679. PyObject *
  1680. PyErr_ProgramText(const char *filename, int lineno)
  1681. {
  1682. if (filename == NULL) {
  1683. return NULL;
  1684. }
  1685. PyObject *filename_obj = PyUnicode_DecodeFSDefault(filename);
  1686. if (filename_obj == NULL) {
  1687. PyErr_Clear();
  1688. return NULL;
  1689. }
  1690. PyObject *res = PyErr_ProgramTextObject(filename_obj, lineno);
  1691. Py_DECREF(filename_obj);
  1692. return res;
  1693. }
  1694. PyObject *
  1695. _PyErr_ProgramDecodedTextObject(PyObject *filename, int lineno, const char* encoding)
  1696. {
  1697. if (filename == NULL || lineno <= 0) {
  1698. return NULL;
  1699. }
  1700. PyThreadState *tstate = _PyThreadState_GET();
  1701. FILE *fp = _Py_fopen_obj(filename, "r" PY_STDIOTEXTMODE);
  1702. if (fp == NULL) {
  1703. _PyErr_Clear(tstate);
  1704. return NULL;
  1705. }
  1706. return err_programtext(tstate, fp, lineno, encoding);
  1707. }
  1708. PyObject *
  1709. PyErr_ProgramTextObject(PyObject *filename, int lineno)
  1710. {
  1711. return _PyErr_ProgramDecodedTextObject(filename, lineno, NULL);
  1712. }
  1713. #ifdef __cplusplus
  1714. }
  1715. #endif