Skip to content

Commit cb5d78c

Browse files
committed
eval? now does "and" and "or"...
By stripping off the "and" or "or"
1 parent 613bb1e commit cb5d78c

3 files changed

Lines changed: 73 additions & 58 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 python3
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: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,36 +22,37 @@
2222
class EvalCommand(DebuggerCommand):
2323
"""**eval** *python-statement*
2424
25-
Run *python-statement* in the context of the current frame.
25+
Run *python-statement* in the context of the current frame.
2626
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:
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:
3030
31-
assert = <expr> => <expr>
32-
{if|elif} <expr> : => <expr>
33-
while <expr> : => <expr>
34-
return <expr> => <expr>
35-
for <var> in <expr> : => <expr>
36-
<var> = <expr> => <expr>
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>
3739
38-
The above is done via regular expression matching. No fancy parsing is
39-
done, say, to look to see if *expr* is split across a line or whether
40-
var an assignment might have multiple variables on the left-hand side.
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.
4143
42-
Examples:
43-
---------
44+
Examples:
45+
---------
4446
45-
eval 1+2 # 3
46-
eval # Run current source-code line
47-
eval? # but strips off leading 'if', 'while', ..
48-
# from command
47+
eval 1+2 # 3
48+
eval # Run current source-code line
49+
eval? # but strips off leading 'if', 'while', ..
50+
# from command
4951
50-
See also:
51-
---------
52+
See also:
53+
---------
5254
53-
`deval`, `set autoeval`, `pr`, `pp` and `examine`.
54-
"""
55+
`deval`, `set autoeval`, `pr`, `pp` and `examine`."""
5556

5657
aliases = ("eval?", "?")
5758
short_help = "Print value of expression EXP"

0 commit comments

Comments
 (0)