Jump to content
Sign in to follow this  
Celebr0

Python4Delphi Crash immediately after launch

Recommended Posts

Hello, after the last update 5 days ago, python4delphi crashes immediately after launch:

 



2024-09-29_022606.thumb.jpg.ae4644e9f6d4a2f2f67d716fb1359ee5.jpg

 

Delphi XE2

Share this post


Link to post

Which version of P4D are you using?  Getit or  github and if the latte from which repo?

Which version of python?

 

Could you post a minimal project that reproduces the crash?

Share this post


Link to post
23 minutes ago, pyscripter said:

Which version of P4D are you using?  Getit or  github and if the latte from which repo?

Which version of python?

 

Could you post a minimal project that reproduces the crash?

1. https://github.com/pyscripter/python4delphi

2. Python version 3.12.1

3. Demo36 

Share this post


Link to post

This was not related to the latest revisions.   There was an issue in the Demo related to PythonEngine being destroyed before the threads terminated.

This is now fixed in version control.   Please try with the latest release and report if the issue has been fixed.

Edited by pyscripter

Share this post


Link to post
2 hours ago, pyscripter said:

This was not related to the latest revisions.   There was an issue in the Demo related to PythonEngine being destroyed before the threads terminated.

This is now fixed in version control.   Please try with the latest release and report if the issue has been fixed.

No, the problem is not in demo36, but in pythonengine.pas itself, I tried my ready-made projects, which worked and are now working with previous versions of python4delphi.

The problem most likely manifested itself after editing:

Paths := string(FPythonPath).Split([PathSep]);

on

Paths := SplitString(string(FPythonPath), PathSep);
When previous versions did not work because of this syntax, I myself tried to fix it on SplitString and immediately such crashes appeared last work version from Delphi XE2 https://github.com/pyscripter/python4delphi/commit/46cd7a66dd9a249b3bc6bac9c2be611f2bde2069

UPD:

Most likely I think that there is an incorrect conversion here: 

PyWideStringList_Append(PWSL, PWCharT(StringToWCharTString(Paths))); 

Please study this demo TStringDynArray is not friendly with such transformations, the output turns out to be crooked

 

program Project2;
 
{$APPTYPE CONSOLE}
 
uses
  System.SysUtils, System.StrUtils, System.Types;
 
var
s:string;
ar:TStringDynArray;
begin
s:='python4 delphi';
ar:=splitstring(s,' ');
writeln(PansiChar(ar));
writeln(PChar(ar));
writeln(PansiChar(ansistring(ar)));
readln;
end.

 

Edited by Celebr0

Share this post


Link to post
3 hours ago, Celebr0 said:

PyWideStringList_Append(PWSL, PWCharT(StringToWCharTString(Paths))); 

There is no such code in P4D.  Instead:

 

PyWideStringList_Append(PWSL, PWCharT(StringToWCharTString(Paths[I])));

Your code sample above makes no sense.  SplitString returns a string array.   So code like this:

 

writeln(PansiChar(ansistring(ar)));

should contain array indices (e.g. ar[1]).

 

And in any case that code is only executed if you try to set the python path and has been around for quite a while.  It is not executed for example by any of the demos.

With the latest change Demo36 should work without issues. Again please provide a minimal project that produces errors. 

Edited by pyscripter
  • Like 1

Share this post


Link to post
6 hours ago, pyscripter said:

There is no such code in P4D.  Instead:

 


PyWideStringList_Append(PWSL, PWCharT(StringToWCharTString(Paths[I])));

Your code sample above makes no sense.  SplitString returns a string array.   So code like this:

 


writeln(PansiChar(ansistring(ar)));

should contain array indices (e.g. ar[1]).

 

And in any case that code is only executed if you try to set the python path and has been around for quite a while.  It is not executed for example by any of the demos.

With the latest change Demo36 should work without issues. Again please provide a minimal project that produces errors. 

Yes, I was really mistaken and the error is not where I previously thought the error was with LoadDll:

 

procedure CreatePyEngine;
begin
  PythonEngine := TPythonEngine.Create(nil);
  PythonEngine.LoadDll; // CRASH HERE
  //TPythonThread.Py_Begin_Allow_Threads;
end;

 

Share this post


Link to post
5 hours ago, Celebr0 said:

the error is not where I previously thought

That's why we debug programs stepping through the code,  To know exactly where the error occurs.

 

And what do you mean crash?  Cannot you debug and step into LoadDLL?

Or set FatalMsgDlg to True and FatalAbort to False.   Do you get an error message?

 

Most likely the python dll fails to load.  The most common reason:

Quote

 

32-bit Delphi applications only work with 32-versions of Python and 64-bit Delphi applications only work with 64-bit versions of Python

Are you compiling for 64bits?

For more details please read FindingPython · pyscripter/python4delphi Wiki (github.com)

 

Also, could you update python to 3.12.3 and see whether it makes a difference?

Needless to say that I cannot reproduce the issue here.

Edited by pyscripter

Share this post


Link to post
1 hour ago, pyscripter said:

That's why we debug programs stepping through the code,  To know exactly where the error occurs.

 

And what do you mean crash?  Cannot you debug and step into LoadDLL?

Or set FatalMsgDlg to True and FatalAbort to False.   Do you get an error message?

 

Most likely the python dll fails to load.  The most common reason:

Are you compiling for 64bits?

For more details please read FindingPython · pyscripter/python4delphi Wiki (github.com)

 

Also, could you update python to 3.12.3 and see whether it makes a difference?

Needless to say that I cannot reproduce the issue here.

