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

Skip to content

Commit 617dbc4

Browse files
committed
Checkpoint. A b it closer to working pickles and pickletools.
Added 'Y' getargs opcode which requires a bytes object.
1 parent 805365e commit 617dbc4

4 files changed

Lines changed: 26 additions & 8 deletions

File tree

Lib/pickletools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1760,11 +1760,12 @@ def assure_pickle_consistency(verbose=False):
17601760
print("skipping %r: it doesn't look like an opcode name" % name)
17611761
continue
17621762
picklecode = getattr(pickle, name)
1763-
if not isinstance(picklecode, str) or len(picklecode) != 1:
1763+
if not isinstance(picklecode, bytes) or len(picklecode) != 1:
17641764
if verbose:
17651765
print(("skipping %r: value %r doesn't look like a pickle "
17661766
"code" % (name, picklecode)))
17671767
continue
1768+
picklecode = picklecode.decode("latin-1")
17681769
if picklecode in copy:
17691770
if verbose:
17701771
print("checking name %r w/ code %r for consistency" % (

Lib/test/test_exceptions.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,12 +251,12 @@ def testAttributes(self):
251251
'print_file_and_line' : None, 'msg' : 'msgStr',
252252
'filename' : None, 'lineno' : None, 'offset' : None}),
253253
(UnicodeError, (), {'message' : '', 'args' : (),}),
254-
(UnicodeEncodeError, ('ascii', 'a', 0, 1, 'ordinal not in range'),
254+
(UnicodeEncodeError, (str8('ascii'), 'a', 0, 1, str8('ordinal not in range')),
255255
{'message' : '', 'args' : ('ascii', 'a', 0, 1,
256256
'ordinal not in range'),
257257
'encoding' : 'ascii', 'object' : 'a',
258258
'start' : 0, 'reason' : 'ordinal not in range'}),
259-
(UnicodeDecodeError, ('ascii', '\xff', 0, 1, 'ordinal not in range'),
259+
(UnicodeDecodeError, (str8('ascii'), b'\xff', 0, 1, str8('ordinal not in range')),
260260
{'message' : '', 'args' : ('ascii', '\xff', 0, 1,
261261
'ordinal not in range'),
262262
'encoding' : 'ascii', 'object' : '\xff',
@@ -278,6 +278,7 @@ def testAttributes(self):
278278

279279
for exc, args, expected in exceptionList:
280280
try:
281+
print("exc=%r, args=%r" % (exc, args))
281282
raise exc(*args)
282283
except BaseException as e:
283284
if type(e) is not exc:
@@ -297,7 +298,9 @@ def testAttributes(self):
297298
if p is None:
298299
continue # cPickle not found -- skip it
299300
for protocol in range(p.HIGHEST_PROTOCOL + 1):
300-
new = p.loads(p.dumps(e, protocol))
301+
##print("p=%s, protocol=%s, e=%r" % (p.__name__, protocol, e))
302+
s = p.dumps(e, protocol)
303+
new = p.loads(s)
301304
for checkArgName in expected:
302305
got = repr(getattr(new, checkArgName))
303306
want = repr(expected[checkArgName])

Modules/cPickle.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,7 +1335,8 @@ save_unicode(Picklerobject *self, PyObject *args, int doput)
13351335
if (!( repr = PyUnicode_AsUTF8String(args)))
13361336
return -1;
13371337

1338-
if ((size = PyString_Size(repr)) < 0)
1338+
assert(PyBytes_Check(repr));
1339+
if ((size = PyBytes_Size(repr)) < 0)
13391340
goto err;
13401341
if (size > INT_MAX)
13411342
return -1; /* string too large */
@@ -1354,7 +1355,7 @@ save_unicode(Picklerobject *self, PyObject *args, int doput)
13541355
PDATA_APPEND(self->file, repr, -1);
13551356
}
13561357
else {
1357-
if (self->write_func(self, PyString_AS_STRING(repr),
1358+
if (self->write_func(self, PyBytes_AS_STRING(repr),
13581359
size) < 0)
13591360
goto err;
13601361
}
@@ -5275,8 +5276,11 @@ cpm_loads(PyObject *self, PyObject *args)
52755276
PyObject *ob, *file = 0, *res = NULL;
52765277
Unpicklerobject *unpickler = 0;
52775278

5278-
if (!( PyArg_ParseTuple(args, "S:loads", &ob)))
5279-
goto finally;
5279+
if (!( PyArg_ParseTuple(args, "S:loads", &ob))) {
5280+
PyErr_Clear();
5281+
if (!PyArg_ParseTuple(args, "Y:loads", &ob))
5282+
goto finally;
5283+
}
52805284

52815285
if (!( file = PycStringIO->NewInput(ob)))
52825286
goto finally;

Python/getargs.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,6 +1099,15 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
10991099
return converterr("string", arg, msgbuf, bufsize);
11001100
break;
11011101
}
1102+
1103+
case 'Y': { /* bytes object */
1104+
PyObject **p = va_arg(*p_va, PyObject **);
1105+
if (PyBytes_Check(arg))
1106+
*p = arg;
1107+
else
1108+
return converterr("bytes", arg, msgbuf, bufsize);
1109+
break;
1110+
}
11021111

11031112
case 'U': { /* Unicode object */
11041113
PyObject **p = va_arg(*p_va, PyObject **);
@@ -1640,6 +1649,7 @@ skipitem(const char **p_format, va_list *p_va, int flags)
16401649
/* object codes */
16411650

16421651
case 'S': /* string object */
1652+
case 'Y': /* string object */
16431653
case 'U': /* unicode string object */
16441654
{
16451655
(void) va_arg(*p_va, PyObject **);

0 commit comments

Comments
 (0)