Ticket #11000: trac_11000.patch

File trac_11000.patch, 4.3 KB (added by iandrus, 2 years ago)
  • sage/interfaces/gap.py

    # HG changeset patch
    # User Ivan Andrus <darthandrus@gmail.com>
    # Date 1300913176 25200
    # Node ID 352b532747174b653da5e3bb315dc8dcb48d8076
    # Parent  482cc7fe2b262e532d369db1a45c6e51f4749b13
    #11000: Fix GAP's handling of multiple lines and comments
    
    diff --git a/sage/interfaces/gap.py b/sage/interfaces/gap.py
    a b  
    311311        if x == 'fail': 
    312312            raise RuntimeError, 'Error loading Gap package %s'%pkg 
    313313 
    314     def eval(self, x, newlines=False, strip=True, **kwds): 
     314    def eval(self, x, newlines=False, strip=True, split_lines=True, **kwds): 
    315315        r""" 
    316316        Send the code in the string s to the GAP interpreter and return the 
    317317        output as a string. 
    318          
     318 
    319319        INPUT: 
    320          
    321          
     320 
     321 
    322322        -  ``s`` - string containing GAP code. 
    323          
     323 
    324324        -  ``newlines`` - bool (default: True); if False, 
    325325           remove all backslash-newlines inserted by the GAP output 
    326326           formatter. 
    327          
     327 
    328328        -  ``strip`` - ignored 
    329          
    330          
     329 
     330        -  ``split_lines`` -- bool (default: True); if True then each 
     331           line is evaluated separately.  If False, then the whole 
     332           block of code is evaluated all at once. 
     333 
    331334        EXAMPLES:: 
    332          
     335 
    333336            sage: gap.eval('2+2') 
    334337            '4' 
    335338            sage: gap.eval('Print(4); #test\n Print(6);') 
     
    338341            '#6' 
    339342            sage: gap.eval('4; \n 6;') 
    340343            '4\n6' 
     344            sage: gap.eval('if 3>2 then\nPrint("hi");\nfi;') 
     345            'hi' 
     346            sage: gap.eval('## this is a test\nPrint("OK")') 
     347            'OK' 
     348            sage: gap.eval('Print("This is a test. Oh no, a #");# but this is a comment\nPrint("OK")') 
     349            'This is a test. Oh no, a #OK' 
     350            sage: gap.eval('if 4>3 then') 
     351            '' 
     352            sage: gap.eval('Print("Hi how are you?")') 
     353            'Hi how are you?' 
     354            sage: gap.eval('fi') 
     355            '' 
    341356        """ 
     357        # '" 
    342358        #We remove all of the comments:  On each line, we try 
    343359        #to find a pound sign.  If we find it, we check to see if 
    344360        #it is occurring in a string.  If it is not in a string, we 
    345361        #strip off the comment. 
    346         input_line = "" 
    347         for line in  str(x).rstrip().split('\n'): 
    348             pound_position = line.rfind('#') 
    349             if pound_position != -1 and not is_in_string(line, pound_position): 
    350                 line = line[:pound_position] 
    351             input_line += line 
    352         if not input_line.endswith(';'): 
    353             input_line += ';' 
     362        if not split_lines: 
     363            input_line=str(x) 
     364        else: 
     365            input_line = "" 
     366            for line in str(x).rstrip().split('\n'): 
     367                pound_position = line.find('#') 
     368                while pound_position != -1: 
     369                    if not is_in_string(line, pound_position): 
     370                        line = line[:pound_position] 
     371                    pound_position = line.find('#',pound_position+1) 
     372                input_line += " "+line 
     373            if not input_line.endswith(';'): 
     374                input_line += ';' 
    354375        result = Expect.eval(self, input_line, **kwds) 
    355376        if not newlines: 
    356377            result = result.replace("\\\n","") 
     
    469490                    raise RuntimeError, "%s produced error output\n%s\n   executing %s"%(self, error,line) 
    470491                if len(normal) == 0: 
    471492                    return '' 
    472                  
    473                 if isinstance(wait_for_prompt, str): 
     493 
     494                if isinstance(wait_for_prompt, str) and normal.ends_with(wait_for_prompt): 
    474495                    n = len(wait_for_prompt) 
     496                elif normal.endswith(self._prompt): 
     497                    n = len(self._prompt) 
     498                elif normal.endswith(self._continuation_prompt()): 
     499                    n = len(self._continuation_prompt()) 
    475500                else: 
    476                     n = len(self._prompt) 
     501                    n = 0 
    477502                out = normal[:-n] 
    478503                if len(out) > 0 and out[-1] == "\n": 
    479504                    out = out[:-1] 
    480505                return out 
    481              
     506 
    482507            except (RuntimeError,),message: 
    483508                if 'EOF' in message: 
    484509                    print "** %s crashed or quit executing '%s' **"%(self, line)