annotate Python/marshal.c @ 87488:124e51c19e4f

Issue #19734: Also run pip version check in isolated mode
author Nick Coghlan <ncoghlan@gmail.com>
date Sun, 24 Nov 2013 16:49:20 +1000
parents e086cb1c6e5a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 /* Write Python objects to files and read them back.
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
3 This is primarily intended for writing and reading compiled Python code,
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
4 even though dicts, lists, sets and frozensets, not commonly seen in
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
5 code objects, are supported.
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
6 Version 3 of this protocol properly supports circular links
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
7 and sharing. */
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
36792
688795873ff1 Make Py_ssize_t clean.
Thomas Wouters <thomas@python.org>
parents: 36784
diff changeset
9 #define PY_SSIZE_T_CLEAN
688795873ff1 Make Py_ssize_t clean.
Thomas Wouters <thomas@python.org>
parents: 36784
diff changeset
10
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
11 #include "Python.h"
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 #include "longintrepr.h"
35742
1c4d192f1568 Merge ast-branch to head
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 35346
diff changeset
13 #include "code.h"
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 #include "marshal.h"
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
16 #define ABS(x) ((x) < 0 ? -(x) : (x))
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
17
13070
7392e71f6cce Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
18 /* High water mark to determine when the marshalled object is dangerously deep
7392e71f6cce Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
19 * and risks coring the interpreter. When the object stack gets this deep,
7392e71f6cce Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
20 * raise an exception instead of continuing.
43007
5fde5d6964d7 [Oops, I forgot half of the patch.]
Guido van Rossum <guido@python.org>
parents: 42665
diff changeset
21 * On Windows debug builds, reduce this value.
13070
7392e71f6cce Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
22 */
43007
5fde5d6964d7 [Oops, I forgot half of the patch.]
Guido van Rossum <guido@python.org>
parents: 42665
diff changeset
23 #if defined(MS_WINDOWS) && defined(_DEBUG)
5fde5d6964d7 [Oops, I forgot half of the patch.]
Guido van Rossum <guido@python.org>
parents: 42665
diff changeset
24 #define MAX_MARSHAL_STACK_DEPTH 1500
5fde5d6964d7 [Oops, I forgot half of the patch.]
Guido van Rossum <guido@python.org>
parents: 42665
diff changeset
25 #else
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41715
diff changeset
26 #define MAX_MARSHAL_STACK_DEPTH 2000
43007
5fde5d6964d7 [Oops, I forgot half of the patch.]
Guido van Rossum <guido@python.org>
parents: 42665
diff changeset
27 #endif
13070
7392e71f6cce Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
28
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
29 #define TYPE_NULL '0'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
30 #define TYPE_NONE 'N'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
31 #define TYPE_FALSE 'F'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
32 #define TYPE_TRUE 'T'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
33 #define TYPE_STOPITER 'S'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
34 #define TYPE_ELLIPSIS '.'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
35 #define TYPE_INT 'i'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
36 #define TYPE_FLOAT 'f'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
37 #define TYPE_BINARY_FLOAT 'g'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
38 #define TYPE_COMPLEX 'x'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
39 #define TYPE_BINARY_COMPLEX 'y'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
40 #define TYPE_LONG 'l'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
41 #define TYPE_STRING 's'
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
42 #define TYPE_INTERNED 't'
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
43 #define TYPE_REF 'r'
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
44 #define TYPE_TUPLE '('
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
45 #define TYPE_LIST '['
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
46 #define TYPE_DICT '{'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
47 #define TYPE_CODE 'c'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
48 #define TYPE_UNICODE 'u'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
49 #define TYPE_UNKNOWN '?'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
50 #define TYPE_SET '<'
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
51 #define TYPE_FROZENSET '>'
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
52 #define FLAG_REF '\x80' /* with a type, add obj to index */
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
54 #define TYPE_ASCII 'a'
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
55 #define TYPE_ASCII_INTERNED 'A'
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
56 #define TYPE_SMALL_TUPLE ')'
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
57 #define TYPE_SHORT_ASCII 'z'
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
58 #define TYPE_SHORT_ASCII_INTERNED 'Z'
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
59
53300
ed3f51f2abfb Added handling of PyOS_double_to_string out-of-memory errors. Closes issue 5775.
Eric Smith <eric@trueblade.com>
parents: 53192
diff changeset
60 #define WFERR_OK 0
ed3f51f2abfb Added handling of PyOS_double_to_string out-of-memory errors. Closes issue 5775.
Eric Smith <eric@trueblade.com>
parents: 53192
diff changeset
61 #define WFERR_UNMARSHALLABLE 1
ed3f51f2abfb Added handling of PyOS_double_to_string out-of-memory errors. Closes issue 5775.
Eric Smith <eric@trueblade.com>
parents: 53192
diff changeset
62 #define WFERR_NESTEDTOODEEP 2
ed3f51f2abfb Added handling of PyOS_double_to_string out-of-memory errors. Closes issue 5775.
Eric Smith <eric@trueblade.com>
parents: 53192
diff changeset
63 #define WFERR_NOMEMORY 3
ed3f51f2abfb Added handling of PyOS_double_to_string out-of-memory errors. Closes issue 5775.
Eric Smith <eric@trueblade.com>
parents: 53192
diff changeset
64
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
65 typedef struct {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
66 FILE *fp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
67 int error; /* see WFERR_* values */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
68 int depth;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
69 /* If fp == NULL, the following are valid: */
71554
9e293f617544 Fix style in code added by edba722f3b02
Éric Araujo <merwok@netwok.org>
parents: 71134
diff changeset
70 PyObject *readable; /* Stream-like object being read from */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
71 PyObject *str;
70435
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
72 PyObject *current_filename;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
73 char *ptr;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
74 char *end;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
75 char *buf;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
76 Py_ssize_t buf_size;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
77 PyObject *refs; /* dict on marshal, list on unmarshal */
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
78 int version;
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
79 } WFILE;
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
80
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
81 #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
82 else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
87131
e086cb1c6e5a Fix compiler warning (on Windows 64-bit): explicit cast Py_ssize_t to unsigned
Victor Stinner <victor.stinner@gmail.com>
parents: 86810
diff changeset
83 else w_more((c), p)
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
85 static void
86262
0383f08bff4c Try to fix weird assertion error on the Fedora buildbot.
Antoine Pitrou <solipsis@pitrou.net>
parents: 86255
diff changeset
86 w_more(char c, WFILE *p)
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
87 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
88 Py_ssize_t size, newsize;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
89 if (p->str == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
90 return; /* An error already occurred */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
91 size = PyBytes_Size(p->str);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
92 newsize = size + size + 1024;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
93 if (newsize > 32*1024*1024) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
94 newsize = size + (size >> 3); /* 12.5% overallocation */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
95 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
96 if (_PyBytes_Resize(&p->str, newsize) != 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
97 p->ptr = p->end = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
98 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
99 else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
100 p->ptr = PyBytes_AS_STRING((PyBytesObject *)p->str) + size;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
101 p->end =
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
102 PyBytes_AS_STRING((PyBytesObject *)p->str) + newsize;
86262
0383f08bff4c Try to fix weird assertion error on the Fedora buildbot.
Antoine Pitrou <solipsis@pitrou.net>
parents: 86255
diff changeset
103 *p->ptr++ = c;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
104 }
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
105 }
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
106
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
107 static void
84546
8b99f2224c3a Fix reference leaks introduced by the patch for issue #5308.
Serhiy Storchaka <storchaka@gmail.com>
parents: 82191
diff changeset
108 w_string(const char *s, Py_ssize_t n, WFILE *p)
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
109 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
110 if (p->fp != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
111 fwrite(s, 1, n, p->fp);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
112 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
113 else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
114 while (--n >= 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
115 w_byte(*s, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
116 s++;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
117 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
118 }
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
119 }
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
120
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
121 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
122 w_short(int x, WFILE *p)
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
123 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
124 w_byte((char)( x & 0xff), p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
125 w_byte((char)((x>> 8) & 0xff), p);
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
126 }
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
127
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
128 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
129 w_long(long x, WFILE *p)
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
130 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
131 w_byte((char)( x & 0xff), p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
132 w_byte((char)((x>> 8) & 0xff), p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
133 w_byte((char)((x>>16) & 0xff), p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
134 w_byte((char)((x>>24) & 0xff), p);
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
135 }
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
136
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
137 #define SIZE32_MAX 0x7FFFFFFF
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
138
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
139 #if SIZEOF_SIZE_T > 4
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
140 # define W_SIZE(n, p) do { \
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
141 if ((n) > SIZE32_MAX) { \
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
142 (p)->depth--; \
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
143 (p)->error = WFERR_UNMARSHALLABLE; \
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
144 return; \
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
145 } \
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
146 w_long((long)(n), p); \
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
147 } while(0)
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
148 #else
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
149 # define W_SIZE w_long
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
150 #endif
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
151
84546
8b99f2224c3a Fix reference leaks introduced by the patch for issue #5308.
Serhiy Storchaka <storchaka@gmail.com>
parents: 82191
diff changeset
152 static void
8b99f2224c3a Fix reference leaks introduced by the patch for issue #5308.
Serhiy Storchaka <storchaka@gmail.com>
parents: 82191
diff changeset
153 w_pstring(const char *s, Py_ssize_t n, WFILE *p)
8b99f2224c3a Fix reference leaks introduced by the patch for issue #5308.
Serhiy Storchaka <storchaka@gmail.com>
parents: 82191
diff changeset
154 {
8b99f2224c3a Fix reference leaks introduced by the patch for issue #5308.
Serhiy Storchaka <storchaka@gmail.com>
parents: 82191
diff changeset
155 W_SIZE(n, p);
8b99f2224c3a Fix reference leaks introduced by the patch for issue #5308.
Serhiy Storchaka <storchaka@gmail.com>
parents: 82191
diff changeset
156 w_string(s, n, p);
8b99f2224c3a Fix reference leaks introduced by the patch for issue #5308.
Serhiy Storchaka <storchaka@gmail.com>
parents: 82191
diff changeset
157 }
8b99f2224c3a Fix reference leaks introduced by the patch for issue #5308.
Serhiy Storchaka <storchaka@gmail.com>
parents: 82191
diff changeset
158
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
159 static void
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
160 w_short_pstring(const char *s, Py_ssize_t n, WFILE *p)
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
161 {
86262
0383f08bff4c Try to fix weird assertion error on the Fedora buildbot.
Antoine Pitrou <solipsis@pitrou.net>
parents: 86255
diff changeset
162 w_byte(Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char), p);
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
163 w_string(s, n, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
164 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
165
85418
4d62a62ba44d Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents: 84556
diff changeset
166 /* We assume that Python ints are stored internally in base some power of
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
167 2**15; for the sake of portability we'll always read and write them in base
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
168 exactly 2**15. */
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
169
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
170 #define PyLong_MARSHAL_SHIFT 15
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
171 #define PyLong_MARSHAL_BASE ((short)1 << PyLong_MARSHAL_SHIFT)
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
172 #define PyLong_MARSHAL_MASK (PyLong_MARSHAL_BASE - 1)
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
173 #if PyLong_SHIFT % PyLong_MARSHAL_SHIFT != 0
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
174 #error "PyLong_SHIFT must be a multiple of PyLong_MARSHAL_SHIFT"
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
175 #endif
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
176 #define PyLong_MARSHAL_RATIO (PyLong_SHIFT / PyLong_MARSHAL_SHIFT)
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
177
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
178 #define W_TYPE(t, p) do { \
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
179 w_byte((t) | flag, (p)); \
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
180 } while(0)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
181
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
182 static void
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
183 w_PyLong(const PyLongObject *ob, char flag, WFILE *p)
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
184 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
185 Py_ssize_t i, j, n, l;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
186 digit d;
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
187
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
188 W_TYPE(TYPE_LONG, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
189 if (Py_SIZE(ob) == 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
190 w_long((long)0, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
191 return;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
192 }
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
193
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
194 /* set l to number of base PyLong_MARSHAL_BASE digits */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
195 n = ABS(Py_SIZE(ob));
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
196 l = (n-1) * PyLong_MARSHAL_RATIO;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
197 d = ob->ob_digit[n-1];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
198 assert(d != 0); /* a PyLong is always normalized */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
199 do {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
200 d >>= PyLong_MARSHAL_SHIFT;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
201 l++;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
202 } while (d != 0);
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
203 if (l > SIZE32_MAX) {
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
204 p->depth--;
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
205 p->error = WFERR_UNMARSHALLABLE;
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
206 return;
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
207 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
208 w_long((long)(Py_SIZE(ob) > 0 ? l : -l), p);
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
209
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
210 for (i=0; i < n-1; i++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
211 d = ob->ob_digit[i];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
212 for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
213 w_short(d & PyLong_MARSHAL_MASK, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
214 d >>= PyLong_MARSHAL_SHIFT;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
215 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
216 assert (d == 0);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
217 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
218 d = ob->ob_digit[n-1];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
219 do {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
220 w_short(d & PyLong_MARSHAL_MASK, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
221 d >>= PyLong_MARSHAL_SHIFT;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
222 } while (d != 0);
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
223 }
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
224
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
225 static int
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
226 w_ref(PyObject *v, char *flag, WFILE *p)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
227 {
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
228 PyObject *id;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
229 PyObject *idx;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
230
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
231 if (p->version < 3 || p->refs == NULL)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
232 return 0; /* not writing object references */
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
233
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
234 /* if it has only one reference, it definitely isn't shared */
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
235 if (Py_REFCNT(v) == 1)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
236 return 0;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
237
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
238 id = PyLong_FromVoidPtr((void*)v);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
239 if (id == NULL)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
240 goto err;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
241 idx = PyDict_GetItem(p->refs, id);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
242 if (idx != NULL) {
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
243 /* write the reference index to the stream */
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
244 long w = PyLong_AsLong(idx);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
245 Py_DECREF(id);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
246 if (w == -1 && PyErr_Occurred()) {
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
247 goto err;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
248 }
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
249 /* we don't store "long" indices in the dict */
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
250 assert(0 <= w && w <= 0x7fffffff);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
251 w_byte(TYPE_REF, p);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
252 w_long(w, p);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
253 return 1;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
254 } else {
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
255 int ok;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
256 Py_ssize_t s = PyDict_Size(p->refs);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
257 /* we don't support long indices */
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
258 if (s >= 0x7fffffff) {
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
259 PyErr_SetString(PyExc_ValueError, "too many objects");
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
260 goto err;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
261 }
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
262 idx = PyLong_FromSsize_t(s);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
263 ok = idx && PyDict_SetItem(p->refs, id, idx) == 0;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
264 Py_DECREF(id);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
265 Py_XDECREF(idx);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
266 if (!ok)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
267 goto err;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
268 *flag |= FLAG_REF;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
269 return 0;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
270 }
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
271 err:
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
272 p->error = WFERR_UNMARSHALLABLE;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
273 return 1;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
274 }
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
275
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
276 static void
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
277 w_complex_object(PyObject *v, char flag, WFILE *p);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
278
4592
3bf9e2beaf8c Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
279 static void
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
280 w_object(PyObject *v, WFILE *p)
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 {
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
282 char flag = '\0';
13070
7392e71f6cce Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 12576
diff changeset
283
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
284 p->depth++;
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
285
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
286 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
287 p->error = WFERR_NESTEDTOODEEP;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
288 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
289 else if (v == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
290 w_byte(TYPE_NULL, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
291 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
292 else if (v == Py_None) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
293 w_byte(TYPE_NONE, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
294 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
295 else if (v == PyExc_StopIteration) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
296 w_byte(TYPE_STOPITER, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
297 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
298 else if (v == Py_Ellipsis) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
299 w_byte(TYPE_ELLIPSIS, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
300 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
301 else if (v == Py_False) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
302 w_byte(TYPE_FALSE, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
303 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
304 else if (v == Py_True) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
305 w_byte(TYPE_TRUE, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
306 }
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
307 else if (!w_ref(v, &flag, p))
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
308 w_complex_object(v, flag, p);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
309
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
310 p->depth--;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
311 }
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
312
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
313 static void
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
314 w_complex_object(PyObject *v, char flag, WFILE *p)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
315 {
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
316 Py_ssize_t i, n;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
317
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
318 if (PyLong_CheckExact(v)) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
319 long x = PyLong_AsLong(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
320 if ((x == -1) && PyErr_Occurred()) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
321 PyLongObject *ob = (PyLongObject *)v;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
322 PyErr_Clear();
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
323 w_PyLong(ob, flag, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
324 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
325 else {
4656
186c2ad4abf3 Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4592
diff changeset
326 #if SIZEOF_LONG > 4
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
327 long y = Py_ARITHMETIC_RIGHT_SHIFT(long, x, 31);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
328 if (y && y != -1) {
78305
461e84fc8c60 Issue #15466: Stop using TYPE_INT64 in marshal,
Martin v. Löwis <martin@v.loewis.de>
parents: 75375
diff changeset
329 /* Too large for TYPE_INT */
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
330 w_PyLong((PyLongObject*)v, flag, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
331 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
332 else
4656
186c2ad4abf3 Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4592
diff changeset
333 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
334 {
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
335 W_TYPE(TYPE_INT, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
336 w_long(x, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
337 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
338 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
339 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
340 else if (PyFloat_CheckExact(v)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
341 if (p->version > 1) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
342 unsigned char buf[8];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
343 if (_PyFloat_Pack8(PyFloat_AsDouble(v),
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
344 buf, 1) < 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
345 p->error = WFERR_UNMARSHALLABLE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
346 return;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
347 }
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
348 W_TYPE(TYPE_BINARY_FLOAT, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
349 w_string((char*)buf, 8, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
350 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
351 else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
352 char *buf = PyOS_double_to_string(PyFloat_AS_DOUBLE(v),
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
353 'g', 17, 0, NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
354 if (!buf) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
355 p->error = WFERR_NOMEMORY;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
356 return;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
357 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
358 n = strlen(buf);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
359 W_TYPE(TYPE_FLOAT, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
360 w_byte((int)n, p);
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
361 w_string(buf, n, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
362 PyMem_Free(buf);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
363 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
364 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
365 else if (PyComplex_CheckExact(v)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
366 if (p->version > 1) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
367 unsigned char buf[8];
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
368 if (_PyFloat_Pack8(PyComplex_RealAsDouble(v),
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
369 buf, 1) < 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
370 p->error = WFERR_UNMARSHALLABLE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
371 return;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
372 }
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
373 W_TYPE(TYPE_BINARY_COMPLEX, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
374 w_string((char*)buf, 8, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
375 if (_PyFloat_Pack8(PyComplex_ImagAsDouble(v),
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
376 buf, 1) < 0) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
377 p->error = WFERR_UNMARSHALLABLE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
378 return;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
379 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
380 w_string((char*)buf, 8, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
381 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
382 else {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
383 char *buf;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
384 W_TYPE(TYPE_COMPLEX, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
385 buf = PyOS_double_to_string(PyComplex_RealAsDouble(v),
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
386 'g', 17, 0, NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
387 if (!buf) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
388 p->error = WFERR_NOMEMORY;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
389 return;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
390 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
391 n = strlen(buf);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
392 w_byte((int)n, p);
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
393 w_string(buf, n, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
394 PyMem_Free(buf);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
395 buf = PyOS_double_to_string(PyComplex_ImagAsDouble(v),
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
396 'g', 17, 0, NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
397 if (!buf) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
398 p->error = WFERR_NOMEMORY;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
399 return;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
400 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
401 n = strlen(buf);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
402 w_byte((int)n, p);
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
403 w_string(buf, n, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
404 PyMem_Free(buf);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
405 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
406 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
407 else if (PyBytes_CheckExact(v)) {
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
408 W_TYPE(TYPE_STRING, p);
84546
8b99f2224c3a Fix reference leaks introduced by the patch for issue #5308.
Serhiy Storchaka <storchaka@gmail.com>
parents: 82191
diff changeset
409 w_pstring(PyBytes_AS_STRING(v), PyBytes_GET_SIZE(v), p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
410 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
411 else if (PyUnicode_CheckExact(v)) {
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
412 if (p->version >= 4 && PyUnicode_IS_ASCII(v)) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
413 int is_short = PyUnicode_GET_LENGTH(v) < 256;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
414 if (is_short) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
415 if (PyUnicode_CHECK_INTERNED(v))
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
416 W_TYPE(TYPE_SHORT_ASCII_INTERNED, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
417 else
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
418 W_TYPE(TYPE_SHORT_ASCII, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
419 w_short_pstring((char *) PyUnicode_1BYTE_DATA(v),
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
420 PyUnicode_GET_LENGTH(v), p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
421 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
422 else {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
423 if (PyUnicode_CHECK_INTERNED(v))
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
424 W_TYPE(TYPE_ASCII_INTERNED, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
425 else
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
426 W_TYPE(TYPE_ASCII, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
427 w_pstring((char *) PyUnicode_1BYTE_DATA(v),
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
428 PyUnicode_GET_LENGTH(v), p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
429 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
430 }
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
431 else {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
432 PyObject *utf8;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
433 utf8 = PyUnicode_AsEncodedString(v, "utf8", "surrogatepass");
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
434 if (utf8 == NULL) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
435 p->depth--;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
436 p->error = WFERR_UNMARSHALLABLE;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
437 return;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
438 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
439 if (p->version >= 3 && PyUnicode_CHECK_INTERNED(v))
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
440 W_TYPE(TYPE_INTERNED, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
441 else
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
442 W_TYPE(TYPE_UNICODE, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
443 w_pstring(PyBytes_AS_STRING(utf8), PyBytes_GET_SIZE(utf8), p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
444 Py_DECREF(utf8);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
445 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
446 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
447 else if (PyTuple_CheckExact(v)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
448 n = PyTuple_Size(v);
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
449 if (p->version >= 4 && n < 256) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
450 W_TYPE(TYPE_SMALL_TUPLE, p);
87131
e086cb1c6e5a Fix compiler warning (on Windows 64-bit): explicit cast Py_ssize_t to unsigned
Victor Stinner <victor.stinner@gmail.com>
parents: 86810
diff changeset
451 w_byte((unsigned char)n, p);
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
452 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
453 else {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
454 W_TYPE(TYPE_TUPLE, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
455 W_SIZE(n, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
456 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
457 for (i = 0; i < n; i++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
458 w_object(PyTuple_GET_ITEM(v, i), p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
459 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
460 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
461 else if (PyList_CheckExact(v)) {
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
462 W_TYPE(TYPE_LIST, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
463 n = PyList_GET_SIZE(v);
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
464 W_SIZE(n, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
465 for (i = 0; i < n; i++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
466 w_object(PyList_GET_ITEM(v, i), p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
467 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
468 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
469 else if (PyDict_CheckExact(v)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
470 Py_ssize_t pos;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
471 PyObject *key, *value;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
472 W_TYPE(TYPE_DICT, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
473 /* This one is NULL object terminated! */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
474 pos = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
475 while (PyDict_Next(v, &pos, &key, &value)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
476 w_object(key, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
477 w_object(value, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
478 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
479 w_object((PyObject *)NULL, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
480 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
481 else if (PyAnySet_CheckExact(v)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
482 PyObject *value, *it;
34394
b480abaf1f6f SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents: 34205
diff changeset
483
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
484 if (PyObject_TypeCheck(v, &PySet_Type))
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
485 W_TYPE(TYPE_SET, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
486 else
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
487 W_TYPE(TYPE_FROZENSET, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
488 n = PyObject_Size(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
489 if (n == -1) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
490 p->depth--;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
491 p->error = WFERR_UNMARSHALLABLE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
492 return;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
493 }
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
494 W_SIZE(n, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
495 it = PyObject_GetIter(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
496 if (it == NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
497 p->depth--;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
498 p->error = WFERR_UNMARSHALLABLE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
499 return;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
500 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
501 while ((value = PyIter_Next(it)) != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
502 w_object(value, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
503 Py_DECREF(value);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
504 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
505 Py_DECREF(it);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
506 if (PyErr_Occurred()) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
507 p->depth--;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
508 p->error = WFERR_UNMARSHALLABLE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
509 return;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
510 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
511 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
512 else if (PyCode_Check(v)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
513 PyCodeObject *co = (PyCodeObject *)v;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
514 W_TYPE(TYPE_CODE, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
515 w_long(co->co_argcount, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
516 w_long(co->co_kwonlyargcount, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
517 w_long(co->co_nlocals, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
518 w_long(co->co_stacksize, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
519 w_long(co->co_flags, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
520 w_object(co->co_code, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
521 w_object(co->co_consts, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
522 w_object(co->co_names, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
523 w_object(co->co_varnames, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
524 w_object(co->co_freevars, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
525 w_object(co->co_cellvars, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
526 w_object(co->co_filename, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
527 w_object(co->co_name, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
528 w_long(co->co_firstlineno, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
529 w_object(co->co_lnotab, p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
530 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
531 else if (PyObject_CheckBuffer(v)) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
532 /* Write unknown buffer-style objects as a string */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
533 Py_buffer view;
75369
185a6ae76479 Issue #14172: Fix reference leak when marshalling a buffer-like object (other than a bytes object).
Antoine Pitrou <solipsis@pitrou.net>
parents: 71554
diff changeset
534 if (PyObject_GetBuffer(v, &view, PyBUF_SIMPLE) != 0) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
535 w_byte(TYPE_UNKNOWN, p);
75369
185a6ae76479 Issue #14172: Fix reference leak when marshalling a buffer-like object (other than a bytes object).
Antoine Pitrou <solipsis@pitrou.net>
parents: 71554
diff changeset
536 p->depth--;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
537 p->error = WFERR_UNMARSHALLABLE;
75369
185a6ae76479 Issue #14172: Fix reference leak when marshalling a buffer-like object (other than a bytes object).
Antoine Pitrou <solipsis@pitrou.net>
parents: 71554
diff changeset
538 return;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
539 }
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
540 W_TYPE(TYPE_STRING, p);
84546
8b99f2224c3a Fix reference leaks introduced by the patch for issue #5308.
Serhiy Storchaka <storchaka@gmail.com>
parents: 82191
diff changeset
541 w_pstring(view.buf, view.len, p);
75369
185a6ae76479 Issue #14172: Fix reference leak when marshalling a buffer-like object (other than a bytes object).
Antoine Pitrou <solipsis@pitrou.net>
parents: 71554
diff changeset
542 PyBuffer_Release(&view);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
543 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
544 else {
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
545 W_TYPE(TYPE_UNKNOWN, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
546 p->error = WFERR_UNMARSHALLABLE;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
547 }
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
548 }
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
549
85418
4d62a62ba44d Issue #18783: Removed existing mentions of Python long type in docstrings,
Serhiy Storchaka <storchaka@gmail.com>
parents: 84556
diff changeset
550 /* version currently has no effect for writing ints. */
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
551 void
32422
3df36e2e5ddc Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents: 32343
diff changeset
552 PyMarshal_WriteLongToFile(long x, FILE *fp, int version)
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
553 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
554 WFILE wf;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
555 wf.fp = fp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
556 wf.error = WFERR_OK;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
557 wf.depth = 0;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
558 wf.refs = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
559 wf.version = version;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
560 w_long(x, &wf);
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
561 }
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
562
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
563 void
32422
3df36e2e5ddc Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents: 32343
diff changeset
564 PyMarshal_WriteObjectToFile(PyObject *x, FILE *fp, int version)
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
565 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
566 WFILE wf;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
567 wf.fp = fp;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
568 wf.error = WFERR_OK;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
569 wf.depth = 0;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
570 if (version >= 3) {
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
571 if ((wf.refs = PyDict_New()) == NULL)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
572 return; /* caller mush check PyErr_Occurred() */
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
573 } else
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
574 wf.refs = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
575 wf.version = version;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
576 w_object(x, &wf);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
577 Py_XDECREF(wf.refs);
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
578 }
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
579
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
580 typedef WFILE RFILE; /* Same struct with different invariants */
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
581
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
582 static char *
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
583 r_string(Py_ssize_t n, RFILE *p)
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
584 {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
585 Py_ssize_t read = -1;
2503
4dd1af0ddef0 various tuple related optimizations; remove unused b/w compat code from ceval.c
Guido van Rossum <guido@python.org>
parents: 2368
diff changeset
586
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
587 if (p->ptr != NULL) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
588 /* Fast path for loads() */
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
589 char *res = p->ptr;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
590 Py_ssize_t left = p->end - p->ptr;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
591 if (left < n) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
592 PyErr_SetString(PyExc_EOFError,
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
593 "marshal data too short");
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
594 return NULL;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
595 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
596 p->ptr += n;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
597 return res;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
598 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
599 if (p->buf == NULL) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
600 p->buf = PyMem_MALLOC(n);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
601 if (p->buf == NULL) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
602 PyErr_NoMemory();
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
603 return NULL;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
604 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
605 p->buf_size = n;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
606 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
607 else if (p->buf_size < n) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
608 p->buf = PyMem_REALLOC(p->buf, n);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
609 if (p->buf == NULL) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
610 PyErr_NoMemory();
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
611 return NULL;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
612 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
613 p->buf_size = n;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
614 }
86807
985f8762ee3e Issue #19437: Fix r_PyLong() of marshal module, stop immediatly at first
Victor Stinner <victor.stinner@gmail.com>
parents: 86484
diff changeset
615
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
616 if (!p->readable) {
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
617 assert(p->fp != NULL);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
618 read = fread(p->buf, 1, n, p->fp);
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
619 }
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
620 else {
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
621 _Py_IDENTIFIER(readinto);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
622 PyObject *res, *mview;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
623 Py_buffer buf;
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
624
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
625 if (PyBuffer_FillInfo(&buf, NULL, p->buf, n, 0, PyBUF_CONTIG) == -1)
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
626 return NULL;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
627 mview = PyMemoryView_FromBuffer(&buf);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
628 if (mview == NULL)
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
629 return NULL;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
630
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
631 res = _PyObject_CallMethodId(p->readable, &PyId_readinto, "N", mview);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
632 if (res != NULL) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
633 read = PyNumber_AsSsize_t(res, PyExc_ValueError);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
634 Py_DECREF(res);
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
635 }
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
636 }
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
637 if (read != n) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
638 if (!PyErr_Occurred()) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
639 if (read > n)
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
640 PyErr_Format(PyExc_ValueError,
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
641 "read() returned too much data: "
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
642 "%zd bytes requested, %zd returned",
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
643 n, read);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
644 else
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
645 PyErr_SetString(PyExc_EOFError,
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
646 "EOF read where not expected");
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
647 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
648 return NULL;
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
649 }
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
650 return p->buf;
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
651 }
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
652
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
653 static int
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
654 r_byte(RFILE *p)
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
655 {
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
656 int c = EOF;
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
657
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
658 if (p->ptr != NULL) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
659 if (p->ptr < p->end)
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
660 c = (unsigned char) *p->ptr++;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
661 return c;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
662 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
663 if (!p->readable) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
664 assert(p->fp);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
665 c = getc(p->fp);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
666 }
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
667 else {
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
668 char *ptr = r_string(1, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
669 if (ptr != NULL)
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
670 c = *(unsigned char *) ptr;
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
671 }
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
672 return c;
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
673 }
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
674
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
675 static int
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
676 r_short(RFILE *p)
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
677 {
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
678 short x = -1;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
679 unsigned char *buffer;
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
680
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
681 buffer = (unsigned char *) r_string(2, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
682 if (buffer != NULL) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
683 x = buffer[0];
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
684 x |= buffer[1] << 8;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
685 /* Sign-extension, in case short greater than 16 bits */
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
686 x |= -(x & 0x8000);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
687 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
688 return x;
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
689 }
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
690
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
691 static long
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
692 r_long(RFILE *p)
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
693 {
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
694 long x = -1;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
695 unsigned char *buffer;
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
696
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
697 buffer = (unsigned char *) r_string(4, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
698 if (buffer != NULL) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
699 x = buffer[0];
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
700 x |= (long)buffer[1] << 8;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
701 x |= (long)buffer[2] << 16;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
702 x |= (long)buffer[3] << 24;
4656
186c2ad4abf3 Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4592
diff changeset
703 #if SIZEOF_LONG > 4
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
704 /* Sign extension for 64-bit machines */
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
705 x |= -(x & 0x80000000L);
4656
186c2ad4abf3 Better way to handle 64-bit ints, keeping gcc -Wall happy.
Guido van Rossum <guido@python.org>
parents: 4592
diff changeset
706 #endif
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
707 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
708 return x;
4592
3bf9e2beaf8c Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
709 }
3bf9e2beaf8c Tentative changes to make this work better on 64-bit machines.
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
710
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
711 static PyObject *
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
712 r_PyLong(RFILE *p)
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
713 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
714 PyLongObject *ob;
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
715 long n, size, i;
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
716 int j, md, shorts_in_top_digit;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
717 digit d;
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
718
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
719 n = r_long(p);
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
720 if (PyErr_Occurred())
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
721 return NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
722 if (n == 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
723 return (PyObject *)_PyLong_New(0);
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
724 if (n < -SIZE32_MAX || n > SIZE32_MAX) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
725 PyErr_SetString(PyExc_ValueError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
726 "bad marshal data (long size out of range)");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
727 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
728 }
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
729
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
730 size = 1 + (ABS(n) - 1) / PyLong_MARSHAL_RATIO;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
731 shorts_in_top_digit = 1 + (ABS(n) - 1) % PyLong_MARSHAL_RATIO;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
732 ob = _PyLong_New(size);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
733 if (ob == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
734 return NULL;
86807
985f8762ee3e Issue #19437: Fix r_PyLong() of marshal module, stop immediatly at first
Victor Stinner <victor.stinner@gmail.com>
parents: 86484
diff changeset
735
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
736 Py_SIZE(ob) = n > 0 ? size : -size;
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
737
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
738 for (i = 0; i < size-1; i++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
739 d = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
740 for (j=0; j < PyLong_MARSHAL_RATIO; j++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
741 md = r_short(p);
86807
985f8762ee3e Issue #19437: Fix r_PyLong() of marshal module, stop immediatly at first
Victor Stinner <victor.stinner@gmail.com>
parents: 86484
diff changeset
742 if (PyErr_Occurred()) {
985f8762ee3e Issue #19437: Fix r_PyLong() of marshal module, stop immediatly at first
Victor Stinner <victor.stinner@gmail.com>
parents: 86484
diff changeset
743 Py_DECREF(ob);
985f8762ee3e Issue #19437: Fix r_PyLong() of marshal module, stop immediatly at first
Victor Stinner <victor.stinner@gmail.com>
parents: 86484
diff changeset
744 return NULL;
985f8762ee3e Issue #19437: Fix r_PyLong() of marshal module, stop immediatly at first
Victor Stinner <victor.stinner@gmail.com>
parents: 86484
diff changeset
745 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
746 if (md < 0 || md > PyLong_MARSHAL_BASE)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
747 goto bad_digit;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
748 d += (digit)md << j*PyLong_MARSHAL_SHIFT;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
749 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
750 ob->ob_digit[i] = d;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
751 }
86807
985f8762ee3e Issue #19437: Fix r_PyLong() of marshal module, stop immediatly at first
Victor Stinner <victor.stinner@gmail.com>
parents: 86484
diff changeset
752
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
753 d = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
754 for (j=0; j < shorts_in_top_digit; j++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
755 md = r_short(p);
86807
985f8762ee3e Issue #19437: Fix r_PyLong() of marshal module, stop immediatly at first
Victor Stinner <victor.stinner@gmail.com>
parents: 86484
diff changeset
756 if (PyErr_Occurred()) {
985f8762ee3e Issue #19437: Fix r_PyLong() of marshal module, stop immediatly at first
Victor Stinner <victor.stinner@gmail.com>
parents: 86484
diff changeset
757 Py_DECREF(ob);
985f8762ee3e Issue #19437: Fix r_PyLong() of marshal module, stop immediatly at first
Victor Stinner <victor.stinner@gmail.com>
parents: 86484
diff changeset
758 return NULL;
985f8762ee3e Issue #19437: Fix r_PyLong() of marshal module, stop immediatly at first
Victor Stinner <victor.stinner@gmail.com>
parents: 86484
diff changeset
759 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
760 if (md < 0 || md > PyLong_MARSHAL_BASE)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
761 goto bad_digit;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
762 /* topmost marshal digit should be nonzero */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
763 if (md == 0 && j == shorts_in_top_digit - 1) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
764 Py_DECREF(ob);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
765 PyErr_SetString(PyExc_ValueError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
766 "bad marshal data (unnormalized long data)");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
767 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
768 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
769 d += (digit)md << j*PyLong_MARSHAL_SHIFT;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
770 }
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
771 if (PyErr_Occurred()) {
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
772 Py_DECREF(ob);
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
773 return NULL;
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
774 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
775 /* top digit should be nonzero, else the resulting PyLong won't be
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
776 normalized */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
777 ob->ob_digit[size-1] = d;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
778 return (PyObject *)ob;
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
779 bad_digit:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
780 Py_DECREF(ob);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
781 PyErr_SetString(PyExc_ValueError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
782 "bad marshal data (digit out of range in long)");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
783 return NULL;
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
784 }
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
785
82847
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
786 /* allocate the reflist index for a new object. Return -1 on failure */
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
787 static Py_ssize_t
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
788 r_ref_reserve(int flag, RFILE *p)
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
789 {
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
790 if (flag) { /* currently only FLAG_REF is defined */
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
791 Py_ssize_t idx = PyList_GET_SIZE(p->refs);
82847
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
792 if (idx >= 0x7ffffffe) {
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
793 PyErr_SetString(PyExc_ValueError, "bad marshal data (index list too large)");
82847
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
794 return -1;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
795 }
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
796 if (PyList_Append(p->refs, Py_None) < 0)
82847
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
797 return -1;
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
798 return idx;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
799 } else
82847
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
800 return 0;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
801 }
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
802
82847
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
803 /* insert the new object 'o' to the reflist at previously
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
804 * allocated index 'idx'.
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
805 * 'o' can be NULL, in which case nothing is done.
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
806 * if 'o' was non-NULL, and the function succeeds, 'o' is returned.
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
807 * if 'o' was non-NULL, and the function fails, 'o' is released and
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
808 * NULL returned. This simplifies error checking at the call site since
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
809 * a single test for NULL for the function result is enough.
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
810 */
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
811 static PyObject *
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
812 r_ref_insert(PyObject *o, Py_ssize_t idx, int flag, RFILE *p)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
813 {
82847
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
814 if (o != NULL && flag) { /* currently only FLAG_REF is defined */
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
815 PyObject *tmp = PyList_GET_ITEM(p->refs, idx);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
816 Py_INCREF(o);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
817 PyList_SET_ITEM(p->refs, idx, o);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
818 Py_DECREF(tmp);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
819 }
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
820 return o;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
821 }
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
822
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
823 /* combination of both above, used when an object can be
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
824 * created whenever it is seen in the file, as opposed to
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
825 * after having loaded its sub-objects.
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
826 */
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
827 static PyObject *
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
828 r_ref(PyObject *o, int flag, RFILE *p)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
829 {
86810
995173ed248a Issue #19437: Cleanup r_ref() of the marshal module
Victor Stinner <victor.stinner@gmail.com>
parents: 86809
diff changeset
830 assert(flag & FLAG_REF);
995173ed248a Issue #19437: Cleanup r_ref() of the marshal module
Victor Stinner <victor.stinner@gmail.com>
parents: 86809
diff changeset
831 if (o == NULL)
995173ed248a Issue #19437: Cleanup r_ref() of the marshal module
Victor Stinner <victor.stinner@gmail.com>
parents: 86809
diff changeset
832 return NULL;
995173ed248a Issue #19437: Cleanup r_ref() of the marshal module
Victor Stinner <victor.stinner@gmail.com>
parents: 86809
diff changeset
833 if (PyList_Append(p->refs, o) < 0) {
995173ed248a Issue #19437: Cleanup r_ref() of the marshal module
Victor Stinner <victor.stinner@gmail.com>
parents: 86809
diff changeset
834 Py_DECREF(o); /* release the new object */
995173ed248a Issue #19437: Cleanup r_ref() of the marshal module
Victor Stinner <victor.stinner@gmail.com>
parents: 86809
diff changeset
835 return NULL;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
836 }
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
837 return o;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
838 }
52215
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
839
bb5de24a343f Issue #4258: Make it possible to use 30-bit digits for PyLongs:
Mark Dickinson <dickinsm@gmail.com>
parents: 52171
diff changeset
840 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
841 r_object(RFILE *p)
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
842 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
843 /* NULL is a valid return value, it does not necessarily means that
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
844 an exception is set. */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
845 PyObject *v, *v2;
82829
511d17372402 fix compiler warning
Benjamin Peterson <benjamin@python.org>
parents: 82828
diff changeset
846 Py_ssize_t idx = 0;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
847 long i, n;
82848
42bf74b90626 Issue #16475 : Correctly handle the EOF when reading marshal streams.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82847
diff changeset
848 int type, code = r_byte(p);
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
849 int flag, is_interned = 0;
86278
2a2b339b6b59 Issue #19219: retval may be used uninitialized value
Christian Heimes <christian@cheimes.de>
parents: 86262
diff changeset
850 PyObject *retval = NULL;
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41715
diff changeset
851
82848
42bf74b90626 Issue #16475 : Correctly handle the EOF when reading marshal streams.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82847
diff changeset
852 if (code == EOF) {
42bf74b90626 Issue #16475 : Correctly handle the EOF when reading marshal streams.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82847
diff changeset
853 PyErr_SetString(PyExc_EOFError,
42bf74b90626 Issue #16475 : Correctly handle the EOF when reading marshal streams.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82847
diff changeset
854 "EOF read where object expected");
42bf74b90626 Issue #16475 : Correctly handle the EOF when reading marshal streams.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82847
diff changeset
855 return NULL;
42bf74b90626 Issue #16475 : Correctly handle the EOF when reading marshal streams.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82847
diff changeset
856 }
42bf74b90626 Issue #16475 : Correctly handle the EOF when reading marshal streams.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82847
diff changeset
857
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
858 p->depth++;
41824
24b60622b944 Merged revisions 55407-55513 via svnmerge from
Guido van Rossum <guido@python.org>
parents: 41715
diff changeset
859
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
860 if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
861 p->depth--;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
862 PyErr_SetString(PyExc_ValueError, "recursion limit exceeded");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
863 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
864 }
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
865
82848
42bf74b90626 Issue #16475 : Correctly handle the EOF when reading marshal streams.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82847
diff changeset
866 flag = code & FLAG_REF;
42bf74b90626 Issue #16475 : Correctly handle the EOF when reading marshal streams.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82847
diff changeset
867 type = code & ~FLAG_REF;
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
868
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
869 #define R_REF(O) do{\
82847
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
870 if (flag) \
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
871 O = r_ref(O, flag, p);\
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
872 } while (0)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
873
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
874 switch (type) {
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
875
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
876 case TYPE_NULL:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
877 break;
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
878
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
879 case TYPE_NONE:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
880 Py_INCREF(Py_None);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
881 retval = Py_None;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
882 break;
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
883
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
884 case TYPE_STOPITER:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
885 Py_INCREF(PyExc_StopIteration);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
886 retval = PyExc_StopIteration;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
887 break;
18148
7bc473b2c654 Merging the gen-branch into the main line, at Guido's direction. Yay!
Tim Peters <tim.peters@gmail.com>
parents: 17707
diff changeset
888
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
889 case TYPE_ELLIPSIS:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
890 Py_INCREF(Py_Ellipsis);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
891 retval = Py_Ellipsis;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
892 break;
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
893
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
894 case TYPE_FALSE:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
895 Py_INCREF(Py_False);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
896 retval = Py_False;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
897 break;
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22705
diff changeset
898
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
899 case TYPE_TRUE:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
900 Py_INCREF(Py_True);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
901 retval = Py_True;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
902 break;
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 22705
diff changeset
903
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
904 case TYPE_INT:
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
905 n = r_long(p);
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
906 retval = PyErr_Occurred() ? NULL : PyLong_FromLong(n);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
907 R_REF(retval);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
908 break;
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
909
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
910 case TYPE_LONG:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
911 retval = r_PyLong(p);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
912 R_REF(retval);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
913 break;
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
914
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
915 case TYPE_FLOAT:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
916 {
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
917 char buf[256], *ptr;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
918 double dx;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
919 n = r_byte(p);
84556
fc7bab8a8618 Issue #17872: Fix a segfault in marshal.load() when input stream returns
Serhiy Storchaka <storchaka@gmail.com>
parents: 84546
diff changeset
920 if (n == EOF) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
921 PyErr_SetString(PyExc_EOFError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
922 "EOF read where object expected");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
923 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
924 }
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
925 ptr = r_string(n, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
926 if (ptr == NULL)
84556
fc7bab8a8618 Issue #17872: Fix a segfault in marshal.load() when input stream returns
Serhiy Storchaka <storchaka@gmail.com>
parents: 84546
diff changeset
927 break;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
928 memcpy(buf, ptr, n);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
929 buf[n] = '\0';
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
930 dx = PyOS_string_to_double(buf, NULL, NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
931 if (dx == -1.0 && PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
932 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
933 retval = PyFloat_FromDouble(dx);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
934 R_REF(retval);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
935 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
936 }
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
937
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
938 case TYPE_BINARY_FLOAT:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
939 {
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
940 unsigned char *buf;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
941 double x;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
942 buf = (unsigned char *) r_string(8, p);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
943 if (buf == NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
944 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
945 x = _PyFloat_Unpack8(buf, 1);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
946 if (x == -1.0 && PyErr_Occurred())
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
947 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
948 retval = PyFloat_FromDouble(x);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
949 R_REF(retval);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
950 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
951 }
35008
44e3f21f0525 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34413
diff changeset
952
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
953 case TYPE_COMPLEX:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
954 {
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
955 char buf[256], *ptr;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
956 Py_complex c;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
957 n = r_byte(p);
84556
fc7bab8a8618 Issue #17872: Fix a segfault in marshal.load() when input stream returns
Serhiy Storchaka <storchaka@gmail.com>
parents: 84546
diff changeset
958 if (n == EOF) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
959 PyErr_SetString(PyExc_EOFError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
960 "EOF read where object expected");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
961 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
962 }
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
963 ptr = r_string(n, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
964 if (ptr == NULL)
84556
fc7bab8a8618 Issue #17872: Fix a segfault in marshal.load() when input stream returns
Serhiy Storchaka <storchaka@gmail.com>
parents: 84546
diff changeset
965 break;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
966 memcpy(buf, ptr, n);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
967 buf[n] = '\0';
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
968 c.real = PyOS_string_to_double(buf, NULL, NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
969 if (c.real == -1.0 && PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
970 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
971 n = r_byte(p);
84556
fc7bab8a8618 Issue #17872: Fix a segfault in marshal.load() when input stream returns
Serhiy Storchaka <storchaka@gmail.com>
parents: 84546
diff changeset
972 if (n == EOF) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
973 PyErr_SetString(PyExc_EOFError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
974 "EOF read where object expected");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
975 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
976 }
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
977 ptr = r_string(n, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
978 if (ptr == NULL)
84556
fc7bab8a8618 Issue #17872: Fix a segfault in marshal.load() when input stream returns
Serhiy Storchaka <storchaka@gmail.com>
parents: 84546
diff changeset
979 break;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
980 memcpy(buf, ptr, n);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
981 buf[n] = '\0';
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
982 c.imag = PyOS_string_to_double(buf, NULL, NULL);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
983 if (c.imag == -1.0 && PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
984 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
985 retval = PyComplex_FromCComplex(c);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
986 R_REF(retval);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
987 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
988 }
35008
44e3f21f0525 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34413
diff changeset
989
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
990 case TYPE_BINARY_COMPLEX:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
991 {
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
992 unsigned char *buf;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
993 Py_complex c;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
994 buf = (unsigned char *) r_string(8, p);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
995 if (buf == NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
996 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
997 c.real = _PyFloat_Unpack8(buf, 1);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
998 if (c.real == -1.0 && PyErr_Occurred())
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
999 break;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1000 buf = (unsigned char *) r_string(8, p);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1001 if (buf == NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1002 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1003 c.imag = _PyFloat_Unpack8(buf, 1);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1004 if (c.imag == -1.0 && PyErr_Occurred())
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1005 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1006 retval = PyComplex_FromCComplex(c);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1007 R_REF(retval);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1008 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1009 }
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1010
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1011 case TYPE_STRING:
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1012 {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1013 char *ptr;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1014 n = r_long(p);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1015 if (PyErr_Occurred())
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1016 break;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1017 if (n < 0 || n > SIZE32_MAX) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1018 PyErr_SetString(PyExc_ValueError, "bad marshal data (string size out of range)");
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1019 break;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1020 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1021 v = PyBytes_FromStringAndSize((char *)NULL, n);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1022 if (v == NULL)
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1023 break;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1024 ptr = r_string(n, p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1025 if (ptr == NULL) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1026 Py_DECREF(v);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1027 break;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1028 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1029 memcpy(PyBytes_AS_STRING(v), ptr, n);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1030 retval = v;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1031 R_REF(retval);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1032 break;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1033 }
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1034
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1035 case TYPE_ASCII_INTERNED:
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1036 is_interned = 1;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1037 case TYPE_ASCII:
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1038 n = r_long(p);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1039 if (PyErr_Occurred())
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1040 break;
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
1041 if (n < 0 || n > SIZE32_MAX) {
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1042 PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1043 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1044 }
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1045 goto _read_ascii;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1046
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1047 case TYPE_SHORT_ASCII_INTERNED:
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1048 is_interned = 1;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1049 case TYPE_SHORT_ASCII:
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1050 n = r_byte(p);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1051 if (n == EOF) {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1052 PyErr_SetString(PyExc_EOFError,
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1053 "EOF read where object expected");
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1054 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1055 }
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1056 _read_ascii:
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1057 {
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1058 char *ptr;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1059 ptr = r_string(n, p);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1060 if (ptr == NULL)
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1061 break;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1062 v = PyUnicode_FromKindAndData(PyUnicode_1BYTE_KIND, ptr, n);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1063 if (v == NULL)
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1064 break;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1065 if (is_interned)
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1066 PyUnicode_InternInPlace(&v);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1067 retval = v;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1068 R_REF(retval);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1069 break;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1070 }
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1071
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1072 case TYPE_INTERNED:
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1073 is_interned = 1;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1074 case TYPE_UNICODE:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1075 {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1076 char *buffer;
11976
513354d87a85 Marc-Andre Lemburg: support marshalling Unicode objects (code 'u').
Guido van Rossum <guido@python.org>
parents: 9300
diff changeset
1077
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1078 n = r_long(p);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1079 if (PyErr_Occurred())
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1080 break;
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
1081 if (n < 0 || n > SIZE32_MAX) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1082 PyErr_SetString(PyExc_ValueError, "bad marshal data (unicode size out of range)");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1083 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1084 }
84245
942a7061e8ad marshal: optimize parsing of empty Unicode strings
Victor Stinner <victor.stinner@gmail.com>
parents: 83313
diff changeset
1085 if (n != 0) {
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1086 buffer = r_string(n, p);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1087 if (buffer == NULL)
84245
942a7061e8ad marshal: optimize parsing of empty Unicode strings
Victor Stinner <victor.stinner@gmail.com>
parents: 83313
diff changeset
1088 break;
942a7061e8ad marshal: optimize parsing of empty Unicode strings
Victor Stinner <victor.stinner@gmail.com>
parents: 83313
diff changeset
1089 v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1090 }
84245
942a7061e8ad marshal: optimize parsing of empty Unicode strings
Victor Stinner <victor.stinner@gmail.com>
parents: 83313
diff changeset
1091 else {
942a7061e8ad marshal: optimize parsing of empty Unicode strings
Victor Stinner <victor.stinner@gmail.com>
parents: 83313
diff changeset
1092 v = PyUnicode_New(0, 0);
942a7061e8ad marshal: optimize parsing of empty Unicode strings
Victor Stinner <victor.stinner@gmail.com>
parents: 83313
diff changeset
1093 }
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1094 if (v == NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1095 break;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1096 if (is_interned)
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1097 PyUnicode_InternInPlace(&v);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1098 retval = v;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1099 R_REF(retval);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1100 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1101 }
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1102
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1103 case TYPE_SMALL_TUPLE:
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1104 n = (unsigned char) r_byte(p);
86808
9e836a945ea9 Issue #19437: Fix r_object() of marshal module, handle r_byte() failure for
Victor Stinner <victor.stinner@gmail.com>
parents: 86807
diff changeset
1105 if (PyErr_Occurred())
9e836a945ea9 Issue #19437: Fix r_object() of marshal module, handle r_byte() failure for
Victor Stinner <victor.stinner@gmail.com>
parents: 86807
diff changeset
1106 break;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1107 goto _read_tuple;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1108 case TYPE_TUPLE:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1109 n = r_long(p);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1110 if (PyErr_Occurred())
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1111 break;
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
1112 if (n < 0 || n > SIZE32_MAX) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1113 PyErr_SetString(PyExc_ValueError, "bad marshal data (tuple size out of range)");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1114 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1115 }
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1116 _read_tuple:
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
1117 v = PyTuple_New(n);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1118 R_REF(v);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1119 if (v == NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1120 break;
86809
fc54e2848c98 Issue #19437: Fix r_object() of marshal module, handle PyDict_SetItem() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 86808
diff changeset
1121
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1122 for (i = 0; i < n; i++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1123 v2 = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1124 if ( v2 == NULL ) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1125 if (!PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1126 PyErr_SetString(PyExc_TypeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1127 "NULL object in marshal data for tuple");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1128 Py_DECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1129 v = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1130 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1131 }
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
1132 PyTuple_SET_ITEM(v, i, v2);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1133 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1134 retval = v;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1135 break;
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1136
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1137 case TYPE_LIST:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1138 n = r_long(p);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1139 if (PyErr_Occurred())
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1140 break;
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
1141 if (n < 0 || n > SIZE32_MAX) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1142 PyErr_SetString(PyExc_ValueError, "bad marshal data (list size out of range)");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1143 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1144 }
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
1145 v = PyList_New(n);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1146 R_REF(v);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1147 if (v == NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1148 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1149 for (i = 0; i < n; i++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1150 v2 = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1151 if ( v2 == NULL ) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1152 if (!PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1153 PyErr_SetString(PyExc_TypeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1154 "NULL object in marshal data for list");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1155 Py_DECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1156 v = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1157 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1158 }
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
1159 PyList_SET_ITEM(v, i, v2);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1160 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1161 retval = v;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1162 break;
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1163
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1164 case TYPE_DICT:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1165 v = PyDict_New();
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1166 R_REF(v);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1167 if (v == NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1168 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1169 for (;;) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1170 PyObject *key, *val;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1171 key = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1172 if (key == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1173 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1174 val = r_object(p);
86809
fc54e2848c98 Issue #19437: Fix r_object() of marshal module, handle PyDict_SetItem() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 86808
diff changeset
1175 if (val == NULL) {
fc54e2848c98 Issue #19437: Fix r_object() of marshal module, handle PyDict_SetItem() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 86808
diff changeset
1176 Py_DECREF(key);
fc54e2848c98 Issue #19437: Fix r_object() of marshal module, handle PyDict_SetItem() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 86808
diff changeset
1177 break;
fc54e2848c98 Issue #19437: Fix r_object() of marshal module, handle PyDict_SetItem() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 86808
diff changeset
1178 }
fc54e2848c98 Issue #19437: Fix r_object() of marshal module, handle PyDict_SetItem() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 86808
diff changeset
1179 if (PyDict_SetItem(v, key, val) < 0) {
fc54e2848c98 Issue #19437: Fix r_object() of marshal module, handle PyDict_SetItem() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 86808
diff changeset
1180 Py_DECREF(key);
fc54e2848c98 Issue #19437: Fix r_object() of marshal module, handle PyDict_SetItem() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 86808
diff changeset
1181 Py_DECREF(val);
fc54e2848c98 Issue #19437: Fix r_object() of marshal module, handle PyDict_SetItem() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 86808
diff changeset
1182 break;
fc54e2848c98 Issue #19437: Fix r_object() of marshal module, handle PyDict_SetItem() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 86808
diff changeset
1183 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1184 Py_DECREF(key);
86809
fc54e2848c98 Issue #19437: Fix r_object() of marshal module, handle PyDict_SetItem() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 86808
diff changeset
1185 Py_DECREF(val);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1186 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1187 if (PyErr_Occurred()) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1188 Py_DECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1189 v = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1190 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1191 retval = v;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1192 break;
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1193
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1194 case TYPE_SET:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1195 case TYPE_FROZENSET:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1196 n = r_long(p);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1197 if (PyErr_Occurred())
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1198 break;
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
1199 if (n < 0 || n > SIZE32_MAX) {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1200 PyErr_SetString(PyExc_ValueError, "bad marshal data (set size out of range)");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1201 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1202 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1203 v = (type == TYPE_SET) ? PySet_New(NULL) : PyFrozenSet_New(NULL);
82847
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
1204 if (type == TYPE_SET) {
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1205 R_REF(v);
82847
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
1206 } else {
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
1207 /* must use delayed registration of frozensets because they must
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
1208 * be init with a refcount of 1
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
1209 */
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
1210 idx = r_ref_reserve(flag, p);
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
1211 if (idx < 0)
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
1212 Py_CLEAR(v); /* signal error */
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
1213 }
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1214 if (v == NULL)
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1215 break;
86809
fc54e2848c98 Issue #19437: Fix r_object() of marshal module, handle PyDict_SetItem() failure
Victor Stinner <victor.stinner@gmail.com>
parents: 86808
diff changeset
1216
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1217 for (i = 0; i < n; i++) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1218 v2 = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1219 if ( v2 == NULL ) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1220 if (!PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1221 PyErr_SetString(PyExc_TypeError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1222 "NULL object in marshal data for set");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1223 Py_DECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1224 v = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1225 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1226 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1227 if (PySet_Add(v, v2) == -1) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1228 Py_DECREF(v);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1229 Py_DECREF(v2);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1230 v = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1231 break;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1232 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1233 Py_DECREF(v2);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1234 }
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1235 if (type != TYPE_SET)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1236 v = r_ref_insert(v, idx, flag, p);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1237 retval = v;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1238 break;
34394
b480abaf1f6f SF 1098985: set objects cannot be marshalled
Raymond Hettinger <python@rcn.com>
parents: 34205
diff changeset
1239
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1240 case TYPE_CODE:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1241 {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1242 int argcount;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1243 int kwonlyargcount;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1244 int nlocals;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1245 int stacksize;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1246 int flags;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1247 PyObject *code = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1248 PyObject *consts = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1249 PyObject *names = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1250 PyObject *varnames = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1251 PyObject *freevars = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1252 PyObject *cellvars = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1253 PyObject *filename = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1254 PyObject *name = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1255 int firstlineno;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1256 PyObject *lnotab = NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1257
82847
f4c21179690b Issue #16475: Simplify the interface to r_ref_allocate and improve comments.
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82829
diff changeset
1258 idx = r_ref_reserve(flag, p);
86301
500b4b62c19a Issue #19219 Remove a lot of "retval = NULL" statements, now that retval
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86278
diff changeset
1259 if (idx < 0)
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1260 break;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1261
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1262 v = NULL;
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1263
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1264 /* XXX ignore long->int overflows for now */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1265 argcount = (int)r_long(p);
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1266 if (PyErr_Occurred())
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1267 goto code_error;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1268 kwonlyargcount = (int)r_long(p);
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1269 if (PyErr_Occurred())
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1270 goto code_error;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1271 nlocals = (int)r_long(p);
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1272 if (PyErr_Occurred())
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1273 goto code_error;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1274 stacksize = (int)r_long(p);
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1275 if (PyErr_Occurred())
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1276 goto code_error;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1277 flags = (int)r_long(p);
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1278 if (PyErr_Occurred())
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1279 goto code_error;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1280 code = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1281 if (code == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1282 goto code_error;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1283 consts = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1284 if (consts == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1285 goto code_error;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1286 names = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1287 if (names == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1288 goto code_error;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1289 varnames = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1290 if (varnames == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1291 goto code_error;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1292 freevars = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1293 if (freevars == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1294 goto code_error;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1295 cellvars = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1296 if (cellvars == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1297 goto code_error;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1298 filename = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1299 if (filename == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1300 goto code_error;
70435
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1301 if (PyUnicode_CheckExact(filename)) {
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1302 if (p->current_filename != NULL) {
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1303 if (!PyUnicode_Compare(filename, p->current_filename)) {
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1304 Py_DECREF(filename);
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1305 Py_INCREF(p->current_filename);
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1306 filename = p->current_filename;
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1307 }
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1308 }
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1309 else {
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1310 p->current_filename = filename;
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1311 }
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1312 }
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1313 name = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1314 if (name == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1315 goto code_error;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1316 firstlineno = (int)r_long(p);
86310
d6d340549c80 Catch a few extra error cases in marshal.c
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86301
diff changeset
1317 if (firstlineno == -1 && PyErr_Occurred())
d6d340549c80 Catch a few extra error cases in marshal.c
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86301
diff changeset
1318 break;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1319 lnotab = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1320 if (lnotab == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1321 goto code_error;
35008
44e3f21f0525 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34413
diff changeset
1322
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1323 v = (PyObject *) PyCode_New(
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1324 argcount, kwonlyargcount,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1325 nlocals, stacksize, flags,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1326 code, consts, names, varnames,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1327 freevars, cellvars, filename, name,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1328 firstlineno, lnotab);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1329 v = r_ref_insert(v, idx, flag, p);
35008
44e3f21f0525 This is my patch:
Michael W. Hudson <mwh@python.net>
parents: 34413
diff changeset
1330
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1331 code_error:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1332 Py_XDECREF(code);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1333 Py_XDECREF(consts);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1334 Py_XDECREF(names);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1335 Py_XDECREF(varnames);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1336 Py_XDECREF(freevars);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1337 Py_XDECREF(cellvars);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1338 Py_XDECREF(filename);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1339 Py_XDECREF(name);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1340 Py_XDECREF(lnotab);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1341 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1342 retval = v;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1343 break;
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1344
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1345 case TYPE_REF:
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1346 n = r_long(p);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1347 if (n < 0 || n >= PyList_GET_SIZE(p->refs)) {
86310
d6d340549c80 Catch a few extra error cases in marshal.c
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86301
diff changeset
1348 if (n == -1 && PyErr_Occurred())
d6d340549c80 Catch a few extra error cases in marshal.c
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents: 86301
diff changeset
1349 break;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1350 PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1351 break;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1352 }
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1353 v = PyList_GET_ITEM(p->refs, n);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1354 if (v == Py_None) {
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1355 PyErr_SetString(PyExc_ValueError, "bad marshal data (invalid reference)");
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1356 break;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1357 }
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1358 Py_INCREF(v);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1359 retval = v;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1360 break;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1361
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1362 default:
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1363 /* Bogus data got written, which isn't ideal.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1364 This will let you keep working and recover. */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1365 PyErr_SetString(PyExc_ValueError, "bad marshal data (unknown type code)");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1366 break;
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1367
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1368 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1369 p->depth--;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1370 return retval;
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1371 }
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1372
32343
54b257e55f99 Make private function static
Neal Norwitz <nnorwitz@gmail.com>
parents: 32305
diff changeset
1373 static PyObject *
31824
331de8b9d1ed Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents: 30311
diff changeset
1374 read_object(RFILE *p)
331de8b9d1ed Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents: 30311
diff changeset
1375 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1376 PyObject *v;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1377 if (PyErr_Occurred()) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1378 fprintf(stderr, "XXX readobject called with exception set\n");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1379 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1380 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1381 v = r_object(p);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1382 if (v == NULL && !PyErr_Occurred())
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1383 PyErr_SetString(PyExc_TypeError, "NULL object in marshal data for object");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1384 return v;
31824
331de8b9d1ed Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents: 30311
diff changeset
1385 }
331de8b9d1ed Marshal clean-up (SF patch #873224)
Armin Rigo <arigo@tunes.org>
parents: 30311
diff changeset
1386
20552
65dd6157f999 SF patch #470393 (Jim Ahlstrom): Add missing marshal function
Guido van Rossum <guido@python.org>
parents: 19605
diff changeset
1387 int
65dd6157f999 SF patch #470393 (Jim Ahlstrom): Add missing marshal function
Guido van Rossum <guido@python.org>
parents: 19605
diff changeset
1388 PyMarshal_ReadShortFromFile(FILE *fp)
65dd6157f999 SF patch #470393 (Jim Ahlstrom): Add missing marshal function
Guido van Rossum <guido@python.org>
parents: 19605
diff changeset
1389 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1390 RFILE rf;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1391 int res;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1392 assert(fp);
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1393 rf.readable = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1394 rf.fp = fp;
70435
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1395 rf.current_filename = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1396 rf.end = rf.ptr = NULL;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1397 rf.buf = NULL;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1398 res = r_short(&rf);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1399 if (rf.buf != NULL)
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1400 PyMem_FREE(rf.buf);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1401 return res;
20552
65dd6157f999 SF patch #470393 (Jim Ahlstrom): Add missing marshal function
Guido van Rossum <guido@python.org>
parents: 19605
diff changeset
1402 }
65dd6157f999 SF patch #470393 (Jim Ahlstrom): Add missing marshal function
Guido van Rossum <guido@python.org>
parents: 19605
diff changeset
1403
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1404 long
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
1405 PyMarshal_ReadLongFromFile(FILE *fp)
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1406 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1407 RFILE rf;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1408 long res;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1409 rf.fp = fp;
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1410 rf.readable = NULL;
70435
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1411 rf.current_filename = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1412 rf.ptr = rf.end = NULL;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1413 rf.buf = NULL;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1414 res = r_long(&rf);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1415 if (rf.buf != NULL)
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1416 PyMem_FREE(rf.buf);
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1417 return res;
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1418 }
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1419
15979
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1420 #ifdef HAVE_FSTAT
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1421 /* Return size of file in bytes; < 0 if unknown. */
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1422 static off_t
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1423 getfilesize(FILE *fp)
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1424 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1425 struct stat st;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1426 if (fstat(fileno(fp), &st) != 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1427 return -1;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1428 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1429 return st.st_size;
15979
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1430 }
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1431 #endif
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1432
15979
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1433 /* If we can get the size of the file up-front, and it's reasonably small,
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1434 * read it in one gulp and delegate to ...FromString() instead. Much quicker
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1435 * than reading a byte at a time from file; speeds .pyc imports.
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1436 * CAUTION: since this may read the entire remainder of the file, don't
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1437 * call it unless you know you're done with the file.
15979
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1438 */
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1439 PyObject *
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1440 PyMarshal_ReadLastObjectFromFile(FILE *fp)
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1441 {
60825
92a633a30acb Merged revisions 80325 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56594
diff changeset
1442 /* REASONABLE_FILE_LIMIT is by defn something big enough for Tkinter.pyc. */
15979
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1443 #define REASONABLE_FILE_LIMIT (1L << 18)
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1444 #ifdef HAVE_FSTAT
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1445 off_t filesize;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1446 filesize = getfilesize(fp);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60825
diff changeset
1447 if (filesize > 0 && filesize <= REASONABLE_FILE_LIMIT) {
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60825
diff changeset
1448 char* pBuf = (char *)PyMem_MALLOC(filesize);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1449 if (pBuf != NULL) {
82190
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
1450 size_t n = fread(pBuf, 1, (size_t)filesize, fp);
e0464fa28c85 Issue #5308: Raise ValueError when marshalling too large object (a sequence
Serhiy Storchaka <storchaka@gmail.com>
parents: 75374
diff changeset
1451 PyObject* v = PyMarshal_ReadObjectFromString(pBuf, n);
61466
cab14be0ada1 Recorded merge of revisions 81029 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 60825
diff changeset
1452 PyMem_FREE(pBuf);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1453 return v;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1454 }
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1455
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1456 }
15979
a36a4932e399 Variant of SF patch 103252: Startup optimize: read *.pyc as string, not with getc().
Tim Peters <tim.peters@gmail.com>
parents: 15408
diff changeset
1457 #endif
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1458 /* We don't have fstat, or we do but the file is larger than
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1459 * REASONABLE_FILE_LIMIT or malloc failed -- read a byte at a time.
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1460 */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1461 return PyMarshal_ReadObjectFromFile(fp);
16311
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1462
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1463 #undef REASONABLE_FILE_LIMIT
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1464 }
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1465
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1466 PyObject *
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1467 PyMarshal_ReadObjectFromFile(FILE *fp)
8c41f3861aad It's unclear whether PyMarshal_XXX() are part of the public or private API.
Tim Peters <tim.peters@gmail.com>
parents: 16257
diff changeset
1468 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1469 RFILE rf;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1470 PyObject *result;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1471 rf.fp = fp;
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1472 rf.readable = NULL;
70435
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1473 rf.current_filename = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1474 rf.depth = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1475 rf.ptr = rf.end = NULL;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1476 rf.buf = NULL;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1477 rf.refs = PyList_New(0);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1478 if (rf.refs == NULL)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1479 return NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1480 result = r_object(&rf);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1481 Py_DECREF(rf.refs);
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1482 if (rf.buf != NULL)
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1483 PyMem_FREE(rf.buf);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1484 return result;
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1485 }
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1486
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1487 PyObject *
86484
3055e61f1e66 Issue #1772673: The type of `char*` arguments now changed to `const char*`.
Serhiy Storchaka <storchaka@gmail.com>
parents: 86376
diff changeset
1488 PyMarshal_ReadObjectFromString(const char *str, Py_ssize_t len)
1270
14369a5e6167 Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1489 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1490 RFILE rf;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1491 PyObject *result;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1492 rf.fp = NULL;
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1493 rf.readable = NULL;
70435
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1494 rf.current_filename = NULL;
86484
3055e61f1e66 Issue #1772673: The type of `char*` arguments now changed to `const char*`.
Serhiy Storchaka <storchaka@gmail.com>
parents: 86376
diff changeset
1495 rf.ptr = (char *)str;
3055e61f1e66 Issue #1772673: The type of `char*` arguments now changed to `const char*`.
Serhiy Storchaka <storchaka@gmail.com>
parents: 86376
diff changeset
1496 rf.end = (char *)str + len;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1497 rf.buf = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1498 rf.depth = 0;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1499 rf.refs = PyList_New(0);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1500 if (rf.refs == NULL)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1501 return NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1502 result = r_object(&rf);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1503 Py_DECREF(rf.refs);
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1504 if (rf.buf != NULL)
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1505 PyMem_FREE(rf.buf);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1506 return result;
1270
14369a5e6167 Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1507 }
14369a5e6167 Support for frozen scripts; added -i option.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
1508
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1509 PyObject *
32422
3df36e2e5ddc Patch #923098: Share interned strings in marshal.
Martin v. Löwis <martin@v.loewis.de>
parents: 32343
diff changeset
1510 PyMarshal_WriteObjectToString(PyObject *x, int version)
4039
23f280ab5409 Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents: 3839
diff changeset
1511 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1512 WFILE wf;
41653
52533370f1ad Make test_marshal pass. Not my best work. :-(
Guido van Rossum <guido@python.org>
parents: 41595
diff changeset
1513
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1514 wf.fp = NULL;
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1515 wf.readable = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1516 wf.str = PyBytes_FromStringAndSize((char *)NULL, 50);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1517 if (wf.str == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1518 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1519 wf.ptr = PyBytes_AS_STRING((PyBytesObject *)wf.str);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1520 wf.end = wf.ptr + PyBytes_Size(wf.str);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1521 wf.error = WFERR_OK;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1522 wf.depth = 0;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1523 wf.version = version;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1524 if (version >= 3) {
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1525 if ((wf.refs = PyDict_New()) == NULL)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1526 return NULL;
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1527 } else
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1528 wf.refs = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1529 w_object(x, &wf);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1530 Py_XDECREF(wf.refs);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1531 if (wf.str != NULL) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1532 char *base = PyBytes_AS_STRING((PyBytesObject *)wf.str);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1533 if (wf.ptr - base > PY_SSIZE_T_MAX) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1534 Py_DECREF(wf.str);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1535 PyErr_SetString(PyExc_OverflowError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1536 "too much marshal data for a string");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1537 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1538 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1539 if (_PyBytes_Resize(&wf.str, (Py_ssize_t)(wf.ptr - base)) < 0)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1540 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1541 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1542 if (wf.error != WFERR_OK) {
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1543 Py_XDECREF(wf.str);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1544 if (wf.error == WFERR_NOMEMORY)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1545 PyErr_NoMemory();
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1546 else
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1547 PyErr_SetString(PyExc_ValueError,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1548 (wf.error==WFERR_UNMARSHALLABLE)?"unmarshallable object"
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1549 :"object too deeply nested to marshal");
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1550 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1551 }
75371
61661842f225 Simplify code in marshal.c.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75370
diff changeset
1552 return wf.str;
4039
23f280ab5409 Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents: 3839
diff changeset
1553 }
23f280ab5409 Added *PyMarshal_WriteObjectToString() (moved the relevant code there
Guido van Rossum <guido@python.org>
parents: 3839
diff changeset
1554
279
9c924f9c3b16 Add marshalling for dictionaries.
Guido van Rossum <guido@python.org>
parents: 272
diff changeset
1555 /* And an interface for Python programs... */
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1556
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1557 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
1558 marshal_dump(PyObject *self, PyObject *args)
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1559 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1560 /* XXX Quick hack -- need to do this differently */
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1561 PyObject *x;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1562 PyObject *f;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1563 int version = Py_MARSHAL_VERSION;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1564 PyObject *s;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1565 PyObject *res;
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
1566 _Py_IDENTIFIER(write);
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
1567
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1568 if (!PyArg_ParseTuple(args, "OO|i:dump", &x, &f, &version))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1569 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1570 s = PyMarshal_WriteObjectToString(x, version);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1571 if (s == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1572 return NULL;
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
1573 res = _PyObject_CallMethodId(f, &PyId_write, "O", s);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1574 Py_DECREF(s);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1575 return res;
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1576 }
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1577
54035
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1578 PyDoc_STRVAR(dump_doc,
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1579 "dump(value, file[, version])\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1580 \n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1581 Write the value on the open file. The value must be a supported type.\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1582 The file must be an open file object such as sys.stdout or returned by\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1583 open() or os.popen(). It must be opened in binary mode ('wb' or 'w+b').\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1584 \n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1585 If the value has (or contains an object that has) an unsupported type, a\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1586 ValueError exception is raised — but garbage data will also be written\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1587 to the file. The object will not be properly read back by load()\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1588 \n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1589 The version argument indicates the data format that dump should use.");
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1590
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1591 static PyObject *
38490
a5298f371be7 Convert more modules to METH_VARARGS.
Georg Brandl <georg@python.org>
parents: 36866
diff changeset
1592 marshal_load(PyObject *self, PyObject *f)
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1593 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1594 PyObject *data, *result;
72925
7109f31300fb Rename _Py_identifier to _Py_IDENTIFIER.
Martin v. Löwis <martin@v.loewis.de>
parents: 72835
diff changeset
1595 _Py_IDENTIFIER(read);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1596 RFILE rf;
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1597
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1598 /*
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1599 * Make a call to the read method, but read zero bytes.
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1600 * This is to ensure that the object passed in at least
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1601 * has a read method which returns bytes.
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1602 * This can be removed if we guarantee good error handling
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1603 * for r_string()
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1604 */
72835
5e33219492bc Add API for static strings, primarily good for identifiers.
Martin v. Löwis <martin@v.loewis.de>
parents: 72475
diff changeset
1605 data = _PyObject_CallMethodId(f, &PyId_read, "i", 0);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1606 if (data == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1607 return NULL;
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1608 if (!PyBytes_Check(data)) {
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1609 PyErr_Format(PyExc_TypeError,
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1610 "f.read() returned not bytes but %.100s",
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1611 data->ob_type->tp_name);
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1612 result = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1613 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1614 else {
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1615 rf.depth = 0;
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1616 rf.fp = NULL;
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1617 rf.readable = f;
71136
42193f3ffc94 Correct uninitialized data problem in marshal code.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 71135
diff changeset
1618 rf.current_filename = NULL;
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1619 rf.ptr = rf.end = NULL;
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1620 rf.buf = NULL;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1621 if ((rf.refs = PyList_New(0)) != NULL) {
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1622 result = read_object(&rf);
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1623 Py_DECREF(rf.refs);
86255
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1624 if (rf.buf != NULL)
4059e871e74e Issue #19219: Speed up marshal.loads(), and make pyc files slightly (5% to 10%) smaller.
Antoine Pitrou <solipsis@pitrou.net>
parents: 85419
diff changeset
1625 PyMem_FREE(rf.buf);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1626 } else
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1627 result = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1628 }
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1629 Py_DECREF(data);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1630 return result;
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1631 }
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1632
54035
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1633 PyDoc_STRVAR(load_doc,
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1634 "load(file)\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1635 \n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1636 Read one value from the open file and return it. If no valid value is\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1637 read (e.g. because the data has a different Python version’s\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1638 incompatible marshal format), raise EOFError, ValueError or TypeError.\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1639 The file must be an open file object opened in binary mode ('rb' or\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1640 'r+b').\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1641 \n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1642 Note: If an object containing an unsupported type was marshalled with\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1643 dump(), load() will substitute None for the unmarshallable type.");
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1644
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1645
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1646 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
1647 marshal_dumps(PyObject *self, PyObject *args)
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1648 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1649 PyObject *x;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1650 int version = Py_MARSHAL_VERSION;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1651 if (!PyArg_ParseTuple(args, "O|i:dumps", &x, &version))
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1652 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1653 return PyMarshal_WriteObjectToString(x, version);
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1654 }
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1655
54035
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1656 PyDoc_STRVAR(dumps_doc,
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1657 "dumps(value[, version])\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1658 \n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1659 Return the string that would be written to a file by dump(value, file).\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1660 The value must be a supported type. Raise a ValueError exception if\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1661 value has (or contains an object that has) an unsupported type.\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1662 \n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1663 The version argument indicates the data format that dumps should use.");
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1664
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1665
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1666 static PyObject *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
1667 marshal_loads(PyObject *self, PyObject *args)
1191
38f0054f44a7 Added separate main program for the Mac: macmain.c
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
1668 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1669 RFILE rf;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1670 Py_buffer p;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1671 char *s;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1672 Py_ssize_t n;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1673 PyObject* result;
75374
4966907d3661 Issue #14177: marshal.loads() now raises TypeError when given an unicode string.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75369
diff changeset
1674 if (!PyArg_ParseTuple(args, "y*:loads", &p))
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1675 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1676 s = p.buf;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1677 n = p.len;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1678 rf.fp = NULL;
71131
edba722f3b02 Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents: 68257
diff changeset
1679 rf.readable = NULL;
70435
27359a4e0f8c try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
Benjamin Peterson <benjamin@python.org>
parents: 70434
diff changeset
1680 rf.current_filename = NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1681 rf.ptr = s;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1682 rf.end = s + n;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1683 rf.depth = 0;
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1684 if ((rf.refs = PyList_New(0)) == NULL)
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1685 return NULL;
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1686 result = read_object(&rf);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1687 PyBuffer_Release(&p);
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1688 Py_DECREF(rf.refs);
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1689 return result;
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1690 }
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1691
54035
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1692 PyDoc_STRVAR(loads_doc,
75374
4966907d3661 Issue #14177: marshal.loads() now raises TypeError when given an unicode string.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75369
diff changeset
1693 "loads(bytes)\n\
54035
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1694 \n\
75374
4966907d3661 Issue #14177: marshal.loads() now raises TypeError when given an unicode string.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75369
diff changeset
1695 Convert the bytes object to a value. If no valid value is found, raise\n\
4966907d3661 Issue #14177: marshal.loads() now raises TypeError when given an unicode string.
Antoine Pitrou <solipsis@pitrou.net>
parents: 75369
diff changeset
1696 EOFError, ValueError or TypeError. Extra characters in the input are\n\
54035
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1697 ignored.");
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1698
5373
5880384d7c71 Quickly renamed the remaining files -- this directory is done.
Guido van Rossum <guido@python.org>
parents: 5197
diff changeset
1699 static PyMethodDef marshal_methods[] = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1700 {"dump", marshal_dump, METH_VARARGS, dump_doc},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1701 {"load", marshal_load, METH_O, load_doc},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1702 {"dumps", marshal_dumps, METH_VARARGS, dumps_doc},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1703 {"loads", marshal_loads, METH_VARARGS, loads_doc},
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1704 {NULL, NULL} /* sentinel */
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1705 };
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1706
54035
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1707
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1708 PyDoc_STRVAR(module_doc,
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1709 "This module contains functions that can read and write Python values in\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1710 a binary format. The format is specific to Python, but independent of\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1711 machine architecture issues.\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1712 \n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1713 Not all Python object types are supported; in general, only objects\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1714 whose value is independent from a particular invocation of Python can be\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1715 written and read by this module. The following types are supported:\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1716 None, integers, floating point numbers, strings, bytes, bytearrays,\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1717 tuples, lists, sets, dictionaries, and code objects, where it\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1718 should be understood that tuples, lists and dictionaries are only\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1719 supported as long as the values contained therein are themselves\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1720 supported; and recursive lists and dictionaries should not be written\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1721 (they will cause infinite loops).\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1722 \n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1723 Variables:\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1724 \n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1725 version -- indicates the format that the module uses. Version 0 is the\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1726 historical format, version 1 shares interned strings and version 2\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1727 uses a binary format for floating point numbers.\n\
82816
01372117a5b4 Issue #16475: Support object instancing, recursion and interned strings
Kristján Valur Jónsson <sweskman@gmail.com>
parents: 82191
diff changeset
1728 Version 3 shares common object references (New in version 3.4).\n\
54035
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1729 \n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1730 Functions:\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1731 \n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1732 dump() -- write value to a file\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1733 load() -- read value from a file\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1734 dumps() -- write value to a string\n\
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1735 loads() -- read value from a string");
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1736
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1737
19e959626109 Merged revisions 72597 via svnmerge from
R. David Murray <rdmurray@bitdance.com>
parents: 53965
diff changeset
1738
48370
bfaf14295794 Rename a variable to be more in line with the name of the module.
Brett Cannon <bcannon@gmail.com>
parents: 48321
diff changeset
1739 static struct PyModuleDef marshalmodule = {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1740 PyModuleDef_HEAD_INIT,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1741 "marshal",
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1742 module_doc,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1743 0,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1744 marshal_methods,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1745 NULL,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1746 NULL,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1747 NULL,
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1748 NULL
47580
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47343
diff changeset
1749 };
94f64f11fdd0 Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents: 47343
diff changeset
1750
30311
f003e48f562d Bug #794140: cygwin builds do not embed
Jason Tishler <jason@tishler.net>
parents: 24435
diff changeset
1751 PyMODINIT_FUNC
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13447
diff changeset
1752 PyMarshal_Init(void)
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1753 {
61467
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1754 PyObject *mod = PyModule_Create(&marshalmodule);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1755 if (mod == NULL)
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1756 return NULL;
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1757 PyModule_AddIntConstant(mod, "version", Py_MARSHAL_VERSION);
f197407bdba2 Recorded merge of revisions 81032 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents: 56297
diff changeset
1758 return mod;
267
457661e47c7b Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1759 }