Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc868d4

Browse files
author
Hirokazu Yamamoto
committed
Issue #3612: Added new types to ctypes.wintypes. (CHAR and pointers)
1 parent 7405c20 commit cc868d4

2 files changed

Lines changed: 97 additions & 74 deletions

File tree

Lib/ctypes/wintypes.py

Lines changed: 95 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,50 @@
11
# The most useful windows datatypes
2-
from ctypes import *
2+
import ctypes
33

4-
BYTE = c_byte
5-
WORD = c_ushort
6-
DWORD = c_ulong
4+
BYTE = ctypes.c_byte
5+
WORD = ctypes.c_ushort
6+
DWORD = ctypes.c_ulong
77

8-
WCHAR = c_wchar
9-
UINT = c_uint
10-
INT = c_int
8+
#UCHAR = ctypes.c_uchar
9+
CHAR = ctypes.c_char
10+
WCHAR = ctypes.c_wchar
11+
UINT = ctypes.c_uint
12+
INT = ctypes.c_int
1113

12-
DOUBLE = c_double
13-
FLOAT = c_float
14+
DOUBLE = ctypes.c_double
15+
FLOAT = ctypes.c_float
1416

1517
BOOLEAN = BYTE
16-
BOOL = c_long
18+
BOOL = ctypes.c_long
1719

18-
from ctypes import _SimpleCData
19-
class VARIANT_BOOL(_SimpleCData):
20+
class VARIANT_BOOL(ctypes._SimpleCData):
2021
_type_ = "v"
2122
def __repr__(self):
2223
return "%s(%r)" % (self.__class__.__name__, self.value)
2324

24-
ULONG = c_ulong
25-
LONG = c_long
25+
ULONG = ctypes.c_ulong
26+
LONG = ctypes.c_long
2627

27-
USHORT = c_ushort
28-
SHORT = c_short
28+
USHORT = ctypes.c_ushort
29+
SHORT = ctypes.c_short
2930

3031
# in the windows header files, these are structures.
31-
_LARGE_INTEGER = LARGE_INTEGER = c_longlong
32-
_ULARGE_INTEGER = ULARGE_INTEGER = c_ulonglong
32+
_LARGE_INTEGER = LARGE_INTEGER = ctypes.c_longlong
33+
_ULARGE_INTEGER = ULARGE_INTEGER = ctypes.c_ulonglong
3334

34-
LPCOLESTR = LPOLESTR = OLESTR = c_wchar_p
35-
LPCWSTR = LPWSTR = c_wchar_p
36-
LPCSTR = LPSTR = c_char_p
37-
LPCVOID = LPVOID = c_void_p
35+
LPCOLESTR = LPOLESTR = OLESTR = ctypes.c_wchar_p
36+
LPCWSTR = LPWSTR = ctypes.c_wchar_p
37+
LPCSTR = LPSTR = ctypes.c_char_p
38+
LPCVOID = LPVOID = ctypes.c_void_p
3839

3940
# WPARAM is defined as UINT_PTR (unsigned type)
4041
# LPARAM is defined as LONG_PTR (signed type)
41-
if sizeof(c_long) == sizeof(c_void_p):
42-
WPARAM = c_ulong
43-
LPARAM = c_long
44-
elif sizeof(c_longlong) == sizeof(c_void_p):
45-
WPARAM = c_ulonglong
46-
LPARAM = c_longlong
42+
if ctypes.sizeof(ctypes.c_long) == ctypes.sizeof(ctypes.c_void_p):
43+
WPARAM = ctypes.c_ulong
44+
LPARAM = ctypes.c_long
45+
elif ctypes.sizeof(ctypes.c_longlong) == ctypes.sizeof(ctypes.c_void_p):
46+
WPARAM = ctypes.c_ulonglong
47+
LPARAM = ctypes.c_longlong
4748

4849
ATOM = WORD
4950
LANGID = WORD
@@ -56,7 +57,7 @@ def __repr__(self):
5657

5758
################################################################
5859
# HANDLE types
59-
HANDLE = c_void_p # in the header files: void *
60+
HANDLE = ctypes.c_void_p # in the header files: void *
6061

6162
HACCEL = HANDLE
6263
HBITMAP = HANDLE
@@ -93,53 +94,53 @@ def __repr__(self):
9394
################################################################
9495
# Some important structure definitions
9596

