Hilko Bengen
2014-Jan-15 16:15 UTC
[Libguestfs] [PATCH 1/4] hivex: Python 2.6 does not have sysconfig.
--- configure.ac | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index 6785037..203f34f 100644 --- a/configure.ac +++ b/configure.ac @@ -329,8 +329,8 @@ AS_IF([test "x$enable_python" != "xno"], AC_MSG_CHECKING([for Python extension suffix (PEP-3149)]) if test -z "$PYTHON_EXT_SUFFIX"; then - python_ext_suffix=`$PYTHON -c "import sysconfig; \ - print (sysconfig.get_config_var('EXT_SUFFIX') or sysconfig.get_config_var('SO'))"` + python_ext_suffix=`$PYTHON -c "import distutils.sysconfig; \ + print (distutils.sysconfig.get_config_var('EXT_SUFFIX') or distutils.sysconfig.get_config_var('SO'))"` PYTHON_EXT_SUFFIX=$python_ext_suffix fi AC_MSG_RESULT([$PYTHON_EXT_SUFFIX]) -- 1.8.5.2
Hilko Bengen
2014-Jan-15 16:15 UTC
[Libguestfs] [PATCH 2/4] hivex: python: Fix encoding for "special" test script
Hopefully. Python's unicode history is a mess. --- python/t/130-special.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/python/t/130-special.py b/python/t/130-special.py index 7adb9d5..f0ac008 100755 --- a/python/t/130-special.py +++ b/python/t/130-special.py @@ -1,4 +1,13 @@ # coding: utf-8 +# http://stackoverflow.com/questions/6625782/unicode-literals-that-work-in-python-3-and-2 +import sys +if sys.version < '3': + import codecs + def u(x): + return codecs.unicode_escape_decode(x)[0] +else: + def u(x): + return x import os import hivex @@ -13,16 +22,20 @@ assert h root = h.root () assert root -ns = [ n for n in h.node_children (root) if h.node_name(n) == u"abcd_äöüß" ] +# "abcd_äöüß" +ns = [ n for n in h.node_children (root) if h.node_name(n) == u("abcd_\u00e4\u00f6\u00fc\u00df") ] assert len (ns) == 1 -vs = [ v for v in h.node_values (ns[0]) if h.value_key(v) == u"abcd_äöüß" ] +# "abcd_äöüß" +vs = [ v for v in h.node_values (ns[0]) if h.value_key(v) == u("abcd_\u00e4\u00f6\u00fc\u00df") ] assert len (vs) == 1 -ns = [ n for n in h.node_children (root) if h.node_name(n) == u"zero\0key" ] +ns = [ n for n in h.node_children (root) if h.node_name(n) == u("zero\0key") ] assert len (ns) == 1 -vs = [ v for v in h.node_values (ns[0]) if h.value_key(v) == u"zero\0val" ] +vs = [ v for v in h.node_values (ns[0]) if h.value_key(v) == u("zero\0val") ] assert len (vs) == 1 -ns = [ n for n in h.node_children (root) if h.node_name(n) == u"weird™" ] +# "weird™" +ns = [ n for n in h.node_children (root) if h.node_name(n) == u("weird\u2122") ] assert len (ns) == 1 -vs = [ v for v in h.node_values (ns[0]) if h.value_key(v) == u"symbols $£₤₧€" ] +# "symbols $£₤₧€" +vs = [ v for v in h.node_values (ns[0]) if h.value_key(v) == u("symbols \u0024\u00a3\u20a4\u20a7\u20ac") ] assert len (vs) == 1 -- 1.8.5.2
Hilko Bengen
2014-Jan-15 16:15 UTC
[Libguestfs] [PATCH 3/4] hivex: python: Produce Unicode strings in get_* methods
--- generator/generator.ml | 23 ++++------------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/generator/generator.ml b/generator/generator.ml index 1f2690d..908c5f3 100755 --- a/generator/generator.ml +++ b/generator/generator.ml @@ -2925,11 +2925,7 @@ put_string_list (char * const * const argv) list = PyList_New (argc); 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 + PyList_SetItem (list, i, PyUnicode_DecodeUTF8 (argv[i], strlen (argv[i]), NULL)); } return list; @@ -2985,11 +2981,7 @@ put_val_type (char *val, size_t len, hive_type t) { PyObject *r = PyTuple_New (2); 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 + PyTuple_SetItem (r, 1, PyUnicode_DecodeUTF8 (val, len, NULL)); return r; } @@ -3194,17 +3186,10 @@ put_val_type (char *val, size_t len, hive_type t) if f_len_exists name then pr " size_t sz = hivex_%s_len (%s);\n" name (String.concat ", " c_params); - pr "#ifdef HAVE_PYSTRING_ASSTRING\n"; - if f_len_exists name then - pr " py_r = PyString_FromStringAndSize (r, sz);\n" - else - pr " py_r = PyString_FromString (r);\n"; - pr "#else\n"; if f_len_exists name then - pr " py_r = PyUnicode_FromStringAndSize (r, sz);\n" + pr " py_r = PyUnicode_DecodeUTF8 (r, sz, NULL);\n" else - pr " py_r = PyUnicode_FromString (r);\n"; - pr "#endif\n"; + pr " py_r = PyUnicode_DecodeUTF8 (r, strlen (r), NULL);\n"; pr " free (r);" | RStringList -> pr " py_r = put_string_list (r);\n"; -- 1.8.5.2
Hilko Bengen
2014-Jan-15 16:15 UTC
[Libguestfs] [PATCH 4/4] hivex: python: Get rid of to_string function in test script
Since values are now returned as strings in Python2 and Python3, treating them as bytes in Python 3 would break tests. --- python/t/210-setvalue.py | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/python/t/210-setvalue.py b/python/t/210-setvalue.py index 9d93519..2ee7ac5 100644 --- a/python/t/210-setvalue.py +++ b/python/t/210-setvalue.py @@ -47,20 +47,12 @@ h.node_set_value (b, value1) value1 = { "key": "Key1", "t": 3, "value": "JKL" } h.node_set_value (b, value1) -# In Python2, the data is returned as a string. In Python3, it is -# returned as bytes. Provide a function to convert either to a string. -def to_string (data): - if sys.version_info[0] == 2: - return data - else: - return str (data, "utf-8") - val = h.node_get_value (b, "Key1") t_data = h.value_value (val) assert t_data[0] == 3 -assert to_string (t_data[1]) == "JKL" +assert t_data[1] == "JKL" val = h.node_get_value (b, "Key3") t_data = h.value_value (val) assert t_data[0] == 3 -assert to_string (t_data[1]) == "GHI" +assert t_data[1] == "GHI" -- 1.8.5.2
Richard W.M. Jones
2014-Jan-15 21:43 UTC
Re: [Libguestfs] [PATCH 1/4] hivex: Python 2.6 does not have sysconfig.
ACK to all 4 patches in this series. Thanks, Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones virt-df lists disk usage of guests without needing to install any software inside the virtual machine. Supports Linux and Windows. http://people.redhat.com/~rjones/virt-df/
Possibly Parallel Threads
- [PATCH] hivex: python: value_value no longer generates Unicode strings
- [hivex PATCH] ruby: improve test functions
- [PATCH 1/4] hivex: Python 2.6 does not have sysconfig.
- [hivex] [PATCH 0/6] Python fixes for node_set_value
- [PATCH] hivex: add hivex_set_value api call and perl bindings, tests