python: Add new compat PYARG_STR_UNI format
authorNoel Power <noel.power@suse.com>
Tue, 11 Dec 2018 15:18:10 +0000 (15:18 +0000)
committerDouglas Bagnall <dbagnall@samba.org>
Sat, 12 Jan 2019 23:40:26 +0000 (00:40 +0100)
In python2 PYARG_STR_UNI evaluates to et which allows str type
(e.g bytes) pass through unencoded and accepts unicode objects
encoded as utf8
In python3 PYARG_STR_UNI evaluates to es which allows str type
encoded as named/specified encoding

Signed-off-by: Noel Power <noel.power@suse.com>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
python/py3compat.h

index 5fa57f323d528a113ae6140df7abc42e1a2c3013..89b7552c791df7e359e20bc72565b4c5642fdddc 100644 (file)
 /* PyArg_ParseTuple/Py_BuildValue argument */
 
 #define PYARG_BYTES_LEN "y#"
+#define PYARG_STR_UNI "es"
 
 #else
 
 /* PyArg_ParseTuple/Py_BuildValue argument */
 
 #define PYARG_BYTES_LEN "s#"
+/*
+ * We want a format that will ensure unicode is encoded using the
+ * specified encoding 'utf8' (to obtain the char* array)
+ * In python3 we use "es" but in python2 the specifiying 'es' will
+ * result in the any incomming 'str' type being decoded first to ascii
+ * then encoded to the specified 'utf8' encoding. In order to avoid that
+ * we use format 'et' in python2 instead.
+ */
+#define PYARG_STR_UNI "et"
 
 /* Module init */