On Oct 23, 2008, at 10:30 PM, Aaron DeVore wrote:
> On Thu, Oct 23, 2008 at 3:08 AM, Stefan Behnel
> <[EMAIL PROTECTED]> wrote:
>
>> It can't use PySet_* anyway, as they are not available in Py2.3, and
>> Cython code currently runs with 2.3. Although I wouldn't mind
>> restricting
>> the C code to Py2.4 if "set" is not redefined in the source (maybe
>> that's
>> already the case, haven't checked).
>
> I just took a peek at the source in cython-dev. I don't really
> understand what's happening in the file but Builtin.py seems to have
> some support for the set type.
>
>>> As a side note, how about making an adjustment to Cython that
>>> automatically handles the set type/sets module trick?
>>
>> Yes, it would be nice to have that. The idea would be to do the
>> Set import
>> internally, to provide replacement implementations for the generated
>> PySet_*() calls that work at the Python level, and to enable that
>> machinery when the C compile-time Python version is Py2.3.
>> However, that's
>> work that someone has to do.
>>
>> OTOH, this is a pure legacy feature for code that must run with
>> Py2.3. I
>> guess people (including myself) can currently live with the little
>> performance impact regarding the set operations (which are fast
>> anyway),
>> given the gain that their code runs unchanged in Py2.3.
>
> I wish I had the time and expertise for this. :(
>
> Would this work if put at the top of every file (dashes indicate
> indentation)?
> if not hasattr(__builtin__, "set"):
> ----from sets import Set as set
>
> or this
> try:
> ----set
> except NameError:
> ----from sets import Set as set
>
> It seems like there could be an issue with static typing using the
> imported Python class sets.Set (e.g. `cdef set foo`). I don't know
> enough about Cython to make a good guess on that one.
I think one would have to do
try:
from __builtin__ import set
except ImportError:
from sets import Set as set
One couldn't, however, do "cdef set foo" because the symbol set would
need to be resolved at compile time, whereas the code (as above)
defers its value to runtime. Consequently the code using the
PySet_Xxxx api directly wouldn't get generated (which of course only
works for 2.4+) Hackery could be done in the compiler to emulate the
builtin set API (via calling the Set module's class), but it should
be noted that the speed gains we're talking about here for the most
part are not that large (compared to, say, indexing into a list or
tuple with an int where there are huge savings).
- Robert
_______________________________________________
Cython-dev mailing list
[email protected]
http://codespeak.net/mailman/listinfo/cython-dev