enthought / enthought/comtypes
Invalid Syntax error for CoClass with numbers only name.
- Dominant language
- Python
- Stars
- 345
- Forks
- 105
- PR merge metrics
- No merged PRs in 30d
Description
When connecting to a third party COM server I get the following error:
```
Traceback (most recent call last):
File "v:/dev/Projekte3/inventsim_py/basic.py", line 4, in
app = comtypes.client.CreateObject("EMInvent.InventSim.App")
File "C:\Users\jan\AppData\Roaming\Python\Python38\site-packages\comtypes\client\__init__.py",
line 250, in CreateObject
return _manage(obj, clsid, interface=interface)
File "C:\Users\jan\AppData\Roaming\Python\Python38\site-packages\comtypes\client\__init__.py",
line 188, in _manage
obj = GetBestInterface(obj)
File "C:\Users\jan\AppData\Roaming\Python\Python38\site-packages\comtypes\client\__init__.py",
line 110, in GetBestInterface
mod = GetModule(tlib)
File "C:\Users\jan\AppData\Roaming\Python\Python38\site-packages\comtypes\client\_generate.py", line 110, in GetModule
mod = _CreateWrapper(tlib, pathname)
File "C:\Users\jan\AppData\Roaming\Python\Python38\site-packages\comtypes\client\_generate.py", line 184, in _CreateWrapper
mod = _my_import(fullname)
File "C:\Users\jan\AppData\Roaming\Python\Python38\site-packages\comtypes\client\_generate.py", line 24, in _my_import
return __import__(fullname, globals(), locals(), ['DUMMY'])
File "C:\Users\jan\AppData\Roaming\Python\Python38\site-packages\comtypes\gen\_1447D2FE_3193_49E8_BAB7_A56CE38BC9D8_0_2_0.py", line 21
class 103205609(CoClass):
^
SyntaxError: invalid syntax
```
The cause of the problem is a coclass named with numbers only:

ParseCoClass of tlbparser.py uses the coclass name as an identifier. The generated generic module has an invalid syntax therefore.
I fixed it by modifying tlbparser.py. I modify the coclass name if necessary. It works for me but I don't know if there may be side effects.
```
def ParseCoClass(self, tinfo, ta):
# possible ta.wTypeFlags: helpstring, helpcontext, licensed,
# version, control, hidden, and appobject
coclass_name, doc = tinfo.GetDocumentation(-1)[0:2]
coclass_name = self.make_valid_identifier(coclass_name) # ijb
tlibattr = tinfo.GetContainingTypeLib()[0].GetLibAttr()
coclass = typedesc.CoClass(coclass_name,
str(ta.guid),
self.coclass_type_flags(ta.wTypeFlags),
tlibattr)
...
```
Contributor guide
Assessment
This issue has not been assessed yet.