Skip to content

Commit 3603cc5

Browse files
author
Victor Stinner
committed
Issue python#9425: PyFile_FromFd() ignores the name argument
This function is only by imp.find_module() which does return the filename in a separated variable.
1 parent 1a4d12d commit 3603cc5

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

Doc/c-api/file.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ the :mod:`io` APIs instead.
2222
Create a Python file object from the file descriptor of an already
2323
opened file *fd*. The arguments *name*, *encoding*, *errors* and *newline*
2424
can be *NULL* to use the defaults; *buffering* can be *-1* to use the
25-
default. Return *NULL* on failure. For a more comprehensive description of
26-
the arguments, please refer to the :func:`io.open` function documentation.
25+
default. *name* is ignored and kept for backward compatibility. Return
26+
*NULL* on failure. For a more comprehensive description of the arguments,
27+
please refer to the :func:`io.open` function documentation.
2728

2829
.. warning::
2930

3031
Since Python streams have their own buffering layer, mixing them with
3132
OS-level file descriptors can produce various issues (such as unexpected
3233
ordering of data).
3334

35+
.. versionchanged:: 3.2
36+
Ignore *name* attribute.
37+
3438

3539
.. cfunction:: int PyObject_AsFileDescriptor(PyObject *p)
3640

Objects/fileobject.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ PyObject *
2929
PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding,
3030
char *errors, char *newline, int closefd)
3131
{
32-
PyObject *io, *stream, *nameobj = NULL;
32+
PyObject *io, *stream;
3333

3434
io = PyImport_ImportModule("io");
3535
if (io == NULL)
@@ -40,16 +40,8 @@ PyFile_FromFd(int fd, char *name, char *mode, int buffering, char *encoding,
4040
Py_DECREF(io);
4141
if (stream == NULL)
4242
return NULL;
43-
if (name != NULL) {
44-
nameobj = PyUnicode_DecodeFSDefault(name);
45-
if (nameobj == NULL)
46-
PyErr_Clear();
47-
else {
48-
if (PyObject_SetAttrString(stream, "name", nameobj) < 0)
49-
PyErr_Clear();
50-
Py_DECREF(nameobj);
51-
}
52-
}
43+
/* ignore name attribute because the name attribute of _BufferedIOMixin
44+
and TextIOWrapper is read only */
5345
return stream;
5446
}
5547

0 commit comments

Comments
 (0)