Fix containing_block
First of all, the documentation sage.repl.preparse.containing_block
function is unclear. Second, it seems to return wrong results.
I suppose that containing_block(s, idx)
is supposed to contain a,b
such that s[a:b]
is the smallest substring with matching parentheses containing idx
.
Actually the role of idx
is not stated in the documentation. From some tests, it seems that we will have a<idx
. The following seems wrong to me:
s[a:b]
actually is not always balanced:
sage: from sage.repl.preparse import containing_block
sage: containing_block('((a))',1)
(0, 4)
sage: '((a))'[0:4]
'((a)'
sage: containing_block('([a)',1)
(0, 4)
sage: '([a)'[0:4]
'([a)'
sage: containing_block('(a])',1)
(0, 4)
sage: '(a])'[0:4]
'(a])'
 Some corner cases:
sage: containing_block('()',1)
File "<string>", line unknown
SyntaxError: Unbalanced or missing ()'s
sage: containing_block('()',0)
File "<string>", line unknown
SyntaxError: Unbalanced or missing ()'s
Suggested fix:
 The specification should be:
s[a:b]
is minimal balanced (in particular we have s[a]
and opening and s[b1]
a closing bracket)
a<=idx<b
(in particular, the char at position idx
is part of s[a:b]
)
 The specification should be documented.
Change History (10)
Description: 
modified (diff)

Description: 
modified (diff)

Description: 
modified (diff)

Branch: 
→ u/SimonKing/fix_containing_block

Authors: 
→ Simon King

Commit: 
→ b282e6d408d73e0c2e488ed6ed4a6c59790e3dfe

Status: 
new →
needs_review

Commit: 
b282e6d408d73e0c2e488ed6ed4a6c59790e3dfe →
3c01d712a8e763bb1e922a515307d853d6deb22b

Reviewers: 
→ Travis Scrimshaw

Status: 
needs_review →
positive_review

Branch: 
u/SimonKing/fix_containing_block →
3c01d712a8e763bb1e922a515307d853d6deb22b

Resolution: 
→ fixed

Status: 
positive_review →
closed

By the way: Why is there no proper traceback when
containing_block
raises a SyntaxError?