Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
4f06d604
Kaydet (Commit)
4f06d604
authored
Agu 09, 2014
tarafından
Serhiy Storchaka
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #22161: Conformed arguments type checks in ctype to actually supported
types. Corrected error messages about bytes arguments.
üst
e4936b83
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
30 additions
and
28 deletions
+30
-28
__init__.py
Lib/ctypes/__init__.py
+2
-2
test_buffers.py
Lib/ctypes/test/test_buffers.py
+4
-0
test_bytes.py
Lib/ctypes/test/test_bytes.py
+15
-0
test_structures.py
Lib/ctypes/test/test_structures.py
+1
-1
_ctypes.c
Modules/_ctypes/_ctypes.c
+5
-22
cfield.c
Modules/_ctypes/cfield.c
+3
-3
No files found.
Lib/ctypes/__init__.py
Dosyayı görüntüle @
4f06d604
...
@@ -49,7 +49,7 @@ def create_string_buffer(init, size=None):
...
@@ -49,7 +49,7 @@ def create_string_buffer(init, size=None):
create_string_buffer(anInteger) -> character array
create_string_buffer(anInteger) -> character array
create_string_buffer(aString, anInteger) -> character array
create_string_buffer(aString, anInteger) -> character array
"""
"""
if
isinstance
(
init
,
(
str
,
bytes
)
):
if
isinstance
(
init
,
bytes
):
if
size
is
None
:
if
size
is
None
:
size
=
len
(
init
)
+
1
size
=
len
(
init
)
+
1
buftype
=
c_char
*
size
buftype
=
c_char
*
size
...
@@ -284,7 +284,7 @@ def create_unicode_buffer(init, size=None):
...
@@ -284,7 +284,7 @@ def create_unicode_buffer(init, size=None):
create_unicode_buffer(anInteger) -> character array
create_unicode_buffer(anInteger) -> character array
create_unicode_buffer(aString, anInteger) -> character array
create_unicode_buffer(aString, anInteger) -> character array
"""
"""
if
isinstance
(
init
,
(
str
,
bytes
)
):
if
isinstance
(
init
,
str
):
if
size
is
None
:
if
size
is
None
:
size
=
len
(
init
)
+
1
size
=
len
(
init
)
+
1
buftype
=
c_wchar
*
size
buftype
=
c_wchar
*
size
...
...
Lib/ctypes/test/test_buffers.py
Dosyayı görüntüle @
4f06d604
...
@@ -21,6 +21,8 @@ class StringBufferTestCase(unittest.TestCase):
...
@@ -21,6 +21,8 @@ class StringBufferTestCase(unittest.TestCase):
self
.
assertEqual
(
b
[::
2
],
b
"ac"
)
self
.
assertEqual
(
b
[::
2
],
b
"ac"
)
self
.
assertEqual
(
b
[::
5
],
b
"a"
)
self
.
assertEqual
(
b
[::
5
],
b
"a"
)
self
.
assertRaises
(
TypeError
,
create_string_buffer
,
"abc"
)
def
test_buffer_interface
(
self
):
def
test_buffer_interface
(
self
):
self
.
assertEqual
(
len
(
bytearray
(
create_string_buffer
(
0
))),
0
)
self
.
assertEqual
(
len
(
bytearray
(
create_string_buffer
(
0
))),
0
)
self
.
assertEqual
(
len
(
bytearray
(
create_string_buffer
(
1
))),
1
)
self
.
assertEqual
(
len
(
bytearray
(
create_string_buffer
(
1
))),
1
)
...
@@ -43,6 +45,8 @@ class StringBufferTestCase(unittest.TestCase):
...
@@ -43,6 +45,8 @@ class StringBufferTestCase(unittest.TestCase):
self
.
assertEqual
(
b
[::
2
],
"ac"
)
self
.
assertEqual
(
b
[::
2
],
"ac"
)
self
.
assertEqual
(
b
[::
5
],
"a"
)
self
.
assertEqual
(
b
[::
5
],
"a"
)
self
.
assertRaises
(
TypeError
,
create_unicode_buffer
,
b
"abc"
)
@need_symbol
(
'c_wchar'
)
@need_symbol
(
'c_wchar'
)
def
test_unicode_conversion
(
self
):
def
test_unicode_conversion
(
self
):
b
=
create_unicode_buffer
(
"abc"
)
b
=
create_unicode_buffer
(
"abc"
)
...
...
Lib/ctypes/test/test_bytes.py
Dosyayı görüntüle @
4f06d604
...
@@ -6,27 +6,40 @@ from ctypes import *
...
@@ -6,27 +6,40 @@ from ctypes import *
class
BytesTest
(
unittest
.
TestCase
):
class
BytesTest
(
unittest
.
TestCase
):
def
test_c_char
(
self
):
def
test_c_char
(
self
):
x
=
c_char
(
b
"x"
)
x
=
c_char
(
b
"x"
)
self
.
assertRaises
(
TypeError
,
c_char
,
"x"
)
x
.
value
=
b
"y"
x
.
value
=
b
"y"
with
self
.
assertRaises
(
TypeError
):
x
.
value
=
"y"
c_char
.
from_param
(
b
"x"
)
c_char
.
from_param
(
b
"x"
)
self
.
assertRaises
(
TypeError
,
c_char
.
from_param
,
"x"
)
(
c_char
*
3
)(
b
"a"
,
b
"b"
,
b
"c"
)
(
c_char
*
3
)(
b
"a"
,
b
"b"
,
b
"c"
)
self
.
assertRaises
(
TypeError
,
c_char
*
3
,
"a"
,
"b"
,
"c"
)
def
test_c_wchar
(
self
):
def
test_c_wchar
(
self
):
x
=
c_wchar
(
"x"
)
x
=
c_wchar
(
"x"
)
self
.
assertRaises
(
TypeError
,
c_wchar
,
b
"x"
)
x
.
value
=
"y"
x
.
value
=
"y"
with
self
.
assertRaises
(
TypeError
):
x
.
value
=
b
"y"
c_wchar
.
from_param
(
"x"
)
c_wchar
.
from_param
(
"x"
)
self
.
assertRaises
(
TypeError
,
c_wchar
.
from_param
,
b
"x"
)
(
c_wchar
*
3
)(
"a"
,
"b"
,
"c"
)
(
c_wchar
*
3
)(
"a"
,
"b"
,
"c"
)
self
.
assertRaises
(
TypeError
,
c_wchar
*
3
,
b
"a"
,
b
"b"
,
b
"c"
)
def
test_c_char_p
(
self
):
def
test_c_char_p
(
self
):
c_char_p
(
b
"foo bar"
)
c_char_p
(
b
"foo bar"
)
self
.
assertRaises
(
TypeError
,
c_char_p
,
"foo bar"
)
def
test_c_wchar_p
(
self
):
def
test_c_wchar_p
(
self
):
c_wchar_p
(
"foo bar"
)
c_wchar_p
(
"foo bar"
)
self
.
assertRaises
(
TypeError
,
c_wchar_p
,
b
"foo bar"
)
def
test_struct
(
self
):
def
test_struct
(
self
):
class
X
(
Structure
):
class
X
(
Structure
):
_fields_
=
[(
"a"
,
c_char
*
3
)]
_fields_
=
[(
"a"
,
c_char
*
3
)]
x
=
X
(
b
"abc"
)
x
=
X
(
b
"abc"
)
self
.
assertRaises
(
TypeError
,
X
,
"abc"
)
self
.
assertEqual
(
x
.
a
,
b
"abc"
)
self
.
assertEqual
(
x
.
a
,
b
"abc"
)
self
.
assertEqual
(
type
(
x
.
a
),
bytes
)
self
.
assertEqual
(
type
(
x
.
a
),
bytes
)
...
@@ -35,6 +48,7 @@ class BytesTest(unittest.TestCase):
...
@@ -35,6 +48,7 @@ class BytesTest(unittest.TestCase):
_fields_
=
[(
"a"
,
c_wchar
*
3
)]
_fields_
=
[(
"a"
,
c_wchar
*
3
)]
x
=
X
(
"abc"
)
x
=
X
(
"abc"
)
self
.
assertRaises
(
TypeError
,
X
,
b
"abc"
)
self
.
assertEqual
(
x
.
a
,
"abc"
)
self
.
assertEqual
(
x
.
a
,
"abc"
)
self
.
assertEqual
(
type
(
x
.
a
),
str
)
self
.
assertEqual
(
type
(
x
.
a
),
str
)
...
@@ -46,5 +60,6 @@ class BytesTest(unittest.TestCase):
...
@@ -46,5 +60,6 @@ class BytesTest(unittest.TestCase):
BSTR
(
"abc"
)
BSTR
(
"abc"
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
unittest
.
main
()
unittest
.
main
()
Lib/ctypes/test/test_structures.py
Dosyayı görüntüle @
4f06d604
...
@@ -322,7 +322,7 @@ class StructureTestCase(unittest.TestCase):
...
@@ -322,7 +322,7 @@ class StructureTestCase(unittest.TestCase):
self
.
assertEqual
(
cls
,
RuntimeError
)
self
.
assertEqual
(
cls
,
RuntimeError
)
self
.
assertEqual
(
msg
,
self
.
assertEqual
(
msg
,
"(Phone) <class 'TypeError'>: "
"(Phone) <class 'TypeError'>: "
"expected
string
, int found"
)
"expected
bytes
, int found"
)
cls
,
msg
=
self
.
get_except
(
Person
,
b
"Someone"
,
(
b
"a"
,
b
"b"
,
b
"c"
))
cls
,
msg
=
self
.
get_except
(
Person
,
b
"Someone"
,
(
b
"a"
,
b
"b"
,
b
"c"
))
self
.
assertEqual
(
cls
,
RuntimeError
)
self
.
assertEqual
(
cls
,
RuntimeError
)
...
...
Modules/_ctypes/_ctypes.c
Dosyayı görüntüle @
4f06d604
...
@@ -1080,7 +1080,7 @@ CharArray_set_raw(CDataObject *self, PyObject *value)
...
@@ -1080,7 +1080,7 @@ CharArray_set_raw(CDataObject *self, PyObject *value)
ptr
=
view
.
buf
;
ptr
=
view
.
buf
;
if
(
size
>
self
->
b_size
)
{
if
(
size
>
self
->
b_size
)
{
PyErr_SetString
(
PyExc_ValueError
,
PyErr_SetString
(
PyExc_ValueError
,
"string too long"
);
"
byte
string too long"
);
goto
fail
;
goto
fail
;
}
}
...
@@ -1132,7 +1132,7 @@ CharArray_set_value(CDataObject *self, PyObject *value)
...
@@ -1132,7 +1132,7 @@ CharArray_set_value(CDataObject *self, PyObject *value)
size
=
PyBytes_GET_SIZE
(
value
);
size
=
PyBytes_GET_SIZE
(
value
);
if
(
size
>
self
->
b_size
)
{
if
(
size
>
self
->
b_size
)
{
PyErr_SetString
(
PyExc_ValueError
,
PyErr_SetString
(
PyExc_ValueError
,
"string too long"
);
"
byte
string too long"
);
Py_DECREF
(
value
);
Py_DECREF
(
value
);
return
-
1
;
return
-
1
;
}
}
...
@@ -1471,7 +1471,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
...
@@ -1471,7 +1471,7 @@ c_wchar_p_from_param(PyObject *type, PyObject *value)
Py_INCREF
(
Py_None
);
Py_INCREF
(
Py_None
);
return
Py_None
;
return
Py_None
;
}
}
if
(
PyUnicode_Check
(
value
)
||
PyBytes_Check
(
value
)
)
{
if
(
PyUnicode_Check
(
value
))
{
PyCArgObject
*
parg
;
PyCArgObject
*
parg
;
struct
fielddesc
*
fd
=
_ctypes_get_fielddesc
(
"Z"
);
struct
fielddesc
*
fd
=
_ctypes_get_fielddesc
(
"Z"
);
...
@@ -1623,25 +1623,8 @@ c_void_p_from_param(PyObject *type, PyObject *value)
...
@@ -1623,25 +1623,8 @@ c_void_p_from_param(PyObject *type, PyObject *value)
return
(
PyObject
*
)
parg
;
return
(
PyObject
*
)
parg
;
}
}
/* XXX struni: remove later */
/* XXX struni: remove later */
/* string */
if
(
PyBytes_Check
(
value
))
{
PyCArgObject
*
parg
;
struct
fielddesc
*
fd
=
_ctypes_get_fielddesc
(
"z"
);
parg
=
PyCArgObject_new
();
if
(
parg
==
NULL
)
return
NULL
;
parg
->
pffi_type
=
&
ffi_type_pointer
;
parg
->
tag
=
'z'
;
parg
->
obj
=
fd
->
setfunc
(
&
parg
->
value
,
value
,
0
);
if
(
parg
->
obj
==
NULL
)
{
Py_DECREF
(
parg
);
return
NULL
;
}
return
(
PyObject
*
)
parg
;
}
/* bytes */
/* bytes */
if
(
PyByte
Array
_Check
(
value
))
{
if
(
PyByte
s
_Check
(
value
))
{
PyCArgObject
*
parg
;
PyCArgObject
*
parg
;
struct
fielddesc
*
fd
=
_ctypes_get_fielddesc
(
"z"
);
struct
fielddesc
*
fd
=
_ctypes_get_fielddesc
(
"z"
);
...
@@ -3218,7 +3201,7 @@ _get_name(PyObject *obj, char **pname)
...
@@ -3218,7 +3201,7 @@ _get_name(PyObject *obj, char **pname)
return
*
pname
?
1
:
0
;
return
*
pname
?
1
:
0
;
}
}
PyErr_SetString
(
PyExc_TypeError
,
PyErr_SetString
(
PyExc_TypeError
,
"function name must be string or integer"
);
"function name must be string
, bytes object
or integer"
);
return
0
;
return
0
;
}
}
...
...
Modules/_ctypes/cfield.c
Dosyayı görüntüle @
4f06d604
...
@@ -1160,7 +1160,7 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
...
@@ -1160,7 +1160,7 @@ c_set(void *ptr, PyObject *value, Py_ssize_t size)
}
}
error:
error:
PyErr_Format
(
PyExc_TypeError
,
PyErr_Format
(
PyExc_TypeError
,
"one character
string
expected"
);
"one character
bytes, bytearray or integer
expected"
);
return
NULL
;
return
NULL
;
}
}
...
@@ -1295,7 +1295,7 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
...
@@ -1295,7 +1295,7 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
Py_INCREF
(
value
);
Py_INCREF
(
value
);
}
else
{
}
else
{
PyErr_Format
(
PyExc_TypeError
,
PyErr_Format
(
PyExc_TypeError
,
"expected
string
, %s found"
,
"expected
bytes
, %s found"
,
value
->
ob_type
->
tp_name
);
value
->
ob_type
->
tp_name
);
return
NULL
;
return
NULL
;
}
}
...
@@ -1311,7 +1311,7 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
...
@@ -1311,7 +1311,7 @@ s_set(void *ptr, PyObject *value, Py_ssize_t length)
++
size
;
++
size
;
}
else
if
(
size
>
length
)
{
}
else
if
(
size
>
length
)
{
PyErr_Format
(
PyExc_ValueError
,
PyErr_Format
(
PyExc_ValueError
,
"
string
too long (%zd, maximum length %zd)"
,
"
bytes
too long (%zd, maximum length %zd)"
,
size
,
length
);
size
,
length
);
Py_DECREF
(
value
);
Py_DECREF
(
value
);
return
NULL
;
return
NULL
;
...
...
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