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
e5095e18
Commit
e5095e18
authored
Jul 13, 2007
by
Thomas Heller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Structure fields of type c_char array or c_wchar array accept bytes or
(unicode) string.
parent
745f5e2d
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
36 additions
and
2 deletions
+36
-2
Lib/ctypes/test/test_bytes.py
Lib/ctypes/test/test_bytes.py
+15
-0
Modules/_ctypes/cfield.c
Modules/_ctypes/cfield.c
+21
-2
No files found.
Lib/ctypes/test/test_bytes.py
View file @
e5095e18
"""Test where byte objects are accepted"""
import
unittest
from
ctypes
import
*
...
...
@@ -22,5 +23,19 @@ class BytesTest(unittest.TestCase):
c_wchar_p
(
"foo bar"
)
c_wchar_p
(
b"foo bar"
)
def
test_struct
(
self
):
class
X
(
Structure
):
_fields_
=
[(
"a"
,
c_char
*
3
)]
X
(
"abc"
)
X
(
b"abc"
)
def
test_struct_W
(
self
):
class
X
(
Structure
):
_fields_
=
[(
"a"
,
c_wchar
*
3
)]
X
(
"abc"
)
X
(
b"abc"
)
if
__name__
==
'__main__'
:
unittest
.
main
()
Modules/_ctypes/cfield.c
View file @
e5095e18
...
...
@@ -1260,7 +1260,7 @@ U_set(void *ptr, PyObject *value, Py_ssize_t length)
/* It's easier to calculate in characters than in bytes */
length
/=
sizeof
(
wchar_t
);
if
(
Py
String
_Check
(
value
))
{
if
(
Py
Bytes
_Check
(
value
))
{
value
=
PyUnicode_FromEncodedObject
(
value
,
conversion_mode_encoding
,
conversion_mode_errors
);
...
...
@@ -1322,7 +1322,23 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
char
*
data
;
Py_ssize_t
size
;
data
=
PyString_AsString
(
value
);
if
(
PyUnicode_Check
(
value
))
{
value
=
PyUnicode_AsEncodedString
(
value
,
conversion_mode_encoding
,
conversion_mode_errors
);
if
(
value
==
NULL
)
return
NULL
;
assert
(
PyBytes_Check
(
value
));
}
else
if
(
PyBytes_Check
(
value
))
{
Py_INCREF
(
value
);
}
else
{
PyErr_Format
(
PyExc_TypeError
,
"expected string, %s found"
,
value
->
ob_type
->
tp_name
);
return
NULL
;
}
data
=
PyBytes_AsString
(
value
);
if
(
!
data
)
return
NULL
;
size
=
strlen
(
data
);
...
...
@@ -1339,10 +1355,13 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
"string too long (%zd, maximum length %zd)"
,
#endif
size
,
length
);
Py_DECREF
(
value
);
return
NULL
;
}
/* Also copy the terminating NUL character if there is space */
memcpy
((
char
*
)
ptr
,
data
,
size
);
Py_DECREF
(
value
);
_RET
(
value
);
}
...
...
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