Patch #923098: Share interned strings in marshal. · pythoncapi/cpython@ef82d2f · GitHub
Skip to content

Commit ef82d2f

Browse files
committed
Patch #923098: Share interned strings in marshal.
1 parent 8d97e33 commit ef82d2f

6 files changed

Lines changed: 122 additions & 31 deletions

File tree

Doc/api/utilities.tex

Lines changed: 14 additions & 3 deletions

Doc/lib/libmarshal.tex

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ \section{\module{marshal} ---
7373
a \exception{ValueError} exception is raised --- but garbage data
7474
will also be written to the file. The object will not be properly
7575
read back by \function{load()}.
76+
77+
\versionadded[The \var{version} argument indicates the data
78+
format that \code{dumps} should use.]{2.4}
7679
\end{funcdesc}
7780

7881
\begin{funcdesc}{load}{file}
@@ -86,15 +89,28 @@ \section{\module{marshal} ---
8689
\code{None} for the unmarshallable type.}
8790
\end{funcdesc}
8891

89-
\begin{funcdesc}{dumps}{value}
92+
\begin{funcdesc}{dumps}{value\optional{, version}}
9093
Return the string that would be written to a file by
9194
\code{dump(\var{value}, \var{file})}. The value must be a supported
9295
type. Raise a \exception{ValueError} exception if value has (or
9396
contains an object that has) an unsupported type.
97+
98+
\versionadded[The \var{version} argument indicates the data
99+
format that \code{dumps} should use.]{2.4}
94100
\end{funcdesc}
95101

96102
\begin{funcdesc}{loads}{string}
97103
Convert the string to a value. If no valid value is found, raise
98104
\exception{EOFError}, \exception{ValueError} or
99105
\exception{TypeError}. Extra characters in the string are ignored.
100106
\end{funcdesc}
107+
108+
In addition, the following constants are defined:
109+
110+
\begin{datadesc}{version}
111+
Indicates the format that the module uses. Version 0 is the
112+
historical format, version 1 (added in Python 2.4) shares
113+
interned strings. The current version is 1.
114+
115+
\versionadded{2.4}
116+
\end{datadesc}

Include/marshal.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
extern "C" {
88
#endif
99

10-
PyAPI_FUNC(void) PyMarshal_WriteLongToFile(long, FILE *);
11-
PyAPI_FUNC(void) PyMarshal_WriteObjectToFile(PyObject *, FILE *);
12-
PyAPI_FUNC(PyObject *) PyMarshal_WriteObjectToString(PyObject *);
10+
#define Py_MARSHAL_VERSION 1
11+
12+
PyAPI_FUNC(void) PyMarshal_WriteLongToFile(long, FILE *, int);
13+
PyAPI_FUNC(void) PyMarshal_WriteObjectToFile(PyObject *, FILE *, int);
14+
PyAPI_FUNC(PyObject *) PyMarshal_WriteObjectToString(PyObject *, int);
1315

1416
PyAPI_FUNC(long) PyMarshal_ReadLongFromFile(FILE *);
1517
PyAPI_FUNC(int) PyMarshal_ReadShortFromFile(FILE *);

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 2.4 alpha 1?
1212
Core and builtins
1313
-----------------
1414

15+
- marshal now shares interned strings. This change introduces
16+
a new .pyc magic.
17+
1518
- Bug #966623. classes created with type() in an exec(, {}) don't
1619
have a __module__, but code in typeobject assumed it would always
1720
be there.

Python/import.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ extern time_t PyOS_GetLastModificationTime(char *, FILE *);
2626
a .pyc file in text mode the magic number will be wrong; also, the
2727
Apple MPW compiler swaps their values, botching string constants.
2828
29-
Apparently, there was a distinction made between even and odd
30-
bytecodes that is related to Unicode. The details aren't clear,
31-
but the magic number has been odd for a long time.
29+
The magic numbers must be spaced apart atleast 2 values, as the
30+
-U interpeter flag will cause MAGIC+1 being used. They have been
31+
odd numbers for some time now.
3232
3333
There were a variety of old schemes for setting the magic number.
3434
The current working scheme is to increment the previous value by
@@ -47,9 +47,9 @@ extern time_t PyOS_GetLastModificationTime(char *, FILE *);
4747
Python 2.3a0: 62011
4848
Python 2.3a0: 62021
4949
Python 2.3a0: 62011 (!)
50-
Python 2.4a0: 62031
50+
Python 2.4a0: 62041
5151
*/
52-
#define MAGIC (62031 | ((long)'\r'<<16) | ((long)'\n'<<24))
52+
#define MAGIC (62041 | ((long)'\r'<<16) | ((long)'\n'<<24))
5353

5454
/* Magic word as global; note that _PyImport_Init() can change the
5555
value of this global to accommodate for alterations of how the
@@ -797,10 +797,10 @@ write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
797797
"# can't create %s\n", cpathname);
798798
return;
799799
}
800-
PyMarshal_WriteLongToFile(pyc_magic, fp);
800+
PyMarshal_WriteLongToFile(pyc_magic, fp, Py_MARSHAL_VERSION);
801801
/* First write a 0 for mtime */
802-
PyMarshal_WriteLongToFile(0L, fp);
803-
PyMarshal_WriteObjectToFile((PyObject *)co, fp);
802+
PyMarshal_WriteLongToFile(0L, fp, Py_MARSHAL_VERSION);
803+
PyMarshal_WriteObjectToFile((PyObject *)co, fp, Py_MARSHAL_VERSION);
804804
if (fflush(fp) != 0 || ferror(fp)) {
805805
if (Py_VerboseFlag)
806806
PySys_WriteStderr("# can't write %s\n", cpathname);
@@ -811,7 +811,7 @@ write_compiled_module(PyCodeObject *co, char *cpathname, long mtime)
811811
}
812812
/* Now write the true mtime */
813813
fseek(fp, 4L, 0);
814-
PyMarshal_WriteLongToFile(mtime, fp);
814+
PyMarshal_WriteLongToFile(mtime, fp, Py_MARSHAL_VERSION);
815815
fflush(fp);
816816
fclose(fp);
817817
if (Py_VerboseFlag)

Python/marshal.c

Lines changed: 74 additions & 15 deletions

0 commit comments

Comments
 (0)