Skip to content

Commit 24a58b8

Browse files
committed
Add "and" and "or" to eval?
by strippign out the "and" or "or"
1 parent b3fd91d commit 24a58b8

3 files changed

Lines changed: 89 additions & 77 deletions

File tree

test/unit/test-lib-eval.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
'Unit test for trepan.lib.eval'
2+
"Unit test for trepan.lib.eval"
33
import unittest
44

55
from trepan.lib import eval as Meval
@@ -8,18 +8,23 @@
88
class TestExtractExpression(unittest.TestCase):
99
def test_extract_expression(self):
1010
for fragment, expect in (
11-
('if condition(x):', 'condition(x)'),
12-
('elif is_magic(name):', 'is_magic(name)'),
13-
('while expression:', 'expression'),
14-
('for i in range(3):', 'range(3)'),
15-
('abc = 123', '123'),
16-
('assert True', 'True'),
17-
('return return_value', 'return_value'),
18-
('nothing_to_be.done', 'nothing_to_be.done'), ):
19-
self.assertEqual(expect , Meval.extract_expression(fragment))
11+
("if condition(x):", "condition(x)"),
12+
("elif is_magic(name):", "is_magic(name)"),
13+
("while expression:", "expression"),
14+
("for i in range(3):", "range(3)"),
15+
("and x > 3", "x > 3"),
16+
("or y < 3", "y < 3"),
17+
("abc = 123", "123"),
18+
("assert True", "True"),
19+
("return return_value", "return_value"),
20+
("nothing_to_be.done", "nothing_to_be.done"),
21+
):
22+
self.assertEqual(expect, Meval.extract_expression(fragment))
2023
pass
2124
pass
25+
2226
pass
2327

24-
if __name__ == '__main__':
28+
29+
if __name__ == "__main__":
2530
unittest.main()

trepan/lib/eval.py

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright (C) 2012-2014 Rocky Bernstein <rocky@gnu.org>
2+
# Copyright (C) 2012-2014, 2020 Rocky Bernstein <rocky@gnu.org>
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU General Public License as published by
@@ -13,47 +13,56 @@
1313
#
1414
# You should have received a copy of the GNU General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
16-
'''Things related to eval/exec'''
16+
"""Things related to eval/exec"""
1717

1818
# extract the "expression" part of a line of source code.
1919
#
2020
import re
2121

2222

2323
def extract_expression(text):
24-
if re.search('^\s*(?:if|elif)\s+', text):
25-
text = re.sub('^\s*(?:if|elif)\s+', '', text)
26-
text = re.sub(':(?:\s+.*$|$)', '', text)
27-
elif re.search('^\s*assert\s+.*', text):
24+
if re.search("^\s*(?:if|elif)\s+", text):
25+
text = re.sub("^\s*(?:if|elif)\s+", "", text)
26+
text = re.sub(":(?:\s+.*$|$)", "", text)
27+
elif re.search("^\s*assert\s+.*", text):
2828
# EXPR in : assert EXPRESSION:
29-
text = re.sub('^\s*assert\s+', '', text)
30-
elif re.search('^\s*(?:while)\s+', text):
31-
text = re.sub('^\s*(?:while)\s+', '', text)
32-
text = re.sub(':(?:\s+.*$|$)', '', text)
33-
elif re.search('^\s*return\s+', text):
29+
text = re.sub("^\s*assert\s+", "", text)
30+
elif re.search("^\s*(?:while)\s+", text):
31+
text = re.sub("^\s*(?:while)\s+", "", text)
32+
text = re.sub(":(?:\s+.*$|$)", "", text)
33+
elif re.search("^\s*return\s+", text):
3434
# EXPRESION in: return EXPRESSION
35-
text = re.sub('^\s*return\s+', '', text)
36-
elif re.search('^\s*for\s+.+\s+in\s+.*:', text):
35+
text = re.sub("^\s*return\s+", "", text)
36+
elif re.search("^\s*for\s+.+\s+in\s+.*:", text):
3737
# EXPRESION in: for VAR in EXPRESSION:
38-
text = re.sub('^\s*for\s+.+\s+in\s+', '', text)
39-
text = re.sub(':.*$', '', text)
40-
elif re.search('\s*[A-Za-z_][A-Za-z0-9_\[\]]*\s*=[^=>]', text):
38+
text = re.sub("^\s*for\s+.+\s+in\s+", "", text)
39+
text = re.sub(":.*$", "", text)
40+
elif re.search("^\s*and\s+.*", text):
41+
# EXPRESION in: and EXPRESSION
42+
text = re.sub("^\s*and\s+", "", text)
43+
elif re.search("^\s*or\s+.*", text):
44+
# EXPRESION in: and EXPRESSION
45+
text = re.sub("^\s*or\s+", "", text)
46+
elif re.search("\s*[A-Za-z_][A-Za-z0-9_\[\]]*\s*=[^=>]", text):
4147
# RHS of an assignment statement.
42-
text = re.sub('^\s*[A-Za-z_][A-Za-z0-9_\[\]]*\s*=\s*', '', text)
48+
text = re.sub("^\s*[A-Za-z_][A-Za-z0-9_\[\]]*\s*=\s*", "", text)
4349
pass
4450
return text
4551

