Mercurial > cpython-lru_cache-weakref
annotate Python/_warnings.c @ 52361:dadbe014bda4 legacy-trunk
update email tests to use SkipTest
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Thu, 26 Mar 2009 21:40:29 +0000 |
| parents | a60facc9f7cf |
| children | b5d23fa9b8a4 |
| rev | line source |
|---|---|
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
1 #include "Python.h" |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
2 #include "frameobject.h" |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
3 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
4 #define MODULE_NAME "_warnings" |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
5 #define DEFAULT_ACTION_NAME "default_action" |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
6 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
7 PyDoc_STRVAR(warnings__doc__, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
8 MODULE_NAME " provides basic warning filtering support.\n" |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
9 "It is a helper module to speed up interpreter start-up."); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
10 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
11 /* Both 'filters' and 'onceregistry' can be set in warnings.py; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
12 get_warnings_attr() will reset these variables accordingly. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
13 static PyObject *_filters; /* List */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
14 static PyObject *_once_registry; /* Dict */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
15 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
16 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
17 static int |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
18 check_matched(PyObject *obj, PyObject *arg) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
19 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
20 PyObject *result; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
21 int rc; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
22 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
23 if (obj == Py_None) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
24 return 1; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
25 result = PyObject_CallMethod(obj, "match", "O", arg); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
26 if (result == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
27 return -1; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
28 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
29 rc = PyObject_IsTrue(result); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
30 Py_DECREF(result); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
31 return rc; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
32 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
33 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
34 /* |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
35 Returns a new reference. |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
36 A NULL return value can mean false or an error. |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
37 */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
38 static PyObject * |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
39 get_warnings_attr(const char *attr) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
40 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
41 static PyObject *warnings_str = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
42 PyObject *all_modules; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
43 PyObject *warnings_module; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
44 int result; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
45 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
46 if (warnings_str == NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
47 warnings_str = PyString_InternFromString("warnings"); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
48 if (warnings_str == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
49 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
50 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
51 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
52 all_modules = PyImport_GetModuleDict(); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
53 result = PyDict_Contains(all_modules, warnings_str); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
54 if (result == -1 || result == 0) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
55 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
56 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
57 warnings_module = PyDict_GetItem(all_modules, warnings_str); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
58 if (!PyObject_HasAttrString(warnings_module, attr)) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
59 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
60 return PyObject_GetAttrString(warnings_module, attr); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
61 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
62 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
63 |
|
46445
6e4ca9f72bba
Correct a refleak found by "regrtest.py -R:: test_structmembers"
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
46428
diff
changeset
|
64 static PyObject * |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
65 get_once_registry(void) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
66 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
67 PyObject *registry; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
68 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
69 registry = get_warnings_attr("onceregistry"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
70 if (registry == NULL) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
71 if (PyErr_Occurred()) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
72 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
73 return _once_registry; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
74 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
75 Py_DECREF(_once_registry); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
76 _once_registry = registry; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
77 return registry; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
78 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
79 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
80 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
81 /* The item is a borrowed reference. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
82 static const char * |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
83 get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
84 PyObject *module, PyObject **item) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
85 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
86 PyObject *action, *m, *d; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
87 Py_ssize_t i; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
88 PyObject *warnings_filters; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
89 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
90 warnings_filters = get_warnings_attr("filters"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
91 if (warnings_filters == NULL) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
92 if (PyErr_Occurred()) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
93 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
94 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
95 else { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
96 Py_DECREF(_filters); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
97 _filters = warnings_filters; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
98 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
99 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
100 if (!PyList_Check(_filters)) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
101 PyErr_SetString(PyExc_ValueError, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
102 MODULE_NAME ".filters must be a list"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
103 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
104 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
105 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
106 /* _filters could change while we are iterating over it. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
107 for (i = 0; i < PyList_GET_SIZE(_filters); i++) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
108 PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
109 Py_ssize_t ln; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
110 int is_subclass, good_msg, good_mod; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
111 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
112 tmp_item = *item = PyList_GET_ITEM(_filters, i); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
113 if (PyTuple_Size(tmp_item) != 5) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
114 PyErr_Format(PyExc_ValueError, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
115 MODULE_NAME ".filters item %zd isn't a 5-tuple", i); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
116 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
117 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
118 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
119 /* Python code: action, msg, cat, mod, ln = item */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
120 action = PyTuple_GET_ITEM(tmp_item, 0); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
121 msg = PyTuple_GET_ITEM(tmp_item, 1); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
122 cat = PyTuple_GET_ITEM(tmp_item, 2); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
123 mod = PyTuple_GET_ITEM(tmp_item, 3); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
124 ln_obj = PyTuple_GET_ITEM(tmp_item, 4); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
125 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
126 good_msg = check_matched(msg, text); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
127 good_mod = check_matched(mod, module); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
128 is_subclass = PyObject_IsSubclass(category, cat); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
129 ln = PyInt_AsSsize_t(ln_obj); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
130 if (good_msg == -1 || good_mod == -1 || is_subclass == -1 || |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
131 (ln == -1 && PyErr_Occurred())) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
132 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
133 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
134 if (good_msg && is_subclass && good_mod && (ln == 0 || lineno == ln)) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
135 return PyString_AsString(action); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
136 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
137 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
138 m = PyImport_ImportModule(MODULE_NAME); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
139 if (m == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
140 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
141 d = PyModule_GetDict(m); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
142 Py_DECREF(m); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
143 if (d == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
144 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
145 action = PyDict_GetItemString(d, DEFAULT_ACTION_NAME); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
146 if (action != NULL) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
147 return PyString_AsString(action); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
148 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
149 PyErr_SetString(PyExc_ValueError, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
150 MODULE_NAME "." DEFAULT_ACTION_NAME " not found"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
151 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
152 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
153 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
154 static int |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
155 already_warned(PyObject *registry, PyObject *key, int should_set) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
156 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
157 PyObject *already_warned; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
158 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
159 if (key == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
160 return -1; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
161 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
162 already_warned = PyDict_GetItem(registry, key); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
163 if (already_warned != NULL) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
164 int rc = PyObject_IsTrue(already_warned); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
165 if (rc != 0) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
166 return rc; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
167 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
168 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
169 /* This warning wasn't found in the registry, set it. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
170 if (should_set) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
171 return PyDict_SetItem(registry, key, Py_True); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
172 return 0; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
173 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
174 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
175 /* New reference. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
176 static PyObject * |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
177 normalize_module(PyObject *filename) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
178 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
179 PyObject *module; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
180 const char *mod_str; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
181 Py_ssize_t len; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
182 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
183 int rc = PyObject_IsTrue(filename); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
184 if (rc == -1) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
185 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
186 else if (rc == 0) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
187 return PyString_FromString("<unknown>"); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
188 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
189 mod_str = PyString_AsString(filename); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
190 if (mod_str == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
191 return NULL; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
192 len = PyString_Size(filename); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
193 if (len < 0) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
194 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
195 if (len >= 3 && |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
196 strncmp(mod_str + (len - 3), ".py", 3) == 0) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
197 module = PyString_FromStringAndSize(mod_str, len-3); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
198 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
199 else { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
200 module = filename; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
201 Py_INCREF(module); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
202 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
203 return module; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
204 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
205 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
206 static int |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
207 update_registry(PyObject *registry, PyObject *text, PyObject *category, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
208 int add_zero) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
209 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
210 PyObject *altkey, *zero = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
211 int rc; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
212 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
213 if (add_zero) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
214 zero = PyInt_FromLong(0); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
215 if (zero == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
216 return -1; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
217 altkey = PyTuple_Pack(3, text, category, zero); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
218 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
219 else |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
220 altkey = PyTuple_Pack(2, text, category); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
221 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
222 rc = already_warned(registry, altkey, 1); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
223 Py_XDECREF(zero); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
224 Py_XDECREF(altkey); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
225 return rc; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
226 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
227 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
228 static void |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
229 show_warning(PyObject *filename, int lineno, PyObject *text, PyObject |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
230 *category, PyObject *sourceline) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
231 { |
|
46667
d9071fa8244a
Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents:
46637
diff
changeset
|
232 PyObject *f_stderr; |
|
d9071fa8244a
Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents:
46637
diff
changeset
|
233 PyObject *name; |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
234 char lineno_str[128]; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
235 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
236 PyOS_snprintf(lineno_str, sizeof(lineno_str), ":%d: ", lineno); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
237 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
238 name = PyObject_GetAttrString(category, "__name__"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
239 if (name == NULL) /* XXX Can an object lack a '__name__' attribute? */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
240 return; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
241 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
242 f_stderr = PySys_GetObject("stderr"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
243 if (f_stderr == NULL) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
244 fprintf(stderr, "lost sys.stderr\n"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
245 Py_DECREF(name); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
246 return; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
247 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
248 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
249 /* Print "filename:lineno: category: text\n" */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
250 PyFile_WriteObject(filename, f_stderr, Py_PRINT_RAW); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
251 PyFile_WriteString(lineno_str, f_stderr); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
252 PyFile_WriteObject(name, f_stderr, Py_PRINT_RAW); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
253 PyFile_WriteString(": ", f_stderr); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
254 PyFile_WriteObject(text, f_stderr, Py_PRINT_RAW); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
255 PyFile_WriteString("\n", f_stderr); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
256 Py_XDECREF(name); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
257 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
258 /* Print " source_line\n" */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
259 if (sourceline) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
260 char *source_line_str = PyString_AS_STRING(sourceline); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
261 while (*source_line_str == ' ' || *source_line_str == '\t' || |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
262 *source_line_str == '\014') |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
263 source_line_str++; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
264 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
265 PyFile_WriteString(source_line_str, f_stderr); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
266 PyFile_WriteString("\n", f_stderr); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
267 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
268 else |
|
47933
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47835
diff
changeset
|
269 _Py_DisplaySourceLine(f_stderr, PyString_AS_STRING(filename), |
|
4f804ee772eb
#3342: In tracebacks, printed source lines were not indented since r62555.
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
47835
diff
changeset
|
270 lineno, 2); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
271 PyErr_Clear(); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
272 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
273 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
274 static PyObject * |
|
46667
d9071fa8244a
Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents:
46637
diff
changeset
|
275 warn_explicit(PyObject *category, PyObject *message, |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
276 PyObject *filename, int lineno, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
277 PyObject *module, PyObject *registry, PyObject *sourceline) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
278 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
279 PyObject *key = NULL, *text = NULL, *result = NULL, *lineno_obj = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
280 PyObject *item = Py_None; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
281 const char *action; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
282 int rc; |
|
47835
dc42efd728ee
warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents:
47527
diff
changeset
|
283 |
|
dc42efd728ee
warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents:
47527
diff
changeset
|
284 if (registry && !PyDict_Check(registry) && (registry != Py_None)) { |
|
dc42efd728ee
warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents:
47527
diff
changeset
|
285 PyErr_SetString(PyExc_TypeError, "'registry' must be a dict"); |
|
dc42efd728ee
warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents:
47527
diff
changeset
|
286 return NULL; |
|
dc42efd728ee
warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents:
47527
diff
changeset
|
287 } |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
288 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
289 /* Normalize module. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
290 if (module == NULL) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
291 module = normalize_module(filename); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
292 if (module == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
293 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
294 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
295 else |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
296 Py_INCREF(module); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
297 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
298 /* Normalize message. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
299 Py_INCREF(message); /* DECREF'ed in cleanup. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
300 rc = PyObject_IsInstance(message, PyExc_Warning); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
301 if (rc == -1) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
302 goto cleanup; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
303 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
304 if (rc == 1) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
305 text = PyObject_Str(message); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
306 category = (PyObject*)message->ob_type; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
307 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
308 else { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
309 text = message; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
310 message = PyObject_CallFunction(category, "O", message); |
|
47835
dc42efd728ee
warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents:
47527
diff
changeset
|
311 if (message == NULL) |
|
dc42efd728ee
warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents:
47527
diff
changeset
|
312 goto cleanup; |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
313 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
314 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
315 lineno_obj = PyInt_FromLong(lineno); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
316 if (lineno_obj == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
317 goto cleanup; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
318 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
319 /* Create key. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
320 key = PyTuple_Pack(3, text, category, lineno_obj); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
321 if (key == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
322 goto cleanup; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
323 |
|
47835
dc42efd728ee
warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents:
47527
diff
changeset
|
324 if ((registry != NULL) && (registry != Py_None)) { |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
325 rc = already_warned(registry, key, 0); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
326 if (rc == -1) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
327 goto cleanup; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
328 else if (rc == 1) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
329 goto return_none; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
330 /* Else this warning hasn't been generated before. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
331 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
332 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
333 action = get_filter(category, text, lineno, module, &item); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
334 if (action == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
335 goto cleanup; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
336 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
337 if (strcmp(action, "error") == 0) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
338 PyErr_SetObject(category, message); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
339 goto cleanup; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
340 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
341 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
342 /* Store in the registry that we've been here, *except* when the action |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
343 is "always". */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
344 rc = 0; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
345 if (strcmp(action, "always") != 0) { |
|
47835
dc42efd728ee
warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents:
47527
diff
changeset
|
346 if (registry != NULL && registry != Py_None && |
|
dc42efd728ee
warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents:
47527
diff
changeset
|
347 PyDict_SetItem(registry, key, Py_True) < 0) |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
348 goto cleanup; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
349 else if (strcmp(action, "ignore") == 0) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
350 goto return_none; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
351 else if (strcmp(action, "once") == 0) { |
|
47835
dc42efd728ee
warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents:
47527
diff
changeset
|
352 if (registry == NULL || registry == Py_None) { |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
353 registry = get_once_registry(); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
354 if (registry == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
355 goto cleanup; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
356 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
357 /* _once_registry[(text, category)] = 1 */ |
|
46667
d9071fa8244a
Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents:
46637
diff
changeset
|
358 rc = update_registry(registry, text, category, 0); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
359 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
360 else if (strcmp(action, "module") == 0) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
361 /* registry[(text, category, 0)] = 1 */ |
|
47835
dc42efd728ee
warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents:
47527
diff
changeset
|
362 if (registry != NULL && registry != Py_None) |
|
46667
d9071fa8244a
Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents:
46637
diff
changeset
|
363 rc = update_registry(registry, text, category, 0); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
364 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
365 else if (strcmp(action, "default") != 0) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
366 PyObject *to_str = PyObject_Str(item); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
367 const char *err_str = "???"; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
368 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
369 if (to_str != NULL) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
370 err_str = PyString_AS_STRING(to_str); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
371 PyErr_Format(PyExc_RuntimeError, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
372 "Unrecognized action (%s) in warnings.filters:\n %s", |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
373 action, err_str); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
374 Py_XDECREF(to_str); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
375 goto cleanup; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
376 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
377 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
378 |
|
49386
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
48948
diff
changeset
|
379 if (rc == 1) /* Already warned for this module. */ |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
380 goto return_none; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
381 if (rc == 0) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
382 PyObject *show_fxn = get_warnings_attr("showwarning"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
383 if (show_fxn == NULL) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
384 if (PyErr_Occurred()) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
385 goto cleanup; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
386 show_warning(filename, lineno, text, category, sourceline); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
387 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
388 else { |
|
52125
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
389 PyObject *res; |
|
46667
d9071fa8244a
Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents:
46637
diff
changeset
|
390 |
|
52125
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
391 if (!PyMethod_Check(show_fxn) && !PyFunction_Check(show_fxn)) { |
|
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
392 PyErr_SetString(PyExc_TypeError, |
|
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
393 "warnings.showwarning() must be set to a " |
|
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
394 "function or method"); |
|
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
395 Py_DECREF(show_fxn); |
|
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
396 goto cleanup; |
|
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
397 } |
|
46667
d9071fa8244a
Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents:
46637
diff
changeset
|
398 |
|
52125
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
399 res = PyObject_CallFunctionObjArgs(show_fxn, message, category, |
|
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
400 filename, lineno_obj, |
|
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
401 NULL); |
|
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
402 Py_DECREF(show_fxn); |
|
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
403 Py_XDECREF(res); |
|
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
404 if (res == NULL) |
|
a60facc9f7cf
Require implementations for warnings.showwarning() support the 'line' argument.
Brett Cannon <bcannon@gmail.com>
parents:
49386
diff
changeset
|
405 goto cleanup; |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
406 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
407 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
408 else /* if (rc == -1) */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
409 goto cleanup; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
410 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
411 return_none: |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
412 result = Py_None; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
413 Py_INCREF(result); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
414 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
415 cleanup: |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
416 Py_XDECREF(key); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
417 Py_XDECREF(text); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
418 Py_XDECREF(lineno_obj); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
419 Py_DECREF(module); |
|
47835
dc42efd728ee
warnings.warn_explicit() did not have the proper TypeErrors in place to prevent
Brett Cannon <bcannon@gmail.com>
parents:
47527
diff
changeset
|
420 Py_XDECREF(message); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
421 return result; /* Py_None or NULL. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
422 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
423 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
424 /* filename, module, and registry are new refs, globals is borrowed */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
425 /* Returns 0 on error (no new refs), 1 on success */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
426 static int |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
427 setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
428 PyObject **module, PyObject **registry) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
429 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
430 PyObject *globals; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
431 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
432 /* Setup globals and lineno. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
433 PyFrameObject *f = PyThreadState_GET()->frame; |
|
46686
2f351fa53a6b
Fix a bug in the handling of the stacklevel argument in warnings.warn() where
Brett Cannon <bcannon@gmail.com>
parents:
46667
diff
changeset
|
434 while (--stack_level > 0 && f != NULL) |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
435 f = f->f_back; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
436 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
437 if (f == NULL) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
438 globals = PyThreadState_Get()->interp->sysdict; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
439 *lineno = 1; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
440 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
441 else { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
442 globals = f->f_globals; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
443 *lineno = PyCode_Addr2Line(f->f_code, f->f_lasti); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
444 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
445 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
446 *module = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
447 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
448 /* Setup registry. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
449 assert(globals != NULL); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
450 assert(PyDict_Check(globals)); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
451 *registry = PyDict_GetItemString(globals, "__warningregistry__"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
452 if (*registry == NULL) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
453 int rc; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
454 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
455 *registry = PyDict_New(); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
456 if (*registry == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
457 return 0; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
458 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
459 rc = PyDict_SetItemString(globals, "__warningregistry__", *registry); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
460 if (rc < 0) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
461 goto handle_error; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
462 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
463 else |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
464 Py_INCREF(*registry); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
465 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
466 /* Setup module. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
467 *module = PyDict_GetItemString(globals, "__name__"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
468 if (*module == NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
469 *module = PyString_FromString("<string>"); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
470 if (*module == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
471 goto handle_error; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
472 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
473 else |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
474 Py_INCREF(*module); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
475 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
476 /* Setup filename. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
477 *filename = PyDict_GetItemString(globals, "__file__"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
478 if (*filename != NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
479 Py_ssize_t len = PyString_Size(*filename); |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
480 const char *file_str = PyString_AsString(*filename); |
|
46634
cc66029d2efe
Fix some indentation errors.
Brett Cannon <bcannon@gmail.com>
parents:
46622
diff
changeset
|
481 if (file_str == NULL || (len < 0 && PyErr_Occurred())) |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
482 goto handle_error; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
483 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
484 /* if filename.lower().endswith((".pyc", ".pyo")): */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
485 if (len >= 4 && |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
486 file_str[len-4] == '.' && |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
487 tolower(file_str[len-3]) == 'p' && |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
488 tolower(file_str[len-2]) == 'y' && |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
489 (tolower(file_str[len-1]) == 'c' || |
|
46634
cc66029d2efe
Fix some indentation errors.
Brett Cannon <bcannon@gmail.com>
parents:
46622
diff
changeset
|
490 tolower(file_str[len-1]) == 'o')) |
|
cc66029d2efe
Fix some indentation errors.
Brett Cannon <bcannon@gmail.com>
parents:
46622
diff
changeset
|
491 { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
492 *filename = PyString_FromStringAndSize(file_str, len-1); |
|
46634
cc66029d2efe
Fix some indentation errors.
Brett Cannon <bcannon@gmail.com>
parents:
46622
diff
changeset
|
493 if (*filename == NULL) |
|
cc66029d2efe
Fix some indentation errors.
Brett Cannon <bcannon@gmail.com>
parents:
46622
diff
changeset
|
494 goto handle_error; |
|
cc66029d2efe
Fix some indentation errors.
Brett Cannon <bcannon@gmail.com>
parents:
46622
diff
changeset
|
495 } |
|
cc66029d2efe
Fix some indentation errors.
Brett Cannon <bcannon@gmail.com>
parents:
46622
diff
changeset
|
496 else |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
497 Py_INCREF(*filename); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
498 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
499 else { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
500 const char *module_str = PyString_AsString(*module); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
501 if (module_str && strcmp(module_str, "__main__") == 0) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
502 PyObject *argv = PySys_GetObject("argv"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
503 if (argv != NULL && PyList_Size(argv) > 0) { |
|
46637
a859f835ef53
Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents:
46634
diff
changeset
|
504 int is_true; |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
505 *filename = PyList_GetItem(argv, 0); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
506 Py_INCREF(*filename); |
|
46637
a859f835ef53
Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents:
46634
diff
changeset
|
507 /* If sys.argv[0] is false, then use '__main__'. */ |
|
a859f835ef53
Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents:
46634
diff
changeset
|
508 is_true = PyObject_IsTrue(*filename); |
|
a859f835ef53
Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents:
46634
diff
changeset
|
509 if (is_true < 0) { |
|
a859f835ef53
Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents:
46634
diff
changeset
|
510 Py_DECREF(*filename); |
|
a859f835ef53
Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents:
46634
diff
changeset
|
511 goto handle_error; |
|
a859f835ef53
Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents:
46634
diff
changeset
|
512 } |
|
a859f835ef53
Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents:
46634
diff
changeset
|
513 else if (!is_true) { |
|
a859f835ef53
Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents:
46634
diff
changeset
|
514 Py_DECREF(*filename); |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
515 *filename = PyString_FromString("__main__"); |
|
46637
a859f835ef53
Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents:
46634
diff
changeset
|
516 if (*filename == NULL) |
|
a859f835ef53
Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents:
46634
diff
changeset
|
517 goto handle_error; |
|
a859f835ef53
Fix the C implementation of 'warnings' to infer the filename of the module that
Brett Cannon <bcannon@gmail.com>
parents:
46634
diff
changeset
|
518 } |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
519 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
520 else { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
521 /* embedded interpreters don't have sys.argv, see bug #839151 */ |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
522 *filename = PyString_FromString("__main__"); |
|
46634
cc66029d2efe
Fix some indentation errors.
Brett Cannon <bcannon@gmail.com>
parents:
46622
diff
changeset
|
523 if (*filename == NULL) |
|
cc66029d2efe
Fix some indentation errors.
Brett Cannon <bcannon@gmail.com>
parents:
46622
diff
changeset
|
524 goto handle_error; |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
525 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
526 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
527 if (*filename == NULL) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
528 *filename = *module; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
529 Py_INCREF(*filename); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
530 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
531 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
532 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
533 return 1; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
534 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
535 handle_error: |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
536 /* filename not XDECREF'ed here as there is no way to jump here with a |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
537 dangling reference. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
538 Py_XDECREF(*registry); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
539 Py_XDECREF(*module); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
540 return 0; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
541 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
542 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
543 static PyObject * |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
544 get_category(PyObject *message, PyObject *category) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
545 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
546 int rc; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
547 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
548 /* Get category. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
549 rc = PyObject_IsInstance(message, PyExc_Warning); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
550 if (rc == -1) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
551 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
552 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
553 if (rc == 1) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
554 category = (PyObject*)message->ob_type; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
555 else if (category == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
556 category = PyExc_UserWarning; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
557 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
558 /* Validate category. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
559 rc = PyObject_IsSubclass(category, PyExc_Warning); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
560 if (rc == -1) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
561 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
562 if (rc == 0) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
563 PyErr_SetString(PyExc_ValueError, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
564 "category is not a subclass of Warning"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
565 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
566 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
567 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
568 return category; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
569 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
570 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
571 static PyObject * |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
572 do_warn(PyObject *message, PyObject *category, Py_ssize_t stack_level) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
573 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
574 PyObject *filename, *module, *registry, *res; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
575 int lineno; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
576 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
577 if (!setup_context(stack_level, &filename, &lineno, &module, ®istry)) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
578 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
579 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
580 res = warn_explicit(category, message, filename, lineno, module, registry, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
581 NULL); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
582 Py_DECREF(filename); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
583 Py_DECREF(registry); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
584 Py_DECREF(module); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
585 return res; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
586 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
587 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
588 static PyObject * |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
589 warnings_warn(PyObject *self, PyObject *args, PyObject *kwds) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
590 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
591 static char *kw_list[] = { "message", "category", "stacklevel", 0 }; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
592 PyObject *message, *category = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
593 Py_ssize_t stack_level = 1; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
594 |
|
46667
d9071fa8244a
Add a DeprecationWarning for when warnings.showwarning() is set to a function
Brett Cannon <bcannon@gmail.com>
parents:
46637
diff
changeset
|
595 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|On:warn", kw_list, |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
596 &message, &category, &stack_level)) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
597 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
598 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
599 category = get_category(message, category); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
600 if (category == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
601 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
602 return do_warn(message, category, stack_level); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
603 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
604 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
605 static PyObject * |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
606 warnings_warn_explicit(PyObject *self, PyObject *args, PyObject *kwds) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
607 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
608 static char *kwd_list[] = {"message", "category", "filename", "lineno", |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
609 "module", "registry", "module_globals", 0}; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
610 PyObject *message; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
611 PyObject *category; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
612 PyObject *filename; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
613 int lineno; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
614 PyObject *module = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
615 PyObject *registry = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
616 PyObject *module_globals = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
617 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
618 if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOOi|OOO:warn_explicit", |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
619 kwd_list, &message, &category, &filename, &lineno, &module, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
620 ®istry, &module_globals)) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
621 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
622 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
623 if (module_globals) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
624 static PyObject *get_source_name = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
625 static PyObject *splitlines_name = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
626 PyObject *loader; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
627 PyObject *module_name; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
628 PyObject *source; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
629 PyObject *source_list; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
630 PyObject *source_line; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
631 PyObject *returned; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
632 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
633 if (get_source_name == NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
634 get_source_name = PyString_InternFromString("get_source"); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
635 if (!get_source_name) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
636 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
637 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
638 if (splitlines_name == NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
639 splitlines_name = PyString_InternFromString("splitlines"); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
640 if (!splitlines_name) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
641 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
642 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
643 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
644 /* Check/get the requisite pieces needed for the loader. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
645 loader = PyDict_GetItemString(module_globals, "__loader__"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
646 module_name = PyDict_GetItemString(module_globals, "__name__"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
647 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
648 if (loader == NULL || module_name == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
649 goto standard_call; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
650 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
651 /* Make sure the loader implements the optional get_source() method. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
652 if (!PyObject_HasAttrString(loader, "get_source")) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
653 goto standard_call; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
654 /* Call get_source() to get the source code. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
655 source = PyObject_CallMethodObjArgs(loader, get_source_name, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
656 module_name, NULL); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
657 if (!source) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
658 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
659 else if (source == Py_None) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
660 Py_DECREF(Py_None); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
661 goto standard_call; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
662 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
663 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
664 /* Split the source into lines. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
665 source_list = PyObject_CallMethodObjArgs(source, splitlines_name, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
666 NULL); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
667 Py_DECREF(source); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
668 if (!source_list) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
669 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
670 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
671 /* Get the source line. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
672 source_line = PyList_GetItem(source_list, lineno-1); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
673 if (!source_line) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
674 Py_DECREF(source_list); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
675 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
676 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
677 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
678 /* Handle the warning. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
679 returned = warn_explicit(category, message, filename, lineno, module, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
680 registry, source_line); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
681 Py_DECREF(source_list); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
682 return returned; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
683 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
684 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
685 standard_call: |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
686 return warn_explicit(category, message, filename, lineno, module, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
687 registry, NULL); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
688 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
689 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
690 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
691 /* Function to issue a warning message; may raise an exception. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
692 int |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
693 PyErr_WarnEx(PyObject *category, const char *text, Py_ssize_t stack_level) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
694 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
695 PyObject *res; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
696 PyObject *message = PyString_FromString(text); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
697 if (message == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
698 return -1; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
699 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
700 if (category == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
701 category = PyExc_RuntimeWarning; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
702 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
703 res = do_warn(message, category, stack_level); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
704 Py_DECREF(message); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
705 if (res == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
706 return -1; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
707 Py_DECREF(res); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
708 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
709 return 0; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
710 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
711 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
712 /* PyErr_Warn is only for backwards compatability and will be removed. |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
713 Use PyErr_WarnEx instead. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
714 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
715 #undef PyErr_Warn |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
716 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
717 PyAPI_FUNC(int) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
718 PyErr_Warn(PyObject *category, char *text) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
719 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
720 return PyErr_WarnEx(category, text, 1); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
721 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
722 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
723 /* Warning with explicit origin */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
724 int |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
725 PyErr_WarnExplicit(PyObject *category, const char *text, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
726 const char *filename_str, int lineno, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
727 const char *module_str, PyObject *registry) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
728 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
729 PyObject *res; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
730 PyObject *message = PyString_FromString(text); |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
731 PyObject *filename = PyString_FromString(filename_str); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
732 PyObject *module = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
733 int ret = -1; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
734 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
735 if (message == NULL || filename == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
736 goto exit; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
737 if (module_str != NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
738 module = PyString_FromString(module_str); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
739 if (module == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
740 goto exit; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
741 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
742 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
743 if (category == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
744 category = PyExc_RuntimeWarning; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
745 res = warn_explicit(category, message, filename, lineno, module, registry, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
746 NULL); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
747 if (res == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
748 goto exit; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
749 Py_DECREF(res); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
750 ret = 0; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
751 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
752 exit: |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
753 Py_XDECREF(message); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
754 Py_XDECREF(module); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
755 Py_XDECREF(filename); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
756 return ret; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
757 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
758 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
759 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
760 PyDoc_STRVAR(warn_doc, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
761 "Issue a warning, or maybe ignore it or raise an exception."); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
762 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
763 PyDoc_STRVAR(warn_explicit_doc, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
764 "Low-level inferface to warnings functionality."); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
765 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
766 static PyMethodDef warnings_functions[] = { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
767 {"warn", (PyCFunction)warnings_warn, METH_VARARGS | METH_KEYWORDS, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
768 warn_doc}, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
769 {"warn_explicit", (PyCFunction)warnings_warn_explicit, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
770 METH_VARARGS | METH_KEYWORDS, warn_explicit_doc}, |
|
49386
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
48948
diff
changeset
|
771 /* XXX(brett.cannon): add showwarning? */ |
|
642f20b22577
Merged revisions 66748 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
48948
diff
changeset
|
772 /* XXX(brett.cannon): Reasonable to add formatwarning? */ |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
773 {NULL, NULL} /* sentinel */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
774 }; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
775 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
776 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
777 static PyObject * |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
778 create_filter(PyObject *category, const char *action) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
779 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
780 static PyObject *ignore_str = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
781 static PyObject *error_str = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
782 static PyObject *default_str = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
783 PyObject *action_obj = NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
784 PyObject *lineno, *result; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
785 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
786 if (!strcmp(action, "ignore")) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
787 if (ignore_str == NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
788 ignore_str = PyString_InternFromString("ignore"); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
789 if (ignore_str == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
790 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
791 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
792 action_obj = ignore_str; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
793 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
794 else if (!strcmp(action, "error")) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
795 if (error_str == NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
796 error_str = PyString_InternFromString("error"); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
797 if (error_str == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
798 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
799 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
800 action_obj = error_str; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
801 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
802 else if (!strcmp(action, "default")) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
803 if (default_str == NULL) { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
804 default_str = PyString_InternFromString("default"); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
805 if (default_str == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
806 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
807 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
808 action_obj = default_str; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
809 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
810 else { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
811 Py_FatalError("unknown action"); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
812 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
813 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
814 /* This assumes the line number is zero for now. */ |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
815 lineno = PyInt_FromLong(0); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
816 if (lineno == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
817 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
818 result = PyTuple_Pack(5, action_obj, Py_None, category, Py_None, lineno); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
819 Py_DECREF(lineno); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
820 return result; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
821 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
822 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
823 static PyObject * |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
824 init_filters(void) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
825 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
826 PyObject *filters = PyList_New(3); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
827 const char *bytes_action; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
828 if (filters == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
829 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
830 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
831 PyList_SET_ITEM(filters, 0, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
832 create_filter(PyExc_PendingDeprecationWarning, "ignore")); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
833 PyList_SET_ITEM(filters, 1, create_filter(PyExc_ImportWarning, "ignore")); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
834 if (Py_BytesWarningFlag > 1) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
835 bytes_action = "error"; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
836 else if (Py_BytesWarningFlag) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
837 bytes_action = "default"; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
838 else |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
839 bytes_action = "ignore"; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
840 PyList_SET_ITEM(filters, 2, create_filter(PyExc_BytesWarning, |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
841 bytes_action)); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
842 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
843 if (PyList_GET_ITEM(filters, 0) == NULL || |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
844 PyList_GET_ITEM(filters, 1) == NULL || |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
845 PyList_GET_ITEM(filters, 2) == NULL) { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
846 Py_DECREF(filters); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
847 return NULL; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
848 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
849 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
850 return filters; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
851 } |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
852 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
853 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
854 PyMODINIT_FUNC |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
855 _PyWarnings_Init(void) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
856 { |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
857 PyObject *m, *default_action; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
858 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
859 m = Py_InitModule3(MODULE_NAME, warnings_functions, warnings__doc__); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
860 if (m == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
861 return; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
862 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
863 _filters = init_filters(); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
864 if (_filters == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
865 return; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
866 Py_INCREF(_filters); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
867 if (PyModule_AddObject(m, "filters", _filters) < 0) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
868 return; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
869 |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
870 _once_registry = PyDict_New(); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
871 if (_once_registry == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
872 return; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
873 Py_INCREF(_once_registry); |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
874 if (PyModule_AddObject(m, "once_registry", _once_registry) < 0) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
875 return; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
876 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47321
diff
changeset
|
877 default_action = PyString_InternFromString("default"); |
|
46414
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
878 if (default_action == NULL) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
879 return; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
880 if (PyModule_AddObject(m, DEFAULT_ACTION_NAME, default_action) < 0) |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
881 return; |
|
840064802952
Re-implement the 'warnings' module in C. This allows for usage of the
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
882 } |
