[Pyrex] cimport

William Stein was at math.harvard.edu
Mon Mar 7 05:32:19 CET 2005


Greg Ewing said:
> Why don't you want to use the full name? Keep in
> mind that you can do things like
>
>    cimport sage.rings.rational as rational
>
> to give yourself a shorter way of referring to
> it in the .pyx file.

I'm very happy using the full name.  The problem is that using full names seem 
to fail in multiple ways.  

> Can you provide an example? As long as you haven't
> lied in any way about the real name of the module,
> Pyrex should keep their definitions in different
> namespaces.

I've put a simple and self-contained example here (it's a 2KB file):

  http://modular.fas.harvard.edu/talks/pyrex/cimport.tgz

The README.txt that I put in cimport.tgz is included below.  Thanks for any 
help, clarification, or suggestions. 

Hello,

This is supposed to illustrate a possible bug in Pyrex Version 0.9.3.
Code involving two dotted cimport's causes pyrexc to print an error
message and return an error code:

  cimport ring.integer
  cimport ring.rational

Actually pyrexc does evidently create the c files OK, so it's not a
big problem.  I have another script that checked the output code from
pyrexc, and if it was error, it terminated with failure.  Now I'm not
sure how to tell whether or not Pyrex successfully compiled a pyx
file, since it seems to succeed, but outputs that it didn't...

To see the error, do the following:

1. Type "make" to build the extension.

2. Observe the line

   /home/was/sage/learn/pyrex/cimport/ex.pyx:7:8: 'ring' redeclared

in the output of make near the end.  This is caused by the line

   pyrexc -I`pwd`/ring ex.pyx

in the setup.py script.   You could also just enter this line directly
to see the error.  This prints an error, *does* create ex.c (evidently
correctly), and returns an error condition.


NOTE: In order to use dotted notation in cimport, I had to create
symlinks.  Thus in the subdirectory ring there are two symlinks:

       ring.integer.pxd -> integer.pxd
       ring.rational.pxd -> rational.pxd

Without these pyrexc just gives the error

       'ring.integer.pxd' not found

and here it really does fail.

---

My current fix is to comment out lines 568-571 of
  Pyrex/Compiler/Symtab.py, i.e., the "if entry:" part of the
  declare_module function (and put a pass there).

---

In any case, the big question for me is what is the right way to use
cimport to include something in a subdirectory?  Using symlinks in a
hackish way seems, well, hackish.  But nothing else seems to work for
me.

Regards,

William

-- 
William Stein
Benjamin Peirce Assistant Professor of Mathematics
Harvard University
http://modular.fas.harvard.edu
617-308-0144 (mobile phone)



More information about the Pyrex mailing list