Problem: new type in Python 2.2

John Machin sjmachin at lexicon.net
Wed Jan 16 16:43:49 EST 2002


[heavily snipped]
"Tim Peters" <tim.one at home.com> wrote in message news:<mailman.1011142047.1218.python-list at python.org>...
> [Dave Kuhlman]
> > TypeError: argument 2 must be string, not libxslt_stylesheet
> > This error message does not make too much sense to me.
> 
> Cheer up, it makes no sense at all <wink>.  This is usually a symptom of a
> function in an extension module setting an error condition but neglecting to
> return NULL (or whatever the proper error return is in context).  A common
> way for that to happen is for an extension to neglect to check the return of
> an API function *it* calls for errors (every call must be checked for an
> error return, and every error must be passed on or explictly cleared -- you
> can never skip these checks).  As a result, the main interpreter loop may
> miss the error at the time it occurs (the extension lied about whether an
> error did occur), and not notice until some later (and by-now irrelevant)
> opcode checks PyErr_Occurred() and picks up a stale error message.  In
> particular, the opcodes involved with for-loop setup and teardown make
> PyErr_Occurred() checks, as part of distinguishing a normal StopIteration
> loop exit from an unexpected exception.
> 
> > I can't find this error message in the Python 2.2 source.
> 
> Me neither -- and that's more evidence that an extension is involved.  The
> Python core is the least likely source (historical truth -- it's *almost*
> never to blame for these things, simply because the core code is so heavily
> exercised by so many people that such mistakes don't survive to see a final
> release).  It could be in your extension, or in any other extension you're
> using.

<timbait>
While the Python core is not to blame, the error message *is* produced
in the Python source, in getargs.c -- it's built up laboriously using
*snprintf(), so that it can't be detected by
bot_parser_hacked_up_using_regexes().
</timbait>

Suggested plausible scenario: an extension is calling
PyArg_ParseTuple() with an arg string that tells it to expect a string
("s") as the second argument. Two things are going wrong: (1) The
extensions's caller is passing in a libxslt_stylesheet (whatever that
is) instead of a string. (2) The extension is not testing the return
value from PyArg_ParseTuple(), and/or is not returning NULL (not a
pointer to PyNone, or some other valid PyObject!!) immediately on
failure. As Tim says, the exception is thrown much later, leading to
confusion about which source line caused the problem.

HTH,
John



More information about the Python-list mailing list