-
-
Notifications
You must be signed in to change notification settings - Fork 102
Define enumerations in generated modules. #345
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I have found an implementation that allows us to act as if it were a submodule in a package without creating another Using the way of implementation for The code for wrapper-module is assumed to be as follows # assuming generated from `stdole2.tlb`
# -*- coding: mbcs -*-
from ctypes import *
from comtypes.automation import DISPPARAMS, EXCEPINFO, IDispatch, IEnumVARIANT
from comtypes import (
_check_version, BSTR, CoClass, COMMETHOD, dispid, DISPMETHOD,
DISPPROPERTY, GUID, IUnknown
)
from ctypes.wintypes import VARIANT_BOOL
from ctypes import HRESULT
_lcid = 0 # change this if required
typelib_path = 'C:\\Windows\\System32\\stdole2.tlb'
OLE_XPOS_HIMETRIC = c_int
...
# values for enumeration 'OLE_TRISTATE'
Unchecked = 0
Checked = 1
Gray = 2
OLE_TRISTATE = c_int # enum
...
class __enumerations(object):
"""Wrapper namespace for stdole enumerations."""
from enum import IntEnum # or IntFlag?
class OLE_TRISTATE(IntEnum):
Unchecked = 0
Checked = 1
Gray = 2
__enumerations.__name__ = __name__ + '.enums'
sys.modules[__enumerations.__name__] = __enumerations |
If it came to writing the enumeration definitions in the wrapper module, I had to change the However, if definitions of the enumerations were in the friendly module, I can do what I want without damaging the wrapper module implementation. Of course, this will break backward compatibility because symbols like But, I suspect that few people would import and use The following code is generated for from enum import IntFlag
from comtypes.gen import _420B2830_E718_11CF_893D_00A0C9054228_0_1_0
from comtypes.gen._420B2830_E718_11CF_893D_00A0C9054228_0_1_0 import (
Alias, _check_version, Folder, helpstring, VARIANT_BOOL, Hidden,
TextStream, dispid, TemporaryFolder, ForWriting, IFile,
IScriptEncoder, ForReading, TristateUseDefault, CDRom, Remote,
Removable, Fixed, Compressed, DatabaseCompare, TristateFalse,
StdOut, Directory, Drives, Dictionary, TristateMixed, StdErr,
StdIn, System, VARIANT, IDictionary, ReadOnly, IFolderCollection,
Library, UnknownType, IDrive, TextCompare, IFileSystem3, Volume,
File, WindowsFolder, TristateTrue, IFolder, ForAppending, Folders,
CoClass, _lcid, RamDisk, BinaryCompare, FileSystemObject, GUID,
IFileSystem, IFileCollection, SystemFolder, typelib_path, Archive,
Drive, COMMETHOD, IDriveCollection, Encoder, HRESULT, IUnknown,
Normal, BSTR, Files, ITextStream
)
class CompareMethod(IntFlag):
BinaryCompare = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.BinaryCompare
TextCompare = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.TextCompare
DatabaseCompare = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.DatabaseCompare
class __MIDL___MIDL_itf_scrrun_0001_0001_0002(IntFlag):
WindowsFolder = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.WindowsFolder
SystemFolder = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.SystemFolder
TemporaryFolder = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.TemporaryFolder
class IOMode(IntFlag):
ForReading = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.ForReading
ForWriting = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.ForWriting
ForAppending = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.ForAppending
class Tristate(IntFlag):
TristateTrue = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.TristateTrue
TristateFalse = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.TristateFalse
TristateUseDefault = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.TristateUseDefault
TristateMixed = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.TristateMixed
class __MIDL___MIDL_itf_scrrun_0001_0001_0001(IntFlag):
UnknownType = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.UnknownType
Removable = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.Removable
Fixed = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.Fixed
Remote = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.Remote
CDRom = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.CDRom
RamDisk = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.RamDisk
class __MIDL___MIDL_itf_scrrun_0001_0001_0003(IntFlag):
StdIn = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.StdIn
StdOut = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.StdOut
StdErr = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.StdErr
class __MIDL___MIDL_itf_scrrun_0000_0000_0001(IntFlag):
Normal = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.Normal
ReadOnly = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.ReadOnly
Hidden = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.Hidden
System = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.System
Volume = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.Volume
Directory = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.Directory
Archive = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.Archive
Alias = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.Alias
Compressed = _420B2830_E718_11CF_893D_00A0C9054228_0_1_0.Compressed
SpecialFolderConst = __MIDL___MIDL_itf_scrrun_0001_0001_0002
DriveTypeConst = __MIDL___MIDL_itf_scrrun_0001_0001_0001
FileAttribute = __MIDL___MIDL_itf_scrrun_0000_0000_0001
StandardStreamTypes = __MIDL___MIDL_itf_scrrun_0001_0001_0003
__all__ = [
'Alias', 'Folder', 'UnknownType',
'__MIDL___MIDL_itf_scrrun_0001_0001_0001',
'__MIDL___MIDL_itf_scrrun_0000_0000_0001', 'IDrive',
'TextCompare', 'Hidden', 'IFileSystem3', 'Volume', 'TextStream',
'TemporaryFolder', 'FileAttribute', 'ForWriting', 'IFile', 'File',
'IScriptEncoder', 'IOMode', 'WindowsFolder', 'ForReading',
'StandardStreamTypes', 'TristateTrue', 'IFolder',
'TristateUseDefault', 'ForAppending', 'CDRom', 'Remote',
'Folders', 'Removable', 'RamDisk', 'Fixed', 'Compressed',
'DatabaseCompare', '__MIDL___MIDL_itf_scrrun_0001_0001_0003',
'BinaryCompare', 'TristateFalse', 'StdOut', 'Directory',
'FileSystemObject', 'DriveTypeConst', 'Drives', 'IFileSystem',
'IFileCollection', 'SystemFolder', 'typelib_path', 'Dictionary',
'Archive', 'TristateMixed', 'SpecialFolderConst', 'Drive',
'StdErr', 'StdIn', 'System', 'IDriveCollection', 'Encoder',
'IDictionary', '__MIDL___MIDL_itf_scrrun_0001_0001_0002',
'Normal', 'ReadOnly', 'CompareMethod', 'IFolderCollection',
'Library', 'Files', 'ITextStream', 'Tristate'
] |
The reasons as to why
|
I PRed #475 |
I don't understand why so many changes are being coupled with the drop py2 effort. I'd suggest simply dropping support for Python 2 and then incrementally remove python 2 compatibility. I notice these PRs are being targeted not at the mainline branch, which makes me worried that there's a lot of accumulated flux that's going to land all at once. I would recommend instead to make frequent releases with small changes to limit the amount of change any particular release creates (and make it easier to bisect any issues or regressions). |
I agree with removing this from the scope of |
Motivation
Currently, when we generate a Python module from a COM type library with
client.GetModule
, the stuffs defined asEnumeration
in the COM type library are defined in Python as aliases forctypes.c_int
.Importing and using them in a production code is rarely done.
They are passed to
STDMETHOD
,COMMETHOD
,DISPMETHOD
orDISPPROPERTY
to define method and property arguments and return values.It is difficult to add features such as enumerated type member information to these symbols by using classes inherited from
c_int
or by using weird tricks regarding__dict__
, given the magnitude of impact.This problem has been left as a comment that marked
XXX
on thecodegenerator
, since this package was born.comtypes/comtypes/tools/codegenerator.py
Lines 406 to 421 in cc9a013
Summary Examples
This feature is implemented by using the same name but making it an alias of
c_int
in the wrapper module and a subclass ofenum.IntFlag
in the friendly module.The
.../comtypes/gen/Scripting.py
prior to this change looks something like this.With this change, it looks like this.
Why
IntFlag
?The reasons as to why
IntFlag
is used for enumerations, because ....value
every time.for interoperability with other systems
.Compatibility with traditional
c_int
aliasesCodebases like below will no longer work as before with this change because they rely on the symbol is an alias for
c_int
.There are some ways to use both new and legacy functionalities.
Users can still use the old definitions by changing import and the definitions of module-level symbols as follows;
previous descriptions (ideas)
I propose the following procedure as a solution to this problem.
The args passed to functions like
STDMETHOD
will be changed toctypes.c_int
itself from the symbols defined asEnumeration
andExternal
orAlias
those referred toEnumeration
in the COM type library.↓
The symbols currently defined as aliases of
ctypes.c_int
will be changed the definition as enumeration types.enum
module.The
enum
module is supported since Py3.4, so it is not available in Py2.7 or Py3.3.Therefore, it will be after older Python versions are dropped that these will actually be changed.
The text was updated successfully, but these errors were encountered: