Skip to content

Commit

Permalink
[WIP] keep_log >= 2 restricts the number of lines in the log
Browse files Browse the repository at this point in the history
  • Loading branch information
tueda committed Aug 25, 2015
1 parent 8abc9ee commit 38dfd79
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 24 deletions.
21 changes: 13 additions & 8 deletions form/form.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""A module to run FORM programs from Python."""

import collections
import errno
import fcntl
import os
Expand Down Expand Up @@ -121,12 +122,13 @@ def open(self, args=None, keep_log=False):
with form.open() as formlink:
# use formlink ...
The optional argument "args" is for the FORM command, a string or
The optional argument `args` is for the FORM command, a string or
a sequence of strings. For example '/path/to/form' or ['tform', '-w4'].
The default value is 'form'.
The other argument "keep_log" indicates the log from FORM is kept
and used as detailed information when an error occurs.
The other argument `keep_log` indicates the log from FORM is kept
and used as detailed information when an error occurs. If its value
is >= 2, it specifies the maximum number of lines for the scrollback.
The default value is False.
"""
if args is None:
Expand Down Expand Up @@ -184,7 +186,10 @@ def open(self, args=None, keep_log=False):

self._closed = False
if keep_log:
self._log = []
if keep_log >= 2:
self._log = collections.deque(maxlen=keep_log)
else:
self._log = []
else:
self._log = None
self._childpid = pid
Expand Down Expand Up @@ -357,16 +362,16 @@ def read(self, *names):
if s:
i = s.rfind('\n')
if i >= 0:
for msg in s[:i].split('\n'):
msgs = s[:i].split('\n')
if self._log is not None:
self._log.extend(msgs)
for msg in msgs:
if msg.find('-->') >= 0 or msg.find('==>') >= 0:
if self._log:
msg += '\n'
msg += '\n'.join(self._log)
msg += '\n' + s[:i]
self.close()
raise RuntimeError(msg)
if not self._log is None:
self._log.append(s[:i])
self._loggingin.unread(s[i+1:])
if self._parentin in r:
out += (self._parentin.read()
Expand Down
68 changes: 52 additions & 16 deletions form/tests/test_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,33 +120,69 @@ def test_many_times(self):
self.assertEqual(int(f.read('$x')), n)

def test_keep_log(self):
script = '''
On stats;
S x;
L F = (1+x)^2;
;* 1
;* 2
;* 3
;* 4
;* 5
;* 6
;* 7
;* 8
;* 9
;* 10
L G = (1-x)^2;
;* 1
;* 2
;* 3
;* 4
;* 5
;* 6
;* 7
;* 8
;* 9
;* 10
.sort
'''

with form.open() as f:
msg = None
f.write('''
On stats;
S x;
L F = (1+x)^2;
.sort
''')
f.write(script)
try:
f.read('G')
f.read('X')
except RuntimeError as e:
msg = str(e)
self.assertTrue(msg is not None and msg.find('L F = (1+x)^2;') < 0)
self.assertTrue(msg is not None)
# Neither F nor G appears in `msg`.
self.assertTrue(msg.find('L F = (1+x)^2;') < 0)
self.assertTrue(msg.find('L G = (1-x)^2;') < 0)

with form.open(keep_log=True) as f:
msg = None
f.write('''
On stats;
S x;
L F = (1+x)^2;
.sort
''')
f.write(script)
try:
f.read('X')
except RuntimeError as e:
msg = str(e)
self.assertTrue(msg is not None)
# Both F and G appear in `msg`.
self.assertTrue(msg.find('L F = (1+x)^2;') >= 0)
self.assertTrue(msg.find('L G = (1-x)^2;') >= 0)

with form.open(keep_log=30) as f:
msg = None
f.write(script)
try:
f.read('G')
f.read('X')
except RuntimeError as e:
msg = str(e)
self.assertTrue(msg is not None and msg.find('L F = (1+x)^2;') >= 0)
self.assertTrue(msg is not None)
# G is still in `msg' but F is not.
self.assertTrue(msg.find('L F = (1+x)^2;') < 0)
self.assertTrue(msg.find('L G = (1-x)^2;') >= 0)

def test_seq_args(self):
with form.open() as f:
Expand Down

0 comments on commit 38dfd79

Please sign in to comment.