[PATCH] D37855: [bindings] allow null strings in Python 3

Masud Rahman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 14 11:00:22 PDT 2017


frutiger created this revision.

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.


https://reviews.llvm.org/D37855

Files:
  bindings/python/clang/cindex.py
  bindings/python/tests/cindex/test_index.py


Index: bindings/python/tests/cindex/test_index.py
===================================================================
--- bindings/python/tests/cindex/test_index.py
+++ bindings/python/tests/cindex/test_index.py
@@ -13,3 +13,5 @@
     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)
Index: bindings/python/clang/cindex.py
===================================================================
--- bindings/python/clang/cindex.py
+++ bindings/python/clang/cindex.py
@@ -94,6 +94,9 @@
                 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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D37855.115249.patch
Type: text/x-patch
Size: 1065 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170914/709f3699/attachment.bin>


More information about the cfe-commits mailing list