summaryrefslogtreecommitdiff
path: root/python/py3compat.h
diff options
context:
space:
mode:
authorNoel Power <noel.power@suse.com>2018-03-07 14:39:54 +0000
committerAndrew Bartlett <abartlet@samba.org>2018-03-23 07:28:25 +0100
commitaea433ee0c87d42f670aa8b9e068b80cf2d9ef09 (patch)
tree444b4cf2896c886c09a98877757cc3178dd0ef5a /python/py3compat.h
parent0d65c1ef65b1485ae772ac87a52bb58611d45c72 (diff)
downloadsamba-aea433ee0c87d42f670aa8b9e068b80cf2d9ef09.tar.gz
python: Add compatability helpers to determine if type is really bytes
py3compat has PyBytes_Check macro which evalates to PyString_Check in python2. To help switch behaviour based on whether you are dealing with the bytes type the following macros have been added. IsPy3Bytes IsPy3BytesOrString IsPy3Bytes will evaluate to false in python2 and will return the expected result in python3. IsPy3BytesOrString will test for string type alone in python2 or bytes and string in python3. Signed-off-by: Noel Power <noel.power@suse.com> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Diffstat (limited to 'python/py3compat.h')
-rw-r--r--python/py3compat.h24
1 files changed, 24 insertions, 0 deletions
diff --git a/python/py3compat.h b/python/py3compat.h
index ce317c65f8e..5fa57f323d5 100644
--- a/python/py3compat.h
+++ b/python/py3compat.h
@@ -82,6 +82,19 @@
#define PY_DESC_PY3_BYTES "bytes"
#define PY_DESC_PY3_STRING "string"
+/* Determine if object is really bytes, for code that runs
+ * in python2 & python3 (note: PyBytes_Check is replaced by
+ * PyString_Check in python2) so care needs to be taken when
+ * writing code that will check if incoming type is bytes that
+ * will work as expected in python2 & python3
+ */
+
+#define IsPy3Bytes PyBytes_Check
+
+#define IsPy3BytesOrString(pystr) \
+ (PyStr_Check(pystr) || PyBytes_Check(pystr))
+
+
/* Ints */
#define PyInt_Type PyLong_Type
@@ -152,6 +165,17 @@
#define PY_DESC_PY3_BYTES "string"
#define PY_DESC_PY3_STRING "unicode"
+/* Determine if object is really bytes, for code that runs
+ * in python2 & python3 (note: PyBytes_Check is replaced by
+ * PyString_Check in python2) so care needs to be taken when
+ * writing code that will check if incoming type is bytes that
+ * will work as expected in python2 & python3
+ */
+
+#define IsPy3Bytes(pystr) false
+
+#define IsPy3BytesOrString PyStr_Check
+
/* PyArg_ParseTuple/Py_BuildValue argument */
#define PYARG_BYTES_LEN "s#"