[python-win32] Need advice on iterator implementation in scripting host

Pulsonix Tech Support support at pulsonix.com
Tue Apr 17 09:57:53 CEST 2012


Our axscript host implementation is based around the old "ActiveScriptHost"
class by Ernest Laurentin. Our base collection class is a template class
implements _NewEnum, Count and other functions as per this dispatch map:

SetNotSupported, VT_UNKNOWN)
DISP_PROPERTY_EX_ID(C##BaseName, "Count", DISP_ID_COUNT, GetCount,
SetNotSupported, VT_I2)
DISP_DEFVALUE(C##BaseName, "Item")

I'm not sure where this came from now, probably from one of the MS MFC
samples, possibly XRTFrame. The implementation of Next does "return
ResultFromScode(S_FALSE)" on reaching the end of the list of available

The debug tracing inside our app shows that all the expected objects are
being created and destroyed, so it may be something wrong in our internal
'wiring' that relates the scriptable objects to our internal data model
objects that is not properly tidying up.

We have yet to prove that it really is the absence of a StopIteration
exception that is causing the app crash, so we are continuing to unpick the

Best regards,

-----Original Message-----
From: Mark Hammond [mailto:skippy.hammond at gmail.com] 
Sent: 17 April 2012 02:15
To: support at pulsonix.com
Cc: python-win32 at python.org
Subject: Re: [python-win32] Need advice on iterator implementation in
scripting host

On 13/04/2012 9:14 PM, Pulsonix Tech Support wrote:
> We have an ActiveX scripting host implementation in our application 
> (written in C++), so that users can run scripts to access our data 
> from inside the application.
> Current problem is that iterating from Python script doesn't work right.
> You can iterate through a collection, but attempts to do this a second 
> time cause application crashes or unexpected behaviour. Seems to work 
> okay from VBscript though.
> This may be because we are not raising StopIteration exception when
> iterator.Next() has no items left. But I can't find any information 
> about how to do this from a C++ scripting host implementation.

win32com has some iterator support built in, but was done way before Python
itself had proper iterators so probably isn't ideal.  The most basic support
is enabled when the object has a "Count" property and an
Item() method.  Some support for IEnumVARIANT also exists, but it requires
you to wrap the object in win32com.client.util.Iterator - which I should
possibly revisit as we might be able to make that seamless.

How are you attempting to expose iteration?


More information about the python-win32 mailing list