diff options
author | Masud Rahman <llvm@masudrahman.com> | 2017-10-21 16:13:41 +0000 |
---|---|---|
committer | Masud Rahman <llvm@masudrahman.com> | 2017-10-21 16:13:41 +0000 |
commit | a6d3ef724655a64ac137945615d3ff002c4f7993 (patch) | |
tree | dac701130c8102c0f483ce98638a1555858d13a4 /bindings | |
parent | 3bddc261ddb5a58ab6dcb502d4889c4ccd5775c4 (diff) |
[bindings] allow null strings in Python 3
Some API calls accept 'NULL' instead of a char array (e.g. the second
argument to 'clang_ParseTranslationUnit'). For Python 3 compatibility,
all strings are passed through 'c_interop_string' which expects to
receive only 'bytes' or 'str' objects. This change extends this
behavior to additionally allow 'None' to be supplied.
A test case was added which breaks in Python 3, and is fixed by this
change. All the test cases pass in both, Python 2 and Python 3.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@316264 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'bindings')
-rw-r--r-- | bindings/python/clang/cindex.py | 3 | ||||
-rw-r--r-- | bindings/python/tests/cindex/test_index.py | 2 |
2 files changed, 5 insertions, 0 deletions
diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py index 1dc1760e4f..d72dd14ef9 100644 --- a/bindings/python/clang/cindex.py +++ b/bindings/python/clang/cindex.py @@ -94,6 +94,9 @@ if sys.version_info[0] == 3: return cls(param) if isinstance(param, bytes): return cls(param) + if param is None: + # Support passing null to C functions expecting char arrays + return None raise TypeError("Cannot convert '{}' to '{}'".format(type(param).__name__, cls.__name__)) @staticmethod diff --git a/bindings/python/tests/cindex/test_index.py b/bindings/python/tests/cindex/test_index.py index dc173f04d2..ef76692a52 100644 --- a/bindings/python/tests/cindex/test_index.py +++ b/bindings/python/tests/cindex/test_index.py @@ -13,3 +13,5 @@ def test_parse(): assert isinstance(index, Index) tu = index.parse(os.path.join(kInputsDir, 'hello.cpp')) assert isinstance(tu, TranslationUnit) + tu = index.parse(None, ['-c', os.path.join(kInputsDir, 'hello.cpp')]) + assert isinstance(tu, TranslationUnit) |