( )⚙ D5994 cleanup: prefer nested context managers to \-continuations

This is an archive of the discontinued Mercurial Phabricator instance.

cleanup: prefer nested context managers to \-continuations
ClosedPublic

Authored by durin42 on Feb 20 2019, 7:33 PM.

Details

Summary

I'd prefer Python accept a tuple of context managers, but alas it
isn't meant to be. This will have to suffice.

Diff Detail

Repository
rHG Mercurial
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

durin42 created this revision.Feb 20 2019, 7:33 PM

I'd prefer Python accept a tuple of context managers

I think I'd also prefer if our context managers didn't acquire the resource in __init__. That would let us write it with less indentation as:

wlock = wlock or util.nullcontextmanager()
lock = lock or util.nullcontextmanager()
trmanager = pushop.trmanager or util.nullcontextmanager()
with wlock, lock, trmanager:

(In this case we probably could do that anyway since we don't seem to worry about the risk of exceptions from the time we take the lock until we enter the context manager.)

I don't care much either way about the patch itself. I'll wait a little to see if someone else cares more, but otherwise I'll take.

I'd prefer Python accept a tuple of context managers

I think I'd also prefer if our context managers didn't acquire the resource in __init__. That would let us write it with less indentation as:

wlock = wlock or util.nullcontextmanager()
lock = lock or util.nullcontextmanager()
trmanager = pushop.trmanager or util.nullcontextmanager()
with wlock, lock, trmanager:

(In this case we probably could do that anyway since we don't seem to worry about the risk of exceptions from the time we take the lock until we enter the context manager.)
I don't care much either way about the patch itself. I'll wait a little to see if someone else cares more, but otherwise I'll take.

That sounds like a good cleanup anyway - probably taking the resource is a vestige of when we were on around Python 2.3 and mpm had this discipline of using del to free things like locks. It worked, but isn't idiomatic today (I'm honestly not sure if it was then - but mpm is where I learned it.)

This revision was automatically updated to reflect the committed changes.