@@ -1860,19 +1860,26 @@ TPythonEngine = class(TPythonInterface)
18601860 procedure SetPythonHome (const PythonHome: UnicodeString);
18611861 procedure SetProgramName (const ProgramName: UnicodeString);
18621862 function IsType (ob: PPyObject; obt: PPyTypeObject): Boolean;
1863- function Run_CommandAsString (const command : AnsiString; mode : Integer) : String;
1864- function Run_CommandAsObject (const command : AnsiString; mode : Integer) : PPyObject; inline;
1865- function Run_CommandAsObjectWithDict (const command : AnsiString; mode : Integer; locals, globals : PPyObject) : PPyObject;
1866- procedure ExecString (const command : AnsiString); overload; inline;
1867- procedure ExecStrings ( strings : TStrings ); overload;
1868- function EvalString (const command : AnsiString) : PPyObject; overload; inline;
1869- function EvalStringAsStr (const command : AnsiString) : String; inline;
1870- function EvalStrings ( strings : TStrings ) : PPyObject; overload;
1871- procedure ExecString (const command : AnsiString; locals, globals : PPyObject ); overload; inline;
1872- procedure ExecStrings ( strings : TStrings; locals, globals : PPyObject ); overload;
1873- function EvalString ( const command : AnsiString; locals, globals : PPyObject ) : PPyObject; overload; inline;
1874- function EvalStrings ( strings : TStrings; locals, globals : PPyObject ) : PPyObject; overload;
1875- function EvalStringsAsStr ( strings : TStrings ) : String;
1863+ function Run_CommandAsString (const command: AnsiString; mode: Integer; const FileName: string = ' <string>' ): string;
1864+ function Run_CommandAsObject (const command: AnsiString; mode: Integer; const FileName: string = ' <string>' ): PPyObject;
1865+ function Run_CommandAsObjectWithDict (const command: AnsiString; mode: Integer; locals, globals: PPyObject; const FileName: string = ' <string>' ): PPyObject;
1866+ function EncodeString (const str: UnicodeString): AnsiString; { $IFDEF FPC} overload;{ $ENDIF}
1867+ { $IFDEF FPC}
1868+ overload;
1869+ function EncodeString (const str: AnsiString): AnsiString; overload;
1870+ { $ENDIF}
1871+ function EncodeWindowsFilePath (const str: string): AnsiString;
1872+ procedure ExecString (const command: AnsiString; const FileName: string = ' <string>' ); overload;
1873+ procedure ExecStrings (strings: TStrings; const FileName: string = ' <string>' ); overload;
1874+ procedure ExecString (const command: AnsiString; locals, globals: PPyObject; const FileName: string = ' <string>' ); overload;
1875+ procedure ExecFile (const FileName: string; locals: PPyObject = nil ; globals: PPyObject = nil ); overload;
1876+ procedure ExecStrings (strings: TStrings; locals, globals: PPyObject; const FileName: string = ' <string>' ); overload;
1877+ function EvalString (const command: AnsiString; const FileName: string = ' <string>' ): PPyObject; overload;
1878+ function EvalStringAsStr (const command: AnsiString; const FileName: string = ' <string>' ): string;
1879+ function EvalStrings (strings: TStrings; const FileName: string = ' <string>' ): PPyObject; overload;
1880+ function EvalString (const command: AnsiString; locals, globals: PPyObject; const FileName: string = ' <string>' ): PPyObject; overload;
1881+ function EvalStrings (strings: TStrings; locals, globals: PPyObject; const FileName: string = ' <string>' ): PPyObject; overload;
1882+ function EvalStringsAsStr (strings: TStrings; const FileName: string = ' <string>' ): string;
18761883 function EvalPyFunction (pyfunc, pyargs:PPyObject): Variant;
18771884 function EvalFunction (pyfunc:PPyObject; args: array of const ): Variant;
18781885 function EvalFunctionNoArgs (pyfunc:PPyObject): Variant;
@@ -4576,40 +4583,55 @@ function TPythonEngine.EvalFunctionNoArgs(pyfunc:PPyObject): Variant;
45764583 end ;
45774584end ;
45784585
4579- function TPythonEngine.EvalStringAsStr (const command : AnsiString) : String;
4586+ function TPythonEngine.EvalStringAsStr (const command: AnsiString; const
4587+ FileName: string = ' <string>' ): string;
45804588begin
4581- Result := Run_CommandAsString( command, eval_input );
4589+ Result := Run_CommandAsString(command, eval_input, FileName );
45824590end ;
45834591
4584- function TPythonEngine.EvalString (const command : AnsiString) : PPyObject;
4592+ function TPythonEngine.EvalString (const command: AnsiString; const FileName:
4593+ string = ' <string>' ): PPyObject;
45854594begin
4586- Result := Run_CommandAsObject( command, eval_input );
4595+ Result := Run_CommandAsObject(command, eval_input, FileName );
45874596end ;
45884597
4589- procedure TPythonEngine.ExecString (const command : AnsiString);
4598+ procedure TPythonEngine.ExecString (const command: AnsiString; const FileName:
4599+ string = ' <string>' );
45904600begin
4591- Py_XDecRef( Run_CommandAsObject( command, file_input ) );
4601+ Py_XDecRef(Run_CommandAsObject(command, file_input, FileName) );
45924602end ;
45934603
4594- function TPythonEngine.Run_CommandAsString (const command : AnsiString; mode : Integer) : String;
4604+ function TPythonEngine.Run_CommandAsString (const command: AnsiString; mode:
4605+ Integer; const FileName: string = ' <string>' ): string;
45954606var
4596- v : PPyObject;
4607+ PRes : PPyObject;
45974608begin
45984609 Result := ' ' ;
4599- v := Run_CommandAsObject( command, mode );
4600- Result := PyObjectAsString( v );
4601- Py_XDECREF(v );
4610+ PRes := Run_CommandAsObject(command, mode, FileName );
4611+ Result := PyObjectAsString(PRes );
4612+ Py_XDECREF(PRes );
46024613end ;
46034614
4604- function TPythonEngine.Run_CommandAsObject (const command : AnsiString; mode : Integer) : PPyObject;
4615+ function TPythonEngine.Run_CommandAsObject (const command: AnsiString; mode:
4616+ Integer; const FileName: string = ' <string>' ): PPyObject;
46054617begin
4606- Result := Run_CommandAsObjectWithDict(command, mode, nil , nil );
4618+ Result := Run_CommandAsObjectWithDict(command, mode, nil , nil , FileName );
46074619end ;
46084620
4609- function TPythonEngine.Run_CommandAsObjectWithDict (const command : AnsiString; mode : Integer; locals, globals : PPyObject) : PPyObject;
4621+ function TPythonEngine.Run_CommandAsObjectWithDict (const command: AnsiString;
4622+ mode: Integer; locals, globals: PPyObject; const FileName: string =
4623+ ' <string>' ): PPyObject;
4624+ {
4625+ This is the core function for executing/evaluating python code
4626+ Parameters:
4627+ - command: utf-8 encoded AnsiString with the code that will be executed or evaluated
4628+ - mode: one of the constants file_input, single_input, eval_input
4629+ - locals, globals: python dictionaries with local/global namespaces. Can be nil.
4630+ - FileName; optional string used when debugging code with external debuggers
4631+ }
46104632var
46114633 m : PPyObject;
4612- _locals, _globals : PPyObject;
4634+ _locals, _globals, Code : PPyObject;
46134635begin
46144636 CheckPython;
46154637 Result := nil ;
@@ -4635,7 +4657,11 @@ function TPythonEngine.Run_CommandAsObjectWithDict(const command : AnsiString; m
46354657 _globals := _locals;
46364658
46374659 try
4638- Result := PyRun_String(PAnsiChar(CleanString(command)), mode, _globals, _locals);
4660+ Code := Py_CompileString(PAnsiChar(CleanString(command)),
4661+ PAnsiChar(EncodeString(FileName)), mode);
4662+ if Code = nil then
4663+ CheckError(False);
4664+ Result := PyEval_EvalCode(Code, _globals, _locals );
46394665 if Result = nil then
46404666 CheckError(False);
46414667 except
@@ -4646,40 +4672,62 @@ function TPythonEngine.Run_CommandAsObjectWithDict(const command : AnsiString; m
46464672 end ;
46474673end ;
46484674
4675+ procedure TPythonEngine.ExecStrings (strings: TStrings; const FileName: string =
4676+ ' <string>' );
4677+ begin
4678+ Py_XDecRef(Run_CommandAsObject(EncodeString(strings.Text), file_input, FileName));
4679+ end ;
46494680
4650- procedure TPythonEngine.ExecStrings ( strings : TStrings );
4681+ function TPythonEngine.EvalStrings (strings: TStrings; const FileName: string =
4682+ ' <string>' ): PPyObject;
46514683begin
4652- Py_XDecRef( Run_CommandAsObject( strings.Text, file_input ) );
4684+ Result := Run_CommandAsObject(EncodeString( strings.Text) , eval_input, FileName );
46534685end ;
46544686
4655- function TPythonEngine.EvalStrings ( strings : TStrings ) : PPyObject;
4687+ procedure TPythonEngine.ExecFile (const FileName: string; locals,
4688+ globals: PPyObject);
4689+ var
4690+ SL: TStringList;
46564691begin
4657- Result := Run_CommandAsObject( strings.Text, eval_input );
4692+ SL := TStringList.Create;
4693+ try
4694+ SL.LoadFromFile(FileName);
4695+ ExecStrings(SL, locals, globals, FileName);
4696+ finally
4697+ SL.Free;
4698+ end ;
46584699end ;
46594700
4660- procedure TPythonEngine.ExecString (const command : AnsiString; locals, globals : PPyObject );
4701+ procedure TPythonEngine.ExecString (const command: AnsiString; locals, globals:
4702+ PPyObject; const FileName: string = ' <string>' );
46614703begin
4662- Py_XDecRef( Run_CommandAsObjectWithDict( command, file_input, locals, globals ) );
4704+ Py_XDecRef(Run_CommandAsObjectWithDict(command, file_input, locals, globals, FileName) );
46634705end ;
46644706
4665- procedure TPythonEngine.ExecStrings ( strings : TStrings; locals, globals : PPyObject );
4707+ procedure TPythonEngine.ExecStrings (strings: TStrings; locals, globals:
4708+ PPyObject; const FileName: string = ' <string>' );
46664709begin
4667- Py_XDecRef( Run_CommandAsObjectWithDict( strings.Text, file_input, locals, globals ) );
4710+ Py_XDecRef( Run_CommandAsObjectWithDict(EncodeString(strings.Text),
4711+ file_input, locals, globals, FileName));
46684712end ;
46694713
4670- function TPythonEngine.EvalString ( const command : AnsiString; locals, globals : PPyObject ) : PPyObject;
4714+ function TPythonEngine.EvalString (const command: AnsiString; locals, globals:
4715+ PPyObject; const FileName: string = ' <string>' ): PPyObject;
46714716begin
4672- Result := Run_CommandAsObjectWithDict( command, eval_input, locals, globals );
4717+ Result := Run_CommandAsObjectWithDict(command, eval_input, locals, globals, FileName );
46734718end ;
46744719
4675- function TPythonEngine.EvalStrings ( strings : TStrings; locals, globals : PPyObject ) : PPyObject;
4720+ function TPythonEngine.EvalStrings (strings: TStrings; locals, globals:
4721+ PPyObject; const FileName: string = ' <string>' ): PPyObject;
46764722begin
4677- Result := Run_CommandAsObjectWithDict( strings.Text, eval_input, locals, globals );
4723+ Result := Run_CommandAsObjectWithDict(EncodeString(strings.Text),
4724+ eval_input, locals, globals, FileName);
46784725end ;
46794726
4680- function TPythonEngine.EvalStringsAsStr ( strings : TStrings ) : String;
4727+ function TPythonEngine.EvalStringsAsStr (strings: TStrings; const FileName:
4728+ string = ' <string>' ): string;
46814729begin
4682- Result := Run_CommandAsString( strings.Text, eval_input );
4730+ Result := Run_CommandAsString(EncodeString( strings.Text) , eval_input, FileName );
46834731end ;
46844732
46854733function TPythonEngine.CheckEvalSyntax ( const str : AnsiString ) : Boolean;
@@ -5035,6 +5083,27 @@ function TPythonEngine.FindClient( const aName : AnsiString ) : TEngineClient;
50355083 end ;
50365084end ;
50375085
5086+ function TPythonEngine.EncodeString (const str: UnicodeString): AnsiString; { $IFDEF FPC} overload;{ $ENDIF}
5087+ begin
5088+ Result := UTF8Encode(str)
5089+ end ;
5090+
5091+ { $IFDEF FPC}
5092+ function TPythonEngine.EncodeString (const str: AnsiString): AnsiString; overload;
5093+ begin
5094+ Result := str;
5095+ end ;
5096+ { $ENDIF}
5097+
5098+ function TPythonEngine.EncodeWindowsFilePath (const str: string): AnsiString;
5099+ { PEP 529}
5100+ begin
5101+ if (MajorVersion > 3 ) or ((MajorVersion = 3 ) and (MinorVersion >=6 ) )then
5102+ Result := UTF8Encode(str)
5103+ else
5104+ Result := AnsiString(str);
5105+ end ;
5106+
50385107function TPythonEngine.TypeByName ( const aTypeName : AnsiString ) : PPyTypeObject;
50395108var
50405109 i : Integer;
0 commit comments