From 3ea375f0dda28c2f88c44910372a8bafaddd183a Mon Sep 17 00:00:00 2001 From: grig27 Date: Thu, 8 Jun 2017 18:12:43 +0500 Subject: [PATCH 1/9] --- PythonForDelphi/Components/Sources/Core/PythonEngine.pas | 1 + 1 file changed, 1 insertion(+) diff --git a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas index 9900134a..7885c6f3 100644 --- a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas +++ b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas @@ -136,6 +136,7 @@ interface (DllName: 'python31.dll'; RegVersion: '3.1'; APIVersion: 1013; CanUseLatest: True), (DllName: 'python32.dll'; RegVersion: '3.2'; APIVersion: 1013; CanUseLatest: True), (DllName: 'python33.dll'; RegVersion: '3.3'; APIVersion: 1013; CanUseLatest: True), + (DllName: 'python34.dll'; RegVersion: '3.4'; APIVersion: 1013; CanUseLatest: True), (DllName: 'python34.dll'; RegVersion: '3.4'; APIVersion: 1013; CanUseLatest: True) ); {$ENDIF} {$IFDEF LINUX} From 27790e980df956053f6a6366100b7eba90cfab84 Mon Sep 17 00:00:00 2001 From: Grigoriy Date: Wed, 14 Jun 2017 15:24:14 +0500 Subject: [PATCH 2/9] --- PythonForDelphi/Components/Sources/Core/PythonEngine.pas | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas index 7885c6f3..ea9517e6 100644 --- a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas +++ b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas @@ -126,7 +126,7 @@ interface end; const {$IFDEF MSWINDOWS} - PYTHON_KNOWN_VERSIONS: array[1..10] of TPythonVersionProp = + PYTHON_KNOWN_VERSIONS: array[1..11] of TPythonVersionProp = ( (DllName: 'python23.dll'; RegVersion: '2.3'; APIVersion: 1012; CanUseLatest: True), (DllName: 'python24.dll'; RegVersion: '2.4'; APIVersion: 1012; CanUseLatest: True), (DllName: 'python25.dll'; RegVersion: '2.5'; APIVersion: 1013; CanUseLatest: True), @@ -137,7 +137,7 @@ interface (DllName: 'python32.dll'; RegVersion: '3.2'; APIVersion: 1013; CanUseLatest: True), (DllName: 'python33.dll'; RegVersion: '3.3'; APIVersion: 1013; CanUseLatest: True), (DllName: 'python34.dll'; RegVersion: '3.4'; APIVersion: 1013; CanUseLatest: True), - (DllName: 'python34.dll'; RegVersion: '3.4'; APIVersion: 1013; CanUseLatest: True) ); + (DllName: 'python35.dll'; RegVersion: '3.5'; APIVersion: 1013; CanUseLatest: True) ); {$ENDIF} {$IFDEF LINUX} PYTHON_KNOWN_VERSIONS: array[1..10] of TPythonVersionProp = From e6af0a5e8daeab051a764be66ef51035a1e5b02c Mon Sep 17 00:00:00 2001 From: Grigoriy Date: Mon, 15 Jan 2018 15:07:28 +0500 Subject: [PATCH 3/9] Add Delphi Tokyo support --- .../Components/Sources/Core/Definition.Inc | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/PythonForDelphi/Components/Sources/Core/Definition.Inc b/PythonForDelphi/Components/Sources/Core/Definition.Inc index 11515028..42649301 100644 --- a/PythonForDelphi/Components/Sources/Core/Definition.Inc +++ b/PythonForDelphi/Components/Sources/Core/Definition.Inc @@ -314,6 +314,28 @@ {$DEFINE DELPHIXE7_OR_HIGHER} {$DEFINE DELPHIXE8_OR_HIGHER} {$ENDIF} +{$IFDEF VER320} // Delphi Tokyo + {$DEFINE DELPHIXE8} + {$DEFINE DELPHI102T} + {$DEFINE DELPHI4_OR_HIGHER} + {$DEFINE DELPHI5_OR_HIGHER} + {$DEFINE DELPHI6_OR_HIGHER} + {$DEFINE DELPHI7_OR_HIGHER} + {$DEFINE DELPHI8_OR_HIGHER} + {$DEFINE DELPHI2005_OR_HIGHER} + {$DEFINE DELPHI2006_OR_HIGHER} + {$DEFINE DELPHI2007_OR_HIGHER} + {$DEFINE DELPHI2009_OR_HIGHER} + {$DEFINE DELPHI2010_OR_HIGHER} + {$DEFINE DELPHIXE_OR_HIGHER} + {$DEFINE DELPHIXE2_OR_HIGHER} + {$DEFINE DELPHIXE3_OR_HIGHER} + {$DEFINE DELPHIXE4_OR_HIGHER} + {$DEFINE DELPHIXE5_OR_HIGHER} + {$DEFINE DELPHIXE6_OR_HIGHER} + {$DEFINE DELPHIXE7_OR_HIGHER} + {$DEFINE DELPHIXE8_OR_HIGHER} +{$ENDIF} ///////////////////////////////////////////////////////////////////////////// // Python versions ///////////////////////////////////////////////////////////////////////////// From 6fc97ebb3dadfd784427543e371e6724e2ebbe33 Mon Sep 17 00:00:00 2001 From: Grigoriy Date: Mon, 7 Oct 2019 10:54:17 +0500 Subject: [PATCH 4/9] --- PythonForDelphi/Components/Sources/Core/PythonEngine.pas | 2 ++ 1 file changed, 2 insertions(+) diff --git a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas index 8c6ddf7d..5b9c8b87 100644 --- a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas +++ b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas @@ -3596,6 +3596,8 @@ function TPythonInterface.GetQuitMessage : String; function TPythonInterface.GetInitialized: Boolean; begin + if Self = nil then Exit; + if Assigned(Py_IsInitialized) then Result := Py_IsInitialized() <> 0 else From 5fd5c9445415a4a38adea48251c73b24235da75a Mon Sep 17 00:00:00 2001 From: Grigoriy Date: Mon, 7 Oct 2019 11:53:24 +0500 Subject: [PATCH 5/9] --- PythonForDelphi/Components/Sources/Core/PythonEngine.pas | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas index 5b9c8b87..f1faf671 100644 --- a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas +++ b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas @@ -1492,6 +1492,7 @@ TPythonInputOutput = class(TComponent) type TDynamicDll = class(TComponent) private + FFindPythonVersionRegistered: Boolean; function IsAPIVersionStored: Boolean; function IsDllNameStored: Boolean; function IsRegVersionStored: Boolean; @@ -1543,6 +1544,8 @@ TDynamicDll = class(TComponent) property FatalAbort : Boolean read FFatalAbort write FFatalAbort default True; property FatalMsgDlg : Boolean read FFatalMsgDlg write FFatalMsgDlg default True; property UseLastKnownVersion: Boolean read FUseLastKnownVersion write FUseLastKnownVersion default True; + property FindPythonVersionRegistered : Boolean read FFindPythonVersionRegistered + write FFindPythonVersionRegistered default True; property OnAfterLoad : TNotifyEvent read FOnAfterLoad write FOnAfterLoad; property OnBeforeLoad : TNotifyEvent read FOnBeforeLoad write FOnBeforeLoad; property OnBeforeUnload : TNotifyEvent read FOnBeforeUnload write FOnBeforeUnload; @@ -3375,7 +3378,8 @@ function TDynamicDll.GetDllPath : String; {$IFDEF MSWINDOWS} if DLLPath = '' then begin - IsPythonVersionRegistered(RegVersion, Result, AllUserInstall); + if FindPythonVersionRegistered then + IsPythonVersionRegistered(RegVersion, Result, AllUserInstall); end; {$ENDIF} From 35ab9beaf2c4576d4698d0aadfc07a55de7f738e Mon Sep 17 00:00:00 2001 From: Grigoriy Date: Wed, 17 Jun 2020 16:14:09 +0500 Subject: [PATCH 6/9] Add lock python on init. --- .../Components/Sources/Core/PythonEngine.pas | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas index c092488c..0eaadef3 100644 --- a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas +++ b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas @@ -1534,7 +1534,7 @@ TPythonInterface=class(TDynamicDll) function GetQuitMessage : string; override; procedure CheckPython; function GetUnicodeTypeSuffix : string; - + function GetInitialized: Boolean; public // define Python flags. See file pyDebug.h Py_DebugFlag: PInteger; @@ -2280,6 +2280,7 @@ TPythonEngine = class(TPythonInterface) property GlobalVars : PPyObject read FGlobalVars Write SetGlobalVars; property IOPythonModule: TObject read FIOPythonModule; {TPythonModule} published + //property LookOnInit property AutoFinalize: Boolean read FAutoFinalize write FAutoFinalize default True; property VenvPythonExe: string read FVenvPythonExe write FVenvPythonExe; property DatetimeConversionMode: TDatetimeConversionMode read FDatetimeConversionMode write FDatetimeConversionMode default DEFAULT_DATETIME_CONVERSION_MODE; @@ -4783,6 +4784,7 @@ procedure TPythonEngine.Initialize; begin if VenvPythonExe <> '' then ExecString(AnsiString(Format(Script, [VenvPythonExe]))); + _path := PySys_GetObject('path'); if Assigned(FOnSysPathInit) then FOnSysPathInit(Self, _path); @@ -4837,6 +4839,7 @@ procedure TPythonEngine.Initialize; var i : Integer; + look: PyGILstate_STATE; begin if Assigned(gPythonEngine) then raise Exception.Create('There is already one instance of TPythonEngine running' ); @@ -4880,6 +4883,11 @@ procedure TPythonEngine.Initialize; else FInitialized := True; FIORedirected := False; + + look := PyGILState_Ensure; + + try + InitSysPath; SetProgramArgs; GetTimeStructType; @@ -4896,6 +4904,9 @@ procedure TPythonEngine.Initialize; ExecStrings( InitScript ); if Assigned(FOnAfterInit) then FOnAfterInit(Self); + finally + PyGILState_Release(look); + end; end; procedure TPythonEngine.SetInitScript(Value: TStrings); From 1577a490aad1021999119a934f204cd72f7d5b11 Mon Sep 17 00:00:00 2001 From: Grigoriy Date: Wed, 14 Jul 2021 12:00:05 +0500 Subject: [PATCH 7/9] --- Source/PythonEngine.pas | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/Source/PythonEngine.pas b/Source/PythonEngine.pas index adaaf34c..02c94151 100644 --- a/Source/PythonEngine.pas +++ b/Source/PythonEngine.pas @@ -4083,10 +4083,28 @@ procedure TPythonEngine.Finalize; FPyDateTime_DateTimeTZType := nil; end; +{var + FRunEnsure: Boolean;} + procedure TPythonEngine.AfterLoad; +var + state: PyGILstate_STATE; begin inherited; - Initialize; + + if IsLibrary then + FInitialized := True + else + Initialize; + + {if FRunEnsure then + state := PyGILState_Ensure; + try + Initialize; + finally + if FRunEnsure then + PyGILState_Release(state); + end;} end; procedure TPythonEngine.BeforeLoad; @@ -4227,7 +4245,9 @@ procedure TPythonEngine.Initialize; else FInitialized := True; FIORedirected := False; + InitSysPath; + SetProgramArgs; GetTimeStructType; GetDateTimeTypes; @@ -9156,6 +9176,8 @@ procedure PythonVersionFromDLLName(LibName: string; out MajorVersion, MinorVersi MinorVersion:= StrToIntDef(LibName, DefaultMinor); end; - +initialization + //FRunEnsure := IsLibrary; +finalization end. From 9dc9b0133bc852eea564de101a0f6d374b2bd1aa Mon Sep 17 00:00:00 2001 From: Grigoriy Date: Wed, 14 Jul 2021 12:00:54 +0500 Subject: [PATCH 8/9] --- Source/PythonEngine.pas | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/Source/PythonEngine.pas b/Source/PythonEngine.pas index 02c94151..8dbaf077 100644 --- a/Source/PythonEngine.pas +++ b/Source/PythonEngine.pas @@ -4083,12 +4083,7 @@ procedure TPythonEngine.Finalize; FPyDateTime_DateTimeTZType := nil; end; -{var - FRunEnsure: Boolean;} - procedure TPythonEngine.AfterLoad; -var - state: PyGILstate_STATE; begin inherited; @@ -4096,15 +4091,6 @@ procedure TPythonEngine.AfterLoad; FInitialized := True else Initialize; - - {if FRunEnsure then - state := PyGILState_Ensure; - try - Initialize; - finally - if FRunEnsure then - PyGILState_Release(state); - end;} end; procedure TPythonEngine.BeforeLoad; @@ -9176,8 +9162,5 @@ procedure PythonVersionFromDLLName(LibName: string; out MajorVersion, MinorVersi MinorVersion:= StrToIntDef(LibName, DefaultMinor); end; -initialization - //FRunEnsure := IsLibrary; -finalization end. From 3dc47c1a18a96117bc55ae88982b9f9ba9b5fea2 Mon Sep 17 00:00:00 2001 From: Grigoriy Date: Mon, 7 Feb 2022 15:54:22 +0500 Subject: [PATCH 9/9] Add initialize from module --- Source/PythonEngine.pas | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Source/PythonEngine.pas b/Source/PythonEngine.pas index 8dbaf077..8d5b2160 100644 --- a/Source/PythonEngine.pas +++ b/Source/PythonEngine.pas @@ -1817,6 +1817,9 @@ TPythonEngine = class(TPythonInterface) procedure AssignPyFlags; public + // This is only for modules + procedure InitializeFromModule; + // Constructors & Destructors constructor Create(AOwner: TComponent); override; destructor Destroy; override; @@ -4142,6 +4145,11 @@ procedure TPythonEngine.AssignPyFlags; SetFlag(Py_IgnoreEnvironmentFlag, pfIgnoreEnvironmentFlag in FPyFlags); end; +procedure TPythonEngine.InitializeFromModule; +begin + gPythonEngine := Self; +end; + procedure TPythonEngine.Initialize; procedure InitSysPath;