Show More
@@ -278,6 +278,25 b' _help_end_re = re.compile(r"""(%{0,2}' | |||
|
278 | 278 | """, |
|
279 | 279 | re.VERBOSE) |
|
280 | 280 | |
|
281 | # Extra pseudotokens for multiline strings and data structures | |
|
282 | _MULTILINE_STRING = object() | |
|
283 | _MULTILINE_STRUCTURE = object() | |
|
284 | ||
|
285 | def _line_tokens(line): | |
|
286 | """Helper for has_comment and ends_in_comment_or_string.""" | |
|
287 | readline = StringIO(line).readline | |
|
288 | toktypes = set() | |
|
289 | try: | |
|
290 | for t in generate_tokens(readline): | |
|
291 | toktypes.add(t[0]) | |
|
292 | except TokenError as e: | |
|
293 | # There are only two cases where a TokenError is raised. | |
|
294 | if 'multi-line string' in e.args[0]: | |
|
295 | toktypes.add(_MULTILINE_STRING) | |
|
296 | else: | |
|
297 | toktypes.add(_MULTILINE_STRUCTURE) | |
|
298 | return toktypes | |
|
299 | ||
|
281 | 300 | def has_comment(src): |
|
282 | 301 | """Indicate whether an input line has (i.e. ends in, or is) a comment. |
|
283 | 302 | |
@@ -293,21 +312,31 b' def has_comment(src):' | |||
|
293 | 312 | comment : bool |
|
294 | 313 | True if source has a comment. |
|
295 | 314 | """ |
|
296 | readline = StringIO(src).readline | |
|
297 | toktypes = set() | |
|
298 | try: | |
|
299 | for t in generate_tokens(readline): | |
|
300 | toktypes.add(t[0]) | |
|
301 | except TokenError: | |
|
302 | pass | |
|
303 | return(tokenize2.COMMENT in toktypes) | |
|
315 | return (tokenize2.COMMENT in _line_tokens(src)) | |
|
304 | 316 | |
|
317 | def ends_in_comment_or_string(src): | |
|
318 | """Indicates whether or not an input line ends in a comment or within | |
|
319 | a multiline string. | |
|
320 | ||
|
321 | Parameters | |
|
322 | ---------- | |
|
323 | src : string | |
|
324 | A single line input string. | |
|
325 | ||
|
326 | Returns | |
|
327 | ------- | |
|
328 | comment : bool | |
|
329 | True if source ends in a comment or multiline string. | |
|
330 | """ | |
|
331 | toktypes = _line_tokens(src) | |
|
332 | return (tokenize2.COMMENT in toktypes) or (_MULTILINE_STRING in toktypes) | |
|
333 | ||
|
305 | 334 | |
|
306 | 335 | @StatelessInputTransformer.wrap |
|
307 | 336 | def help_end(line): |
|
308 | 337 | """Translate lines with ?/?? at the end""" |
|
309 | 338 | m = _help_end_re.search(line) |
|
310 |
if m is None or |
|
|
339 | if m is None or ends_in_comment_or_string(line): | |
|
311 | 340 | return line |
|
312 | 341 | target = m.group(1) |
|
313 | 342 | esc = m.group(3) |
General Comments 0
You need to be logged in to leave comments.
Login now