96-
class RECT(Structure):
97-
_fields_ = [("left", c_long),
98-
("top", c_long),
99-
("right", c_long),
100-
("bottom", c_long)]
97+
class RECT(ctypes.Structure):
98+
_fields_ = [("left", LONG),
99+
("top", LONG),
100+
("right", LONG),
101+
("bottom", LONG)]
101102
tagRECT = _RECTL = RECTL = RECT
102103

103-
class _SMALL_RECT(Structure):
104-
_fields_ = [('Left', c_short),
105-
('Top', c_short),
106-
('Right', c_short),
107-
('Bottom', c_short)]
104+
class _SMALL_RECT(ctypes.Structure):
105+
_fields_ = [('Left', SHORT),
106+
('Top', SHORT),
107+
('Right', SHORT),
108+
('Bottom', SHORT)]
108109
SMALL_RECT = _SMALL_RECT
109110

110-
class _COORD(Structure):
111-
_fields_ = [('X', c_short),
112-
('Y', c_short)]
111+
class _COORD(ctypes.Structure):
112+
_fields_ = [('X', SHORT),
113+
('Y', SHORT)]
113114

114-
class POINT(Structure):
115-
_fields_ = [("x", c_long),
116-
("y", c_long)]
115+
class POINT(ctypes.Structure):
116+
_fields_ = [("x", LONG),
117+
("y", LONG)]
117118
tagPOINT = _POINTL = POINTL = POINT
118119

119-
class SIZE(Structure):
120-
_fields_ = [("cx", c_long),
121-
("cy", c_long)]
120+
class SIZE(ctypes.Structure):
121+
_fields_ = [("cx", LONG),
122+
("cy", LONG)]
122123
tagSIZE = SIZEL = SIZE
123124

124125
def RGB(red, green, blue):
125126
return red + (green << 8) + (blue << 16)
126127

127-
class FILETIME(Structure):
128+
class FILETIME(ctypes.Structure):
128129
_fields_ = [("dwLowDateTime", DWORD),
129130
("dwHighDateTime", DWORD)]
130131
_FILETIME = FILETIME
131132

132-
class MSG(Structure):
133+
class MSG(ctypes.Structure):
133134
_fields_ = [("hWnd", HWND),
134-
("message", c_uint),
135+
("message", UINT),
135136
("wParam", WPARAM),
136137
("lParam", LPARAM),
137138
("time", DWORD),
138139
("pt", POINT)]
139140
tagMSG = MSG
140141
MAX_PATH = 260
141142

142-
class WIN32_FIND_DATAA(Structure):
143+
class WIN32_FIND_DATAA(ctypes.Structure):
143144
_fields_ = [("dwFileAttributes", DWORD),
144145
("ftCreationTime", FILETIME),
145146
("ftLastAccessTime", FILETIME),
@@ -148,10 +149,10 @@ class WIN32_FIND_DATAA(Structure):
148149
("nFileSizeLow", DWORD),
149150
("dwReserved0", DWORD),
150151
("dwReserved1", DWORD),
151-
("cFileName", c_char * MAX_PATH),
152-
("cAlternateFileName", c_char * 14)]
152+
("cFileName", CHAR * MAX_PATH),
153+
("cAlternateFileName", CHAR * 14)]
153154

