[issue1631171] implement warnings module in C
Brett Cannon [EMAIL PROTECTED] added the comment: Committed in revision 62303. -- resolution: - accepted status: pending - closed _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon [EMAIL PROTECTED] added the comment: After all the threats about checking in code that break stuff, I am not about to check this in. =) I will get to the changes when I can and then commit after the alpha. -- assignee: nnorwitz - brett.cannon _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon [EMAIL PROTECTED] added the comment: Neal's issues are addressed in this patch. I also finally filled out warnings.h. The only thing that I didn't deal with is Neal's worry of exposing _PyWarnings_Init(). It is not explicitly exported anywhere as part of the API so I am not sure what he is worrying about. The attached patch is finished for CPython. I do want to go back and put back in the pure Python code that _warnings.c replaces so that alternative VMs don't have to implement any part of warnings if they don't want to. Added file: http://bugs.python.org/file9917/c_warnings.diff _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon [EMAIL PROTECTED]: Removed file: http://bugs.python.org/file9761/c_warnings.diff _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon [EMAIL PROTECTED]: Removed file: http://bugs.python.org/file9917/c_warnings.diff _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon [EMAIL PROTECTED] added the comment: Attached should have everything, including a pure Python fallback. As soon as the next alpha is out I will apply. -- status: open - pending Added file: http://bugs.python.org/file9920/c_warnings.diff _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Neal Norwitz [EMAIL PROTECTED] added the comment: On Tue, Apr 1, 2008 at 6:14 AM, Brett Cannon [EMAIL PROTECTED] wrote: Brett Cannon [EMAIL PROTECTED] added the comment: Neal's issues are addressed in this patch. I also finally filled out warnings.h. The only thing that I didn't deal with is Neal's worry of exposing _PyWarnings_Init(). It is not explicitly exported anywhere as part of the API so I am not sure what he is worrying about. I wasn't so worried about exposing it, I didn't like the name pollution (we're talking about init_warnings, right?). I know that we have other modules that use init*, but those are broken too. I'm not sure we should fix those in 2.6, but 3.0 we should. _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon [EMAIL PROTECTED] added the comment: On another note, the warnings module should be made to work with or without _warnings. that way IronPython, Jython, and PyPy won't have to re- implement stuff. This also means that test cases need to be changed to test this. -- priority: normal - high _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Neal Norwitz [EMAIL PROTECTED] added the comment: I didn't realize this was waiting for me. You should have just checked it in, that would have gotten me to review faster. :-) pythonrun.c: * Should PyModule_GetWarningsModule() return a valid pointer? * The code below crashes. Need to XDECREF, not DECREF (or similar). +PyObject *warnings_module = PyImport_ImportModule(warnings); +if (!warnings_module) +PyErr_Clear(); +Py_DECREF(warnings_module); Python/_warnings.c: * Remove // XXX(nnorwitz): need to parse -W cmd line flags Include/pythonrun.h * init_warnings has the wrong name (not prefixed with _Py). I'm not sure it should be exported at all. test_support/frozen: did you want the captured_std{out,err} change in this patch? Changes to Makefile.pre.in other than adding _warnings.o? I think this is good enough if it's working. How about checking it in after 1) the alpha is released Wed and 2) fixing up the nits? _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon [EMAIL PROTECTED]: Removed file: http://bugs.python.org/file9490/c_warnings.diff _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon [EMAIL PROTECTED]: Removed file: http://bugs.python.org/file9667/c_warnings.diff _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon [EMAIL PROTECTED] added the comment: Attached is what I think is a completely working version of warnings implemented in a mixture of C and Python. I am not worrying about documenting the new C APIs I had to add since they are pretty specific to internal stuff. Probably should add a leading '_', but I'm tired. =) I think the fleshed out tests do a pretty good job of testing new code. Even if this patch gets held up the tests should definitely get backported as is reasonable. Assigning to Neal to review (hopefully soon). -- assignee: brett.cannon - nnorwitz Added file: http://bugs.python.org/file9761/c_warnings.diff _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon [EMAIL PROTECTED] added the comment: Add tests for the 'line' argument to formatwarning() and showwarning(). Added file: http://bugs.python.org/file9667/c_warnings.diff _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon added the comment: Patch that has been brought up-to-date with r60968. No new work, though. Added file: http://bugs.python.org/file9490/c_warnings.diff _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon: Removed file: http://bugs.python.org/file7708/c-warnings.diff _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon: Removed file: http://bugs.python.org/file7709/_warnings.c _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon: Removed file: http://bugs.python.org/file8495/_warnings.c _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon: Removed file: http://bugs.python.org/file8496/c_warnings.diff _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon: Removed file: http://bugs.python.org/file8557/_warnings.c _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon: Removed file: http://bugs.python.org/file8565/_warnings.c _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon: Removed file: http://bugs.python.org/file8772/test_warnings.py _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon: Removed file: http://bugs.python.org/file8783/warnings.diff _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon: Removed file: http://bugs.python.org/file8858/_warnings.diff _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon added the comment: I see two ways of implementing the fetching of a source code line from __loader__.get_source(). One is to do it in Python. We have a function provided that can suppress the second line of output from a warning and just handle it in Python code. That has the requirement that Python code be available to import, but if you are using Lib/warnings.py instead of Python/_warnings.c that is pretty much guaranteed. The other option is to rely on the fact that get_source() is supposed to use universal newlines. Then we can find the index of the x and x-1 newlines and print the substring between the two. That can be done in C code by checking for the loader, checking for get_source(), calling it, getting the char buffer, and then just looping through looking for the needed newlines to find the needed indexes. Otherwise we can use the Python API on strings to do what would have been done in pure Python, but that is a lot of PyObject_Call() usage and seems overly inefficient if one bothers with coding it in C. =) _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon added the comment: Implementing warn_explicit() is going to be troublesome with the new module_globals argument. It requires not only to go through the hassle of looking for a loader and calling get_source(), but it will most likely require reworking the traceback function introduced to print out the second line of output (it doesn't look like it directly supports loaders, but I have not tried it personally). _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon added the comment: This version of test_warnings has tests for _warnings.c. It currently is failing as the second line of output for warnings has not been implemented yet for _warnings.c. But all the specified tests in my last comment have now been implemented. Added file: http://bugs.python.org/file8772/test_warnings.py _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ test_warnings.py Description: Binary data ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon: -- assignee: nnorwitz - brett.cannon _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon added the comment: Regression test suite now passes. =) Had to add the support for when warnings.showwarning() is set and a bug in PyErr_WarnExplicit() where a NULL value for the module name was not being allowed. Couple things still left to implement. One is the second output line in show_warning(). Don't notice this in unit tests as it imports warnings and thus uses the Python implementation that works properly. Also need to implement warn_explicit(). Lastly, -W arguments need to be handled. In terms of differing semantics, file paths are different. Old way did absolute paths. It also handled what the file path should be when executing a warning from the interpreter differently. In terms of testing, some _warnings-specific tests are needed. As mentioned above, the differences between _warnings.show_warning() and warnings.showwarning() are not being picked up. This shows how tests for setting of 'filters', 'onceregistry', and 'showwarning' needs to be tested. Assigning to Neal to see if there is anything I missed in terms of todos and see if he wants to take any of them on. =) -- assignee: brett.cannon - nnorwitz Added file: http://bugs.python.org/file8565/_warnings.c _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _#include Python.h #include frameobject.h #define MODULE_NAME _warnings #define DEFAULT_ACTION_NAME default_action PyDoc_STRVAR(warnings__doc__, MODULE_NAME provides basic warning filtering support.\n It is a helper module to speed up interpreter start-up.); /* Both 'filters' and 'onceregistry' can be set in warnings.py; get_warnings_attr() will reset these variables accordingly. */ static PyObject *_filters; /* List */ static PyObject *_once_registry; /* Dict */ static int check_matched(PyObject *obj, PyObject *arg) { PyObject *result; int rc; if (obj == Py_None) return 1; result = PyObject_CallMethod(obj, match, O, arg); if (result == NULL) return -1; rc = PyObject_IsTrue(result); Py_DECREF(result); return rc; } /* Returns a new reference. A NULL return value can mean false or an error. */ static PyObject * get_warnings_attr(const char *attr) { static PyObject *warnings_str = NULL; PyObject *all_modules; PyObject *warnings_module; int result; if (warnings_str == NULL) { warnings_str = PyString_FromString(warnings); if (warnings_str == NULL) return NULL; } all_modules = PyImport_GetModuleDict(); result = PyDict_Contains(all_modules, warnings_str); if (result == -1 || result == 0) return NULL; warnings_module = PyDict_GetItem(all_modules, warnings_str); if (!PyObject_HasAttrString(warnings_module, attr)) return NULL; return PyObject_GetAttrString(warnings_module, attr); } PyObject * get_once_registry(void) { PyObject *registry; registry = get_warnings_attr(onceregistry); if (registry == NULL) { if (PyErr_Occurred()) return NULL; return _once_registry; } Py_DECREF(_once_registry); _once_registry = registry; return registry; } /* The item is a borrowed reference. */ static const char * get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno, PyObject *module, PyObject **item) { PyObject *action, *m, *d; Py_ssize_t i; PyObject *warnings_filters; warnings_filters = get_warnings_attr(filters); if (warnings_filters == NULL) { if (PyErr_Occurred()) return NULL; } else { Py_DECREF(_filters); _filters = warnings_filters; } if (!PyList_Check(_filters)) { PyErr_SetString(PyExc_ValueError, MODULE_NAME .filters must be a list); return NULL; } /* _filters could change while we are iterating over it. */ for (i = 0; i PyList_GET_SIZE(_filters); i++) { PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj; Py_ssize_t ln; int is_subclass, good_msg, good_mod; tmp_item = *item = PyList_GET_ITEM(_filters, i); if (PyTuple_Size(tmp_item) != 5) { PyErr_Format(PyExc_ValueError, MODULE_NAME .filters item %zd isn't a 5-tuple, i); return NULL; } /* Python code: action, msg, cat, mod, ln = item */ action = PyTuple_GET_ITEM(tmp_item, 0); msg = PyTuple_GET_ITEM(tmp_item, 1); cat = PyTuple_GET_ITEM(tmp_item, 2); mod = PyTuple_GET_ITEM(tmp_item, 3); ln_obj = PyTuple_GET_ITEM(tmp_item, 4); good_msg = check_matched(msg, text); good_mod = check_matched(mod, module); is_subclass = PyObject_IsSubclass(category, cat); ln = PyInt_AsSsize_t(ln_obj); if (good_msg == -1 || good_mod == -1 || is_subclass == -1 || (ln == -1 PyErr_Occurred()))
[issue1631171] implement warnings module in C
Neal Norwitz added the comment: I think this is good enough for now. The approach will probably stand even if the details change. Go for it! _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Neal Norwitz: -- assignee: nnorwitz - brett.cannon _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon added the comment: Attached is a new version of _warnings.c that checks to see if 'warnings' has been imported, and if so, uses the attributes from that module for onceregistry and 'filters'. I did it in such a way so that 'warnings' is in no way required nor imported through checking. If Neal does a code review and OKs the approach then it can also be implemented for showwarning() which should make testing test_warnings possible. =) Then we can start testing that changes to the module attributes actually affect things properly. -- assignee: brett.cannon - nnorwitz _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _#include Python.h #include frameobject.h #define MODULE_NAME _warnings #define DEFAULT_ACTION_NAME default_action PyDoc_STRVAR(warnings__doc__, MODULE_NAME provides basic warning filtering support.\n It is a helper module to speed up interpreter start-up.); /* Both 'filters' and 'onceregistry' can be set in warnings.py; get_warnings_attr() will reset these variables accordingly. */ static PyObject *_filters; /* List */ static PyObject *_once_registry; /* Dict */ static int check_matched(PyObject *obj, PyObject *arg) { PyObject *result; int rc; if (obj == Py_None) return 1; result = PyObject_CallMethod(obj, match, O, arg); if (result == NULL) return -1; rc = PyObject_IsTrue(result); Py_DECREF(result); return rc; } /* Returns a new reference. A NULL return value can mean false or an error. */ static PyObject * get_warnings_attr(const char *attr) { static PyObject *warnings_str = NULL; PyObject *all_modules; PyObject *warnings_module; int result; if (warnings_str == NULL) { warnings_str = PyString_FromString(warnings); if (warnings_str == NULL) return NULL; } all_modules = PyImport_GetModuleDict(); result = PyDict_Contains(all_modules, warnings_str); if (result == -1 || result == 0) return NULL; warnings_module = PyDict_GetItem(all_modules, warnings_str); if (!PyObject_HasAttrString(warnings_module, attr)) return NULL; return PyObject_GetAttrString(warnings_module, attr); } PyObject * get_once_registry(void) { PyObject *registry; /* XXX(brett.cannon): Test detection of warnings.py change. */ registry = get_warnings_attr(onceregistry); if (registry == NULL) { if (PyErr_Occurred()) return NULL; return _once_registry; } Py_DECREF(_once_registry); _once_registry = registry; return registry; } /* The item is a borrowed reference. */ static const char * get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno, PyObject *module, PyObject **item) { PyObject *action, *m, *d; Py_ssize_t i; PyObject *warnings_filters; /* XXX(brett.cannon): Test warning.py changes. */ warnings_filters = get_warnings_attr(filters); if (warnings_filters == NULL) { if (PyErr_Occurred()) return NULL; } else { Py_DECREF(_filters); _filters = warnings_filters; } if (!PyList_Check(_filters)) { PyErr_SetString(PyExc_ValueError, MODULE_NAME .filters must be a list); return NULL; } /* _filters could change while we are iterating over it. */ for (i = 0; i PyList_GET_SIZE(_filters); i++) { PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj; Py_ssize_t ln; int is_subclass, good_msg, good_mod; tmp_item = *item = PyList_GET_ITEM(_filters, i); if (PyTuple_Size(tmp_item) != 5) { PyErr_Format(PyExc_ValueError, MODULE_NAME .filters item %zd isn't a 5-tuple, i); return NULL; } /* Python code: action, msg, cat, mod, ln = item */ action = PyTuple_GET_ITEM(tmp_item, 0); msg = PyTuple_GET_ITEM(tmp_item, 1); cat = PyTuple_GET_ITEM(tmp_item, 2); mod = PyTuple_GET_ITEM(tmp_item, 3); ln_obj = PyTuple_GET_ITEM(tmp_item, 4); good_msg = check_matched(msg, text); good_mod = check_matched(mod, module); is_subclass = PyObject_IsSubclass(category, cat); ln = PyInt_AsSsize_t(ln_obj); if (good_msg == -1 || good_mod == -1 || is_subclass == -1 || (ln == -1 PyErr_Occurred())) return NULL; if (good_msg is_subclass good_mod (ln == 0 || lineno == ln)) return PyString_AsString(action); } m = PyImport_ImportModule(MODULE_NAME); if (m == NULL) return NULL; d = PyModule_GetDict(m); Py_DECREF(m); if (d == NULL) return NULL; action = PyDict_GetItemString(d, DEFAULT_ACTION_NAME); if (action != NULL) return PyString_AsString(action); PyErr_SetString(PyExc_ValueError,
[issue1631171] implement warnings module in C
Brett Cannon added the comment: So the descriptor idea didn't work. Another idea is to have the C code that relies on attributes on warnings that are allowed to change have an initial check for warnings, and if that fails to fall back on C code. That way the module can still be completely self-sufficient while still allowing users to change values from Python code. For instance, take warnings.filters. Initially it can be set to _warnings.filters. But in the C code, where access to the filters list is needed, a check is first done to see if the warnings module has been imported. If so, check for a filters attribute. If it exists, replace the internal list with that one and continue on. If the module is not there, however, use the list stored statically in the module without change. Same idea of the once registry and a similar idea for showwarnings(). _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon added the comment: I just tried Neal's version of _warnings.c and it has the same failures, so they are not my fault. =) _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Brett Cannon added the comment: I have uploaded my touched up version of _warnings.c . I also have a new diff since the old one did not apply cleanly (test_warnings didn't get patched nor did PCbuild8). It also lacked the changes needed to Modules/config.c . It is still not complete, though. Some things are missing from _warnings.c and test_warnings is completely failing. But I figured I should get something uploaded so that other people can help if they want. _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _#include Python.h #include frameobject.h #define MODULE_NAME _warnings #define DEFAULT_ACTION_NAME default_action PyDoc_STRVAR(warnings__doc__, MODULE_NAME provides basic warning filtering support.\n It is a helper module to speed up interpreter start-up.); /* Filters list, also used in warnings.py. */ static PyObject *_filters; /* List */ static PyObject *_once_registry; /* Dict */ static int check_matched(PyObject *obj, PyObject *arg) { PyObject *result; int rc; if (obj == Py_None) return 1; result = PyObject_CallMethod(obj, match, O, arg); if (result == NULL) return -1; rc = PyObject_IsTrue(result); Py_DECREF(result); return rc; } /* The item is a borrowed reference. */ static const char * get_filter(PyObject *category, PyObject *text, Py_ssize_t lineno, PyObject *module, PyObject **item) { PyObject *action, *m, *d; Py_ssize_t i; if (!PyList_Check(_filters)) { PyErr_SetString(PyExc_ValueError, MODULE_NAME .filters must be a list); return NULL; } /* _filters could change while we are iterating over it. */ for (i = 0; i PyList_GET_SIZE(_filters); i++) { PyObject *tmp_item, *action, *msg, *cat, *mod, *ln_obj; Py_ssize_t ln; int is_subclass, good_msg, good_mod; tmp_item = *item = PyList_GET_ITEM(_filters, i); if (PyTuple_Size(tmp_item) != 5) { PyErr_Format(PyExc_ValueError, MODULE_NAME .filters item %zd isn't a 5-tuple, i); return NULL; } /* Python code: action, msg, cat, mod, ln = item */ action = PyTuple_GET_ITEM(tmp_item, 0); msg = PyTuple_GET_ITEM(tmp_item, 1); cat = PyTuple_GET_ITEM(tmp_item, 2); mod = PyTuple_GET_ITEM(tmp_item, 3); ln_obj = PyTuple_GET_ITEM(tmp_item, 4); good_msg = check_matched(msg, text); good_mod = check_matched(mod, module); is_subclass = PyObject_IsSubclass(category, cat); ln = PyInt_AsSsize_t(ln_obj); if (good_msg == -1 || good_mod == -1 || is_subclass == -1 || (ln == -1 PyErr_Occurred())) return NULL; if (good_msg is_subclass good_mod (ln == 0 || lineno == ln)) return PyString_AsString(action); } m = PyImport_ImportModule(MODULE_NAME); if (m == NULL) return NULL; d = PyModule_GetDict(m); Py_DECREF(m); if (d == NULL) return NULL; action = PyDict_GetItemString(d, DEFAULT_ACTION_NAME); if (action != NULL) return PyString_AsString(action); PyErr_SetString(PyExc_ValueError, MODULE_NAME . DEFAULT_ACTION_NAME not found); return NULL; } static int already_warned(PyObject *registry, PyObject *key, int should_set) { PyObject *already_warned; if (key == NULL) return -1; already_warned = PyDict_GetItem(registry, key); if (already_warned != NULL) { int rc = PyObject_IsTrue(already_warned); if (rc != 0) return rc; } /* This warning wasn't found in the registry, set it. */ if (should_set) return PyDict_SetItem(registry, key, Py_True); return 0; } /* New reference. */ static PyObject * normalize_module(PyObject *filename) { PyObject *module; const char *mod_str; Py_ssize_t len; int rc = PyObject_IsTrue(filename); if (rc == -1) return NULL; else if (rc == 0) return PyString_FromString(unknown); mod_str = PyString_AsString(filename); if (mod_str == NULL) return NULL; len = PyString_Size(filename); if (len 0) return NULL; if (len = 3 strncmp(mod_str + (len - 3), .py, 3) == 0) { module = PyString_FromStringAndSize(mod_str, len-3); } else { module = filename; Py_INCREF(module); } return module; } static int update_registry(PyObject *registry, PyObject *text, PyObject *category, int add_zero) { PyObject *altkey, *zero = NULL; int rc; if (add_zero) { zero = PyInt_FromLong(0); if (zero == NULL) return -1; altkey = PyTuple_Pack(3, text, category, zero); } else altkey = PyTuple_Pack(2, text, category); rc = already_warned(registry, altkey, 1);
[issue1631171] implement warnings module in C
Changes by Brett Cannon: _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue1631171] implement warnings module in C
Changes by Brett Cannon: -- assignee: - brett.cannon nosy: +brett.cannon _ Tracker [EMAIL PROTECTED] http://bugs.python.org/issue1631171 _ ___ Python-bugs-list mailing list Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com