@@ -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
51125134end ;
51135135
5136+ procedure TPythonEngine.SetPythonHome (PythonHome: String);
5137+ begin
5138+ FPythonHomeW := PythonHome;
5139+ FPythonHome := ToPythonRawString(PythonHome);
5140+ end ;
5141+
51145142function TPythonEngine.IsType (ob: PPyObject; obt: PPyTypeObject): Boolean;
51155143begin
51165144 result := ob^.ob_type = obt;
@@ -5280,12 +5308,12 @@ function TPythonEngine.Run_CommandAsObjectWithDict(const command : AnsiString; m
52805308
52815309procedure TPythonEngine.ExecStrings ( strings : TStrings );
52825310begin
5283- Py_XDecRef( Run_CommandAsObject( CleanString( AnsiString (strings.Text) ), file_input ) );
5311+ Py_XDecRef( Run_CommandAsObject( CleanString( ToPythonRawString (strings.Text) ), file_input ) );
52845312end ;
52855313
52865314function TPythonEngine.EvalStrings ( strings : TStrings ) : PPyObject;
52875315begin
5288- Result := Run_CommandAsObject( CleanString( AnsiString (strings.Text) ), eval_input );
5316+ Result := Run_CommandAsObject( CleanString( ToPythonRawString (strings.Text) ), eval_input );
52895317end ;
52905318
52915319procedure TPythonEngine.ExecString (const command : AnsiString; locals, globals : PPyObject );
@@ -5295,7 +5323,7 @@ procedure TPythonEngine.ExecString(const command : AnsiString; locals, globals :
52955323
52965324procedure TPythonEngine.ExecStrings ( strings : TStrings; locals, globals : PPyObject );
52975325begin
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 ) );
52995327end ;
53005328
53015329function TPythonEngine.EvalString ( const command : AnsiString; locals, globals : PPyObject ) : PPyObject;
@@ -5305,12 +5333,12 @@ function TPythonEngine.EvalString( const command : AnsiString; locals, globals :
53055333
53065334function TPythonEngine.EvalStrings ( strings : TStrings; locals, globals : PPyObject ) : PPyObject;
53075335begin
5308- Result := Run_CommandAsObjectWithDict( CleanString( AnsiString (strings.Text) ), eval_input, locals, globals );
5336+ Result := Run_CommandAsObjectWithDict( CleanString( ToPythonRawString (strings.Text) ), eval_input, locals, globals );
53095337end ;
53105338
53115339function TPythonEngine.EvalStringsAsStr ( strings : TStrings ) : String;
53125340begin
5313- Result := Run_CommandAsString( CleanString( AnsiString (strings.Text) ), eval_input );
5341+ Result := Run_CommandAsString( CleanString( ToPythonRawString (strings.Text) ), eval_input );
53145342end ;
53155343
53165344function TPythonEngine.CheckEvalSyntax ( const str : AnsiString ) : Boolean;
@@ -5658,6 +5686,14 @@ function TPythonEngine.FindClient( const aName : string ) : TEngineClient;
56585686 end ;
56595687end ;
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+
56615697function TPythonEngine.TypeByName ( const aTypeName : AnsiString ) : PPyTypeObject;
56625698var
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 }
97359771end ;
97369772
97379773{ $IFDEF MSWINDOWS}
0 commit comments