r50969 - in python/trunk: Include/pyerrors.h Misc/NEWS Modules/_struct.c Python/errors.c

Author: neal.norwitz Date: Sun Jul 30 08:55:48 2006 New Revision: 50969 Modified: python/trunk/Include/pyerrors.h python/trunk/Misc/NEWS python/trunk/Modules/_struct.c python/trunk/Python/errors.c Log: Add PyErr_WarnEx() so C code can pass the stacklevel to warnings.warn(). This provides the proper warning for struct.pack(). PyErr_Warn() is now deprecated in favor of PyErr_WarnEx(). As mentioned by Tim Peters on python-dev. Modified: python/trunk/Include/pyerrors.h ============================================================================== --- python/trunk/Include/pyerrors.h (original) +++ python/trunk/Include/pyerrors.h Sun Jul 30 08:55:48 2006 @@ -225,10 +225,14 @@ PyAPI_FUNC(void) PyErr_WriteUnraisable(PyObject *); /* Issue a warning or exception */ -PyAPI_FUNC(int) PyErr_Warn(PyObject *, char *); +PyAPI_FUNC(int) PyErr_WarnEx(PyObject *category, const char *msg, + Py_ssize_t stack_level); PyAPI_FUNC(int) PyErr_WarnExplicit(PyObject *, const char *, const char *, int, const char *, PyObject *); +/* PyErr_Warn is only for backwards compatability and will be removed. + Use PyErr_WarnEx instead. */ +#define PyErr_Warn(category, msg) PyErr_WarnEx(category, msg, 1) /* In sigcheck.c or signalmodule.c */ PyAPI_FUNC(int) PyErr_CheckSignals(void); Modified: python/trunk/Misc/NEWS ============================================================================== --- python/trunk/Misc/NEWS (original) +++ python/trunk/Misc/NEWS Sun Jul 30 08:55:48 2006 @@ -12,6 +12,10 @@ Core and builtins ----------------- +- Add PyErr_WarnEx() so C code can pass the stacklevel to warnings.warn(). + This provides the proper warning for struct.pack(). + PyErr_Warn() is now deprecated in favor of PyErr_WarnEx(). + - Patch #1531113: Fix augmented assignment with yield expressions. Also fix a SystemError when trying to assign to yield expressions. Modified: python/trunk/Modules/_struct.c ============================================================================== --- python/trunk/Modules/_struct.c (original) +++ python/trunk/Modules/_struct.c Sun Jul 30 08:55:48 2006 @@ -214,6 +214,8 @@ /* Helper routine to get a Python integer and raise the appropriate error if it isn't one */ +#define INT_OVERFLOW "struct integer overflow masking is deprecated" + static int get_wrapped_long(PyObject *v, long *p) { @@ -223,7 +225,7 @@ PyObject *wrapped; long x; PyErr_Clear(); - if (PyErr_Warn(PyExc_DeprecationWarning, "struct integer overflow masking is deprecated") < 0) + if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0) return -1; wrapped = PyNumber_And(v, pylong_ulong_mask); if (wrapped == NULL) @@ -250,7 +252,7 @@ wrapped = PyNumber_And(v, pylong_ulong_mask); if (wrapped == NULL) return -1; - if (PyErr_Warn(PyExc_DeprecationWarning, "struct integer overflow masking is deprecated") < 0) { + if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0) { Py_DECREF(wrapped); return -1; } @@ -345,8 +347,8 @@ Py_XDECREF(ptraceback); if (msg == NULL) return -1; - rval = PyErr_Warn(PyExc_DeprecationWarning, - PyString_AS_STRING(msg)); + rval = PyErr_WarnEx(PyExc_DeprecationWarning, + PyString_AS_STRING(msg), 2); Py_DECREF(msg); if (rval == 0) return 0; Modified: python/trunk/Python/errors.c ============================================================================== --- python/trunk/Python/errors.c (original) +++ python/trunk/Python/errors.c Sun Jul 30 08:55:48 2006 @@ -632,7 +632,7 @@ /* Function to issue a warning message; may raise an exception. */ int -PyErr_Warn(PyObject *category, char *message) +PyErr_WarnEx(PyObject *category, const char *message, Py_ssize_t stack_level) { PyObject *dict, *func = NULL; PyObject *warnings_module = PyModule_GetWarningsModule(); @@ -650,7 +650,8 @@ if (category == NULL) category = PyExc_RuntimeWarning; - res = PyObject_CallFunction(func, "sO", message, category); + res = PyObject_CallFunction(func, "sOn", + message, category, stack_level); if (res == NULL) return -1; Py_DECREF(res); @@ -658,6 +659,16 @@ } } +/* PyErr_Warn is only for backwards compatability and will be removed. + Use PyErr_WarnEx instead. */ + +#undef PyErr_Warn + +int +PyErr_Warn(PyObject *category, char *message) +{ + return PyErr_WarnEx(category, message, 1); +} /* Warning with explicit origin */ int

