[Python-ideas] Python 2's re module should take longs

Chris Kaynor ckaynor at zindagigames.com
Tue Sep 30 22:32:14 CEST 2014


On Tue, Sep 30, 2014 at 1:12 PM, <random832 at fastmail.us> wrote:

> Disregard my last message, I was looking at the wrong code.
>
> But looking at what I think is the right code
> (https://hg.python.org/cpython/file/d49b9c8ee8ed/Modules), I am
> confused, since this error is raised after the index has already been
> converted to a Py_ssize_t.


According to my quick look at the code[1], it looks like the problem is
in match_getindex (~line 3304). If the line "if (PyInt_Check(index))" read
"if (PyInt_Check(index) || PyLong_Check(index))" instead, it appears that
it would properly handle longs as well as ints (at least based on what is
happening a little father down, near line 3312). It may be possible that
the conditions need to be seperated so that the long case calls
PyLong_AsSsize_t rather than PyInt_AsSsize_t, but that may not be needed.

It appears that in case an index is passed in, the re module just converts
that to a C size_t, otherwise it looks it up in the group name dictionary
to get the index. I suspect the indexes don't exist as keys in the mapping,
only the group names. As the initial conversion checks for int
specifically, and ignores longs, longs are treated differently than ints.

As a side note, it appears the documentation at
https://docs.python.org/2/c-api/long.html is slightly incorrect: there
appear to be two instances of a few functions, with slightly different
documentation, but the same return, arguments, and name. The ones I can
seeare "PyLong_FromSsize_t" and "PyLong_AsSsize_t". Prehaps I am just
missing some subtle difference in the names or arguments?

[1] https://hg.python.org/cpython/file/d49b9c8ee8ed/Modules/_sre.c
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20140930/7ced9e5c/attachment-0001.html>


More information about the Python-ideas mailing list