LCOV - code coverage report
Current view: top level - Python - errors.c (source / functions) Hit Total Coverage
Test: CPython lcov report Lines: 139 321 43.3 %
Date: 2017-04-19 Functions: 14 24 58.3 %

          Line data    Source code
       1             : 
       2             : /* Error handling */
       3             : 
       4             : #include "Python.h"
       5             : 
       6             : #ifndef __STDC__
       7             : #ifndef MS_WINDOWS
       8             : extern char *strerror(int);
       9             : #endif
      10             : #endif
      11             : 
      12             : #ifdef MS_WINDOWS
      13             : #include "windows.h"
      14             : #include "winbase.h"
      15             : #endif
      16             : 
      17             : #include <ctype.h>
      18             : 
      19             : #ifdef __cplusplus
      20             : extern "C" {
      21             : #endif
      22             : 
      23             : 
      24             : void
      25       35325 : PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
      26             : {
      27       35325 :     PyThreadState *tstate = PyThreadState_GET();
      28             :     PyObject *oldtype, *oldvalue, *oldtraceback;
      29             : 
      30       35325 :     if (traceback != NULL && !PyTraceBack_Check(traceback)) {
      31             :         /* XXX Should never happen -- fatal error instead? */
      32             :         /* Well, it could be None. */
      33           0 :         Py_DECREF(traceback);
      34           0 :         traceback = NULL;
      35             :     }
      36             : 
      37             :     /* Save these in locals to safeguard against recursive
      38             :        invocation through Py_XDECREF */
      39       35325 :     oldtype = tstate->curexc_type;
      40       35325 :     oldvalue = tstate->curexc_value;
      41       35325 :     oldtraceback = tstate->curexc_traceback;
      42             : 
      43       35325 :     tstate->curexc_type = type;
      44       35325 :     tstate->curexc_value = value;
      45       35325 :     tstate->curexc_traceback = traceback;
      46             : 
      47       35325 :     Py_XDECREF(oldtype);
      48       35325 :     Py_XDECREF(oldvalue);
      49       35325 :     Py_XDECREF(oldtraceback);
      50       35325 : }
      51             : 
      52             : void
      53       11183 : PyErr_SetObject(PyObject *exception, PyObject *value)
      54             : {
      55       11183 :     Py_XINCREF(exception);
      56       11183 :     Py_XINCREF(value);
      57       11183 :     PyErr_Restore(exception, value, (PyObject *)NULL);
      58       11183 : }
      59             : 
      60             : void
      61           9 : PyErr_SetNone(PyObject *exception)
      62             : {
      63           9 :     PyErr_SetObject(exception, (PyObject *)NULL);
      64           9 : }
      65             : 
      66             : void
      67        2651 : PyErr_SetString(PyObject *exception, const char *string)
      68             : {
      69        2651 :     PyObject *value = PyString_FromString(string);
      70        2651 :     PyErr_SetObject(exception, value);
      71        2651 :     Py_XDECREF(value);
      72        2651 : }
      73             : 
      74             : 
      75             : PyObject *
      76       63848 : PyErr_Occurred(void)
      77             : {
      78       63848 :     PyThreadState *tstate = PyThreadState_GET();
      79             : 
      80       63848 :     return tstate->curexc_type;
      81             : }
      82             : 
      83             : 
      84             : int
      85        9896 : PyErr_GivenExceptionMatches(PyObject *err, PyObject *exc)
      86             : {
      87        9896 :     if (err == NULL || exc == NULL) {
      88             :         /* maybe caused by "import exceptions" that failed early on */
      89           0 :         return 0;
      90             :     }
      91        9896 :     if (PyTuple_Check(exc)) {
      92             :         Py_ssize_t i, n;
      93           3 :         n = PyTuple_Size(exc);
      94           3 :         for (i = 0; i < n; i++) {
      95             :             /* Test recursively */
      96           3 :              if (PyErr_GivenExceptionMatches(
      97             :                  err, PyTuple_GET_ITEM(exc, i)))
      98             :              {
      99           3 :                  return 1;
     100             :              }
     101             :         }
     102           0 :         return 0;
     103             :     }
     104             :     /* err might be an instance, so check its class. */
     105        9893 :     if (PyExceptionInstance_Check(err))
     106           0 :         err = PyExceptionInstance_Class(err);
     107             : 
     108        9893 :     if (PyExceptionClass_Check(err) && PyExceptionClass_Check(exc)) {
     109        9893 :         int res = 0, reclimit;
     110             :         PyObject *exception, *value, *tb;
     111        9893 :         PyErr_Fetch(&exception, &value, &tb);
     112             :         /* Temporarily bump the recursion limit, so that in the most
     113             :            common case PyObject_IsSubclass will not raise a recursion
     114             :            error we have to ignore anyway.  Don't do it when the limit
     115             :            is already insanely high, to avoid overflow */
     116        9893 :         reclimit = Py_GetRecursionLimit();
     117        9893 :         if (reclimit < (1 << 30))
     118        9893 :             Py_SetRecursionLimit(reclimit + 5);
     119        9893 :         res = PyObject_IsSubclass(err, exc);
     120        9893 :         Py_SetRecursionLimit(reclimit);
     121             :         /* This function must not fail, so print the error here */
     122        9893 :         if (res == -1) {
     123           0 :             PyErr_WriteUnraisable(err);
     124           0 :             res = 0;
     125             :         }
     126        9893 :         PyErr_Restore(exception, value, tb);
     127        9893 :         return res;
     128             :     }
     129             : 
     130           0 :     return err == exc;
     131             : }
     132             : 
     133             : 
     134             : int
     135        8915 : PyErr_ExceptionMatches(PyObject *exc)
     136             : {
     137        8915 :     return PyErr_GivenExceptionMatches(PyErr_Occurred(), exc);
     138             : }
     139             : 
     140             : 
     141             : /* Used in many places to normalize a raised exception, including in
     142             :    eval_code2(), do_raise(), and PyErr_Print()
     143             : */
     144             : void
     145         975 : PyErr_NormalizeException(PyObject **exc, PyObject **val, PyObject **tb)
     146             : {
     147         975 :     PyObject *type = *exc;
     148         975 :     PyObject *value = *val;
     149         975 :     PyObject *inclass = NULL;
     150         975 :     PyObject *initial_tb = NULL;
     151         975 :     PyThreadState *tstate = NULL;
     152             : 
     153         975 :     if (type == NULL) {
     154             :         /* There was no exception, so nothing to do. */
     155           0 :         return;
     156             :     }
     157             : 
     158             :     /* If PyErr_SetNone() was used, the value will have been actually
     159             :        set to NULL.
     160             :     */
     161         975 :     if (!value) {
     162           0 :         value = Py_None;
     163           0 :         Py_INCREF(value);
     164             :     }
     165             : 
     166         975 :     if (PyExceptionInstance_Check(value))
     167          24 :         inclass = PyExceptionInstance_Class(value);
     168             : 
     169             :     /* Normalize the exception so that if the type is a class, the
     170             :        value will be an instance.
     171             :     */
     172         975 :     if (PyExceptionClass_Check(type)) {
     173             :         /* if the value was not an instance, or is not an instance
     174             :            whose class is (or is derived from) type, then use the
     175             :            value as an argument to instantiation of the type
     176             :            class.
     177             :         */
     178        1926 :         if (!inclass || !PyObject_IsSubclass(inclass, type)) {
     179             :             PyObject *args, *res;
     180             : 
     181         951 :             if (value == Py_None)
     182           9 :                 args = PyTuple_New(0);
     183         942 :             else if (PyTuple_Check(value)) {
     184         921 :                 Py_INCREF(value);
     185         921 :                 args = value;
     186             :             }
     187             :             else
     188          21 :                 args = PyTuple_Pack(1, value);
     189             : 
     190         951 :             if (args == NULL)
     191           0 :                 goto finally;
     192         951 :             res = PyEval_CallObject(type, args);
     193         951 :             Py_DECREF(args);
     194         951 :             if (res == NULL)
     195           0 :                 goto finally;
     196         951 :             Py_DECREF(value);
     197         951 :             value = res;
     198             :         }
     199             :         /* if the class of the instance doesn't exactly match the
     200             :            class of the type, believe the instance
     201             :         */
     202          24 :         else if (inclass != type) {
     203           0 :             Py_DECREF(type);
     204           0 :             type = inclass;
     205           0 :             Py_INCREF(type);
     206             :         }
     207             :     }
     208         975 :     *exc = type;
     209         975 :     *val = value;
     210         975 :     return;
     211             : finally:
     212           0 :     Py_DECREF(type);
     213           0 :     Py_DECREF(value);
     214             :     /* If the new exception doesn't set a traceback and the old
     215             :        exception had a traceback, use the old traceback for the
     216             :        new exception.  It's better than nothing.
     217             :     */
     218           0 :     initial_tb = *tb;
     219           0 :     PyErr_Fetch(exc, val, tb);
     220           0 :     if (initial_tb != NULL) {
     221           0 :         if (*tb == NULL)
     222           0 :             *tb = initial_tb;
     223             :         else
     224           0 :             Py_DECREF(initial_tb);
     225             :     }
     226             :     /* normalize recursively */
     227           0 :     tstate = PyThreadState_GET();
     228           0 :     if (++tstate->recursion_depth > Py_GetRecursionLimit()) {
     229           0 :         --tstate->recursion_depth;
     230             :         /* throw away the old exception and use the recursion error instead */
     231           0 :         Py_INCREF(PyExc_RuntimeError);
     232           0 :         Py_SETREF(*exc, PyExc_RuntimeError);
     233           0 :         Py_INCREF(PyExc_RecursionErrorInst);
     234           0 :         Py_SETREF(*val, PyExc_RecursionErrorInst);
     235             :         /* just keeping the old traceback */
     236           0 :         return;
     237             :     }
     238           0 :     PyErr_NormalizeException(exc, val, tb);
     239           0 :     --tstate->recursion_depth;
     240             : }
     241             : 
     242             : 
     243             : void
     244       14849 : PyErr_Fetch(PyObject **p_type, PyObject **p_value, PyObject **p_traceback)
     245             : {
     246       14849 :     PyThreadState *tstate = PyThreadState_GET();
     247             : 
     248       14849 :     *p_type = tstate->curexc_type;
     249       14849 :     *p_value = tstate->curexc_value;
     250       14849 :     *p_traceback = tstate->curexc_traceback;
     251             : 
     252       14849 :     tstate->curexc_type = NULL;
     253       14849 :     tstate->curexc_value = NULL;
     254       14849 :     tstate->curexc_traceback = NULL;
     255       14849 : }
     256             : 
     257             : void
     258       10238 : PyErr_Clear(void)
     259             : {
     260       10238 :     PyErr_Restore(NULL, NULL, NULL);
     261       10238 : }
     262             : 
     263             : /* Restore previously fetched exception if an exception is not set,
     264             :    otherwise drop previously fetched exception.
     265             :    Like _PyErr_ChainExceptions() in Python 3, but doesn't set the context.
     266             :  */
     267             : void
     268           0 : _PyErr_ReplaceException(PyObject *exc, PyObject *val, PyObject *tb)
     269             : {
     270           0 :     if (exc == NULL)
     271           0 :         return;
     272             : 
     273           0 :     if (PyErr_Occurred()) {
     274           0 :         Py_DECREF(exc);
     275           0 :         Py_XDECREF(val);
     276           0 :         Py_XDECREF(tb);
     277             :     }
     278             :     else {
     279           0 :         PyErr_Restore(exc, val, tb);
     280             :     }
     281             : }
     282             : 
     283             : /* Convenience functions to set a type error exception and return 0 */
     284             : 
     285             : int
     286           0 : PyErr_BadArgument(void)
     287             : {
     288           0 :     PyErr_SetString(PyExc_TypeError,
     289             :                     "bad argument type for built-in operation");
     290           0 :     return 0;
     291             : }
     292             : 
     293             : PyObject *
     294           0 : PyErr_NoMemory(void)
     295             : {
     296           0 :     if (PyErr_ExceptionMatches(PyExc_MemoryError))
     297             :         /* already current */
     298           0 :         return NULL;
     299             : 
     300             :     /* raise the pre-allocated instance if it still exists */
     301           0 :     if (PyExc_MemoryErrorInst)
     302           0 :         PyErr_SetObject(PyExc_MemoryError, PyExc_MemoryErrorInst);
     303             :     else
     304             :         /* this will probably fail since there's no memory and hee,
     305             :            hee, we have to instantiate this class
     306             :         */
     307           0 :         PyErr_SetNone(PyExc_MemoryError);
     308             : 
     309           0 :     return NULL;
     310             : }
     311             : 
     312             : PyObject *
     313          75 : PyErr_SetFromErrnoWithFilenameObject(PyObject *exc, PyObject *filenameObject)
     314             : {
     315             :     PyObject *v;
     316             :     char *s;
     317          75 :     int i = errno;
     318             : #ifdef PLAN9
     319             :     char errbuf[ERRMAX];
     320             : #endif
     321             : #ifdef MS_WINDOWS
     322             :     char *s_buf = NULL;
     323             :     char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
     324             : #endif
     325             : #ifdef EINTR
     326          75 :     if (i == EINTR && PyErr_CheckSignals())
     327           0 :         return NULL;
     328             : #endif
     329             : #ifdef PLAN9
     330             :     rerrstr(errbuf, sizeof errbuf);
     331             :     s = errbuf;
     332             : #else
     333          75 :     if (i == 0)
     334           0 :         s = "Error"; /* Sometimes errno didn't get set */
     335             :     else
     336             : #ifndef MS_WINDOWS
     337          75 :         s = strerror(i);
     338             : #else
     339             :     {
     340             :         /* Note that the Win32 errors do not lineup with the
     341             :            errno error.  So if the error is in the MSVC error
     342             :            table, we use it, otherwise we assume it really _is_
     343             :            a Win32 error code
     344             :         */
     345             :         if (i > 0 && i < _sys_nerr) {
     346             :             s = _sys_errlist[i];
     347             :         }
     348             :         else {
     349             :             int len = FormatMessage(
     350             :                 FORMAT_MESSAGE_ALLOCATE_BUFFER |
     351             :                 FORMAT_MESSAGE_FROM_SYSTEM |
     352             :                 FORMAT_MESSAGE_IGNORE_INSERTS,
     353             :                 NULL,                   /* no message source */
     354             :                 i,
     355             :                 MAKELANGID(LANG_NEUTRAL,
     356             :                            SUBLANG_DEFAULT),
     357             :                            /* Default language */
     358             :                 (LPTSTR) &s_buf,
     359             :                 0,                      /* size not used */
     360             :                 NULL);                  /* no args */
     361             :             if (len==0) {
     362             :                 /* Only ever seen this in out-of-mem
     363             :                    situations */
     364             :                 sprintf(s_small_buf, "Windows Error 0x%X", i);
     365             :                 s = s_small_buf;
     366             :                 s_buf = NULL;
     367             :             } else {
     368             :                 s = s_buf;
     369             :                 /* remove trailing cr/lf and dots */
     370             :                 while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
     371             :                     s[--len] = '\0';
     372             :             }
     373             :         }
     374             :     }
     375             : #endif /* Unix/Windows */
     376             : #endif /* PLAN 9*/
     377          75 :     if (filenameObject != NULL)
     378          75 :         v = Py_BuildValue("(isO)", i, s, filenameObject);
     379             :     else
     380           0 :         v = Py_BuildValue("(is)", i, s);
     381          75 :     if (v != NULL) {
     382          75 :         PyErr_SetObject(exc, v);
     383          75 :         Py_DECREF(v);
     384             :     }
     385             : #ifdef MS_WINDOWS
     386             :     LocalFree(s_buf);
     387             : #endif
     388          75 :     return NULL;
     389             : }
     390             : 
     391             : 
     392             : PyObject *
     393          72 : PyErr_SetFromErrnoWithFilename(PyObject *exc, const char *filename)
     394             : {
     395          72 :     PyObject *name = filename ? PyString_FromString(filename) : NULL;
     396          72 :     PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
     397          72 :     Py_XDECREF(name);
     398          72 :     return result;
     399             : }
     400             : 
     401             : #ifdef MS_WINDOWS
     402             : PyObject *
     403             : PyErr_SetFromErrnoWithUnicodeFilename(PyObject *exc, const Py_UNICODE *filename)
     404             : {
     405             :     PyObject *name = filename ?
     406             :                      PyUnicode_FromUnicode(filename, wcslen(filename)) :
     407             :              NULL;
     408             :     PyObject *result = PyErr_SetFromErrnoWithFilenameObject(exc, name);
     409             :     Py_XDECREF(name);
     410             :     return result;
     411             : }
     412             : #endif /* MS_WINDOWS */
     413             : 
     414             : PyObject *
     415           0 : PyErr_SetFromErrno(PyObject *exc)
     416             : {
     417           0 :     return PyErr_SetFromErrnoWithFilenameObject(exc, NULL);
     418             : }
     419             : 
     420             : #ifdef MS_WINDOWS
     421             : /* Windows specific error code handling */
     422             : PyObject *PyErr_SetExcFromWindowsErrWithFilenameObject(
     423             :     PyObject *exc,
     424             :     int ierr,
     425             :     PyObject *filenameObject)
     426             : {
     427             :     int len;
     428             :     char *s;
     429             :     char *s_buf = NULL; /* Free via LocalFree */
     430             :     char s_small_buf[28]; /* Room for "Windows Error 0xFFFFFFFF" */
     431             :     PyObject *v;
     432             :     DWORD err = (DWORD)ierr;
     433             :     if (err==0) err = GetLastError();
     434             :     len = FormatMessage(
     435             :         /* Error API error */
     436             :         FORMAT_MESSAGE_ALLOCATE_BUFFER |
     437             :         FORMAT_MESSAGE_FROM_SYSTEM |
     438             :         FORMAT_MESSAGE_IGNORE_INSERTS,
     439             :         NULL,           /* no message source */
     440             :         err,
     441             :         MAKELANGID(LANG_NEUTRAL,
     442             :         SUBLANG_DEFAULT), /* Default language */
     443             :         (LPTSTR) &s_buf,
     444             :         0,              /* size not used */
     445             :         NULL);          /* no args */
     446             :     if (len==0) {
     447             :         /* Only seen this in out of mem situations */
     448             :         sprintf(s_small_buf, "Windows Error 0x%X", err);
     449             :         s = s_small_buf;
     450             :         s_buf = NULL;
     451             :     } else {
     452             :         s = s_buf;
     453             :         /* remove trailing cr/lf and dots */
     454             :         while (len > 0 && (s[len-1] <= ' ' || s[len-1] == '.'))
     455             :             s[--len] = '\0';
     456             :     }
     457             :     if (filenameObject != NULL)
     458             :         v = Py_BuildValue("(isO)", err, s, filenameObject);
     459             :     else
     460             :         v = Py_BuildValue("(is)", err, s);
     461             :     if (v != NULL) {
     462             :         PyErr_SetObject(exc, v);
     463             :         Py_DECREF(v);
     464             :     }
     465             :     LocalFree(s_buf);
     466             :     return NULL;
     467             : }
     468             : 
     469             : PyObject *PyErr_SetExcFromWindowsErrWithFilename(
     470             :     PyObject *exc,
     471             :     int ierr,
     472             :     const char *filename)
     473             : {
     474             :     PyObject *name = filename ? PyString_FromString(filename) : NULL;
     475             :     PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
     476             :                                                                  ierr,
     477             :                                                                  name);
     478             :     Py_XDECREF(name);
     479             :     return ret;
     480             : }
     481             : 
     482             : PyObject *PyErr_SetExcFromWindowsErrWithUnicodeFilename(
     483             :     PyObject *exc,
     484             :     int ierr,
     485             :     const Py_UNICODE *filename)
     486             : {
     487             :     PyObject *name = filename ?
     488             :                      PyUnicode_FromUnicode(filename, wcslen(filename)) :
     489             :              NULL;
     490             :     PyObject *ret = PyErr_SetExcFromWindowsErrWithFilenameObject(exc,
     491             :                                                                  ierr,
     492             :                                                                  name);
     493             :     Py_XDECREF(name);
     494             :     return ret;
     495             : }
     496             : 
     497             : PyObject *PyErr_SetExcFromWindowsErr(PyObject *exc, int ierr)
     498             : {
     499             :     return PyErr_SetExcFromWindowsErrWithFilename(exc, ierr, NULL);
     500             : }
     501             : 
     502             : PyObject *PyErr_SetFromWindowsErr(int ierr)
     503             : {
     504             :     return PyErr_SetExcFromWindowsErrWithFilename(PyExc_WindowsError,
     505             :                                                   ierr, NULL);
     506             : }
     507             : PyObject *PyErr_SetFromWindowsErrWithFilename(
     508             :     int ierr,
     509             :     const char *filename)
     510             : {
     511             :     PyObject *name = filename ? PyString_FromString(filename) : NULL;
     512             :     PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
     513             :                                                   PyExc_WindowsError,
     514             :                                                   ierr, name);
     515             :     Py_XDECREF(name);
     516             :     return result;
     517             : }
     518             : 
     519             : PyObject *PyErr_SetFromWindowsErrWithUnicodeFilename(
     520             :     int ierr,
     521             :     const Py_UNICODE *filename)
     522             : {
     523             :     PyObject *name = filename ?
     524             :                      PyUnicode_FromUnicode(filename, wcslen(filename)) :
     525             :              NULL;
     526             :     PyObject *result = PyErr_SetExcFromWindowsErrWithFilenameObject(
     527             :                                                   PyExc_WindowsError,
     528             :                                                   ierr, name);
     529             :     Py_XDECREF(name);
     530             :     return result;
     531             : }
     532             : #endif /* MS_WINDOWS */
     533             : 
     534             : void
     535           0 : _PyErr_BadInternalCall(const char *filename, int lineno)
     536             : {
     537           0 :     PyErr_Format(PyExc_SystemError,
     538             :                  "%s:%d: bad argument to internal function",
     539             :                  filename, lineno);
     540           0 : }
     541             : 
     542             : /* Remove the preprocessor macro for PyErr_BadInternalCall() so that we can
     543             :    export the entry point for existing object code: */
     544             : #undef PyErr_BadInternalCall
     545             : void
     546           0 : PyErr_BadInternalCall(void)
     547             : {
     548           0 :     PyErr_Format(PyExc_SystemError,
     549             :                  "bad argument to internal function");
     550           0 : }
     551             : #define PyErr_BadInternalCall() _PyErr_BadInternalCall(__FILE__, __LINE__)
     552             : 
     553             : 
     554             : 
     555             : PyObject *
     556        5646 : PyErr_Format(PyObject *exception, const char *format, ...)
     557             : {
     558             :     va_list vargs;
     559             :     PyObject* string;
     560             : 
     561             : #ifdef HAVE_STDARG_PROTOTYPES
     562        5646 :     va_start(vargs, format);
     563             : #else
     564             :     va_start(vargs);
     565             : #endif
     566             : 
     567        5646 :     string = PyString_FromFormatV(format, vargs);
     568        5646 :     PyErr_SetObject(exception, string);
     569        5646 :     Py_XDECREF(string);
     570        5646 :     va_end(vargs);
     571        5646 :     return NULL;
     572             : }
     573             : 
     574             : 
     575             : 
     576             : PyObject *
     577          18 : PyErr_NewException(char *name, PyObject *base, PyObject *dict)
     578             : {
     579             :     char *dot;
     580          18 :     PyObject *modulename = NULL;
     581          18 :     PyObject *classname = NULL;
     582          18 :     PyObject *mydict = NULL;
     583          18 :     PyObject *bases = NULL;
     584          18 :     PyObject *result = NULL;
     585          18 :     dot = strrchr(name, '.');
     586          18 :     if (dot == NULL) {
     587           0 :         PyErr_SetString(PyExc_SystemError,
     588             :             "PyErr_NewException: name must be module.class");
     589           0 :         return NULL;
     590             :     }
     591          18 :     if (base == NULL)
     592          12 :         base = PyExc_Exception;
     593          18 :     if (dict == NULL) {
     594          18 :         dict = mydict = PyDict_New();
     595          18 :         if (dict == NULL)
     596           0 :             goto failure;
     597             :     }
     598          18 :     if (PyDict_GetItemString(dict, "__module__") == NULL) {
     599          18 :         modulename = PyString_FromStringAndSize(name,
     600             :                                              (Py_ssize_t)(dot-name));
     601          18 :         if (modulename == NULL)
     602           0 :             goto failure;
     603          18 :         if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
     604           0 :             goto failure;
     605             :     }
     606          18 :     if (PyTuple_Check(base)) {
     607           0 :         bases = base;
     608             :         /* INCREF as we create a new ref in the else branch */
     609           0 :         Py_INCREF(bases);
     610             :     } else {
     611          18 :         bases = PyTuple_Pack(1, base);
     612          18 :         if (bases == NULL)
     613           0 :             goto failure;
     614             :     }
     615             :     /* Create a real new-style class. */
     616          18 :     result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
     617             :                                    dot+1, bases, dict);
     618             :   failure:
     619          18 :     Py_XDECREF(bases);
     620          18 :     Py_XDECREF(mydict);
     621          18 :     Py_XDECREF(classname);
     622          18 :     Py_XDECREF(modulename);
     623          18 :     return result;
     624             : }
     625             : 
     626             : 
     627             : /* Create an exception with docstring */
     628             : PyObject *
     629           0 : PyErr_NewExceptionWithDoc(char *name, char *doc, PyObject *base, PyObject *dict)
     630             : {
     631             :     int result;
     632           0 :     PyObject *ret = NULL;
     633           0 :     PyObject *mydict = NULL; /* points to the dict only if we create it */
     634             :     PyObject *docobj;
     635             : 
     636           0 :     if (dict == NULL) {
     637           0 :         dict = mydict = PyDict_New();
     638           0 :         if (dict == NULL) {
     639           0 :             return NULL;
     640             :         }
     641             :     }
     642             : 
     643           0 :     if (doc != NULL) {
     644           0 :         docobj = PyString_FromString(doc);
     645           0 :         if (docobj == NULL)
     646           0 :             goto failure;
     647           0 :         result = PyDict_SetItemString(dict, "__doc__", docobj);
     648           0 :         Py_DECREF(docobj);
     649           0 :         if (result < 0)
     650           0 :             goto failure;
     651             :     }
     652             : 
     653           0 :     ret = PyErr_NewException(name, base, dict);
     654             :   failure:
     655           0 :     Py_XDECREF(mydict);
     656           0 :     return ret;
     657             : }
     658             : 
     659             : 
     660             : /* Call when an exception has occurred but there is no way for Python
     661             :    to handle it.  Examples: exception in __del__ or during GC. */
     662             : void
     663           0 : PyErr_WriteUnraisable(PyObject *obj)
     664             : {
     665             :     PyObject *f, *t, *v, *tb;
     666           0 :     PyErr_Fetch(&t, &v, &tb);
     667           0 :     f = PySys_GetObject("stderr");
     668           0 :     if (f != NULL) {
     669           0 :         PyFile_WriteString("Exception ", f);
     670           0 :         if (t) {
     671             :             PyObject* moduleName;
     672             :             char* className;
     673             :             assert(PyExceptionClass_Check(t));
     674           0 :             className = PyExceptionClass_Name(t);
     675           0 :             if (className != NULL) {
     676           0 :                 char *dot = strrchr(className, '.');
     677           0 :                 if (dot != NULL)
     678           0 :                     className = dot+1;
     679             :             }
     680             : 
     681           0 :             moduleName = PyObject_GetAttrString(t, "__module__");
     682           0 :             if (moduleName == NULL)
     683           0 :                 PyFile_WriteString("<unknown>", f);
     684             :             else {
     685           0 :                 char* modstr = PyString_AsString(moduleName);
     686           0 :                 if (modstr &&
     687           0 :                     strcmp(modstr, "exceptions") != 0)
     688             :                 {
     689           0 :                     PyFile_WriteString(modstr, f);
     690           0 :                     PyFile_WriteString(".", f);
     691             :                 }
     692             :             }
     693           0 :             if (className == NULL)
     694           0 :                 PyFile_WriteString("<unknown>", f);
     695             :             else
     696           0 :                 PyFile_WriteString(className, f);
     697           0 :             if (v && v != Py_None) {
     698           0 :                 PyFile_WriteString(": ", f);
     699           0 :                 if (PyFile_WriteObject(v, f, 0) < 0) {
     700           0 :                     PyErr_Clear();
     701           0 :                     PyFile_WriteString("<exception repr() failed>", f);
     702             :                 }
     703             :             }
     704           0 :             Py_XDECREF(moduleName);
     705             :         }
     706           0 :         PyFile_WriteString(" in ", f);
     707           0 :         if (PyFile_WriteObject(obj, f, 0) < 0) {
     708           0 :             PyErr_Clear();
     709           0 :             PyFile_WriteString("<object repr() failed>", f);
     710             :         }
     711           0 :         PyFile_WriteString(" ignored\n", f);
     712           0 :         PyErr_Clear(); /* Just in case */
     713             :     }
     714           0 :     Py_XDECREF(t);
     715           0 :     Py_XDECREF(v);
     716           0 :     Py_XDECREF(tb);
     717           0 : }
     718             : 
     719             : extern PyObject *PyModule_GetWarningsModule(void);
     720             : 
     721             : 
     722             : /* Set file and line information for the current exception.
     723             :    If the exception is not a SyntaxError, also sets additional attributes
     724             :    to make printing of exceptions believe it is a syntax error. */
     725             : 
     726             : void
     727           0 : PyErr_SyntaxLocation(const char *filename, int lineno)
     728             : {
     729             :     PyObject *exc, *v, *tb, *tmp;
     730             : 
     731             :     /* add attributes for the line number and filename for the error */
     732           0 :     PyErr_Fetch(&exc, &v, &tb);
     733           0 :     PyErr_NormalizeException(&exc, &v, &tb);
     734             :     /* XXX check that it is, indeed, a syntax error. It might not
     735             :      * be, though. */
     736           0 :     tmp = PyInt_FromLong(lineno);
     737           0 :     if (tmp == NULL)
     738           0 :         PyErr_Clear();
     739             :     else {
     740           0 :         if (PyObject_SetAttrString(v, "lineno", tmp))
     741           0 :             PyErr_Clear();
     742           0 :         Py_DECREF(tmp);
     743             :     }
     744           0 :     if (filename != NULL) {
     745           0 :         tmp = PyString_FromString(filename);
     746           0 :         if (tmp == NULL)
     747           0 :             PyErr_Clear();
     748             :         else {
     749           0 :             if (PyObject_SetAttrString(v, "filename", tmp))
     750           0 :                 PyErr_Clear();
     751           0 :             Py_DECREF(tmp);
     752             :         }
     753             : 
     754           0 :         tmp = PyErr_ProgramText(filename, lineno);
     755           0 :         if (tmp) {
     756           0 :             if (PyObject_SetAttrString(v, "text", tmp))
     757           0 :                 PyErr_Clear();
     758           0 :             Py_DECREF(tmp);
     759             :         }
     760             :     }
     761           0 :     if (PyObject_SetAttrString(v, "offset", Py_None)) {
     762           0 :         PyErr_Clear();
     763             :     }
     764           0 :     if (exc != PyExc_SyntaxError) {
     765           0 :         if (!PyObject_HasAttrString(v, "msg")) {
     766           0 :             tmp = PyObject_Str(v);
     767           0 :             if (tmp) {
     768           0 :                 if (PyObject_SetAttrString(v, "msg", tmp))
     769           0 :                     PyErr_Clear();
     770           0 :                 Py_DECREF(tmp);
     771             :             } else {
     772           0 :                 PyErr_Clear();
     773             :             }
     774             :         }
     775           0 :         if (!PyObject_HasAttrString(v, "print_file_and_line")) {
     776           0 :             if (PyObject_SetAttrString(v, "print_file_and_line",
     777             :                                        Py_None))
     778           0 :                 PyErr_Clear();
     779             :         }
     780             :     }
     781           0 :     PyErr_Restore(exc, v, tb);
     782           0 : }
     783             : 
     784             : /* com_fetch_program_text will attempt to load the line of text that
     785             :    the exception refers to.  If it fails, it will return NULL but will
     786             :    not set an exception.
     787             : 
     788             :    XXX The functionality of this function is quite similar to the
     789             :    functionality in tb_displayline() in traceback.c.
     790             : */
     791             : 
     792             : PyObject *
     793           0 : PyErr_ProgramText(const char *filename, int lineno)
     794             : {
     795             :     FILE *fp;
     796             :     int i;
     797             :     char linebuf[1000];
     798             : 
     799           0 :     if (filename == NULL || *filename == '\0' || lineno <= 0)
     800           0 :         return NULL;
     801           0 :     fp = fopen(filename, "r" PY_STDIOTEXTMODE);
     802           0 :     if (fp == NULL)
     803           0 :         return NULL;
     804           0 :     for (i = 0; i < lineno; i++) {
     805           0 :         char *pLastChar = &linebuf[sizeof(linebuf) - 2];
     806             :         do {
     807           0 :             *pLastChar = '\0';
     808           0 :             if (Py_UniversalNewlineFgets(linebuf, sizeof linebuf, fp, NULL) == NULL)
     809           0 :                 break;
     810             :             /* fgets read *something*; if it didn't get as
     811             :                far as pLastChar, it must have found a newline
     812             :                or hit the end of the file; if pLastChar is \n,
     813             :                it obviously found a newline; else we haven't
     814             :                yet seen a newline, so must continue */
     815           0 :         } while (*pLastChar != '\0' && *pLastChar != '\n');
     816             :     }
     817           0 :     fclose(fp);
     818           0 :     if (i == lineno) {
     819           0 :         char *p = linebuf;
     820           0 :         while (*p == ' ' || *p == '\t' || *p == '\014')
     821           0 :             p++;
     822           0 :         return PyString_FromString(p);
     823             :     }
     824           0 :     return NULL;
     825             : }
     826             : 
     827             : #ifdef __cplusplus
     828             : }
     829             : #endif
     830             : 

Generated by: LCOV version 1.10