neal.norwitz schrieb:
Author: neal.norwitz Date: Sun Jul 30 08:55:48 2006 New Revision: 50969
Modified: python/trunk/Include/pyerrors.h python/trunk/Misc/NEWS python/trunk/Modules/_struct.c python/trunk/Python/errors.c Log: Add PyErr_WarnEx() so C code can pass the stacklevel to warnings.warn(). This provides the proper warning for struct.pack(). PyErr_Warn() is now deprecated in favor of PyErr_WarnEx(). As mentioned by Tim Peters on python-dev.
Modified: python/trunk/Include/pyerrors.h ============================================================================== --- python/trunk/Include/pyerrors.h (original) +++ python/trunk/Include/pyerrors.h Sun Jul 30 08:55:48 2006 @@ -225,10 +225,14 @@ PyAPI_FUNC(void) PyErr_WriteUnraisable(PyObject *);
/* Issue a warning or exception */ -PyAPI_FUNC(int) PyErr_Warn(PyObject *, char *); +PyAPI_FUNC(int) PyErr_WarnEx(PyObject *category, const char *msg, + Py_ssize_t stack_level); PyAPI_FUNC(int) PyErr_WarnExplicit(PyObject *, const char *, const char *, int, const char *, PyObject *); +/* PyErr_Warn is only for backwards compatability and will be removed. + Use PyErr_WarnEx instead. */ +#define PyErr_Warn(category, msg) PyErr_WarnEx(category, msg, 1)
This change removed (on Windows) the PyErr_Warn *exported* function from the python25.dll. As a result, extensions compiled with earlier beta versions of python 2.5 cannot be loaded any more; for example the win32com extensions from Mark Hammond - but of course any other extensions that use this function. Btw: Shouldn't the PYTHON_API_VERSION (or how it's called) have also been changed? I'm unsure what to do: - Implement PyErr_Warn as a function again (this was also done with other functions, see also #1465834). Would this require another beta ;-)? - Leave it as it is, and require that extensions needs to be rebuilt with 2.5b3 (plus change the PYTHON_API_VERSION, ...) Anyway, it seems to me that *some* policy regarding changes to exported functions should be established. Thomas

Thomas Heller wrote:
neal.norwitz schrieb:
Author: neal.norwitz Date: Sun Jul 30 08:55:48 2006 New Revision: 50969
Modified: python/trunk/Include/pyerrors.h python/trunk/Misc/NEWS python/trunk/Modules/_struct.c python/trunk/Python/errors.c Log: Add PyErr_WarnEx() so C code can pass the stacklevel to warnings.warn(). This provides the proper warning for struct.pack(). PyErr_Warn() is now deprecated in favor of PyErr_WarnEx(). As mentioned by Tim Peters on python-dev.
Modified: python/trunk/Include/pyerrors.h ============================================================================== --- python/trunk/Include/pyerrors.h (original) +++ python/trunk/Include/pyerrors.h Sun Jul 30 08:55:48 2006 @@ -225,10 +225,14 @@ PyAPI_FUNC(void) PyErr_WriteUnraisable(PyObject *);
/* Issue a warning or exception */ -PyAPI_FUNC(int) PyErr_Warn(PyObject *, char *); +PyAPI_FUNC(int) PyErr_WarnEx(PyObject *category, const char *msg, + Py_ssize_t stack_level); PyAPI_FUNC(int) PyErr_WarnExplicit(PyObject *, const char *, const char *, int, const char *, PyObject *); +/* PyErr_Warn is only for backwards compatability and will be removed. + Use PyErr_WarnEx instead. */ +#define PyErr_Warn(category, msg) PyErr_WarnEx(category, msg, 1)
This change removed (on Windows) the PyErr_Warn *exported* function from the python25.dll. As a result, extensions compiled with earlier beta versions of python 2.5 cannot be loaded any more; for example the win32com extensions from Mark Hammond - but of course any other extensions that use this function.
Btw: Shouldn't the PYTHON_API_VERSION (or how it's called) have also been changed?
I'm unsure what to do: - Implement PyErr_Warn as a function again (this was also done with other functions, see also #1465834). Would this require another beta ;-)? - Leave it as it is, and require that extensions needs to be rebuilt with 2.5b3 (plus change the PYTHON_API_VERSION, ...)
Anyway, it seems to me that *some* policy regarding changes to exported functions should be established.
In the past we've always kept a stub function around that was exported when converting a function to a macro. I guess this ought to happen in this case as well. -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Aug 04 2006)
Python/Zope Consulting and Support ... http://www.egenix.com/ mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/ mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::

