From d6245588fa55a01b13334f0809952474982b813f Mon Sep 17 00:00:00 2001 From: Ben Avrahami Date: Sun, 24 Jun 2018 16:51:02 +0300 Subject: [PATCH 1/2] publicize GetDllPath + option to use latest DLL + exec and eval accept utf8 code --- .../Components/Sources/Core/PythonEngine.pas | 33 ++++++++++++++----- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas index cae49ef4..736388ce 100644 --- a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas +++ b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas @@ -1518,7 +1518,6 @@ TDynamicDll = class(TComponent) procedure BeforeUnload; virtual; function GetQuitMessage : String; virtual; procedure DoOpenDll(const aDllName : String); virtual; - function GetDllPath : String; public // Constructors & Destructors @@ -1531,6 +1530,7 @@ TDynamicDll = class(TComponent) procedure LoadDll; procedure UnloadDll; procedure Quit; + function GetDllPath : String; // Public properties published @@ -2207,6 +2207,7 @@ TPythonEngine = class(TPythonInterface) FPyDateTime_TZInfoType: PPyObject; FPyDateTime_TimeTZType: PPyObject; FPyDateTime_DateTimeTZType: PPyObject; + FUseLatestDll: boolean; function GetVersion: String; procedure SetVersion(const Value: String); @@ -4600,6 +4601,7 @@ constructor TPythonEngine.Create(AOwner: TComponent); FUseWindowsConsole := False; FPyFlags := []; FDatetimeConversionMode := DEFAULT_DATETIME_CONVERSION_MODE; + FUseLatestDll := True; if csDesigning in ComponentState then begin for i := 0 to AOwner.ComponentCount - 1 do @@ -4716,10 +4718,18 @@ procedure TPythonEngine.BeforeLoad; procedure TPythonEngine.DoOpenDll(const aDllName : String); var - i : Integer; + i, stop : Integer; begin - if UseLastKnownVersion then - for i:= Integer(COMPILED_FOR_PYTHON_VERSION_INDEX) to High(PYTHON_KNOWN_VERSIONS) do + if UseLastKnownVersion then begin + if FUseLatestDll then begin + i := High(PYTHON_KNOWN_VERSIONS); + stop := Integer(COMPILED_FOR_PYTHON_VERSION_INDEX)-1 + end + else begin + i := Integer(COMPILED_FOR_PYTHON_VERSION_INDEX); + stop := High(PYTHON_KNOWN_VERSIONS)+1 + end; + while i <> stop do begin RegVersion := PYTHON_KNOWN_VERSIONS[i].RegVersion; FDLLHandle := SafeLoadLibrary(GetDllPath+PYTHON_KNOWN_VERSIONS[i].DllName); @@ -4731,7 +4741,12 @@ procedure TPythonEngine.DoOpenDll(const aDllName : String); end; if not PYTHON_KNOWN_VERSIONS[i].CanUseLatest then Break; + if FUseLatestDll then + i:=i-1 + else + i:=i+1; end; + end; inherited; end; @@ -5282,12 +5297,12 @@ function TPythonEngine.Run_CommandAsObjectWithDict(const command : AnsiString; m procedure TPythonEngine.ExecStrings( strings : TStrings ); begin - Py_XDecRef( Run_CommandAsObject( CleanString( AnsiString(strings.Text) ), file_input ) ); + Py_XDecRef( Run_CommandAsObject( CleanString( UTF8Encode(strings.Text) ), file_input ) ); end; function TPythonEngine.EvalStrings( strings : TStrings ) : PPyObject; begin - Result := Run_CommandAsObject( CleanString( AnsiString(strings.Text) ), eval_input ); + Result := Run_CommandAsObject( CleanString( UTF8Encode(strings.Text) ), eval_input ); end; procedure TPythonEngine.ExecString(const command : AnsiString; locals, globals : PPyObject ); @@ -5297,7 +5312,7 @@ procedure TPythonEngine.ExecString(const command : AnsiString; locals, globals : procedure TPythonEngine.ExecStrings( strings : TStrings; locals, globals : PPyObject ); begin - Py_XDecRef( Run_CommandAsObjectWithDict( CleanString( AnsiString(strings.Text) ), file_input, locals, globals ) ); + Py_XDecRef( Run_CommandAsObjectWithDict( CleanString( UTF8Encode(strings.Text) ), file_input, locals, globals ) ); end; function TPythonEngine.EvalString( const command : AnsiString; locals, globals : PPyObject ) : PPyObject; @@ -5307,12 +5322,12 @@ function TPythonEngine.EvalString( const command : AnsiString; locals, globals : function TPythonEngine.EvalStrings( strings : TStrings; locals, globals : PPyObject ) : PPyObject; begin - Result := Run_CommandAsObjectWithDict( CleanString( AnsiString(strings.Text) ), eval_input, locals, globals ); + Result := Run_CommandAsObjectWithDict( CleanString( UTF8Encode(strings.Text) ), eval_input, locals, globals ); end; function TPythonEngine.EvalStringsAsStr( strings : TStrings ) : String; begin - Result := Run_CommandAsString( CleanString( AnsiString(strings.Text) ), eval_input ); + Result := Run_CommandAsString( CleanString( UTF8Encode(strings.Text) ), eval_input ); end; function TPythonEngine.CheckEvalSyntax( const str : AnsiString ) : Boolean; From b487557195851a94856ace0b6cd66b73602ce013 Mon Sep 17 00:00:00 2001 From: Ben Avrahami Date: Tue, 26 Jun 2018 09:42:54 +0300 Subject: [PATCH 2/2] reverted version changes --- .../Components/Sources/Core/PythonEngine.pas | 23 ++++--------------- 1 file changed, 4 insertions(+), 19 deletions(-) diff --git a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas index 736388ce..3b700058 100644 --- a/PythonForDelphi/Components/Sources/Core/PythonEngine.pas +++ b/PythonForDelphi/Components/Sources/Core/PythonEngine.pas @@ -1518,6 +1518,7 @@ TDynamicDll = class(TComponent) procedure BeforeUnload; virtual; function GetQuitMessage : String; virtual; procedure DoOpenDll(const aDllName : String); virtual; + function GetDllPath : String; public // Constructors & Destructors @@ -1530,7 +1531,6 @@ TDynamicDll = class(TComponent) procedure LoadDll; procedure UnloadDll; procedure Quit; - function GetDllPath : String; // Public properties published @@ -2207,7 +2207,6 @@ TPythonEngine = class(TPythonInterface) FPyDateTime_TZInfoType: PPyObject; FPyDateTime_TimeTZType: PPyObject; FPyDateTime_DateTimeTZType: PPyObject; - FUseLatestDll: boolean; function GetVersion: String; procedure SetVersion(const Value: String); @@ -4601,7 +4600,6 @@ constructor TPythonEngine.Create(AOwner: TComponent); FUseWindowsConsole := False; FPyFlags := []; FDatetimeConversionMode := DEFAULT_DATETIME_CONVERSION_MODE; - FUseLatestDll := True; if csDesigning in ComponentState then begin for i := 0 to AOwner.ComponentCount - 1 do @@ -4718,18 +4716,10 @@ procedure TPythonEngine.BeforeLoad; procedure TPythonEngine.DoOpenDll(const aDllName : String); var - i, stop : Integer; + i : Integer; begin - if UseLastKnownVersion then begin - if FUseLatestDll then begin - i := High(PYTHON_KNOWN_VERSIONS); - stop := Integer(COMPILED_FOR_PYTHON_VERSION_INDEX)-1 - end - else begin - i := Integer(COMPILED_FOR_PYTHON_VERSION_INDEX); - stop := High(PYTHON_KNOWN_VERSIONS)+1 - end; - while i <> stop do + if UseLastKnownVersion then + for i:= Integer(COMPILED_FOR_PYTHON_VERSION_INDEX) to High(PYTHON_KNOWN_VERSIONS) do begin RegVersion := PYTHON_KNOWN_VERSIONS[i].RegVersion; FDLLHandle := SafeLoadLibrary(GetDllPath+PYTHON_KNOWN_VERSIONS[i].DllName); @@ -4741,12 +4731,7 @@ procedure TPythonEngine.DoOpenDll(const aDllName : String); end; if not PYTHON_KNOWN_VERSIONS[i].CanUseLatest then Break; - if FUseLatestDll then - i:=i-1 - else - i:=i+1; end; - end; inherited; end;