summaryrefslogtreecommitdiffstats
path: root/Modules/_pickle.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-11-16 07:44:43 (GMT)
committerGitHub <noreply@github.com>2017-11-16 07:44:43 (GMT)
commit3daaafb700df45716bb55f3a293f88773baf3463 (patch)
tree72e6fe022b82ba7660e82f3561ef991e33a345dd /Modules/_pickle.c
parent0a2abdfca2495291809855cf7dfc6721c9c962e3 (diff)
downloadcpython-3daaafb700df45716bb55f3a293f88773baf3463.zip
cpython-3daaafb700df45716bb55f3a293f88773baf3463.tar.gz
cpython-3daaafb700df45716bb55f3a293f88773baf3463.tar.bz2
bpo-32037: Use the INT opcode for 32-bit integers in protocol 0 pickles. (#4407)
Diffstat (limited to 'Modules/_pickle.c')
-rw-r--r--Modules/_pickle.c46
1 files changed, 23 insertions, 23 deletions
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
index 4b7f1ed..943c701 100644
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -1858,18 +1858,13 @@ save_long(PicklerObject *self, PyObject *obj)
PyObject *repr = NULL;
Py_ssize_t size;
long val;
+ int overflow;
int status = 0;
- const char long_op = LONG;
-
- val= PyLong_AsLong(obj);
- if (val == -1 && PyErr_Occurred()) {
- /* out of range for int pickling */
- PyErr_Clear();
- }
- else if (self->bin &&
- (sizeof(long) <= 4 ||
- (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) {
+ val= PyLong_AsLongAndOverflow(obj, &overflow);
+ if (!overflow && (sizeof(long) <= 4 ||
+ (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1))))
+ {
/* result fits in a signed 4-byte integer.
Note: we can't use -0x80000000L in the above condition because some
@@ -1882,31 +1877,35 @@ save_long(PicklerObject *self, PyObject *obj)
char pdata[32];
Py_ssize_t len = 0;
- pdata[1] = (unsigned char)(val & 0xff);
- pdata[2] = (unsigned char)((val >> 8) & 0xff);
- pdata[3] = (unsigned char)((val >> 16) & 0xff);
- pdata[4] = (unsigned char)((val >> 24) & 0xff);
-
- if ((pdata[4] == 0) && (pdata[3] == 0)) {
- if (pdata[2] == 0) {
- pdata[0] = BININT1;
- len = 2;
+ if (self->bin) {
+ pdata[1] = (unsigned char)(val & 0xff);
+ pdata[2] = (unsigned char)((val >> 8) & 0xff);
+ pdata[3] = (unsigned char)((val >> 16) & 0xff);
+ pdata[4] = (unsigned char)((val >> 24) & 0xff);
+
+ if ((pdata[4] != 0) || (pdata[3] != 0)) {
+ pdata[0] = BININT;
+ len = 5;
}
- else {
+ else if (pdata[2] != 0) {
pdata[0] = BININT2;
len = 3;
}
+ else {
+ pdata[0] = BININT1;
+ len = 2;
+ }
}
else {
- pdata[0] = BININT;
- len = 5;
+ sprintf(pdata, "%c%ld\n", INT, val);
+ len = strlen(pdata);
}
-
if (_Pickler_Write(self, pdata, len) < 0)
return -1;
return 0;
}
+ assert(!PyErr_Occurred());
if (self->proto >= 2) {
/* Linear-time pickling. */
@@ -1986,6 +1985,7 @@ save_long(PicklerObject *self, PyObject *obj)
goto error;
}
else {
+ const char long_op = LONG;
const char *string;
/* proto < 2: write the repr and newline. This is quadratic-time (in