Skip to content

Commit 8989679

Browse files
karthik2804dicej
andauthored
switch types generated for resources to be context managers (#56)
* switch types generated for resources to be context managers Co-authored-by: Joel Dice <joel.dice@fermyon.com> Signed-off-by: karthik2804 <karthik.ganeshram@fermyon.com> * update poll_loop.py to use `__exit__` instead of `drop` Signed-off-by: Joel Dice <joel.dice@fermyon.com> * remove obsolete TODO comment Signed-off-by: Joel Dice <joel.dice@fermyon.com> --------- Signed-off-by: karthik2804 <karthik.ganeshram@fermyon.com> Signed-off-by: Joel Dice <joel.dice@fermyon.com> Co-authored-by: Joel Dice <joel.dice@fermyon.com>
1 parent 2f4808f commit 8989679

3 files changed

Lines changed: 15 additions & 12 deletions

File tree

bundled/poll_loop.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ async def next(self) -> Optional[bytes]:
6363
except Err as e:
6464
if isinstance(e.value, StreamErrorClosed):
6565
if self.stream is not None:
66-
self.stream.drop()
66+
self.stream.__exit__()
6767
self.stream = None
6868
if self.body is not None:
6969
IncomingBody.finish(self.body)
@@ -102,7 +102,7 @@ async def send(self, chunk: bytes):
102102
def close(self):
103103
"""Close the stream, indicating no further data will be written."""
104104

105-
self.stream.drop()
105+
self.stream.__exit__()
106106
self.stream = None
107107
OutgoingBody.finish(self.body, None)
108108
self.body = None
@@ -140,7 +140,7 @@ def run_until_complete(self, future):
140140

141141
for (ready, pollable), waker in zip(zip(ready, pollables), wakers):
142142
if ready:
143-
pollable.drop()
143+
pollable.__exit__()
144144
waker.set_result(None)
145145
else:
146146
new_wakers.append((pollable, waker))

src/summary.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,27 +1214,28 @@ class {camel}(Flag):
12141214
}
12151215
})
12161216
.map(method)
1217-
// TODO: make resource classes context managers per
1218-
// https://docs.python.org/3/reference/datamodel.html#context-managers and call this
1219-
// `__exit__`:
12201217
.chain(iter::once({
12211218
let newline = '\n';
12221219
let indent = " ";
12231220
let doc = "Release this resource.";
12241221
let docs =
12251222
format!(r#""""{newline}{indent}{doc}{newline}{indent}"""{newline}{indent}"#);
1226-
1223+
let enter = r#"
1224+
def __enter__(self):
1225+
"""Returns self"""
1226+
return self
1227+
"#;
12271228
if stub_runtime_calls {
12281229
format!(
1229-
"
1230-
def drop(self):
1230+
"{enter}
1231+
def __exit__(self, *args):
12311232
{docs}raise NotImplementedError
12321233
"
12331234
)
12341235
} else {
12351236
format!(
1236-
"
1237-
def drop(self):
1237+
"{enter}
1238+
def __exit__(self, *args):
12381239
{docs}(_, func, args, _) = self.finalizer.detach()
12391240
self.handle = None
12401241
func(args[0], args[1])

src/test/tests.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ class Thing(resource_import_and_export.Thing):
183183
184184
@staticmethod
185185
def baz(a: Self, b: Self) -> Self:
186-
return Thing(HostThing.baz(a.value, b.value).foo() + 9)
186+
with HostThing.baz(a.value, b.value) as bar:
187+
value = bar.foo()
188+
return Thing(value + 9)
187189
"#,
188190
),
189191
(

0 commit comments

Comments
 (0)