Context to automatically close something at the end of a block. Code like this: with closing( .open( )) as f: is equivalent to this: f = .open( ) try: finally: f.close()
| 340 | |
| 341 | |
| 342 | class closing(AbstractContextManager): |
| 343 | """Context to automatically close something at the end of a block. |
| 344 | |
| 345 | Code like this: |
| 346 | |
| 347 | with closing(<module>.open(<arguments>)) as f: |
| 348 | <block> |
| 349 | |
| 350 | is equivalent to this: |
| 351 | |
| 352 | f = <module>.open(<arguments>) |
| 353 | try: |
| 354 | <block> |
| 355 | finally: |
| 356 | f.close() |
| 357 | |
| 358 | """ |
| 359 | def __init__(self, thing): |
| 360 | self.thing = thing |
| 361 | def __enter__(self): |
| 362 | return self.thing |
| 363 | def __exit__(self, *exc_info): |
| 364 | self.thing.close() |
| 365 | |
| 366 | |
| 367 | class aclosing(AbstractAsyncContextManager): |
no outgoing calls
searching dependent graphs…