# 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
|
|
| 311 | 311 | if x == 'fail': |
| 312 | 312 | raise RuntimeError, 'Error loading Gap package %s'%pkg |
| 313 | 313 | |
| 314 | | def eval(self, x, newlines=False, strip=True, **kwds): |
| | 314 | def eval(self, x, newlines=False, strip=True, split_lines=True, **kwds): |
| 315 | 315 | r""" |
| 316 | 316 | Send the code in the string s to the GAP interpreter and return the |
| 317 | 317 | output as a string. |
| 318 | | |
| | 318 | |
| 319 | 319 | INPUT: |
| 320 | | |
| 321 | | |
| | 320 | |
| | 321 | |
| 322 | 322 | - ``s`` - string containing GAP code. |
| 323 | | |
| | 323 | |
| 324 | 324 | - ``newlines`` - bool (default: True); if False, |
| 325 | 325 | remove all backslash-newlines inserted by the GAP output |
| 326 | 326 | formatter. |
| 327 | | |
| | 327 | |
| 328 | 328 | - ``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 | |
| 331 | 334 | EXAMPLES:: |
| 332 | | |
| | 335 | |
| 333 | 336 | sage: gap.eval('2+2') |
| 334 | 337 | '4' |
| 335 | 338 | sage: gap.eval('Print(4); #test\n Print(6);') |
| … |
… |
|
| 338 | 341 | '#6' |
| 339 | 342 | sage: gap.eval('4; \n 6;') |
| 340 | 343 | '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 | '' |
| 341 | 356 | """ |
| | 357 | # '" |
| 342 | 358 | #We remove all of the comments: On each line, we try |
| 343 | 359 | #to find a pound sign. If we find it, we check to see if |
| 344 | 360 | #it is occurring in a string. If it is not in a string, we |
| 345 | 361 | #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 += ';' |
| 354 | 375 | result = Expect.eval(self, input_line, **kwds) |
| 355 | 376 | if not newlines: |
| 356 | 377 | result = result.replace("\\\n","") |
| … |
… |
|
| 469 | 490 | raise RuntimeError, "%s produced error output\n%s\n executing %s"%(self, error,line) |
| 470 | 491 | if len(normal) == 0: |
| 471 | 492 | return '' |
| 472 | | |
| 473 | | if isinstance(wait_for_prompt, str): |
| | 493 | |
| | 494 | if isinstance(wait_for_prompt, str) and normal.ends_with(wait_for_prompt): |
| 474 | 495 | 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()) |
| 475 | 500 | else: |
| 476 | | n = len(self._prompt) |
| | 501 | n = 0 |
| 477 | 502 | out = normal[:-n] |
| 478 | 503 | if len(out) > 0 and out[-1] == "\n": |
| 479 | 504 | out = out[:-1] |
| 480 | 505 | return out |
| 481 | | |
| | 506 | |
| 482 | 507 | except (RuntimeError,),message: |
| 483 | 508 | if 'EOF' in message: |
| 484 | 509 | print "** %s crashed or quit executing '%s' **"%(self, line) |