Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
19b52545
Commit
19b52545
authored
Jul 13, 2007
by
Thomas Heller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
c_char, c_char_p objects and c_char array structure fields return
their value now as str, no longer str8.
parent
27384da6
Changes
7
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
29 additions
and
40 deletions
+29
-40
Lib/ctypes/test/test_buffers.py
Lib/ctypes/test/test_buffers.py
+3
-3
Lib/ctypes/test/test_bytes.py
Lib/ctypes/test/test_bytes.py
+6
-2
Lib/ctypes/test/test_objects.py
Lib/ctypes/test/test_objects.py
+1
-1
Lib/ctypes/test/test_random_things.py
Lib/ctypes/test/test_random_things.py
+1
-1
Lib/ctypes/test/test_repr.py
Lib/ctypes/test/test_repr.py
+1
-1
Lib/ctypes/test/test_slicing.py
Lib/ctypes/test/test_slicing.py
+1
-1
Modules/_ctypes/cfield.c
Modules/_ctypes/cfield.c
+16
-31
No files found.
Lib/ctypes/test/test_buffers.py
View file @
19b52545
...
@@ -7,12 +7,12 @@ class StringBufferTestCase(unittest.TestCase):
...
@@ -7,12 +7,12 @@ class StringBufferTestCase(unittest.TestCase):
b
=
create_string_buffer
(
32
)
b
=
create_string_buffer
(
32
)
self
.
failUnlessEqual
(
len
(
b
),
32
)
self
.
failUnlessEqual
(
len
(
b
),
32
)
self
.
failUnlessEqual
(
sizeof
(
b
),
32
*
sizeof
(
c_char
))
self
.
failUnlessEqual
(
sizeof
(
b
),
32
*
sizeof
(
c_char
))
self
.
failUnless
(
type
(
b
[
0
])
is
str
8
)
self
.
failUnless
(
type
(
b
[
0
])
is
str
)
b
=
create_string_buffer
(
"abc"
)
b
=
create_string_buffer
(
"abc"
)
self
.
failUnlessEqual
(
len
(
b
),
4
)
# trailing nul char
self
.
failUnlessEqual
(
len
(
b
),
4
)
# trailing nul char
self
.
failUnlessEqual
(
sizeof
(
b
),
4
*
sizeof
(
c_char
))
self
.
failUnlessEqual
(
sizeof
(
b
),
4
*
sizeof
(
c_char
))
self
.
failUnless
(
type
(
b
[
0
])
is
str
8
)
self
.
failUnless
(
type
(
b
[
0
])
is
str
)
self
.
failUnlessEqual
(
b
[
0
],
"a"
)
self
.
failUnlessEqual
(
b
[
0
],
"a"
)
self
.
failUnlessEqual
(
b
[:],
"abc
\
0
"
)
self
.
failUnlessEqual
(
b
[:],
"abc
\
0
"
)
...
@@ -20,7 +20,7 @@ class StringBufferTestCase(unittest.TestCase):
...
@@ -20,7 +20,7 @@ class StringBufferTestCase(unittest.TestCase):
b
=
create_string_buffer
(
"abc"
)
b
=
create_string_buffer
(
"abc"
)
self
.
failUnlessEqual
(
len
(
b
),
4
)
# trailing nul char
self
.
failUnlessEqual
(
len
(
b
),
4
)
# trailing nul char
self
.
failUnlessEqual
(
sizeof
(
b
),
4
*
sizeof
(
c_char
))
self
.
failUnlessEqual
(
sizeof
(
b
),
4
*
sizeof
(
c_char
))
self
.
failUnless
(
type
(
b
[
0
])
is
str
8
)
self
.
failUnless
(
type
(
b
[
0
])
is
str
)
self
.
failUnlessEqual
(
b
[
0
],
"a"
)
self
.
failUnlessEqual
(
b
[
0
],
"a"
)
self
.
failUnlessEqual
(
b
[:],
"abc
\
0
"
)
self
.
failUnlessEqual
(
b
[:],
"abc
\
0
"
)
...
...
Lib/ctypes/test/test_bytes.py
View file @
19b52545
...
@@ -29,14 +29,18 @@ class BytesTest(unittest.TestCase):
...
@@ -29,14 +29,18 @@ class BytesTest(unittest.TestCase):
_fields_
=
[(
"a"
,
c_char
*
3
)]
_fields_
=
[(
"a"
,
c_char
*
3
)]
X
(
"abc"
)
X
(
"abc"
)
X
(
b"abc"
)
x
=
X
(
b"abc"
)
self
.
assertEqual
(
x
.
a
,
"abc"
)
self
.
assertEqual
(
type
(
x
.
a
),
str
)
def
test_struct_W
(
self
):
def
test_struct_W
(
self
):
class
X
(
Structure
):
class
X
(
Structure
):
_fields_
=
[(
"a"
,
c_wchar
*
3
)]
_fields_
=
[(
"a"
,
c_wchar
*
3
)]
X
(
"abc"
)
X
(
"abc"
)
X
(
b"abc"
)
x
=
X
(
b"abc"
)
self
.
assertEqual
(
x
.
a
,
"abc"
)
self
.
assertEqual
(
type
(
x
.
a
),
str
)
if
sys
.
platform
==
"win32"
:
if
sys
.
platform
==
"win32"
:
def
test_BSTR
(
self
):
def
test_BSTR
(
self
):
...
...
Lib/ctypes/test/test_objects.py
View file @
19b52545
...
@@ -24,7 +24,7 @@ assigned from Python must be kept.
...
@@ -24,7 +24,7 @@ assigned from Python must be kept.
>>> array._objects
>>> array._objects
{'4': b'foo bar'}
{'4': b'foo bar'}
>>> array[4]
>>> array[4]
s
'foo bar'
'foo bar'
>>>
>>>
It gets more complicated when the ctypes instance itself is contained
It gets more complicated when the ctypes instance itself is contained
...
...
Lib/ctypes/test/test_random_things.py
View file @
19b52545
...
@@ -69,7 +69,7 @@ class CallbackTracbackTestCase(unittest.TestCase):
...
@@ -69,7 +69,7 @@ class CallbackTracbackTestCase(unittest.TestCase):
out
=
self
.
capture_stderr
(
cb
,
"spam"
)
out
=
self
.
capture_stderr
(
cb
,
"spam"
)
self
.
failUnlessEqual
(
out
.
splitlines
()[
-
1
],
self
.
failUnlessEqual
(
out
.
splitlines
()[
-
1
],
"TypeError: "
"TypeError: "
"unsupported operand type(s) for /: 'int' and 'str
8
'"
)
"unsupported operand type(s) for /: 'int' and 'str'"
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
Lib/ctypes/test/test_repr.py
View file @
19b52545
...
@@ -22,7 +22,7 @@ class ReprTest(unittest.TestCase):
...
@@ -22,7 +22,7 @@ class ReprTest(unittest.TestCase):
self
.
failUnlessEqual
(
"<X object at"
,
repr
(
typ
(
42
))[:
12
])
self
.
failUnlessEqual
(
"<X object at"
,
repr
(
typ
(
42
))[:
12
])
def
test_char
(
self
):
def
test_char
(
self
):
self
.
failUnlessEqual
(
"c_char(
s
'x')"
,
repr
(
c_char
(
'x'
)))
self
.
failUnlessEqual
(
"c_char('x')"
,
repr
(
c_char
(
'x'
)))
self
.
failUnlessEqual
(
"<X object at"
,
repr
(
X
(
'x'
))[:
12
])
self
.
failUnlessEqual
(
"<X object at"
,
repr
(
X
(
'x'
))[:
12
])
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
Lib/ctypes/test/test_slicing.py
View file @
19b52545
...
@@ -70,7 +70,7 @@ class SlicesTestCase(unittest.TestCase):
...
@@ -70,7 +70,7 @@ class SlicesTestCase(unittest.TestCase):
dll
.
my_strdup
.
errcheck
=
errcheck
dll
.
my_strdup
.
errcheck
=
errcheck
try
:
try
:
res
=
dll
.
my_strdup
(
s
)
res
=
dll
.
my_strdup
(
s
)
self
.
failUnlessEqual
(
res
,
s
)
self
.
failUnlessEqual
(
res
,
s
tr
(
s
)
)
finally
:
finally
:
del
dll
.
my_strdup
.
errcheck
del
dll
.
my_strdup
.
errcheck
...
...
Modules/_ctypes/cfield.c
View file @
19b52545
...
@@ -1137,9 +1137,7 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
...
@@ -1137,9 +1137,7 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
return
NULL
;
return
NULL
;
if
(
PyBytes_GET_SIZE
(
value
)
!=
1
)
{
if
(
PyBytes_GET_SIZE
(
value
)
!=
1
)
{
Py_DECREF
(
value
);
Py_DECREF
(
value
);
PyErr_Format
(
PyExc_TypeError
,
goto
error
;
"one character string expected"
);
return
NULL
;
}
}
*
(
char
*
)
ptr
=
PyBytes_AsString
(
value
)[
0
];
*
(
char
*
)
ptr
=
PyBytes_AsString
(
value
)[
0
];
Py_DECREF
(
value
);
Py_DECREF
(
value
);
...
@@ -1149,22 +1147,17 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
...
@@ -1149,22 +1147,17 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
*
(
char
*
)
ptr
=
PyBytes_AsString
(
value
)[
0
];
*
(
char
*
)
ptr
=
PyBytes_AsString
(
value
)[
0
];
_RET
(
value
);
_RET
(
value
);
}
}
/* XXX struni remove later */
error:
if
(
!
PyString_Check
(
value
)
||
(
1
!=
PyString_Size
(
value
)))
{
PyErr_Format
(
PyExc_TypeError
,
PyErr_Format
(
PyExc_TypeError
,
"one character string expected"
);
"one character string expected"
);
return
NULL
;
return
NULL
;
}
*
(
char
*
)
ptr
=
PyString_AS_STRING
(
value
)[
0
];
_RET
(
value
);
}
}
static
PyObject
*
static
PyObject
*
c_get
(
void
*
ptr
,
Py_ssize_t
size
)
c_get
(
void
*
ptr
,
Py_ssize_t
size
)
{
{
/* XXX struni return PyBytes (or PyUnicode?) later */
return
PyUnicode_FromStringAndSize
((
char
*
)
ptr
,
1
);
return
PyString_FromStringAndSize
((
char
*
)
ptr
,
1
);
}
}
#ifdef CTYPES_UNICODE
#ifdef CTYPES_UNICODE
...
@@ -1280,24 +1273,16 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
...
@@ -1280,24 +1273,16 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
static
PyObject
*
static
PyObject
*
s_get
(
void
*
ptr
,
Py_ssize_t
size
)
s_get
(
void
*
ptr
,
Py_ssize_t
size
)
{
{
Py
Object
*
result
;
Py
_ssize_t
i
;
size_t
slen
;
char
*
p
;
result
=
PyString_FromString
((
char
*
)
ptr
);
p
=
(
char
*
)
ptr
;
if
(
!
result
)
for
(
i
=
0
;
i
<
size
;
++
i
)
{
return
NULL
;
if
(
*
p
++
==
'\0'
)
/* chop off at the first NUL character, if any.
break
;
* On error, result will be deallocated and set to NULL.
}
*/
slen
=
strlen
(
PyString_AS_STRING
(
result
));
return
PyUnicode_FromStringAndSize
((
char
*
)
ptr
,
(
Py_ssize_t
)
i
);
size
=
min
(
size
,
(
Py_ssize_t
)
slen
);
if
(
result
->
ob_refcnt
==
1
)
{
/* shorten the result */
_PyString_Resize
(
&
result
,
size
);
return
result
;
}
else
/* cannot shorten the result */
return
PyString_FromStringAndSize
(
ptr
,
size
);
}
}
static
PyObject
*
static
PyObject
*
...
@@ -1393,7 +1378,7 @@ z_get(void *ptr, Py_ssize_t size)
...
@@ -1393,7 +1378,7 @@ z_get(void *ptr, Py_ssize_t size)
return
NULL
;
return
NULL
;
}
}
#endif
#endif
return
Py
String
_FromString
(
*
(
char
**
)
ptr
);
return
Py
Unicode
_FromString
(
*
(
char
**
)
ptr
);
}
else
{
}
else
{
Py_INCREF
(
Py_None
);
Py_INCREF
(
Py_None
);
return
Py_None
;
return
Py_None
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment