diff --git a/src/calibre/devices/mtp/windows/device_enumeration.cpp b/src/calibre/devices/mtp/windows/device_enumeration.cpp index c06c33d403..f0030d0576 100644 --- a/src/calibre/devices/mtp/windows/device_enumeration.cpp +++ b/src/calibre/devices/mtp/windows/device_enumeration.cpp @@ -220,7 +220,7 @@ PyObject* get_device_information(IPortableDevice *device, IPortableDevicePropert LPWSTR temp; ULONG ti; PyObject *t, *ans = NULL, *storage = NULL; - char *type; + const char *type = NULL; Py_BEGIN_ALLOW_THREADS; hr = CoCreateInstance(CLSID_PortableDeviceKeyCollection, NULL, @@ -302,7 +302,11 @@ PyObject* get_device_information(IPortableDevice *device, IPortableDevicePropert default: type = "unknown"; } +#if PY_MAJOR_VERSION >= 3 + t = PyUnicode_FromString(type); +#else t = PyString_FromString(type); +#endif if (t != NULL) { PyDict_SetItemString(ans, "type", t); Py_DECREF(t); } diff --git a/src/calibre/utils/podofo/utils.cpp b/src/calibre/utils/podofo/utils.cpp index 668ae83e3d..dd934b7f3d 100644 --- a/src/calibre/utils/podofo/utils.cpp +++ b/src/calibre/utils/podofo/utils.cpp @@ -18,15 +18,14 @@ void pdf::podofo_set_exception(const PdfError &err) { PyObject * pdf::podofo_convert_pdfstring(const PdfString &s) { std::string raw = s.GetStringUtf8(); - return PyString_FromStringAndSize(raw.c_str(), raw.length()); + return PyBytes_FromStringAndSize(raw.c_str(), raw.length()); } PdfString * pdf::podofo_convert_pystring(PyObject *py) { - Py_UNICODE* u = PyUnicode_AS_UNICODE(py); - PyObject *u8 = PyUnicode_EncodeUTF8(u, PyUnicode_GET_SIZE(py), "replace"); - if (u8 == NULL) { PyErr_NoMemory(); return NULL; } - pdf_utf8 *s8 = reinterpret_cast(PyString_AS_STRING(u8)); + PyObject *u8 = PyUnicode_AsEncodedString(py, "UTF-8", "replace"); + if (u8 == NULL) { return NULL; } + pdf_utf8 *s8 = reinterpret_cast(PyBytes_AS_STRING(u8)); PdfString *ans = new PdfString(s8); Py_DECREF(u8); if (ans == NULL) PyErr_NoMemory(); @@ -35,10 +34,9 @@ pdf::podofo_convert_pystring(PyObject *py) { PdfString * pdf::podofo_convert_pystring_single_byte(PyObject *py) { - Py_UNICODE* u = PyUnicode_AS_UNICODE(py); - PyObject *s = PyUnicode_Encode(u, PyUnicode_GET_SIZE(py), "cp1252", "replace"); - if (s == NULL) { PyErr_NoMemory(); return NULL; } - PdfString *ans = new PdfString(PyString_AS_STRING(s)); + PyObject *s = PyUnicode_AsEncodedString(py, "cp1252", "replace"); + if (s == NULL) { return NULL; } + PdfString *ans = new PdfString(PyBytes_AS_STRING(s)); Py_DECREF(s); if (ans == NULL) PyErr_NoMemory(); return ans;