Misc py3 porting work

This commit is contained in:
Kovid Goyal 2019-02-27 08:55:21 +05:30
parent 7a6fbdac2c
commit 7fd2cb4893
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 12 additions and 10 deletions

View File

@ -220,7 +220,7 @@ PyObject* get_device_information(IPortableDevice *device, IPortableDevicePropert
LPWSTR temp; LPWSTR temp;
ULONG ti; ULONG ti;
PyObject *t, *ans = NULL, *storage = NULL; PyObject *t, *ans = NULL, *storage = NULL;
char *type; const char *type = NULL;
Py_BEGIN_ALLOW_THREADS; Py_BEGIN_ALLOW_THREADS;
hr = CoCreateInstance(CLSID_PortableDeviceKeyCollection, NULL, hr = CoCreateInstance(CLSID_PortableDeviceKeyCollection, NULL,
@ -302,7 +302,11 @@ PyObject* get_device_information(IPortableDevice *device, IPortableDevicePropert
default: default:
type = "unknown"; type = "unknown";
} }
#if PY_MAJOR_VERSION >= 3
t = PyUnicode_FromString(type);
#else
t = PyString_FromString(type); t = PyString_FromString(type);
#endif
if (t != NULL) { if (t != NULL) {
PyDict_SetItemString(ans, "type", t); Py_DECREF(t); PyDict_SetItemString(ans, "type", t); Py_DECREF(t);
} }

View File

@ -18,15 +18,14 @@ void pdf::podofo_set_exception(const PdfError &err) {
PyObject * PyObject *
pdf::podofo_convert_pdfstring(const PdfString &s) { pdf::podofo_convert_pdfstring(const PdfString &s) {
std::string raw = s.GetStringUtf8(); std::string raw = s.GetStringUtf8();
return PyString_FromStringAndSize(raw.c_str(), raw.length()); return PyBytes_FromStringAndSize(raw.c_str(), raw.length());
} }
PdfString * PdfString *
pdf::podofo_convert_pystring(PyObject *py) { pdf::podofo_convert_pystring(PyObject *py) {
Py_UNICODE* u = PyUnicode_AS_UNICODE(py); PyObject *u8 = PyUnicode_AsEncodedString(py, "UTF-8", "replace");
PyObject *u8 = PyUnicode_EncodeUTF8(u, PyUnicode_GET_SIZE(py), "replace"); if (u8 == NULL) { return NULL; }
if (u8 == NULL) { PyErr_NoMemory(); return NULL; } pdf_utf8 *s8 = reinterpret_cast<pdf_utf8 *>(PyBytes_AS_STRING(u8));
pdf_utf8 *s8 = reinterpret_cast<pdf_utf8 *>(PyString_AS_STRING(u8));
PdfString *ans = new PdfString(s8); PdfString *ans = new PdfString(s8);
Py_DECREF(u8); Py_DECREF(u8);
if (ans == NULL) PyErr_NoMemory(); if (ans == NULL) PyErr_NoMemory();
@ -35,10 +34,9 @@ pdf::podofo_convert_pystring(PyObject *py) {
PdfString * PdfString *
pdf::podofo_convert_pystring_single_byte(PyObject *py) { pdf::podofo_convert_pystring_single_byte(PyObject *py) {
Py_UNICODE* u = PyUnicode_AS_UNICODE(py); PyObject *s = PyUnicode_AsEncodedString(py, "cp1252", "replace");
PyObject *s = PyUnicode_Encode(u, PyUnicode_GET_SIZE(py), "cp1252", "replace"); if (s == NULL) { return NULL; }
if (s == NULL) { PyErr_NoMemory(); return NULL; } PdfString *ans = new PdfString(PyBytes_AS_STRING(s));
PdfString *ans = new PdfString(PyString_AS_STRING(s));
Py_DECREF(s); Py_DECREF(s);
if (ans == NULL) PyErr_NoMemory(); if (ans == NULL) PyErr_NoMemory();
return ans; return ans;