From 09fcaf6b250dd25bec0d6442d91b3656a01cab97 Mon Sep 17 00:00:00 2001 From: Idan Miara Date: Wed, 5 Jun 2024 10:12:49 +0300 Subject: [PATCH 1/3] PythonEngineOnAfterInitialization --- Source/PythonEngine.pas | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Source/PythonEngine.pas b/Source/PythonEngine.pas index 9e0343da..1f764a69 100644 --- a/Source/PythonEngine.pas +++ b/Source/PythonEngine.pas @@ -3084,6 +3084,9 @@ procedure MaskFPUExceptions(ExceptionsMasked : boolean; function CleanString(const s : AnsiString; AppendLF : Boolean = True) : AnsiString; overload; function CleanString(const s : UnicodeString; AppendLF : Boolean = True) : UnicodeString; overload; +var + PythonEngineOnAfterInitialization : TNotifyEvent = nil; + implementation uses @@ -4718,6 +4721,8 @@ procedure TPythonEngine.AfterLoad; begin inherited; Initialize; + if Assigned(PythonEngineOnAfterInitialization) then + PythonEngineOnAfterInitialization( Self ); end; procedure TPythonEngine.BeforeLoad; From dd39916fe855d5cfed03240447a29c12c1f540ce Mon Sep 17 00:00:00 2001 From: Idan Miara Date: Wed, 7 May 2025 11:33:44 +0300 Subject: [PATCH 2/3] PythonEngine.pas - add GetPythonPathAsStrings --- Source/PythonEngine.pas | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Source/PythonEngine.pas b/Source/PythonEngine.pas index 1f764a69..2bee411c 100644 --- a/Source/PythonEngine.pas +++ b/Source/PythonEngine.pas @@ -2033,6 +2033,8 @@ TPythonTraceback = class TPythonType = class; //forward declaration + TWhichPythonPaths = (wppAll, wppOnlyCustom, wppOnlyBase); + {$IF not Defined(FPC) and (CompilerVersion >= 23)} [ComponentPlatformsAttribute(pidSupportedPlatforms)] {$IFEND} @@ -2069,6 +2071,7 @@ TPythonEngine = class(TPythonInterface) FPyDateTime_DateTimeTZType: PPyObject; protected + FBasePythonPath: TStrings; procedure Initialize; procedure Finalize; procedure AfterLoad; override; @@ -2091,6 +2094,7 @@ TPythonEngine = class(TPythonInterface) // Constructors & Destructors constructor Create(AOwner: TComponent); override; destructor Destroy; override; + procedure GetPythonPathAsStrings(Strings: TStrings; WhichPaths: TWhichPythonPaths=wppAll); // Public methods procedure SetPythonHome(const PythonHome: UnicodeString); @@ -4643,6 +4647,7 @@ constructor TPythonEngine.Create(AOwner: TComponent); FUseWindowsConsole := False; FPyFlags := DEFAULT_FLAGS; FDatetimeConversionMode := DEFAULT_DATETIME_CONVERSION_MODE; + FBasePythonPath := TStringList.Create; if csDesigning in ComponentState then begin for i := 0 to AOwner.ComponentCount - 1 do @@ -4665,6 +4670,7 @@ destructor TPythonEngine.Destroy; FClients.Free; FInitScript.Free; FTraceback.Free; + FBasePythonPath.Free; inherited; end; @@ -4717,10 +4723,36 @@ procedure TPythonEngine.Finalize; FPyDateTime_DateTimeTZType := nil; end; +procedure TPythonEngine.GetPythonPathAsStrings(Strings: TStrings; WhichPaths: TWhichPythonPaths); +var + list, obj2: PPyObject; + i: integer; + item: String; + ReturnAll: boolean; +begin + case WhichPaths of + wppAll, wppOnlyCustom: begin + ReturnAll := WhichPaths = wppAll; + Strings.Clear; + list := self.PySys_GetObject('path'); + for i := 0 to PyList_Size(list)-1 do begin + obj2 := PyList_GetItem(list, i); + item := PyObjectAsString(obj2); + if ReturnAll or not FBasePythonPath.Contains(item) then + Strings.Add(item); + end; + end; + wppOnlyBase: begin + Strings.SetStrings(FBasePythonPath) + end; + end; +end; + procedure TPythonEngine.AfterLoad; begin inherited; Initialize; + GetPythonPathAsStrings(FBasePythonPath, wppAll); if Assigned(PythonEngineOnAfterInitialization) then PythonEngineOnAfterInitialization( Self ); end; From d804599cddf783ec74e6874b4df7df619eb9c849 Mon Sep 17 00:00:00 2001 From: Idan Miara Date: Thu, 1 Jan 2026 23:17:27 +0200 Subject: [PATCH 3/3] PythonEngine - use TArray --- Source/PythonEngine.pas | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-) diff --git a/Source/PythonEngine.pas b/Source/PythonEngine.pas index 2bee411c..1e746f43 100644 --- a/Source/PythonEngine.pas +++ b/Source/PythonEngine.pas @@ -2071,7 +2071,7 @@ TPythonEngine = class(TPythonInterface) FPyDateTime_DateTimeTZType: PPyObject; protected - FBasePythonPath: TStrings; + FBasePythonPath: TArray; procedure Initialize; procedure Finalize; procedure AfterLoad; override; @@ -2094,7 +2094,7 @@ TPythonEngine = class(TPythonInterface) // Constructors & Destructors constructor Create(AOwner: TComponent); override; destructor Destroy; override; - procedure GetPythonPathAsStrings(Strings: TStrings; WhichPaths: TWhichPythonPaths=wppAll); + function GetPythonPathAsStrings(WhichPaths: TWhichPythonPaths=wppAll): TArray; // Public methods procedure SetPythonHome(const PythonHome: UnicodeString); @@ -3106,7 +3106,8 @@ implementation PsAPI, {$ENDIF} {$ENDIF} - Math; + Math, + Generics.Collections; (*******************************************************) (** **) @@ -4647,7 +4648,6 @@ constructor TPythonEngine.Create(AOwner: TComponent); FUseWindowsConsole := False; FPyFlags := DEFAULT_FLAGS; FDatetimeConversionMode := DEFAULT_DATETIME_CONVERSION_MODE; - FBasePythonPath := TStringList.Create; if csDesigning in ComponentState then begin for i := 0 to AOwner.ComponentCount - 1 do @@ -4670,7 +4670,7 @@ destructor TPythonEngine.Destroy; FClients.Free; FInitScript.Free; FTraceback.Free; - FBasePythonPath.Free; + FBasePythonPath := nil; inherited; end; @@ -4723,7 +4723,7 @@ procedure TPythonEngine.Finalize; FPyDateTime_DateTimeTZType := nil; end; -procedure TPythonEngine.GetPythonPathAsStrings(Strings: TStrings; WhichPaths: TWhichPythonPaths); +function TPythonEngine.GetPythonPathAsStrings(WhichPaths: TWhichPythonPaths): TArray; var list, obj2: PPyObject; i: integer; @@ -4733,17 +4733,16 @@ procedure TPythonEngine.GetPythonPathAsStrings(Strings: TStrings; WhichPaths: TW case WhichPaths of wppAll, wppOnlyCustom: begin ReturnAll := WhichPaths = wppAll; - Strings.Clear; list := self.PySys_GetObject('path'); for i := 0 to PyList_Size(list)-1 do begin obj2 := PyList_GetItem(list, i); item := PyObjectAsString(obj2); - if ReturnAll or not FBasePythonPath.Contains(item) then - Strings.Add(item); + if ReturnAll or not TArray.Contains(FBasePythonPath, item) then + Result := Result + [item]; end; end; wppOnlyBase: begin - Strings.SetStrings(FBasePythonPath) + Result := Copy(FBasePythonPath); end; end; end; @@ -4752,7 +4751,7 @@ procedure TPythonEngine.AfterLoad; begin inherited; Initialize; - GetPythonPathAsStrings(FBasePythonPath, wppAll); + FBasePythonPath := GetPythonPathAsStrings(wppAll); if Assigned(PythonEngineOnAfterInitialization) then PythonEngineOnAfterInitialization( Self ); end;