Author: Armin Rigo <[email protected]>
Branch: py3.5
Changeset: r89943:386cfc419af7
Date: 2017-02-05 17:44 +0100
http://bitbucket.org/pypy/pypy/changeset/386cfc419af7/
Log: PyErr_WarnFormat
diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py
--- a/pypy/module/cpyext/api.py
+++ b/pypy/module/cpyext/api.py
@@ -571,6 +571,7 @@
'_Py_BuildValue_SizeT', '_Py_VaBuildValue_SizeT',
'PyErr_Format', 'PyErr_NewException', 'PyErr_NewExceptionWithDoc',
+ 'PyErr_WarnFormat',
'PySys_WriteStdout', 'PySys_WriteStderr',
'PyEval_CallFunction', 'PyEval_CallMethod', 'PyObject_CallFunction',
@@ -1321,6 +1322,7 @@
source_dir / "bytesobject.c",
source_dir / "complexobject.c",
source_dir / "import.c",
+ source_dir / "_warnings.c",
]
def build_eci(code, use_micronumpy=False, translating=False):
diff --git a/pypy/module/cpyext/include/warnings.h
b/pypy/module/cpyext/include/warnings.h
--- a/pypy/module/cpyext/include/warnings.h
+++ b/pypy/module/cpyext/include/warnings.h
@@ -6,6 +6,9 @@
#define PyErr_WarnPy3k(msg, stacklevel) 0
+PyAPI_FUNC(int) PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
+ const char *format, ...);
+
#ifdef __cplusplus
}
#endif
diff --git a/pypy/module/cpyext/src/_warnings.c
b/pypy/module/cpyext/src/_warnings.c
new file mode 100644
--- /dev/null
+++ b/pypy/module/cpyext/src/_warnings.c
@@ -0,0 +1,25 @@
+#include <Python.h>
+
+int
+PyErr_WarnFormat(PyObject *category, Py_ssize_t stack_level,
+ const char *format, ...)
+{
+ int ret;
+ PyObject *message;
+ va_list vargs;
+
+#ifdef HAVE_STDARG_PROTOTYPES
+ va_start(vargs, format);
+#else
+ va_start(vargs);
+#endif
+ message = PyUnicode_FromFormatV(format, vargs);
+ if (message != NULL) {
+ ret = PyErr_WarnEx(category, PyUnicode_AsUTF8(message), stack_level);
+ Py_DECREF(message);
+ }
+ else
+ ret = -1;
+ va_end(vargs);
+ return ret;
+}
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -405,14 +405,6 @@
(sys.getfilesystemencoding())."""
raise NotImplementedError
-@cpython_api([PyObject, Py_ssize_t, rffi.CCHARP, ], rffi.INT_real, error=-1)
-def PyErr_WarnFormat(space, category, stack_level, format, ):
- """Function similar to PyErr_WarnEx(), but use
- PyUnicode_FromFormat() to format the warning message. format is
- an ASCII-encoded string.
- """
- raise NotImplementedError
-
@cpython_api([rffi.INT_real], rffi.INT_real, error=-1)
def PySignal_SetWakeupFd(space, fd):
diff --git a/pypy/module/cpyext/test/test_pyerrors.py
b/pypy/module/cpyext/test/test_pyerrors.py
--- a/pypy/module/cpyext/test/test_pyerrors.py
+++ b/pypy/module/cpyext/test/test_pyerrors.py
@@ -428,3 +428,18 @@
assert orig_exc_info == reset_sys_exc_info
assert new_exc_info == (new_exc.__class__, new_exc, None)
assert new_exc_info == new_sys_exc_info
+
+ def test_PyErr_WarnFormat(self):
+ import warnings
+
+ module = self.import_extension('foo', [
+ ("test", "METH_NOARGS",
+ '''
+ PyErr_WarnFormat(PyExc_UserWarning, 1, "foo %d bar", 42);
+ Py_RETURN_NONE;
+ '''),
+ ])
+ with warnings.catch_warnings(record=True) as l:
+ module.test()
+ assert len(l) == 1
+ assert "foo 42 bar" in str(l[0])
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit