X Tutup
Skip to content

Commit 02d62a9

Browse files
committed
Fix argument position when lambdas are used in arguments.
Closes #158.
1 parent 2d78935 commit 02d62a9

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

bpython/repl.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,15 @@ def get_args(self):
473473
stack[-1][1] += 1
474474
except TypeError:
475475
stack[-1][1] = ''
476+
elif value == ':' and stack[-1][2] == 'lambda':
477+
stack.pop()
476478
elif (token is Token.Name or token in Token.Name.subtypes or
477479
token is Token.Operator and value == '.'):
478480
stack[-1][0] += value
479481
elif token is Token.Operator and value == '=':
480482
stack[-1][1] = stack[-1][0]
483+
elif token is Token.Keyword and value == 'lambda':
484+
stack.append(['', 0, value])
481485
else:
482486
stack[-1][0] = ''
483487
while stack[-1][2] in '[{':

bpython/test/test_repl.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import os
12
import unittest
23
from itertools import islice
34

4-
from bpython import repl
5+
from bpython import config, repl
56

67

78
class TestHistory(unittest.TestCase):
@@ -129,6 +130,34 @@ def test_update(self):
129130
self.assertNotEqual(list(slice), self.matches)
130131
self.assertEqual(list(newslice), newmatches)
131132

133+
class TestRepl(repl.Repl):
134+
def __init__(self):
135+
config_struct = config.Struct()
136+
config.loadini(config_struct, os.devnull)
137+
repl.Repl.__init__(self, repl.Interpreter(), config_struct)
138+
self.input_line = ""
139+
140+
def current_line(self):
141+
return self.input_line
142+
143+
class TestArgspec(unittest.TestCase):
144+
def setUp(self):
145+
self.repl = TestRepl()
146+
self.repl.push("def spam(a, b, c):\n", False)
147+
self.repl.push(" pass\n", False)
148+
self.repl.push("\n", False)
149+
150+
def setInputLine(self, line):
151+
"""Set current input line of the test REPL."""
152+
self.repl.input_line = line
153+
154+
def test_lambda_position(self):
155+
self.setInputLine("spam(lambda a, b: 1, ")
156+
self.assertTrue(self.repl.get_args())
157+
self.assertTrue(self.repl.argspec)
158+
# Argument position
159+
self.assertEqual(self.repl.argspec[3], 1)
160+
132161

133162
if __name__ == '__main__':
134163
unittest.main()

0 commit comments

Comments
 (0)
X Tutup