Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize BSTR definition. #599

Merged
merged 5 commits into from
Aug 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions comtypes/_post_coinit/bstr.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
from ctypes import _SimpleCData, windll
from typing import Any, Callable, TYPE_CHECKING

if TYPE_CHECKING:
from comtypes import hints # type: ignore


class BSTR(_SimpleCData):
"The windows BSTR data type"
"""The windows BSTR data type"""

_type_ = "X"
_needsfree = False

def __repr__(self):
return "%s(%r)" % (self.__class__.__name__, self.value)
def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.value!r})"

def __ctypes_from_outparam__(self):
def __ctypes_from_outparam__(self) -> Any:
self._needsfree = True
return self.value

def __del__(self, _free=windll.oleaut32.SysFreeString):
def __del__(
self, _free: Callable[["BSTR"], Any] = windll.oleaut32.SysFreeString
) -> None:
# Free the string if self owns the memory
# or if instructed by __ctypes_from_outparam__.
if self._b_base_ is None or self._needsfree:
_free(self)

@classmethod
def from_param(cls, value):
def from_param(cls, value: Any) -> "hints.Self":
"""Convert into a foreign function call parameter."""
if isinstance(value, cls):
return value
Expand Down