Skip to content

Commit

Permalink
Trac #28537: remove one deprecated thing in misc/cython
Browse files Browse the repository at this point in the history
plus small pep8 cleanup

after #24722

URL: https://trac.sagemath.org/28537
Reported by: chapoton
Ticket author(s): Frédéric Chapoton
Reviewer(s): Travis Scrimshaw
  • Loading branch information
Release Manager committed Oct 5, 2019
2 parents 4838a45 + 5338d8a commit 257b96d
Showing 1 changed file with 13 additions and 62 deletions.
75 changes: 13 additions & 62 deletions src/sage/misc/cython.py
Expand Up @@ -64,6 +64,7 @@

sequence_number = {}


def cython(filename, verbose=0, compile_message=False,
use_cache=False, create_local_c_file=False, annotate=True, sage_namespace=True,
create_local_so_file=False):
Expand Down Expand Up @@ -221,7 +222,7 @@ def cython(filename, verbose=0, compile_message=False,
prev_so = [F for F in os.listdir(target_dir) if F.endswith(loadable_module_extension())]
if len(prev_so) > 0:
prev_so = prev_so[0] # should have length 1 because of deletes below
if os.path.getmtime(filename) <= os.path.getmtime('%s/%s'%(target_dir, prev_so)):
if os.path.getmtime(filename) <= os.path.getmtime('%s/%s' % (target_dir, prev_so)):
# We do not have to rebuild.
return prev_so[:-len(loadable_module_extension())], target_dir

Expand All @@ -243,7 +244,7 @@ def cython(filename, verbose=0, compile_message=False,
global sequence_number
if base not in sequence_number:
sequence_number[base] = 0
name = '%s_%s'%(base, sequence_number[base])
name = '%s_%s' % (base, sequence_number[base])

# increment the sequence number so will use a different one next time.
sequence_number[base] += 1
Expand Down Expand Up @@ -287,12 +288,12 @@ def cython(filename, verbose=0, compile_message=False,
with restore_cwd(target_dir):
try:
ext, = cythonize([ext],
aliases=cython_aliases(),
include_path=includes,
compiler_directives=directives,
quiet=(verbose <= 0),
errors_to_stderr=False,
use_listing_file=True)
aliases=cython_aliases(),
include_path=includes,
compiler_directives=directives,
quiet=(verbose <= 0),
errors_to_stderr=False,
use_listing_file=True)
finally:
# Read the "listing file" which is the file containing
# warning and error messages generated by Cython.
Expand Down Expand Up @@ -408,7 +409,7 @@ def cython_lambda(vars, expr, verbose=0, **kwds):
if isinstance(vars, str):
v = vars
else:
v = ', '.join(['%s %s'%(typ,var) for typ, var in vars])
v = ', '.join(['%s %s' % (typ, var) for typ, var in vars])

s = """
cdef class _s:
Expand All @@ -428,68 +429,18 @@ def __getattr__(self, name):
def f(%s):
return %s
"""%(v, expr)
""" % (v, expr)
if verbose > 0:
print(s)
tmpfile = tmp_filename(ext=".pyx")
with open(tmpfile,'w') as f:
with open(tmpfile, 'w') as f:
f.write(s)

d = {}
cython_import_all(tmpfile, d, verbose=verbose, **kwds)
return d['f']


def cython_create_local_so(filename):
r"""
Compile filename and make it available as a loadable shared object file.
INPUT:
- ``filename`` - string: a Cython (.spyx) file
OUTPUT: None
EFFECT: A compiled, python "importable" loadable shared object file is created.
.. note::
Shared object files are *not* reloadable. The intent is for
imports in other scripts. A possible development cycle might
go thus:
- Attach a .spyx file
- Interactively test and edit it to your satisfaction
- Use ``cython_create_local_so`` to create the shared object file
- Import the .so file in other scripts
EXAMPLES::
sage: curdir = os.path.abspath(os.curdir)
sage: dir = tmp_dir(); os.chdir(dir)
sage: f = open('hello.spyx', 'w')
sage: s = "def hello():\n print('hello')\n"
sage: _ = f.write(s)
sage: f.close()
sage: cython_create_local_so('hello.spyx')
doctest:...: DeprecationWarning: cython_create_local_so is deprecated, call cython() with the create_local_so_file=True keyword
See http://trac.sagemath.org/24722 for details.
Compiling hello.spyx...
sage: sys.path.append('.')
sage: import hello
sage: hello.hello()
hello
sage: os.chdir(curdir)
AUTHORS:
- David Fu (2008-04-09): initial version
"""
from sage.misc.superseded import deprecation
deprecation(24722, "cython_create_local_so is deprecated, call cython() with the create_local_so_file=True keyword")
cython(filename, compile_message=True, use_cache=False, create_local_so_file=True)


################################################################
# IMPORT
################################################################
Expand Down Expand Up @@ -643,6 +594,6 @@ def cython_compile(code, **kwds):
compiled once.
"""
tmpfile = tmp_filename(ext=".pyx")
with open(tmpfile,'w') as f:
with open(tmpfile, 'w') as f:
f.write(code)
return cython_import_all(tmpfile, get_globals(), **kwds)

0 comments on commit 257b96d

Please sign in to comment.