makegwparse.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. """Utilities for makegw - Parse a header file to build an interface
  2. This module contains the core code for parsing a header file describing a
  3. COM interface, and building it into an "Interface" structure.
  4. Each Interface has methods, and each method has arguments.
  5. Each argument knows how to use Py_BuildValue or Py_ParseTuple to
  6. exchange itself with Python.
  7. See the @win32com.makegw@ module for information in building a COM
  8. interface
  9. """
  10. import re
  11. import traceback
  12. class error_not_found(Exception):
  13. def __init__(self, msg="The requested item could not be found"):
  14. super(error_not_found, self).__init__(msg)
  15. class error_not_supported(Exception):
  16. def __init__(self, msg="The required functionality is not supported"):
  17. super(error_not_supported, self).__init__(msg)
  18. VERBOSE=0
  19. DEBUG=0
  20. ## NOTE : For interfaces as params to work correctly, you must
  21. ## make sure any PythonCOM extensions which expose the interface are loaded
  22. ## before generating.
  23. class ArgFormatter:
  24. """An instance for a specific type of argument. Knows how to convert itself"""
  25. def __init__(self, arg, builtinIndirection, declaredIndirection = 0):
  26. #print 'init:', arg.name, builtinIndirection, declaredIndirection, arg.indirectionLevel
  27. self.arg = arg
  28. self.builtinIndirection = builtinIndirection
  29. self.declaredIndirection = declaredIndirection
  30. self.gatewayMode = 0
  31. def _IndirectPrefix(self, indirectionFrom, indirectionTo):
  32. """Given the indirection level I was declared at (0=Normal, 1=*, 2=**)
  33. return a string prefix so I can pass to a function with the
  34. required indirection (where the default is the indirection of the method's param.
  35. eg, assuming my arg has indirection level of 2, if this function was passed 1
  36. it would return "&", so that a variable declared with indirection of 1
  37. can be prefixed with this to turn it into the indirection level required of 2
  38. """
  39. dif = indirectionFrom - indirectionTo
  40. if dif==0:
  41. return ""
  42. elif dif==-1:
  43. return "&"
  44. elif dif==1:
  45. return "*"
  46. else:
  47. return "?? (%d)" % (dif,)
  48. raise error_not_supported("Can't indirect this far - please fix me :-)")
  49. def GetIndirectedArgName(self, indirectFrom, indirectionTo):
  50. #print 'get:',self.arg.name, indirectFrom,self._GetDeclaredIndirection() + self.builtinIndirection, indirectionTo, self.arg.indirectionLevel
  51. if indirectFrom is None:
  52. ### ACK! this does not account for [in][out] variables.
  53. ### when this method is called, we need to know which
  54. indirectFrom = self._GetDeclaredIndirection() + self.builtinIndirection
  55. return self._IndirectPrefix(indirectFrom, indirectionTo) + self.arg.name
  56. def GetBuildValueArg(self):
  57. "Get the argument to be passes to Py_BuildValue"
  58. return self.arg.name
  59. def GetParseTupleArg(self):
  60. "Get the argument to be passed to PyArg_ParseTuple"
  61. if self.gatewayMode:
  62. # use whatever they were declared with
  63. return self.GetIndirectedArgName(None, 1)
  64. # local declarations have just their builtin indirection
  65. return self.GetIndirectedArgName(self.builtinIndirection, 1)
  66. def GetInterfaceCppObjectInfo(self):
  67. """Provide information about the C++ object used.
  68. Simple variables (such as integers) can declare their type (eg an integer)
  69. and use it as the target of both PyArg_ParseTuple and the COM function itself.
  70. More complex types require a PyObject * declared as the target of PyArg_ParseTuple,
  71. then some conversion routine to the C++ object which is actually passed to COM.
  72. This method provides the name, and optionally the type of that C++ variable.
  73. If the type if provided, the caller will likely generate a variable declaration.
  74. The name must always be returned.
  75. Result is a tuple of (variableName, [DeclareType|None|""])
  76. """
  77. # the first return element is the variable to be passed as
  78. # an argument to an interface method. the variable was
  79. # declared with only its builtin indirection level. when
  80. # we pass it, we'll need to pass in whatever amount of
  81. # indirection was applied (plus the builtin amount)
  82. # the second return element is the variable declaration; it
  83. # should simply be builtin indirection
  84. return self.GetIndirectedArgName(self.builtinIndirection, self.arg.indirectionLevel + self.builtinIndirection), \
  85. "%s %s" % (self.GetUnconstType(), self.arg.name)
  86. def GetInterfaceArgCleanup(self):
  87. "Return cleanup code for C++ args passed to the interface method."
  88. if DEBUG:
  89. return "/* GetInterfaceArgCleanup output goes here: %s */\n" % self.arg.name
  90. else:
  91. return ""
  92. def GetInterfaceArgCleanupGIL(self):
  93. """Return cleanup code for C++ args passed to the interface
  94. method that must be executed with the GIL held"""
  95. if DEBUG:
  96. return "/* GetInterfaceArgCleanup (GIL held) output goes here: %s */\n" % self.arg.name
  97. else:
  98. return ""
  99. def GetUnconstType(self):
  100. return self.arg.unc_type
  101. def SetGatewayMode(self):
  102. self.gatewayMode = 1
  103. def _GetDeclaredIndirection(self):
  104. return self.arg.indirectionLevel
  105. print('declared:', self.arg.name, self.gatewayMode)
  106. if self.gatewayMode:
  107. return self.arg.indirectionLevel
  108. else:
  109. return self.declaredIndirection
  110. def DeclareParseArgTupleInputConverter(self):
  111. "Declare the variable used as the PyArg_ParseTuple param for a gateway"
  112. # Only declare it??
  113. #if self.arg.indirectionLevel==0:
  114. # return "\t%s %s;\n" % (self.arg.type, self.arg.name)
  115. #else:
  116. if DEBUG:
  117. return "/* Declare ParseArgTupleInputConverter goes here: %s */\n" % self.arg.name
  118. else:
  119. return ""
  120. def GetParsePostCode(self):
  121. "Get a string of C++ code to be executed after (ie, to finalise) the PyArg_ParseTuple conversion"
  122. if DEBUG:
  123. return "/* GetParsePostCode code goes here: %s */\n" % self.arg.name
  124. else:
  125. return ""
  126. def GetBuildForInterfacePreCode(self):
  127. "Get a string of C++ code to be executed before (ie, to initialise) the Py_BuildValue conversion for Interfaces"
  128. if DEBUG:
  129. return "/* GetBuildForInterfacePreCode goes here: %s */\n" % self.arg.name
  130. else:
  131. return ""
  132. def GetBuildForGatewayPreCode(self):
  133. "Get a string of C++ code to be executed before (ie, to initialise) the Py_BuildValue conversion for Gateways"
  134. s = self.GetBuildForInterfacePreCode() # Usually the same
  135. if DEBUG:
  136. if s[:4] == "/* G":
  137. s = "/* GetBuildForGatewayPreCode goes here: %s */\n" % self.arg.name
  138. return s
  139. def GetBuildForInterfacePostCode(self):
  140. "Get a string of C++ code to be executed after (ie, to finalise) the Py_BuildValue conversion for Interfaces"
  141. if DEBUG:
  142. return "/* GetBuildForInterfacePostCode goes here: %s */\n" % self.arg.name
  143. return ""
  144. def GetBuildForGatewayPostCode(self):
  145. "Get a string of C++ code to be executed after (ie, to finalise) the Py_BuildValue conversion for Gateways"
  146. s = self.GetBuildForInterfacePostCode() # Usually the same
  147. if DEBUG:
  148. if s[:4] == "/* G":
  149. s = "/* GetBuildForGatewayPostCode goes here: %s */\n" % self.arg.name
  150. return s
  151. def GetAutoduckString(self):
  152. return '// @pyparm %s|%s||Description for %s' % (self._GetPythonTypeDesc(), self.arg.name, self.arg.name)
  153. def _GetPythonTypeDesc(self):
  154. "Returns a string with the description of the type. Used for doco purposes"
  155. return None
  156. def NeedUSES_CONVERSION(self):
  157. "Determines if this arg forces a USES_CONVERSION macro"
  158. return 0
  159. # Special formatter for floats since they're smaller than Python floats.
  160. class ArgFormatterFloat(ArgFormatter):
  161. def GetFormatChar(self):
  162. return "f"
  163. def DeclareParseArgTupleInputConverter(self):
  164. # Declare a double variable
  165. return "\tdouble dbl%s;\n" % self.arg.name
  166. def GetParseTupleArg(self):
  167. return "&dbl" + self.arg.name
  168. def _GetPythonTypeDesc(self):
  169. return "float"
  170. def GetBuildValueArg(self):
  171. return "&dbl" + self.arg.name
  172. def GetBuildForInterfacePreCode(self):
  173. return "\tdbl" + self.arg.name + " = " + self.arg.name + ";\n"
  174. def GetBuildForGatewayPreCode(self):
  175. return "\tdbl%s = " % self.arg.name + self._IndirectPrefix( \
  176. self._GetDeclaredIndirection(),
  177. 0) + self.arg.name + ";\n"
  178. def GetParsePostCode(self):
  179. s = "\t"
  180. if self.gatewayMode:
  181. s = s + self._IndirectPrefix(
  182. self._GetDeclaredIndirection(),
  183. 0)
  184. s = s + self.arg.name
  185. s = s + " = (float)dbl%s;\n" % self.arg.name
  186. return s
  187. # Special formatter for Shorts because they're
  188. # a different size than Python ints!
  189. class ArgFormatterShort(ArgFormatter):
  190. def GetFormatChar(self):
  191. return "i"
  192. def DeclareParseArgTupleInputConverter(self):
  193. # Declare a double variable
  194. return "\tINT i%s;\n" % self.arg.name
  195. def GetParseTupleArg(self):
  196. return "&i" + self.arg.name
  197. def _GetPythonTypeDesc(self):
  198. return "int"
  199. def GetBuildValueArg(self):
  200. return "&i" + self.arg.name
  201. def GetBuildForInterfacePreCode(self):
  202. return "\ti" + self.arg.name + " = " + self.arg.name + ";\n"
  203. def GetBuildForGatewayPreCode(self):
  204. return "\ti%s = " % self.arg.name + self._IndirectPrefix( \
  205. self._GetDeclaredIndirection(),
  206. 0) + self.arg.name + ";\n"
  207. def GetParsePostCode(self):
  208. s = "\t"
  209. if self.gatewayMode:
  210. s = s + self._IndirectPrefix(
  211. self._GetDeclaredIndirection(),
  212. 0)
  213. s = s + self.arg.name
  214. s = s + " = i%s;\n" % self.arg.name
  215. return s
  216. # for types which are 64bits on AMD64 - eg, HWND
  217. class ArgFormatterLONG_PTR(ArgFormatter):
  218. def GetFormatChar(self):
  219. return "O"
  220. def DeclareParseArgTupleInputConverter(self):
  221. # Declare a PyObject variable
  222. return "\tPyObject *ob%s;\n" % self.arg.name
  223. def GetParseTupleArg(self):
  224. return "&ob"+self.arg.name
  225. def _GetPythonTypeDesc(self):
  226. return "int/long"
  227. def GetBuildValueArg(self):
  228. return "ob" + self.arg.name
  229. def GetBuildForInterfacePostCode(self):
  230. return "\tPy_XDECREF(ob%s);\n" % self.arg.name
  231. def DeclareParseArgTupleInputConverter(self):
  232. # Declare a PyObject variable
  233. return "\tPyObject *ob%s;\n" % self.arg.name
  234. def GetParsePostCode(self):
  235. return "\tif (bPythonIsHappy && !PyWinLong_AsULONG_PTR(ob%s, (ULONG_PTR *)%s)) bPythonIsHappy = FALSE;\n" % (self.arg.name, self.GetIndirectedArgName(None, 2))
  236. def GetBuildForInterfacePreCode(self):
  237. notdirected = self.GetIndirectedArgName(None, 1)
  238. return "\tob%s = PyWinObject_FromULONG_PTR(%s);\n" % \
  239. (self.arg.name, notdirected)
  240. def GetBuildForGatewayPostCode(self):
  241. return "\tPy_XDECREF(ob%s);\n" % self.arg.name
  242. class ArgFormatterPythonCOM(ArgFormatter):
  243. """An arg formatter for types exposed in the PythonCOM module"""
  244. def GetFormatChar(self):
  245. return "O"
  246. #def GetInterfaceCppObjectInfo(self):
  247. # return ArgFormatter.GetInterfaceCppObjectInfo(self)[0], \
  248. # "%s %s%s" % (self.arg.unc_type, "*" * self._GetDeclaredIndirection(), self.arg.name)
  249. def DeclareParseArgTupleInputConverter(self):
  250. # Declare a PyObject variable
  251. return "\tPyObject *ob%s;\n" % self.arg.name
  252. def GetParseTupleArg(self):
  253. return "&ob"+self.arg.name
  254. def _GetPythonTypeDesc(self):
  255. return "<o Py%s>" % self.arg.type
  256. def GetBuildValueArg(self):
  257. return "ob" + self.arg.name
  258. def GetBuildForInterfacePostCode(self):
  259. return "\tPy_XDECREF(ob%s);\n" % self.arg.name
  260. def DeclareParseArgTupleInputConverter(self):
  261. # Declare a PyObject variable
  262. return "\tPyObject *ob%s;\n" % self.arg.name
  263. class ArgFormatterBSTR(ArgFormatterPythonCOM):
  264. def _GetPythonTypeDesc(self):
  265. return "<o unicode>"
  266. def GetParsePostCode(self):
  267. return "\tif (bPythonIsHappy && !PyWinObject_AsBstr(ob%s, %s)) bPythonIsHappy = FALSE;\n" % (self.arg.name, self.GetIndirectedArgName(None, 2))
  268. def GetBuildForInterfacePreCode(self):
  269. notdirected = self.GetIndirectedArgName(None, 1)
  270. return "\tob%s = MakeBstrToObj(%s);\n" % \
  271. (self.arg.name, notdirected)
  272. def GetBuildForInterfacePostCode(self):
  273. return "\tSysFreeString(%s);\n" % (self.arg.name,) + \
  274. ArgFormatterPythonCOM.GetBuildForInterfacePostCode(self)
  275. def GetBuildForGatewayPostCode(self):
  276. return "\tPy_XDECREF(ob%s);\n" % self.arg.name
  277. class ArgFormatterOLECHAR(ArgFormatterPythonCOM):
  278. def _GetPythonTypeDesc(self):
  279. return "<o unicode>"
  280. def GetUnconstType(self):
  281. if self.arg.type[:3]=="LPC":
  282. return self.arg.type[:2] + self.arg.type[3:]
  283. else:
  284. return self.arg.unc_type
  285. def GetParsePostCode(self):
  286. return "\tif (bPythonIsHappy && !PyWinObject_AsBstr(ob%s, %s)) bPythonIsHappy = FALSE;\n" % (self.arg.name, self.GetIndirectedArgName(None, 2))
  287. def GetInterfaceArgCleanup(self):
  288. return "\tSysFreeString(%s);\n" % self.GetIndirectedArgName(None, 1)
  289. def GetBuildForInterfacePreCode(self):
  290. # the variable was declared with just its builtin indirection
  291. notdirected = self.GetIndirectedArgName(self.builtinIndirection, 1)
  292. return "\tob%s = MakeOLECHARToObj(%s);\n" % \
  293. (self.arg.name, notdirected)
  294. def GetBuildForInterfacePostCode(self):
  295. # memory returned into an OLECHAR should be freed
  296. return "\tCoTaskMemFree(%s);\n" % (self.arg.name,) + \
  297. ArgFormatterPythonCOM.GetBuildForInterfacePostCode(self)
  298. def GetBuildForGatewayPostCode(self):
  299. return "\tPy_XDECREF(ob%s);\n" % self.arg.name
  300. class ArgFormatterTCHAR(ArgFormatterPythonCOM):
  301. def _GetPythonTypeDesc(self):
  302. return "string/<o unicode>"
  303. def GetUnconstType(self):
  304. if self.arg.type[:3]=="LPC":
  305. return self.arg.type[:2] + self.arg.type[3:]
  306. else:
  307. return self.arg.unc_type
  308. def GetParsePostCode(self):
  309. return "\tif (bPythonIsHappy && !PyWinObject_AsTCHAR(ob%s, %s)) bPythonIsHappy = FALSE;\n" % (self.arg.name, self.GetIndirectedArgName(None, 2))
  310. def GetInterfaceArgCleanup(self):
  311. return "\tPyWinObject_FreeTCHAR(%s);\n" % self.GetIndirectedArgName(None, 1)
  312. def GetBuildForInterfacePreCode(self):
  313. # the variable was declared with just its builtin indirection
  314. notdirected = self.GetIndirectedArgName(self.builtinIndirection, 1)
  315. return "\tob%s = PyWinObject_FromTCHAR(%s);\n" % \
  316. (self.arg.name, notdirected)
  317. def GetBuildForInterfacePostCode(self):
  318. return "// ??? - TCHAR post code\n"
  319. def GetBuildForGatewayPostCode(self):
  320. return "\tPy_XDECREF(ob%s);\n" % self.arg.name
  321. class ArgFormatterIID(ArgFormatterPythonCOM):
  322. def _GetPythonTypeDesc(self):
  323. return "<o PyIID>"
  324. def GetParsePostCode(self):
  325. return "\tif (!PyWinObject_AsIID(ob%s, &%s)) bPythonIsHappy = FALSE;\n" % (self.arg.name, self.arg.name)
  326. def GetBuildForInterfacePreCode(self):
  327. # notdirected = self.GetIndirectedArgName(self.arg.indirectionLevel, 0)
  328. notdirected = self.GetIndirectedArgName(None, 0)
  329. return "\tob%s = PyWinObject_FromIID(%s);\n" % (self.arg.name, notdirected)
  330. def GetInterfaceCppObjectInfo(self):
  331. return self.arg.name, "IID %s" % (self.arg.name)
  332. class ArgFormatterTime(ArgFormatterPythonCOM):
  333. def __init__(self, arg, builtinIndirection, declaredIndirection = 0):
  334. # we don't want to declare LPSYSTEMTIME / LPFILETIME objects
  335. if arg.indirectionLevel == 0 and arg.unc_type[:2] == "LP":
  336. arg.unc_type = arg.unc_type[2:]
  337. # reduce the builtin and increment the declaration
  338. arg.indirectionLevel = arg.indirectionLevel + 1
  339. builtinIndirection = 0
  340. ArgFormatterPythonCOM.__init__(self, arg, builtinIndirection, declaredIndirection)
  341. def _GetPythonTypeDesc(self):
  342. return "<o PyDateTime>"
  343. def GetParsePostCode(self):
  344. # variable was declared with only the builtinIndirection
  345. ### NOTE: this is an [in] ... so use only builtin
  346. return '\tif (!PyTime_Check(ob%s)) {\n\t\tPyErr_SetString(PyExc_TypeError, "The argument must be a PyTime object");\n\t\tbPythonIsHappy = FALSE;\n\t}\n\tif (!((PyTime *)ob%s)->GetTime(%s)) bPythonIsHappy = FALSE;\n' % (self.arg.name, self.arg.name, self.GetIndirectedArgName(self.builtinIndirection, 1))
  347. def GetBuildForInterfacePreCode(self):
  348. ### use just the builtinIndirection again...
  349. notdirected = self.GetIndirectedArgName(self.builtinIndirection,0)
  350. return "\tob%s = new PyTime(%s);\n" % (self.arg.name, notdirected)
  351. def GetBuildForInterfacePostCode(self):
  352. ### hack to determine if we need to free stuff
  353. ret = ''
  354. if self.builtinIndirection + self.arg.indirectionLevel > 1:
  355. # memory returned into an OLECHAR should be freed
  356. ret = "\tCoTaskMemFree(%s);\n" % self.arg.name
  357. return ret + ArgFormatterPythonCOM.GetBuildForInterfacePostCode(self)
  358. class ArgFormatterSTATSTG(ArgFormatterPythonCOM):
  359. def _GetPythonTypeDesc(self):
  360. return "<o STATSTG>"
  361. def GetParsePostCode(self):
  362. return '\tif (!PyCom_PyObjectAsSTATSTG(ob%s, %s, 0/*flags*/)) bPythonIsHappy = FALSE;\n' % (self.arg.name, self.GetIndirectedArgName(None, 1))
  363. def GetBuildForInterfacePreCode(self):
  364. notdirected = self.GetIndirectedArgName(None, 1)
  365. return "\tob%s = PyCom_PyObjectFromSTATSTG(%s);\n\t// STATSTG doco says our responsibility to free\n\tif ((%s).pwcsName) CoTaskMemFree((%s).pwcsName);\n" % (self.arg.name, self.GetIndirectedArgName(None, 1),notdirected,notdirected)
  366. class ArgFormatterGeneric(ArgFormatterPythonCOM):
  367. def _GetPythonTypeDesc(self):
  368. return "<o %s>" % self.arg.type
  369. def GetParsePostCode(self):
  370. return '\tif (!PyObject_As%s(ob%s, &%s) bPythonIsHappy = FALSE;\n' % (self.arg.type, self.arg.name, self.GetIndirectedArgName(None, 1))
  371. def GetInterfaceArgCleanup(self):
  372. return '\tPyObject_Free%s(%s);\n' % (self.arg.type, self.arg.name)
  373. def GetBuildForInterfacePreCode(self):
  374. notdirected = self.GetIndirectedArgName(None, 1)
  375. return "\tob%s = PyObject_From%s(%s);\n" % (self.arg.name, self.arg.type, self.GetIndirectedArgName(None, 1))
  376. class ArgFormatterIDLIST(ArgFormatterPythonCOM):
  377. def _GetPythonTypeDesc(self):
  378. return "<o PyIDL>"
  379. def GetParsePostCode(self):
  380. return '\tif (bPythonIsHappy && !PyObject_AsPIDL(ob%s, &%s)) bPythonIsHappy = FALSE;\n' % (self.arg.name, self.GetIndirectedArgName(None, 1))
  381. def GetInterfaceArgCleanup(self):
  382. return '\tPyObject_FreePIDL(%s);\n' % (self.arg.name,)
  383. def GetBuildForInterfacePreCode(self):
  384. notdirected = self.GetIndirectedArgName(None, 1)
  385. return "\tob%s = PyObject_FromPIDL(%s);\n" % (self.arg.name, self.GetIndirectedArgName(None, 1))
  386. class ArgFormatterHANDLE(ArgFormatterPythonCOM):
  387. def _GetPythonTypeDesc(self):
  388. return "<o PyHANDLE>"
  389. def GetParsePostCode(self):
  390. return '\tif (!PyWinObject_AsHANDLE(ob%s, &%s, FALSE) bPythonIsHappy = FALSE;\n' % (self.arg.name, self.GetIndirectedArgName(None, 1))
  391. def GetBuildForInterfacePreCode(self):
  392. notdirected = self.GetIndirectedArgName(None, 1)
  393. return "\tob%s = PyWinObject_FromHANDLE(%s);\n" % (self.arg.name, self.GetIndirectedArgName(None, 0))
  394. class ArgFormatterLARGE_INTEGER(ArgFormatterPythonCOM):
  395. def GetKeyName(self):
  396. return "LARGE_INTEGER"
  397. def _GetPythonTypeDesc(self):
  398. return "<o %s>" % self.GetKeyName()
  399. def GetParsePostCode(self):
  400. return '\tif (!PyWinObject_As%s(ob%s, %s)) bPythonIsHappy = FALSE;\n' % (self.GetKeyName(), self.arg.name, self.GetIndirectedArgName(None, 1))
  401. def GetBuildForInterfacePreCode(self):
  402. notdirected = self.GetIndirectedArgName(None, 0)
  403. return "\tob%s = PyWinObject_From%s(%s);\n" % (self.arg.name, self.GetKeyName(), notdirected)
  404. class ArgFormatterULARGE_INTEGER(ArgFormatterLARGE_INTEGER):
  405. def GetKeyName(self):
  406. return "ULARGE_INTEGER"
  407. class ArgFormatterInterface(ArgFormatterPythonCOM):
  408. def GetInterfaceCppObjectInfo(self):
  409. return self.GetIndirectedArgName(1, self.arg.indirectionLevel), \
  410. "%s * %s" % (self.GetUnconstType(), self.arg.name)
  411. def GetParsePostCode(self):
  412. # This gets called for out params in gateway mode
  413. if self.gatewayMode:
  414. sArg = self.GetIndirectedArgName(None, 2)
  415. else:
  416. # vs. in params for interface mode.
  417. sArg = self.GetIndirectedArgName(1, 2)
  418. return "\tif (bPythonIsHappy && !PyCom_InterfaceFromPyInstanceOrObject(ob%s, IID_%s, (void **)%s, TRUE /* bNoneOK */))\n\t\t bPythonIsHappy = FALSE;\n" % (self.arg.name, self.arg.type, sArg)
  419. def GetBuildForInterfacePreCode(self):
  420. return "\tob%s = PyCom_PyObjectFromIUnknown(%s, IID_%s, FALSE);\n" % (self.arg.name, self.arg.name, self.arg.type)
  421. def GetBuildForGatewayPreCode(self):
  422. sPrefix = self._IndirectPrefix(self._GetDeclaredIndirection(), 1)
  423. return "\tob%s = PyCom_PyObjectFromIUnknown(%s%s, IID_%s, TRUE);\n" % (self.arg.name, sPrefix, self.arg.name, self.arg.type)
  424. def GetInterfaceArgCleanup(self):
  425. return "\tif (%s) %s->Release();\n" % (self.arg.name, self.arg.name)
  426. class ArgFormatterVARIANT(ArgFormatterPythonCOM):
  427. def GetParsePostCode(self):
  428. return "\tif ( !PyCom_VariantFromPyObject(ob%s, %s) )\n\t\tbPythonIsHappy = FALSE;\n" % (self.arg.name, self.GetIndirectedArgName(None, 1))
  429. def GetBuildForGatewayPreCode(self):
  430. notdirected = self.GetIndirectedArgName(None, 1)
  431. return "\tob%s = PyCom_PyObjectFromVariant(%s);\n" % (self.arg.name, notdirected)
  432. def GetBuildForGatewayPostCode(self):
  433. return "\tPy_XDECREF(ob%s);\n" % self.arg.name
  434. # Key : , Python Type Description, ParseTuple format char
  435. ConvertSimpleTypes = {"BOOL":("BOOL", "int", "i"),
  436. "UINT":("UINT", "int", "i"),
  437. "BYTE": ("BYTE", "int", "i"),
  438. "INT": ("INT", "int", "i"),
  439. "DWORD": ("DWORD", "int", "l"),
  440. "HRESULT":("HRESULT", "int", "l"),
  441. "ULONG": ("ULONG", "int", "l"),
  442. "LONG": ("LONG", "int", "l"),
  443. "int": ("int", "int", "i"),
  444. "long": ("long", "int", "l"),
  445. "DISPID": ("DISPID", "long", "l"),
  446. "APPBREAKFLAGS": ("int", "int", "i"),
  447. "BREAKRESUMEACTION": ("int", "int", "i"),
  448. "ERRORRESUMEACTION": ("int", "int", "i"),
  449. "BREAKREASON": ("int", "int", "i"),
  450. "BREAKPOINT_STATE": ("int", "int", "i"),
  451. "BREAKRESUME_ACTION": ("int", "int", "i"),
  452. "SOURCE_TEXT_ATTR": ("int", "int", "i"),
  453. "TEXT_DOC_ATTR": ("int", "int", "i"),
  454. "QUERYOPTION": ("int", "int", "i"),
  455. "PARSEACTION": ("int", "int", "i"),
  456. }
  457. class ArgFormatterSimple(ArgFormatter):
  458. """An arg formatter for simple integer etc types"""
  459. def GetFormatChar(self):
  460. return ConvertSimpleTypes[self.arg.type][2]
  461. def _GetPythonTypeDesc(self):
  462. return ConvertSimpleTypes[self.arg.type][1]
  463. AllConverters = {"const OLECHAR": (ArgFormatterOLECHAR, 0, 1),
  464. "WCHAR": (ArgFormatterOLECHAR, 0, 1),
  465. "OLECHAR": (ArgFormatterOLECHAR, 0, 1),
  466. "LPCOLESTR": (ArgFormatterOLECHAR, 1, 1),
  467. "LPOLESTR": (ArgFormatterOLECHAR, 1, 1),
  468. "LPCWSTR": (ArgFormatterOLECHAR, 1, 1),
  469. "LPWSTR": (ArgFormatterOLECHAR, 1, 1),
  470. "LPCSTR": (ArgFormatterOLECHAR, 1, 1),
  471. "LPTSTR": (ArgFormatterTCHAR, 1, 1),
  472. "LPCTSTR": (ArgFormatterTCHAR, 1, 1),
  473. "HANDLE": (ArgFormatterHANDLE, 0),
  474. "BSTR": (ArgFormatterBSTR, 1, 0),
  475. "const IID": (ArgFormatterIID, 0),
  476. "CLSID": (ArgFormatterIID, 0),
  477. "IID": (ArgFormatterIID, 0),
  478. "GUID": (ArgFormatterIID, 0),
  479. "const GUID": (ArgFormatterIID, 0),
  480. "const IID": (ArgFormatterIID, 0),
  481. "REFCLSID": (ArgFormatterIID, 0),
  482. "REFIID": (ArgFormatterIID, 0),
  483. "REFGUID": (ArgFormatterIID, 0),
  484. "const FILETIME": (ArgFormatterTime, 0),
  485. "const SYSTEMTIME":(ArgFormatterTime, 0),
  486. "const LPSYSTEMTIME":(ArgFormatterTime, 1, 1),
  487. "LPSYSTEMTIME": (ArgFormatterTime, 1, 1),
  488. "FILETIME": (ArgFormatterTime, 0),
  489. "SYSTEMTIME": (ArgFormatterTime, 0),
  490. "STATSTG": (ArgFormatterSTATSTG, 0),
  491. "LARGE_INTEGER": (ArgFormatterLARGE_INTEGER, 0),
  492. "ULARGE_INTEGER": (ArgFormatterULARGE_INTEGER, 0),
  493. "VARIANT": (ArgFormatterVARIANT, 0),
  494. "float": (ArgFormatterFloat, 0),
  495. "single": (ArgFormatterFloat, 0),
  496. "short": (ArgFormatterShort, 0),
  497. "WORD": (ArgFormatterShort, 0),
  498. "VARIANT_BOOL": (ArgFormatterShort, 0),
  499. "HWND": (ArgFormatterLONG_PTR, 1),
  500. "HMENU": (ArgFormatterLONG_PTR, 1),
  501. "HOLEMENU": (ArgFormatterLONG_PTR, 1),
  502. "HICON": (ArgFormatterLONG_PTR, 1),
  503. "HDC": (ArgFormatterLONG_PTR, 1),
  504. "LPARAM": (ArgFormatterLONG_PTR, 1),
  505. "WPARAM": (ArgFormatterLONG_PTR, 1),
  506. "LRESULT": (ArgFormatterLONG_PTR, 1),
  507. "UINT": (ArgFormatterShort, 0),
  508. "SVSIF": (ArgFormatterShort, 0),
  509. "Control": (ArgFormatterInterface, 0, 1),
  510. "DataObject": (ArgFormatterInterface, 0, 1),
  511. "_PropertyBag": (ArgFormatterInterface, 0, 1),
  512. "AsyncProp": (ArgFormatterInterface, 0, 1),
  513. "DataSource": (ArgFormatterInterface, 0, 1),
  514. "DataFormat": (ArgFormatterInterface, 0, 1),
  515. "void **": (ArgFormatterInterface, 2, 2),
  516. "ITEMIDLIST": (ArgFormatterIDLIST, 0, 0),
  517. "LPITEMIDLIST": (ArgFormatterIDLIST, 0, 1),
  518. "LPCITEMIDLIST": (ArgFormatterIDLIST, 0, 1),
  519. "const ITEMIDLIST": (ArgFormatterIDLIST, 0, 1),
  520. }
  521. # Auto-add all the simple types
  522. for key in ConvertSimpleTypes.keys():
  523. AllConverters[key] = ArgFormatterSimple, 0
  524. def make_arg_converter(arg):
  525. try:
  526. clz = AllConverters[arg.type][0]
  527. bin = AllConverters[arg.type][1]
  528. decl = 0
  529. if len(AllConverters[arg.type])>2:
  530. decl = AllConverters[arg.type][2]
  531. return clz(arg,bin, decl)
  532. except KeyError:
  533. if arg.type[0]=="I":
  534. return ArgFormatterInterface(arg, 0, 1)
  535. raise error_not_supported("The type '%s' (%s) is unknown." % (arg.type, arg.name))
  536. #############################################################
  537. #
  538. # The instances that represent the args, methods and interface
  539. class Argument:
  540. """A representation of an argument to a COM method
  541. This class contains information about a specific argument to a method.
  542. In addition, methods exist so that an argument knows how to convert itself
  543. to/from Python arguments.
  544. """
  545. # in,out type name [ ]
  546. # -------------- -------- ------------ ------
  547. regex = re.compile(r'/\* \[([^\]]*.*?)] \*/[ \t](.*[* ]+)(\w+)(\[ *])?[\),]')
  548. def __init__(self, good_interface_names):
  549. self.good_interface_names = good_interface_names
  550. self.inout = self.name = self.type = None
  551. self.const = 0
  552. self.arrayDecl = 0
  553. def BuildFromFile(self, file):
  554. """Parse and build my data from a file
  555. Reads the next line in the file, and matches it as an argument
  556. description. If not a valid argument line, an error_not_found exception
  557. is raised.
  558. """
  559. line = file.readline()
  560. mo = self.regex.search(line)
  561. if not mo:
  562. raise error_not_found
  563. self.name = mo.group(3)
  564. self.inout = mo.group(1).split('][')
  565. typ = mo.group(2).strip()
  566. self.raw_type = typ
  567. self.indirectionLevel = 0
  568. if mo.group(4): # Has "[ ]" decl
  569. self.arrayDecl = 1
  570. try:
  571. pos = typ.rindex("__RPC_FAR")
  572. self.indirectionLevel = self.indirectionLevel + 1
  573. typ = typ[:pos].strip()
  574. except ValueError:
  575. pass
  576. typ = typ.replace("__RPC_FAR", "")
  577. while 1:
  578. try:
  579. pos = typ.rindex("*")
  580. self.indirectionLevel = self.indirectionLevel + 1
  581. typ = typ[:pos].strip()
  582. except ValueError:
  583. break
  584. self.type = typ
  585. if self.type[:6]=="const ":
  586. self.unc_type = self.type[6:]
  587. else:
  588. self.unc_type = self.type
  589. if VERBOSE:
  590. print(" Arg %s of type %s%s (%s)" % (self.name, self.type, "*" * self.indirectionLevel, self.inout))
  591. def HasAttribute(self, typ):
  592. """Determines if the argument has the specific attribute.
  593. Argument attributes are specified in the header file, such as
  594. "[in][out][retval]" etc. You can pass a specific string (eg "out")
  595. to find if this attribute was specified for the argument
  596. """
  597. return typ in self.inout
  598. def GetRawDeclaration(self):
  599. ret = "%s %s" % (self.raw_type, self.name)
  600. if self.arrayDecl:
  601. ret = ret + "[]"
  602. return ret
  603. class Method:
  604. """A representation of a C++ method on a COM interface
  605. This class contains information about a specific method, as well as
  606. a list of all @Argument@s
  607. """
  608. # options ret type callconv name
  609. # ----------------- -------- -------- --------
  610. regex = re.compile(r'virtual (/\*.*?\*/ )?(.*?) (.*?) (.*?)\(\w?')
  611. def __init__(self, good_interface_names):
  612. self.good_interface_names = good_interface_names
  613. self.name = self.result = self.callconv = None
  614. self.args = []
  615. def BuildFromFile(self, file):
  616. """Parse and build my data from a file
  617. Reads the next line in the file, and matches it as a method
  618. description. If not a valid method line, an error_not_found exception
  619. is raised.
  620. """
  621. line = file.readline()
  622. mo = self.regex.search(line)
  623. if not mo:
  624. raise error_not_found
  625. self.name = mo.group(4)
  626. self.result = mo.group(2)
  627. if self.result != "HRESULT":
  628. if self.result=="DWORD": # DWORD is for old old stuff?
  629. print("Warning: Old style interface detected - compilation errors likely!")
  630. else:
  631. print("Method %s - Only HRESULT return types are supported." % self.name)
  632. # raise error_not_supported, if VERBOSE:
  633. print(" Method %s %s(" % (self.result, self.name))
  634. while 1:
  635. arg = Argument(self.good_interface_names)
  636. try:
  637. arg.BuildFromFile(file)
  638. self.args.append(arg)
  639. except error_not_found:
  640. break
  641. class Interface:
  642. """A representation of a C++ COM Interface
  643. This class contains information about a specific interface, as well as
  644. a list of all @Method@s
  645. """
  646. # name base
  647. # -------- --------
  648. regex = re.compile("(interface|) ([^ ]*) : public (.*)$")
  649. def __init__(self, mo):
  650. self.methods = []
  651. self.name = mo.group(2)
  652. self.base = mo.group(3)
  653. if VERBOSE:
  654. print("Interface %s : public %s" % (self.name, self.base))
  655. def BuildMethods(self, file):
  656. """Build all sub-methods for this interface"""
  657. # skip the next 2 lines.
  658. file.readline();file.readline();
  659. while 1:
  660. try:
  661. method = Method([self.name])
  662. method.BuildFromFile(file)
  663. self.methods.append(method)
  664. except error_not_found:
  665. break
  666. def find_interface(interfaceName, file):
  667. """Find and return an interface in a file
  668. Given an interface name and file, search for the specified interface.
  669. Upon return, the interface itself has been built,
  670. but not the methods.
  671. """
  672. interface = None
  673. line = file.readline()
  674. while line:
  675. mo = Interface.regex.search(line)
  676. if mo:
  677. name = mo.group(2)
  678. print(name)
  679. AllConverters[name] = (ArgFormatterInterface, 0, 1)
  680. if name==interfaceName:
  681. interface = Interface(mo)
  682. interface.BuildMethods(file)
  683. line = file.readline()
  684. if interface:
  685. return interface
  686. raise error_not_found
  687. def parse_interface_info(interfaceName, file):
  688. """Find, parse and return an interface in a file
  689. Given an interface name and file, search for the specified interface.
  690. Upon return, the interface itself is fully built,
  691. """
  692. try:
  693. return find_interface(interfaceName, file)
  694. except re.error:
  695. traceback.print_exc()
  696. print("The interface could not be built, as the regular expression failed!")
  697. def test():
  698. f=open("d:\\msdev\\include\\objidl.h")
  699. try:
  700. parse_interface_info("IPersistStream", f)
  701. finally:
  702. f.close()
  703. def test_regex(r,text):
  704. res=r.search(text,0)
  705. if res==-1:
  706. print("** Not found")
  707. else:
  708. print("%d\n%s\n%s\n%s\n%s" % (res, r.group(1), r.group(2), r.group(3), r.group(4)))