[Python-checkins] cpython (merge 3.2 -> default): (merge 3.2) Issue #12451: Open files in binary mode in some tests when the text

victor.stinner python-checkins at python.org
Thu Jun 30 18:21:45 CEST 2011


http://hg.python.org/cpython/rev/3969b6377f52
changeset: 71098:3969b6377f52
parent: 71096:e8eea84a90dc
parent: 71097:68bc1a29ba5a
user: Victor Stinner <victor.stinner at haypocalc.com>
date: Thu Jun 30 18:21:39 2011 +0200
summary:
 (merge 3.2) Issue #12451: Open files in binary mode in some tests when the text
file is not needed.
Remove also an unused variable (blank) in test_threading.
files:
 Lib/test/test_fcntl.py | 4 ++--
 Lib/test/test_ioctl.py | 6 +++---
 Lib/test/test_mmap.py | 6 +++---
 Lib/test/test_os.py | 13 ++++++-------
 Lib/test/test_threading.py | 5 ++---
 5 files changed, 16 insertions(+), 18 deletions(-)
diff --git a/Lib/test/test_fcntl.py b/Lib/test/test_fcntl.py
--- a/Lib/test/test_fcntl.py
+++ b/Lib/test/test_fcntl.py
@@ -62,7 +62,7 @@
 
 def test_fcntl_fileno(self):
 # the example from the library docs
- self.f = open(TESTFN, 'w')
+ self.f = open(TESTFN, 'wb')
 rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
 if verbose:
 print('Status from fcntl with O_NONBLOCK: ', rv)
@@ -74,7 +74,7 @@
 
 def test_fcntl_file_descriptor(self):
 # again, but pass the file rather than numeric descriptor
- self.f = open(TESTFN, 'w')
+ self.f = open(TESTFN, 'wb')
 rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
 if sys.platform not in ['os2emx']:
 rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
diff --git a/Lib/test/test_ioctl.py b/Lib/test/test_ioctl.py
--- a/Lib/test/test_ioctl.py
+++ b/Lib/test/test_ioctl.py
@@ -7,7 +7,7 @@
 get_attribute(termios, 'TIOCGPGRP') #Can't run tests without this feature
 
 try:
- tty = open("/dev/tty", "r")
+ tty = open("/dev/tty", "rb")
 except IOError:
 raise unittest.SkipTest("Unable to open /dev/tty")
 else:
@@ -30,7 +30,7 @@
 # If this process has been put into the background, TIOCGPGRP returns
 # the session ID instead of the process group id.
 ids = (os.getpgrp(), os.getsid(0))
- with open("/dev/tty", "r") as tty:
+ with open("/dev/tty", "rb") as tty:
 r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ")
 rpgrp = struct.unpack("i", r)[0]
 self.assertIn(rpgrp, ids)
@@ -47,7 +47,7 @@
 self.assertEqual(len(buf) * intsize, nbytes) # sanity check
 else:
 buf.append(fill)
- with open("/dev/tty", "r") as tty:
+ with open("/dev/tty", "rb") as tty:
 r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, 1)
 rpgrp = buf[0]
 self.assertEqual(r, 0)
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -108,7 +108,7 @@
 
 # Check that the underlying file is truncated too
 # (bug #728515)
- f = open(TESTFN)
+ f = open(TESTFN, 'rb')
 try:
 f.seek(0, 2)
 self.assertEqual(f.tell(), 512)
@@ -308,7 +308,7 @@
 f.write(2**16 * b'a') # Arbitrary character
 f.close()
 
- f = open(TESTFN)
+ f = open(TESTFN, 'rb')
 mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ)
 mf.close()
 mf.close()
@@ -530,7 +530,7 @@
 self.assertEqual(m[0:3], b'foo')
 
 # Check that the underlying file is truncated too
- f = open(TESTFN)
+ f = open(TESTFN, 'rb')
 f.seek(0, 2)
 self.assertEqual(f.tell(), halfsize + 512)
 f.close()
diff --git a/Lib/test/test_os.py b/Lib/test/test_os.py
--- a/Lib/test/test_os.py
+++ b/Lib/test/test_os.py
@@ -694,12 +694,11 @@
 
 class DevNullTests(unittest.TestCase):
 def test_devnull(self):
- f = open(os.devnull, 'w')
- f.write('hello')
- f.close()
- f = open(os.devnull, 'r')
- self.assertEqual(f.read(), '')
- f.close()
+ with open(os.devnull, 'wb') as f:
+ f.write(b'hello')
+ f.close()
+ with open(os.devnull, 'rb') as f:
+ self.assertEqual(f.read(), b'')
 
 class URandomTests(unittest.TestCase):
 def test_urandom(self):
@@ -1049,7 +1048,7 @@
 
 def test_open(self):
 for fn in self.unicodefn:
- f = open(os.path.join(self.dir, fn))
+ f = open(os.path.join(self.dir, fn), 'rb')
 f.close()
 
 def test_stat(self):
diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py
--- a/Lib/test/test_threading.py
+++ b/Lib/test/test_threading.py
@@ -650,11 +650,10 @@
 
 def random_io():
 '''Loop for a while sleeping random tiny amounts and doing some I/O.'''
- blank = b'x' * 200
 while True:
- in_f = open(os.__file__, 'r')
+ in_f = open(os.__file__, 'rb')
 stuff = in_f.read(200)
- null_f = open(os.devnull, 'w')
+ null_f = open(os.devnull, 'wb')
 null_f.write(stuff)
 time.sleep(random.random() / 1995)
 null_f.close()
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /