Summary
Codon's current with implementation diverges from Python in several ways.
This issue tracks three compatibility gaps.
1. as binds the class object, not __enter__()'s return value
In Python:
class Manager:
def __enter__(self):
return (1, 2)
def __exit__(self, exc_type, exc, tb):
return None
with Manager() as pair:
assert pair == (1, 2)
pair receives the return value of __enter__() method.
In current Codon, Identifier pair is not assigned tuple (1, 2). Instead, an instance of class Manager itself is assigned. So below code is more likely to work:
class Manager:
def __enter__(self):
return None
def __exit__(self):
return None
def total(self):
return 3
with Manager() as pair:
assert pair.total() == 3
# pair must be “None” Object.
# But Actually, pair.total() works.
print("PASS classes_and_with")
This causes a clear Python compatibility gap.
2. with ... as ... only accepts a single identifier target
Python allows assignment targets such as tuple unpacking:
Current Codon grammar only accepts an identifier after as, so forms like these are rejected:
with Manager() as (x, y):
with Manager() as obj.attr:
with Manager() as arr[i]:
So the code below is not working.
class Manager:
def __enter__(self):
return (1, 2)
def __exit__(self):
return None
with Manager() as (x, y):
assert x + y == 3
print("PASS with_tuple_unpack_target_python_compat")
The restriction appears in codon/parser/peg/grammar.peg , and the current WithStmt AST representation also only preserves a single identifier-like binding target.
This means the limitation is not only in parsing, but also in AST representation and lowering.
3. __exit__ does not receive Python-style exception information
Python context managers receive:
__exit__(exc_type, exc, tb)
and may suppress exceptions by returning a truthy value.
Current Codon lowering calls __exit__() without exception arguments and does not model Python's suppression behavior. This means Python-style context manager cleanup/error handling semantics are not preserved.
But this likely requires more than a local with lowering change, as it touches the exception model, traceback representation, and with error-path lowering.
This item seems closely related to issue #758.
Why these matter
As you can see in interop pattern of #758, These differences make Codon's with syntax look Python-compatible on the surface while behaving differently in meaningful cases. That can lead to subtle surprises when porting Python code or reasoning about context manager behavior.
Summary
Codon's current
withimplementation diverges from Python in several ways.This issue tracks three compatibility gaps.
1.
asbinds theclass object, not__enter__()'s return valueIn Python:
pairreceives thereturn value of __enter__() method.In current Codon, Identifier
pairis not assigned tuple (1, 2). Instead, an instance ofclass Manageritself is assigned. So below code is more likely to work:This causes a clear Python compatibility gap.
2.
with ... as ...only accepts a single identifier targetPython allows assignment targets such as tuple unpacking:
Current Codon grammar
only accepts an identifierafter as, so forms like these are rejected:So the code below is not working.
The restriction appears in
codon/parser/peg/grammar.peg, and the currentWithStmt ASTrepresentation also only preserves a single identifier-like binding target.This means the limitation is not only in parsing, but also in AST representation and lowering.
3.
__exit__does not receive Python-style exception informationPython context managers receive:
and may suppress exceptions by returning a truthy value.
Current Codon lowering calls
__exit__()without exception arguments and does not model Python's suppression behavior. This means Python-style context manager cleanup/error handling semantics are not preserved.But this likely requires more than a local with lowering change, as it touches the exception model, traceback representation, and with error-path lowering.
This item seems closely related to issue #758.
Why these matter
As you can see in interop pattern of #758, These differences make Codon's with syntax look Python-compatible on the surface while behaving differently in meaningful cases. That can lead to subtle surprises when porting Python code or reasoning about context manager behavior.