Could I use a compiler directive to import sets if possible, then use another compiler directive to pick the type when using cdef? Example:
IF PY_VERSION_HEX < 0x02040000: ----DEF USE_SETS_MODULE 1 ----from sets import Set as set Later in the file... cdef class Foo: ----IF USE_SETS_MODULE: --------cdef object member_set ----ELSE: --------cdef set member_set I haven't figured out how to import compiler directive constants from Python.h, hence the lack of me just trying it out on my own. -Aaron >>> 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 > _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
