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
1fcdc232
Kaydet (Commit)
1fcdc232
authored
May 27, 2006
tarafından
Bob Ippolito
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Fix up struct docstrings, add struct.pack_to function for symmetry
üst
90bd0a55
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
8 deletions
+42
-8
struct.py
Lib/struct.py
+12
-0
test_struct.py
Lib/test/test_struct.py
+22
-0
_struct.c
Modules/_struct.c
+8
-8
No files found.
Lib/struct.py
Dosyayı görüntüle @
1fcdc232
...
@@ -62,6 +62,18 @@ def pack(fmt, *args):
...
@@ -62,6 +62,18 @@ def pack(fmt, *args):
o
=
_compile
(
fmt
)
o
=
_compile
(
fmt
)
return
o
.
pack
(
*
args
)
return
o
.
pack
(
*
args
)
def
pack_to
(
fmt
,
buf
,
offset
,
*
args
):
"""
Pack the values v2, v2, ... according to fmt, write
the packed bytes into the writable buffer buf starting at offset.
See struct.__doc__ for more on format strings.
"""
try
:
o
=
_cache
[
fmt
]
except
KeyError
:
o
=
_compile
(
fmt
)
return
o
.
pack_to
(
buf
,
offset
,
*
args
)
def
unpack
(
fmt
,
s
):
def
unpack
(
fmt
,
s
):
"""
"""
Unpack the string, containing packed C structure data, according
Unpack the string, containing packed C structure data, according
...
...
Lib/test/test_struct.py
Dosyayı görüntüle @
1fcdc232
...
@@ -509,6 +509,28 @@ class PackBufferTestCase(unittest.TestCase):
...
@@ -509,6 +509,28 @@ class PackBufferTestCase(unittest.TestCase):
self
.
assertRaises
(
struct
.
error
,
s
.
pack_to
,
small_buf
,
0
,
test_string
)
self
.
assertRaises
(
struct
.
error
,
s
.
pack_to
,
small_buf
,
0
,
test_string
)
self
.
assertRaises
(
struct
.
error
,
s
.
pack_to
,
small_buf
,
2
,
test_string
)
self
.
assertRaises
(
struct
.
error
,
s
.
pack_to
,
small_buf
,
2
,
test_string
)
def
test_pack_to_fn
(
self
):
test_string
=
'Reykjavik rocks, eow!'
writable_buf
=
array
.
array
(
'c'
,
' '
*
100
)
fmt
=
'21s'
pack_to
=
lambda
*
args
:
struct
.
pack_to
(
fmt
,
*
args
)
# Test without offset
pack_to
(
writable_buf
,
0
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)]
self
.
assertEquals
(
from_buf
,
test_string
)
# Test with offset.
pack_to
(
writable_buf
,
10
,
test_string
)
from_buf
=
writable_buf
.
tostring
()[:
len
(
test_string
)
+
10
]
self
.
assertEquals
(
from_buf
,
(
test_string
[:
10
]
+
test_string
))
# Go beyond boundaries.
small_buf
=
array
.
array
(
'c'
,
' '
*
10
)
self
.
assertRaises
(
struct
.
error
,
pack_to
,
small_buf
,
0
,
test_string
)
self
.
assertRaises
(
struct
.
error
,
pack_to
,
small_buf
,
2
,
test_string
)
def
test_main
():
def
test_main
():
test
.
test_support
.
run_unittest
(
PackBufferTestCase
)
test
.
test_support
.
run_unittest
(
PackBufferTestCase
)
...
...
Modules/_struct.c
Dosyayı görüntüle @
1fcdc232
...
@@ -1277,7 +1277,7 @@ fail:
...
@@ -1277,7 +1277,7 @@ fail:
PyDoc_STRVAR
(
s_unpack__doc__
,
PyDoc_STRVAR
(
s_unpack__doc__
,
"unpack(str) -> (v1, v2, ...)
\n
\
"
S.
unpack(str) -> (v1, v2, ...)
\n
\
\n
\
\n
\
Return tuple containing values unpacked according to this Struct's format.
\n
\
Return tuple containing values unpacked according to this Struct's format.
\n
\
Requires len(str) == self.size. See struct.__doc__ for more on format
\n
\
Requires len(str) == self.size. See struct.__doc__ for more on format
\n
\
...
@@ -1299,7 +1299,7 @@ s_unpack(PyObject *self, PyObject *inputstr)
...
@@ -1299,7 +1299,7 @@ s_unpack(PyObject *self, PyObject *inputstr)
}
}
PyDoc_STRVAR
(
s_unpack_from__doc__
,
PyDoc_STRVAR
(
s_unpack_from__doc__
,
"unpack_from(buffer[, offset]) -> (v1, v2, ...)
\n
\
"
S.
unpack_from(buffer[, offset]) -> (v1, v2, ...)
\n
\
\n
\
\n
\
Return tuple containing values unpacked according to this Struct's format.
\n
\
Return tuple containing values unpacked according to this Struct's format.
\n
\
Unlike unpack, unpack_from can unpack values from any object supporting
\n
\
Unlike unpack, unpack_from can unpack values from any object supporting
\n
\
...
@@ -1407,7 +1407,7 @@ s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
...
@@ -1407,7 +1407,7 @@ s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
PyDoc_STRVAR
(
s_pack__doc__
,
PyDoc_STRVAR
(
s_pack__doc__
,
"pack(v1, v2, ...) -> string
\n
\
"
S.
pack(v1, v2, ...) -> string
\n
\
\n
\
\n
\
Return a string containing values v1, v2, ... packed according to this
\n
\
Return a string containing values v1, v2, ... packed according to this
\n
\
Struct's format. See struct.__doc__ for more on format strings."
);
Struct's format. See struct.__doc__ for more on format strings."
);
...
@@ -1445,11 +1445,11 @@ s_pack(PyObject *self, PyObject *args)
...
@@ -1445,11 +1445,11 @@ s_pack(PyObject *self, PyObject *args)
}
}
PyDoc_STRVAR
(
s_pack_to__doc__
,
PyDoc_STRVAR
(
s_pack_to__doc__
,
"pack_to(buffer, offset, v1, v2, ...)
\n
\
"
S.
pack_to(buffer, offset, v1, v2, ...)
\n
\
\n
\
\n
\
Pack the values v2, v2, ... according to this Struct's format, write
\n
\
Pack the values v2, v2, ... according to this Struct's format, write
\n
\
the packed bytes into the
given buffer at the given offset. Note that
\n
\
the packed bytes into the
writable buffer buf starting at offset. Note
\n
\
the offset is not an optional argument. See struct.__doc__ for
\n
\
th
at th
e offset is not an optional argument. See struct.__doc__ for
\n
\
more on format strings."
);
more on format strings."
);
static
PyObject
*
static
PyObject
*
...
@@ -1530,8 +1530,8 @@ PyDoc_STRVAR(s__doc__, "Compiled struct object");
...
@@ -1530,8 +1530,8 @@ PyDoc_STRVAR(s__doc__, "Compiled struct object");
#define OFF(x) offsetof(PyStructObject, x)
#define OFF(x) offsetof(PyStructObject, x)
static
PyGetSetDef
s_getsetlist
[]
=
{
static
PyGetSetDef
s_getsetlist
[]
=
{
{
"format"
,
(
getter
)
s_get_format
,
(
setter
)
NULL
,
"
buffer's capacity
"
,
NULL
},
{
"format"
,
(
getter
)
s_get_format
,
(
setter
)
NULL
,
"
struct format string
"
,
NULL
},
{
"size"
,
(
getter
)
s_get_size
,
(
setter
)
NULL
,
"
buffer's position
"
,
NULL
},
{
"size"
,
(
getter
)
s_get_size
,
(
setter
)
NULL
,
"
struct size in bytes
"
,
NULL
},
{
NULL
}
/* sentinel */
{
NULL
}
/* sentinel */
};
};
...
...
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