Skip to content

Commit ad6ceff

Browse files
committed
Fix for issue #109 (bpython.inspection.parsekeywordpairs).
1 parent dff9dd7 commit ad6ceff

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

bpython/inspection.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
import keyword
2828
import pydoc
2929
import re
30-
import sys
3130
import types
32-
from itertools import dropwhile
3331

3432
from pygments.token import Token
3533

@@ -115,36 +113,31 @@ def __repr__(self):
115113

116114
def parsekeywordpairs(signature):
117115
tokens = PythonLexer().get_tokens(signature)
116+
preamble = True
118117
stack = []
119118
substack = []
120119
parendepth = 0
121-
begin = False
122120
for token, value in tokens:
123-
if not begin:
124-
if token is Token.Punctuation and value == u'(':
125-
begin = True
121+
if preamble:
122+
if token is Token.Punctuation and value == u"(":
123+
preamble = False
126124
continue
127125

128126
if token is Token.Punctuation:
129-
if value == u'(':
127+
if value in [u'(', u'{', u'[']:
130128
parendepth += 1
131-
elif value == u')':
129+
elif value in [u')', u'}', u']']:
132130
parendepth -= 1
133131
elif value == ':' and parendepth == -1:
134132
# End of signature reached
135133
break
134+
if ((value == ',' and parendepth == 0) or
135+
(value == ')' and parendepth == -1)):
136+
stack.append(substack)
137+
substack = []
138+
continue
136139

137-
if parendepth > 0:
138-
substack.append(value)
139-
continue
140-
141-
if (token is Token.Punctuation and
142-
(value == ',' or (value == ')' and parendepth == -1))):
143-
stack.append(substack[:])
144-
del substack[:]
145-
continue
146-
147-
if value and value.strip():
140+
if value and (parendepth > 0 or value.strip()):
148141
substack.append(value)
149142

150143
d = {}

bpython/test/test_inspection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def spam():
3131
self.assertFalse(inspection.is_callable(None))
3232

3333
def test_parsekeywordpairs(self):
34+
# See issue #109
3435
def fails(spam=['-a', '-b']):
3536
pass
3637

0 commit comments

Comments
 (0)