Thanks to visit codestin.com
Credit goes to github.com

Skip to content

SetPythonHome; Ansi/UTF8 eval; warning silent #59

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 9, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions PythonForDelphi/Components/Sources/Core/PythonEngine.pas
Original file line number Diff line number Diff line change
Expand Up @@ -1968,6 +1968,10 @@ TPythonInterface=class(TDynamicDll)
Py_GetCopyright : function : PAnsiChar; cdecl;
Py_GetExecPrefix : function : PAnsiChar; cdecl;
Py_GetPath : function : PAnsiChar; cdecl;
Py_SetPythonHome : procedure (home : PAnsiChar); cdecl;
Py_GetPythonHome : function : PAnsiChar; cdecl;
Py_SetPythonHome3000 : procedure (home : PWideChar); cdecl;
Py_GetPythonHome3000 : function : PWideChar; cdecl;
Py_GetPrefix : function : PAnsiChar; cdecl;
Py_GetProgramName : function : PAnsiChar; cdecl;

Expand Down Expand Up @@ -2187,6 +2191,8 @@ TPythonEngine = class(TPythonInterface)
FAutoFinalize: Boolean;
FProgramName: AnsiString;
FProgramNameW: UnicodeString;
FPythonHome: AnsiString;
FPythonHomeW: UnicodeString;
FInitThreads: Boolean;
FOnPathInitialization: TPathInitializationEvent;
FOnSysPathInit: TSysPathInitEvent;
Expand Down Expand Up @@ -2240,12 +2246,14 @@ TPythonEngine = class(TPythonInterface)
procedure Finalize;
procedure Lock;
procedure Unlock;
procedure SetPythonHome(PythonHome: String);
function IsType(ob: PPyObject; obt: PPyTypeObject): Boolean;
function GetAttrString(obj: PPyObject; AName: PAnsiChar):PAnsiChar;
function CleanString(const s : AnsiString) : AnsiString;
function Run_CommandAsString(const command : AnsiString; mode : Integer) : String;
function Run_CommandAsObject(const command : AnsiString; mode : Integer) : PPyObject;
function Run_CommandAsObjectWithDict(const command : AnsiString; mode : Integer; locals, globals : PPyObject) : PPyObject;
function ToPythonRawString (str: String): AnsiString;
procedure ExecString(const command : AnsiString); overload;
procedure ExecStrings( strings : TStrings ); overload;
function EvalString(const command : AnsiString) : PPyObject; overload;
Expand Down Expand Up @@ -4056,6 +4064,14 @@ procedure TPythonInterface.MapDll;
Py_GetCopyright :=Import('Py_GetCopyright');
Py_GetExecPrefix :=Import('Py_GetExecPrefix');
Py_GetPath :=Import('Py_GetPath');
if IsPython3000 then
Py_SetPythonHome3000 :=Import('Py_SetPythonHome')
else
Py_SetPythonHome :=Import('Py_SetPythonHome');
if IsPython3000 then
Py_GetPythonHome3000 :=Import('Py_GetPythonHome')
else
Py_GetPythonHome :=Import('Py_GetPythonHome');
Py_GetPrefix :=Import('Py_GetPrefix');
Py_GetProgramName :=Import('Py_GetProgramName');
PyParser_SimpleParseString :=Import('PyParser_SimpleParseString');
Expand Down Expand Up @@ -4850,6 +4866,12 @@ procedure TPythonEngine.Initialize;
end
end;
AssignPyFlags;
if FPythonHomeW<>'' then begin
if IsPython3000 then
Py_SetPythonHome3000(PChar(FPythonHomeW))
else
Py_SetPythonHome(PAnsiChar(FPythonHome));
end;
Py_Initialize;
FInitialized := True;
FIORedirected := False;
Expand Down Expand Up @@ -5111,6 +5133,12 @@ procedure TPythonEngine.SetPyFlags(const Value: TPythonFlags);
end; // of if
end;

procedure TPythonEngine.SetPythonHome(PythonHome: String);
begin
FPythonHomeW := PythonHome;
FPythonHome := ToPythonRawString(PythonHome);
end;

function TPythonEngine.IsType(ob: PPyObject; obt: PPyTypeObject): Boolean;
begin
result := ob^.ob_type = obt;
Expand Down Expand Up @@ -5280,12 +5308,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( ToPythonRawString(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( ToPythonRawString(strings.Text) ), eval_input );
end;

procedure TPythonEngine.ExecString(const command : AnsiString; locals, globals : PPyObject );
Expand All @@ -5295,7 +5323,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( ToPythonRawString(strings.Text) ), file_input, locals, globals ) );
end;

function TPythonEngine.EvalString( const command : AnsiString; locals, globals : PPyObject ) : PPyObject;
Expand All @@ -5305,12 +5333,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( ToPythonRawString(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( ToPythonRawString(strings.Text) ), eval_input );
end;

function TPythonEngine.CheckEvalSyntax( const str : AnsiString ) : Boolean;
Expand Down Expand Up @@ -5658,6 +5686,14 @@ function TPythonEngine.FindClient( const aName : string ) : TEngineClient;
end;
end;

function TPythonEngine.ToPythonRawString(str: String): AnsiString;
begin
if IsPython3000 then
Result := UTF8Encode(str)
else
Result := AnsiString(str);
end;

function TPythonEngine.TypeByName( const aTypeName : AnsiString ) : PPyTypeObject;
var
i : Integer;
Expand Down Expand Up @@ -9726,12 +9762,12 @@ procedure MaskFPUExceptions(ExceptionsMasked : boolean;
exOverflow, exUnderflow, exPrecision])
else
SetExceptionMask([exDenormalized, exUnderflow, exPrecision]);
{$IFNDEF NEXTGEN}
{$IFNDEF NEXTGEN}{$WARN SYMBOL_PLATFORM OFF}
if MatchPythonPrecision then
SetPrecisionMode(pmDouble)
else
SetPrecisionMode(pmExtended);
{$ENDIF !NEXTGEN}
{$ENDIF !NEXTGEN}{$WARN SYMBOL_PLATFORM ON}
end;

{$IFDEF MSWINDOWS}
Expand Down