r316264 - [bindings] allow null strings in Python 3

Masud Rahman via cfe-commits cfe-commits at lists.llvm.org
Sat Oct 21 09:13:41 PDT 2017


Author: frutiger
Date: Sat Oct 21 09:13:41 2017
New Revision: 316264

URL: http://llvm.org/viewvc/llvm-project?rev=316264&view=rev
Log:
[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.

Modified:
    cfe/trunk/bindings/python/clang/cindex.py
    cfe/trunk/bindings/python/tests/cindex/test_index.py

Modified: cfe/trunk/bindings/python/clang/cindex.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/clang/cindex.py?rev=316264&r1=316263&r2=316264&view=diff
==============================================================================
--- cfe/trunk/bindings/python/clang/cindex.py (original)
+++ cfe/trunk/bindings/python/clang/cindex.py Sat Oct 21 09:13:41 2017
@@ -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

Modified: cfe/trunk/bindings/python/tests/cindex/test_index.py
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/bindings/python/tests/cindex/test_index.py?rev=316264&r1=316263&r2=316264&view=diff
==============================================================================
--- cfe/trunk/bindings/python/tests/cindex/test_index.py (original)
+++ cfe/trunk/bindings/python/tests/cindex/test_index.py Sat Oct 21 09:13:41 2017
@@ -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)




More information about the cfe-commits mailing list