antoine.pitrou
2008-09-15 23:02:57 UTC
Author: antoine.pitrou
Date: Tue Sep 16 01:02:56 2008
New Revision: 66469
Log:
Issue #3782: os.write() must not accept unicode strings
Modified:
python/branches/py3k/Lib/pty.py
python/branches/py3k/Lib/test/test_asyncore.py
python/branches/py3k/Lib/test/test_os.py
python/branches/py3k/Lib/test/test_pty.py
python/branches/py3k/Lib/test/test_subprocess.py
python/branches/py3k/Lib/test/test_tempfile.py
python/branches/py3k/Lib/test/tf_inherit_check.py
python/branches/py3k/Misc/NEWS
python/branches/py3k/Modules/posixmodule.c
Modified: python/branches/py3k/Lib/pty.py
==============================================================================
--- python/branches/py3k/Lib/pty.py (original)
+++ python/branches/py3k/Lib/pty.py Tue Sep 16 01:02:56 2008
@@ -129,7 +129,7 @@
def _writen(fd, data):
"""Write all the data to a descriptor."""
- while data != '':
+ while data:
n = os.write(fd, data)
data = data[n:]
Modified: python/branches/py3k/Lib/test/test_asyncore.py
==============================================================================
--- python/branches/py3k/Lib/test/test_asyncore.py (original)
+++ python/branches/py3k/Lib/test/test_asyncore.py Tue Sep 16 01:02:56 2008
@@ -381,8 +381,8 @@
if hasattr(asyncore, 'file_wrapper'):
class FileWrapperTest(unittest.TestCase):
def setUp(self):
- self.d = "It's not dead, it's sleeping!"
- open(TESTFN, 'w').write(self.d)
+ self.d = b"It's not dead, it's sleeping!"
+ open(TESTFN, 'wb').write(self.d)
def tearDown(self):
unlink(TESTFN)
@@ -400,8 +400,8 @@
self.assertRaises(OSError, w.read, 1)
def test_send(self):
- d1 = "Come again?"
- d2 = "I want to buy some cheese."
+ d1 = b"Come again?"
+ d2 = b"I want to buy some cheese."
fd = os.open(TESTFN, os.O_WRONLY | os.O_APPEND)
w = asyncore.file_wrapper(fd)
os.close(fd)
@@ -409,7 +409,7 @@
w.write(d1)
w.send(d2)
w.close()
- self.assertEqual(open(TESTFN).read(), self.d + d1 + d2)
+ self.assertEqual(open(TESTFN, 'rb').read(), self.d + d1 + d2)
def test_main():
Modified: python/branches/py3k/Lib/test/test_os.py
==============================================================================
--- python/branches/py3k/Lib/test/test_os.py (original)
+++ python/branches/py3k/Lib/test/test_os.py Tue Sep 16 01:02:56 2008
@@ -41,7 +41,7 @@
os.close(second)
# close a fd that is open, and one that isn't
os.closerange(first, first + 2)
- self.assertRaises(OSError, os.write, first, "a")
+ self.assertRaises(OSError, os.write, first, b"a")
def test_rename(self):
path = support.TESTFN
@@ -50,6 +50,28 @@
new = sys.getrefcount(path)
self.assertEqual(old, new)
+ def test_read(self):
+ with open(support.TESTFN, "w+b") as fobj:
+ fobj.write(b"spam")
+ fobj.flush()
+ fd = fobj.fileno()
+ os.lseek(fd, 0, 0)
+ s = os.read(fd, 4)
+ self.assertEqual(type(s), bytes)
+ self.assertEqual(s, b"spam")
+
+ def test_write(self):
+ # os.write() accepts bytes- and buffer-like objects but not strings
+ fd = os.open(support.TESTFN, os.O_CREAT | os.O_WRONLY)
+ self.assertRaises(TypeError, os.write, fd, "beans")
+ os.write(fd, b"bacon\n")
+ os.write(fd, bytearray(b"eggs\n"))
+ os.write(fd, memoryview(b"spam\n"))
+ os.close(fd)
+ with open(support.TESTFN, "rb") as fobj:
+ self.assertEqual(fobj.read(), b"bacon\neggs\nspam\n")
+
+
class TemporaryFileTests(unittest.TestCase):
def setUp(self):
self.files = []
Modified: python/branches/py3k/Lib/test/test_pty.py
==============================================================================
--- python/branches/py3k/Lib/test/test_pty.py (original)
+++ python/branches/py3k/Lib/test/test_pty.py Tue Sep 16 01:02:56 2008
@@ -7,8 +7,8 @@
from test.support import verbose, TestSkipped, run_unittest
import unittest
-TEST_STRING_1 = "I wish to buy a fish license.\n"
-TEST_STRING_2 = "For my pet fish, Eric.\n"
+TEST_STRING_1 = b"I wish to buy a fish license.\n"
+TEST_STRING_2 = b"For my pet fish, Eric.\n"
if verbose:
def debug(msg):
Modified: python/branches/py3k/Lib/test/test_subprocess.py
==============================================================================
--- python/branches/py3k/Lib/test/test_subprocess.py (original)
+++ python/branches/py3k/Lib/test/test_subprocess.py Tue Sep 16 01:02:56 2008
@@ -128,7 +128,7 @@
# stdin is set to open file descriptor
tf = tempfile.TemporaryFile()
d = tf.fileno()
- os.write(d, "pear")
+ os.write(d, b"pear")
os.lseek(d, 0, 0)
p = subprocess.Popen([sys.executable, "-c",
'import sys; sys.exit(sys.stdin.read() == "pear")'],
@@ -237,7 +237,7 @@
def test_stdout_filedes_of_stdout(self):
# stdout is set to 1 (#1531862).
- cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), '.\n'))"
+ cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), b'.\n'))"
rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
self.assertEquals(rc, 2)
@@ -548,11 +548,12 @@
def test_args_string(self):
# args is a string
- f, fname = self.mkstemp()
- os.write(f, "#!/bin/sh\n")
- os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
- sys.executable)
- os.close(f)
+ fd, fname = self.mkstemp()
+ # reopen in text mode
+ with open(fd, "w") as fobj:
+ fobj.write("#!/bin/sh\n")
+ fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
+ sys.executable)
os.chmod(fname, 0o700)
p = subprocess.Popen(fname)
p.wait()
@@ -590,11 +591,12 @@
def test_call_string(self):
# call() function with string argument on UNIX
- f, fname = self.mkstemp()
- os.write(f, "#!/bin/sh\n")
- os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
- sys.executable)
- os.close(f)
+ fd, fname = self.mkstemp()
+ # reopen in text mode
+ with open(fd, "w") as fobj:
+ fobj.write("#!/bin/sh\n")
+ fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
+ sys.executable)
os.chmod(fname, 0o700)
rc = subprocess.call(fname)
os.remove(fname)
Modified: python/branches/py3k/Lib/test/test_tempfile.py
==============================================================================
--- python/branches/py3k/Lib/test/test_tempfile.py (original)
+++ python/branches/py3k/Lib/test/test_tempfile.py Tue Sep 16 01:02:56 2008
@@ -252,7 +252,7 @@
# _mkstemp_inner can create files in a user-selected directory
dir = tempfile.mkdtemp()
try:
- self.do_create(dir=dir).write("blat")
+ self.do_create(dir=dir).write(b"blat")
finally:
os.rmdir(dir)
Modified: python/branches/py3k/Lib/test/tf_inherit_check.py
==============================================================================
--- python/branches/py3k/Lib/test/tf_inherit_check.py (original)
+++ python/branches/py3k/Lib/test/tf_inherit_check.py Tue Sep 16 01:02:56 2008
@@ -10,7 +10,7 @@
fd = int(sys.argv[2])
try:
- os.write(fd, "blat")
+ os.write(fd, b"blat")
except os.error:
# Success -- could not write to fd.
sys.exit(0)
Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS (original)
+++ python/branches/py3k/Misc/NEWS Tue Sep 16 01:02:56 2008
@@ -140,6 +140,8 @@
Extension Modules
-----------------
+- Issue #3782: os.write() must not accept unicode strings.
+
- Issue #2975: When compiling several extension modules with Visual Studio 2008
from the same python interpreter, some environment variables would grow
without limit.
Modified: python/branches/py3k/Modules/posixmodule.c
==============================================================================
--- python/branches/py3k/Modules/posixmodule.c (original)
+++ python/branches/py3k/Modules/posixmodule.c Tue Sep 16 01:02:56 2008
@@ -4896,7 +4896,7 @@
int fd;
Py_ssize_t size;
- if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
+ if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
return NULL;
Py_BEGIN_ALLOW_THREADS
size = write(fd, pbuf.buf, (size_t)pbuf.len);
Date: Tue Sep 16 01:02:56 2008
New Revision: 66469
Log:
Issue #3782: os.write() must not accept unicode strings
Modified:
python/branches/py3k/Lib/pty.py
python/branches/py3k/Lib/test/test_asyncore.py
python/branches/py3k/Lib/test/test_os.py
python/branches/py3k/Lib/test/test_pty.py
python/branches/py3k/Lib/test/test_subprocess.py
python/branches/py3k/Lib/test/test_tempfile.py
python/branches/py3k/Lib/test/tf_inherit_check.py
python/branches/py3k/Misc/NEWS
python/branches/py3k/Modules/posixmodule.c
Modified: python/branches/py3k/Lib/pty.py
==============================================================================
--- python/branches/py3k/Lib/pty.py (original)
+++ python/branches/py3k/Lib/pty.py Tue Sep 16 01:02:56 2008
@@ -129,7 +129,7 @@
def _writen(fd, data):
"""Write all the data to a descriptor."""
- while data != '':
+ while data:
n = os.write(fd, data)
data = data[n:]
Modified: python/branches/py3k/Lib/test/test_asyncore.py
==============================================================================
--- python/branches/py3k/Lib/test/test_asyncore.py (original)
+++ python/branches/py3k/Lib/test/test_asyncore.py Tue Sep 16 01:02:56 2008
@@ -381,8 +381,8 @@
if hasattr(asyncore, 'file_wrapper'):
class FileWrapperTest(unittest.TestCase):
def setUp(self):
- self.d = "It's not dead, it's sleeping!"
- open(TESTFN, 'w').write(self.d)
+ self.d = b"It's not dead, it's sleeping!"
+ open(TESTFN, 'wb').write(self.d)
def tearDown(self):
unlink(TESTFN)
@@ -400,8 +400,8 @@
self.assertRaises(OSError, w.read, 1)
def test_send(self):
- d1 = "Come again?"
- d2 = "I want to buy some cheese."
+ d1 = b"Come again?"
+ d2 = b"I want to buy some cheese."
fd = os.open(TESTFN, os.O_WRONLY | os.O_APPEND)
w = asyncore.file_wrapper(fd)
os.close(fd)
@@ -409,7 +409,7 @@
w.write(d1)
w.send(d2)
w.close()
- self.assertEqual(open(TESTFN).read(), self.d + d1 + d2)
+ self.assertEqual(open(TESTFN, 'rb').read(), self.d + d1 + d2)
def test_main():
Modified: python/branches/py3k/Lib/test/test_os.py
==============================================================================
--- python/branches/py3k/Lib/test/test_os.py (original)
+++ python/branches/py3k/Lib/test/test_os.py Tue Sep 16 01:02:56 2008
@@ -41,7 +41,7 @@
os.close(second)
# close a fd that is open, and one that isn't
os.closerange(first, first + 2)
- self.assertRaises(OSError, os.write, first, "a")
+ self.assertRaises(OSError, os.write, first, b"a")
def test_rename(self):
path = support.TESTFN
@@ -50,6 +50,28 @@
new = sys.getrefcount(path)
self.assertEqual(old, new)
+ def test_read(self):
+ with open(support.TESTFN, "w+b") as fobj:
+ fobj.write(b"spam")
+ fobj.flush()
+ fd = fobj.fileno()
+ os.lseek(fd, 0, 0)
+ s = os.read(fd, 4)
+ self.assertEqual(type(s), bytes)
+ self.assertEqual(s, b"spam")
+
+ def test_write(self):
+ # os.write() accepts bytes- and buffer-like objects but not strings
+ fd = os.open(support.TESTFN, os.O_CREAT | os.O_WRONLY)
+ self.assertRaises(TypeError, os.write, fd, "beans")
+ os.write(fd, b"bacon\n")
+ os.write(fd, bytearray(b"eggs\n"))
+ os.write(fd, memoryview(b"spam\n"))
+ os.close(fd)
+ with open(support.TESTFN, "rb") as fobj:
+ self.assertEqual(fobj.read(), b"bacon\neggs\nspam\n")
+
+
class TemporaryFileTests(unittest.TestCase):
def setUp(self):
self.files = []
Modified: python/branches/py3k/Lib/test/test_pty.py
==============================================================================
--- python/branches/py3k/Lib/test/test_pty.py (original)
+++ python/branches/py3k/Lib/test/test_pty.py Tue Sep 16 01:02:56 2008
@@ -7,8 +7,8 @@
from test.support import verbose, TestSkipped, run_unittest
import unittest
-TEST_STRING_1 = "I wish to buy a fish license.\n"
-TEST_STRING_2 = "For my pet fish, Eric.\n"
+TEST_STRING_1 = b"I wish to buy a fish license.\n"
+TEST_STRING_2 = b"For my pet fish, Eric.\n"
if verbose:
def debug(msg):
Modified: python/branches/py3k/Lib/test/test_subprocess.py
==============================================================================
--- python/branches/py3k/Lib/test/test_subprocess.py (original)
+++ python/branches/py3k/Lib/test/test_subprocess.py Tue Sep 16 01:02:56 2008
@@ -128,7 +128,7 @@
# stdin is set to open file descriptor
tf = tempfile.TemporaryFile()
d = tf.fileno()
- os.write(d, "pear")
+ os.write(d, b"pear")
os.lseek(d, 0, 0)
p = subprocess.Popen([sys.executable, "-c",
'import sys; sys.exit(sys.stdin.read() == "pear")'],
@@ -237,7 +237,7 @@
def test_stdout_filedes_of_stdout(self):
# stdout is set to 1 (#1531862).
- cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), '.\n'))"
+ cmd = r"import sys, os; sys.exit(os.write(sys.stdout.fileno(), b'.\n'))"
rc = subprocess.call([sys.executable, "-c", cmd], stdout=1)
self.assertEquals(rc, 2)
@@ -548,11 +548,12 @@
def test_args_string(self):
# args is a string
- f, fname = self.mkstemp()
- os.write(f, "#!/bin/sh\n")
- os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
- sys.executable)
- os.close(f)
+ fd, fname = self.mkstemp()
+ # reopen in text mode
+ with open(fd, "w") as fobj:
+ fobj.write("#!/bin/sh\n")
+ fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
+ sys.executable)
os.chmod(fname, 0o700)
p = subprocess.Popen(fname)
p.wait()
@@ -590,11 +591,12 @@
def test_call_string(self):
# call() function with string argument on UNIX
- f, fname = self.mkstemp()
- os.write(f, "#!/bin/sh\n")
- os.write(f, "exec '%s' -c 'import sys; sys.exit(47)'\n" %
- sys.executable)
- os.close(f)
+ fd, fname = self.mkstemp()
+ # reopen in text mode
+ with open(fd, "w") as fobj:
+ fobj.write("#!/bin/sh\n")
+ fobj.write("exec '%s' -c 'import sys; sys.exit(47)'\n" %
+ sys.executable)
os.chmod(fname, 0o700)
rc = subprocess.call(fname)
os.remove(fname)
Modified: python/branches/py3k/Lib/test/test_tempfile.py
==============================================================================
--- python/branches/py3k/Lib/test/test_tempfile.py (original)
+++ python/branches/py3k/Lib/test/test_tempfile.py Tue Sep 16 01:02:56 2008
@@ -252,7 +252,7 @@
# _mkstemp_inner can create files in a user-selected directory
dir = tempfile.mkdtemp()
try:
- self.do_create(dir=dir).write("blat")
+ self.do_create(dir=dir).write(b"blat")
finally:
os.rmdir(dir)
Modified: python/branches/py3k/Lib/test/tf_inherit_check.py
==============================================================================
--- python/branches/py3k/Lib/test/tf_inherit_check.py (original)
+++ python/branches/py3k/Lib/test/tf_inherit_check.py Tue Sep 16 01:02:56 2008
@@ -10,7 +10,7 @@
fd = int(sys.argv[2])
try:
- os.write(fd, "blat")
+ os.write(fd, b"blat")
except os.error:
# Success -- could not write to fd.
sys.exit(0)
Modified: python/branches/py3k/Misc/NEWS
==============================================================================
--- python/branches/py3k/Misc/NEWS (original)
+++ python/branches/py3k/Misc/NEWS Tue Sep 16 01:02:56 2008
@@ -140,6 +140,8 @@
Extension Modules
-----------------
+- Issue #3782: os.write() must not accept unicode strings.
+
- Issue #2975: When compiling several extension modules with Visual Studio 2008
from the same python interpreter, some environment variables would grow
without limit.
Modified: python/branches/py3k/Modules/posixmodule.c
==============================================================================
--- python/branches/py3k/Modules/posixmodule.c (original)
+++ python/branches/py3k/Modules/posixmodule.c Tue Sep 16 01:02:56 2008
@@ -4896,7 +4896,7 @@
int fd;
Py_ssize_t size;
- if (!PyArg_ParseTuple(args, "is*:write", &fd, &pbuf))
+ if (!PyArg_ParseTuple(args, "iy*:write", &fd, &pbuf))
return NULL;
Py_BEGIN_ALLOW_THREADS
size = write(fd, pbuf.buf, (size_t)pbuf.len);