Ticket #9970: 9970_r_strings.patch

File 9970_r_strings.patch, 2.5 KB (added by wjp, 9 years ago)
  • sage/interfaces/r.py

    # HG changeset patch
    # User Willem Jan Palenstijn <wjp@usecode.org>
    # Date 1294960987 -3600
    # Node ID 0993227e6fb780db79a98c20ef70010c88310929
    # Parent  aa280a304e44caecf86c4de28d26eecfe51a2f4b
    #9970: Don't parse inside R strings
    
    diff -r aa280a304e44 -r 0993227e6fb7 sage/interfaces/r.py
    a b  
    11331133   
    11341134
    11351135# patterns for _sage_()
     1136rel_re_quotes = re.compile(r'''"(?:(?:\\.)|(?:[^"\\]))*"''')
     1137rel_re_quotes_only = re.compile(r'''""''')
    11361138rel_re_param = re.compile('\s([\w\.]+)\s=')
    11371139rel_re_xrange = re.compile('([\d]+):([\d]+)')
    11381140rel_re_integer = re.compile('([^\d])([\d]+)L')
     
    16551657            [('DATA', [1, 1.75, 3, 2.875, 4, 5]),
    16561658             ('_Names', ['Min.', '1st Qu.', 'Median', 'Mean', '3rd Qu.', 'Max.']),
    16571659             ('_r_class', 'table')]
     1660
     1661        TESTS::
     1662
     1663        Ensure we don't parse inside strings (#9970) ::
     1664
     1665            sage: r('"1L"')._sage_()
     1666            '1L'
     1667
    16581668        """
    16591669        self._check_valid()
    16601670        P = self.parent()
     
    16951705        # we want this in a single line
    16961706        exp.replace('\n','')
    16971707        exp = "".join(exp.split("\n"))
    1698        
     1708
     1709        # We replace every quoted string by "" to prevent
     1710        # any of the below substitutions from changing literal strings.
     1711        # The strings are stored in exp_strings, and are replaced below.
     1712        exp_strings = []
     1713        def _collect_strings(s):
     1714            exp_strings.insert(0, s.group())
     1715            return '""'
     1716        exp = rel_re_quotes.sub(_collect_strings, exp)
     1717
    16991718        # python compatible parameters
    17001719        exp = rel_re_param.sub(self._subs_dots, exp)
    17011720
     
    17031722        exp = re.sub(' class = "', ' _r_class = "',exp)
    17041723
    17051724        # Change 'structure' to '_r_structure'
    1706         # TODO: check that we are outside of quotes ""
    17071725        exp = re.sub(' structure\(', ' _r_structure(', exp)
    17081726        exp = re.sub('^structure\(', '_r_structure(', exp) #special case
    17091727
     
    17411759
    17421760        exp = self._convert_nested_r_list(exp)
    17431761
     1762        # Replace every instance of "" by the collected literal strings
     1763        # in exp_strings
     1764        def _replace_strings(s):
     1765            return exp_strings.pop()
     1766        exp = rel_re_quotes_only.sub(_replace_strings, exp)
     1767
    17441768        # Set up the globals
    17451769        globs = {'NA':None, 'NULL':None, 'FALSE':False, 'TRUE':True,
    17461770                 '_r_list':self._r_list, '_r_structure':self._r_structure,