Jump to content
Registration disabled at the moment Read more... ×
Sign in to follow this  
Jacek Laskowski

TPyDelphiWrapper and initialization/finalization issue

Recommended Posts

I have code that gets an initialized python engine and creates a wrapper around TMemoryStream for its needs:

 

procedure TMainForm.MainTest(PyEngine: TPythonEngine; PyModule: TPythonModule);
var
  Stream: TMemoryStream;
  PyStream: PPyObject;
  PyDelphiWrapper: TPyDelphiWrapper;
begin
  Stream := TMemoryStream.Create();
  PyDelphiWrapper := TPyDelphiWrapper.Create(nil);
  try
    PyDelphiWrapper.Engine := PyEngine;
    PyDelphiWrapper.Module := PyModule;
    PyDelphiWrapper.Initialize; <-- generates an exception on the next execution, when previously there was an error in the python runtime (ExecString)

    PyStream := PyDelphiWrapper.Wrap(Stream, soReference);
    try
      PyModule.SetVar('streamOut', PyStream);
    finally
      PyEngine.Py_DecRef(PyStream);
    end;

    try
      PyEngine.ExecStrings(fSrcLines, 'delphibridge'); <--- Problem starts when any error in python syntax generates an exception here
      Stream.SaveToFile(edtOutputXML.Text);
    finally
      PyEngine.Py_IncRef(PyEngine.Py_None);
      PyModule.SetVar('streamOut', PyEngine.Py_None);
    end;
  finally
    PyDelphiWrapper.Free;
    Stream.Free;
  end;
end;

And now the problem is that when there is no error in the Python script then I can call this method repeatedly without any problem.
However, when there is an exception in the Python runtime then calling this method again throws a serious error already at the PyDelphiWrapper.Initialize line:

 

ExceptionMessage="Access violation at address 67012FDA. Read of address 00000054"
ExceptionName="EAccessViolation"
ExceptionDisplayName="$C0000005"

 

I'm guessing that I have some problem here with the correct management of TPyDelphiWrapper object (finalization?), but I can't understand what is the reason. Please give me a hint.

Edited by Jacek Laskowski

Share this post


Link to post

There is no need to create and destroy TPyDelphiWrapper.   Just create it once with the engine.  It will be automatically initialized when the python dll is loaded.

Share this post


Link to post
32 minutes ago, pyscripter said:

There is no need to create and destroy TPyDelphiWrapper.   Just create it once with the engine.  It will be automatically initialized when the python dll is loaded.

And can I use one instance of TPyDelphiWrapper for multiple delphi objects in different python scripts?

Share this post


Link to post
2 hours ago, Jacek Laskowski said:

And can I use one instance of TPyDelphiWrapper for multiple delphi objects in different python scripts?

Yes you can.

  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×