Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bdbddf8

Browse files
committed
#2491: os.fdopen() is now almost an alias to the builtin open(), and accepts the same parameters.
It just checks that the first argument is a file descriptor.
1 parent e19cadb commit bdbddf8

3 files changed

Lines changed: 11 additions & 11 deletions

File tree

Lib/os.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -651,9 +651,9 @@ def __getattr__(self, name):
651651
def __iter__(self):
652652
return iter(self._stream)
653653

654-
# Supply os.fdopen() (used by subprocess!)
655-
def fdopen(fd, mode="r", buffering=-1):
654+
# Supply os.fdopen()
655+
def fdopen(fd, *args, **kwargs):
656656
if not isinstance(fd, int):
657657
raise TypeError("invalid fd type (%s, expected integer)" % type(fd))
658658
import io
659-
return io.open(fd, mode, buffering)
659+
return io.open(fd, *args, **kwargs)

Lib/test/test_urllibnet.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,18 +113,14 @@ def test_getcode(self):
113113
self.assertEqual(code, 404)
114114

115115
def test_fileno(self):
116-
if (sys.platform in ('win32',) or
117-
not hasattr(os, 'fdopen')):
116+
if sys.platform in ('win32',):
118117
# On Windows, socket handles are not file descriptors; this
119118
# test can't pass on Windows.
120119
return
121120
# Make sure fd returned by fileno is valid.
122121
open_url = self.urlopen("http://www.python.org/")
123122
fd = open_url.fileno()
124-
# XXX(nnorwitz): There is currently no way to pass errors, encoding,
125-
# etc to fdopen. :-(
126-
FILE = os.fdopen(fd)
127-
FILE._errors = 'ignore'
123+
FILE = os.fdopen(fd, encoding='utf-8')
128124
try:
129125
self.assert_(FILE.read(), "reading from file created using fd "
130126
"returned by fileno failed")
@@ -156,7 +152,7 @@ def test_basic(self):
156152
file_location,info = self.urlretrieve("http://www.python.org/")
157153
self.assert_(os.path.exists(file_location), "file location returned by"
158154
" urlretrieve is not a valid path")
159-
FILE = open(file_location, errors='ignore')
155+
FILE = open(file_location, encoding='utf-8')
160156
try:
161157
self.assert_(FILE.read(), "reading from the file location returned"
162158
" by urlretrieve failed")
@@ -170,7 +166,7 @@ def test_specified_path(self):
170166
support.TESTFN)
171167
self.assertEqual(file_location, support.TESTFN)
172168
self.assert_(os.path.exists(file_location))
173-
FILE = open(file_location, errors='ignore')
169+
FILE = open(file_location, encoding='utf-8')
174170
try:
175171
self.assert_(FILE.read(), "reading from temporary file failed")
176172
finally:

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ Core and Builtins
1818
Library
1919
-------
2020

21+
- Issue #2491: os.fdopen is now almost an alias for the built-in open(), and
22+
accepts the same parameters. It just checks that its first argument is an
23+
integer.
24+
2125
- Issue #3394: zipfile.writestr sets external attributes when passed a
2226
file name rather than a ZipInfo instance, so files are extracted with
2327
mode 0600 rather than 000 under Unix.

0 commit comments

Comments
 (0)