Skip to content

Commit 3553b25

Browse files
author
Idan Miara
committed
ToPythonRawString - Python 2.x expects ANSI and Python 3.x expects utf8; ToPythonRawString will make the right RawString.
1 parent 645daec commit 3553b25

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

PythonForDelphi/Components/Sources/Core/PythonEngine.pas

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2253,6 +2253,7 @@ TPythonEngine = class(TPythonInterface)
22532253
function Run_CommandAsString(const command : AnsiString; mode : Integer) : String;
22542254
function Run_CommandAsObject(const command : AnsiString; mode : Integer) : PPyObject;
22552255
function Run_CommandAsObjectWithDict(const command : AnsiString; mode : Integer; locals, globals : PPyObject) : PPyObject;
2256+
function ToPythonRawString (str: String): AnsiString;
22562257
procedure ExecString(const command : AnsiString); overload;
22572258
procedure ExecStrings( strings : TStrings ); overload;
22582259
function EvalString(const command : AnsiString) : PPyObject; overload;
@@ -5135,7 +5136,7 @@ procedure TPythonEngine.SetPyFlags(const Value: TPythonFlags);
51355136
procedure TPythonEngine.SetPythonHome(PythonHome: String);
51365137
begin
51375138
FPythonHomeW := PythonHome;
5138-
FPythonHome := UTF8Encode(PythonHome);
5139+
FPythonHome := ToPythonRawString(PythonHome);
51395140
end;
51405141

51415142
function TPythonEngine.IsType(ob: PPyObject; obt: PPyTypeObject): Boolean;
@@ -5307,12 +5308,12 @@ function TPythonEngine.Run_CommandAsObjectWithDict(const command : AnsiString; m
53075308

53085309
procedure TPythonEngine.ExecStrings( strings : TStrings );
53095310
begin
5310-
Py_XDecRef( Run_CommandAsObject( CleanString( UTF8Encode(strings.Text) ), file_input ) );
5311+
Py_XDecRef( Run_CommandAsObject( CleanString( ToPythonRawString(strings.Text) ), file_input ) );
53115312
end;
53125313

53135314
function TPythonEngine.EvalStrings( strings : TStrings ) : PPyObject;
53145315
begin
5315-
Result := Run_CommandAsObject( CleanString( UTF8Encode(strings.Text) ), eval_input );
5316+
Result := Run_CommandAsObject( CleanString( ToPythonRawString(strings.Text) ), eval_input );
53165317
end;
53175318

53185319
procedure TPythonEngine.ExecString(const command : AnsiString; locals, globals : PPyObject );
@@ -5322,7 +5323,7 @@ procedure TPythonEngine.ExecString(const command : AnsiString; locals, globals :
53225323

53235324
procedure TPythonEngine.ExecStrings( strings : TStrings; locals, globals : PPyObject );
53245325
begin
5325-
Py_XDecRef( Run_CommandAsObjectWithDict( CleanString( UTF8Encode(strings.Text) ), file_input, locals, globals ) );
5326+
Py_XDecRef( Run_CommandAsObjectWithDict( CleanString( ToPythonRawString(strings.Text) ), file_input, locals, globals ) );
53265327
end;
53275328

53285329
function TPythonEngine.EvalString( const command : AnsiString; locals, globals : PPyObject ) : PPyObject;
@@ -5332,12 +5333,12 @@ function TPythonEngine.EvalString( const command : AnsiString; locals, globals :
53325333

53335334
function TPythonEngine.EvalStrings( strings : TStrings; locals, globals : PPyObject ) : PPyObject;
53345335
begin
5335-
Result := Run_CommandAsObjectWithDict( CleanString( UTF8Encode(strings.Text) ), eval_input, locals, globals );
5336+
Result := Run_CommandAsObjectWithDict( CleanString( ToPythonRawString(strings.Text) ), eval_input, locals, globals );
53365337
end;
53375338

53385339
function TPythonEngine.EvalStringsAsStr( strings : TStrings ) : String;
53395340
begin
5340-
Result := Run_CommandAsString( CleanString( UTF8Encode(strings.Text) ), eval_input );
5341+
Result := Run_CommandAsString( CleanString( ToPythonRawString(strings.Text) ), eval_input );
53415342
end;
53425343

53435344
function TPythonEngine.CheckEvalSyntax( const str : AnsiString ) : Boolean;
@@ -5685,6 +5686,14 @@ function TPythonEngine.FindClient( const aName : string ) : TEngineClient;
56855686
end;
56865687
end;
56875688

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+
56885697
function TPythonEngine.TypeByName( const aTypeName : AnsiString ) : PPyTypeObject;
56895698
var
56905699
i : Integer;

0 commit comments

Comments
 (0)