[Python-Dev] ANSIfication problems: warnings in marshal.c
Tim Peters
tim_one@email.msn.com
Sun, 23 Jul 2000 15:43:33 -0400
> w_byte is a macro that either calls C's putc, or w_more.
>
> putc takes an int, and all calls to w_byte make sure they're
> passing the right thing.
>
> however, w_more is defined to take a char, which results in
> warnings from MSVC5.
>
> quick fix: change w_more to take an integer too:
> ...
And I see you checked that in. Good! I later replaced part of it, via a
new mini-subsystem that may tick somebody off, so I'll explain that here so
people can yell at me now:
1. asserts are a fundamental tool of good C practice, but are very
rarely used in Python source. To encourage their use, I added an
include of <assert.h> to Python.h, so that assert is always available.
2. Added a new Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) macro to pyport.h,
which is also always available. This casts VALUE from type WIDE to
type NARROW, but assert-fails if info is lost due to the cast and
Py_DEBUG is #define'd (when we're not in Py_DEBUG mode, it just
expands to (NARROW)(VALUE)). Note: this kind of thing is much
easier to do in C++, because of inline functions and being able to
templatize those on the types involved (i.e., the compiler can figure
out which specific checker to expand at compile-time, rather than
making you redundantly-- and hence possibly incorrectly --name the
types "by hand" again).
3. It's nice that eyeballing every call to marshal's w_byte allows one
to infer that the derived calls to w_more are safe, but it's much
better to let the compiler check that. So I replaced the raw cast
with an invocation of Py_SAFE_DOWNCAST. Doesn't cost anything
except in Py_DEBUG mode.