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;
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);
}

View File

@ -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<pdf_utf8 *>(PyString_AS_STRING(u8));
PyObject *u8 = PyUnicode_AsEncodedString(py, "UTF-8", "replace");
if (u8 == NULL) { return NULL; }
pdf_utf8 *s8 = reinterpret_cast<pdf_utf8 *>(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;