-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpreproc.py
More file actions
190 lines (142 loc) · 4.6 KB
/
Copy pathcpreproc.py
File metadata and controls
190 lines (142 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
__all__ = [
'define',
'ifdef',
'ifndef',
'getdef',
'undef',
'error',
'resetdef',
'defined',
'get_defines',
'get_version',
'timestamp',
'PreprocessorError',
'pragma_pack_pop',
'pragma_pack_push',
'pragma_pack',
'pragma_once',
'line', 'file'
]
class PreprocessorError(OSError): ...
from ctypes import c_uint64, c_void_p, c_int, windll, sizeof, Structure
from queue import Queue
_defines = {}
class _CPreprocStateType:
__slots__ = ('_pack_stack', '_cur_pack', '_internal_cached_UNICODE')
# friend module defbase
def __init__(self):
self._cur_pack = sizeof(c_void_p)
self._pack_stack = Queue(-1)
self._internal_cached_UNICODE = False
_pack_stack: Queue
_cur_pack: int
_internal_cached_UNICODE: bool # defbase.unicode(...) optimization
_CPreprocState = _CPreprocStateType()
def define(k: str, e=None):
if k == 'UNICODE':
_CPreprocState._internal_cached_UNICODE = k
if ifndef(k):
_defines[k] = e
def ifdef(k: str) -> bool:
return k in _defines
def ifndef(k: str) -> bool:
return k not in _defines
def defined(k: str) -> bool: ...
defined = ifdef
def getdef(k: str):
return _defines.get(k)
def undef(k: str):
if ifdef(k):
if k == 'UNICODE':
_CPreprocState._internal_cached_UNICODE = False
del _defines[k]
def resetdef():
global _defines
_defines = {}
def get_defines():
return _defines.copy()
def error(message):
raise PreprocessorError(message)
def pragma_pack_pop():
if _CPreprocState._pack_stack.empty():
raise PreprocessorError("Pack stack underflow.")
_CPreprocState._cur_pack = _CPreprocState._pack_stack.get()
def pragma_push(n: int = 0):
if not n:
n = _CPreprocState._cur_pack
_CPreprocState._pack_stack.put(n)
def pragma_pack(n: int):
if n <= 0:
raise PreprocessorError("Pack is lesser or equals 0.")
_CPreprocState._cur_pack = n
def get_version():
return getdef("_WINVER")
from time import strftime, localtime
import typing as t
import sys
def timestamp():
return strftime('%a %d %b %H:%M:%S %Y', localtime())
if hasattr(sys, '_getframe'):
from sys import _getframe as getframe
def pragma_once(k: t.Optional[str] = None):
if k is None:
k = f'__{getframe().f_back.f_code.co_filename.replace(".", "_")}__'
if ifndef(k):
define(k)
return True
return False
def line():
return getframe().f_back.f_lineno
def file():
return getframe().f_back.f_code.co_filename
else:
def pragma_once(k: t.Optional[str] = None):
if k is None:
raise PreprocessorError('Key cannot be None. ("sys" module doesn\'t have attribute "_getframe")')
if ifndef(k):
define(k)
return True
return False
def line():
raise RuntimeError('Method not implemented. "sys" module doesn\'t have attribute "_getframe".')
def file():
raise RuntimeError('Method not implemented. "sys" module doesn\'t have attribute "_getframe".')
from ctypes.wintypes import WORD, BYTE, DWORD
# define the default definitions
# CRITICAL SECTION START ***
def _cpreproc_init():
import platform
define('UNICODE')
# for architecture specifying
if sizeof(c_void_p) == 4:
define("_WIN32")
DWORD_PTR = c_int
else:
define("_WIN64")
define("_M_X64", 100)
DWORD_PTR = c_uint64
# predefined macros
arch = platform.machine().lower()
if arch in ('arm64', 'aarch64'):
define("_M_ARM64", 1)
elif arch in ('arm', 'armv7l', 'armv8l'):
define("_M_ARM", 7)
elif arch in ('x86_64', 'amd64'):
define("_M_AMD64")
elif arch in ('x86', 'i386', 'i686'):
define("_M_IX86")
define("_MSC_VER", 1700)
# for guiddef.py
define("INITGUID")
# for version indicating
MAKEWORD = lambda a, b: (WORD((BYTE(DWORD_PTR(a).value & 0xFF).value) | (WORD(BYTE(DWORD_PTR(b).value & 0xFF).value)).value << 8)).value
LOWORD = lambda l: (WORD((DWORD_PTR(l)).value & 0xFFFF)).value
LOBYTE = lambda w: (BYTE((DWORD_PTR(w)).value & 0xFF)).value
HIBYTE = lambda w: (WORD((DWORD_PTR(w).value >> 8) & 0xFF)).value
windll.kernel32.GetVersion.argtypes = []
windll.kernel32.GetVersion.restype = DWORD
dwVersion = windll.kernel32.GetVersion()
define("_WINVER", MAKEWORD(HIBYTE(LOWORD(dwVersion)), LOBYTE(LOWORD(dwVersion))))
_cpreproc_init()
del _cpreproc_init
# CRITICAL SECTION END ***