summaryrefslogtreecommitdiffstats
path: root/generator
diff options
context:
space:
mode:
authorRichard W.M. Jones <rjones@redhat.com>2011-11-19 16:54:26 +0000
committerRichard W.M. Jones <rjones@redhat.com>2011-11-19 16:54:26 +0000
commit207402a20ac8be74e767876f8ac93aba6292b2a9 (patch)
tree961eb78edec509b6a55f27b0047a042ef5760ae6 /generator
parenta6ba26dca8dd496bec71d65746b3c3b65e604f03 (diff)
downloadhivex-207402a20ac8be74e767876f8ac93aba6292b2a9.tar.gz
hivex-207402a20ac8be74e767876f8ac93aba6292b2a9.tar.xz
hivex-207402a20ac8be74e767876f8ac93aba6292b2a9.zip
python: Support Python 3 (RHBZ#752916).
Diffstat (limited to 'generator')
-rwxr-xr-xgenerator/generator.ml97
1 files changed, 73 insertions, 24 deletions
diff --git a/generator/generator.ml b/generator/generator.ml
index def516f..065c25d 100755
--- a/generator/generator.ml
+++ b/generator/generator.ml
@@ -2694,6 +2694,8 @@ and generate_python_c () =
generate_header CStyle LGPLv2plus;
pr "\
+#include <config.h>
+
#define PY_SSIZE_T_CLEAN 1
#include <Python.h>
@@ -2747,40 +2749,42 @@ static int
get_value (PyObject *v, hive_set_value *ret)
{
PyObject *obj;
+#ifndef HAVE_PYSTRING_ASSTRING
+ PyObject *bytes;
+#endif
obj = PyDict_GetItemString (v, \"key\");
if (!obj) {
PyErr_SetString (PyExc_RuntimeError, \"no 'key' element in dictionary\");
return -1;
}
- if (!PyString_Check (obj)) {
- PyErr_SetString (PyExc_RuntimeError, \"'key' element is not a string\");
- return -1;
- }
+#ifdef HAVE_PYSTRING_ASSTRING
ret->key = PyString_AsString (obj);
+#else
+ bytes = PyUnicode_AsUTF8String (obj);
+ ret->key = PyBytes_AS_STRING (bytes);
+#endif
obj = PyDict_GetItemString (v, \"t\");
if (!obj) {
PyErr_SetString (PyExc_RuntimeError, \"no 't' element in dictionary\");
return -1;
}
- if (!PyInt_Check (obj)) {
- PyErr_SetString (PyExc_RuntimeError, \"'t' element is not an integer\");
- return -1;
- }
- ret->t = PyInt_AsLong (obj);
+ ret->t = PyLong_AsLong (obj);
obj = PyDict_GetItemString (v, \"value\");
if (!obj) {
PyErr_SetString (PyExc_RuntimeError, \"no 'value' element in dictionary\");
return -1;
}
- if (!PyString_Check (obj)) {
- PyErr_SetString (PyExc_RuntimeError, \"'value' element is not a string\");
- return -1;
- }
+#ifdef HAVE_PYSTRING_ASSTRING
ret->value = PyString_AsString (obj);
ret->len = PyString_Size (obj);
+#else
+ bytes = PyUnicode_AsUTF8String (obj);
+ ret->value = PyBytes_AS_STRING (bytes);
+ ret->len = PyBytes_GET_SIZE (bytes);
+#endif
return 0;
}
@@ -2834,8 +2838,13 @@ put_string_list (char * const * const argv)
;
list = PyList_New (argc);
- for (i = 0; i < argc; ++i)
+ for (i = 0; i < argc; ++i) {
+#ifdef HAVE_PYSTRING_ASSTRING
PyList_SetItem (list, i, PyString_FromString (argv[i]));
+#else
+ PyList_SetItem (list, i, PyUnicode_FromString (argv[i]));
+#endif
+ }
return list;
}
@@ -2871,7 +2880,7 @@ static PyObject *
put_len_type (size_t len, hive_type t)
{
PyObject *r = PyTuple_New (2);
- PyTuple_SetItem (r, 0, PyInt_FromLong ((long) t));
+ PyTuple_SetItem (r, 0, PyLong_FromLong ((long) t));
PyTuple_SetItem (r, 1, PyLong_FromLongLong ((long) len));
return r;
}
@@ -2880,8 +2889,12 @@ static PyObject *
put_val_type (char *val, size_t len, hive_type t)
{
PyObject *r = PyTuple_New (2);
- PyTuple_SetItem (r, 0, PyInt_FromLong ((long) t));
+ PyTuple_SetItem (r, 0, PyLong_FromLong ((long) t));
+#ifdef HAVE_PYSTRING_ASSTRING
PyTuple_SetItem (r, 1, PyString_FromStringAndSize (val, len));
+#else
+ PyTuple_SetItem (r, 1, PyBytes_FromStringAndSize (val, len));
+#endif
return r;
}
@@ -3077,7 +3090,11 @@ put_val_type (char *val, size_t len, hive_type t)
| RValue ->
pr " py_r = PyLong_FromLongLong (r);\n"
| RString ->
+ pr "#ifdef HAVE_PYSTRING_ASSTRING\n";
pr " py_r = PyString_FromString (r);\n";
+ pr "#else\n";
+ pr " py_r = PyUnicode_FromString (r);\n";
+ pr "#endif\n";
pr " free (r);"
| RStringList ->
pr " py_r = put_string_list (r);\n";
@@ -3088,7 +3105,7 @@ put_val_type (char *val, size_t len, hive_type t)
pr " py_r = put_val_type (r, len, t);\n";
pr " free (r);\n"
| RInt32 ->
- pr " py_r = PyInt_FromLong ((long) r);\n"
+ pr " py_r = PyLong_FromLong ((long) r);\n"
| RInt64 ->
pr " py_r = PyLong_FromLongLong (r);\n"
);
@@ -3110,22 +3127,54 @@ put_val_type (char *val, size_t len, hive_type t)
(* Init function. *)
pr "\
+#if PY_MAJOR_VERSION >= 3
+static struct PyModuleDef moduledef = {
+ PyModuleDef_HEAD_INIT,
+ \"libhivexmod\", /* m_name */
+ \"hivex module\", /* m_doc */
+ -1, /* m_size */
+ methods, /* m_methods */
+ NULL, /* m_reload */
+ NULL, /* m_traverse */
+ NULL, /* m_clear */
+ NULL, /* m_free */
+};
+#endif
+
+static PyObject *
+moduleinit (void)
+{
+ PyObject *m;
+
+#if PY_MAJOR_VERSION >= 3
+ m = PyModule_Create (&moduledef);
+#else
+ m = Py_InitModule ((char *) \"libhivexmod\", methods);
+#endif
+
+ return m; /* m might be NULL if module init failed */
+}
+
+#if PY_MAJOR_VERSION >= 3
+PyMODINIT_FUNC
+PyInit_libhivexmod (void)
+{
+ return moduleinit ();
+}
+#else
void
initlibhivexmod (void)
{
- static int initialized = 0;
-
- if (initialized) return;
- Py_InitModule ((char *) \"libhivexmod\", methods);
- initialized = 1;
+ (void) moduleinit ();
}
+#endif
"
and generate_python_py () =
generate_header HashStyle LGPLv2plus;
pr "\
-u\"\"\"Python bindings for hivex
+\"\"\"Python bindings for hivex
import hivex
h = hivex.Hivex (filename)
@@ -3178,7 +3227,7 @@ class Hivex:
pr " def %s (self" name;
List.iter (fun arg -> pr ", %s" (name_of_argt arg)) args;
pr "):\n";
- pr " u\"\"\"%s\"\"\"\n" shortdesc;
+ pr " \"\"\"%s\"\"\"\n" shortdesc;
pr " return libhivexmod.%s (self._o" name;
List.iter (
fun arg ->