[Python-ideas] Re: os.workdir() context manager

2021-10-22 Thread Marc-Andre Lemburg
Just as an update to this thread: The workdir context manager got implemented for Python 3.11 and now lives in contextlib as contextlib.chdir(): https://github.com/python/cpython/commit/3592980f9122ab0d9ed93711347742d110b749c2 Thanks to everyone who contributed to the thread, the SC for

[Python-ideas] Re: os.workdir() context manager

2021-09-16 Thread Jeremiah Paige
On Wed, Sep 15, 2021 at 4:20 AM Steven D'Aprano wrote: > On Wed, Sep 15, 2021 at 08:53:21PM +1000, Chris Angelico wrote: > > > Sometimes there is no valid path to something, > > I don't understand what you mean by that. If there is no valid path to > something, where exactly are you cd'ing to,

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Steven D'Aprano
On Wed, Sep 15, 2021 at 12:02:04PM -0700, Guido van Rossum wrote: > To make chdir() return a context manager *and* keep it working without > calling `__enter__`, it would have to call `getcwd()`, which I've heard is > expensive. > > So I don't think that would work, alas. How expensive would it

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Marc-Andre Lemburg
On 15.09.2021 21:02, Guido van Rossum wrote: > To make chdir() return a context manager *and* keep it working without calling > `__enter__`, it would have to call `getcwd()`, which I've heard is expensive. > > So I don't think that would work, alas. At least on Linux, the cost for os.getcwd() is

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Guido van Rossum
To make chdir() return a context manager *and* keep it working without calling `__enter__`, it would have to call `getcwd()`, which I've heard is expensive. So I don't think that would work, alas. On Wed, Sep 15, 2021 at 11:55 AM Eric V. Smith wrote: > On 9/15/2021 2:48 PM, Eric Fahlgren

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Eric V. Smith
On 9/15/2021 2:48 PM, Eric Fahlgren wrote: On Wed, Sep 15, 2021 at 12:21 AM Eric V. Smith > wrote: And I'm not crazy about the name "workdir". To me, it sounds like it returns something, not sets and resets something. But I don't have a particularly great

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Eric Fahlgren
On Wed, Sep 15, 2021 at 12:21 AM Eric V. Smith wrote: > And I'm not crazy about the name "workdir". To me, it sounds like it > returns something, not sets and resets something. But I don't have a > particularly great alternative in mind: in my own code I've used > "change_dir", which isn't

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Joao S. O. Bueno
On Tue, 14 Sept 2021 at 22:59, David Mertz, Ph.D. wrote: > I think this would be convenient. > > And yes, it's not thread safe. But neither is os.chdir() to start with. > Someone whose script, or library, wants to chdir can already shoot > themselves in the foot. This makes that slightly less

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Filipe Laíns
On Tue, 2021-09-14 at 21:43 +0200, Marc-Andre Lemburg wrote: > Hello all, > > I sometimes write Python scripts which need to work in specific > work directories. > > When putting such code into functions, the outer function typically > does not expect the current work dir (CWD) to be changed, so

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Alex Waygood
I also really like this idea. Though I wonder if it might be a better fit for `contextlib` rather than `os`? Can see arguments both ways (and it's a minor detail, anyway). Alex > On 14 Sep 2021, at 20:55, Paul Moore wrote: > > On Tue, 14 Sept 2021 at 20:44, Marc-Andre Lemburg wrote: >> >>

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Chris Angelico
On Wed, Sep 15, 2021 at 9:22 PM Steven D'Aprano wrote: > > On Wed, Sep 15, 2021 at 08:53:21PM +1000, Chris Angelico wrote: > > > Sometimes there is no valid path to something, > > I don't understand what you mean by that. If there is no valid path to > something, where exactly are you cd'ing to,

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Steven D'Aprano
On Wed, Sep 15, 2021 at 08:53:21PM +1000, Chris Angelico wrote: > Sometimes there is no valid path to something, I don't understand what you mean by that. If there is no valid path to something, where exactly are you cd'ing to, and how does it help you with the thing that has no valid path to

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Eryk Sun
On 9/15/21, Paul Moore wrote: > > Just a somewhat off-topic note, but dir_fd arguments are only > supported on Unix, and the functionality only appears to be present at > the NT Kernel level on Windows, not in the Windows API. Handle-relative paths are supported by all NT system calls that

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Steven D'Aprano
On Wed, Sep 15, 2021 at 11:56:17AM +0200, Marc-Andre Lemburg wrote: > > - The context manager could be made more elaborate, e.g. adding optional > > logging for debugging purposes. > > We could add a logger parameter to the context manager for > this to customize the logger, or simply use the

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Chris Angelico
On Wed, Sep 15, 2021 at 8:41 PM Christian Heimes wrote: > > On 15/09/2021 11.56, Marc-Andre Lemburg wrote: > > - Chris mentioned that library code should not be changing the > > CWD. In an ideal world, I'd agree, but there are cases where > > you don't have an option and you need to change

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Serhiy Storchaka
14.09.21 22:43, Marc-Andre Lemburg пише: > Would there be interest in adding something like this to the os module > as os.workdir() ? There is already an open issue and PR for this: https://bugs.python.org/issue25625 https://github.com/python/cpython/pull/28271 My main concern is that it is not

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Christian Heimes
On 15/09/2021 11.56, Marc-Andre Lemburg wrote: > - Chris mentioned that library code should not be changing the > CWD. In an ideal world, I'd agree, but there are cases where > you don't have an option and you need to change the CWD in order > make certain things work, e.g. you don't control

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Marc-Andre Lemburg
On 15.09.2021 00:16, Guido van Rossum wrote: > Here I think we need to drop our perfectionist attitude. When I saw > Marc-Andre's > proposal my first response was also "but what about threads." But really, > os.chdir() is the culprit here, and since it's a syscall we can't fix it. If > we > can

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Eric V. Smith
> On Sep 15, 2021, at 4:11 AM, Christian Heimes wrote: > > On 15/09/2021 09.21, Eric V. Smith wrote: >>> On 9/15/2021 3:02 AM, Christian Heimes wrote: >>> On 15/09/2021 01.55, Guido van Rossum wrote: I know where I'd file a bug. :-) "Bug magnet" is an extremely subjective

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Christian Heimes
On 15/09/2021 09.21, Eric V. Smith wrote: > On 9/15/2021 3:02 AM, Christian Heimes wrote: >> On 15/09/2021 01.55, Guido van Rossum wrote: >>> I know where I'd file a bug. :-) >>> >>> "Bug magnet" is an extremely subjective pejorative term. When the >>> *better* way to do things (os.workdir()) is

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Paul Moore
On Wed, 15 Sept 2021 at 08:02, Christian Heimes wrote: > The "better way" to handle current working directory is to use the > modern *at() variants of syscalls, e.g. openat() instead open(). The > variants take an additional file descriptor dirfd that is used as the > current working directory

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Eric V. Smith
On 9/15/2021 3:02 AM, Christian Heimes wrote: On 15/09/2021 01.55, Guido van Rossum wrote: I know where I'd file a bug. :-) "Bug magnet" is an extremely subjective pejorative term. When the *better* way to do things (os.workdir()) is harder than the *easy* way to do (os.chdir()), which is the

[Python-ideas] Re: os.workdir() context manager

2021-09-15 Thread Christian Heimes
On 15/09/2021 01.55, Guido van Rossum wrote: > I know where I'd file a bug. :-) > > "Bug magnet" is an extremely subjective pejorative term. When the > *better* way to do things (os.workdir()) is harder than the *easy* way > to do (os.chdir()), which is the real bug magnet? The "better way" to

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread David Mertz, Ph.D.
On Tue, Sep 14, 2021, 11:17 PM Finn Mason wrote: > I disagree. The way I see it, `pathlib` is home of *Path(). No functions. > Just *Path(). Then again, I didn't write pathlib. I could be > misunderstanding the intent. > When I wrote "in" I was thinking more of a method than a function. E.g.

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Finn Mason
On Tue, Sep 14, 2021, 7:58 PM David Mertz, Ph.D. wrote: > I think this would be convenient. > > And yes, it's not thread safe. But neither is os.chdir() to start with. > Someone whose script, or library, wants to chdir can already shoot > themselves in the foot. This makes that slightly less

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread David Mertz, Ph.D.
I think this would be convenient. And yes, it's not thread safe. But neither is os.chdir() to start with. Someone whose script, or library, wants to chdir can already shoot themselves in the foot. This makes that slightly less likely, not more. In terms of the bikeshed color, I think putting

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Guido van Rossum
For the stdlib context managers that I know of, all-lowercase seems the convention. Would it even be a class? The simplest implementation would use contextlib.contextmanager (unless that’s a undesirable dependency for os.py). —Guido On Tue, Sep 14, 2021 at 17:53 Finn Mason wrote: > BTW, should

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Finn Mason
BTW, should it be `WorkDir` instead of `workdir` because it's a class, or would that be too inconsistent? On Tue, Sep 14, 2021, 6:47 PM Finn Mason wrote: > > On Tue, Sep 14, 2021, 5:36 PM Cameron Simpson wrote: > >> On 15Sep2021 07:50, Chris Angelico wrote: >> >On Wed, Sep 15, 2021 at 7:43 AM

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Finn Mason
On Tue, Sep 14, 2021, 5:36 PM Cameron Simpson wrote: > On 15Sep2021 07:50, Chris Angelico wrote: > >On Wed, Sep 15, 2021 at 7:43 AM Cameron Simpson wrote: > >> I know I'm atypical, but I have quite a lot of multithreaded stuff, > >> including command line code. So while it'd be ok to avoid

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Guido van Rossum
On Tue, Sep 14, 2021 at 4:36 PM Cameron Simpson wrote: > On 15Sep2021 07:50, Chris Angelico wrote: > >On Wed, Sep 15, 2021 at 7:43 AM Cameron Simpson wrote: > >> I know I'm atypical, but I have quite a lot of multithreaded stuff, > >> including command line code. So while it'd be ok to avoid

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Chris Angelico
On Wed, Sep 15, 2021 at 9:36 AM Cameron Simpson wrote: > > On 15Sep2021 07:50, Chris Angelico wrote: > >On Wed, Sep 15, 2021 at 7:43 AM Cameron Simpson wrote: > >> I know I'm atypical, but I have quite a lot of multithreaded stuff, > >> including command line code. So while it'd be ok to avoid

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Cameron Simpson
On 14Sep2021 15:16, Guido van Rossum wrote: >Here I think we need to drop our perfectionist attitude. When I saw >Marc-Andre's proposal my first response was also "but what about threads." >But really, os.chdir() is the culprit here, and since it's a syscall we >can't fix it. If we can live with

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Cameron Simpson
On 15Sep2021 07:50, Chris Angelico wrote: >On Wed, Sep 15, 2021 at 7:43 AM Cameron Simpson wrote: >> I know I'm atypical, but I have quite a lot of multithreaded stuff, >> including command line code. So while it'd be ok to avoid this context >> manager for my own code, I fear library modules,

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Guido van Rossum
Here I think we need to drop our perfectionist attitude. When I saw Marc-Andre's proposal my first response was also "but what about threads." But really, os.chdir() is the culprit here, and since it's a syscall we can't fix it. If we can live with that, we can live with the proposed os.workdir().

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Chris Angelico
On Wed, Sep 15, 2021 at 7:43 AM Cameron Simpson wrote: > > On 14Sep2021 21:43, M.-A. Lemburg wrote: > >- The context manager is not thread safe. There's no thread safe model > > for the current work dir. OTOH, scripts usually don't use threads, > > so not a big deal. > > This is the source of

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Cameron Simpson
On 14Sep2021 21:43, M.-A. Lemburg wrote: >- The context manager is not thread safe. There's no thread safe model > for the current work dir. OTOH, scripts usually don't use threads, > so not a big deal. This is the source of my concerns. Though of course it applies to any process global

[Python-ideas] Re: os.workdir() context manager

2021-09-14 Thread Paul Moore
On Tue, 14 Sept 2021 at 20:44, Marc-Andre Lemburg wrote: > > I sometimes write Python scripts which need to work in specific > work directories. > > When putting such code into functions, the outer function typically > does not expect the current work dir (CWD) to be changed, so wrap the > code