Ticket #11954: trac_11954-review.patch

File trac_11954-review.patch, 15.6 KB (added by David Loeffler, 11 years ago)

Apply over previous patch

  • sage/misc/cython.py

    # HG changeset patch
    # User David Loeffler <d.loeffler.01@cantab.net>
    # Date 1331602658 0
    # Node ID 07d23cafbe189a4e73370df5bdbc7990ce8c4662
    # Parent  2536fbaf1320e1b940e19fcacedad07849e4e358
    #11954: formatting fixes
    
    diff --git a/sage/misc/cython.py b/sage/misc/cython.py
    a b  
    2323
    2424def cblas():
    2525    """
    26     Return the name of the cblas library on this system.  If the
    27     environment variable :envvar:`$SAGE_CBLAS` is set, just return its
    28     value.  If not, return 'cblas' if :file:`/usr/lib/libcblas.so`
    29     or :file:`/usr/lib/libcblas.dylib` exists, return 'blas' if
    30     :file:`/usr/lib/libblas.dll.a` exists, and return 'gslcblas'
    31     otherwise.
     26    Return the name of the cblas library on this system. If the environment
     27    variable :envvar:`$SAGE_CBLAS` is set, just return its value. If not,
     28    return ``'cblas'`` if :file:`/usr/lib/libcblas.so` or
     29    :file:`/usr/lib/libcblas.dylib` exists, return ``'blas'`` if
     30    :file:`/usr/lib/libblas.dll.a` exists, and return ``'gslcblas'`` otherwise.
    3231
    3332    EXAMPLES::
    3433
     
    4342    elif os.path.exists('/usr/lib/libblas.dll.a'):   # untested.
    4443        return 'blas'
    4544    else:
    46         # This is very slow  (?), but *guaranteed* to be available.
    47         return 'gslcblas' 
     45        # This is very slow (?), but *guaranteed* to be available.
     46        return 'gslcblas'
    4847
    4948# In case of ATLAS we need to link against cblas as well as atlas
    5049# In the other cases we just return the same library name as cblas()
    5150# which is fine for the linker
    5251#
    53 # We should be using the Accelerate FrameWork on OS X, but that requires 
     52# We should be using the Accelerate FrameWork on OS X, but that requires
    5453# some magic due to distutils having ridden on the short bus :)
    5554def atlas():
    5655    """
    57     Returns the name of the ATLAS library to use.  On Darwin or
    58     Cygwin, this is 'blas', and otherwise it is 'atlas'.
     56    Returns the name of the ATLAS library to use. On Darwin or Cygwin, this is
     57    ``'blas'``, and otherwise it is ``'atlas'``.
    5958
    6059    EXAMPLES::
    6160
     
    8382
    8483def parse_keywords(kwd, s):
    8584    r"""
    86     Given a keyword kwd and a string s, return a list of all arguments
    87     on the same line as that keyword in s, as well as a new copy of
    88     s in which each occurrence of kwd is in a comment. If a comment
    89     already occurs on the line containing kwd, no words after the #
    90     are added to the list.
     85    Given a keyword ``kwd`` and a string ``s``, return a list of all arguments
     86    on the same line as that keyword in ``s``, as well as a new copy of ``s``
     87    in which each occurrence of ``kwd`` is in a comment. If a comment already
     88    occurs on the line containing ``kwd``, no words after the ``#`` are added
     89    to the list.
    9190
    9291    EXAMPLES::
    93    
     92
    9493        sage: sage.misc.cython.parse_keywords('clib', " clib foo bar baz\n #cinclude bar\n")
    9594        (['foo', 'bar', 'baz'], ' #clib foo bar baz\n #cinclude bar\n')
    9695
     
    126125            if X[0] == '#':   # skip rest of line
    127126                break
    128127            v.append(X)
    129            
     128
    130129    return v, s
    131130
    132131def environ_parse(s):
    133132    r"""
    134     Given a string s, find each substring of the form '\$ABC'. If the
    135     environment variable :envvar:`ABC` is set, replace '\$ABC' with its
    136     value and move on to the next such substring.  If it is not set,
    137     stop parsing there.
     133    Given a string s, find each substring of the form ``'\$ABC'``. If the
     134    environment variable :envvar:`$ABC` is set, replace ``'\$ABC'`` with its
     135    value and move on to the next such substring. If it is not set, stop
     136    parsing there.
    138137
    139138    EXAMPLES::
    140139
     
    164163
    165164def pyx_preparse(s):
    166165    r"""
    167     Preparse a pyx file
     166    Preparse a pyx file:
    168167
    169     * include cdefs.pxi, interrupt.pxi, stdsage.pxi
    170     * parse clang pragma (c or c++)
    171     * parse clib pragma (additional libraries to link in)
    172     * parse cinclude (additional include directories)
    173     * parse cfile (additional files to be included)
    174     * parse cargs (additional parameters passed to the compiler)
     168    * include ``cdefs.pxi``, ``interrupt.pxi``, ``stdsage.pxi``
     169    * parse ``clang`` pragma (c or c++)
     170    * parse ``clib`` pragma (additional libraries to link in)
     171    * parse ``cinclude`` (additional include directories)
     172    * parse ``cfile`` (additional files to be included)
     173    * parse ``cargs`` (additional parameters passed to the compiler)
    175174
    176175    The pragmas:
    177176
    178     - ``clang`` - may be either c or c++ indicating whether a C or C++
    179       compiler should be used
     177    - ``clang`` - may be either ``'c'`` or ``'c++'`` indicating whether a C or
     178      C++ compiler should be used
    180179
    181     - ``clib`` - additional libraries to be linked in, the space
    182       separated list is split and passed to distutils.
     180    - ``clib`` - additional libraries to be linked in, the space separated list
     181      is split and passed to distutils.
    183182
    184     - ``cinclude`` - additional directories to search for header
    185       files. The space separated list is split and passed to
    186       distutils.
     183    - ``cinclude`` - additional directories to search for header files. The
     184      space separated list is split and passed to distutils.
    187185
    188186    - ``cfile`` - additional C or C++ files to be compiled. Also,
    189       ``$SAGE_ROOT`` is expanded, but other environment variables
    190       are not.
     187      :envvar:`$SAGE_ROOT` is expanded, but other environment variables are
     188      not.
    191189
    192190    - ``cargs`` - additional parameters passed to the compiler
    193191
     
    238236        'csage']
    239237        sage: libs[1:] == sage.misc.cython.standard_libs
    240238        True
    241        
     239
    242240        sage: inc
    243241        ['bar',
    244242        '.../local/include/csage/',
     
    270268    libs = v + standard_libs
    271269
    272270    additional_source_files, s = parse_keywords('cfile', s)
    273    
     271
    274272    v, s = parse_keywords('cinclude', s)
    275273    inc = [environ_parse(x.replace('"','').replace("'","")) for x in v] + include_dirs
    276274    s = """\ninclude "cdefs.pxi"\n""" + s
     
    290288# overwriting the definitions of everything in the original .so file.
    291289#
    292290# HOW: By using a sequence_number for each .spyx file; we keep
    293 # these sequence numbers in a dict. 
     291# these sequence numbers in a dict.
    294292#
    295293################################################################
    296294
     
    300298           use_cache=False, create_local_c_file=False, annotate=True, sage_namespace=True,
    301299           create_local_so_file=False):
    302300    """
    303     Compile a Cython file.  This converts a Cython file to a C (or C++
    304     file), and then compiles that. The .c file and the .so file are
     301    Compile a Cython file. This converts a Cython file to a C (or C++ file),
     302    and then compiles that. The .c file and the .so file are
    305303    created in a temporary directory.
    306304
    307305    INPUTS:
    308306
    309     - ``filename`` - the name of the file to be compiled.  Should end
    310       with 'pyx'.
     307    - ``filename`` - the name of the file to be compiled. Should end with
     308      'pyx'.
    311309
    312310    - ``verbose`` (bool, default False) - if True, print debugging
    313311      information.
    314312
    315313    - ``compile_message`` (bool, default False) - if True, print
    316       'Compiling <filename>...'
     314      ``'Compiling <filename>...'``
    317315
    318316    - ``use_cache`` (bool, default False) - if True, check the
    319317      temporary build directory to see if there is already a
    320       corresponding .so file.  If so, and if the .so file is newer
    321       than the Cython file, don't recompile, just reuse the .so file.
     318      corresponding .so file. If so, and if the .so file is newer than the
     319      Cython file, don't recompile, just reuse the .so file.
    322320
    323321    - ``create_local_c_file`` (bool, default False) - if True, save a
    324322      copy of the .c file in the current directory.
    325323
    326     - ``annotate`` (bool, default True) - if True, create an html file
    327       which annotates the conversion from .pyx to .c.  By default this
    328       is only created in the temporary directory, but if
    329       ``create_local_c_file`` is also True, then save a copy of the
    330       .html file in the current directory.
     324    - ``annotate`` (bool, default True) - if True, create an html file which
     325      annotates the conversion from .pyx to .c. By default this is only created
     326      in the temporary directory, but if ``create_local_c_file`` is also True,
     327      then save a copy of the .html file in the current directory.
    331328
    332329    - ``sage_namespace`` (bool, default True) - if True, import
    333330      ``sage.all``.
     
    340337    if not filename.endswith('pyx'):
    341338        print "File (=%s) should have extension .pyx"%filename
    342339
    343     # base is the name of the .so module that we create.  If we are
     340    # base is the name of the .so module that we create. If we are
    344341    # creating a local shared object file, we use a more natural
    345342    # naming convention. If we are not creating a local shared object
    346343    # file, the main constraint is that it is unique and determined by
     
    355352
    356353    # This is the *temporary* directory where we build the pyx file.
    357354    # This is deleted when sage exits, which means pyx files must be
    358     # rebuilt every time Sage is restarted at present. 
     355    # rebuilt every time Sage is restarted at present.
    359356    build_dir = '%s/%s'%(SPYX_TMP, base)
    360357
    361358    if os.path.exists(build_dir):
    362         # There is already a module here.  Maybe we do not have to rebuild?
     359        # There is already a module here. Maybe we do not have to rebuild?
    363360        # Find the name.
    364361        if use_cache:
    365362            prev_so = [F for F in os.listdir(build_dir) if F[-3:] == '.so']
     
    379376            pass
    380377
    381378    # Get the absolute path to the directory that contains the pyx file.
    382     # We will use this only to make some convenient symbolic links. 
     379    # We will use this only to make some convenient symbolic links.
    383380    abs_base = os.path.split(os.path.abspath(filename))[0]
    384381
    385382    # bad things happen if the current directory is devel/sage-*
     
    388385        os.system(cmd)
    389386        if os.path.exists("%s/setup.py" % build_dir):
    390387            os.unlink("%s/setup.py" % build_dir)
    391    
     388
    392389    if compile_message:
    393390        print "Compiling %s..."%filename
    394        
     391
    395392    F = open(filename).read()
    396393
    397394    F, libs, includes, language, additional_source_files, extra_args = pyx_preparse(F)
     
    447444                     extra_compile_args = extra_compile_args,
    448445                     extra_link_args = extra_link_args,
    449446                     language = '%s' )]
    450                      
     447
    451448setup(ext_modules = ext_modules,
    452449      include_dirs = %s)
    453450    """%(extra_args, name, name, extension, additional_source_files, libs, language, includes)
    454451    open('%s/setup.py'%build_dir,'w').write(setup)
    455452
    456453    cython_include = ' '.join(["-I '%s'"%x for x in includes if len(x.strip()) > 0 ])
    457    
     454
    458455    options = ['-p']
    459456    if annotate:
    460457        options.append('-a')
     
    471468        if annotate:
    472469            target_html = '%s/_%s.html'%(os.path.abspath(os.curdir), base)
    473470            cmd += " && cp '%s.html' '%s'"%(name, target_html)
    474        
     471
    475472    if verbose:
    476473        print cmd
    477474    if os.system(cmd):
     
    485482##     if make_c_file_nice and os.path.exists(target_c):
    486483##         R = open(target_c).read()
    487484##         R = "/* THIS IS A PARSED TO MAKE READABLE VERSION OF THE C FILE. */" + R
    488        
     485
    489486##         # 1. Get rid of the annoying __pyx_'s before variable names.
    490487##         # R = R.replace('__pyx_v_','').replace('__pyx','')
    491488##         # 2. Replace the line number references by the actual code from the file,
     
    510507##             except IndexError:
    511508##                 line = '(missing code)'
    512509##             R = R[:i+2] + '%s\n\n Line %s: %s\n\n%s'%(stars, line_number, line, stars) + R[i+j:]
    513            
     510
    514511##         open(target_c,'w').write(R)
    515    
     512
    516513
    517514    cmd = 'cd %s && python setup.py build 1>log 2>err'%build_dir
    518515    if verbose: print cmd
     
    520517        log = open('%s/log'%build_dir).read()
    521518        err = open('%s/err'%build_dir).read()
    522519        raise RuntimeError, "Error compiling %s:\n%s\n%s"%(filename, log, err)
    523    
     520
    524521    # Move from lib directory.
    525522    cmd = 'mv %s/build/lib.*/* %s'%(build_dir, build_dir)
    526523    if verbose: print cmd
     
    539536
    540537def subtract_from_line_numbers(s, n):
    541538    r"""
    542     Given a string ``s`` and an integer ``n``, for any line of ``s``
    543     which has the form 'text:NUM:text' subtract ``n`` from NUM and
    544     return 'text:(NUM-n):text'.  Return other lines of ``s`` without
    545     change.
     539    Given a string ``s`` and an integer ``n``, for any line of ``s`` which has
     540    the form ``'text:NUM:text'`` subtract ``n`` from NUM and return
     541    ``'text:(NUM-n):text'``. Return other lines of ``s`` without change.
    546542
    547543    EXAMPLES::
    548544
     
    571567                 compile_message=False,
    572568                 use_cache=False):
    573569    """
    574     Create a compiled function which evaluates expr assuming machine
    575     values for vars.
     570    Create a compiled function which evaluates ``expr`` assuming machine values
     571    for ``vars``.
    576572
    577     WARNING: This implementation is not well tested.
     573    .. warning::
     574
     575        This implementation is not well tested.
    578576
    579577    INPUT:
    580578
    581     - ``vars`` - list of pairs (variable name, c-data type), where
    582       the variable names and data types are strings, OR a string such
    583       as 'double x, int y, int z'
     579    - ``vars`` - list of pairs (variable name, c-data type), where the variable
     580      names and data types are strings, OR a string such as ``'double x, int y,
     581      int z'``
    584582
    585     - ``expr`` - an expression involving the vars and constants; you
    586       can access objects defined in the current module scope globals()
    587       using sagobject_name.  See the examples below.
     583    - ``expr`` - an expression involving the vars and constants; you can access
     584      objects defined in the current module scope ``globals()`` using
     585      ``sage.object_name``.
    588586
    589587    EXAMPLES:
    590    
     588
    591589    We create a Lambda function in pure Python (using the r to make sure the 3.2
    592590    is viewed as a Python float)::
    593591
     
    609607        24.455978889110629
    610608        sage: a = 50
    611609        sage: f(10)                                                                      # optional -- gcc
    612         49.455978889110632       
     610        49.455978889110632
    613611    """
    614612    if isinstance(vars, str):
    615613        v = vars
     
    639637                                         use_cache=use_cache,
    640638                                         create_local_c_file=False)
    641639    return d['f']
    642    
     640
    643641def cython_create_local_so(filename):
    644642    r"""
    645643    Compile filename and make it available as a loadable shared object file.
    646    
     644
    647645    INPUT:
    648646
    649647    - ``filename`` - string: a Cython (formerly SageX) (.spyx) file
    650    
     648
    651649    OUTPUT: None
    652    
     650
    653651    EFFECT: A compiled, python "importable" loadable shared object file is created.
    654652
    655653    .. note::
    656654
    657         Shared object files are {NOT} reloadable. The intent is for
     655        Shared object files are *not* reloadable. The intent is for
    658656        imports in other scripts. A possible development cycle might
    659657        go thus:
    660  
     658
    661659        - Attach a .spyx file
    662660        - Interactively test and edit it to your satisfaction
    663         - Use cython_create_local_so to create the shared object file
     661        - Use ``cython_create_local_so`` to create the shared object file
    664662        - Import the .so file in other scripts
    665663
    666664    EXAMPLES::
     
    687685
    688686def sanitize(f):
    689687    """
    690     Given a filename f, replace it by a filename that is a valid Python
     688    Given a filename ``f``, replace it by a filename that is a valid Python
    691689    module name.
    692690
    693     This means that the characters are all alphanumeric or _'s and
    694     doesn't begin with a numeral.
     691    This means that the characters are all alphanumeric or ``_``'s and doesn't
     692    begin with a numeral.
    695693
    696694    EXAMPLES::
    697695