win32com: Problem with dynamic Dispatch

Timo Linna timo.zap.linna at iki.zap.fi.invalid
Mon Mar 17 07:47:44 EST 2003


I'm using Microsoft's MSXML 4.0 COM library with ActivePython 2.2.2. For some
odd reason, dynamically dispatched COM objects (XML nodes) behave differently
with and without generated type info cache.

The test application is below. Don't forget to delete your gen_py cache before
running it..

# --- BEGIN
import win32com.client, unittest, sys, shutil
from os.path import dirname, join, isdir

class MSXMLTest(unittest.TestCase):

    def _getNodes(self):
        dom = win32com.client.dynamic.Dispatch('Msxml2.DOMDocument.4.0')
        assert dom.loadXML('''<?xml version='1.0'?>
            <greetings>
                <hello from='John'>Hi!</hello>
                <hello from='Timo'>Terve!</hello>
            </greetings>
            ''')
        return dom.selectNodes('//hello') # IXMLDOMNodeList of IXMLDOMNodes

    def _generateTypeInfo(self):
        """Generate Python support for Msxml2.DOMDocument 4.0 Type Library."""
        win32com.client.gencache.EnsureModule(
            '{F5078F18-C551-11D3-89B9-0000F81FE221}',0, 4, 0, bForDemand=1)

    def _testNodesWithWhileLoop(self):
        nodes = self._getNodes()
        node = nodes.nextNode()
        while node:
            assert node.getAttribute('from')
            node = nodes.nextNode()

    def _testNodesWithForLoop(self):
        nodes = self._getNodes()
        for node in nodes:
            assert node.getAttribute('from')

    def test1_ForcedLateBound(self):
        self._testNodesWithWhileLoop() # While loop is ok, but..

        # .. the for loop fails!
        # AttributeError: 'NoneType' object has no attribute 'getAttribute'
        self._testNodesWithForLoop()

    def test2_ForcedLateBoundWithTypeInfo(self):
        self._generateTypeInfo()
        self._testNodesWithForLoop() # Now the for loop is ok, but..

        # ..the while loop fails!
        # AttributeError: getAttribute
        self._testNodesWithWhileLoop()

print 'Executing MSXML test with Python', sys.version

try:
    unittest.main()
finally:
    # Make sure gen_py cache is erased after test.
    gen_py_path = join(dirname(win32com.__file__), 'gen_py')
    if isdir(gen_py_path):
        print 'Removing directory:', gen_py_path
        shutil.rmtree(gen_py_path)

# --- END

BR,
    timo







More information about the Python-list mailing list