Skip to content

Commit

Permalink
fix uninitialize_sta
Browse files Browse the repository at this point in the history
  • Loading branch information
dlech committed May 24, 2024
1 parent 59a4fbb commit 26a7893
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
9 changes: 7 additions & 2 deletions bleak/backends/winrt/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ def uninitialize_sta():
.. versionadded:: 0.22
"""

try:
assert_mta()
except BleakError:
_get_apartment_type()
except OSError as e:
# All is OK if not initialized yet. WinRT will initialize it.
if e.winerror == _CO_E_NOTINITIALIZED:
return
else:
ctypes.windll.ole32.CoUninitialize()
12 changes: 11 additions & 1 deletion tests/bleak/backends/winrt/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from ctypes import windll, wintypes

from bleak.backends.winrt.util import _check_hresult, assert_mta
from bleak.backends.winrt.util import _check_hresult, assert_mta, uninitialize_sta
from bleak.exc import BleakError

# https://learn.microsoft.com/en-us/windows/win32/api/objbase/ne-objbase-coinit
Expand Down Expand Up @@ -64,3 +64,13 @@ async def test_assert_mta_init_sta():
await assert_mta()
finally:
_CoUninitialize()


@pytest.mark.asyncio
async def test_uninitialize_sta():
"""Test device_path_from_characteristic_path."""

_CoInitializeEx(None, COINIT_APARTMENTTHREADED)
uninitialize_sta()

await assert_mta()

0 comments on commit 26a7893

Please sign in to comment.