Przeglądaj źródła
pythonlib: use isinstance to distinguish string types (fixes https://trac.osgeo.org/grass/changeset/65787)
isinstance(bytes) seems to better distinguish if we can/need to decode/encode
a string. hasattr with decode/encode fails in Python 2 because all are
present everywhere (see below).
This enables Python modules to be executed with non-ascii characters
as option values (test which uses UTF-8 included). This also asks for locale
only when needed.
In Python 2:
>>> print(hasattr(b"", "decode"), hasattr("", "decode"), hasattr(u"", "decode"))
(True, True, True)
>>> print(hasattr(b"", "encode"), hasattr("", "encode"), hasattr(u"", "encode"))
(True, True, True)
>>> print(isinstance(b"", bytes), isinstance("", bytes), isinstance(u"", bytes))
(True, True, False)
In Python 3:
>>> print(hasattr(b"", "decode"), hasattr("", "decode"), hasattr(u"", "decode"))
True False False
>>> print(hasattr(b"", "encode"), hasattr("", "encode"), hasattr(u"", "encode"))
False True True
>>> print(isinstance(b"", bytes), isinstance("", bytes), isinstance(u"", bytes))
True False False
git-svn-id: https://svn.osgeo.org/grass/grass/trunk@68927 15284696-431f-4ddb-bdfa-cd5b030d7da7