Skip to content

Commit

Permalink
Trac #9552: cython.py references the old sage notebook code
Browse files Browse the repository at this point in the history
I noticed to my surprise that misc/cython.py has these lines in it
(which should be fixed, of course):
{{{
 import sage.server.support
    d = {}
    sage.server.support.cython_import_all(tmpfile, d,
                                         verbose=verbose,
compile_message=compile_message,
                                         use_cache=use_cache,
                                         create_local_c_file=False)
}}}

URL: http://trac.sagemath.org/9552
Reported by: was
Ticket author(s): Jeroen Demeyer
Reviewer(s): François Bissey
  • Loading branch information
Release Manager authored and vbraun committed May 30, 2015
2 parents 454585b + 1fc034e commit 82d2d88
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 113 deletions.
6 changes: 3 additions & 3 deletions src/sage/misc/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@
from sage_input import sage_input

from cython import cython_lambda, cython_create_local_so
from cython_c import cython
pyrex = cython # synonym -- for now
sagex = cython # synonym -- for now
from cython_c import cython_compile as cython
lazy_import("sage.misc.cython_c", "cython_compile", "pyrex", deprecation=9552)
lazy_import("sage.misc.cython_c", "cython_compile", "sagex", deprecation=9552)

from persist import save, load, dumps, loads, db, db_save

Expand Down
106 changes: 64 additions & 42 deletions src/sage/misc/cython.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

from __future__ import print_function

import os, sys, platform
import os, sys, platform, __builtin__

from sage.env import SAGE_LOCAL, SAGE_SRC, UNAME
from misc import SPYX_TMP
from temporary_file import tmp_filename


def cblas():
"""
Expand Down Expand Up @@ -495,38 +497,6 @@ def cython(filename, verbose=False, compile_message=False,
if language=='c++':
os.system("cd '%s' && mv '%s.c' '%s.cpp'"%(build_dir,name,name))

## if make_c_file_nice and os.path.exists(target_c):
## R = open(target_c).read()
## R = "/* THIS IS A PARSED TO MAKE READABLE VERSION OF THE C FILE. */" + R

## # 1. Get rid of the annoying __pyx_'s before variable names.
## # R = R.replace('__pyx_v_','').replace('__pyx','')
## # 2. Replace the line number references by the actual code from the file,
## # since it is very painful to go back and forth, and the philosophy
## # of Sage is that everything that can be very easy *is*.

## pyx_file = os.path.abspath('%s/%s.pyx'%(build_dir,name))
## S = '/* "%s":'%pyx_file
## n = len(S)
## last_i = -1
## X = F.split('\n')
## stars = '*'*80
## while True:
## i = R.find(S)
## if i == -1 or i == last_i: break
## last_i = i
## j = R[i:].find('\n')
## if j == -1: break
## line_number = int(R[i+n: i+j])
## try:
## line = X[line_number-1]
## except IndexError:
## line = '(missing code)'
## R = R[:i+2] + '%s\n\n Line %s: %s\n\n%s'%(stars, line_number, line, stars) + R[i+j:]

## open(target_c,'w').write(R)


cmd = 'cd %s && python setup.py build 1>log 2>err'%build_dir
if verbose:
print(cmd)
Expand Down Expand Up @@ -658,18 +628,17 @@ def f(%s):
"""%(v, expr)
if verbose:
print(s)
import sage.misc.misc
tmpfile = sage.misc.temporary_file.tmp_filename(ext=".spyx")
tmpfile = tmp_filename(ext=".spyx")
open(tmpfile,'w').write(s)

import sage.server.support
d = {}
sage.server.support.cython_import_all(tmpfile, d,
verbose=verbose, compile_message=compile_message,
use_cache=use_cache,
create_local_c_file=False)
cython_import_all(tmpfile, d,
verbose=verbose, compile_message=compile_message,
use_cache=use_cache,
create_local_c_file=False)
return d['f']


def cython_create_local_so(filename):
r"""
Compile filename and make it available as a loadable shared object file.
Expand Down Expand Up @@ -715,6 +684,61 @@ def cython_create_local_so(filename):
"""
cython(filename, compile_message=True, use_cache=False, create_local_so_file=True)


