diff --git a/telegram/lib/darwin/libtdjson.dylib b/telegram/lib/darwin/x86_64/libtdjson.dylib similarity index 100% rename from telegram/lib/darwin/libtdjson.dylib rename to telegram/lib/darwin/x86_64/libtdjson.dylib diff --git a/telegram/tdjson.py b/telegram/tdjson.py index 2ad4694d..439de7eb 100644 --- a/telegram/tdjson.py +++ b/telegram/tdjson.py @@ -16,7 +16,8 @@ def _get_tdjson_lib_path() -> str: return system_library if platform.system().lower() == "darwin": - lib_name = "darwin/libtdjson.dylib" + platform_architecture = platform.machine() + lib_name = f"darwin/{platform_architecture}/libtdjson.dylib" else: lib_name = "linux/libtdjson.so" @@ -32,7 +33,9 @@ def __init__(self, library_path: Optional[str] = None, verbosity: int = 2) -> No self._build_client(library_path, verbosity) def __del__(self) -> None: - if hasattr(self, "_tdjson") and hasattr(self._tdjson, "_td_json_client_destroy"): + if hasattr(self, "_tdjson") and hasattr( + self._tdjson, "_td_json_client_destroy" + ): self.stop() def _build_client(self, library_path: str, verbosity: int) -> None: @@ -77,7 +80,9 @@ def _build_client(self, library_path: str, verbosity: int) -> None: fatal_error_callback_type = CFUNCTYPE(None, c_char_p) - self._td_set_log_fatal_error_callback = self._tdjson.td_set_log_fatal_error_callback + self._td_set_log_fatal_error_callback = ( + self._tdjson.td_set_log_fatal_error_callback + ) self._td_set_log_fatal_error_callback.restype = None self._td_set_log_fatal_error_callback.argtypes = [fatal_error_callback_type] diff --git a/tests/test_tdjson.py b/tests/test_tdjson.py index 16447d51..dda01a76 100644 --- a/tests/test_tdjson.py +++ b/tests/test_tdjson.py @@ -4,6 +4,46 @@ class TestGetTdjsonTdlibPath: + def test_for_darwin_x86_64(self): + mocked_system = Mock(return_value="Darwin") + mocked_machine_name = Mock(return_value="x86_64") + mocked_resource = Mock() + mocked_find_library = Mock(return_value=None) + + with patch("telegram.tdjson.platform.system", mocked_system): + with patch("telegram.tdjson.platform.machine", mocked_machine_name): + with patch( + "telegram.tdjson.pkg_resources.resource_filename", mocked_resource + ): + with patch( + "telegram.tdjson.ctypes.util.find_library", mocked_find_library + ): + _get_tdjson_lib_path() + + mocked_resource.assert_called_once_with( + "telegram", "lib/darwin/x86_64/libtdjson.dylib" + ) + + def test_for_darwin_arm64(self): + mocked_system = Mock(return_value="Darwin") + mocked_machine_name = Mock(return_value="arm64") + mocked_resource = Mock() + mocked_find_library = Mock(return_value=None) + + with patch("telegram.tdjson.platform.system", mocked_system): + with patch("telegram.tdjson.platform.machine", mocked_machine_name): + with patch( + "telegram.tdjson.pkg_resources.resource_filename", mocked_resource + ): + with patch( + "telegram.tdjson.ctypes.util.find_library", mocked_find_library + ): + _get_tdjson_lib_path() + + mocked_resource.assert_called_once_with( + "telegram", "lib/darwin/arm64/libtdjson.dylib" + ) + def test_for_darwin(self): mocked_system = Mock(return_value="Darwin") mocked_files = Mock()