Skip to content

Commit b1acc06

Browse files
authored
Merge pull request pyscripter#59 from talos-gis/master
SetPythonHome; Ansi/UTF8 eval; warning sielnt
2 parents b1c0ed5 + 3553b25 commit b1acc06

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

PythonForDelphi/Components/Sources/Core/PythonEngine.pas

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,6 +1968,10 @@ TPythonInterface=class(TDynamicDll)
19681968
Py_GetCopyright : function : PAnsiChar; cdecl;
19691969
Py_GetExecPrefix : function : PAnsiChar; cdecl;
19701970
Py_GetPath : function : PAnsiChar; cdecl;
1971+
Py_SetPythonHome : procedure (home : PAnsiChar); cdecl;
1972+
Py_GetPythonHome : function : PAnsiChar; cdecl;
1973+
Py_SetPythonHome3000 : procedure (home : PWideChar); cdecl;
1974+
Py_GetPythonHome3000 : function : PWideChar; cdecl;
19711975
Py_GetPrefix : function : PAnsiChar; cdecl;
19721976
Py_GetProgramName : function : PAnsiChar; cdecl;
19731977

@@ -2187,6 +2191,8 @@ TPythonEngine = class(TPythonInterface)
21872191
FAutoFinalize: Boolean;
21882192
FProgramName: AnsiString;
21892193
FProgramNameW: UnicodeString;
2194+
FPythonHome: AnsiString;
2195+
FPythonHomeW: UnicodeString;
21902196
FInitThreads: Boolean;
21912197
FOnPathInitialization: TPathInitializationEvent;
21922198
FOnSysPathInit: TSysPathInitEvent;
@@ -2240,12 +2246,14 @@ TPythonEngine = class(TPythonInterface)
22402246
procedure Finalize;
22412247
procedure Lock;
22422248
procedure Unlock;
2249+
procedure SetPythonHome(PythonHome: String);
22432250
function IsType(ob: PPyObject; obt: PPyTypeObject): Boolean;
22442251
function GetAttrString(obj: PPyObject; AName: PAnsiChar):PAnsiChar;
22452252
function CleanString(const s : AnsiString) : AnsiString;
22462253
function Run_CommandAsString(const command : AnsiString; mode : Integer) : String;
22472254
function Run_CommandAsObject(const command : AnsiString; mode : Integer) : PPyObject;
22482255
function Run_CommandAsObjectWithDict(const command : AnsiString; mode : Integer; locals, globals : PPyObject) : PPyObject;
2256+
function ToPythonRawString (str: String): AnsiString;
22492257
procedure ExecString(const command : AnsiString); overload;
22502258
procedure ExecStrings( strings : TStrings ); overload;
22512259
function EvalString(const command : AnsiString) : PPyObject; overload;
@@ -4056,6 +4064,14 @@ procedure TPythonInterface.MapDll;
40564064
Py_GetCopyright :=Import('Py_GetCopyright');
40574065
Py_GetExecPrefix :=Import('Py_GetExecPrefix');
40584066
Py_GetPath :=Import('Py_GetPath');
4067+
if IsPython3000 then
4068+
Py_SetPythonHome3000 :=Import('Py_SetPythonHome')
4069+
else
4070+
Py_SetPythonHome :=Import('Py_SetPythonHome');
4071+
if IsPython3000 then
4072+
Py_GetPythonHome3000 :=Import('Py_GetPythonHome')
4073+
else
4074+
Py_GetPythonHome :=Import('Py_GetPythonHome');
40594075
Py_GetPrefix :=Import('Py_GetPrefix');
40604076
Py_GetProgramName :=Import('Py_GetProgramName');
40614077
PyParser_SimpleParseString :=Import('PyParser_SimpleParseString');
@@ -4850,6 +4866,12 @@ procedure TPythonEngine.Initialize;
48504866
end
48514867
end;
48524868
AssignPyFlags;
4869+
if FPythonHomeW<>'' then begin
4870+
if IsPython3000 then
4871+
Py_SetPythonHome3000(PChar(FPythonHomeW))
4872+
else
4873+
Py_SetPythonHome(PAnsiChar(FPythonHome));
4874+
end;
48534875
Py_Initialize;
48544876
FInitialized := True;
48554877
FIORedirected := False;
@@ -5111,6 +5133,12 @@ procedure TPythonEngine.SetPyFlags(const Value: TPythonFlags);
51115133
end; // of if
51125134
end;
51135135