I was wondering if this error could be related to the fact that I am using Python under Windows 7  https://github.com/adang1345/PythonWin7 ?

Although this version https://github.com/pyscripter/python4delphi/commit/46cd7a66dd9a249b3bc6bac9c2be611f2bde2069 python4delphi works successfully with this !

 

Share this post


Link to post

Possibly.   There have been a lot of changes in the last six months.  I am afraid support for running unofficial newer versions of python on Windows 7 is outside the scope of the P4D project.  

It might help though if you debug and report at which exact point the error occurs.

Edited by pyscripter

Share this post


Link to post
38 minutes ago, pyscripter said:

Possibly.   There have been a lot of changes in the last six months.  I am afraid support for running unofficial newer versions of python on Windows 7 is outside the scope of the P4D project.  

It might help though if you debug and report at which exact point the error occurs.

I can’t debug it because it’s crashing here please fix it somehow or try a virtual machine with windows 7 + delphi XE2

Share this post


Link to post
11 minutes ago, Celebr0 said:

can’t debug it because it’s crashing here

Just step into LoadDLL and observe where exactly the error occurs.

 

11 minutes ago, Celebr0 said:

please fix it somehow

I am not a magician.

11 minutes ago, Celebr0 said:

try a virtual machine with windows 7 + delphi XE2

As I said, python 3.12 + windows 7 + delphi XE2 is not supported by python and by P4D.  If you can come up with a fix I would consider implementing it.

Edited by pyscripter

Share this post


Link to post
1 hour ago, pyscripter said:

Just step into LoadDLL and observe where exactly the error occurs.

 

I am not a magician.

As I said, python 3.12 + windows 7 + delphi XE2 is not supported by python and by P4D.  If you can come up with a fix I would consider implementing it.

Hello debugger brought me here:

procedure TPythonEngine.AfterLoad;
begin
  inherited;
  Initialize;
end;

If I comment out Initialize; then there is no crash

Share this post


Link to post
3 minutes ago, pyscripter said:

So why don't you step in Initialize?

I don’t understand how and where initialization is performed, you should know this

Share this post


Link to post
17 minutes ago, Celebr0 said:

I don’t understand how and where initialization is performed, you should know this

You do not need to understand anything,  Just place a breakpoint on the Initialize statement and after the debugger stops there, keep pressing F7 (step in) until the error occurs.

 

Anyway, I have downloaded the embedded version of python 3.12.6 64bits from  https://github.com/adang1345/PythonWin7, extracted the zip file in a directory 

Quote

c:\python-3.12.6-embed-amd64

and used the following properties in PythonEngine:

 

  DllPath  C:\python-3.12.6-embed-amd64
  PythonEngine.RegVersion : 3.12
  PythonEngine.DllName : python312.dll
  PythonEngine.UseLastKnownVersion : False

 

It seems to work fine on a Windows 11 machine.  You can try the same on your windows 7 machine.

 

Edited by pyscripter

Share this post


Link to post
10 minutes ago, pyscripter said:

You do not need to understand anything,  Just place a breakpoint on the Initialize statement and after the debugger stops there, keep pressing F7 (step in) until the error occurs.

 

Anyway, I have downloaded the embedded version of python 3.12.6 64bits from  https://github.com/adang1345/PythonWin7, extracted the zip file in a directory 

and used the following properties in PythonEngine:

 

  DllPath  C:\python-3.12.6-embed-amd64
  PythonEngine.RegVersion : 3.12
  PythonEngine.DllName : python312.dll
  PythonEngine.UseLastKnownVersion : False

 

It seems to work fine on a Windows 11 machine.  You can try the same on your windows 7 machine.

 

I guess I found something CRASH in AssignPyFlags(Config); from here:

 

var
  i : Integer;
  Config: PyConfig;
  Status: PyStatus;
  ErrMsg: string;
begin
  if Assigned(gPythonEngine) then
    raise Exception.Create('There is already one instance of TPythonEngine running' );

  gPythonEngine := Self;

  FIORedirected := False;
  if FInExtensionModule then
    FInitialized := True
  else
  begin
    // Fills Config with zeros and then sets some default values
    if pfIsolated in FPyFlags then
      PyConfig_InitIsolatedConfig(Config)
    else
      PyConfig_InitPythonConfig(Config);
    try
       AssignPyFlags(Config); // CRASH HERE

      // Set programname and pythonhome if available
      if FProgramName <> '' then
        PyConfig_SetString(Config,
          PPWcharT(PByte(@Config) + ConfigOffests[MinorVersion, TConfigFields.program_name]),
          PWCharT(StringToWCharTString(FProgramName)));
      if FPythonHome <> '' then
        PyConfig_SetString(Config,
          PPWcharT(PByte(@Config) + ConfigOffests[MinorVersion, TConfigFields.home]),
          PWCharT(StringToWCharTString(FPythonHome)));
      // Set venv executable if available
      if FVenvPythonExe <> '' then
        PyConfig_SetString(Config,
          PPWcharT(PByte(@Config) + ConfigOffests[MinorVersion, TConfigFields.executable]),
          PWCharT(StringToWCharTString(FVenvPythonExe)));

      // Set program arguments (sys.argv)
      SetProgramArgs(Config);

      // PythonPath
      SetPythonPath(Config);

 

Share this post


Link to post
On 9/30/2024 at 5:09 AM, pyscripter said:

So why don't you step in Initialize?

Hello, the problem has not yet been resolved. I downloaded the newest version of Python4Delphi from GitHub and Python 3.13.0, but it still crashes 

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  

×