Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#10249 closed enhancement (invalid)

(invalid -- do not use) c++ and %cython in the notebook

Reported by: was Owned by: jason
Priority: major Milestone: sage-duplicate/invalid/wontfix
Component: misc Keywords:
Cc: Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges


Make it so that the "#clang c++" pragma in %cython blocks in the notebook correctly invokes the --cplus option to the Cython compiler.

This is related to trac #10094.

Attachments (1)

trac_10249.patch (833 bytes) - added by was 12 years ago.

Download all attachments as: .zip

Change History (5)

Changed 12 years ago by was

comment:1 Changed 12 years ago by was

You could test this by pasting the following into a notebook cell:

struct Elt10 {
    long a, b;

Elt10 operator+(Elt10 x, Elt10 y) {
    Elt10 z;
    z.a = x.a + y.a;
    z.b = x.a + y.a;
    return z;

void iadd(Elt10* x, Elt10 y) {
    x->a = x->a + y.a;
    x->b = x->b + y.b;

Elt10 operator*(Elt10 x, Elt10 y) {
    Elt10 z;
    z.a = x.a*y.a + x.b*y.b;
    z.b = x.b*y.a + x.a*y.b + x.b*y.b;
    return z;

Then making another notebook cell and pasting the following in:

#clang c++

cdef extern from "stdlib.h":
    long random()

cdef extern from "/tmp/a.h":
    cdef cppclass Elt10:
        long a, b
    Elt10 operator+(Elt10 x, Elt10 y)
    Elt10 operator*(Elt10 x, Elt10 y)
    void iadd(Elt10* x, Elt10 y)

cdef class Vec4:
    cdef Elt10* elements
    cdef long n
    def __cinit__(self, Py_ssize_t n):
        # dynamic memory allocation on the heap
        self.elements = <Elt10*>malloc(n*sizeof(Elt10))
        self.n = n
    def __dealloc__(self):
    def randomize(self):
        cdef Py_ssize_t m
        for m in range(self.n):
            self.elements[m].a = random(); self.elements[m].b = random()
    def dot(self, Vec4 other not None):
        cdef Elt10 ans, tmp
        ans.a = 0; ans.b = 0
        cdef Py_ssize_t m
        # This is the part that gets very worrisome, since the code does
        # not look easy to read.  And it can easily get massively more complicated
        # when the arithmetic isn't so simple!
        for m in range(self.n): 
            ans = ans + self.elements[m] * other.elements[m]
        return ans.a, ans.b

Then in a third cell do:

v = Vec4(10^6); w = Vec4(10^6); v.randomize(); w.randomize()

OK, not exactly a minimal example :-)

comment:2 Changed 12 years ago by vbraun

Are there any prerequisite patches? On sage-4.6 with no other patches to the notebook, I tried

#clang c++
cdef extern from "math.h":
    double sin(double)
cpdef t():
    return sin(1)

This works without the #clang, but in c++ mode the notebook gives me an error

cp: cannot stat `_home_vbraun__sage_sage_notebook_sagenb_home_admin_2_code_sage29_spyx_0.c': No such file or directory
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "", line 10, in <module>
    exec compile(u'_support_.cython_import_all("/home/vbraun/.sage/sage_notebook.sagenb/home/admin/2/code/sage29.spyx", globals())
  File "", line 1, in <module>
  File "/home/vbraun/Sage/sage/devel/sagenb/sagenb/misc/", line 519, in cython_import_all
  File "/home/vbraun/Sage/sage/devel/sagenb/sagenb/misc/", line 496, in cython_import
  File "/home/vbraun/Sage/sage/local/lib/python2.6/site-packages/sage/misc/", line 372, in cython
    raise RuntimeError, "Error converting %s to C:\n%s\n%s"%(filename, log, err)
RuntimeError: Error converting /home/vbraun/.sage/sage_notebook.sagenb/home/admin/2/code/sage29.spyx to C:

Cython created the source file _home_vbraun__sage_sage_notebook_sagenb_home_admin_2_code_sage29_spyx_0.cpp, so cp shouldn't try to copy the .c file.

comment:3 Changed 12 years ago by was

  • Resolution set to invalid
  • Status changed from new to closed
  • Summary changed from c++ and %cython in the notebook to (invalid -- do not use) c++ and %cython in the notebook

This is no longer needed in sage-4.6, and in fact breaks things! So marked as in valid -- do not apply.

comment:4 Changed 12 years ago by mvngu

  • Milestone changed from sage-4.6.1 to sage-duplicate/invalid/wontfix
Note: See TracTickets for help on using tickets.