Included assert.h in Python.h -- it's absurd that this basic tool of
authorTim Peters <tim.peters@gmail.com>
Sun, 23 Jul 2000 19:28:35 +0000 (19:28 +0000)
committerTim Peters <tim.peters@gmail.com>
Sun, 23 Jul 2000 19:28:35 +0000 (19:28 +0000)
good C practice hasn't been available to everything all along.
Added Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) macro to pyport.h; this
just casts VALUE from type WIDE to type NARROW, but assert-fails if
Py_DEBUG is defined and info is lost due to casting.
Replaced a line in Fredrik's fix to marshal.c to use the new macro.

Include/Python.h
Include/pyport.h
Python/marshal.c

index 3cea5914d3bee247fda60ef38d6e98d690e2736f..8ba466fe42cfb06d0fe20de64785c57f94be270a 100644 (file)
@@ -31,7 +31,6 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 
 #include "patchlevel.h"
 #include "config.h"
-#include "pyport.h"
 
 /* config.h may or may not define DL_IMPORT */
 #ifndef DL_IMPORT      /* declarations for DLL import/export */
@@ -51,6 +50,9 @@ redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 #ifdef HAVE_STDLIB_H
 #include <stdlib.h>
 #endif
+#include <assert.h>
+
+#include "pyport.h"
 
 #include "myproto.h"
 
index 4faba7c9f0eae81def76272e82550487c52c78d5..ed8a28eb7b628ae621b0fddeacf665ead6989038 100644 (file)
@@ -27,6 +27,10 @@ RETSIGTYPE
 Meaning:  Expands to void or int, depending on what the platform wants
           signal handlers to return.  Note that only void is ANSI!
 Used in:  Py_RETURN_FROM_SIGNAL_HANDLER
+
+Py_DEBUG
+Meaning:  Extra checks compiled in for debug mode.
+Used in:  Py_SAFE_DOWNCAST
 **************************************************************************/
 
 
@@ -74,6 +78,19 @@ extern "C" {
 #define Py_RETURN_FROM_SIGNAL_HANDLER(VALUE) \
         Py_FORCE_EXPANSION(RETSIGTYPE) ## _PySIGRETURN(VALUE)
 
+/* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW)
+ * Cast VALUE to type NARROW from type WIDE.  In Py_DEBUG mode, this
+ * assert-fails if any information is lost.
+ * Caution:
+ *    VALUE may be evaluated more than once.
+ */
+#ifdef Py_DEBUG
+#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
+       (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
+#else
+#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
+#endif
+
 #ifdef __cplusplus
 }
 #endif
index 74c547417a2837325c9bf5b1631ac2d4affe11b3..f6447131d0edf47dd1e09b8a4e7d3e2341fce9ae 100644 (file)
@@ -69,7 +69,7 @@ w_more(int c, WFILE *p)
                p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size;
                p->end =
                        PyString_AS_STRING((PyStringObject *)p->str) + newsize;
-               *p->ptr++ = (char) c;
+               *p->ptr++ = Py_SAFE_DOWNCAST(c, int, char);
        }
 }