-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#469] Implement cppbind helper for loading of C++ optimized code
- Loading branch information
Showing
3 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
""" | ||
The module provides helper for importing of optimized C++ classes. | ||
""" | ||
|
||
import importlib | ||
import os | ||
import typing | ||
|
||
from zserio.exception import PythonRuntimeException | ||
|
||
ZSERIO_PYTHON_IMPLEMENTATION_ENV = "ZSERIO_PYTHON_IMPLEMENTATION" | ||
ZSERIO_CPP_MODULE = "zserio_cpp" | ||
|
||
def import_cpp_class(cppname: str) -> typing.Optional[typing.Type[typing.Any]]: | ||
""" | ||
Tries to import optimized C++ implementation of the given python class if 'ZSERIO_PYTHON_IMPLEMENTATION' | ||
environment variable is either unset or set to 'cpp'. | ||
Depending on the content of the 'ZSERIO_PYTHON_IMPLEMENTATION' environment variable, | ||
it either fails when no C++ implementation is available ('cpp') or ignores missing implementation | ||
and just return the original python class (None) or even does not try to load the C++ implementation if | ||
the variable is set to anyhing else (e.g. 'python'). | ||
:param pyclass: Pure python class implemenation for which the C++ optimized version should be loaded. | ||
:param cppname: Name of optimized C++ class in case that it differs from the pyclass name. | ||
:returns: Requested implemenation of the given pyclass. | ||
:raises PythonRuntimeException: When the requested implementation is not available. | ||
""" | ||
|
||
impl = os.getenv(ZSERIO_PYTHON_IMPLEMENTATION_ENV) | ||
if not impl in [None, "python", "cpp", "c++"]: | ||
raise PythonRuntimeException(f"Zserio Python runtime implementation '{impl}' is not available!") | ||
|
||
if impl != "python": | ||
try: | ||
return getattr(importlib.import_module(ZSERIO_CPP_MODULE), cppname) | ||
except (ImportError, AttributeError) as err: | ||
if impl in ["cpp", "c++"]: | ||
message = f"Zserio C++ implementation of '{cppname}' is not available!" | ||
raise PythonRuntimeException(message) from err | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import unittest | ||
import os | ||
|
||
from zserio.cppbind import import_cpp_class | ||
from zserio.exception import PythonRuntimeException | ||
|
||
class MissingCppClass: | ||
pass | ||
|
||
class CppBindTest(unittest.TestCase): | ||
|
||
def test_invalid_env(self): | ||
os.environ["ZSERIO_PYTHON_IMPLEMENTATION"] = "invalid" | ||
with self.assertRaises(PythonRuntimeException): | ||
import_cpp_class(MissingCppClass) | ||
|
||
def test_missing_cpp_default(self): | ||
os.environ.pop("ZSERIO_PYTHON_IMPLEMENTATION") | ||
imported = import_cpp_class(MissingCppClass) | ||
self.assertEqual(MissingCppClass, imported) | ||
|
||
def test_missing_cpp_python(self): | ||
os.environ["ZSERIO_PYTHON_IMPLEMENTATION"] = "python" | ||
imported = import_cpp_class(MissingCppClass) | ||
self.assertEqual(MissingCppClass, imported) | ||
|
||
def test_missing_cpp_cpp(self): | ||
os.environ["ZSERIO_PYTHON_IMPLEMENTATION"] = "cpp" | ||
with self.assertRaises(PythonRuntimeException): | ||
import_cpp_class(MissingCppClass) |