Kaydet (Commit) a6d2c769 authored tarafından Victor Stinner's avatar Victor Stinner

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.
üst eaf399e3
...@@ -62,7 +62,7 @@ class TestFcntl(unittest.TestCase): ...@@ -62,7 +62,7 @@ class TestFcntl(unittest.TestCase):
def test_fcntl_fileno(self): def test_fcntl_fileno(self):
# the example from the library docs # 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) rv = fcntl.fcntl(self.f.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
if verbose: if verbose:
print('Status from fcntl with O_NONBLOCK: ', rv) print('Status from fcntl with O_NONBLOCK: ', rv)
...@@ -74,7 +74,7 @@ class TestFcntl(unittest.TestCase): ...@@ -74,7 +74,7 @@ class TestFcntl(unittest.TestCase):
def test_fcntl_file_descriptor(self): def test_fcntl_file_descriptor(self):
# again, but pass the file rather than numeric descriptor # 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) rv = fcntl.fcntl(self.f, fcntl.F_SETFL, os.O_NONBLOCK)
if sys.platform not in ['os2emx']: if sys.platform not in ['os2emx']:
rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata) rv = fcntl.fcntl(self.f, fcntl.F_SETLKW, lockdata)
......
...@@ -7,7 +7,7 @@ termios = import_module('termios') ...@@ -7,7 +7,7 @@ termios = import_module('termios')
get_attribute(termios, 'TIOCGPGRP') #Can't run tests without this feature get_attribute(termios, 'TIOCGPGRP') #Can't run tests without this feature
try: try:
tty = open("/dev/tty", "r") tty = open("/dev/tty", "rb")
except IOError: except IOError:
raise unittest.SkipTest("Unable to open /dev/tty") raise unittest.SkipTest("Unable to open /dev/tty")
else: else:
...@@ -30,7 +30,7 @@ class IoctlTests(unittest.TestCase): ...@@ -30,7 +30,7 @@ class IoctlTests(unittest.TestCase):
# If this process has been put into the background, TIOCGPGRP returns # If this process has been put into the background, TIOCGPGRP returns
# the session ID instead of the process group id. # the session ID instead of the process group id.
ids = (os.getpgrp(), os.getsid(0)) 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, " ") r = fcntl.ioctl(tty, termios.TIOCGPGRP, " ")
rpgrp = struct.unpack("i", r)[0] rpgrp = struct.unpack("i", r)[0]
self.assertIn(rpgrp, ids) self.assertIn(rpgrp, ids)
...@@ -47,7 +47,7 @@ class IoctlTests(unittest.TestCase): ...@@ -47,7 +47,7 @@ class IoctlTests(unittest.TestCase):
self.assertEqual(len(buf) * intsize, nbytes) # sanity check self.assertEqual(len(buf) * intsize, nbytes) # sanity check
else: else:
buf.append(fill) 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) r = fcntl.ioctl(tty, termios.TIOCGPGRP, buf, 1)
rpgrp = buf[0] rpgrp = buf[0]
self.assertEqual(r, 0) self.assertEqual(r, 0)
......
...@@ -108,7 +108,7 @@ class MmapTests(unittest.TestCase): ...@@ -108,7 +108,7 @@ class MmapTests(unittest.TestCase):
# Check that the underlying file is truncated too # Check that the underlying file is truncated too
# (bug #728515) # (bug #728515)
f = open(TESTFN) f = open(TESTFN, 'rb')
try: try:
f.seek(0, 2) f.seek(0, 2)
self.assertEqual(f.tell(), 512) self.assertEqual(f.tell(), 512)
...@@ -308,7 +308,7 @@ class MmapTests(unittest.TestCase): ...@@ -308,7 +308,7 @@ class MmapTests(unittest.TestCase):
f.write(2**16 * b'a') # Arbitrary character f.write(2**16 * b'a') # Arbitrary character
f.close() f.close()
f = open(TESTFN) f = open(TESTFN, 'rb')
mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ) mf = mmap.mmap(f.fileno(), 2**16, access=mmap.ACCESS_READ)
mf.close() mf.close()
mf.close() mf.close()
...@@ -501,7 +501,7 @@ class MmapTests(unittest.TestCase): ...@@ -501,7 +501,7 @@ class MmapTests(unittest.TestCase):
self.assertEqual(m[0:3], b'foo') self.assertEqual(m[0:3], b'foo')
# Check that the underlying file is truncated too # Check that the underlying file is truncated too
f = open(TESTFN) f = open(TESTFN, 'rb')
f.seek(0, 2) f.seek(0, 2)
self.assertEqual(f.tell(), halfsize + 512) self.assertEqual(f.tell(), halfsize + 512)
f.close() f.close()
......
...@@ -689,12 +689,11 @@ class MakedirTests(unittest.TestCase): ...@@ -689,12 +689,11 @@ class MakedirTests(unittest.TestCase):
class DevNullTests(unittest.TestCase): class DevNullTests(unittest.TestCase):
def test_devnull(self): def test_devnull(self):
f = open(os.devnull, 'w') with open(os.devnull, 'wb') as f:
f.write('hello') f.write(b'hello')
f.close() f.close()
f = open(os.devnull, 'r') with open(os.devnull, 'rb') as f:
self.assertEqual(f.read(), '') self.assertEqual(f.read(), b'')
f.close()
class URandomTests(unittest.TestCase): class URandomTests(unittest.TestCase):
def test_urandom(self): def test_urandom(self):
...@@ -1044,7 +1043,7 @@ if sys.platform != 'win32': ...@@ -1044,7 +1043,7 @@ if sys.platform != 'win32':
def test_open(self): def test_open(self):
for fn in self.unicodefn: for fn in self.unicodefn:
f = open(os.path.join(self.dir, fn)) f = open(os.path.join(self.dir, fn), 'rb')
f.close() f.close()
def test_stat(self): def test_stat(self):
......
...@@ -642,11 +642,10 @@ class ThreadJoinOnShutdown(BaseTestCase): ...@@ -642,11 +642,10 @@ class ThreadJoinOnShutdown(BaseTestCase):
def random_io(): def random_io():
'''Loop for a while sleeping random tiny amounts and doing some I/O.''' '''Loop for a while sleeping random tiny amounts and doing some I/O.'''
blank = b'x' * 200
while True: while True:
in_f = open(os.__file__, 'r') in_f = open(os.__file__, 'rb')
stuff = in_f.read(200) stuff = in_f.read(200)
null_f = open(os.devnull, 'w') null_f = open(os.devnull, 'wb')
null_f.write(stuff) null_f.write(stuff)
time.sleep(random.random() / 1995) time.sleep(random.random() / 1995)
null_f.close() null_f.close()
......
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