154-
class WIN32_FIND_DATAW(Structure):
155+
class WIN32_FIND_DATAW(ctypes.Structure):
155156
_fields_ = [("dwFileAttributes", DWORD),
156157
("ftCreationTime", FILETIME),
157158
("ftLastAccessTime", FILETIME),
@@ -160,22 +161,42 @@ class WIN32_FIND_DATAW(Structure):
160161
("nFileSizeLow", DWORD),
161162
("dwReserved0", DWORD),
162163
("dwReserved1", DWORD),
163-
("cFileName", c_wchar * MAX_PATH),
164-
("cAlternateFileName", c_wchar * 14)]
165-
166-
__all__ = ['ATOM', 'BOOL', 'BOOLEAN', 'BYTE', 'COLORREF', 'DOUBLE', 'DWORD',
167-
'FILETIME', 'FLOAT', 'HACCEL', 'HANDLE', 'HBITMAP', 'HBRUSH',
168-
'HCOLORSPACE', 'HDC', 'HDESK', 'HDWP', 'HENHMETAFILE', 'HFONT',
169-
'HGDIOBJ', 'HGLOBAL', 'HHOOK', 'HICON', 'HINSTANCE', 'HKEY',
170-
'HKL', 'HLOCAL', 'HMENU', 'HMETAFILE', 'HMODULE', 'HMONITOR',
171-
'HPALETTE', 'HPEN', 'HRGN', 'HRSRC', 'HSTR', 'HTASK', 'HWINSTA',
172-
'HWND', 'INT', 'LANGID', 'LARGE_INTEGER', 'LCID', 'LCTYPE',
173-
'LGRPID', 'LONG', 'LPARAM', 'LPCOLESTR', 'LPCSTR', 'LPCVOID',
174-
'LPCWSTR', 'LPOLESTR', 'LPSTR', 'LPVOID', 'LPWSTR', 'MAX_PATH',
175-
'MSG', 'OLESTR', 'POINT', 'POINTL', 'RECT', 'RECTL', 'RGB',
176-
'SC_HANDLE', 'SERVICE_STATUS_HANDLE', 'SHORT', 'SIZE', 'SIZEL',
177-
'SMALL_RECT', 'UINT', 'ULARGE_INTEGER', 'ULONG', 'USHORT',
178-
'VARIANT_BOOL', 'WCHAR', 'WIN32_FIND_DATAA', 'WIN32_FIND_DATAW',
179-
'WORD', 'WPARAM', '_COORD', '_FILETIME', '_LARGE_INTEGER',
180-
'_POINTL', '_RECTL', '_SMALL_RECT', '_ULARGE_INTEGER', 'tagMSG',
181-
'tagPOINT', 'tagRECT', 'tagSIZE']
164+
("cFileName", WCHAR * MAX_PATH),
165+
("cAlternateFileName", WCHAR * 14)]
166+
167+
################################################################
168+
# Pointer types
169+
170+
LPBOOL = PBOOL = ctypes.POINTER(BOOL)
171+
PBOOLEAN = ctypes.POINTER(BOOLEAN)
172+
LPBYTE = PBYTE = ctypes.POINTER(BYTE)
173+
PCHAR = ctypes.POINTER(CHAR)
174+
LPCOLORREF = ctypes.POINTER(COLORREF)
175+
LPDWORD = PDWORD = ctypes.POINTER(DWORD)
176+
LPFILETIME = PFILETIME = ctypes.POINTER(FILETIME)
177+
PFLOAT = ctypes.POINTER(FLOAT)
178+
LPHANDLE = PHANDLE = ctypes.POINTER(HANDLE)
179+
PHKEY = ctypes.POINTER(HKEY)
180+
LPHKL = ctypes.POINTER(HKL)
181+
LPINT = PINT = ctypes.POINTER(INT)
182+
PLARGE_INTEGER = ctypes.POINTER(LARGE_INTEGER)
183+
PLCID = ctypes.POINTER(LCID)
184+
LPLONG = PLONG = ctypes.POINTER(LONG)
185+
LPMSG = PMSG = ctypes.POINTER(MSG)
186+
LPPOINT = PPOINT = ctypes.POINTER(POINT)
187+
PPOINTL = ctypes.POINTER(POINTL)
188+
LPRECT = PRECT = ctypes.POINTER(RECT)
189+
LPRECTL = PRECTL = ctypes.POINTER(RECTL)
190+
LPSC_HANDLE = ctypes.POINTER(SC_HANDLE)
191+
PSHORT = ctypes.POINTER(SHORT)
192+
LPSIZE = PSIZE = ctypes.POINTER(SIZE)
193+
LPSIZEL = PSIZEL = ctypes.POINTER(SIZEL)
194+
PSMALL_RECT = ctypes.POINTER(SMALL_RECT)
195+
LPUINT = PUINT = ctypes.POINTER(UINT)
196+
PULARGE_INTEGER = ctypes.POINTER(ULARGE_INTEGER)
197+
PULONG = ctypes.POINTER(ULONG)
198+
PUSHORT = ctypes.POINTER(USHORT)
199+
PWCHAR = ctypes.POINTER(WCHAR)
200+
LPWIN32_FIND_DATAA = PWIN32_FIND_DATAA = ctypes.POINTER(WIN32_FIND_DATAA)
201+
LPWIN32_FIND_DATAW = PWIN32_FIND_DATAW = ctypes.POINTER(WIN32_FIND_DATAW)
202+
LPWORD = PWORD = ctypes.POINTER(WORD)

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ Core and Builtins
7474
Library
7575
-------
7676

77+
- Issue #3612: Added new types to ctypes.wintypes. (CHAR and pointers)
78+
7779
- Issue #9950: Fix socket.sendall() crash or misbehaviour when a signal is
7880
received. Now sendall() properly calls signal handlers if necessary,
7981
and retries sending if these returned successfully, including on sockets

0 commit comments

Comments
 (0)