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
9e7a4c97
Commit
9e7a4c97
authored
Mar 22, 2010
by
Florent Xicluna
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue #7703: ctypes supports both buffer() and memoryview(). The former is deprecated.
Complement of r79288.
parent
c9d1a784
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
25 additions
and
6 deletions
+25
-6
Lib/ctypes/test/test_strings.py
Lib/ctypes/test/test_strings.py
+9
-2
Misc/NEWS
Misc/NEWS
+3
-0
Modules/_ctypes/_ctypes.c
Modules/_ctypes/_ctypes.c
+13
-4
No files found.
Lib/ctypes/test/test_strings.py
View file @
9e7a4c97
import
unittest
from
ctypes
import
*
from
test
import
test_support
class
StringArrayTestCase
(
unittest
.
TestCase
):
def
test
(
self
):
...
...
@@ -24,7 +25,7 @@ class StringArrayTestCase(unittest.TestCase):
self
.
assertRaises
(
ValueError
,
setattr
,
buf
,
"value"
,
"aaaaaaaa"
)
self
.
assertRaises
(
TypeError
,
setattr
,
buf
,
"value"
,
42
)
def
test_c_buffer_value
(
self
):
def
test_c_buffer_value
(
self
,
memoryview
=
memoryview
):
buf
=
c_buffer
(
32
)
buf
.
value
=
"Hello, World"
...
...
@@ -34,7 +35,7 @@ class StringArrayTestCase(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
setattr
,
buf
,
"value"
,
memoryview
(
"abc"
))
self
.
assertRaises
(
ValueError
,
setattr
,
buf
,
"raw"
,
memoryview
(
"x"
*
100
))
def
test_c_buffer_raw
(
self
):
def
test_c_buffer_raw
(
self
,
memoryview
=
memoryview
):
buf
=
c_buffer
(
32
)
buf
.
raw
=
memoryview
(
"Hello, World"
)
...
...
@@ -42,6 +43,12 @@ class StringArrayTestCase(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
setattr
,
buf
,
"value"
,
memoryview
(
"abc"
))
self
.
assertRaises
(
ValueError
,
setattr
,
buf
,
"raw"
,
memoryview
(
"x"
*
100
))
def
test_c_buffer_deprecated
(
self
):
# Compatibility with 2.x
with
test_support
.
check_py3k_warnings
():
self
.
test_c_buffer_value
(
buffer
)
self
.
test_c_buffer_raw
(
buffer
)
def
test_param_1
(
self
):
BUF
=
c_char
*
4
buf
=
BUF
()
...
...
Misc/NEWS
View file @
9e7a4c97
...
...
@@ -29,6 +29,9 @@ Core and Builtins
Library
-------
- Issue #7703: ctypes supports both buffer() and memoryview(). The former is
deprecated.
- Issue #7860: platform.uname now reports the correct 'machine' type
when Python is running in WOW64 mode on 64 bit Windows.
...
...
Modules/_ctypes/_ctypes.c
View file @
9e7a4c97
...
...
@@ -1076,22 +1076,31 @@ CharArray_set_raw(CDataObject *self, PyObject *value)
{
char
*
ptr
;
Py_ssize_t
size
;
Py_buffer
view
=
{
0
};
if
(
PyBuffer_Check
(
value
))
{
size
=
Py_TYPE
(
value
)
->
tp_as_buffer
->
bf_getreadbuffer
(
value
,
0
,
(
void
*
)
&
ptr
);
if
(
size
<
0
)
return
-
1
;
}
else
if
(
-
1
==
PyString_AsStringAndSize
(
value
,
&
ptr
,
&
size
))
{
return
-
1
;
goto
fail
;
}
else
{
if
(
PyObject_GetBuffer
(
value
,
&
view
,
PyBUF_SIMPLE
)
<
0
)
goto
fail
;
size
=
view
.
len
;
ptr
=
view
.
buf
;
}
if
(
size
>
self
->
b_size
)
{
PyErr_SetString
(
PyExc_ValueError
,
"string too long"
);
return
-
1
;
goto
fail
;
}
memcpy
(
self
->
b_ptr
,
ptr
,
size
);
PyBuffer_Release
(
&
view
);
return
0
;
fail:
PyBuffer_Release
(
&
view
);
return
-
1
;
}
static
PyObject
*
...
...
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