################################################################
# IMPORT
################################################################
def cython_import(filename, verbose=False, compile_message=False,
use_cache=False, create_local_c_file=True, **kwds):
"""
Compile a file containing Cython code, then import and return the
module. Raises an ``ImportError`` if anything goes wrong.
INPUT:
- ``filename`` - a string; name of a file that contains Cython
code
See the function :func:`sage.misc.cython.cython` for documentation
for the other inputs.
OUTPUT:
- the module that contains the compiled Cython code.
"""
name, build_dir = cython(filename, verbose=verbose,
compile_message=compile_message,
use_cache=use_cache,
create_local_c_file=create_local_c_file,
**kwds)
sys.path.append(build_dir)
return __builtin__.__import__(name)


def cython_import_all(filename, globals, verbose=False, compile_message=False,
use_cache=False, create_local_c_file=True):
"""
Imports all non-private (i.e., not beginning with an underscore)
attributes of the specified Cython module into the given context.
This is similar to::
from module import *
Raises an ``ImportError`` exception if anything goes wrong.
INPUT:
- ``filename`` - a string; name of a file that contains Cython
code
"""
m = cython_import(filename, verbose=verbose, compile_message=compile_message,
use_cache=use_cache,
create_local_c_file=create_local_c_file)
for k, x in m.__dict__.iteritems():
if k[0] != '_':
globals[k] = x


def sanitize(f):
"""
Given a filename ``f``, replace it by a filename that is a valid Python
Expand Down Expand Up @@ -760,10 +784,8 @@ def compile_and_load(code):
sage: module.f(10)
100
"""
from sage.misc.temporary_file import tmp_filename
file = tmp_filename(ext=".pyx")
open(file,'w').write(code)
from sage.server.support import cython_import
return cython_import(file, create_local_c_file=False)


Expand Down
18 changes: 8 additions & 10 deletions src/sage/misc/cython_c.pyx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"On-the-fly generation of compiled extensions"

import sage.misc.misc
import sage.server.support
from sage.misc.temporary_file import tmp_filename
from sage.misc.cython import cython_import_all

def cython(code,
def cython_compile(code,
verbose=False, compile_message=False,
make_c_file_nice=False, use_cache=False):
"""
Expand Down Expand Up @@ -58,11 +58,9 @@ def cython(code,
Need to create a clever caching system so code only gets
compiled once.
"""
tmpfile = sage.misc.temporary_file.tmp_filename(ext=".spyx")
tmpfile = tmp_filename(ext=".spyx")
open(tmpfile,'w').write(code)
sage.server.support.cython_import_all(tmpfile, globals(),
verbose=verbose, compile_message=compile_message,
use_cache=use_cache,
create_local_c_file=False)


cython_import_all(tmpfile, globals(),
verbose=verbose, compile_message=compile_message,
use_cache=use_cache,
create_local_c_file=False)
60 changes: 2 additions & 58 deletions src/sage/server/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,61 +29,5 @@
'source_code', 'syseval'),
deprecation=2891)


######################################################################
# Cython
######################################################################
import sage.misc.cython
import sys
import __builtin__

def cython_import(filename, verbose=False, compile_message=False,
use_cache=False, create_local_c_file=True, **kwds):
"""
Compile a file containing Cython code, then import and return the
module. Raises an ``ImportError`` if anything goes wrong.
INPUT:
- ``filename`` - a string; name of a file that contains Cython
code
See the function :func:`sage.misc.cython.cython` for documentation
for the other inputs.
OUTPUT:
- the module that contains the compiled Cython code.
"""
name, build_dir = sage.misc.cython.cython(filename, verbose=verbose,
compile_message=compile_message,
use_cache=use_cache,
create_local_c_file=create_local_c_file,
**kwds)
sys.path.append(build_dir)
return __builtin__.__import__(name)


def cython_import_all(filename, globals, verbose=False, compile_message=False,
use_cache=False, create_local_c_file=True):
"""
Imports all non-private (i.e., not beginning with an underscore)
attributes of the specified Cython module into the given context.
This is similar to::
from module import *
Raises an ``ImportError`` exception if anything goes wrong.
INPUT:
- ``filename`` - a string; name of a file that contains Cython
code
"""
m = cython_import(filename, verbose=verbose, compile_message=compile_message,
use_cache=use_cache,
create_local_c_file=create_local_c_file)
for k, x in m.__dict__.iteritems():
if k[0] != '_':
globals[k] = x

lazy_import('sage.misc.cython', ('cython_import', 'cython_import_all'),
deprecation=9552)

0 comments on commit 82d2d88

Please sign in to comment.