Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b0c168c

Browse files
committed
Tentative changes to make this work better on 64-bit machines.
A plain int that doesn't fit in 32 bits will be marshalled using a new type. 32 bits machines can't handle this and will issue a warning.
1 parent 9abe64a commit b0c168c

1 file changed

Lines changed: 66 additions & 3 deletions

File tree

Python/marshal.c

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ PERFORMANCE OF THIS SOFTWARE.
4646
#define TYPE_NONE 'N'
4747
#define TYPE_ELLIPSIS '.'
4848
#define TYPE_INT 'i'
49+
#define TYPE_INT64 'I'
4950
#define TYPE_FLOAT 'f'
5051
#define TYPE_COMPLEX 'x'
5152
#define TYPE_LONG 'l'
@@ -126,6 +127,18 @@ w_long(x, p)
126127
w_byte((int)((x>>24) & 0xff), p);
127128
}
128129

130+
static void
131+
w_long64(x, p)
132+
long x;
133+
WFILE *p;
134+
{
135+
w_long(x, p);
136+
w_byte((int)((x>>32) & 0xff), p);
137+
w_byte((int)((x>>40) & 0xff), p);
138+
w_byte((int)((x>>48) & 0xff), p);
139+
w_byte((int)((x>>56) & 0xff), p);
140+
}
141+
129142
static void
130143
w_object(v, p)
131144
object *v;
@@ -140,8 +153,16 @@ w_object(v, p)
140153
else if (v == Py_Ellipsis)
141154
w_byte(TYPE_ELLIPSIS, p);
142155
else if (is_intobject(v)) {
143-
w_byte(TYPE_INT, p);
144-
w_long(getintvalue(v), p);
156+
long x = GETINTVALUE((intobject *)v);
157+
long y = x>>31;
158+
if (y && y != -1) {
159+
w_byte(TYPE_INT64, p);
160+
w_long64(x, p);
161+
}
162+
else {
163+
w_byte(TYPE_INT, p);
164+
w_long(x, p);
165+
}
145166
}
146167
else if (is_longobject(v)) {
147168
longobject *ob = (longobject *)v;
@@ -307,7 +328,46 @@ r_long(p)
307328
x |= (long)rs_byte(p) << 16;
308329
x |= (long)rs_byte(p) << 24;
309330
}
310-
/* XXX If your long is > 32 bits, add sign-extension here!!! */
331+
/* Sign extension for 64-bit machines */
332+
x <<= (8*sizeof(long) - 32);
333+
x >>= (8*sizeof(long) - 32);
334+
return x;
335+
}
336+
337+
static long
338+
r_long64(p)
339+
RFILE *p;
340+
{
341+
register long x;
342+
register FILE *fp = p->fp;
343+
if (sizeof(long) < 8) {
344+
object *f = sysget("stderr");
345+
err_clear();
346+
if (f != NULL) {
347+
writestring(
348+
"Warning: un-marshal 64-bit int in 32-bit mode\n", f);
349+
}
350+
}
351+
if (fp) {
352+
x = getc(fp);
353+
x |= (long)getc(fp) << 8;
354+
x |= (long)getc(fp) << 16;
355+
x |= (long)getc(fp) << 24;
356+
x |= (long)getc(fp) << 32;
357+
x |= (long)getc(fp) << 40;
358+
x |= (long)getc(fp) << 48;
359+
x |= (long)getc(fp) << 56;
360+
}
361+
else {
362+
x = rs_byte(p);
363+
x |= (long)rs_byte(p) << 8;
364+
x |= (long)rs_byte(p) << 16;
365+
x |= (long)rs_byte(p) << 24;
366+
x |= (long)rs_byte(p) << 32;
367+
x |= (long)rs_byte(p) << 40;
368+
x |= (long)rs_byte(p) << 48;
369+
x |= (long)rs_byte(p) << 56;
370+
}
311371
return x;
312372
}
313373

@@ -339,6 +399,9 @@ r_object(p)
339399
case TYPE_INT:
340400
return newintobject(r_long(p));
341401

402+
case TYPE_INT64:
403+
return newintobject(r_long64(p));
404+
342405
case TYPE_LONG:
343406
{
344407
int size;

0 commit comments

Comments
 (0)