[Python-ideas] chdir context manager (original) (raw)

Terry Reedy tjreedy at udel.edu
Sat Jan 19 19:07:09 CET 2013


On 1/19/2013 10:06 AM, Daniel Shahaf wrote:

Terry Reedy wrote on Sat, Jan 19, 2013 at 08:37:17 -0500:

On 1/19/2013 5:10 AM, Daniel Shahaf wrote:

The following is a common pattern (used by, for example, shutil.makearchive):

savecwd = os.getcwd() try: foo() finally: os.chdir(savecwd) I suggest this deserves a context manager: with savedcwd(): foo() So to me, your proposal is only 1/2 or 2/3 of a context manager. (And 'returns an open file descriptor for the saved directory' seems backward or wrong for a context manager.) It does not actually make a new What should enter return, then? It could return None, the to-be-restored directory's file descriptor, or the newly-changed-to directory (once a "directory to chdir to" optional argument is added). The latter could be either a pathname (string) or a file descriptor (since it's just passed through to os.chdir). It seems to me returning the old dir's fd would be the most useful of the three option, since the other two are things callers already have --- None, which is global, and the argument to the context manager.

make_archive would prefer the old dir pathname, as it wants that for the logging call. But I do not think that that should drive design.

context. A proper tempcwd context manager should have one parameter, the new working directory, with chdir(newcwd) in the enter method. To allow for conditional switching, the two chdir system calls could be conditional on newcwd (either None or '' would mean no chdir calls).

I think making the newcwd argument optional would be useful if the context manager body does multiple chdir() calls: with savedcwd(): os.chdir('/foo') dosomething() os.chdir('/bar') dosomething() I'm not sure if that's exactly what you suggest --- you seem to be suggesting that savedcwd(None) will avoid calling fchdir() from exit()?

I was, but that is a non-essential optimization. My idea is basically similar to Bueno's except for parameter absent versus None (and the two cases could be handled differently).

I think this proposal suffers a bit from being both too specific and too general. Eli explained the 'too specific' part: there are many things that might be changed and changed back. The 'too general' part is that specific applications need different specific details. There are various possibilities of what to do in and return from enter.

However, given the strong -1 from at least three core developers and one other person, the detail seem moot.

-- Terry Jan Reedy

-- Terry Jan Reedy



More information about the Python-ideas mailing list