[Python-ideas] Define __fspath__() for NamedTemporaryFile and TemporaryDirectory

Brett Cannon brett at python.org
Thu Jun 8 15:22:25 EDT 2017


On Thu, 8 Jun 2017 at 08:27 Ethan Furman <ethan at stoneleaf.us> wrote:

> On 06/08/2017 06:42 AM, Antoine Pietri wrote:
> > Hello everyone!
> >
> > A very common pattern when dealing with temporary files is code like
> this:
> >
> >      with tempfile.TemporaryDirectory() as tmpdir:
> >          tmp_path = tmpdir.name
> >
> >          os.chmod(tmp_path)
> >          os.foobar(tmp_path)
> >          open(tmp_path).read(barquux)
> >
> > PEP 519 (https://www.python.org/dev/peps/pep-0519/) introduced the
> > concept of "path-like objects", objects that define a __fspath__()
> > method. Most of the standard library has been adapted so that the
> > functions accept path-like objects.
> >
> > My proposal is to define __fspath__() for TemporaryDirectory and
> > NamedTemporaryFile so that we can pass those directly to the library
> > functions instead of having to use the .name attribute explicitely.
> >
> > Thoughts? :-)
>
> Good idea.  Check bugs.python.org to see if there is already an issue for
> this, and if not create one! :)
>

Already exists, been discussed, and rejected:
https://bugs.python.org/issue29447 .
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170608/f17d5a70/attachment.html>


More information about the Python-ideas mailing list