Craig Citro wrote: > I have a question about the semantics of mixing imports and cimports. > I started drafting a long message with lots of details and examples, > but I realized I should just start with a few simple questions: if you > do > > from Foo import Bar > from Foo cimport Bar > > in a .pyx file, is it safe to assume that the Bar we import at runtime > is going to be coming from the same module whose .pxd we analyzed at > compile-time? Is it at least safe to assume that the Bar we import at > runtime will be the same *type* as the Bar we saw at compile-time? > (The next obvious question: what other information can we assume is > the same?) > I can't help you, but: If not much is assumed at the moment, my vote is to be able to start assuming stuff (and that the backwards compatability breakage is a sacrifice one can make).
Basically, if a pxd and a Python module has the same name, assume that the pxd file is written with detailed knowledge of what goes on in the Python module, and exists to provide hints to Cython about faster execution. > Of course, this sounds reasonable enough, but completely goes against > the usual Python import semantics, since there are so many ways to > change what will actually get imported at runtime. > But one can still do import tricks etc., as long as the pxd file written is written with knowledge about it. > In general, it would be nice (specifically as our type inferencer > becomes more sophisticated) to be able to provide Cython with as much > information as possible about the module it's going to see at runtime > -- not just what's in the .pxd. In fact, if you know at compile-time > which module is going to be there at runtime, it would be helpful to > be able to tell Cython "here's the module, feel free to analyze it > however you'd like." This makes sense even for import statements > without a corresponding cimport. Would people be opposed to a > "@cython.staticimport" decorator or the like? > Hmm. In my experience the return value of functions is much more important though, and how can one possibly get those through introspection? So I'm unsure about the gain... This is more in a grey zone, because there's a long tradition of making standalone C files which can be compiled on any system independent of the exact installed version of the Python library (at least, with a pxd file, there's a human to make conscious decisions about everything). But I'm usually not opposed to directives that are off by default... Dag Sverre _______________________________________________ Cython-dev mailing list [email protected] http://codespeak.net/mailman/listinfo/cython-dev
