Kaydet (Commit) bdbddf8a authored tarafından Amaury Forgeot d'Arc's avatar Amaury Forgeot d'Arc

#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.
üst e19cadb4
...@@ -651,9 +651,9 @@ class _wrap_close: ...@@ -651,9 +651,9 @@ class _wrap_close:
def __iter__(self): def __iter__(self):
return iter(self._stream) return iter(self._stream)
# Supply os.fdopen() (used by subprocess!) # Supply os.fdopen()
def fdopen(fd, mode="r", buffering=-1): def fdopen(fd, *args, **kwargs):
if not isinstance(fd, int): if not isinstance(fd, int):
raise TypeError("invalid fd type (%s, expected integer)" % type(fd)) raise TypeError("invalid fd type (%s, expected integer)" % type(fd))
import io import io
return io.open(fd, mode, buffering) return io.open(fd, *args, **kwargs)
...@@ -113,18 +113,14 @@ class urlopenNetworkTests(unittest.TestCase): ...@@ -113,18 +113,14 @@ class urlopenNetworkTests(unittest.TestCase):
self.assertEqual(code, 404) self.assertEqual(code, 404)
def test_fileno(self): def test_fileno(self):
if (sys.platform in ('win32',) or if sys.platform in ('win32',):
not hasattr(os, 'fdopen')):
# On Windows, socket handles are not file descriptors; this # On Windows, socket handles are not file descriptors; this
# test can't pass on Windows. # test can't pass on Windows.
return return
# Make sure fd returned by fileno is valid. # Make sure fd returned by fileno is valid.
open_url = self.urlopen("http://www.python.org/") open_url = self.urlopen("http://www.python.org/")
fd = open_url.fileno() fd = open_url.fileno()
# XXX(nnorwitz): There is currently no way to pass errors, encoding, FILE = os.fdopen(fd, encoding='utf-8')
# etc to fdopen. :-(
FILE = os.fdopen(fd)
FILE._errors = 'ignore'
try: try:
self.assert_(FILE.read(), "reading from file created using fd " self.assert_(FILE.read(), "reading from file created using fd "
"returned by fileno failed") "returned by fileno failed")
...@@ -156,7 +152,7 @@ class urlretrieveNetworkTests(unittest.TestCase): ...@@ -156,7 +152,7 @@ class urlretrieveNetworkTests(unittest.TestCase):
file_location,info = self.urlretrieve("http://www.python.org/") file_location,info = self.urlretrieve("http://www.python.org/")
self.assert_(os.path.exists(file_location), "file location returned by" self.assert_(os.path.exists(file_location), "file location returned by"
" urlretrieve is not a valid path") " urlretrieve is not a valid path")
FILE = open(file_location, errors='ignore') FILE = open(file_location, encoding='utf-8')
try: try:
self.assert_(FILE.read(), "reading from the file location returned" self.assert_(FILE.read(), "reading from the file location returned"
" by urlretrieve failed") " by urlretrieve failed")
...@@ -170,7 +166,7 @@ class urlretrieveNetworkTests(unittest.TestCase): ...@@ -170,7 +166,7 @@ class urlretrieveNetworkTests(unittest.TestCase):
support.TESTFN) support.TESTFN)
self.assertEqual(file_location, support.TESTFN) self.assertEqual(file_location, support.TESTFN)
self.assert_(os.path.exists(file_location)) self.assert_(os.path.exists(file_location))
FILE = open(file_location, errors='ignore') FILE = open(file_location, encoding='utf-8')
try: try:
self.assert_(FILE.read(), "reading from temporary file failed") self.assert_(FILE.read(), "reading from temporary file failed")
finally: finally:
......
...@@ -18,6 +18,10 @@ Core and Builtins ...@@ -18,6 +18,10 @@ Core and Builtins
Library Library
------- -------
- Issue #2491: os.fdopen is now almost an alias for the built-in open(), and
accepts the same parameters. It just checks that its first argument is an
integer.
- Issue #3394: zipfile.writestr sets external attributes when passed a - Issue #3394: zipfile.writestr sets external attributes when passed a
file name rather than a ZipInfo instance, so files are extracted with file name rather than a ZipInfo instance, so files are extracted with
mode 0600 rather than 000 under Unix. mode 0600 rather than 000 under Unix.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment