Embedding Python: Creating Python Class from Application

Kakacek krivanek.jirka at seznam.cz
Thu Jan 12 11:32:16 EST 2006


In the mean time I did this. It works however, it creates memory leaks.
Any idea what could be wrong?

procedure TForm1.PyCreateInstance(Sender: TObject; PSelf, Args:
PPyObject;
  var Result: PPyObject);
  var
    BClassName: PChar;
    BInstName: PChar;
    //
    BB: Boolean;
    BModule: PPyObject;
    BClass: PPyObject;
    BInstance: PPyObject;
begin
  // create_instance
  PE.PyArg_ParseTuple(Args, 'ss:create_instance', [@BClassName,
@BInstName]);
  PE.CheckError;
  // Find module
  BModule := PE.FindModule('__main__');
  PE.CheckError;
  if BModule <> nil then begin
    // Find class object in the module
    BClass := PE.PyObject_GetAttrString(BModule, BClassName);
    PE.CheckError;
    if BClass <> nil then begin
      // Check it really is a class object
      BB := PE.PyClass_Check(BClass);
      PE.CheckError;
      if BB then begin
        // Calling the class object (to create a new instance)
        BInstance := PE.PyObject_CallObject(BClass, nil);
        PE.CheckError;
        if BInstance <> nil then begin
          // Add the instance to the module object
          PE.PyObject_SetAttrString(BModule, BInstName, BInstance);
          PE.CheckError;
        end;
      end;
      PE.Py_DECREF(BClass);
    end;
  end;
  //
  Result := PE.ReturnNone;
  PE.CheckError;
end;




More information about the Python-list mailing list