[Python-Dev] Baffled by PyArg_ParseTupleAndKeywords modification

"Martin v. Löwis" martin at v.loewis.de
Sun Feb 12 00:45:59 CET 2006


Armin Rigo wrote:
> Alas, this doesn't make gcc happy either.  (I'm trying gcc 3.4.4.)  In
> theory, it prevents the const-bypassing trick showed by Martin, but
> apparently the C standard (or gcc) is not smart enough to realize that.

It appears to be language-defined. Looking at the assignment

  char **a;
  const char* const* b;
  b = a;

then, in C++, 4.4p4 [conv.qual] has a rather longish formula to
decide that the assignment is well-formed. In essence, it goes
like this:
- the pointers are "similar": they have the same levels of indirection,
  and the same underlying type.
- In all places where the type of a has const/volatile qualification,
  the type of b also has these qualifications (i.e. none in the
  example)
- Starting from the first point where the qualifications differ
  (from left to right), all later levels also have const.

I'm unsure about C; I think the rule comes from 6.3.2.3p2:

       [#2]  For  any  qualifier  q, a pointer to a non-q-qualified
       type may be  converted  to  a  pointer  to  the  q-qualified
       version  of  the type; the values stored in the original and
       converted pointers shall compare equal.

So it is possible to convert a non-const pointer to a const pointer,
but only if the the target types are the same. In the example, they
are not: the target type of a is char*, the target of b is
const char*.

Regards,
Martin


More information about the Python-Dev mailing list