On Wed, May 6, 2009 at 8:51 AM,  <[email protected]> wrote:
>
> Hi Folks!
>
> Is it really impossible to use sage Cython classes outside the devel
> tree (say, in an spkg)?

No. It is possible.  For example, if you use %cython mode in the
notebook, everything happens outside of the devel tree in
%HOME/.sage/temp/.   And you can certainly cimport from sage classes
in %cython mode.    You might want to look at the setup.py that
%cython mode autogenerates.

Here's a little notebook session that uses cimport from the Sage
library to do evil mutation of a sage integer:


{{{id=12|
%cython
from sage.rings.integer cimport Integer
def double(Integer n):
    mpz_add(n.value, n.value, n.value)
///
}}}

{{{id=11|
n = 5
///
}}}

{{{id=13|
double(n)
///
}}}

{{{id=10|
n
///
10
}}}


Now, look at the variable SAGE_TMP (from the notebook).  For me it is
/Users/wstein/.sage/temp/teragon.local/65341/
cd to that directory, and go into the spyx subdirectory.
Look at the setup.py file there.   You can even build it:

 sage -python setup.py build

And you can try the extension module you get:

teragon:_Users_wstein__sage_sage_notebook_worksheets_admin_128_code_sage33_spyx
wstein$ sage
----------------------------------------------------------------------
| Sage Version 3.4.2.rc0, Release Date: 2009-04-30                   |
| Type notebook() for the GUI, and license() for information.        |
----------------------------------------------------------------------
sage: import 
_Users_wstein__sage_sage_notebook_worksheets_admin_128_code_sage33_spyx_0
sage: n = 5
sage: 
_Users_wstein__sage_sage_notebook_worksheets_admin_128_code_sage33_spyx_0.double(n)
sage: n
10

William


> I made the following tests:
>
> - The file TestImport.pyx contains a single line:
> from sage.structure.element cimport RingElement
>
> - If TestImport.pyx is contained in SAGE_ROOT/devel, then
>  sage -cython TestImport.pyx
> works.
>
> - Trying
>  sage -cython TestImport.pyx
> does *not* work, if TestImport.pyx is in SAGE_ROOT or outside the sage
> tree.
>
> - suppose you have a setup.py comprising:
> from distutils.core import setup
> from distutils.extension import Extension
> from Cython.Distutils import build_ext
> from sage.all import SAGE_ROOT
> setup(
>  name = "TestImport",
>  version = "1.0",
>  author = "Simon A. King",
>  ext_modules=[
>    Extension("TestImport",
>              sources = ["TestImport.pyx"],
>              include_dirs = ['.',SAGE_ROOT+'/devel/'])],
>  cmdclass = {'build_ext': build_ext})
>
> Running "python setup.py build_ext --inplace" in the sage-shell does
> not work.
>
> This is surprising for me, because I expected that include_dirs would
> provide search paths for pxd-headers, and because "sage -cython
> Testimport.pyx" worked inside SAGE_ROOT/devel/.
>
> So, what must I do in order to successfully build my TestImport
> extension (don't worry, I am not going to submit TestImport as an
> spkg...)?
>
> Cheers,
>     Simon
>
> >
>



-- 
William Stein
Associate Professor of Mathematics
University of Washington
http://wstein.org

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to