4652

4753
# Demo it
48-
if __name__=='__main__':
54+
if __name__ == "__main__":
4955
for stmt in (
50-
'if condition(x):',
51-
'elif _is_magic(name):',
52-
'while expression:',
53-
'for i in range(3):',
54-
'abc = 123',
55-
'return return_value',
56-
'nothing_to_be.done'):
56+
"if condition(x):",
57+
"elif _is_magic(name):",
58+
"while expression:",
59+
"for i in range(3):",
60+
"and x > 3",
61+
"or y < 3",
62+
"abc = 123",
63+
"return return_value",
64+
"nothing_to_be.done",
65+
):
5766
print(extract_expression(stmt))
5867
pass
5968
pass

trepan/processor/command/eval.py

Lines changed: 39 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright (C) 2012-2013, 2015, 2017 Rocky Bernstein
2+
# Copyright (C) 2012-2013, 2015, 2017, 2020 Rocky Bernstein
33
#
44
# This program is free software: you can redistribute it and/or modify
55
# it under the terms of the GNU General Public License as published by
@@ -14,75 +14,73 @@
1414
# You should have received a copy of the GNU General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17-
import os
18-
1917
# Our local modules
20-
from trepan.processor.command import base_cmd as Mbase_cmd
21-
from trepan.lib import eval as Meval
18+
from trepan.processor.command.base_cmd import DebuggerCommand
19+
from trepan.lib.eval import extract_expression
2220

2321

24-
class EvalCommand(Mbase_cmd.DebuggerCommand):
22+
class EvalCommand(DebuggerCommand):
2523
"""**eval** *python-statement*
2624
27-
Run *python-statement* in the context of the current frame.
25+
Run *python-statement* in the context of the current frame.
26+
27+
If no string is given, we run the string from the current source code
28+
about to be run. If the command ends `?` (via an alias) and no string is
29+
given, the following translations occur:
2830
29-
If no string is given, we run the string from the current source code
30-
about to be run. If the command ends `?` (via an alias) and no string is
31-
given, the following translations occur:
31+
assert = <expr> => <expr>
32+
{if|elif} <expr> : => <expr>
33+
while <expr> : => <expr>
34+
return <expr> => <expr>
35+
for <var> in <expr> : => <expr>
36+
and <expr> => <expr>
37+
or <expr> => <expr>
38+
<var> = <expr> => <expr>
3239
33-
assert = <expr> => <expr>
34-
{if|elif} <expr> : => <expr>
35-
while <expr> : => <expr>
36-
return <expr> => <expr>
37-
for <var> in <expr> : => <expr>
38-
<var> = <expr> => <expr>
40+
The above is done via regular expression matching. No fancy parsing is
41+
done, say, to look to see if *expr* is split across a line or whether
42+
var an assignment might have multiple variables on the left-hand side.
3943
40-
The above is done via regular expression matching. No fancy parsing is
41-
done, say, to look to see if *expr* is split across a line or whether
42-
var an assignment might have multiple variables on the left-hand side.
44+
Examples:
45+
---------
4346
44-
Examples:
45-
---------
47+
eval 1+2 # 3
48+
eval # Run current source-code line
49+
eval? # but strips off leading 'if', 'while', ..
50+
# from command
4651
47-
eval 1+2 # 3
48-
eval # Run current source-code line
49-
eval? # but strips off leading 'if', 'while', ..
50-
# from command
52+
See also:
53+
---------
5154
52-
See also:
53-
---------
55+
`deval`, `set autoeval`, `pr`, `pp` and `examine`."""
5456

55-
`deval`, `set autoeval`, `pr`, `pp` and `examine`.
56-
"""
57-
aliases = ('eval?', '?')
58-
category = 'data'
59-
min_args = 0
60-
max_args = None
61-
name = os.path.basename(__file__).split('.')[0]
62-
need_stack = True
63-
short_help = 'Print value of expression EXP'
57+
aliases = ("eval?", "?")
58+
short_help = "Print value of expression EXP"
59+
60+
DebuggerCommand.setup(locals(), category="data", need_stack=True)
6461

6562
def run(self, args):
6663
if 1 == len(args):
6764
if self.proc.current_source_text:
68-
text = self.proc.current_source_text.rstrip('\n')
69-
if '?' == args[0][-1]:
70-
text = Meval.extract_expression(text)
65+
text = self.proc.current_source_text.rstrip("\n")
66+
if "?" == args[0][-1]:
67+
text = extract_expression(text)
7168
self.msg("eval: %s" % text)
7269
pass
7370
else:
7471
self.errmsg("Don't have program source text")
7572
return
7673
else:
77-
text = self.proc.current_command[len(self.proc.cmd_name):]
74+
text = self.proc.current_command[len(self.proc.cmd_name) :]
7875
pass
7976
text = text.strip()
8077
try:
8178
self.proc.exec_line(text)
8279
except:
8380
pass
8481

85-
if __name__ == '__main__':
82+
83+
if __name__ == "__main__":
8684
import inspect
8785
from trepan import debugger
8886
d = debugger.Debugger()

0 commit comments

Comments
 (0)