Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TemporaryFile name returns an integer in python3 #62095

Closed
jtaylor mannequin opened this issue May 3, 2013 · 5 comments
Closed

TemporaryFile name returns an integer in python3 #62095

jtaylor mannequin opened this issue May 3, 2013 · 5 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@jtaylor
Copy link
Mannequin

jtaylor mannequin commented May 3, 2013

BPO 17895
Nosy @birkenfeld, @terryjreedy, @ncoghlan, @pitrou, @bitdancer, @serhiy-storchaka

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2013-05-10.19:01:23.346>
created_at = <Date 2013-05-03.16:54:17.138>
labels = ['type-feature', 'library']
title = 'TemporaryFile name returns an integer in python3'
updated_at = <Date 2013-05-10.19:01:23.344>
user = 'https://bugs.python.org/jtaylor'

bugs.python.org fields:

activity = <Date 2013-05-10.19:01:23.344>
actor = 'terry.reedy'
assignee = 'none'
closed = True
closed_date = <Date 2013-05-10.19:01:23.346>
closer = 'terry.reedy'
components = ['Library (Lib)']
creation = <Date 2013-05-03.16:54:17.138>
creator = 'jtaylor'
dependencies = []
files = []
hgrepos = []
issue_num = 17895
keywords = []
message_count = 5.0
messages = ['188305', '188311', '188314', '188316', '188865']
nosy_count = 7.0
nosy_names = ['georg.brandl', 'terry.reedy', 'ncoghlan', 'pitrou', 'r.david.murray', 'jtaylor', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = 'rejected'
stage = None
status = 'closed'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue17895'
versions = ['Python 3.3']

@jtaylor
Copy link
Mannequin Author

jtaylor mannequin commented May 3, 2013

sys.version_info(major=3, minor=3, micro=1, releaselevel='final', serial=0)
In [3]: type(tempfile.TemporaryFile().name)
Out[3]: builtins.int

in python2 it returned a string, this is a somewhat pointless api change which breaks some third party code, e.g. numpy (numpy/numpy#3302)

@jtaylor jtaylor mannequin added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir labels May 3, 2013
@bitdancer
Copy link
Member

The 'name' attribute of TemporaryFile is not part of the API. It happens to exist only because the underlying file object has a 'name' attribute. On posix platforms the value is not really useful for anything. In other words, that numpy code was buggy to start with, the bug was just hidden by the fact that in python2 name happened to be a string ('<fdopen>'), and nobody looked at the result. What numpy was doing with it produced a nonsense value, but I guess nobody noticed.

Now, that said, I don't know why the value changed between Python2 and Python3, and that might conceivably be a bug of some sort. I'm guessing it is a consequence of the IO system rewrite and is not a bug per-se, but it might also be that there are improvements that could be made here.

@pitrou
Copy link
Member

pitrou commented May 3, 2013

Given the following property of TemporaryFile:

"Under Unix, the directory entry for the file is removed immediately after the file is created. Other platforms do not support this; your code should not rely on a temporary file created using this function having or not having a visible name in the file system."
(from http://docs.python.org/dev/library/tempfile.html#tempfile.TemporaryFile)

I find it absurd to expect the name attribute to return an actual filename, since it won't exist anymore by the time you use it (under Unix, at least). So, we could return an invalid filename, but I don't see the point, and the current behaviour is as good as any other.

If you want a usable name, by definition you must use NamedTemporaryFile. Recommend closing.

@serhiy-storchaka
Copy link
Member

http://docs.python.org/3/library/io.html#io.FileIO.name

The file name. This is the file descriptor of the file when no name is given in the constructor.

@terryjreedy
Copy link
Member

Given that the changed behavior is documented and pretty obviously intentional, it is not a bug. The proposal would have to be that we 'enhance' 3.4 *or later* by reverting back to the 2.x -- and possibly break 3.0 to 3.3 code. We are not going to do that without extremely good reason.

For a nameless file, using the integer fd as a 'name' seems more truthful than '<fdopen>'. I believe the latter can be a actual filename on *nix. On Windows, while Explorer rejects it, there may be ways to bypass the check. The integer fd is also more useful than None and 'isintance(x.name, int)' is as easy to check as 'x.name is None'.

@terryjreedy terryjreedy added type-feature A feature request or enhancement and removed type-bug An unexpected behavior, bug, or error labels May 10, 2013
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

4 participants