Ticket #12719: trac_12719-move_to_preparser_rebased.patch

File trac_12719-move_to_preparser_rebased.patch, 13.3 KB (added by jhpalmieri, 9 years ago)
  • sage/misc/interpreter.py

    # HG changeset patch
    # User Mike Hansen <mhansen@gmail.com>
    # Date 1334124076 25200
    # Node ID ebec42ba8aef15147043c4e4751ee17f4b05bf93
    # Parent  7f724965dfde17fb955e83d69db27fc3ec70a2a3
    #12719: Move things which don't belong in sage.misc.interpreter to sage.misc.preparser
    
    diff --git a/sage/misc/interpreter.py b/sage/misc/interpreter.py
    a b import os, log, re, new, sys 
    8686from preparser import (preparse, preparse_file, load_wrap,
    8787                       modified_attached_files, attached_files)
    8888
    89 def load_cython(name):
    90     import cython
    91     cur = os.path.abspath(os.curdir)
    92     try:
    93         mod, dir  = cython.cython(name, compile_message=True, use_cache=True)
    94     except (IOError, OSError, RuntimeError), msg:
    95         print "Error compiling cython file:\n%s"%msg
    96         return ''
    97     import sys
    98     sys.path.append(dir)
    99     return 'from %s import *'%mod
    100 
    101 def handle_encoding_declaration(contents, out):
    102     r"""Find a PEP 263-style Python encoding declaration in the first or
    103     second line of `contents`. If found, output it to `out` and return
    104     `contents` without the encoding line; otherwise output a default
    105     UTF-8 declaration and return `contents`.
    106 
    107     EXAMPLES::
    108 
    109         sage: from sage.misc.interpreter import handle_encoding_declaration
    110         sage: import sys
    111         sage: c1='# -*- coding: latin-1 -*-\nimport os, sys\n...'
    112         sage: c2='# -*- coding: iso-8859-15 -*-\nimport os, sys\n...'
    113         sage: c3='# -*- coding: ascii -*-\nimport os, sys\n...'
    114         sage: c4='import os, sys\n...'
    115         sage: handle_encoding_declaration(c1, sys.stdout)
    116         # -*- coding: latin-1 -*-
    117         'import os, sys\n...'
    118         sage: handle_encoding_declaration(c2, sys.stdout)
    119         # -*- coding: iso-8859-15 -*-
    120         'import os, sys\n...'
    121         sage: handle_encoding_declaration(c3, sys.stdout)
    122         # -*- coding: ascii -*-
    123         'import os, sys\n...'
    124         sage: handle_encoding_declaration(c4, sys.stdout)
    125         # -*- coding: utf-8 -*-
    126         'import os, sys\n...'
    127 
    128     TESTS:
    129 
    130     These are some of the tests listed in PEP 263::
    131 
    132         sage: contents = '#!/usr/bin/python\n# -*- coding: latin-1 -*-\nimport os, sys'
    133         sage: handle_encoding_declaration(contents, sys.stdout)
    134         # -*- coding: latin-1 -*-
    135         '#!/usr/bin/python\nimport os, sys'
    136 
    137         sage: contents = '# This Python file uses the following encoding: utf-8\nimport os, sys'
    138         sage: handle_encoding_declaration(contents, sys.stdout)
    139         # This Python file uses the following encoding: utf-8
    140         'import os, sys'
    141 
    142         sage: contents = '#!/usr/local/bin/python\n# coding: latin-1\nimport os, sys'
    143         sage: handle_encoding_declaration(contents, sys.stdout)
    144         # coding: latin-1
    145         '#!/usr/local/bin/python\nimport os, sys'
    146 
    147     Two hash marks are okay; this shows up in SageTeX-generated scripts::
    148 
    149         sage: contents = '## -*- coding: utf-8 -*-\nimport os, sys\nprint x'
    150         sage: handle_encoding_declaration(contents, sys.stdout)
    151         ## -*- coding: utf-8 -*-
    152         'import os, sys\nprint x'
    153 
    154     When the encoding declaration doesn't match the specification, we
    155     spit out a default UTF-8 encoding.
    156 
    157     Incorrect coding line::
    158 
    159         sage: contents = '#!/usr/local/bin/python\n# latin-1\nimport os, sys'
    160         sage: handle_encoding_declaration(contents, sys.stdout)
    161         # -*- coding: utf-8 -*-
    162         '#!/usr/local/bin/python\n# latin-1\nimport os, sys'
    163 
    164     Encoding declaration not on first or second line::
    165 
    166         sage: contents ='#!/usr/local/bin/python\n#\n# -*- coding: latin-1 -*-\nimport os, sys'
    167         sage: handle_encoding_declaration(contents, sys.stdout)
    168         # -*- coding: utf-8 -*-
    169         '#!/usr/local/bin/python\n#\n# -*- coding: latin-1 -*-\nimport os, sys'
    170        
    171     We don't check for legal encoding names; that's Python's job::
    172 
    173         sage: contents ='#!/usr/local/bin/python\n# -*- coding: utf-42 -*-\nimport os, sys'
    174         sage: handle_encoding_declaration(contents, sys.stdout)
    175         # -*- coding: utf-42 -*-
    176         '#!/usr/local/bin/python\nimport os, sys'
    177 
    178 
    179     NOTES::
    180 
    181         PEP 263: http://www.python.org/dev/peps/pep-0263/
    182 
    183         PEP 263 says that Python will interpret a UTF-8 byte order mark
    184         as a declaration of UTF-8 encoding, but I don't think we do
    185         that; this function only sees a Python string so it can't
    186         account for a BOM.
    187 
    188         We default to UTF-8 encoding even though PEP 263 says that
    189         Python files should default to ASCII.
    190 
    191         Also see http://docs.python.org/ref/encodings.html.
    192 
    193     AUTHORS::
    194 
    195         - Lars Fischer
    196         - Dan Drake (2010-12-08, rewrite for ticket #10440)
    197     """
    198     lines = contents.splitlines()
    199     for num, line in enumerate(lines[:2]):
    200         if re.search(r"coding[:=]\s*([-\w.]+)", line):
    201             out.write(line + '\n')
    202             return '\n'.join(lines[:num] + lines[(num+1):])
    203 
    204     # If we didn't find any encoding hints, use utf-8. This is not in
    205     # conformance with PEP 263, which says that Python files default to
    206     # ascii encoding.
    207     out.write("# -*- coding: utf-8 -*-\n")
    208     return contents
    209    
    210 def preparse_file_named_to_stream(name, out):
    211     r"""
    212     Preparse file named \code{name} (presumably a .sage file), outputting to
    213     stream \code{out}.
    214     """
    215     name = os.path.abspath(name)
    216     dir, _ = os.path.split(name)
    217     cur = os.path.abspath(os.curdir)
    218     os.chdir(dir)
    219     contents = open(name).read()
    220     contents = handle_encoding_declaration(contents, out)
    221     parsed = preparse_file(contents)
    222     os.chdir(cur)
    223     out.write("# -*- encoding: utf-8 -*-\n")
    224     out.write('#'*70+'\n')
    225     out.write('# This file was *autogenerated* from the file %s.\n' % name)
    226     out.write('#'*70+'\n')
    227     out.write(parsed)
    228 
    229 def preparse_file_named(name):
    230     r"""
    231     Preparse file named \code{name} (presumably a .sage file), outputting to a
    232     temporary file.  Returns name of temporary file.
    233     """
    234     import sage.misc.misc
    235     name = os.path.abspath(name)
    236     tmpfilename = sage.misc.misc.tmp_filename('preparse', ext='.py')
    237     out = open(tmpfilename,'w')
    238     preparse_file_named_to_stream(name, out)
    239     out.close()
    240     return tmpfilename
    241 
    24289def embedded():
    24390    """
    24491    Returns True if Sage is being run from the notebook.
  • sage/misc/preparser.py

    diff --git a/sage/misc/preparser.py b/sage/misc/preparser.py
    a b def reset_load_attach_path(): 
    14801480# user can modify the path with the function load_attach_path.
    14811481reset_load_attach_path()
    14821482
     1483def load_cython(name):
     1484    import cython
     1485    cur = os.path.abspath(os.curdir)
     1486    try:
     1487        mod, dir  = cython.cython(name, compile_message=True, use_cache=True)
     1488    except (IOError, OSError, RuntimeError), msg:
     1489        print "Error compiling cython file:\n%s"%msg
     1490        return ''
     1491    import sys
     1492    sys.path.append(dir)
     1493    return 'from %s import *'%mod
     1494
     1495def handle_encoding_declaration(contents, out):
     1496    r"""Find a PEP 263-style Python encoding declaration in the first or
     1497    second line of `contents`. If found, output it to `out` and return
     1498    `contents` without the encoding line; otherwise output a default
     1499    UTF-8 declaration and return `contents`.
     1500
     1501    EXAMPLES::
     1502
     1503        sage: from sage.misc.preparser import handle_encoding_declaration
     1504        sage: import sys
     1505        sage: c1='# -*- coding: latin-1 -*-\nimport os, sys\n...'
     1506        sage: c2='# -*- coding: iso-8859-15 -*-\nimport os, sys\n...'
     1507        sage: c3='# -*- coding: ascii -*-\nimport os, sys\n...'
     1508        sage: c4='import os, sys\n...'
     1509        sage: handle_encoding_declaration(c1, sys.stdout)
     1510        # -*- coding: latin-1 -*-
     1511        'import os, sys\n...'
     1512        sage: handle_encoding_declaration(c2, sys.stdout)
     1513        # -*- coding: iso-8859-15 -*-
     1514        'import os, sys\n...'
     1515        sage: handle_encoding_declaration(c3, sys.stdout)
     1516        # -*- coding: ascii -*-
     1517        'import os, sys\n...'
     1518        sage: handle_encoding_declaration(c4, sys.stdout)
     1519        # -*- coding: utf-8 -*-
     1520        'import os, sys\n...'
     1521
     1522    TESTS:
     1523
     1524    These are some of the tests listed in PEP 263::
     1525
     1526        sage: contents = '#!/usr/bin/python\n# -*- coding: latin-1 -*-\nimport os, sys'
     1527        sage: handle_encoding_declaration(contents, sys.stdout)
     1528        # -*- coding: latin-1 -*-
     1529        '#!/usr/bin/python\nimport os, sys'
     1530
     1531        sage: contents = '# This Python file uses the following encoding: utf-8\nimport os, sys'
     1532        sage: handle_encoding_declaration(contents, sys.stdout)
     1533        # This Python file uses the following encoding: utf-8
     1534        'import os, sys'
     1535
     1536        sage: contents = '#!/usr/local/bin/python\n# coding: latin-1\nimport os, sys'
     1537        sage: handle_encoding_declaration(contents, sys.stdout)
     1538        # coding: latin-1
     1539        '#!/usr/local/bin/python\nimport os, sys'
     1540
     1541    Two hash marks are okay; this shows up in SageTeX-generated scripts::
     1542
     1543        sage: contents = '## -*- coding: utf-8 -*-\nimport os, sys\nprint x'
     1544        sage: handle_encoding_declaration(contents, sys.stdout)
     1545        ## -*- coding: utf-8 -*-
     1546        'import os, sys\nprint x'
     1547
     1548    When the encoding declaration doesn't match the specification, we
     1549    spit out a default UTF-8 encoding.
     1550
     1551    Incorrect coding line::
     1552
     1553        sage: contents = '#!/usr/local/bin/python\n# latin-1\nimport os, sys'
     1554        sage: handle_encoding_declaration(contents, sys.stdout)
     1555        # -*- coding: utf-8 -*-
     1556        '#!/usr/local/bin/python\n# latin-1\nimport os, sys'
     1557
     1558    Encoding declaration not on first or second line::
     1559
     1560        sage: contents ='#!/usr/local/bin/python\n#\n# -*- coding: latin-1 -*-\nimport os, sys'
     1561        sage: handle_encoding_declaration(contents, sys.stdout)
     1562        # -*- coding: utf-8 -*-
     1563        '#!/usr/local/bin/python\n#\n# -*- coding: latin-1 -*-\nimport os, sys'
     1564       
     1565    We don't check for legal encoding names; that's Python's job::
     1566
     1567        sage: contents ='#!/usr/local/bin/python\n# -*- coding: utf-42 -*-\nimport os, sys'
     1568        sage: handle_encoding_declaration(contents, sys.stdout)
     1569        # -*- coding: utf-42 -*-
     1570        '#!/usr/local/bin/python\nimport os, sys'
     1571
     1572
     1573    NOTES::
     1574
     1575        PEP 263: http://www.python.org/dev/peps/pep-0263/
     1576
     1577        PEP 263 says that Python will interpret a UTF-8 byte order mark
     1578        as a declaration of UTF-8 encoding, but I don't think we do
     1579        that; this function only sees a Python string so it can't
     1580        account for a BOM.
     1581
     1582        We default to UTF-8 encoding even though PEP 263 says that
     1583        Python files should default to ASCII.
     1584
     1585        Also see http://docs.python.org/ref/encodings.html.
     1586
     1587    AUTHORS::
     1588
     1589        - Lars Fischer
     1590        - Dan Drake (2010-12-08, rewrite for ticket #10440)
     1591    """
     1592    lines = contents.splitlines()
     1593    for num, line in enumerate(lines[:2]):
     1594        if re.search(r"coding[:=]\s*([-\w.]+)", line):
     1595            out.write(line + '\n')
     1596            return '\n'.join(lines[:num] + lines[(num+1):])
     1597
     1598    # If we didn't find any encoding hints, use utf-8. This is not in
     1599    # conformance with PEP 263, which says that Python files default to
     1600    # ascii encoding.
     1601    out.write("# -*- coding: utf-8 -*-\n")
     1602    return contents
     1603   
     1604def preparse_file_named_to_stream(name, out):
     1605    r"""
     1606    Preparse file named \code{name} (presumably a .sage file), outputting to
     1607    stream \code{out}.
     1608    """
     1609    name = os.path.abspath(name)
     1610    dir, _ = os.path.split(name)
     1611    cur = os.path.abspath(os.curdir)
     1612    os.chdir(dir)
     1613    contents = open(name).read()
     1614    contents = handle_encoding_declaration(contents, out)
     1615    parsed = preparse_file(contents)
     1616    os.chdir(cur)
     1617    out.write("# -*- encoding: utf-8 -*-\n")
     1618    out.write('#'*70+'\n')
     1619    out.write('# This file was *autogenerated* from the file %s.\n' % name)
     1620    out.write('#'*70+'\n')
     1621    out.write(parsed)
     1622
     1623def preparse_file_named(name):
     1624    r"""
     1625    Preparse file named \code{name} (presumably a .sage file), outputting to a
     1626    temporary file.  Returns name of temporary file.
     1627    """
     1628    import sage.misc.misc
     1629    name = os.path.abspath(name)
     1630    tmpfilename = os.path.abspath(sage.misc.misc.tmp_filename(name) + ".py")
     1631    out = open(tmpfilename,'w')
     1632    preparse_file_named_to_stream(name, out)
     1633    out.close()
     1634    return tmpfilename
     1635
    14831636def load(filename, globals, attach=False):
    14841637    """
    14851638    Executes a file in the scope given by ``globals``.  The
    def load(filename, globals, attach=False 
    16561809            # code snippets. Use preparse_file_named to make
    16571810            # the file name appear in the traceback as well.
    16581811            # See Trac 11812.
    1659             from sage.misc.interpreter import preparse_file_named         
    16601812            execfile(preparse_file_named(fpath), globals)
    16611813        else:
    16621814            # Preparse in memory only for speed.
    16631815            exec(preparse_file(open(fpath).read()) + "\n", globals)
    16641816    elif fpath.endswith('.spyx') or fpath.endswith('.pyx'):
    1665         import interpreter
    1666         exec(interpreter.load_cython(fpath), globals)
     1817        exec(load_cython(fpath), globals)
    16671818    elif fpath.endswith('.m'):
    16681819        # Assume magma for now, though maybe .m is used by maple and
    16691820        # mathematica too, and we should really analyze the file