diff --git a/Source/PythonEngine.pas b/Source/PythonEngine.pas index 9e0343da..1e746f43 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: TArray; procedure Initialize; procedure Finalize; procedure AfterLoad; override; @@ -2091,6 +2094,7 @@ TPythonEngine = class(TPythonInterface) // Constructors & Destructors constructor Create(AOwner: TComponent); override; destructor Destroy; override; + function GetPythonPathAsStrings(WhichPaths: TWhichPythonPaths=wppAll): TArray; // Public methods procedure SetPythonHome(const PythonHome: UnicodeString); @@ -3084,6 +3088,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 @@ -3099,7 +3106,8 @@ implementation PsAPI, {$ENDIF} {$ENDIF} - Math; + Math, + Generics.Collections; (*******************************************************) (** **) @@ -4662,6 +4670,7 @@ destructor TPythonEngine.Destroy; FClients.Free; FInitScript.Free; FTraceback.Free; + FBasePythonPath := nil; inherited; end; @@ -4714,10 +4723,37 @@ procedure TPythonEngine.Finalize; FPyDateTime_DateTimeTZType := nil; end; +function TPythonEngine.GetPythonPathAsStrings(WhichPaths: TWhichPythonPaths): TArray; +var + list, obj2: PPyObject; + i: integer; + item: String; + ReturnAll: boolean; +begin + case WhichPaths of + wppAll, wppOnlyCustom: begin + ReturnAll := WhichPaths = wppAll; + 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 TArray.Contains(FBasePythonPath, item) then + Result := Result + [item]; + end; + end; + wppOnlyBase: begin + Result := Copy(FBasePythonPath); + end; + end; +end; + procedure TPythonEngine.AfterLoad; begin inherited; Initialize; + FBasePythonPath := GetPythonPathAsStrings(wppAll); + if Assigned(PythonEngineOnAfterInitialization) then + PythonEngineOnAfterInitialization( Self ); end; procedure TPythonEngine.BeforeLoad;