On Sunday, 21 May 2017 18:29:03 CEST Matteo Cafasso wrote:
The set_decode_error_handler function allows the User to set the decoding error scheme to be used when non UTF8 characters are encountered in Python 3.
s/User/user/, and s/UTF8/UTF-8/
The function has no effect in Python 2.
Signed-off-by: Matteo Cafasso <noxdafox@gmail.com> --- generator/python.ml | 16 ++++++++++++++++ python/handle.c | 18 ++++++++++++++++-- python/t/test830RHBZ1406906.py | 6 ++++++ 3 files changed, 38 insertions(+), 2 deletions(-)
diff --git a/generator/python.ml b/generator/python.ml index f7c1f80bb..66bb7f27d 100644 --- a/generator/python.ml +++ b/generator/python.ml @@ -82,6 +82,7 @@ put_handle (guestfs_h *g) }
extern void guestfs_int_py_extend_module (PyObject *module); +extern PyObject *guestfs_int_py_set_decode_error_handler (PyObject *self, PyObject *args);
extern PyObject *guestfs_int_py_create (PyObject *self, PyObject *args); extern PyObject *guestfs_int_py_close (PyObject *self, PyObject *args); @@ -577,6 +578,8 @@ and generate_python_module () =
(* Table of functions. *) pr "static PyMethodDef methods[] = {\n"; + pr " { (char *) \"set_decode_error_handler\", \n"; + pr " guestfs_int_py_set_decode_error_handler, METH_VARARGS, NULL },\n";
This is implemented as global for the whole module, which means changing the behaviour for an handle changes it for all the existing handles (and in a racy behaviour, even). This IMHO should be a per-handle setting.
pr " { (char *) \"create\", guestfs_int_py_create, METH_VARARGS, NULL },\n"; pr " { (char *) \"close\", guestfs_int_py_close, METH_VARARGS, NULL },\n"; pr " { (char *) \"set_event_callback\",\n"; @@ -728,6 +731,19 @@ class ClosedHandle(ValueError): pass
+def set_decode_error_handler(handler):
'handler' usually is a function/callback, while in this case is a behaviour/mode, so I'd use a different naming.
+ \"\"\"Set the error handling scheme to use for the handling + of decoding errors. + The default is 'strict' meaning that decoding errors raise a + UnicodeDecodeError. + + The other possible value is 'surrogateescape', see PEP383 for reference. + + Return the previous error handler. + \"\"\" + return libguestfsmod.set_decode_error_handler(handler) + + class GuestFS(object): \"\"\"Instances of this class are libguestfs API handles.\"\"\"
diff --git a/python/handle.c b/python/handle.c index 52c36f1d2..b665bb899 100644 --- a/python/handle.c +++ b/python/handle.c @@ -35,6 +35,8 @@
#include "actions.h"
+static const char *decode_error_handler = "strict"; + static PyObject **get_all_event_callbacks (guestfs_h *g, size_t *len_rtn);
void @@ -45,6 +47,17 @@ guestfs_int_py_extend_module (PyObject *module) }
PyObject * +guestfs_int_py_set_decode_error_handler (PyObject *self, PyObject *args) +{ + const char *previous_handler = decode_error_handler; + + if (!PyArg_ParseTuple (args, (char *) "s:set_decode_error_handler", &decode_error_handler)) + return NULL;
I really doubt "decode_error_handler" will hold a valid pointer after guestfs_int_py_set_decode_error_handler is done (and the args PyObject is disposed. -- Pino Toscano