Module initialization problem when using COM

Robin Boerdijk robin.boerdijk at nl.origin-it.com
Fri May 21 08:51:20 EDT 1999


We have the following problem: The initialization function of the ILU
extension module gets called more than once when used within a Python COM
server and called from Visual Basic (and this makes ILU crash).

To investigate the problem, we built the following small extension module,
called "empty", that only has an initialization function that counts the
number of times it gets called and logs this into a file:

/* empty.cpp */

#include <stdio.h>
#include "Python.h"

int counter = 0;

static PyMethodDef empty_methods[] = {
  {NULL, NULL}           /* sentinel */
};

void initempty()
{
  FILE* log;

  log = fopen("c:\\empty.log", "a");
  fprintf(log, "Counter: %d\n", counter);
  fclose(log);

  Py_InitModule("empty", empty_methods);

  counter++;
};

We also have a simple Python COM server that imports this module and exposes
a simple Hello method to be called from VB:

# testcomserver.py

import empty

class HelloWorld:

 _reg_clsid_ = "{112077C1-078D-11D3-8427-00600894A6FB}"
 _reg_desc_ = "Python Test COM Server"
 _reg_progid_ = "Python.TestServer"
 _reg_class_spec_ = "testcomserver.HelloWorld"
 _public_methods_ = ['Hello']

 def Hello(self, who):

  return "Name: " + str(who)

if __name__=='__main__':

 import win32com.server.register
 win32com.server.register.UseCommandLine(HelloWorld)

Now, we call the Hello() method from the following Visual Basic program by
clicking the button a few times.

' testcomclient

Private Sub Command1_Click()
    Dim test As Object
    Set test = CreateObject("python.testserver")
    MsgBox test.Hello("Mark")
End Sub

The initempty() function from the Python extension module now gets called
multiple times as we can see from the c:\empty.log file:

Counter: 0
Counter: 1
Counter: 2

Again, for ILU this causes the extension module to crash. Is this a VB,
Python or COM problem ? Any ideas how to solve it ?

Robin.







More information about the Python-list mailing list