Is there an alternative way to import ctypes.wintypes in Cygwin?

隐身守侯 提交于 2019-12-23 19:18:32

问题


Apparently using ctypes on Cygwin has the bad side effect of not recognizing the host OS as being using Windows, so the Python interpreter cannot use/import the often used Window (API) specific DLL loaders, such as:

from ctypes import WinDLL
import wintypes
import msvcrt

The reason is that the import mechanism is using sys.platform to check for win32, but receives cygwin, and thinks it's on another OS and therefore disables any Windows related imports.

As I have mentioned in this answer, you can import DLL's using the cdll loader from ctypes, but I have no idea if this is correct and what is the difference from using the windll.

Is there a way to side-load the windll (or wintypes, msvcrt) ctypes libraries on Cygwin?

(It's very sad that Cygwin, ctypes and the python maintainers have not figured out what to do in this case, even after 6+ years of being first reported.)


For reference, when trying to import wintypes, you get:

>>> from ctypes import wintypes
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.6/ctypes/wintypes.py", line 20, in <module>
    class VARIANT_BOOL(ctypes._SimpleCData):
ValueError: _type_ 'v' not supported

Where the offending lines are in wintypes.py:

class VARIANT_BOOL(ctypes._SimpleCData):
    _type_ = "v"
    def __repr__(self):
        return "%s(%r)" % (self.__class__.__name__, self.value)

ADENDUM

Probably we should not try to use the wintypes libraries in this use case. As stated in the python ctypes docs:

Note: Accessing the standard C library through cdll.msvcrt will use an outdated version of the library that may be incompatible with the one being used by Python. Where possible, use native Python functionality, or else import and use the msvcrt module.

来源:https://stackoverflow.com/questions/53781370/is-there-an-alternative-way-to-import-ctypes-wintypes-in-cygwin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!