Side by Side Diff

Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Keyboard Shortcuts

File
u :up to issue
m :publish + mail comments
M :edit review message
j / k :jump to file after / before current file
J / K :jump to next file with a comment after / before current file
Side-by-side diff
i :toggle intra-line diffs
e :expand all comments
c :collapse all comments
s :toggle showing all comments
n / p :next / previous diff chunk or comment
N / P :next / previous comment
<Up> / <Down> :next / previous line
<Enter> :respond to / edit current comment
d :mark current comment as done
Issue
u :up to list of issues
m :publish + mail comments
j / k :jump to patch after / before current patch
o / <Enter> :open current patch in side-by-side view
i :open current patch in unified diff view
Issue List
j / k :jump to issue after / before current issue
o / <Enter> :open current issue
# : close issue
Comment/message editing
<Ctrl> + s or <Ctrl> + Enter :save comment
<Esc> :cancel edit
Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(619)
Issues Repositories Search
Open Issues | Closed Issues | All Issues | Sign in with your Google Account to create issues and add comments

Side by Side Diff: Modules/_struct.c

Issue 1258041: PEP 3318 - New struct string syntax Base URL: http://svn.python.org/view/*checkout*/python/branches/py3k/
Patch Set: Created 15 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_struct.py ('k') | no next file » | no next file with comments »
('i') | ('e') | ('c') | ('s')
OLDNEW
1 /* struct module -- pack values into and (out of) bytes objects */ 1 /* struct module -- pack values into and (out of) bytes objects */
2 2
3 /* New version supporting byte order, alignment and size options, 3 /* New version supporting byte order, alignment and size options,
4 character strings, and unsigned numbers */ 4 character strings, and unsigned numbers */
5 5
6 #define PY_SSIZE_T_CLEAN 6 #define PY_SSIZE_T_CLEAN
7 7
8 #include "Python.h" 8 #include "Python.h"
9 #include "structseq.h" 9 #include "structseq.h"
10 #include "structmember.h" 10 #include "structmember.h"
(...skipping 11 matching lines...) Loading...
22 int (*pack)(char *, PyObject *, 22 int (*pack)(char *, PyObject *,
23 const struct _formatdef *); 23 const struct _formatdef *);
24 } formatdef; 24 } formatdef;
25 25
26 typedef struct _formatcode { 26 typedef struct _formatcode {
27 const struct _formatdef *fmtdef; 27 const struct _formatdef *fmtdef;
28 Py_ssize_t offset; 28 Py_ssize_t offset;
29 Py_ssize_t size; 29 Py_ssize_t size;
30 } formatcode; 30 } formatcode;
31 31
32 /* A left child / right sibling tree used to represent the structure
33 of the format codes. Child nodes are added to represent nested
34 struct formats. */
35
36 typedef struct _formattree {
37 Py_ssize_t s_size;
38 Py_ssize_t s_len;
39 formatcode *s_codes;
40 struct _formattree *child;
41 struct _formattree *sibling;
42 } formattree;
43
44 /* Holds the state of the struct string parser. */
45
46 typedef struct _parser_state {
47 const formatdef *byte_fmt;
48 const char *fmt;
49 Py_ssize_t offset;
50 Py_ssize_t error_cnt;
51 } parser_state;
52
53 #define FormatTree_HasChildren(ft) ((ft)->child != 0)
54 #define FormatTree_AppendChild(ft, new_child) \
55 do { \
56 if ((ft)->child == NULL) { \
57 (ft)->child = (new_child); \
58 } else { \
59 formattree *t = (ft)->child; \
60 for ( ; t->sibling != NULL; t = t->sibling); \
61 t->sibling = (new_child); \
62 } \
63 } while (0)
64
32 /* Struct object interface */ 65 /* Struct object interface */
33 66
34 typedef struct { 67 typedef struct {
35 PyObject_HEAD 68 PyObject_HEAD
36 Py_ssize_t s_size; 69 formattree *s_tree;
37 Py_ssize_t s_len;
38 formatcode *s_codes;
39 PyObject *s_format; 70 PyObject *s_format;
40 PyObject *weakreflist; /* List of weak references */ 71 PyObject *weakreflist; /* List of weak references */
41 } PyStructObject; 72 } PyStructObject;
42 73
43 74
44 #define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType) 75 #define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
45 #define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType) 76 #define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
46 77
47 78
48 /* Exception */ 79 /* Exception */
(...skipping 36 matching lines...) | | Loading...
85 116
86 #define STRINGIFY(x) #x 117 #define STRINGIFY(x) #x
87 118
88 #ifdef __powerc 119 #ifdef __powerc
89 #pragma options align=reset 120 #pragma options align=reset
90 #endif 121 #endif
91 122
92 /* Helper for integer format codes: converts an arbitrary Python object to a 123 /* Helper for integer format codes: converts an arbitrary Python object to a
93 PyLongObject if possible, otherwise fails. Caller should decref. */ 124 PyLongObject if possible, otherwise fails. Caller should decref. */
94 125
126 /* Prototypes -- have to put these because the parser is mutually recursive. */
127
128 static formattree*
129 parse_struct_string(parser_state *state, formattree *root);
130
131 static formattree*
132 parse_struct(parser_state *state);
133
134 static formattree*
135 parse_primitive(parser_state *state);
136
137
138 static formattree *
139 formattree_new(void)
140 {
141 formattree * new_tree = PyMem_MALLOC(sizeof(formattree));
142 if (new_tree == NULL) {
143 PyErr_NoMemory();
144 return NULL;
145 }
146
147 new_tree->s_size = 0;
148 new_tree->s_len = 0;
149 new_tree->s_codes = NULL;
150 new_tree->child = NULL;
151 new_tree->sibling = NULL;
152
153 return new_tree;
154 }
155
156 static void
157 formattree_free_r(formattree *tree)
158 {
159 formattree *child = tree->child;
160 formattree *del_child = NULL;
161 while (child != NULL) {
162 if (FormatTree_HasChildren(child)) {
163 formattree_free_r(child);
164 } else {
165 PyMem_FREE(child->s_codes);
166 }
167 del_child = child;
168 child = child->sibling;
169 PyMem_FREE(del_child);
170 }
171 }
172
173 static void
174 formattree_free(formattree *tree)
175 {
176 formattree_free_r(tree);
177 PyMem_FREE(tree);
178 }
179
180 /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
181
95 static PyObject * 182 static PyObject *
96 get_pylong(PyObject *v) 183 get_pylong(PyObject *v)
97 { 184 {
98 assert(v != NULL); 185 assert(v != NULL);
99 if (!PyLong_Check(v)) { 186 if (!PyLong_Check(v)) {
100 /* Not an integer; try to use __index__ to convert. */ 187 /* Not an integer; try to use __index__ to convert. */
101 if (PyIndex_Check(v)) { 188 if (PyIndex_Check(v)) {
102 v = PyNumber_Index(v); 189 v = PyNumber_Index(v);
103 if (v == NULL) 190 if (v == NULL)
104 return NULL; 191 return NULL;
(...skipping 1046 matching lines...) | | Loading...
1151 if (e->format == c) { 1238 if (e->format == c) {
1152 if (e->alignment) { 1239 if (e->alignment) {
1153 size = ((size + e->alignment - 1) 1240 size = ((size + e->alignment - 1)
1154 / e->alignment) 1241 / e->alignment)
1155 * e->alignment; 1242 * e->alignment;
1156 } 1243 }
1157 } 1244 }
1158 return size; 1245 return size;
1159 } 1246 }
1160 1247
1161 1248 static void
1162 /* calculate the size of a format string */ 1249 whitespace(parser_state *state)
1250 {
1251 while ( (*state->fmt != '0円') && isspace(Py_CHARMASK(*state->fmt)))
1252 state->fmt++;
1253 }
1163 1254
1164 static int 1255 static int
1165 prepare_s(PyStructObject *self) 1256 match(parser_state *state, char c)
1166 { 1257 {
1258 if (*state->fmt == c) {
1259 state->fmt++;
1260 return 0;
1261 } else {
1262 return -1;
1263 }
1264 }
1265
1266 static int
1267 is_primitive(char c)
1268 {
1269 static char *primitive_codes = "xcbB?hHiIlLqQfdspP";
1270
1271 const char *begin;
1272 for (begin = primitive_codes; *begin; ++begin) {
1273 if (c == *begin)
1274 return 1;
1275 }
1276 return isdigit(c);
1277 }
1278
1279 static int
1280 is_byte_order_marker(char c)
1281 {
1282 static char *byte_order_codes = "<>!=@";
1283
1284 const char *begin;
1285 for (begin = byte_order_codes; *begin; ++begin) {
1286 if (c == *begin)
1287 return 1;
1288 }
1289 return 0;
1290 }
1291
1292 static void
1293 parse_error(parser_state *state, const char *error_msg)
1294 {
1295 PyErr_SetString(StructError, error_msg);
1296 state->error_cnt += 1;
1297 }
1298
1299 static formattree*
1300 parse_primitive(parser_state *state)
1301 {
1302 #define Return_NoMemory() \
1303 do { \
1304 PyErr_NoMemory(); \
1305 state->error_cnt += 1; \
1306 PyMem_FREE(tree); \
1307 return NULL; \
1308 } while (0)
1309
1310 formattree *tree = formattree_new();
1311 if (tree == NULL) {
1312 return NULL;
1313 }
1314
1167 const formatdef *f; 1315 const formatdef *f;
1168 const formatdef *e; 1316 const formatdef *e;
1169 formatcode *codes; 1317 formatcode *codes;
1170 1318
1171 const char *s; 1319 const char *s;
1172 const char *fmt;
1173 char c; 1320 char c;
1174 Py_ssize_t size, len, num, itemsize, x; 1321 Py_ssize_t size, len, num, itemsize, x;
1175 1322
1176 fmt = PyBytes_AS_STRING(self->s_format); 1323 s = state->fmt;
1177 1324 /* Start 'size' at '*offset' instead of 0 so that the proper alignment
1178 f = whichtable((char **)&fmt); 1325 is maintained. */
1179 1326 size = state->offset;
1180 s = fmt;
1181 size = 0;
1182 len = 0; 1327 len = 0;
1328 f = state->byte_fmt;
1183 while ((c = *s++) != '0円') { 1329 while ((c = *s++) != '0円') {
1184 if (isspace(Py_CHARMASK(c))) 1330 if (isspace(Py_CHARMASK(c)))
1185 continue; 1331 continue;
1186 if ('0' <= c && c <= '9') { 1332 if ('0' <= c && c <= '9') {
1187 num = c - '0'; 1333 num = c - '0';
1188 while ('0' <= (c = *s++) && c <= '9') { 1334 while ('0' <= (c = *s++) && c <= '9') {
1189 x = num*10 + (c - '0'); 1335 x = num*10 + (c - '0');
1190 if (x/10 != num) { 1336 if (x/10 != num) {
1191 PyErr_SetString( 1337 parse_error(state,
1192 StructError,
1193 "overflow in item count"); 1338 "overflow in item count");
1194 return -1; 1339 return NULL;
1195 } 1340 }
1196 num = x; 1341 num = x;
1197 } 1342 }
1198 if (c == '0円') 1343 if (c == '0円')
1199 break; 1344 break;
1200 } 1345 }
1201 else 1346 else
1202 num = 1; 1347 num = 1;
1348 if (!is_primitive(c))
1349 break;
1203 1350
1204 e = getentry(c, f); 1351 e = getentry(c, f);
1205 if (e == NULL) 1352 if (e == NULL)
1206 return -1; 1353 return NULL;
1207 1354
1208 switch (c) { 1355 switch (c) {
1209 case 's': /* fall through */ 1356 case 's': /* fall through */
1210 case 'p': len++; break; 1357 case 'p': len++; break;
1211 case 'x': break; 1358 case 'x': break;
1212 default: len += num; break; 1359 default: len += num; break;
1213 } 1360 }
1214 1361
1215 itemsize = e->size; 1362 itemsize = e->size;
1216 size = align(size, c, e); 1363 size = align(size, c, e);
1217 x = num * itemsize; 1364 x = num * itemsize;
1218 size += x; 1365 size += x;
1219 if (x/itemsize != num || size < 0) { 1366 if (x/itemsize != num || size < 0) {
1220 PyErr_SetString(StructError, 1367 parse_error(state, "total struct size too long");
1221 "total struct size too long"); 1368 return NULL;
1222 return -1;
1223 } 1369 }
1224 } 1370 }
1225 1371
1226 /* check for overflow */ 1372 /* check for overflow */
1227 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) { 1373 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1228 PyErr_NoMemory(); 1374 Return_NoMemory();
1229 return -1;
1230 } 1375 }
1231 1376
1232 self->s_size = size;
1233 self->s_len = len;
1234 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode)); 1377 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
1235 if (codes == NULL) { 1378 if (codes == NULL) {
1236 PyErr_NoMemory(); 1379 Return_NoMemory();
1237 return -1;
1238 } 1380 }
1239 self->s_codes = codes; 1381 tree->s_codes = codes;
1240 1382
1241 s = fmt; 1383 /* Start 'size' at '*offset' instead of 0 so that the proper alignment
1242 size = 0; 1384 is maintained. */
1243 while ((c = *s++) != '0円') { 1385 size = state->offset;
1386 f = state->byte_fmt;
1387 while ((c = *state->fmt++) != '0円') {
1244 if (isspace(Py_CHARMASK(c))) 1388 if (isspace(Py_CHARMASK(c)))
1245 continue; 1389 continue;
1246 if ('0' <= c && c <= '9') { 1390 if ('0' <= c && c <= '9') {
1247 num = c - '0'; 1391 num = c - '0';
1248 while ('0' <= (c = *s++) && c <= '9') 1392 while ('0' <= (c = *state->fmt++) && c <= '9')
1249 num = num*10 + (c - '0'); 1393 num = num*10 + (c - '0');
1250 if (c == '0円') 1394 if (c == '0円')
1251 break; 1395 break;
1252 } 1396 }
1253 else 1397 else
1254 num = 1; 1398 num = 1;
1399 if (!is_primitive(c))
1400 break;
1255 1401
1256 e = getentry(c, f); 1402 e = getentry(c, f);
1257 1403
1258 size = align(size, c, e); 1404 size = align(size, c, e);
1259 if (c == 's' || c == 'p') { 1405 if (c == 's' || c == 'p') {
1260 codes->offset = size; 1406 codes->offset = size;
1261 codes->size = num; 1407 codes->size = num;
1262 codes->fmtdef = e; 1408 codes->fmtdef = e;
1263 codes++; 1409 codes++;
1264 size += num; 1410 size += num;
1265 } else if (c == 'x') { 1411 } else if (c == 'x') {
1266 size += num; 1412 size += num;
1267 } else { 1413 } else {
1268 while (--num >= 0) { 1414 while (--num >= 0) {
1269 codes->offset = size; 1415 codes->offset = size;
1270 codes->size = e->size; 1416 codes->size = e->size;
1271 codes->fmtdef = e; 1417 codes->fmtdef = e;
1272 codes++; 1418 codes++;
1273 size += e->size; 1419 size += e->size;
1274 } 1420 }
1275 } 1421 }
1276 } 1422 }
1423 state->fmt--;
1277 codes->fmtdef = NULL; 1424 codes->fmtdef = NULL;
1278 codes->offset = size; 1425 codes->offset = size;
1279 codes->size = 0; 1426 codes->size = 0;
1427 /* Since we started 'size' at '*offset' to maintain alignment, we
1428 need to subtract out '*offset' to compute the true size. */
1429 tree->s_size = size - state->offset;
1430 tree->s_len = len;
1431 state->byte_fmt = f;
1280 1432
1281 return 0; 1433 return tree;
1434
1435 #undef Return_NoMemory
1436 }
1437
1438 static formattree*
1439 parse_struct(parser_state *state)
1440 {
1441 formattree *tree = NULL;
1442
1443 if (match(state, 'T') == 0) {
1444 whitespace(state);
1445 if (match(state, '{') == 0) {
1446 tree = parse_struct_string(state, NULL);
1447 whitespace(state);
1448 if (match(state, '}') != 0) {
1449 parse_error(state,
1450 "missing '}' in struct string");
1451 return NULL;
1452 }
1453 } else {
1454 parse_error(state,
1455 "missing '{' after 'T' in struct string");
1456 return NULL;
1457 }
1458 }
1459
1460 return tree;
1461 }
1462
1463 /* Parses a struct string and builds an intermediate representation of that
1464 * string in the form of a formattree.
1465 *
1466 * NOTE: This function is called recursively and *can* return NULL even
1467 * when no errors have occurred. This happens when an empty structure·
1468 * 'T{}' is encountered.·
1469 */
1470
1471 static formattree*
1472 parse_struct_string(parser_state *state, formattree *root)
1473 {
1474 formattree *tree = NULL;
1475
1476 if (root != NULL) {
1477 tree = root;
1478 }
1479
1480 whitespace(state);
1481 while (*state->fmt != '0円') {
1482 formattree *child = NULL;
1483 int n = 0;
1484
1485 if (is_byte_order_marker(*state->fmt)) {
1486 state->byte_fmt = whichtable((char**)&state->fmt);
1487 } else if (*state->fmt == '}') {
1488 /* Return back to invocation that started parsing the
1489 structure. */
1490 break;
1491 } else if (*state->fmt == 'T') {
1492 child = parse_struct(state);
1493 n = 1;
1494 } else if (is_primitive(*state->fmt)) {
1495 child = parse_primitive(state);
1496 if (child != NULL) {
1497 state->offset += child->s_size;
1498 n = child->s_len;
1499 }
1500 } else {
1501 parse_error(state,
1502 "unexpected character in struct string");
1503 return NULL;
1504 }
1505
1506 if (state->error_cnt > 0) {
1507 return NULL;
1508 }
1509
1510 /* If 'child' is NULL then, either an empty structure was encountered
1511 * or a byte order change was made.
1512 */
1513 if (child != NULL) {
1514 if (tree == NULL) {
1515 tree = formattree_new();
1516 if (tree == NULL) {
1517 return NULL;
1518 }
1519 }
1520 FormatTree_AppendChild(tree, child);
1521 tree->s_size += child->s_size;
1522 tree->s_len += n;
1523 }
1524
1525 whitespace(state);
1526 }
1527 return tree;
1528 }
1529
1530 static int
1531 prepare_s(PyStructObject *self)
1532 {
1533 parser_state state = {
1534 native_table,
1535 PyBytes_AS_STRING(self->s_format),
1536 0,
1537 0
1538 };
1539 (void) parse_struct_string(&state, self->s_tree);
1540 return (state.error_cnt == 0) ? 0 : -1;
1282 } 1541 }
1283 1542
1284 static PyObject * 1543 static PyObject *
1285 s_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 1544 s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1286 { 1545 {
1287 PyObject *self; 1546 PyObject *self;
1288 1547
1289 assert(type != NULL && type->tp_alloc != NULL); 1548 assert(type != NULL && type->tp_alloc != NULL);
1290 1549
1291 self = type->tp_alloc(type, 0); 1550 self = type->tp_alloc(type, 0);
1292 if (self != NULL) { 1551 if (self != NULL) {
1293 PyStructObject *s = (PyStructObject*)self; 1552 PyStructObject *s = (PyStructObject*)self;
1294 Py_INCREF(Py_None); 1553 Py_INCREF(Py_None);
1295 s->s_format = Py_None; 1554 s->s_format = Py_None;
1296 s->s_codes = NULL; 1555 s->s_tree = formattree_new();
1297 s->s_size = -1;
1298 s->s_len = -1;
1299 } 1556 }
1300 return self; 1557 return self;
1301 } 1558 }
1302 1559
1303 static int 1560 static int
1304 s_init(PyObject *self, PyObject *args, PyObject *kwds) 1561 s_init(PyObject *self, PyObject *args, PyObject *kwds)
1305 { 1562 {
1306 PyStructObject *soself = (PyStructObject *)self; 1563 PyStructObject *soself = (PyStructObject *)self;
1307 PyObject *o_format = NULL; 1564 PyObject *o_format = NULL;
1308 int ret = 0; 1565 int ret = 0;
(...skipping 28 matching lines...) Loading...
1337 1594
1338 ret = prepare_s(soself); 1595 ret = prepare_s(soself);
1339 return ret; 1596 return ret;
1340 } 1597 }
1341 1598
1342 static void 1599 static void
1343 s_dealloc(PyStructObject *s) 1600 s_dealloc(PyStructObject *s)
1344 { 1601 {
1345 if (s->weakreflist != NULL) 1602 if (s->weakreflist != NULL)
1346 PyObject_ClearWeakRefs((PyObject *)s); 1603 PyObject_ClearWeakRefs((PyObject *)s);
1347 if (s->s_codes != NULL) { 1604 if (s->s_tree != NULL) {
1348 PyMem_FREE(s->s_codes); 1605 formattree_free(s->s_tree);
1349 } 1606 }
1350 Py_XDECREF(s->s_format); 1607 Py_XDECREF(s->s_format);
1351 Py_TYPE(s)->tp_free((PyObject *)s); 1608 Py_TYPE(s)->tp_free((PyObject *)s);
1352 } 1609 }
1353 1610
1611 /* Unpacks the given buffer in accordance with the given formattree. The·
1612 * formattree should not have any children and should have a sequence of
1613 * format codes defined.
1614 *·
1615 * A Tuple object is returned upon success. NULL is returned upon failure.·
1616 */
1617
1354 static PyObject * 1618 static PyObject *
1355 s_unpack_internal(PyStructObject *soself, char *startfrom){ 1619 s_unpack_primitive(formattree *tree, const char *startfrom)
1620 {
1356 formatcode *code; 1621 formatcode *code;
1357 Py_ssize_t i = 0; 1622 Py_ssize_t i = 0;
1358 PyObject *result = PyTuple_New(soself->s_len); 1623 PyObject *result;
1624
1625 assert(tree->s_codes != NULL);
1626 assert(!FormatTree_HasChildren(tree));
1627
1628 result = PyTuple_New(tree->s_len);
1359 if (result == NULL) 1629 if (result == NULL)
1360 return NULL; 1630 return NULL;
1361 1631
1362 for (code = soself->s_codes; code->fmtdef != NULL; code++) { 1632 for (code = tree->s_codes; code->fmtdef != NULL; code++) {
1363 PyObject *v; 1633 PyObject *v;
1364 const formatdef *e = code->fmtdef; 1634 const formatdef *e = code->fmtdef;
1365 const char *res = startfrom + code->offset; 1635 const char *res = startfrom + code->offset;
1366 if (e->format == 's') { 1636 if (e->format == 's') {
1367 v = PyBytes_FromStringAndSize(res, code->size); 1637 v = PyBytes_FromStringAndSize(res, code->size);
1368 } else if (e->format == 'p') { 1638 } else if (e->format == 'p') {
1369 Py_ssize_t n = *(unsigned char*)res; 1639 Py_ssize_t n = *(unsigned char*)res;
1370 if (n >= code->size) 1640 if (n >= code->size)
1371 n = code->size - 1; 1641 n = code->size - 1;
1372 v = PyBytes_FromStringAndSize(res + 1, n); 1642 v = PyBytes_FromStringAndSize(res + 1, n);
1373 } else { 1643 } else {
1374 v = e->unpack(res, e); 1644 v = e->unpack(res, e);
1375 } 1645 }
1376 if (v == NULL) 1646 if (v == NULL)
1377 goto fail; 1647 goto fail;
1378 PyTuple_SET_ITEM(result, i++, v); 1648 PyTuple_SET_ITEM(result, i++, v);
1379 } 1649 }
1380 1650
1381 return result; 1651 return result;
1382 fail: 1652 fail:
1383 Py_DECREF(result); 1653 Py_DECREF(result);
1384 return NULL; 1654 return NULL;
1385 } 1655 }
1386 1656
1657 /* Unpacks the given buffer in accordance with the given formattree. The·
1658 * formattree specifies how the given character buffer should be unpacked.
1659 *·
1660 * A Tuple object is returned upon success. This Tuple object may have nested
1661 * Tuple objects, if the formattree dictacts as such. NULL is returned upon
1662 * failure.·
1663 */
1664
1665 static PyObject *
1666 s_unpack_struct(formattree *tree, const char *startfrom)
1667 {
1668 formattree *child = NULL;
1669 PyObject *tup = PyTuple_New(0);
1670 if (tup == NULL)
1671 return NULL;
1672
1673 for (child = tree->child; child != NULL; child = child->sibling) {
1674 PyObject *sub_tup = NULL;
1675 PyObject *new_tup = NULL;
1676
1677 if (FormatTree_HasChildren(child)) {
1678 PyObject *wrap_tup = NULL;
1679 sub_tup = s_unpack_struct(child, startfrom);
1680 if (sub_tup == NULL)
1681 goto fail;
1682 wrap_tup = PyTuple_New(1);
1683 if (wrap_tup == NULL) {
1684 Py_DECREF(sub_tup);
1685 goto fail;
1686 }
1687 PyTuple_SetItem(wrap_tup, 0, sub_tup);
1688 new_tup = PySequence_Concat(tup, wrap_tup);
1689 Py_DECREF(wrap_tup);
1690 } else {
1691 sub_tup = s_unpack_primitive(child, startfrom);
1692 if (sub_tup == NULL)
1693 goto fail;
1694 new_tup = PySequence_Concat(tup, sub_tup);
1695 Py_DECREF(sub_tup);
1696 }
1697 Py_DECREF(tup);
1698 if (new_tup == NULL)
1699 goto fail;
1700 tup = new_tup;
1701 }
1702
1703 return tup;
1704 fail:
1705 Py_DECREF(tup);
1706 return NULL;
1707 }
1708
1709 static PyObject *
1710 s_unpack_internal(PyStructObject *soself, char *startfrom)
1711 {
1712 return s_unpack_struct(soself->s_tree, startfrom);
1713 }
1387 1714
1388 PyDoc_STRVAR(s_unpack__doc__, 1715 PyDoc_STRVAR(s_unpack__doc__,
1389 "S.unpack(buffer) -> (v1, v2, ...)\n\ 1716 "S.unpack(buffer) -> (v1, v2, ...)\n\
1390 \n\ 1717 \n\
1391 Return tuple containing values unpacked according to this Struct's format.\n\ 1718 Return tuple containing values unpacked according to this Struct's format.\n\
1392 Requires len(buffer) == self.size. See struct.__doc__ for more on format\n\ 1719 Requires len(buffer) == self.size. See struct.__doc__ for more on format\n\
1393 strings."); 1720 strings.");
1394 1721
1395 static PyObject * 1722 static PyObject *
1396 s_unpack(PyObject *self, PyObject *input) 1723 s_unpack(PyObject *self, PyObject *input)
1397 { 1724 {
1398 Py_buffer vbuf; 1725 Py_buffer vbuf;
1399 PyObject *result; 1726 PyObject *result;
1400 PyStructObject *soself = (PyStructObject *)self; 1727 PyStructObject *soself = (PyStructObject *)self;
1401 1728
1402 assert(PyStruct_Check(self)); 1729 assert(PyStruct_Check(self));
1403 assert(soself->s_codes != NULL); 1730 assert(soself->s_codes != NULL);
minge 2010年05月20日 13:58:04 This should be 'assert(soself->s_tree->s_codes !=
This should be 'assert(soself->s_tree->s_codes != NULL)'.
1404 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0) 1731 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1405 return NULL; 1732 return NULL;
1406 if (vbuf.len != soself->s_size) { 1733 if (vbuf.len != soself->s_tree->s_size) {
1407 PyErr_Format(StructError, 1734 PyErr_Format(StructError,
1408 "unpack requires a bytes argument of length %zd", 1735 "unpack requires a bytes argument of length %zd",
1409 soself->s_size); 1736 soself->s_tree->s_size);
1410 PyBuffer_Release(&vbuf); 1737 PyBuffer_Release(&vbuf);
1411 return NULL; 1738 return NULL;
1412 } 1739 }
1413 result = s_unpack_internal(soself, vbuf.buf); 1740 result = s_unpack_internal(soself, vbuf.buf);
1414 PyBuffer_Release(&vbuf); 1741 PyBuffer_Release(&vbuf);
1415 return result; 1742 return result;
1416 } 1743 }
1417 1744
1418 PyDoc_STRVAR(s_unpack_from__doc__, 1745 PyDoc_STRVAR(s_unpack_from__doc__,
1419 "S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\ 1746 "S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
1420 \n\ 1747 \n\
1421 Return tuple containing values unpacked according to this Struct's format.\n\ 1748 Return tuple containing values unpacked according to this Struct's format.\n\
1422 Unlike unpack, unpack_from can unpack values from any object supporting\n\ 1749 Unlike unpack, unpack_from can unpack values from any object supporting\n\
1423 the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\ 1750 the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1424 See struct.__doc__ for more on format strings."); 1751 See struct.__doc__ for more on format strings.");
1425 1752
1426 static PyObject * 1753 static PyObject *
1427 s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds) 1754 s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1428 { 1755 {
1429 static char *kwlist[] = {"buffer", "offset", 0}; 1756 static char *kwlist[] = {"buffer", "offset", 0};
1430 1757
1431 PyObject *input; 1758 PyObject *input;
1432 Py_ssize_t offset = 0; 1759 Py_ssize_t offset = 0;
1433 Py_buffer vbuf; 1760 Py_buffer vbuf;
1434 PyObject *result; 1761 PyObject *result;
1435 PyStructObject *soself = (PyStructObject *)self; 1762 PyStructObject *soself = (PyStructObject *)self;
1436 1763
1437 assert(PyStruct_Check(self)); 1764 assert(PyStruct_Check(self));
1438 assert(soself->s_codes != NULL); 1765 assert(soself->s_codes != NULL);
minge 2010年05月20日 13:58:04 This should be 'assert(soself->s_tree->s_codes !=
This should be 'assert(soself->s_tree->s_codes != NULL)'.
1439 1766
1440 if (!PyArg_ParseTupleAndKeywords(args, kwds, 1767 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1441 "O|n:unpack_from", kwlist, 1768 "O|n:unpack_from", kwlist,
1442 &input, &offset)) 1769 &input, &offset))
1443 return NULL; 1770 return NULL;
1444 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0) 1771 if (PyObject_GetBuffer(input, &vbuf, PyBUF_SIMPLE) < 0)
1445 return NULL; 1772 return NULL;
1446 if (offset < 0) 1773 if (offset < 0)
1447 offset += vbuf.len; 1774 offset += vbuf.len;
1448 if (offset < 0 || vbuf.len - offset < soself->s_size) { 1775 if (offset < 0 || vbuf.len - offset < soself->s_tree->s_size) {
1449 PyErr_Format(StructError, 1776 PyErr_Format(StructError,
1450 "unpack_from requires a buffer of at least %zd bytes", 1777 "unpack_from requires a buffer of at least %zd bytes",
1451 soself->s_size); 1778 soself->s_tree->s_size);
1452 PyBuffer_Release(&vbuf); 1779 PyBuffer_Release(&vbuf);
1453 return NULL; 1780 return NULL;
1454 } 1781 }
1455 result = s_unpack_internal(soself, (char*)vbuf.buf + offset); 1782 result = s_unpack_internal(soself, (char*)vbuf.buf + offset);
1456 PyBuffer_Release(&vbuf); 1783 PyBuffer_Release(&vbuf);
1457 return result; 1784 return result;
1458 } 1785 }
1459 1786
1787 /* Packs a non-nested tuples of arguments to the given buffer.
1788 *
1789 * Takes a struct object, a tuple of arguments, and a character buffer for
1790 * writing the packed string. 0 is returned on success, -1 is returned if
1791 * there is an error.·
1792 */
1460 1793
1461 /*
1462 * Guts of the pack function.
1463 *
1464 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1465 * argument for where to start processing the arguments for packing, and a
1466 * character buffer for writing the packed string. The caller must insure
1467 * that the buffer may contain the required length for packing the arguments.
1468 * 0 is returned on success, 1 is returned if there is an error.
1469 *
1470 */
1471 static int 1794 static int
1472 s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf) 1795 s_pack_primitive(formattree *tree, PyObject *args, char *buf)
1473 { 1796 {
1474 formatcode *code; 1797 formatcode *code;
1475 /* XXX(nnorwitz): why does i need to be a local? can we use 1798 Py_ssize_t i = 0;
1476 the offset parameter or do we need the wider width? */ 1799 assert(tree->s_codes != NULL);
1477 Py_ssize_t i;
1478 1800
1479 memset(buf, '0円', soself->s_size); 1801 if (PyTuple_GET_SIZE(args) != tree->s_len)
1480 i = offset; 1802 {
1481 for (code = soself->s_codes; code->fmtdef != NULL; code++) { 1803 PyErr_Format(StructError,
1804 "pack requires exactly %zd arguments", tree->s_len);
1805 return -1;
1806 }
1807
1808 for (code = tree->s_codes; code->fmtdef != NULL; code++) {
1482 Py_ssize_t n; 1809 Py_ssize_t n;
1483 PyObject *v = PyTuple_GET_ITEM(args, i++); 1810 PyObject *v = PyTuple_GET_ITEM(args, i++);
1484 const formatdef *e = code->fmtdef; 1811 const formatdef *e = code->fmtdef;
1485 char *res = buf + code->offset; 1812 char *res = buf + code->offset;
1486 if (e->format == 's') { 1813 if (e->format == 's') {
1487 int isstring; 1814 int isstring;
1488 void *p; 1815 void *p;
1489 if (PyUnicode_Check(v)) { 1816 if (PyUnicode_Check(v)) {
1490 v = _PyUnicode_AsDefaultEncodedString(v, NULL); 1817 v = _PyUnicode_AsDefaultEncodedString(v, NULL);
1491 if (v == NULL) 1818 if (v == NULL)
(...skipping 54 matching lines...) | | Loading...
1546 return -1; 1873 return -1;
1547 } 1874 }
1548 } 1875 }
1549 } 1876 }
1550 1877
1551 /* Success */ 1878 /* Success */
1552 return 0; 1879 return 0;
1553 } 1880 }
1554 1881
1555 1882
1883 /* Packs a potentially nested tuple of arguments to the given buffer.
1884 *
1885 * Takes a struct object, a tuple of arguments, and a character buffer for
1886 * writing the packed string. If there are nested structures within the given
1887 * structure, then the nested structures are recursively packed. 0 is returned
1888 * on success, -1 is returned if there is an error.·
1889 */
1890
1891 static int
1892 s_pack_struct(formattree *tree, PyObject *args, char *buf)
1893 {
1894 Py_ssize_t arg_i = 0;
1895 formattree *child = NULL;
1896
1897 if (PyTuple_GET_SIZE(args) != tree->s_len)
1898 {
1899 PyErr_Format(StructError,
1900 "pack requires exactly %zd arguments", tree->s_len);
1901 return -1;
1902 }
1903
1904 for (child = tree->child; child != NULL; child = child->sibling) {
1905 int ret = 0;
1906 if (FormatTree_HasChildren(child)) {
1907 /* args[arg_i] */
1908 PyObject *arg = PyTuple_GET_ITEM(args, arg_i);
1909 if (!PyTuple_Check(arg)) {
1910 PyErr_SetString(StructError,
1911 "argument for 'T{...}'"
1912 " must be a tuple");
1913 return -1;
1914 }
1915 ret = s_pack_struct(child, arg, buf);
1916 arg_i += 1;
1917 } else {
1918 /* args[arg_i : arg_i + n] */
1919 PyObject *slice =
1920 PyTuple_GetSlice(args, arg_i,
1921 arg_i + child->s_len);
1922 ret = s_pack_primitive(child, slice, buf);
1923 arg_i += child->s_len;
1924 Py_DECREF(slice);
1925 }
1926
1927 if (ret < 0) {
1928 return -1;
1929 }
1930 }
1931
1932 return 0;
1933 }
1934
1935 static int
1936 s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char *buf)
1937 {
1938 memset(buf, '0円', soself->s_tree->s_size);
1939 PyObject *new_args = PyTuple_GetSlice(args, offset, PyTuple_Size(args));
1940 int ret = s_pack_struct(soself->s_tree, new_args, buf);
1941 Py_DECREF(new_args);
1942 return ret;
1943 }
1944
1556 PyDoc_STRVAR(s_pack__doc__, 1945 PyDoc_STRVAR(s_pack__doc__,
1557 "S.pack(v1, v2, ...) -> bytes\n\ 1946 "S.pack(v1, v2, ...) -> bytes\n\
1558 \n\ 1947 \n\
1559 Return a bytes containing values v1, v2, ... packed according to this\n\ 1948 Return a bytes containing values v1, v2, ... packed according to this\n\
1560 Struct's format. See struct.__doc__ for more on format strings."); 1949 Struct's format. See struct.__doc__ for more on format strings.");
1561 1950
1562 static PyObject * 1951 static PyObject *
1563 s_pack(PyObject *self, PyObject *args) 1952 s_pack(PyObject *self, PyObject *args)
1564 { 1953 {
1565 PyStructObject *soself; 1954 PyStructObject *soself;
1566 PyObject *result; 1955 PyObject *result;
1567 1956
1568 /* Validate arguments. */ 1957 /* Validate arguments. */
1569 soself = (PyStructObject *)self; 1958 soself = (PyStructObject *)self;
1570 assert(PyStruct_Check(self)); 1959 assert(PyStruct_Check(self));
1571 assert(soself->s_codes != NULL);
1572 if (PyTuple_GET_SIZE(args) != soself->s_len)
1573 {
1574 PyErr_Format(StructError,
1575 "pack requires exactly %zd arguments", soself->s_len);
1576 return NULL;
1577 }
1578 1960
1579 /* Allocate a new string */ 1961 /* Allocate a new string */
1580 result = PyBytes_FromStringAndSize((char *)NULL, soself->s_size); 1962 result = PyBytes_FromStringAndSize((char *)NULL, soself->s_tree->s_size);
1581 if (result == NULL) 1963 if (result == NULL)
1582 return NULL; 1964 return NULL;
1583 1965
1584 /* Call the guts */ 1966 /* Call the guts */
1585 if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) { 1967 if ( s_pack_internal(soself, args, 0, PyBytes_AS_STRING(result)) != 0 ) {
1586 Py_DECREF(result); 1968 Py_DECREF(result);
1587 return NULL; 1969 return NULL;
1588 } 1970 }
1589 1971
1590 return result; 1972 return result;
(...skipping 10 matching lines...) Loading...
1601 static PyObject * 1983 static PyObject *
1602 s_pack_into(PyObject *self, PyObject *args) 1984 s_pack_into(PyObject *self, PyObject *args)
1603 { 1985 {
1604 PyStructObject *soself; 1986 PyStructObject *soself;
1605 char *buffer; 1987 char *buffer;
1606 Py_ssize_t buffer_len, offset; 1988 Py_ssize_t buffer_len, offset;
1607 1989
1608 /* Validate arguments. +1 is for the first arg as buffer. */ 1990 /* Validate arguments. +1 is for the first arg as buffer. */
1609 soself = (PyStructObject *)self; 1991 soself = (PyStructObject *)self;
1610 assert(PyStruct_Check(self)); 1992 assert(PyStruct_Check(self));
1611 assert(soself->s_codes != NULL);
1612 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
1613 {
1614 PyErr_Format(StructError,
1615 "pack_into requires exactly %zd arguments",
1616 (soself->s_len + 2));
1617 return NULL;
1618 }
1619 1993
1620 /* Extract a writable memory buffer from the first argument */ 1994 /* Extract a writable memory buffer from the first argument */
1621 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0), 1995 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1622 (void**)&buffer, &bu ffer_len) == -1 ) { 1996 (void**)&buffer, &bu ffer_len) == -1 ) {
1623 return NULL; 1997 return NULL;
1624 } 1998 }
1625 assert( buffer_len >= 0 ); 1999 assert( buffer_len >= 0 );
1626 2000
1627 /* Extract the offset from the first argument */ 2001 /* Extract the offset from the first argument */
1628 offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError); 2002 offset = PyNumber_AsSsize_t(PyTuple_GET_ITEM(args, 1), PyExc_IndexError);
1629 if (offset == -1 && PyErr_Occurred()) 2003 if (offset == -1 && PyErr_Occurred())
1630 return NULL; 2004 return NULL;
1631 2005
1632 /* Support negative offsets. */ 2006 /* Support negative offsets. */
1633 if (offset < 0) 2007 if (offset < 0)
1634 offset += buffer_len; 2008 offset += buffer_len;
1635 2009
1636 /* Check boundaries */ 2010 /* Check boundaries */
1637 if (offset < 0 || (buffer_len - offset) < soself->s_size) { 2011 if (offset < 0 || (buffer_len - offset) < soself->s_tree->s_size) {
1638 PyErr_Format(StructError, 2012 PyErr_Format(StructError,
1639 "pack_into requires a buffer of at least %zd bytes", 2013 "pack_into requires a buffer of at least %zd bytes",
1640 soself->s_size); 2014 soself->s_tree->s_size);
1641 return NULL; 2015 return NULL;
1642 } 2016 }
1643 2017
1644 /* Call the guts */ 2018 /* Call the guts */
1645 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) { 2019 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1646 return NULL; 2020 return NULL;
1647 } 2021 }
1648 2022
1649 Py_RETURN_NONE; 2023 Py_RETURN_NONE;
1650 } 2024 }
1651 2025
1652 static PyObject * 2026 static PyObject *
1653 s_get_format(PyStructObject *self, void *unused) 2027 s_get_format(PyStructObject *self, void *unused)
1654 { 2028 {
1655 Py_INCREF(self->s_format); 2029 Py_INCREF(self->s_format);
1656 return self->s_format; 2030 return self->s_format;
1657 } 2031 }
1658 2032
1659 static PyObject * 2033 static PyObject *
1660 s_get_size(PyStructObject *self, void *unused) 2034 s_get_size(PyStructObject *self, void *unused)
1661 { 2035 {
1662 return PyLong_FromSsize_t(self->s_size); 2036 return PyLong_FromSsize_t(self->s_tree->s_size);
1663 } 2037 }
1664 2038
1665 /* List of functions */ 2039 /* List of functions */
1666 2040
1667 static struct PyMethodDef s_methods[] = { 2041 static struct PyMethodDef s_methods[] = {
1668 {"pack", s_pack, METH_VARARGS, s_pack__doc__}, 2042 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1669 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__}, 2043 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1670 {"unpack", s_unpack, METH_O, s_unpack__doc__}, 2044 {"unpack", s_unpack, METH_O, s_unpack__doc__},
1671 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS, 2045 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
1672 s_unpack_from__doc__}, 2046 s_unpack_from__doc__},
(...skipping 100 matching lines...) | | Loading...
1773 PyDoc_STRVAR(calcsize_doc, 2147 PyDoc_STRVAR(calcsize_doc,
1774 "Return size of C struct described by format string fmt."); 2148 "Return size of C struct described by format string fmt.");
1775 2149
1776 static PyObject * 2150 static PyObject *
1777 calcsize(PyObject *self, PyObject *fmt) 2151 calcsize(PyObject *self, PyObject *fmt)
1778 { 2152 {
1779 Py_ssize_t n; 2153 Py_ssize_t n;
1780 PyObject *s_object = cache_struct(fmt); 2154 PyObject *s_object = cache_struct(fmt);
1781 if (s_object == NULL) 2155 if (s_object == NULL)
1782 return NULL; 2156 return NULL;
1783 n = ((PyStructObject *)s_object)->s_size; 2157 n = ((PyStructObject *)s_object)->s_tree->s_size;
1784 Py_DECREF(s_object); 2158 Py_DECREF(s_object);
1785 return PyLong_FromSsize_t(n); 2159 return PyLong_FromSsize_t(n);
1786 } 2160 }
1787 2161
1788 PyDoc_STRVAR(pack_doc, 2162 PyDoc_STRVAR(pack_doc,
1789 "Return bytes containing values v1, v2, ... packed according to fmt."); 2163 "Return bytes containing values v1, v2, ... packed according to fmt.");
1790 2164
1791 static PyObject * 2165 static PyObject *
1792 pack(PyObject *self, PyObject *args) 2166 pack(PyObject *self, PyObject *args)
1793 { 2167 {
(...skipping 113 matching lines...) | | Loading...
1907 2281
1908 2282
1909 /* Module initialization */ 2283 /* Module initialization */
1910 2284
1911 PyDoc_STRVAR(module_doc, 2285 PyDoc_STRVAR(module_doc,
1912 "Functions to convert between Python values and C structs.\n\ 2286 "Functions to convert between Python values and C structs.\n\
1913 Python bytes objects are used to hold the data representing the C struct\n\ 2287 Python bytes objects are used to hold the data representing the C struct\n\
1914 and also as format strings (explained below) to describe the layout of data\n\ 2288 and also as format strings (explained below) to describe the layout of data\n\
1915 in the C struct.\n\ 2289 in the C struct.\n\
1916 \n\ 2290 \n\
1917 The optional first format char indicates byte order, size and alignment:\n\ 2291 The following format chars indicates byte order, size and alignment:\n\
1918 @: native order, size & alignment (default)\n\ 2292 @: native order, size & alignment (default)\n\
1919 =: native order, std. size & alignment\n\ 2293 =: native order, std. size & alignment\n\
1920 <: little-endian, std. size & alignment\n\ 2294 <: little-endian, std. size & alignment\n\
1921 >: big-endian, std. size & alignment\n\ 2295 >: big-endian, std. size & alignment\n\
1922 !: same as >\n\ 2296 !: same as >\n\
2297 They may be used anywhere in the format string. The byte order, size and\n\
2298 alignment dictated by one of these chars is in effect until the next\n\
2299 such char is encountered.\n\
1923 \n\ 2300 \n\
1924 The remaining chars indicate types of args and must match exactly;\n\ 2301 The primitive format chars are used to indicate types of primitive arg\n\
1925 these can be preceded by a decimal repeatcount:\n\ 2302 values and must match exactly;these can be preceded by a decimal repeat\n\
2303 count:\n\
1926 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\ 2304 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
1927 ?: _Bool (requires C99; if not available, char is used instead)\n\ 2305 ?: _Bool (requires C99; if not available, char is used instead)\n\
1928 h:short; H:unsigned short; i:int; I:unsigned int;\n\ 2306 h:short; H:unsigned short; i:int; I:unsigned int;\n\
1929 l:long; L:unsigned long; f:float; d:double.\n\ 2307 l:long; L:unsigned long; f:float; d:double.\n\
1930 Special cases (preceding decimal count indicates length):\n\ 2308 Special cases (preceding decimal count indicates length):\n\
1931 s:string (array of char); p: pascal string (with count byte).\n\ 2309 s:string (array of char); p: pascal string (with count byte).\n\
1932 Special case (only available in native format):\n\ 2310 Special case (only available in native format):\n\
1933 P:an integer type that is wide enough to hold a pointer.\n\ 2311 P:an integer type that is wide enough to hold a pointer.\n\
1934 Special case (not in native mode unless 'long long' in platform C):\n\ 2312 Special case (not in native mode unless 'long long' in platform C):\n\
1935 q:long long; Q:unsigned long long\n\ 2313 q:long long; Q:unsigned long long\n\
1936 Whitespace between formats is ignored.\n\ 2314 Whitespace between formats is ignored.\n\
1937 \n\ 2315 \n\
2316 Additionally, there may be nested structures. These are specified by\n\
2317 surrounding a format string with 'T{' ... '}'. They maybe arbitrarily\n\
2318 nested.\n\
2319 \n\
1938 The variable struct.error is an exception raised on errors.\n"); 2320 The variable struct.error is an exception raised on errors.\n");
1939 2321
1940 2322
1941 static struct PyModuleDef _structmodule = { 2323 static struct PyModuleDef _structmodule = {
1942 PyModuleDef_HEAD_INIT, 2324 PyModuleDef_HEAD_INIT,
1943 "_struct", 2325 "_struct",
1944 module_doc, 2326 module_doc,
1945 -1, 2327 -1,
1946 module_functions, 2328 module_functions,
1947 NULL, 2329 NULL,
(...skipping 68 matching lines...) | | Loading...
2016 Py_INCREF(StructError); 2398 Py_INCREF(StructError);
2017 PyModule_AddObject(m, "error", StructError); 2399 PyModule_AddObject(m, "error", StructError);
2018 2400
2019 Py_INCREF((PyObject*)&PyStructType); 2401 Py_INCREF((PyObject*)&PyStructType);
2020 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType); 2402 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
2021 2403
2022 PyModule_AddObject(m, "__version__", ver); 2404 PyModule_AddObject(m, "__version__", ver);
2023 2405
2024 return m; 2406 return m;
2025 } 2407 }
OLDNEW
« no previous file with comments | « Lib/test/test_struct.py ('k') | no next file » | no next file with comments »
Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b

AltStyle によって変換されたページ (->オリジナル) /