[Python-checkins] r74665 - in python/branches/release31-maint: Lib/ctypes/test/test_cast.py Lib/ctypes/test/test_funcptr.py Lib/ctypes/test/test_functions.py Lib/ctypes/test/test_incomplete.py Lib/ctypes/test/test_memfunctions.py Lib/ctypes/test/test_objects.py Lib/ctypes/test/test_pointers.py Lib/ctypes/test/test_prototypes.py Lib/ctypes/test/test_random_things.py Lib/ctypes/test/test_returnfuncptrs.py Lib/ctypes/test/test_slicing.py Lib/ctypes/test/test_stringptr.py Lib/ctypes/test/test_unicode.py Misc/NEWS Modules/_ctypes/cfield.c
thomas.heller
python-checkins at python.org
Fri Sep 4 20:37:04 CEST 2009
Author: thomas.heller
Date: Fri Sep 4 20:37:03 2009
New Revision: 74665
Log:
Merged revisions 74664 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r74664 | thomas.heller | 2009-09-04 20:24:41 +0200 (Fr, 04 Sep 2009) | 1 line
Issue 6239: ctypes.c_char_p return value must return bytes.
........
Modified:
python/branches/release31-maint/ (props changed)
python/branches/release31-maint/Lib/ctypes/test/test_cast.py
python/branches/release31-maint/Lib/ctypes/test/test_funcptr.py
python/branches/release31-maint/Lib/ctypes/test/test_functions.py
python/branches/release31-maint/Lib/ctypes/test/test_incomplete.py
python/branches/release31-maint/Lib/ctypes/test/test_memfunctions.py
python/branches/release31-maint/Lib/ctypes/test/test_objects.py
python/branches/release31-maint/Lib/ctypes/test/test_pointers.py
python/branches/release31-maint/Lib/ctypes/test/test_prototypes.py
python/branches/release31-maint/Lib/ctypes/test/test_random_things.py
python/branches/release31-maint/Lib/ctypes/test/test_returnfuncptrs.py
python/branches/release31-maint/Lib/ctypes/test/test_slicing.py
python/branches/release31-maint/Lib/ctypes/test/test_stringptr.py
python/branches/release31-maint/Lib/ctypes/test/test_unicode.py
python/branches/release31-maint/Misc/NEWS
python/branches/release31-maint/Modules/_ctypes/cfield.c
Modified: python/branches/release31-maint/Lib/ctypes/test/test_cast.py
==============================================================================
--- python/branches/release31-maint/Lib/ctypes/test/test_cast.py (original)
+++ python/branches/release31-maint/Lib/ctypes/test/test_cast.py Fri Sep 4 20:37:03 2009
@@ -73,7 +73,7 @@
# This didn't work: bad argument to internal function
s = c_char_p("hiho")
self.assertEqual(cast(cast(s, c_void_p), c_char_p).value,
- "hiho")
+ b"hiho")
try:
c_wchar_p
Modified: python/branches/release31-maint/Lib/ctypes/test/test_funcptr.py
==============================================================================
--- python/branches/release31-maint/Lib/ctypes/test/test_funcptr.py (original)
+++ python/branches/release31-maint/Lib/ctypes/test/test_funcptr.py Fri Sep 4 20:37:03 2009
@@ -97,8 +97,8 @@
strchr = lib.my_strchr
strchr.restype = c_char_p
strchr.argtypes = (c_char_p, c_char)
- self.assertEqual(strchr("abcdefghi", "b"), "bcdefghi")
- self.assertEqual(strchr("abcdefghi", "x"), None)
+ self.assertEqual(strchr(b"abcdefghi", b"b"), b"bcdefghi")
+ self.assertEqual(strchr(b"abcdefghi", b"x"), None)
strtok = lib.my_strtok
@@ -111,16 +111,16 @@
size = len(init) + 1
return (c_char*size)(*init)
- s = "a\nb\nc"
+ s = b"a\nb\nc"
b = c_string(s)
## b = (c_char * (len(s)+1))()
## b.value = s
## b = c_string(s)
- self.assertEqual(strtok(b, b"\n"), "a")
- self.assertEqual(strtok(None, b"\n"), "b")
- self.assertEqual(strtok(None, b"\n"), "c")
+ self.assertEqual(strtok(b, b"\n"), b"a")
+ self.assertEqual(strtok(None, b"\n"), b"b")
+ self.assertEqual(strtok(None, b"\n"), b"c")
self.assertEqual(strtok(None, b"\n"), None)
if __name__ == '__main__':
Modified: python/branches/release31-maint/Lib/ctypes/test/test_functions.py
==============================================================================
--- python/branches/release31-maint/Lib/ctypes/test/test_functions.py (original)
+++ python/branches/release31-maint/Lib/ctypes/test/test_functions.py Fri Sep 4 20:37:03 2009
@@ -177,7 +177,7 @@
f.argtypes = None
f.restype = c_char_p
result = f(b"123")
- self.assertEqual(result, "123")
+ self.assertEqual(result, b"123")
result = f(None)
self.assertEqual(result, None)
Modified: python/branches/release31-maint/Lib/ctypes/test/test_incomplete.py
==============================================================================
--- python/branches/release31-maint/Lib/ctypes/test/test_incomplete.py (original)
+++ python/branches/release31-maint/Lib/ctypes/test/test_incomplete.py Fri Sep 4 20:37:03 2009
@@ -17,9 +17,9 @@
SetPointerType(lpcell, cell)
c1 = cell()
- c1.name = "foo"
+ c1.name = b"foo"
c2 = cell()
- c2.name = "bar"
+ c2.name = b"bar"
c1.next = pointer(c2)
c2.next = pointer(c1)
@@ -30,7 +30,7 @@
for i in range(8):
result.append(p.name)
p = p.next[0]
- self.assertEqual(result, ["foo", "bar"] * 4)
+ self.assertEqual(result, [b"foo", b"bar"] * 4)
# to not leak references, we must clean _pointer_type_cache
from ctypes import _pointer_type_cache
Modified: python/branches/release31-maint/Lib/ctypes/test/test_memfunctions.py
==============================================================================
--- python/branches/release31-maint/Lib/ctypes/test/test_memfunctions.py (original)
+++ python/branches/release31-maint/Lib/ctypes/test/test_memfunctions.py Fri Sep 4 20:37:03 2009
@@ -37,7 +37,7 @@
def test_cast(self):
a = (c_ubyte * 32)(*map(ord, "abcdef"))
- self.assertEqual(cast(a, c_char_p).value, "abcdef")
+ self.assertEqual(cast(a, c_char_p).value, b"abcdef")
self.assertEqual(cast(a, POINTER(c_byte))[:7],
[97, 98, 99, 100, 101, 102, 0])
self.assertEqual(cast(a, POINTER(c_byte))[:7:],
Modified: python/branches/release31-maint/Lib/ctypes/test/test_objects.py
==============================================================================
--- python/branches/release31-maint/Lib/ctypes/test/test_objects.py (original)
+++ python/branches/release31-maint/Lib/ctypes/test/test_objects.py Fri Sep 4 20:37:03 2009
@@ -24,7 +24,7 @@
>>> array._objects
{'4': b'foo bar'}
>>> array[4]
-'foo bar'
+b'foo bar'
>>>
It gets more complicated when the ctypes instance itself is contained
Modified: python/branches/release31-maint/Lib/ctypes/test/test_pointers.py
==============================================================================
--- python/branches/release31-maint/Lib/ctypes/test/test_pointers.py (original)
+++ python/branches/release31-maint/Lib/ctypes/test/test_pointers.py Fri Sep 4 20:37:03 2009
@@ -140,10 +140,10 @@
func.restype = c_char_p
argv = (c_char_p * 2)()
argc = c_int( 2 )
- argv[0] = 'hello'
- argv[1] = 'world'
+ argv[0] = b'hello'
+ argv[1] = b'world'
result = func( byref(argc), argv )
- assert result == 'world', result
+ self.assertEqual(result, b'world')
def test_bug_1467852(self):
# http://sourceforge.net/tracker/?func=detail&atid=532154&aid=1467852&group_id=71702
Modified: python/branches/release31-maint/Lib/ctypes/test/test_prototypes.py
==============================================================================
--- python/branches/release31-maint/Lib/ctypes/test/test_prototypes.py (original)
+++ python/branches/release31-maint/Lib/ctypes/test/test_prototypes.py Fri Sep 4 20:37:03 2009
@@ -92,14 +92,14 @@
func.argtypes = POINTER(c_char),
self.assertEqual(None, func(None))
- self.assertEqual("123", func("123"))
+ self.assertEqual(b"123", func(b"123"))
self.assertEqual(None, func(c_char_p(None)))
- self.assertEqual("123", func(c_char_p("123")))
+ self.assertEqual(b"123", func(c_char_p(b"123")))
- self.assertEqual("123", func(c_buffer("123")))
- ca = c_char("a")
- self.assertEqual("a", func(pointer(ca))[0])
- self.assertEqual("a", func(byref(ca))[0])
+ self.assertEqual(b"123", func(c_buffer(b"123")))
+ ca = c_char(b"a")
+ self.assertEqual(ord(b"a"), func(pointer(ca))[0])
+ self.assertEqual(ord(b"a"), func(byref(ca))[0])
def test_c_char_p_arg(self):
func = testdll._testfunc_p_p
@@ -107,14 +107,14 @@
func.argtypes = c_char_p,
self.assertEqual(None, func(None))
- self.assertEqual("123", func("123"))
+ self.assertEqual(b"123", func(b"123"))
self.assertEqual(None, func(c_char_p(None)))
- self.assertEqual("123", func(c_char_p("123")))
+ self.assertEqual(b"123", func(c_char_p(b"123")))
- self.assertEqual("123", func(c_buffer("123")))
- ca = c_char("a")
- self.assertEqual("a", func(pointer(ca))[0])
- self.assertEqual("a", func(byref(ca))[0])
+ self.assertEqual(b"123", func(c_buffer(b"123")))
+ ca = c_char(b"a")
+ self.assertEqual(ord(b"a"), func(pointer(ca))[0])
+ self.assertEqual(ord(b"a"), func(byref(ca))[0])
def test_c_void_p_arg(self):
func = testdll._testfunc_p_p
@@ -122,14 +122,14 @@
func.argtypes = c_void_p,
self.assertEqual(None, func(None))
- self.assertEqual("123", func(b"123"))
- self.assertEqual("123", func(c_char_p("123")))
+ self.assertEqual(b"123", func(b"123"))
+ self.assertEqual(b"123", func(c_char_p(b"123")))
self.assertEqual(None, func(c_char_p(None)))
- self.assertEqual("123", func(c_buffer("123")))
+ self.assertEqual(b"123", func(c_buffer(b"123")))
ca = c_char("a")
- self.assertEqual("a", func(pointer(ca))[0])
- self.assertEqual("a", func(byref(ca))[0])
+ self.assertEqual(ord(b"a"), func(pointer(ca))[0])
+ self.assertEqual(ord(b"a"), func(byref(ca))[0])
func(byref(c_int()))
func(pointer(c_int()))
Modified: python/branches/release31-maint/Lib/ctypes/test/test_random_things.py
==============================================================================
--- python/branches/release31-maint/Lib/ctypes/test/test_random_things.py (original)
+++ python/branches/release31-maint/Lib/ctypes/test/test_random_things.py Fri Sep 4 20:37:03 2009
@@ -69,7 +69,7 @@
out = self.capture_stderr(cb, "spam")
self.assertEqual(out.splitlines()[-1],
"TypeError: "
- "unsupported operand type(s) for /: 'int' and 'str'")
+ "unsupported operand type(s) for /: 'int' and 'bytes'")
if __name__ == '__main__':
unittest.main()
Modified: python/branches/release31-maint/Lib/ctypes/test/test_returnfuncptrs.py
==============================================================================
--- python/branches/release31-maint/Lib/ctypes/test/test_returnfuncptrs.py (original)
+++ python/branches/release31-maint/Lib/ctypes/test/test_returnfuncptrs.py Fri Sep 4 20:37:03 2009
@@ -12,12 +12,12 @@
get_strchr = dll.get_strchr
get_strchr.restype = CFUNCTYPE(c_char_p, c_char_p, c_char)
strchr = get_strchr()
- self.assertEqual(strchr("abcdef", "b"), "bcdef")
- self.assertEqual(strchr("abcdef", "x"), None)
- self.assertEqual(strchr("abcdef", 98), "bcdef")
- self.assertEqual(strchr("abcdef", 107), None)
- self.assertRaises(ArgumentError, strchr, "abcdef", 3.0)
- self.assertRaises(TypeError, strchr, "abcdef")
+ self.assertEqual(strchr(b"abcdef", b"b"), b"bcdef")
+ self.assertEqual(strchr(b"abcdef", b"x"), None)
+ self.assertEqual(strchr(b"abcdef", 98), b"bcdef")
+ self.assertEqual(strchr(b"abcdef", 107), None)
+ self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0)
+ self.assertRaises(TypeError, strchr, b"abcdef")
def test_without_prototype(self):
dll = CDLL(_ctypes_test.__file__)
Modified: python/branches/release31-maint/Lib/ctypes/test/test_slicing.py
==============================================================================
--- python/branches/release31-maint/Lib/ctypes/test/test_slicing.py (original)
+++ python/branches/release31-maint/Lib/ctypes/test/test_slicing.py Fri Sep 4 20:37:03 2009
@@ -109,7 +109,7 @@
dll.my_strdup.errcheck = errcheck
try:
res = dll.my_strdup(s)
- self.assertEqual(res, s.decode())
+ self.assertEqual(res, s)
finally:
del dll.my_strdup.errcheck
Modified: python/branches/release31-maint/Lib/ctypes/test/test_stringptr.py
==============================================================================
--- python/branches/release31-maint/Lib/ctypes/test/test_stringptr.py (original)
+++ python/branches/release31-maint/Lib/ctypes/test/test_stringptr.py Fri Sep 4 20:37:03 2009
@@ -35,10 +35,10 @@
# c_char_p and Python string is compatible
# c_char_p and c_buffer is NOT compatible
self.assertEqual(x.str, None)
- x.str = "Hello, World"
- self.assertEqual(x.str, "Hello, World")
- b = c_buffer("Hello, World")
- self.assertRaises(TypeError, setattr, x, "str", b)
+ x.str = b"Hello, World"
+ self.assertEqual(x.str, b"Hello, World")
+ b = c_buffer(b"Hello, World")
+ self.assertRaises(TypeError, setattr, x, b"str", b)
def test_functions(self):
@@ -48,15 +48,15 @@
# c_char_p and Python string is compatible
# c_char_p and c_buffer are now compatible
strchr.argtypes = c_char_p, c_char
- self.assertEqual(strchr("abcdef", "c"), "cdef")
- self.assertEqual(strchr(c_buffer("abcdef"), "c"), "cdef")
+ self.assertEqual(strchr(b"abcdef", b"c"), b"cdef")
+ self.assertEqual(strchr(c_buffer(b"abcdef"), b"c"), b"cdef")
# POINTER(c_char) and Python string is NOT compatible
# POINTER(c_char) and c_buffer() is compatible
strchr.argtypes = POINTER(c_char), c_char
- buf = c_buffer("abcdef")
- self.assertEqual(strchr(buf, "c"), "cdef")
- self.assertEqual(strchr("abcdef", "c"), "cdef")
+ buf = c_buffer(b"abcdef")
+ self.assertEqual(strchr(buf, b"c"), b"cdef")
+ self.assertEqual(strchr(b"abcdef", b"c"), b"cdef")
# XXX These calls are dangerous, because the first argument
# to strchr is no longer valid after the function returns!
Modified: python/branches/release31-maint/Lib/ctypes/test/test_unicode.py
==============================================================================
--- python/branches/release31-maint/Lib/ctypes/test/test_unicode.py (original)
+++ python/branches/release31-maint/Lib/ctypes/test/test_unicode.py Fri Sep 4 20:37:03 2009
@@ -94,15 +94,15 @@
def test_ascii_ignore(self):
ctypes.set_conversion_mode("ascii", "ignore")
- self.assertEqual(func("abc"), "abc")
- self.assertEqual(func("abc"), "abc")
- self.assertEqual(func("\xe4\xf6\xfc\xdf"), "")
+ self.assertEqual(func("abc"), b"abc")
+ self.assertEqual(func("abc"), b"abc")
+ self.assertEqual(func("\xe4\xf6\xfc\xdf"), b"")
def test_ascii_replace(self):
ctypes.set_conversion_mode("ascii", "replace")
- self.assertEqual(func("abc"), "abc")
- self.assertEqual(func("abc"), "abc")
- self.assertEqual(func("\xe4\xf6\xfc\xdf"), "????")
+ self.assertEqual(func("abc"), b"abc")
+ self.assertEqual(func("abc"), b"abc")
+ self.assertEqual(func("\xe4\xf6\xfc\xdf"), b"????")
def test_buffers(self):
ctypes.set_conversion_mode("ascii", "strict")
Modified: python/branches/release31-maint/Misc/NEWS
==============================================================================
--- python/branches/release31-maint/Misc/NEWS (original)
+++ python/branches/release31-maint/Misc/NEWS Fri Sep 4 20:37:03 2009
@@ -50,6 +50,8 @@
Library
-------
+- Issue #6239: ctypes.c_char_p return value must return bytes.
+
- Issue #6838: Use a list to accumulate the value instead of
repeatedly concatenating strings in http.client's
HTTPResponse._read_chunked providing a significant speed increase
Modified: python/branches/release31-maint/Modules/_ctypes/cfield.c
==============================================================================
--- python/branches/release31-maint/Modules/_ctypes/cfield.c (original)
+++ python/branches/release31-maint/Modules/_ctypes/cfield.c Fri Sep 4 20:37:03 2009
@@ -1428,7 +1428,8 @@
return NULL;
}
#endif
- return PyUnicode_FromString(*(char **)ptr);
+ return PyBytes_FromStringAndSize(*(char **)ptr,
+ strlen(*(char **)ptr));
} else {
Py_INCREF(Py_None);
return Py_None;
More information about the Python-checkins
mailing list