From e641094ac9860a656b05750fbc7b0119e257703b Mon Sep 17 00:00:00 2001 From: Matej Kenda Date: Tue, 7 Jan 2020 15:59:04 +0100 Subject: [PATCH] Python3: fixed string type checking in iio.NetworkContext to be compatible with Python 2 and Python 3. Signed-off-by: Matej Kenda --- bindings/python/iio.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/bindings/python/iio.py b/bindings/python/iio.py index 88acb4fc1..76e2a8279 100644 --- a/bindings/python/iio.py +++ b/bindings/python/iio.py @@ -39,6 +39,15 @@ def _checkNegative(result, func, arguments): else: raise OSError(-result, _strerror(-result)) +# Python 2 and Python 3 compatible _isstring function. +def _isstring(s): + try: + # attempt to evaluate basestring (Python 2) + return isinstance(s, basestring) + except NameError: + # No basestring --> Python 3 + return isinstance(s, str) + class _ScanContext(Structure): pass class _ContextInfo(Structure): @@ -739,7 +748,7 @@ def __init__(self, _context=None): if(_context is None): self._context = _new_default() - elif type(_context) is str or type(_context) is unicode: + elif _isstring(_context): self._context = _new_uri(_context.encode('ascii')) else: self._context = _context