Skip to content
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

win32com Cannot Handle Decimal In COM Objects #1149

Open
suresafe opened this issue Jan 23, 2018 · 4 comments
Open

win32com Cannot Handle Decimal In COM Objects #1149

suresafe opened this issue Jan 23, 2018 · 4 comments

Comments

@suresafe
Copy link

Attempting to makepy for a COM object which has parameters of type Decimal [i.e. .NET Decimal] fails with an error "NameError: name 'Decimal' is not defined"

C:\Python36\Lib\site-packages\win32com\client>makepy
Generating to C:\Python36\lib\site-packages\win32com\gen_py\DFD995C9-8BA8-46A0-A794-D445C13227AEx0x1x1.py
Building definitions from type library...
Generating...
Importing module
Traceback (most recent call last):
  File "C:\Python36\Lib\site-packages\win32com\client\makepy.py", line 387, in <module>
    rc = main()
  File "C:\Python36\Lib\site-packages\win32com\client\makepy.py", line 380, in main
    GenerateFromTypeLibSpec(arg, f, verboseLevel = verboseLevel, bForDemand = bForDemand, BuildHidden = hiddenSpec)
  File "C:\Python36\Lib\site-packages\win32com\client\makepy.py", line 286, in GenerateFromTypeLibSpec
    gencache.AddModuleToCache(info.clsid, info.lcid, info.major, info.minor)
  File "C:\Python36\lib\site-packages\win32com\client\gencache.py", line 548, in AddModuleToCache
    mod = _GetModule(fname)
  File "C:\Python36\lib\site-packages\win32com\client\gencache.py", line 627, in _GetModule
    mod = __import__(mod_name)
  File "C:\Python36\lib\site-packages\win32com\gen_py\DFD995C9-8BA8-46A0-A794-D445C13227AEx0x1x1.py", line 2587, in <module>
    class PurchaseOrderHeader(DispatchBaseClass):
  File "C:\Python36\lib\site-packages\win32com\gen_py\DFD995C9-8BA8-46A0-A794-D445C13227AEx0x1x1.py", line 2595, in PurchaseOrderHeader
    def AddPurchaseOrderProductLine(self, ProductCode_=defaultNamedNotOptArg, QuantityOrdered_=defaultNamedNotOptArg, description_='', UnitPurchasePrice_=Decimal('-1')
NameError: name 'Decimal' is not defined
@suresafe
Copy link
Author

Error can be overcome with 1 change to genpy.py (win32com/client)

Add after line 848

    print('from decimal import Decimal', file=self.file)

Additional checking will need done on the specific COM object I have to see if it operates correctly.

@mhammond
Copy link
Owner

Thanks for the report - please let me if you testing shows that is the only change needed.

@suresafe
Copy link
Author

I'm still testing to be sure, but so far this does seem to work.

I also found an interesting comment at http://docs.activestate.com/activepython/3.2/pywin32/html/com/win32com/readme.htm under "Important Currency changes"

Up until build 212, code could set pythoncom.future_currency = True to force use of the decimal module, with a warning issued otherwise. In builds 213 and later, the decimal module is unconditionally used when pythoncon returns you a currency value.

Python 2.3 does not ship with a decimal module. For this reason, win32com/decimal_23.py is supplied with pywin32. This is a clone of the decimal module, but will only be used when import decimal fails. If you want to use this functionality in your Python 2.3 programs, you should write something similar to:

try:
import decimal
except ImportError:
import win32com.decimal_23 as decimal
val = decimal.Decimal("123.45")

@junkmd
Copy link

junkmd commented Dec 8, 2024

I have probably encountered the same kind of problem.

Traceback (most recent call last):
  File "C:\hostedtoolcache\windows\Python\3.10.11\x64\lib\site-packages\win32com\client\gencache.py", line 261, in GetModuleForCLSID
    __import__(sub_mod_name)
ModuleNotFoundError: No module named 'win32com.gen_py.6BAA1C79-4BA0-47F2-9AD7-D2FFB1C0F3E3x0x1x0.DTestDispServer'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\a\comtypes\comtypes\comtypes\test\test_dispinterface.py", line 36, in test_win32com_ensure_dispatch
    d = EnsureDispatch("TestDispServerLib.TestDispServer")
  File "C:\hostedtoolcache\windows\Python\3.10.11\x64\lib\site-packages\win32com\client\gencache.py", line 636, in EnsureDispatch
    GetModuleForCLSID(disp_clsid)
  File "C:\hostedtoolcache\windows\Python\3.10.11\x64\lib\site-packages\win32com\client\gencache.py", line 270, in GetModuleForCLSID
    makepy.GenerateChildFromTypeLibSpec(sub_mod, info)
  File "C:\hostedtoolcache\windows\Python\3.10.11\x64\lib\site-packages\win32com\client\makepy.py", line 374, in GenerateChildFromTypeLibSpec
    __import__("win32com.gen_py." + dir_name + "." + child)
  File "C:\Users\RUNNER~1\AppData\Local\Temp\gen_py\3.10\6BAA1C79-4BA0-47F2-9AD7-D2FFB1C0F3E3x0x1x0\DTestDispServer.py", line 28, in <module>
    class DTestDispServer(DispatchBaseClass):
  File "C:\Users\RUNNER~1\AppData\Local\Temp\gen_py\3.10\6BAA1C79-4BA0-47F2-9AD7-D2FFB1C0F3E3x0x1x0\DTestDispServer.py", line 48, in DTestDispServer
    def do_cy(self, value=Decimal('32.78')):
NameError: name 'Decimal' is not defined

Please refer to this for the reproducer: enthought/comtypes#693 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants