Opened 4 years ago

Closed 3 years ago

#22113 closed defect (fixed)

fix file extension error using c++ with %cython

Reported by: boothby Owned by:
Priority: minor Milestone: sage-7.5
Component: cython Keywords: sagenb cpp cython
Cc: Merged in:
Authors: Kelly Boothby Reviewers: Jeroen Demeyer, Julian Rüth
Report Upstream: N/A Work issues:
Branch: 77f3c56 (Commits) Commit: 77f3c566f6cee069379fe40248471e8d629f80be
Dependencies: Stopgaps:

Description

Running this in the notebook...

%cython
#clang c++

produces the following error:

cp: cannot stat ‘_home_boothby__sage_sage_notebook_sagenb_home_admin_90_code_sage6_spyx_0.c’: No such file or directory
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_5.py", line 10, in <module>
    exec compile(u'_support_.cython_import_all("/home/boothby/.sage/sage_notebook.sagenb/home/admin/90/code/sage6.spyx", globals())
  File "", line 1, in <module>
    
  File "/home/boothby/sage-7.4/local/lib/python2.7/site-packages/sagenb/misc/support.py", line 484, in cython_import_all
    create_local_c_file=create_local_c_file)
  File "/home/boothby/sage-7.4/local/lib/python2.7/site-packages/sagenb/misc/support.py", line 461, in cython_import
    create_local_c_file=create_local_c_file)
  File "/home/boothby/sage-7.4/local/lib/python2.7/site-packages/sage/misc/cython.py", line 473, in cython
    raise RuntimeError("Error converting {} to C:\n{}\n{}".format(filename, log, err))
RuntimeError: Error converting /home/boothby/.sage/sage_notebook.sagenb/home/admin/90/code/sage6.spyx to C:

Note that this issue has been known for six years, but wasn't fixed because it was mentioned on a related-looking ticket that got closed: https://trac.sagemath.org/ticket/10249#comment:2

Also, the doctests in misc.cython.cython are problematic in that they look like they're testing that function, but they're actually testing misc.cython_c.cython_compile. To that end, I'm adding another doctest,

    Check that compiling c++ code works, when creating a local c file,
    first moving to a tempdir to avoid clutter::

        sage: import sage.misc.cython, sage.misc.temporary_file, os
        sage: d = sage.misc.temporary_file.tmp_dir()
        sage: os.chdir(d)
        sage: f = open(d+"src.pyx", 'w')
        sage: f.write("#clang C++\n"
        ....:       "from libcpp.vector cimport vector\n"
        ....:       "cdef vector[int] * v = new vector[int](4)\n")
        sage: f.close()
        sage: output = sage.misc.cython.cython(d+"src.pyx", create_local_c_file=True)

Change History (12)

comment:1 Changed 4 years ago by boothby

  • Branch set to u/boothby/fix_file_extension_error_using_c___with__cython

comment:2 Changed 4 years ago by git

  • Commit set to c32a0753e8ba3f1fa62937c1a0a6b09a5c3d0b38

Branch pushed to git repo; I updated commit sha1. New commits:

c32a075fixing a newbie git-trac mistake

comment:3 Changed 4 years ago by boothby

  • Status changed from new to needs_review

comment:4 Changed 4 years ago by boothby

  • Authors set to Kelly Boothby

comment:5 Changed 4 years ago by dimpase

Is it OSX's clang, or linux clang that you test it on?

comment:6 Changed 4 years ago by boothby

I tested on linux. clang is just a keyword, not the llvm frontend.

comment:7 Changed 4 years ago by dimpase

I find this quite confusing. I'd say that if you use #clang then by right it ought to mean that you have clang installed and all this. (It's actually not hard to install clang and clang++ on linux).

comment:8 Changed 4 years ago by boothby

My understanding is that this particular 5-letter string "clang" has absolutely nothing to do with the software project named CLANG (written allcaps to disambiguate).

Please refer to the documentation of sage.misc.cython_c.cython_compile:

    Given a block of Cython code (as a text string), this function compiles it using a C compiler, and includes it into the global scope of the module that called this function.

    The following pragmas are available:

        clang - may be either c or c++ (or C or C++) indicating whether a C or C++ compiler should be used.
        clib - additional libraries to be linked in, the space separated list is split and passed to distutils.
        cinclude - additional directories to search for header files. The space separated list is split and passed to distutils.
        cfile - additional C or C++ files to be compiled. Also, $SAGE_ROOT is expanded, but other environment variables are not.
        cargs - additional parameters passed to the compiler

    For example:

    #clang C++
    #clib givaro
    #cinclude /usr/local/include/
    #cargs -ggdb
    #cfile foo.c

    AUTHOR: William Stein, 2006-10-31

Note that William wrote this code a full year before CLANG was released -- the code was working back then, without CLANG. It should still work without CLANG.

comment:9 Changed 3 years ago by jdemeyer

  • Branch changed from u/boothby/fix_file_extension_error_using_c___with__cython to u/jdemeyer/fix_file_extension_error_using_c___with__cython

comment:10 Changed 3 years ago by jdemeyer

  • Commit changed from c32a0753e8ba3f1fa62937c1a0a6b09a5c3d0b38 to 77f3c566f6cee069379fe40248471e8d629f80be
  • Reviewers set to Jeroen Demeyer

Rebased and simplified the tests. Needs review.


New commits:

eb37060fixed bug where a .c extension was hardcoded and needs to be .cpp sometimes
77f3c56Simplify doctest

comment:11 Changed 3 years ago by saraedum

  • Reviewers changed from Jeroen Demeyer to Jeroen Demeyer, Julian Rüth
  • Status changed from needs_review to positive_review

comment:12 Changed 3 years ago by vbraun

  • Branch changed from u/jdemeyer/fix_file_extension_error_using_c___with__cython to 77f3c566f6cee069379fe40248471e8d629f80be
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.