Debian Codegraph @

pypy/module/cpyext/src/pyerrors.c

raw symbols SCIP
1 #include <Python.h>
2 #include <string.h>
3 #include <signal.h>
4
5 PyObject *
6 PyErr_Format(PyObject *exception, const char *format, ...)
7 {
8 va_list vargs;
9 PyObject* string;
10
11 #ifdef HAVE_STDARG_PROTOTYPES
12 va_start(vargs, format);
13 #else
14 va_start(vargs);
15 #endif
16
17 string = PyUnicode_FromFormatV(format, vargs);
18 PyErr_SetObject(exception, string);
19 Py_XDECREF(string);
20 va_end(vargs);
21 return NULL;
22 }
23
24 PyObject *
25 _PyErr_FormatFromCause(PyObject *exception, const char *format, ...)
26 {
27 PyObject *exc, *val, *val2, *tb;
28 va_list vargs;
29 PyObject* string;
30 #ifdef HAVE_STDARG_PROTOTYPES
31 va_start(vargs, format);
32 #else
33 va_start(vargs);
34 #endif
35
36 assert(PyErr_Occurred());
37 PyErr_Fetch(&exc, &val, &tb);
38 PyErr_NormalizeException(&exc, &val, &tb);
39 if (tb != NULL) {
40 PyException_SetTraceback(val, tb);
41 Py_DECREF(tb);
42 }
43 Py_DECREF(exc);
44 assert(!PyErr_Occurred());
45
46 string = PyUnicode_FromFormatV(format, vargs);
47 PyErr_SetObject(exception, string);
48 Py_XDECREF(string);
49 va_end(vargs);
50
51 PyErr_Fetch(&exc, &val2, &tb);
52 PyErr_NormalizeException(&exc, &val2, &tb);
53 Py_INCREF(val);
54 PyException_SetCause(val2, val);
55 PyException_SetContext(val2, val);
56 PyErr_Restore(exc, val2, tb);
57
58 return NULL;
59 }
60
61 PyObject *
62 PyErr_NewException(const char *name, PyObject *base, PyObject *dict)
63 {
64 const char *dot;
65 PyObject *modulename = NULL;
66 PyObject *classname = NULL;
67 PyObject *mydict = NULL;
68 PyObject *bases = NULL;
69 PyObject *result = NULL;
70 dot = strrchr(name, '.');
71 if (dot == NULL) {
72 PyErr_SetString(PyExc_SystemError,
73 "PyErr_NewException: name must be module.class");
74 return NULL;
75 }
76 if (base == NULL)
77 base = PyExc_Exception;
78 if (dict == NULL) {
79 dict = mydict = PyDict_New();
80 if (dict == NULL)
81 goto failure;
82 }
83 if (PyDict_GetItemString(dict, "__module__") == NULL) {
84 modulename = PyUnicode_FromStringAndSize(name,
85 (Py_ssize_t)(dot-name));
86 if (modulename == NULL)
87 goto failure;
88 if (PyDict_SetItemString(dict, "__module__", modulename) != 0)
89 goto failure;
90 }
91 if (PyTuple_Check(base)) {
92 bases = base;
93 /* INCREF as we create a new ref in the else branch */
94 Py_INCREF(bases);
95 } else {
96 bases = PyTuple_Pack(1, base);
97 if (bases == NULL)
98 goto failure;
99 }
100 /* Create a real new-style class. */
101 result = PyObject_CallFunction((PyObject *)&PyType_Type, "sOO",
102 dot+1, bases, dict);
103 failure:
104 Py_XDECREF(bases);
105 Py_XDECREF(mydict);
106 Py_XDECREF(classname);
107 Py_XDECREF(modulename);
108 return result;
109 }
110
111
112 /* Create an exception with docstring */
113 PyObject *
114 PyErr_NewExceptionWithDoc(const char *name, const char *doc,
115 PyObject *base, PyObject *dict)
116 {
117 int result;
118 PyObject *ret = NULL;
119 PyObject *mydict = NULL; /* points to the dict only if we create it */
120 PyObject *docobj;
121
122 if (dict == NULL) {
123 dict = mydict = PyDict_New();
124 if (dict == NULL) {
125 return NULL;
126 }
127 }
128
129 if (doc != NULL) {
130 docobj = PyUnicode_FromString(doc);
131 if (docobj == NULL)
132 goto failure;
133 result = PyDict_SetItemString(dict, "__doc__", docobj);
134 Py_DECREF(docobj);
135 if (result < 0)
136 goto failure;
137 }
138
139 ret = PyErr_NewException(name, base, dict);
140 failure:
141 Py_XDECREF(mydict);
142 return ret;
143 }
144
145 void pypysig_pushback(int);
146
147 #ifndef NSIG
148 # if defined(_NSIG)
149 # define NSIG _NSIG /* For BSD/SysV */
150 # elif defined(_SIGMAX)
151 # define NSIG (_SIGMAX + 1) /* For QNX */
152 # elif defined(SIGMAX)
153 # define NSIG (SIGMAX + 1) /* For djgpp */
154 # else
155 # define NSIG 64 /* Use a reasonable default value */
156 # endif
157 #endif
158
159 int
160 PyErr_SetInterruptEx(int signum) {
161
162 // This must not invoke the GIL
163 if (signum < 1 || signum >= NSIG) {
164 return -1;
165 }
166 pypysig_pushback(signum);
167 return 0;
168 }
169
170 void
171 PyErr_SetInterrupt() {
172 PyErr_SetInterruptEx(SIGINT);
173 }