[clang] [libclang/python] Add LIBCLANG_LIBRARY_PATH and LIBCLANG_LIBRARY_FILE (PR #170201)
Thomas Applencourt via cfe-commits
cfe-commits at lists.llvm.org
Thu Dec 4 09:27:23 PST 2025
https://github.com/TApplencourt updated https://github.com/llvm/llvm-project/pull/170201
>From 53ff62579c345b373fca0450cc3453061e1a8f4a Mon Sep 17 00:00:00 2001
From: tapplencourt <tapplencourt at anl.gov>
Date: Mon, 1 Dec 2025 20:54:20 +0000
Subject: [PATCH 1/9] Add LIBCLANG_LIBRARY_PATH and LIBCLANG_LIBRARY_FILE
---
clang/bindings/python/clang/cindex.py | 4 ++--
clang/docs/ReleaseNotes.rst | 3 +++
2 files changed, 5 insertions(+), 2 deletions(-)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index d352373e85c60..b728a8d8369ad 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -4383,8 +4383,8 @@ def register(item: LibFunc) -> None:
class Config:
- library_path = None
- library_file: str | None = None
+ library_path: str | None = os.environ.get("LIBCLANG_LIBRARY_PATH")
+ library_file: str | None = os.environ.get("LIBCLANG_LIBRARY_FILE")
compatibility_check = True
loaded = False
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 9f8d781c93021..fb812a21f2f31 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -170,6 +170,9 @@ Clang Python Bindings Potentially Breaking Changes
ElaboratedTypes. The value becomes unused, and all the existing users should
expect the former underlying type to be reported instead.
- Remove ``AccessSpecifier.NONE`` kind. No libclang interfaces ever returned this kind.
+- Added the environment variables ``LIBCLANG_LIBRARY_PATH`` and ``LIBCLANG_LIBRARY_FILE``,
+ which allow users to specify the directory path and the exact library file that
+ should be used to locate libclang.
What's New in Clang |release|?
==============================
>From 8de9e2df1d7bb06ab9cb0dc3b516a3734c827037 Mon Sep 17 00:00:00 2001
From: Thomas Applencourt <tapplencourt at anl.gov>
Date: Wed, 3 Dec 2025 11:27:31 -0600
Subject: [PATCH 2/9] Update clang/docs/ReleaseNotes.rst
Co-authored-by: Jannick Kremer <jannick.kremer at mailbox.org>
---
clang/docs/ReleaseNotes.rst | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index fb812a21f2f31..226048a5c290e 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -170,9 +170,9 @@ Clang Python Bindings Potentially Breaking Changes
ElaboratedTypes. The value becomes unused, and all the existing users should
expect the former underlying type to be reported instead.
- Remove ``AccessSpecifier.NONE`` kind. No libclang interfaces ever returned this kind.
-- Added the environment variables ``LIBCLANG_LIBRARY_PATH`` and ``LIBCLANG_LIBRARY_FILE``,
- which allow users to specify the directory path and the exact library file that
- should be used to locate libclang.
+- Allow setting the path to the libclang library via environment variables: ``LIBCLANG_LIBRARY_PATH``
+ to specifiy the path to the folder, or ``LIBCLANG_LIBRARY_FILE`` to specify the path to
+ the library file
What's New in Clang |release|?
==============================
>From 45478dfdc5baad6e04f7c4491a8d9fb27a28b339 Mon Sep 17 00:00:00 2001
From: tapplencourt <tapplencourt at anl.gov>
Date: Wed, 3 Dec 2025 16:58:47 +0000
Subject: [PATCH 3/9] Move too LIBCLANG_LIBRARY_PATH
---
clang/bindings/python/README.txt | 4 +-
clang/bindings/python/tests/CMakeLists.txt | 2 +-
.../tests/cindex/test_access_specifiers.py | 2 -
.../bindings/python/tests/cindex/test_cdb.py | 2 -
.../tests/cindex/test_code_completion.py | 2 -
.../python/tests/cindex/test_comment.py | 2 -
.../python/tests/cindex/test_cursor.py | 2 -
.../python/tests/cindex/test_cursor_kind.py | 2 -
.../tests/cindex/test_cursor_language.py | 2 -
.../python/tests/cindex/test_diagnostics.py | 2 -
.../tests/cindex/test_environment_variable.py | 43 +++++++++++++++++++
.../test_exception_specification_kind.py | 2 -
.../bindings/python/tests/cindex/test_file.py | 2 -
.../python/tests/cindex/test_index.py | 2 -
.../bindings/python/tests/cindex/test_lib.py | 2 -
.../python/tests/cindex/test_linkage.py | 2 -
.../python/tests/cindex/test_location.py | 2 -
.../python/tests/cindex/test_source_range.py | 2 -
.../python/tests/cindex/test_tls_kind.py | 2 -
.../python/tests/cindex/test_token_kind.py | 2 -
.../python/tests/cindex/test_tokens.py | 2 -
.../tests/cindex/test_translation_unit.py | 2 -
.../bindings/python/tests/cindex/test_type.py | 2 -
23 files changed, 46 insertions(+), 43 deletions(-)
create mode 100644 clang/bindings/python/tests/cindex/test_environment_variable.py
diff --git a/clang/bindings/python/README.txt b/clang/bindings/python/README.txt
index 3e509662144fa..1898b4d303b45 100644
--- a/clang/bindings/python/README.txt
+++ b/clang/bindings/python/README.txt
@@ -4,12 +4,12 @@
This directory implements Python bindings for Clang.
-You may need to set CLANG_LIBRARY_PATH so that the Clang library can be
+You may need to set LIBCLANG_LIBRARY_PATH so that the Clang library can be
found. The unit tests are designed to be run with any standard test
runner. For example:
--
$ env PYTHONPATH=$(echo ~/llvm/clang/bindings/python/) \
- CLANG_LIBRARY_PATH=$(llvm-config --libdir) \
+ LIBCLANG_LIBRARY_PATH=$(llvm-config --libdir) \
python3 -m unittest discover -v
tests.cindex.test_index.test_create ... ok
...
diff --git a/clang/bindings/python/tests/CMakeLists.txt b/clang/bindings/python/tests/CMakeLists.txt
index d9a6bbf452bd6..21fe6fb79793f 100644
--- a/clang/bindings/python/tests/CMakeLists.txt
+++ b/clang/bindings/python/tests/CMakeLists.txt
@@ -5,7 +5,7 @@
add_custom_target(check-clang-python
COMMAND ${CMAKE_COMMAND} -E env
CLANG_NO_DEFAULT_CONFIG=1
- CLANG_LIBRARY_PATH=$<TARGET_FILE_DIR:libclang>
+ LIBCLANG_LIBRARY_PATH=$<TARGET_FILE_DIR:libclang>
"${Python3_EXECUTABLE}" -m unittest discover
DEPENDS libclang
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/..)
diff --git a/clang/bindings/python/tests/cindex/test_access_specifiers.py b/clang/bindings/python/tests/cindex/test_access_specifiers.py
index ca2bbd3cc8611..b43fda26aba48 100644
--- a/clang/bindings/python/tests/cindex/test_access_specifiers.py
+++ b/clang/bindings/python/tests/cindex/test_access_specifiers.py
@@ -2,8 +2,6 @@
from clang.cindex import AccessSpecifier, Config
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_cdb.py b/clang/bindings/python/tests/cindex/test_cdb.py
index 5abe56f0d65f8..e88aef4569926 100644
--- a/clang/bindings/python/tests/cindex/test_cdb.py
+++ b/clang/bindings/python/tests/cindex/test_cdb.py
@@ -2,8 +2,6 @@
from clang.cindex import CompilationDatabase, CompilationDatabaseError, Config
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import gc
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_code_completion.py b/clang/bindings/python/tests/cindex/test_code_completion.py
index c7a86aa82a8eb..86ca26887672f 100644
--- a/clang/bindings/python/tests/cindex/test_code_completion.py
+++ b/clang/bindings/python/tests/cindex/test_code_completion.py
@@ -2,8 +2,6 @@
from clang.cindex import Config, TranslationUnit
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
from pathlib import Path
diff --git a/clang/bindings/python/tests/cindex/test_comment.py b/clang/bindings/python/tests/cindex/test_comment.py
index 1ecbb42c18ffc..991fed77ad93e 100644
--- a/clang/bindings/python/tests/cindex/test_comment.py
+++ b/clang/bindings/python/tests/cindex/test_comment.py
@@ -2,8 +2,6 @@
from clang.cindex import Config, TranslationUnit
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_cursor.py b/clang/bindings/python/tests/cindex/test_cursor.py
index 7cb616a7ef148..6cc544b0380f0 100644
--- a/clang/bindings/python/tests/cindex/test_cursor.py
+++ b/clang/bindings/python/tests/cindex/test_cursor.py
@@ -15,8 +15,6 @@
conf,
)
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import gc
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_cursor_kind.py b/clang/bindings/python/tests/cindex/test_cursor_kind.py
index 3b693ff45cfd4..65ea0e8e0142b 100644
--- a/clang/bindings/python/tests/cindex/test_cursor_kind.py
+++ b/clang/bindings/python/tests/cindex/test_cursor_kind.py
@@ -2,8 +2,6 @@
from clang.cindex import Config, CursorKind
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_cursor_language.py b/clang/bindings/python/tests/cindex/test_cursor_language.py
index de07a7bdeef40..9c9e3c0e87f0e 100644
--- a/clang/bindings/python/tests/cindex/test_cursor_language.py
+++ b/clang/bindings/python/tests/cindex/test_cursor_language.py
@@ -2,8 +2,6 @@
from clang.cindex import Config, LanguageKind
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_diagnostics.py b/clang/bindings/python/tests/cindex/test_diagnostics.py
index ee7d37c896d70..8bb01f58012d5 100644
--- a/clang/bindings/python/tests/cindex/test_diagnostics.py
+++ b/clang/bindings/python/tests/cindex/test_diagnostics.py
@@ -2,8 +2,6 @@
from clang.cindex import Config, Diagnostic
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_environment_variable.py b/clang/bindings/python/tests/cindex/test_environment_variable.py
new file mode 100644
index 0000000000000..47035943dfcef
--- /dev/null
+++ b/clang/bindings/python/tests/cindex/test_environment_variable.py
@@ -0,0 +1,43 @@
+import unittest
+import unittest.mock
+import sys
+import os
+from clang.cindex import Config
+
+
+class TestEnvironementVariable(unittest.TestCase):
+
+ def test_working_libclang_library_file(self):
+ ref_libclang_library_file = Config().get_filename()
+ with unittest.mock.patch.dict(
+ os.environ, {"LIBCLANG_LIBRARY_FILE": ref_libclang_library_file}
+ ):
+ Config().lib
+
+ @unittest.mock.patch.dict("os.environ", {"LIBCLANG_LIBRARY_FILE": "/dev/null"})
+ def _test_non_working_libclang_library_file(self):
+ with self.assertRaises(clang.cindex.LibclangError):
+ Config().lib
+
+ def test_working_libclang_library_path(self):
+ ref_libclang_library_file = Config().get_filename()
+ ref_libclang_library_path, filename = os.path.split(ref_libclang_library_file)
+ filename_root, filename_ext = os.path.splitext(filename)
+
+ # Config only recognizes the default libclang filename.
+ # If LIBCLANG_LIBRARY_FILE points to a non-standard name, skip this test.
+
+ if not (
+ filename_root == "libclang" and filename_ext in (".so", ".dll", ".dylib")
+ ):
+ self.skipTest(f"Skipping because {filename} is not a default libclang name")
+
+ with unittest.mock.patch.dict(
+ os.environ, {"LIBCLANG_LIBRARY_PATH": ref_libclang_library_path}
+ ):
+ Config().lib
+
+ @unittest.mock.patch.dict("os.environ", {"LIBCLANG_LIBRARY_PATH": "not_a_real_dir"})
+ def _test_non_working_libclang_library_path(self):
+ with self.assertRaises(clang.cindex.LibclangError):
+ Config().lib
diff --git a/clang/bindings/python/tests/cindex/test_exception_specification_kind.py b/clang/bindings/python/tests/cindex/test_exception_specification_kind.py
index f7806ffad8012..2cf5b33686028 100644
--- a/clang/bindings/python/tests/cindex/test_exception_specification_kind.py
+++ b/clang/bindings/python/tests/cindex/test_exception_specification_kind.py
@@ -2,8 +2,6 @@
from clang.cindex import Config, CursorKind, ExceptionSpecificationKind
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_file.py b/clang/bindings/python/tests/cindex/test_file.py
index 2be9b9e332611..06ad16bd89306 100644
--- a/clang/bindings/python/tests/cindex/test_file.py
+++ b/clang/bindings/python/tests/cindex/test_file.py
@@ -2,8 +2,6 @@
from clang.cindex import Config, File, Index, TranslationUnit
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_index.py b/clang/bindings/python/tests/cindex/test_index.py
index f3d3ac00e5f7b..2284bb12863c0 100644
--- a/clang/bindings/python/tests/cindex/test_index.py
+++ b/clang/bindings/python/tests/cindex/test_index.py
@@ -2,8 +2,6 @@
from clang.cindex import Config, Index, TranslationUnit
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_lib.py b/clang/bindings/python/tests/cindex/test_lib.py
index 5e88ebf9d8448..91d5dd74a6387 100644
--- a/clang/bindings/python/tests/cindex/test_lib.py
+++ b/clang/bindings/python/tests/cindex/test_lib.py
@@ -2,8 +2,6 @@
import clang.cindex
-if "CLANG_LIBRARY_PATH" in os.environ:
- clang.cindex.Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
import ast
diff --git a/clang/bindings/python/tests/cindex/test_linkage.py b/clang/bindings/python/tests/cindex/test_linkage.py
index 93bf43a042047..84631b547780d 100644
--- a/clang/bindings/python/tests/cindex/test_linkage.py
+++ b/clang/bindings/python/tests/cindex/test_linkage.py
@@ -2,8 +2,6 @@
from clang.cindex import Config, LinkageKind
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_location.py b/clang/bindings/python/tests/cindex/test_location.py
index 3c6b0357e2f83..404e5f6310e64 100644
--- a/clang/bindings/python/tests/cindex/test_location.py
+++ b/clang/bindings/python/tests/cindex/test_location.py
@@ -10,8 +10,6 @@
TranslationUnit,
)
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_source_range.py b/clang/bindings/python/tests/cindex/test_source_range.py
index ca3ebc4041955..9a7de6cd7ba77 100644
--- a/clang/bindings/python/tests/cindex/test_source_range.py
+++ b/clang/bindings/python/tests/cindex/test_source_range.py
@@ -3,8 +3,6 @@
from clang.cindex import Config, SourceLocation, SourceRange, TranslationUnit
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_tls_kind.py b/clang/bindings/python/tests/cindex/test_tls_kind.py
index f80a46f4d5680..8e8947f8582ee 100644
--- a/clang/bindings/python/tests/cindex/test_tls_kind.py
+++ b/clang/bindings/python/tests/cindex/test_tls_kind.py
@@ -2,8 +2,6 @@
from clang.cindex import Config, TLSKind
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_token_kind.py b/clang/bindings/python/tests/cindex/test_token_kind.py
index 594f30a448d84..7788722949a14 100644
--- a/clang/bindings/python/tests/cindex/test_token_kind.py
+++ b/clang/bindings/python/tests/cindex/test_token_kind.py
@@ -2,8 +2,6 @@
from clang.cindex import Config, TokenKind
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_tokens.py b/clang/bindings/python/tests/cindex/test_tokens.py
index 6658579c63835..c18b1648fea25 100644
--- a/clang/bindings/python/tests/cindex/test_tokens.py
+++ b/clang/bindings/python/tests/cindex/test_tokens.py
@@ -2,8 +2,6 @@
from clang.cindex import Config, CursorKind, SourceLocation, SourceRange, TokenKind
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import unittest
diff --git a/clang/bindings/python/tests/cindex/test_translation_unit.py b/clang/bindings/python/tests/cindex/test_translation_unit.py
index 272cf05bed7b7..c58c38bcdf100 100644
--- a/clang/bindings/python/tests/cindex/test_translation_unit.py
+++ b/clang/bindings/python/tests/cindex/test_translation_unit.py
@@ -13,8 +13,6 @@
TranslationUnitSaveError,
)
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import gc
import tempfile
diff --git a/clang/bindings/python/tests/cindex/test_type.py b/clang/bindings/python/tests/cindex/test_type.py
index cc101beca8cc5..eaa6686b6016c 100644
--- a/clang/bindings/python/tests/cindex/test_type.py
+++ b/clang/bindings/python/tests/cindex/test_type.py
@@ -10,8 +10,6 @@
TypeKind,
)
-if "CLANG_LIBRARY_PATH" in os.environ:
- Config.set_library_path(os.environ["CLANG_LIBRARY_PATH"])
import gc
import unittest
>From b13a58b4ffe45c021704d13a37fb5ff6e0d48be6 Mon Sep 17 00:00:00 2001
From: tapplencourt <tapplencourt at anl.gov>
Date: Wed, 3 Dec 2025 18:16:33 +0000
Subject: [PATCH 4/9] update error message
---
clang/bindings/python/clang/cindex.py | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index b728a8d8369ad..50b7f8e16c244 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -4468,11 +4468,12 @@ def get_cindex_library(self) -> CDLL:
try:
library = cdll.LoadLibrary(self.get_filename())
except OSError as e:
- msg = (
- str(e) + ". To provide a path to libclang use "
- "Config.set_library_path() or "
- "Config.set_library_file()."
- )
+ msg = str(e) + (
+ "To provide the path to libclang, you can use the environment variable "
+ "LIBCLANG_LIBRARY_PATH or call Config.set_library_path(). "
+ "Alternatively, you can specify the exact library file using "
+ "LIBCLANG_LIBRARY_FILE or Config.set_library_file()."
+ )
raise LibclangError(msg)
return library
>From 2b52da50c76d4e917d02bc24b34d9d61dbbdd213 Mon Sep 17 00:00:00 2001
From: tapplencourt <tapplencourt at anl.gov>
Date: Wed, 3 Dec 2025 18:21:51 +0000
Subject: [PATCH 5/9] Apply darker sugestion
---
clang/bindings/python/clang/cindex.py | 2 +-
clang/bindings/python/tests/cindex/test_environment_variable.py | 1 -
2 files changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 50b7f8e16c244..89c6edfde23f8 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -4473,7 +4473,7 @@ def get_cindex_library(self) -> CDLL:
"LIBCLANG_LIBRARY_PATH or call Config.set_library_path(). "
"Alternatively, you can specify the exact library file using "
"LIBCLANG_LIBRARY_FILE or Config.set_library_file()."
- )
+ )
raise LibclangError(msg)
return library
diff --git a/clang/bindings/python/tests/cindex/test_environment_variable.py b/clang/bindings/python/tests/cindex/test_environment_variable.py
index 47035943dfcef..db84bf22a890c 100644
--- a/clang/bindings/python/tests/cindex/test_environment_variable.py
+++ b/clang/bindings/python/tests/cindex/test_environment_variable.py
@@ -6,7 +6,6 @@
class TestEnvironementVariable(unittest.TestCase):
-
def test_working_libclang_library_file(self):
ref_libclang_library_file = Config().get_filename()
with unittest.mock.patch.dict(
>From 0ff4d953a6d32c060c899b1e851cb81645304a72 Mon Sep 17 00:00:00 2001
From: tapplencourt <tapplencourt at anl.gov>
Date: Wed, 3 Dec 2025 18:45:37 +0000
Subject: [PATCH 6/9] Fix negative tests...
---
.../tests/cindex/test_environment_variable.py | 31 ++++++++++++++-----
1 file changed, 23 insertions(+), 8 deletions(-)
diff --git a/clang/bindings/python/tests/cindex/test_environment_variable.py b/clang/bindings/python/tests/cindex/test_environment_variable.py
index db84bf22a890c..ead522a432685 100644
--- a/clang/bindings/python/tests/cindex/test_environment_variable.py
+++ b/clang/bindings/python/tests/cindex/test_environment_variable.py
@@ -2,24 +2,36 @@
import unittest.mock
import sys
import os
-from clang.cindex import Config
+
+
+def reset_import_and_get_frech_config():
+ # Reloads the clang.cindex module to reset any class-level state in Config.
+ sys.modules.pop("clang.cindex", None)
+ sys.modules.pop("clang", None)
+ from clang.cindex import Config
+
+ return Config()
class TestEnvironementVariable(unittest.TestCase):
def test_working_libclang_library_file(self):
- ref_libclang_library_file = Config().get_filename()
+ ref_libclang_library_file = reset_import_and_get_frech_config().get_filename()
with unittest.mock.patch.dict(
os.environ, {"LIBCLANG_LIBRARY_FILE": ref_libclang_library_file}
):
- Config().lib
+ reset_import_and_get_frech_config().lib
@unittest.mock.patch.dict("os.environ", {"LIBCLANG_LIBRARY_FILE": "/dev/null"})
- def _test_non_working_libclang_library_file(self):
+ def test_non_working_libclang_library_file(self):
+ config = reset_import_and_get_frech_config()
+ import clang.cindex
+
with self.assertRaises(clang.cindex.LibclangError):
- Config().lib
+ config.lib
def test_working_libclang_library_path(self):
- ref_libclang_library_file = Config().get_filename()
+ # Get adequate libclang path
+ ref_libclang_library_file = reset_import_and_get_frech_config().get_filename()
ref_libclang_library_path, filename = os.path.split(ref_libclang_library_file)
filename_root, filename_ext = os.path.splitext(filename)
@@ -34,9 +46,12 @@ def test_working_libclang_library_path(self):
with unittest.mock.patch.dict(
os.environ, {"LIBCLANG_LIBRARY_PATH": ref_libclang_library_path}
):
- Config().lib
+ reset_import_and_get_frech_config().lib
@unittest.mock.patch.dict("os.environ", {"LIBCLANG_LIBRARY_PATH": "not_a_real_dir"})
def _test_non_working_libclang_library_path(self):
+ config = reset_import_and_get_frech_config()
+ import clang.cindex
+
with self.assertRaises(clang.cindex.LibclangError):
- Config().lib
+ config.lib
>From d124b436355fb0205e91023f92a94f15a4e17290 Mon Sep 17 00:00:00 2001
From: tapplencourt <tapplencourt at anl.gov>
Date: Wed, 3 Dec 2025 18:52:24 +0000
Subject: [PATCH 7/9] One more set of fix
---
.../python/tests/cindex/test_environment_variable.py | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)
diff --git a/clang/bindings/python/tests/cindex/test_environment_variable.py b/clang/bindings/python/tests/cindex/test_environment_variable.py
index ead522a432685..aa3258d58e523 100644
--- a/clang/bindings/python/tests/cindex/test_environment_variable.py
+++ b/clang/bindings/python/tests/cindex/test_environment_variable.py
@@ -46,10 +46,16 @@ def test_working_libclang_library_path(self):
with unittest.mock.patch.dict(
os.environ, {"LIBCLANG_LIBRARY_PATH": ref_libclang_library_path}
):
+ # Remove LIBCLANG_LIBRARY_FILE to avoid it taking precedence if set by the user
+ # Need to be in the mocked environement
+ os.environ.pop("LIBCLANG_LIBRARY_FILE", None)
reset_import_and_get_frech_config().lib
@unittest.mock.patch.dict("os.environ", {"LIBCLANG_LIBRARY_PATH": "not_a_real_dir"})
- def _test_non_working_libclang_library_path(self):
+ def test_non_working_libclang_library_path(self):
+ # Remove LIBCLANG_LIBRARY_FILE to avoid it taking precedence if set by the user
+ os.environ.pop("LIBCLANG_LIBRARY_FILE", None)
+
config = reset_import_and_get_frech_config()
import clang.cindex
>From 5185f5fc881cff8f0006e5d33df0acfeadbf07bd Mon Sep 17 00:00:00 2001
From: Thomas Applencourt <tapplencourt at anl.gov>
Date: Thu, 4 Dec 2025 11:27:05 -0600
Subject: [PATCH 8/9] Update clang/bindings/python/clang/cindex.py
Co-authored-by: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
---
clang/bindings/python/clang/cindex.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 89c6edfde23f8..4576b75e0ea5a 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -4471,7 +4471,7 @@ def get_cindex_library(self) -> CDLL:
msg = str(e) + (
"To provide the path to libclang, you can use the environment variable "
"LIBCLANG_LIBRARY_PATH or call Config.set_library_path(). "
- "Alternatively, you can specify the exact library file using "
+ "Alternatively, you can specify path of the library file using "
"LIBCLANG_LIBRARY_FILE or Config.set_library_file()."
)
raise LibclangError(msg)
>From 3609e2d9c2dea6b1e235dc35d9b4dffa13f07616 Mon Sep 17 00:00:00 2001
From: Thomas Applencourt <tapplencourt at anl.gov>
Date: Thu, 4 Dec 2025 11:27:11 -0600
Subject: [PATCH 9/9] Update clang/bindings/python/clang/cindex.py
Co-authored-by: Vlad Serebrennikov <serebrennikov.vladislav at gmail.com>
---
clang/bindings/python/clang/cindex.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/clang/bindings/python/clang/cindex.py b/clang/bindings/python/clang/cindex.py
index 4576b75e0ea5a..1605605459f1f 100644
--- a/clang/bindings/python/clang/cindex.py
+++ b/clang/bindings/python/clang/cindex.py
@@ -4469,7 +4469,7 @@ def get_cindex_library(self) -> CDLL:
library = cdll.LoadLibrary(self.get_filename())
except OSError as e:
msg = str(e) + (
- "To provide the path to libclang, you can use the environment variable "
+ "To provide the path to the directory containing libclang, you can use the environment variable "
"LIBCLANG_LIBRARY_PATH or call Config.set_library_path(). "
"Alternatively, you can specify path of the library file using "
"LIBCLANG_LIBRARY_FILE or Config.set_library_file()."
More information about the cfe-commits
mailing list