msxml3 and IStream interface

Mark Hammond MarkH at ActiveState.com
Sat Dec 30 02:24:07 CET 2000


jj wrote:

> I want to use MSXML3 library for writing xml in file, but I dont know how to
> implement IStream interface in acceptable way. In following source is
> stripped Stream class from python examples. Too prove that all other code is
> working there is working example of saving in COM structured document file.
> I would also appreciate comments about my coding, especially how to get
> another interface pointer from object. I found only working solution using
> king of import typelib with makepy.py utility.

You are certainly on the right track.  Note that IStream is somewhat 
special, in that the win32com package knows about this explicitly, so 
there is no need for makepy support for this interface.  You _do_ need 
makepy support for the XML lib, as you have done.  (you probably knew 
all that - just being clear)

>  def Read(self, amount): return 0

Read will be expecing a string to come back.  This will be raising an 
"internal exception", which you may only see when using the special 
debugging features.

*sigh*.  Looking at the code, you wont even get a decent error message 
:-(  Probably "TypeError: len() of unsized object" rather than a more 
useful "must be a string"

>  def Write(self, data): return 0

This should be

def Write(self, data): return len(data)

for a workable stub implementation.


> 
> class SaxWriter: #wrapper around xmlwriter
>  def __init__(self):
>   self.elements = []
>   self.wr = win32com.client.Dispatch("Msxml2.MXXMLWriter")
>   # !!! reason for importing typelibrary, could be done without importing
> typelib !!!!
>   self.ch =msxml_TLB.IVBSAXContentHandler(self.wr)
>   self.atrs = win32com.client.Dispatch("Msxml2.SAXAttributes")

Dig up some recent posts from myself and Alex Martelli on "makepy" and 
"gencache", and you should find answers to cleaning this up.

> def test():
>  s = SaxWriter()
>  st = Stream()  # !!!! its not working

You need to "wrap" the object.

Try:

   st = win32com.server.util.wrap(Stream())

And to see any internal errors, you can try:

   useDispatcher = win32com.server.dispatcher.DefaultDebugDispatcher
   st = win32com.server.util.wrap(Stream(), useDispatcher=useDispatcher)

then open the "Trace collector debugging tool" from Pythonwin's tools 
menu to see debug output.

For more info, you've really gotta buy my and Andy's book, or trawl deja.

Mark.




More information about the Python-list mailing list