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

Fix NumPy >= 1.16 Support #220

Merged
merged 3 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion comtypes/automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ def _set_value(self, value):
elif npsupport.isndarray(value):
# Try to convert a simple array of basic types.
descr = value.dtype.descr[0][1]
typ = npsupport.numpy.ctypeslib._typecodes.get(descr)
typ = npsupport.typecodes.get(descr)
if typ is None:
# Try for variant
obj = _midlSAFEARRAY(VARIANT).create(value)
Expand Down
23 changes: 22 additions & 1 deletion comtypes/npsupport.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,33 @@ def isdatetime64(value):
return isinstance(value, datetime64)


def _check_ctypeslib_typecodes():
import numpy as np
from numpy import ctypeslib
try:
from numpy.ctypeslib import _typecodes
except ImportError:
from numpy.ctypeslib import as_ctypes_type

ctypes_to_dtypes = {}

for tp in set(np.sctypeDict.values()):
try:
ctype_for = as_ctypes_type(tp)
ctypes_to_dtypes[ctype_for] = tp
except NotImplementedError:
continue
ctypeslib._typecodes = ctypes_to_dtypes
return ctypeslib._typecodes


com_null_date64 = None
datetime64 = None
VARIANT_dtype = None
typecodes = {}

if HAVE_NUMPY:

typecodes = _check_ctypeslib_typecodes()
# dtype for VARIANT. This allows for packing of variants into an array, and
# subsequent conversion to a multi-dimensional safearray.
try:
Expand Down
4 changes: 2 additions & 2 deletions comtypes/safearray.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def create_from_ndarray(cls, value, extra, lBound=0):
ai = value.__array_interface__
if ai["version"] != 3:
raise TypeError("only __array_interface__ version 3 supported")
if cls._itemtype_ != numpy.ctypeslib._typecodes[ai["typestr"]]:
if cls._itemtype_ != npsupport.typecodes[ai["typestr"]]:
raise TypeError("Wrong array item type")

# SAFEARRAYs have Fortran order; convert the numpy array if needed
Expand Down Expand Up @@ -308,7 +308,7 @@ def _get_elements_raw(self, num_elements):
# XXX Only try to convert types known to
# numpy.ctypeslib.
if (safearray_as_ndarray and self._itemtype_ in
numpy.ctypeslib._typecodes.values()):
npsupport.typecodes.values()):
arr = numpy.ctypeslib.as_array(ptr,
(num_elements,))
return arr.copy()
Expand Down