Re: tempname.mktemp functionality deprecation

2017-05-02 Thread Cameron Simpson
On 01May2017 14:31, Tim Chase wrote: On 2017-05-01 18:40, Gregory Ewing wrote: The following function should be immune to race conditions and doesn't use mktemp. [loop trying names until os.link does not fail die to an existing name] Ah, this is a good alternative and solves the problem at h

Re: tempname.mktemp functionality deprecation

2017-05-01 Thread Tim Chase
On 2017-05-01 18:40, Gregory Ewing wrote: > The following function should be immune to race conditions > and doesn't use mktemp. > > def templink(destpath): > """Create a hard link to the given file with a unique name. > Returns the name of the link.""" > pid = os.getpid() > i

Re: tempname.mktemp functionality deprecation

2017-05-01 Thread eryk sun
On Sat, Apr 29, 2017 at 6:45 PM, Tim Chase wrote: > Working on some deduplication code, I want do my my best at > performing an atomic re-hard-linking atop an existing file, akin to > "ln -f source.txt dest.txt" > > However, when I issue > > os.link("source.txt", "dest.txt") > > it fails with an

Re: tempname.mktemp functionality deprecation

2017-04-30 Thread Gregory Ewing
The following function should be immune to race conditions and doesn't use mktemp. def templink(destpath): """Create a hard link to the given file with a unique name. Returns the name of the link.""" pid = os.getpid() i = 1 while True: linkpath = "%s-%s-%s" % (destpath

Re: tempname.mktemp functionality deprecation

2017-04-30 Thread Tim Chase
On 2017-05-01 08:41, Cameron Simpson wrote: > On 30Apr2017 06:52, Tim Chase wrote: > >> > - use a GUID-named temp-file instead for less chance of > >> > collision? > > You could, but mktemp is supposed to robustly perform that task, > versus "very very probably". Though with the potential of its

Re: tempname.mktemp functionality deprecation

2017-04-30 Thread Tim Chase
On 2017-05-01 09:15, Ben Finney wrote: > I reported this – for a different use case – in issue26362 [0] > https://bugs.python.org/issue26362>. > > The suggested solutions in the documentation do not address the use > case described there; and they do not address the use case you've > described her

Re: tempname.mktemp functionality deprecation

2017-04-30 Thread Dan Stromberg
On Sat, Apr 29, 2017 at 11:45 AM, Tim Chase wrote: > Working on some deduplication code, I want do my my best at > performing an atomic re-hard-linking atop an existing file, akin to > "ln -f source.txt dest.txt" > > However, when I issue > > os.link("source.txt", "dest.txt") > > it fails with a

Re: tempname.mktemp functionality deprecation

2017-04-30 Thread Ben Finney
Tim Chase writes: > Unfortunately, tempfile.mktemp() is described as deprecated since 2.3 > (though appears to still exist in the 3.4.2 that is the default Py3 on > Debian Stable). While the deprecation notice says "In version 2.3 of > Python, this module was overhauled for enhanced security. It

Re: tempname.mktemp functionality deprecation

2017-04-30 Thread Cameron Simpson
On 30Apr2017 06:52, Tim Chase wrote: On 2017-04-29 20:51, Devin Jeanpierre wrote: On Sat, Apr 29, 2017 at 11:45 AM, Tim Chase wrote > So which route should I pursue? > - go ahead and use tempfile.mktemp() ignoring the deprecation? I'd be tempted to. But... > - use a GUID-named temp-file ins

Re: tempname.mktemp functionality deprecation

2017-04-30 Thread Tim Chase
On 2017-04-29 20:51, Devin Jeanpierre wrote: > On Sat, Apr 29, 2017 at 11:45 AM, Tim Chase wrote > > So which route should I pursue? > > > > - go ahead and use tempfile.mktemp() ignoring the deprecation? > > > > - use a GUID-named temp-file instead for less chance of collision? > > > > - I happen t

Re: tempname.mktemp functionality deprecation

2017-04-29 Thread Gregory Ewing
Devin Jeanpierre wrote: I vote the last one: you can read the .name attribute of the returned file(-like) object from NamedTemporaryFile to get a path to a file, which can be passed to other functions. I don't think that helps. You would have to delete the file first before you could create a l

Re: tempname.mktemp functionality deprecation

2017-04-29 Thread Chris Angelico
On Sun, Apr 30, 2017 at 1:51 PM, Devin Jeanpierre wrote: > I guess ideally, one would use linkat instead of os.link[*], but that's > platform-specific and not exposed in Python AFAIK. It is actually - src_dir_fd and dst_dir_fd. https://docs.python.org/3/library/os.html#os.link ChrisA -- https:

Re: tempname.mktemp functionality deprecation

2017-04-29 Thread Devin Jeanpierre
On Sat, Apr 29, 2017 at 11:45 AM, Tim Chase wrote: > Unfortunately, tempfile.mktemp() is described as deprecated > since 2.3 (though appears to still exist in the 3.4.2 that is the > default Py3 on Debian Stable). While the deprecation notice says > "In version 2.3 of Python, this module was overh

tempname.mktemp functionality deprecation

2017-04-29 Thread Tim Chase
Working on some deduplication code, I want do my my best at performing an atomic re-hard-linking atop an existing file, akin to "ln -f source.txt dest.txt" However, when I issue os.link("source.txt", "dest.txt") it fails with an OSError (EEXISTS). This isn't surprising as it's documented. Un