Browse Source

fix Python version on macOS

Johann ELSASS 1 year ago
parent
commit
fcb54154cb
3 changed files with 21 additions and 10 deletions
  1. 1 2
      lazpaint/lazpaintinstance.pas
  2. 6 4
      lazpaint/uprocessauto.pas
  3. 14 4
      lazpaint/upython.pas

+ 1 - 2
lazpaint/lazpaintinstance.pas

@@ -1901,8 +1901,7 @@ begin
     p.OnCommand:=@PythonScriptCommand;
     p.OnBusy := @PythonBusy;
     p.OnWarning:= @PythonWarning;
-    p.Run(AFilename);
-    if p.ErrorText<>'' then
+    if not p.Run(AFilename) and (p.ErrorText<>'') then
     begin
       fError := TForm.Create(nil);
       try

+ 6 - 4
lazpaint/uprocessauto.pas

@@ -24,11 +24,11 @@ type
 var
   AutomationEnvironment: TStringList;
 
-procedure RunProcessAutomation(AExecutable: string; AParameters: array of string;
+function RunProcessAutomation(AExecutable: string; AParameters: array of string;
   out ASendLine: TSendLineMethod;
   AOnReceiveOutput: TReceiveLineEvent;
   AOnReceiveError: TReceiveLineEvent;
-  AOnBusy: TBusyEvent);
+  AOnBusy: TBusyEvent): integer;
 
 implementation
 
@@ -45,11 +45,11 @@ type
     procedure SendLine(const ALine: RawByteString);
   end;
 
-procedure RunProcessAutomation(AExecutable: string; AParameters: array of string;
+function RunProcessAutomation(AExecutable: string; AParameters: array of string;
   out ASendLine: TSendLineMethod;
   AOnReceiveOutput: TReceiveLineEvent;
   AOnReceiveError: TReceiveLineEvent;
-  AOnBusy: TBusyEvent);
+  AOnBusy: TBusyEvent): integer;
 
 type
   TReceiveBuffer = record
@@ -120,6 +120,7 @@ var
   i: integer;
   shouldSleep: Boolean;
 begin
+  result := 0;
   p := TAutomatedProcess.Create(nil);
   ASendLine := @p.SendLine;
   try
@@ -145,6 +146,7 @@ begin
     end;
     Receive(p.Output, Output);
     Receive(p.Stderr, Error);
+    result := p.ExitCode;
   finally
     p.Free;
   end;

+ 14 - 4
lazpaint/upython.pas

@@ -10,6 +10,9 @@ uses
 
 const
   DefaultPythonBin = {$IFDEF WINDOWS}'pyw'{$ELSE}'python3'{$ENDIF};
+  {$IFDEF DARWIN}
+  UserPythonBin = '/usr/local/bin/python3';
+  {$ENDIF}
 
 type
   TReceiveLineEvent = procedure(ASender: TObject; ALine: UTF8String) of object;
@@ -38,7 +41,7 @@ type
     function CheckScriptAndDependencySafe(AFilename: UTF8String; APythonVersion: integer): boolean;
   public
     constructor Create(APythonBin: string = DefaultPythonBin);
-    procedure Run(AScriptFilename: UTF8String; APythonVersion: integer = 3);
+    function Run(AScriptFilename: UTF8String; APythonVersion: integer = 3): boolean;
     class function DefaultScriptDirectory: string;
     property OnOutputLine: TReceiveLineEvent read FOnOutputLine write FOnOutputLine;
     property OnError: TReceiveLineEvent read FOnError write FOnError;
@@ -618,6 +621,10 @@ end;
 constructor TPythonScript.Create(APythonBin: string);
 begin
   FPythonBin := APythonBin;
+  {$IFDEF DARWIN}
+  if (FPythonBin = 'python3') and FileExists(UserPythonBin) then
+    FPythonBin:= UserPythonBin;
+  {$ENDIF}
   FPythonVersion:= GetPythonVersion(FPythonBin);
 end;
 
@@ -641,20 +648,23 @@ begin
     val(copy(PythonVersion,1,posDot-1), result, errPos);
 end;
 
-procedure TPythonScript.Run(AScriptFilename: UTF8String;
-  APythonVersion: integer);
+function TPythonScript.Run(AScriptFilename: UTF8String;
+  APythonVersion: integer): boolean;
+var exitCode: integer;
 begin
+  result := false;
   if not CheckScriptAndDependencySafe(AScriptFilename, APythonVersion) then exit;
   FLinePrefix := '';
   FFirstOutput:= true;
   AutomationEnvironment.Values['PYTHONPATH'] := DefaultScriptDirectory;
   AutomationEnvironment.Values['PYTHONIOENCODING'] := 'utf-8';
   try
-    RunProcessAutomation(FPythonBin, ['-u', AScriptFilename], FPythonSend, @PythonOutput, @PythonError, @PythonBusy);
+    exitCode := RunProcessAutomation(FPythonBin, ['-u', AScriptFilename], FPythonSend, @PythonOutput, @PythonError, @PythonBusy);
   finally
     AutomationEnvironment.Clear;
   end;
   FPythonSend := nil;
+  result := exitCode = 0;
 end;
 
 class function TPythonScript.DefaultScriptDirectory: string;