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
b8285d96
Kaydet (Commit)
b8285d96
authored
Şub 21, 2015
tarafından
Serhiy Storchaka
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Issue #22113: struct.pack_into() now supports new buffer protocol (in
particular accepts writable memoryview).
üst
4809d1fc
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
29 additions
and
15 deletions
+29
-15
test_struct.py
Lib/test/test_struct.py
+14
-5
NEWS
Misc/NEWS
+3
-0
_struct.c
Modules/_struct.c
+12
-10
No files found.
Lib/test/test_struct.py
Dosyayı görüntüle @
b8285d96
...
...
@@ -433,24 +433,24 @@ class StructTest(unittest.TestCase):
self
.
assertRaises
(
struct
.
error
,
s
.
unpack_from
,
data
,
i
)
self
.
assertRaises
(
struct
.
error
,
struct
.
unpack_from
,
fmt
,
data
,
i
)
def
test_pack_into
(
self
):
def
test_pack_into
(
self
,
cls
=
bytearray
,
tobytes
=
str
):
test_string
=
'Reykjavik rocks, eow!'
writable_buf
=
array
.
array
(
'c'
,
' '
*
100
)
writable_buf
=
cls
(
' '
*
100
)
fmt
=
'21s'
s
=
struct
.
Struct
(
fmt
)
# Test without offset
s
.
pack_into
(
writable_buf
,
0
,
test_string
)
from_buf
=
writable_buf
.
tostring
(
)[:
len
(
test_string
)]
from_buf
=
tobytes
(
writable_buf
)[:
len
(
test_string
)]
self
.
assertEqual
(
from_buf
,
test_string
)
# Test with offset.
s
.
pack_into
(
writable_buf
,
10
,
test_string
)
from_buf
=
writable_buf
.
tostring
(
)[:
len
(
test_string
)
+
10
]
from_buf
=
tobytes
(
writable_buf
)[:
len
(
test_string
)
+
10
]
self
.
assertEqual
(
from_buf
,
test_string
[:
10
]
+
test_string
)
# Go beyond boundaries.
small_buf
=
array
.
array
(
'c'
,
' '
*
10
)
small_buf
=
cls
(
' '
*
10
)
self
.
assertRaises
((
ValueError
,
struct
.
error
),
s
.
pack_into
,
small_buf
,
0
,
test_string
)
self
.
assertRaises
((
ValueError
,
struct
.
error
),
s
.
pack_into
,
small_buf
,
2
,
...
...
@@ -461,6 +461,15 @@ class StructTest(unittest.TestCase):
self
.
assertRaises
((
TypeError
,
struct
.
error
),
struct
.
pack_into
,
b
''
,
sb
,
None
)
def
test_pack_into_array
(
self
):
self
.
test_pack_into
(
cls
=
lambda
b
:
array
.
array
(
'c'
,
b
),
tobytes
=
array
.
array
.
tostring
)
def
test_pack_into_memoryview
(
self
):
# Issue #22113
self
.
test_pack_into
(
cls
=
lambda
b
:
memoryview
(
bytearray
(
b
)),
tobytes
=
memoryview
.
tobytes
)
def
test_pack_into_fn
(
self
):
test_string
=
'Reykjavik rocks, eow!'
writable_buf
=
array
.
array
(
'c'
,
' '
*
100
)
...
...
Misc/NEWS
Dosyayı görüntüle @
b8285d96
...
...
@@ -18,6 +18,9 @@ Core and Builtins
Library
-------
- Issue #22113: struct.pack_into() now supports new buffer protocol (in
particular accepts writable memoryview).
- Issues #814253, #9179: Warnings now are raised when group references and
conditional group references are used in lookbehind assertions in regular
expressions.
...
...
Modules/_struct.c
Dosyayı görüntüle @
b8285d96
...
...
@@ -1657,8 +1657,8 @@ static PyObject *
s_pack_into
(
PyObject
*
self
,
PyObject
*
args
)
{
PyStructObject
*
soself
;
char
*
buffer
;
Py_ssize_t
buffer_len
,
offset
;
Py_buffer
buf
;
Py_ssize_t
offset
;
/* Validate arguments. +1 is for the first arg as buffer. */
soself
=
(
PyStructObject
*
)
self
;
...
...
@@ -1683,33 +1683,35 @@ s_pack_into(PyObject *self, PyObject *args)
}
/* Extract a writable memory buffer from the first argument */
if
(
PyObject_AsWriteBuffer
(
PyTuple_GET_ITEM
(
args
,
0
),
(
void
**
)
&
buffer
,
&
buffer_len
)
==
-
1
)
{
if
(
!
PyArg_Parse
(
PyTuple_GET_ITEM
(
args
,
0
),
"w*"
,
&
buf
))
return
NULL
;
}
assert
(
buffer_len
>=
0
);
/* Extract the offset from the first argument */
offset
=
PyInt_AsSsize_t
(
PyTuple_GET_ITEM
(
args
,
1
));
if
(
offset
==
-
1
&&
PyErr_Occurred
())
if
(
offset
==
-
1
&&
PyErr_Occurred
())
{
PyBuffer_Release
(
&
buf
);
return
NULL
;
}
/* Support negative offsets. */
if
(
offset
<
0
)
offset
+=
buf
fer_
len
;
offset
+=
buf
.
len
;
/* Check boundaries */
if
(
offset
<
0
||
(
buf
fer_
len
-
offset
)
<
soself
->
s_size
)
{
if
(
offset
<
0
||
(
buf
.
len
-
offset
)
<
soself
->
s_size
)
{
PyErr_Format
(
StructError
,
"pack_into requires a buffer of at least %zd bytes"
,
soself
->
s_size
);
PyBuffer_Release
(
&
buf
);
return
NULL
;
}
/* Call the guts */
if
(
s_pack_internal
(
soself
,
args
,
2
,
buffer
+
offset
)
!=
0
)
{
if
(
s_pack_internal
(
soself
,
args
,
2
,
(
char
*
)
buf
.
buf
+
offset
)
!=
0
)
{
PyBuffer_Release
(
&
buf
);
return
NULL
;
}
PyBuffer_Release
(
&
buf
);
Py_RETURN_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