diff -r b9a0ecae02cb Lib/copy_reg.py --- a/Lib/copy_reg.py Sat Oct 10 10:52:35 2015 +0000 +++ b/Lib/copy_reg.py Mon Oct 12 23:31:47 2015 +0300 @@ -5,6 +5,7 @@ C, not for instances of user-defined cla """ from types import ClassType as _ClassType +import codecs __all__ = ["pickle", "constructor", "add_extension", "remove_extension", "clear_extension_cache"] @@ -41,6 +42,17 @@ else: pickle(complex, pickle_complex, complex) +# Support for pickling Python 3 compatible bytes objects. + +try: + unicode +except NameError: + Bytes = bytes +else: + class Bytes(bytes): + def __reduce__(self): + return codecs.encode, (unicode(self, 'latin1'), 'latin1') + # Support for pickling new-style objects def _reconstructor(cls, base, state): diff -r b9a0ecae02cb Modules/datetimemodule.c --- a/Modules/datetimemodule.c Sat Oct 10 10:52:35 2015 +0000 +++ b/Modules/datetimemodule.c Mon Oct 12 23:31:47 2015 +0300 @@ -1483,6 +1483,16 @@ static PyObject *us_per_day = NULL; static PyObject *us_per_week = NULL; /* 1e6*3600*24*7 as Python long */ static PyObject *seconds_per_day = NULL; /* 3600*24 as Python int */ +static PyObject *bytes_class = NULL; + +static PyObject * +convert_bytes(PyObject *bytes) +{ + if (bytes != NULL && bytes_class != NULL) + bytes = PyObject_CallFunction(bytes_class, "N", bytes); + return bytes; +} + /* --------------------------------------------------------------------------- * Class implementations. */ @@ -2696,8 +2706,9 @@ date_getstate(PyDateTime_Date *self) { return Py_BuildValue( "(N)", + convert_bytes( PyString_FromStringAndSize((char *)self->data, - _PyDateTime_DATE_DATASIZE)); + _PyDateTime_DATE_DATASIZE))); } static PyObject * @@ -3497,6 +3508,7 @@ time_getstate(PyDateTime_Time *self) basestate = PyString_FromStringAndSize((char *)self->data, _PyDateTime_TIME_DATASIZE); + basestate = convert_bytes(basestate); if (basestate != NULL) { if (! HASTZINFO(self) || self->tzinfo == Py_None) result = PyTuple_Pack(1, basestate); @@ -4570,6 +4582,7 @@ datetime_utctimetuple(PyDateTime_DateTim * So it's a tuple in any (non-error) case. * __getstate__ isn't exposed. */ + static PyObject * datetime_getstate(PyDateTime_DateTime *self) { @@ -4578,6 +4591,7 @@ datetime_getstate(PyDateTime_DateTime *s basestate = PyString_FromStringAndSize((char *)self->data, _PyDateTime_DATETIME_DATASIZE); + basestate = convert_bytes(basestate); if (basestate != NULL) { if (! HASTZINFO(self) || self->tzinfo == Py_None) result = PyTuple_Pack(1, basestate); @@ -4767,6 +4781,7 @@ initdatetime(void) PyObject *m; /* a module object */ PyObject *d; /* its dict */ PyObject *x; + PyObject *copyreg; m = Py_InitModule3("datetime", module_methods, "Fast implementation of the datetime type."); @@ -4916,6 +4931,14 @@ initdatetime(void) us_per_week = PyLong_FromDouble(604800000000.0); if (us_per_hour == NULL || us_per_day == NULL || us_per_week == NULL) return; + + copyreg = PyImport_ImportModuleNoBlock("copy_reg"); + if (copyreg == NULL) + return; + bytes_class = PyObject_GetAttrString(copyreg, "Bytes"); + Py_DECREF(copyreg); + if (bytes_class == NULL) + return; } /* ---------------------------------------------------------------------------