build.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. """Contains knowledge to build a COM object definition.
  2. This module is used by both the @dynamic@ and @makepy@ modules to build
  3. all knowledge of a COM object.
  4. This module contains classes which contain the actual knowledge of the object.
  5. This include parameter and return type information, the COM dispid and CLSID, etc.
  6. Other modules may use this information to generate .py files, use the information
  7. dynamically, or possibly even generate .html documentation for objects.
  8. """
  9. #
  10. # NOTES: DispatchItem and MapEntry used by dynamic.py.
  11. # the rest is used by makepy.py
  12. #
  13. # OleItem, DispatchItem, MapEntry, BuildCallList() is used by makepy
  14. import sys
  15. import string
  16. from keyword import iskeyword
  17. import pythoncom
  18. from pywintypes import TimeType
  19. import winerror
  20. import datetime
  21. # It isn't really clear what the quoting rules are in a C/IDL string and
  22. # literals like a quote char and backslashes makes life a little painful to
  23. # always render the string perfectly - so just punt and fall-back to a repr()
  24. def _makeDocString(s):
  25. if sys.version_info < (3,):
  26. s = s.encode("mbcs")
  27. return repr(s)
  28. error = "PythonCOM.Client.Build error"
  29. class NotSupportedException(Exception): pass # Raised when we cant support a param type.
  30. DropIndirection="DropIndirection"
  31. NoTranslateTypes = [
  32. pythoncom.VT_BOOL, pythoncom.VT_CLSID, pythoncom.VT_CY,
  33. pythoncom.VT_DATE, pythoncom.VT_DECIMAL, pythoncom.VT_EMPTY,
  34. pythoncom.VT_ERROR, pythoncom.VT_FILETIME, pythoncom.VT_HRESULT,
  35. pythoncom.VT_I1, pythoncom.VT_I2, pythoncom.VT_I4,
  36. pythoncom.VT_I8, pythoncom.VT_INT, pythoncom.VT_NULL,
  37. pythoncom.VT_R4, pythoncom.VT_R8, pythoncom.VT_NULL,
  38. pythoncom.VT_STREAM,
  39. pythoncom.VT_UI1, pythoncom.VT_UI2, pythoncom.VT_UI4,
  40. pythoncom.VT_UI8, pythoncom.VT_UINT, pythoncom.VT_VOID,
  41. ]
  42. NoTranslateMap = {}
  43. for v in NoTranslateTypes:
  44. NoTranslateMap[v] = None
  45. class MapEntry:
  46. "Simple holder for named attibutes - items in a map."
  47. def __init__(self, desc_or_id, names=None, doc=None, resultCLSID=pythoncom.IID_NULL, resultDoc = None, hidden=0):
  48. if type(desc_or_id)==type(0):
  49. self.dispid = desc_or_id
  50. self.desc = None
  51. else:
  52. self.dispid = desc_or_id[0]
  53. self.desc = desc_or_id
  54. self.names = names
  55. self.doc = doc
  56. self.resultCLSID = resultCLSID
  57. self.resultDocumentation = resultDoc
  58. self.wasProperty = 0 # Have I been transformed into a function so I can pass args?
  59. self.hidden = hidden
  60. def __repr__(self):
  61. return ('MapEntry(dispid={s.dispid}, desc={s.desc}, names={s.names}, doc={s.doc!r}, '
  62. 'resultCLSID={s.resultCLSID}, resultDocumentation={s.resultDocumentation}, '
  63. 'wasProperty={s.wasProperty}, hidden={s.hidden}').format(s=self)
  64. def GetResultCLSID(self):
  65. rc = self.resultCLSID
  66. if rc == pythoncom.IID_NULL: return None
  67. return rc
  68. # Return a string, suitable for output - either "'{...}'" or "None"
  69. def GetResultCLSIDStr(self):
  70. rc = self.GetResultCLSID()
  71. if rc is None: return "None"
  72. return repr(str(rc)) # Convert the IID object to a string, then to a string in a string.
  73. def GetResultName(self):
  74. if self.resultDocumentation is None:
  75. return None
  76. return self.resultDocumentation[0]
  77. class OleItem:
  78. typename = "OleItem"
  79. def __init__(self, doc=None):
  80. self.doc = doc
  81. if self.doc:
  82. self.python_name = MakePublicAttributeName(self.doc[0])
  83. else:
  84. self.python_name = None
  85. self.bWritten = 0
  86. self.bIsDispatch = 0
  87. self.bIsSink = 0
  88. self.clsid = None
  89. self.co_class = None
  90. class DispatchItem(OleItem):
  91. typename = "DispatchItem"
  92. def __init__(self, typeinfo=None, attr=None, doc=None, bForUser=1):
  93. OleItem.__init__(self,doc)
  94. self.propMap = {}
  95. self.propMapGet = {}
  96. self.propMapPut = {}
  97. self.mapFuncs = {}
  98. self.defaultDispatchName = None
  99. self.hidden = 0
  100. if typeinfo:
  101. self.Build(typeinfo, attr, bForUser)
  102. def _propMapPutCheck_(self,key,item):
  103. ins, outs, opts = self.CountInOutOptArgs(item.desc[2])
  104. if ins>1: # if a Put property takes more than 1 arg:
  105. if opts+1==ins or ins==item.desc[6]+1:
  106. newKey = "Set" + key
  107. deleteExisting = 0 # This one is still OK
  108. else:
  109. deleteExisting = 1 # No good to us
  110. if key in self.mapFuncs or key in self.propMapGet:
  111. newKey = "Set" + key
  112. else:
  113. newKey = key
  114. item.wasProperty = 1
  115. self.mapFuncs[newKey] = item
  116. if deleteExisting:
  117. del self.propMapPut[key]
  118. def _propMapGetCheck_(self,key,item):
  119. ins, outs, opts = self.CountInOutOptArgs(item.desc[2])
  120. if ins > 0: # if a Get property takes _any_ in args:
  121. if item.desc[6]==ins or ins==opts:
  122. newKey = "Get" + key
  123. deleteExisting = 0 # This one is still OK
  124. else:
  125. deleteExisting = 1 # No good to us
  126. if key in self.mapFuncs:
  127. newKey = "Get" + key
  128. else:
  129. newKey = key
  130. item.wasProperty = 1
  131. self.mapFuncs[newKey] = item
  132. if deleteExisting:
  133. del self.propMapGet[key]
  134. def _AddFunc_(self,typeinfo,fdesc,bForUser):
  135. assert(fdesc.desckind == pythoncom.DESCKIND_FUNCDESC)
  136. id = fdesc.memid
  137. funcflags = fdesc.wFuncFlags
  138. try:
  139. names = typeinfo.GetNames(id)
  140. name=names[0]
  141. except pythoncom.ole_error:
  142. name = ""
  143. names = None
  144. doc = None
  145. try:
  146. if bForUser:
  147. doc = typeinfo.GetDocumentation(id)
  148. except pythoncom.ole_error:
  149. pass
  150. if id==0 and name:
  151. self.defaultDispatchName = name
  152. invkind = fdesc.invkind
  153. # We need to translate any Alias', Enums, structs etc in result and args
  154. typerepr, flag, defval = fdesc.rettype
  155. # sys.stderr.write("%s result - %s -> " % (name, typerepr))
  156. typerepr, resultCLSID, resultDoc = _ResolveType(typerepr, typeinfo)
  157. # sys.stderr.write("%s\n" % (typerepr,))
  158. fdesc.rettype = typerepr, flag, defval, resultCLSID
  159. # Translate any Alias or Enums in argument list.
  160. argList = []
  161. for argDesc in fdesc.args:
  162. typerepr, flag, defval = argDesc
  163. # sys.stderr.write("%s arg - %s -> " % (name, typerepr))
  164. arg_type, arg_clsid, arg_doc = _ResolveType(typerepr, typeinfo)
  165. argDesc = arg_type, flag, defval, arg_clsid
  166. # sys.stderr.write("%s\n" % (argDesc[0],))
  167. argList.append(argDesc)
  168. fdesc.args = tuple(argList)
  169. hidden = (funcflags & pythoncom.FUNCFLAG_FHIDDEN) != 0
  170. if invkind == pythoncom.INVOKE_PROPERTYGET:
  171. map = self.propMapGet
  172. # This is not the best solution, but I dont think there is
  173. # one without specific "set" syntax.
  174. # If there is a single PUT or PUTREF, it will function as a property.
  175. # If there are both, then the PUT remains a property, and the PUTREF
  176. # gets transformed into a function.
  177. # (in vb, PUT=="obj=other_obj", PUTREF="set obj=other_obj
  178. elif invkind in (pythoncom.INVOKE_PROPERTYPUT, pythoncom.INVOKE_PROPERTYPUTREF):
  179. # Special case
  180. existing = self.propMapPut.get(name, None)
  181. if existing is not None:
  182. if existing.desc[4]==pythoncom.INVOKE_PROPERTYPUT: # Keep this one
  183. map = self.mapFuncs
  184. name = "Set"+name
  185. else: # Existing becomes a func.
  186. existing.wasProperty = 1
  187. self.mapFuncs["Set"+name]=existing
  188. map = self.propMapPut # existing gets overwritten below.
  189. else:
  190. map = self.propMapPut # first time weve seen it.
  191. elif invkind == pythoncom.INVOKE_FUNC:
  192. map = self.mapFuncs
  193. else:
  194. map = None
  195. if not map is None:
  196. # if map.has_key(name):
  197. # sys.stderr.write("Warning - overwriting existing method/attribute %s\n" % name)
  198. map[name] = MapEntry(fdesc, names, doc, resultCLSID, resultDoc, hidden)
  199. # any methods that can't be reached via DISPATCH we return None
  200. # for, so dynamic dispatch doesnt see it.
  201. if fdesc.funckind != pythoncom.FUNC_DISPATCH:
  202. return None
  203. return (name,map)
  204. return None
  205. def _AddVar_(self, typeinfo, vardesc, bForUser):
  206. ### need pythoncom.VARFLAG_FRESTRICTED ...
  207. ### then check it
  208. assert(vardesc.desckind == pythoncom.DESCKIND_VARDESC)
  209. if vardesc.varkind == pythoncom.VAR_DISPATCH:
  210. id = vardesc.memid
  211. names = typeinfo.GetNames(id)
  212. # Translate any Alias or Enums in result.
  213. typerepr, flags, defval = vardesc.elemdescVar
  214. typerepr, resultCLSID, resultDoc = _ResolveType(typerepr, typeinfo)
  215. vardesc.elemdescVar = typerepr, flags, defval
  216. doc = None
  217. try:
  218. if bForUser: doc = typeinfo.GetDocumentation(id)
  219. except pythoncom.ole_error:
  220. pass
  221. # handle the enumerator specially
  222. map = self.propMap
  223. # Check if the element is hidden.
  224. hidden = (vardesc.wVarFlags & 0x40) != 0 # VARFLAG_FHIDDEN
  225. map[names[0]] = MapEntry(vardesc, names, doc, resultCLSID, resultDoc, hidden)
  226. return (names[0],map)
  227. else:
  228. return None
  229. def Build(self, typeinfo, attr, bForUser = 1):
  230. self.clsid = attr[0]
  231. self.bIsDispatch = (attr.wTypeFlags & pythoncom.TYPEFLAG_FDISPATCHABLE) != 0
  232. if typeinfo is None: return
  233. # Loop over all methods
  234. for j in range(attr[6]):
  235. fdesc = typeinfo.GetFuncDesc(j)
  236. self._AddFunc_(typeinfo,fdesc,bForUser)
  237. # Loop over all variables (ie, properties)
  238. for j in range(attr[7]):
  239. fdesc = typeinfo.GetVarDesc(j)
  240. self._AddVar_(typeinfo,fdesc,bForUser)
  241. # Now post-process the maps. For any "Get" or "Set" properties
  242. # that have arguments, we must turn them into methods. If a method
  243. # of the same name already exists, change the name.
  244. for key, item in list(self.propMapGet.items()):
  245. self._propMapGetCheck_(key,item)
  246. for key, item in list(self.propMapPut.items()):
  247. self._propMapPutCheck_(key,item)
  248. def CountInOutOptArgs(self, argTuple):
  249. "Return tuple counting in/outs/OPTS. Sum of result may not be len(argTuple), as some args may be in/out."
  250. ins = out = opts = 0
  251. for argCheck in argTuple:
  252. inOut = argCheck[1]
  253. if inOut==0:
  254. ins = ins + 1
  255. out = out + 1
  256. else:
  257. if inOut & pythoncom.PARAMFLAG_FIN:
  258. ins = ins + 1
  259. if inOut & pythoncom.PARAMFLAG_FOPT:
  260. opts = opts + 1
  261. if inOut & pythoncom.PARAMFLAG_FOUT:
  262. out = out + 1
  263. return ins, out, opts
  264. def MakeFuncMethod(self, entry, name, bMakeClass = 1):
  265. # If we have a type description, and not varargs...
  266. if entry.desc is not None and (len(entry.desc) < 6 or entry.desc[6]!=-1):
  267. return self.MakeDispatchFuncMethod(entry, name, bMakeClass)
  268. else:
  269. return self.MakeVarArgsFuncMethod(entry, name, bMakeClass)
  270. def MakeDispatchFuncMethod(self, entry, name, bMakeClass = 1):
  271. fdesc = entry.desc
  272. doc = entry.doc
  273. names = entry.names
  274. ret = []
  275. if bMakeClass:
  276. linePrefix = "\t"
  277. defNamedOptArg = "defaultNamedOptArg"
  278. defNamedNotOptArg = "defaultNamedNotOptArg"
  279. defUnnamedArg = "defaultUnnamedArg"
  280. else:
  281. linePrefix = ""
  282. defNamedOptArg = "pythoncom.Missing"
  283. defNamedNotOptArg = "pythoncom.Missing"
  284. defUnnamedArg = "pythoncom.Missing"
  285. defOutArg = "pythoncom.Missing"
  286. id = fdesc[0]
  287. s = linePrefix + 'def ' + name + '(self' + BuildCallList(fdesc, names, defNamedOptArg, defNamedNotOptArg, defUnnamedArg, defOutArg) + '):'
  288. ret.append(s)
  289. if doc and doc[1]:
  290. ret.append(linePrefix + '\t' + _makeDocString(doc[1]))
  291. resclsid = entry.GetResultCLSID()
  292. if resclsid:
  293. resclsid = "'%s'" % resclsid
  294. else:
  295. resclsid = 'None'
  296. # Strip the default values from the arg desc
  297. retDesc = fdesc[8][:2]
  298. argsDesc = tuple([what[:2] for what in fdesc[2]])
  299. # The runtime translation of the return types is expensive, so when we know the
  300. # return type of the function, there is no need to check the type at runtime.
  301. # To qualify, this function must return a "simple" type, and have no byref args.
  302. # Check if we have byrefs or anything in the args which mean we still need a translate.
  303. param_flags = [what[1] for what in fdesc[2]]
  304. bad_params = [flag for flag in param_flags if flag & (pythoncom.PARAMFLAG_FOUT | pythoncom.PARAMFLAG_FRETVAL)!=0]
  305. s = None
  306. if len(bad_params)==0 and len(retDesc)==2 and retDesc[1]==0:
  307. rd = retDesc[0]
  308. if rd in NoTranslateMap:
  309. s = '%s\treturn self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s)' % (linePrefix, id, fdesc[4], retDesc, argsDesc, _BuildArgList(fdesc, names))
  310. elif rd in [pythoncom.VT_DISPATCH, pythoncom.VT_UNKNOWN]:
  311. s = '%s\tret = self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s)\n' % (linePrefix, id, fdesc[4], retDesc, repr(argsDesc), _BuildArgList(fdesc, names))
  312. s = s + '%s\tif ret is not None:\n' % (linePrefix,)
  313. if rd == pythoncom.VT_UNKNOWN:
  314. s = s + "%s\t\t# See if this IUnknown is really an IDispatch\n" % (linePrefix,)
  315. s = s + "%s\t\ttry:\n" % (linePrefix,)
  316. s = s + "%s\t\t\tret = ret.QueryInterface(pythoncom.IID_IDispatch)\n" % (linePrefix,)
  317. s = s + "%s\t\texcept pythoncom.error:\n" % (linePrefix,)
  318. s = s + "%s\t\t\treturn ret\n" % (linePrefix,)
  319. s = s + '%s\t\tret = Dispatch(ret, %s, %s)\n' % (linePrefix,repr(name), resclsid)
  320. s = s + '%s\treturn ret' % (linePrefix)
  321. elif rd == pythoncom.VT_BSTR:
  322. s = "%s\t# Result is a Unicode object\n" % (linePrefix,)
  323. s = s + '%s\treturn self._oleobj_.InvokeTypes(%d, LCID, %s, %s, %s%s)' % (linePrefix, id, fdesc[4], retDesc, repr(argsDesc), _BuildArgList(fdesc, names))
  324. # else s remains None
  325. if s is None:
  326. s = '%s\treturn self._ApplyTypes_(%d, %s, %s, %s, %s, %s%s)' % (linePrefix, id, fdesc[4], retDesc, argsDesc, repr(name), resclsid, _BuildArgList(fdesc, names))
  327. ret.append(s)
  328. ret.append("")
  329. return ret
  330. def MakeVarArgsFuncMethod(self, entry, name, bMakeClass = 1):
  331. fdesc = entry.desc
  332. names = entry.names
  333. doc = entry.doc
  334. ret = []
  335. argPrefix = "self"
  336. if bMakeClass:
  337. linePrefix = "\t"
  338. else:
  339. linePrefix = ""
  340. ret.append(linePrefix + 'def ' + name + '(' + argPrefix + ', *args):')
  341. if doc and doc[1]: ret.append(linePrefix + '\t' + _makeDocString(doc[1]))
  342. if fdesc:
  343. invoketype = fdesc[4]
  344. else:
  345. invoketype = pythoncom.DISPATCH_METHOD
  346. s = linePrefix + '\treturn self._get_good_object_(self._oleobj_.Invoke(*(('
  347. ret.append(s + str(entry.dispid) + ",0,%d,1)+args)),'%s')" % (invoketype, names[0]))
  348. ret.append("")
  349. return ret
  350. # Note - "DispatchItem" poorly named - need a new intermediate class.
  351. class VTableItem(DispatchItem):
  352. def Build(self, typeinfo, attr, bForUser = 1):
  353. DispatchItem.Build(self, typeinfo, attr, bForUser)
  354. assert typeinfo is not None, "Cant build vtables without type info!"
  355. meth_list = list(self.mapFuncs.values()) + list(self.propMapGet.values()) + list(self.propMapPut.values())
  356. meth_list.sort(key=lambda m: m.desc[7])
  357. # Now turn this list into the run-time representation
  358. # (ready for immediate use or writing to gencache)
  359. self.vtableFuncs = []
  360. for entry in meth_list:
  361. self.vtableFuncs.append( (entry.names, entry.dispid, entry.desc) )
  362. # A Lazy dispatch item - builds an item on request using info from
  363. # an ITypeComp. The dynamic module makes the called to build each item,
  364. # and also holds the references to the typeinfo and typecomp.
  365. class LazyDispatchItem(DispatchItem):
  366. typename = "LazyDispatchItem"
  367. def __init__(self, attr, doc):
  368. self.clsid = attr[0]
  369. DispatchItem.__init__(self, None, attr, doc, 0)
  370. typeSubstMap = {
  371. pythoncom.VT_INT: pythoncom.VT_I4,
  372. pythoncom.VT_UINT: pythoncom.VT_UI4,
  373. pythoncom.VT_HRESULT: pythoncom.VT_I4,
  374. }
  375. def _ResolveType(typerepr, itypeinfo):
  376. # Resolve VT_USERDEFINED (often aliases or typed IDispatches)
  377. if type(typerepr)==tuple:
  378. indir_vt, subrepr = typerepr
  379. if indir_vt == pythoncom.VT_PTR:
  380. # If it is a VT_PTR to a VT_USERDEFINED that is an IDispatch/IUnknown,
  381. # then it resolves to simply the object.
  382. # Otherwise, it becomes a ByRef of the resolved type
  383. # We need to drop an indirection level on pointer to user defined interfaces.
  384. # eg, (VT_PTR, (VT_USERDEFINED, somehandle)) needs to become VT_DISPATCH
  385. # only when "somehandle" is an object.
  386. # but (VT_PTR, (VT_USERDEFINED, otherhandle)) doesnt get the indirection dropped.
  387. was_user = type(subrepr)==tuple and subrepr[0]==pythoncom.VT_USERDEFINED
  388. subrepr, sub_clsid, sub_doc = _ResolveType(subrepr, itypeinfo)
  389. if was_user and subrepr in [pythoncom.VT_DISPATCH, pythoncom.VT_UNKNOWN, pythoncom.VT_RECORD]:
  390. # Drop the VT_PTR indirection
  391. return subrepr, sub_clsid, sub_doc
  392. # Change PTR indirection to byref
  393. return subrepr | pythoncom.VT_BYREF, sub_clsid, sub_doc
  394. if indir_vt == pythoncom.VT_SAFEARRAY:
  395. # resolve the array element, and convert to VT_ARRAY
  396. subrepr, sub_clsid, sub_doc = _ResolveType(subrepr, itypeinfo)
  397. return pythoncom.VT_ARRAY | subrepr, sub_clsid, sub_doc
  398. if indir_vt == pythoncom.VT_CARRAY: # runtime has no support for this yet.
  399. # resolve the array element, and convert to VT_CARRAY
  400. # sheesh - return _something_
  401. return pythoncom.VT_CARRAY, None, None
  402. if indir_vt == pythoncom.VT_USERDEFINED:
  403. try:
  404. resultTypeInfo = itypeinfo.GetRefTypeInfo(subrepr)
  405. except pythoncom.com_error as details:
  406. if details.hresult in [winerror.TYPE_E_CANTLOADLIBRARY, winerror.TYPE_E_LIBNOTREGISTERED]:
  407. # an unregistered interface
  408. return pythoncom.VT_UNKNOWN, None, None
  409. raise
  410. resultAttr = resultTypeInfo.GetTypeAttr()
  411. typeKind = resultAttr.typekind
  412. if typeKind == pythoncom.TKIND_ALIAS:
  413. tdesc = resultAttr.tdescAlias
  414. return _ResolveType(tdesc, resultTypeInfo)
  415. elif typeKind in [pythoncom.TKIND_ENUM, pythoncom.TKIND_MODULE]:
  416. # For now, assume Long
  417. return pythoncom.VT_I4, None, None
  418. elif typeKind == pythoncom.TKIND_DISPATCH:
  419. clsid = resultTypeInfo.GetTypeAttr()[0]
  420. retdoc = resultTypeInfo.GetDocumentation(-1)
  421. return pythoncom.VT_DISPATCH, clsid, retdoc
  422. elif typeKind in [pythoncom.TKIND_INTERFACE,
  423. pythoncom.TKIND_COCLASS]:
  424. # XXX - should probably get default interface for CO_CLASS???
  425. clsid = resultTypeInfo.GetTypeAttr()[0]
  426. retdoc = resultTypeInfo.GetDocumentation(-1)
  427. return pythoncom.VT_UNKNOWN, clsid, retdoc
  428. elif typeKind == pythoncom.TKIND_RECORD:
  429. return pythoncom.VT_RECORD, None, None
  430. raise NotSupportedException("Can not resolve alias or user-defined type")
  431. return typeSubstMap.get(typerepr,typerepr), None, None
  432. def _BuildArgList(fdesc, names):
  433. "Builds list of args to the underlying Invoke method."
  434. # Word has TypeInfo for Insert() method, but says "no args"
  435. numArgs = max(fdesc[6], len(fdesc[2]))
  436. names = list(names)
  437. while None in names:
  438. i = names.index(None)
  439. names[i] = "arg%d" % (i,)
  440. # We've seen 'source safe' libraries offer the name of 'ret' params in
  441. # 'names' - although we can't reproduce this, it would be insane to offer
  442. # more args than we have arg infos for - hence the upper limit on names...
  443. names = list(map(MakePublicAttributeName, names[1:(numArgs + 1)]))
  444. name_num = 0
  445. while len(names) < numArgs:
  446. names.append("arg%d" % (len(names),))
  447. # As per BuildCallList(), avoid huge lines.
  448. # Hack a "\n" at the end of every 5th name - "strides" would be handy
  449. # here but don't exist in 2.2
  450. for i in range(0, len(names), 5):
  451. names[i] = names[i] + "\n\t\t\t"
  452. return "," + ", ".join(names)
  453. valid_identifier_chars = string.ascii_letters + string.digits + "_"
  454. def demunge_leading_underscores(className):
  455. i = 0
  456. while className[i] == "_":
  457. i += 1
  458. assert i >= 2, "Should only be here with names starting with '__'"
  459. return className[i-1:] + className[:i-1]
  460. # Given a "public name" (eg, the name of a class, function, etc)
  461. # make sure it is a legal (and reasonable!) Python name.
  462. def MakePublicAttributeName(className, is_global = False):
  463. # Given a class attribute that needs to be public, convert it to a
  464. # reasonable name.
  465. # Also need to be careful that the munging doesnt
  466. # create duplicates - eg, just removing a leading "_" is likely to cause
  467. # a clash.
  468. # if is_global is True, then the name is a global variable that may
  469. # overwrite a builtin - eg, "None"
  470. if className[:2]=='__':
  471. return demunge_leading_underscores(className)
  472. elif className == 'None':
  473. # assign to None is evil (and SyntaxError in 2.4, even though
  474. # iskeyword says False there) - note that if it was a global
  475. # it would get picked up below
  476. className = 'NONE'
  477. elif iskeyword(className):
  478. # most keywords are lower case (except True, False etc in py3k)
  479. ret = className.capitalize()
  480. # but those which aren't get forced upper.
  481. if ret == className:
  482. ret = ret.upper()
  483. return ret
  484. elif is_global and hasattr(__builtins__, className):
  485. # builtins may be mixed case. If capitalizing it doesn't change it,
  486. # force to all uppercase (eg, "None", "True" become "NONE", "TRUE"
  487. ret = className.capitalize()
  488. if ret==className: # didn't change - force all uppercase.
  489. ret = ret.upper()
  490. return ret
  491. # Strip non printable chars
  492. return ''.join([char for char in className if char in valid_identifier_chars])
  493. # Given a default value passed by a type library, return a string with
  494. # an appropriate repr() for the type.
  495. # Takes a raw ELEMDESC and returns a repr string, or None
  496. # (NOTE: The string itself may be '"None"', which is valid, and different to None.
  497. # XXX - To do: Dates are probably screwed, but can they come in?
  498. def MakeDefaultArgRepr(defArgVal):
  499. try:
  500. inOut = defArgVal[1]
  501. except IndexError:
  502. # something strange - assume is in param.
  503. inOut = pythoncom.PARAMFLAG_FIN
  504. if inOut & pythoncom.PARAMFLAG_FHASDEFAULT:
  505. # times need special handling...
  506. val = defArgVal[2]
  507. if isinstance(val, datetime.datetime):
  508. # VARIANT <-> SYSTEMTIME conversions always lose any sub-second
  509. # resolution, so just use a 'timetuple' here.
  510. return repr(tuple(val.utctimetuple()))
  511. if type(val) is TimeType:
  512. # must be the 'old' pywintypes time object...
  513. year=val.year; month=val.month; day=val.day; hour=val.hour; minute=val.minute; second=val.second; msec=val.msec
  514. return "pywintypes.Time((%(year)d, %(month)d, %(day)d, %(hour)d, %(minute)d, %(second)d,0,0,0,%(msec)d))" % locals()
  515. return repr(val)
  516. return None
  517. def BuildCallList(fdesc, names, defNamedOptArg, defNamedNotOptArg, defUnnamedArg, defOutArg, is_comment = False):
  518. "Builds a Python declaration for a method."
  519. # Names[0] is the func name - param names are from 1.
  520. numArgs = len(fdesc[2])
  521. numOptArgs = fdesc[6]
  522. strval = ''
  523. if numOptArgs==-1: # Special value that says "var args after here"
  524. firstOptArg = numArgs
  525. numArgs = numArgs - 1
  526. else:
  527. firstOptArg = numArgs - numOptArgs
  528. for arg in range(numArgs):
  529. try:
  530. argName = names[arg+1]
  531. namedArg = argName is not None
  532. except IndexError:
  533. namedArg = 0
  534. if not namedArg: argName = "arg%d" % (arg)
  535. thisdesc = fdesc[2][arg]
  536. # See if the IDL specified a default value
  537. defArgVal = MakeDefaultArgRepr(thisdesc)
  538. if defArgVal is None:
  539. # Out params always get their special default
  540. if thisdesc[1] & (pythoncom.PARAMFLAG_FOUT | pythoncom.PARAMFLAG_FIN) == pythoncom.PARAMFLAG_FOUT:
  541. defArgVal = defOutArg
  542. else:
  543. # Unnamed arg - always allow default values.
  544. if namedArg:
  545. # Is a named argument
  546. if arg >= firstOptArg:
  547. defArgVal = defNamedOptArg
  548. else:
  549. defArgVal = defNamedNotOptArg
  550. else:
  551. defArgVal = defUnnamedArg
  552. argName = MakePublicAttributeName(argName)
  553. # insanely long lines with an 'encoding' flag crashes python 2.4.0
  554. # keep 5 args per line
  555. # This may still fail if the arg names are insane, but that seems
  556. # unlikely. See also _BuildArgList()
  557. if (arg+1) % 5 == 0:
  558. strval = strval + "\n"
  559. if is_comment:
  560. strval = strval + "#"
  561. strval = strval + "\t\t\t"
  562. strval = strval + ", " + argName
  563. if defArgVal:
  564. strval = strval + "=" + defArgVal
  565. if numOptArgs==-1:
  566. strval = strval + ", *" + names[-1]
  567. return strval
  568. if __name__=='__main__':
  569. print("Use 'makepy.py' to generate Python code - this module is just a helper")