5136+
procedure TPythonEngine.SetPythonHome(PythonHome: String);
5137+
begin
5138+
FPythonHomeW := PythonHome;
5139+
FPythonHome := ToPythonRawString(PythonHome);
5140+
end;
5141+
51145142
function TPythonEngine.IsType(ob: PPyObject; obt: PPyTypeObject): Boolean;
51155143
begin
51165144
result := ob^.ob_type = obt;
@@ -5280,12 +5308,12 @@ function TPythonEngine.Run_CommandAsObjectWithDict(const command : AnsiString; m
52805308

52815309
procedure TPythonEngine.ExecStrings( strings : TStrings );
52825310
begin
5283-
Py_XDecRef( Run_CommandAsObject( CleanString( AnsiString(strings.Text) ), file_input ) );
5311+
Py_XDecRef( Run_CommandAsObject( CleanString( ToPythonRawString(strings.Text) ), file_input ) );
52845312
end;
52855313

52865314
function TPythonEngine.EvalStrings( strings : TStrings ) : PPyObject;
52875315
begin
5288-
Result := Run_CommandAsObject( CleanString( AnsiString(strings.Text) ), eval_input );
5316+
Result := Run_CommandAsObject( CleanString( ToPythonRawString(strings.Text) ), eval_input );
52895317
end;
52905318

52915319
procedure TPythonEngine.ExecString(const command : AnsiString; locals, globals : PPyObject );
@@ -5295,7 +5323,7 @@ procedure TPythonEngine.ExecString(const command : AnsiString; locals, globals :
52955323

52965324
procedure TPythonEngine.ExecStrings( strings : TStrings; locals, globals : PPyObject );
52975325
begin
5298-
Py_XDecRef( Run_CommandAsObjectWithDict( CleanString( AnsiString(strings.Text) ), file_input, locals, globals ) );
5326+
Py_XDecRef( Run_CommandAsObjectWithDict( CleanString( ToPythonRawString(strings.Text) ), file_input, locals, globals ) );
52995327
end;
53005328

53015329
function TPythonEngine.EvalString( const command : AnsiString; locals, globals : PPyObject ) : PPyObject;
@@ -5305,12 +5333,12 @@ function TPythonEngine.EvalString( const command : AnsiString; locals, globals :
53055333

53065334
function TPythonEngine.EvalStrings( strings : TStrings; locals, globals : PPyObject ) : PPyObject;
53075335
begin
5308-
Result := Run_CommandAsObjectWithDict( CleanString( AnsiString(strings.Text) ), eval_input, locals, globals );
5336+
Result := Run_CommandAsObjectWithDict( CleanString( ToPythonRawString(strings.Text) ), eval_input, locals, globals );
53095337
end;
53105338

53115339
function TPythonEngine.EvalStringsAsStr( strings : TStrings ) : String;
53125340
begin
5313-
Result := Run_CommandAsString( CleanString( AnsiString(strings.Text) ), eval_input );
5341+
Result := Run_CommandAsString( CleanString( ToPythonRawString(strings.Text) ), eval_input );
53145342
end;
53155343

53165344
function TPythonEngine.CheckEvalSyntax( const str : AnsiString ) : Boolean;
@@ -5658,6 +5686,14 @@ function TPythonEngine.FindClient( const aName : string ) : TEngineClient;
56585686
end;
56595687
end;
56605688

5689+
function TPythonEngine.ToPythonRawString(str: String): AnsiString;
5690+
begin
5691+
if IsPython3000 then
5692+
Result := UTF8Encode(str)
5693+
else
5694+
Result := AnsiString(str);
5695+
end;
5696+
56615697
function TPythonEngine.TypeByName( const aTypeName : AnsiString ) : PPyTypeObject;
56625698
var
56635699
i : Integer;
@@ -9726,12 +9762,12 @@ procedure MaskFPUExceptions(ExceptionsMasked : boolean;
97269762
exOverflow, exUnderflow, exPrecision])
97279763
else
97289764
SetExceptionMask([exDenormalized, exUnderflow, exPrecision]);
9729-
{$IFNDEF NEXTGEN}
9765+
{$IFNDEF NEXTGEN}{$WARN SYMBOL_PLATFORM OFF}
97309766
if MatchPythonPrecision then
97319767
SetPrecisionMode(pmDouble)
97329768
else
97339769
SetPrecisionMode(pmExtended);
9734-
{$ENDIF !NEXTGEN}
9770+
{$ENDIF !NEXTGEN}{$WARN SYMBOL_PLATFORM ON}
97359771
end;
97369772

97379773
{$IFDEF MSWINDOWS}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy