except clause syntax question

Ian Kelly ian.g.kelly at gmail.com
Wed Feb 1 02:12:44 CET 2012


On Tue, Jan 31, 2012 at 6:09 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
> On Tue, Jan 31, 2012 at 5:53 PM, Chris Angelico <rosuav at gmail.com> wrote:
>> On Wed, Feb 1, 2012 at 9:03 AM, Duncan Booth
>> <duncan.booth at invalid.invalid> wrote:
>>> Abitrarily nested tuples of exceptions cannot contain loops so the code
>>> simply needs to walk through the tuples until it finds a match.
>>
>> Is this absolutely guaranteed? The C API for CPython provides:
>> (Py2) http://docs.python.org/c-api/tuple.html#PyTuple_SetItem
>> (Py3) http://docs.python.org/dev/c-api/tuple.html#PyTuple_SetItem
>>
>> which doesn't have massive warnings on it saying "USE THIS ONLY TO
>> INITIALIZE A TUPLE" (compare, for instance, _PyTuple_Resize which does
>> carry a similar warning). Is the assumption then that we're all
>> adults, and that mutating a tuple is like passing a null pointer to an
>> API function (aka "loaded gun in proximity to foot")?
>
> I don't know why the docs are written the way that they are, but if
> you check the code, you can see that PyTuple_SetItem will raise a
> SystemError if the reference count is anything other than 1.  So I
> think that it is only meant to be used with similar caution and
> restraint.

Incidentally, I *think* that any correctly written C code attempting
to nest a tuple inside itself would make the reference count of the
tuple be at least 2 at the time of the call, and so it would fail.



More information about the Python-list mailing list