On 8/4/06, M.-A. Lemburg <mal@egenix.com> wrote:
This change removed (on Windows) the PyErr_Warn *exported* function from the python25.dll. As a result, extensions compiled with earlier beta versions of python 2.5 cannot be loaded any more; for example the win32com extensions from Mark Hammond - but of course any other extensions that use this function.
Btw: Shouldn't the PYTHON_API_VERSION (or how it's called) have also been changed?
I'm unsure what to do: - Implement PyErr_Warn as a function again (this was also done with other functions, see also #1465834). Would this require another beta ;-)? - Leave it as it is, and require that extensions needs to be rebuilt with 2.5b3 (plus change the PYTHON_API_VERSION, ...)
Anyway, it seems to me that *some* policy regarding changes to exported functions should be established.
In the past we've always kept a stub function around that was exported when converting a function to a macro.
I guess this ought to happen in this case as well.
That was my intent. I thought I left it in the C file so it would be in a library, but any newly compiled code would use the new API. Thomas if you can work up a patch, that would be great. I will try to take a look at it later. n

Neal Norwitz schrieb:
On 8/4/06, M.-A. Lemburg <mal@egenix.com> wrote:
This change removed (on Windows) the PyErr_Warn *exported* function from the python25.dll. As a result, extensions compiled with earlier beta versions of python 2.5 cannot be loaded any more; for example the win32com extensions from Mark Hammond - but of course any other extensions that use this function.
Btw: Shouldn't the PYTHON_API_VERSION (or how it's called) have also been changed?
I'm unsure what to do: - Implement PyErr_Warn as a function again (this was also done with other functions, see also #1465834). Would this require another beta ;-)? - Leave it as it is, and require that extensions needs to be rebuilt with 2.5b3 (plus change the PYTHON_API_VERSION, ...)
Anyway, it seems to me that *some* policy regarding changes to exported functions should be established.
In the past we've always kept a stub function around that was exported when converting a function to a macro.
I guess this ought to happen in this case as well.
That was my intent. I thought I left it in the C file so it would be in a library, but any newly compiled code would use the new API.
Ah, I didn't look into Python/errors.h. The function must be marked PyAPI_FUNC to be exported from the dll.
Thomas if you can work up a patch, that would be great. I will try to take a look at it later.
Index: errors.c =================================================================== --- errors.c (Revision 51087) +++ errors.c (Arbeitskopie) @@ -664,7 +664,7 @@ #undef PyErr_Warn -int +PyAPI_FUNC(int) PyErr_Warn(PyObject *category, char *message) { return PyErr_WarnEx(category, message, 1); Just for fun: with ctypes, testing for this is trivial. First, Python 2.5b3: Python 2.5b3 (r25b3:51041, Aug 3 2006, 09:35:06) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
from ctypes import * pythonapi.PyErr_Warn Traceback (most recent call last): File "<stdin>", line 1, in <module> File "c:\svn\theller\ctypes\ctypes\__init__.py", line 328, in __getattr__ func = self.__getitem__(name) File "c:\svn\theller\ctypes\ctypes\__init__.py", line 333, in __getitem__ func = self._FuncPtr((name_or_ordinal, self)) AttributeError: function 'PyErr_Warn' not found ^Z
Then, Python SVN trunk with this patch: Python 2.5b3 (trunk:51087M, Aug 4 2006, 19:43:25) [MSC v.1310 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.
from ctypes import * pythonapi.PyErr_Warn <_FuncPtr object at 0x009E6E40> ^Z
Thomas

On 8/4/06, Thomas Heller <theller@python.net> wrote:
Ah, I didn't look into Python/errors.h. The function must be marked PyAPI_FUNC to be exported from the dll.
Thanks, I didn't know that!
Thomas if you can work up a patch, that would be great. I will try to take a look at it later.
Can you check that in and make an entry in Misc/NEWS? Thanks, n
participants (4)
-
M.-A. Lemburg
-
Neal Norwitz
-
neal.norwitz
-
Thomas Heller