@@ -1860,19 +1860,26 @@ TPythonEngine = class(TPythonInterface)
1860
1860
procedure SetPythonHome (const PythonHome: UnicodeString);
1861
1861
procedure SetProgramName (const ProgramName: UnicodeString);
1862
1862
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;
1876
1883
function EvalPyFunction (pyfunc, pyargs:PPyObject): Variant;
1877
1884
function EvalFunction (pyfunc:PPyObject; args: array of const ): Variant;
1878
1885
function EvalFunctionNoArgs (pyfunc:PPyObject): Variant;
@@ -4576,40 +4583,55 @@ function TPythonEngine.EvalFunctionNoArgs(pyfunc:PPyObject): Variant;
4576
4583
end ;
4577
4584
end ;
4578
4585
4579
- function TPythonEngine.EvalStringAsStr (const command : AnsiString) : String;
4586
+ function TPythonEngine.EvalStringAsStr (const command: AnsiString; const
4587
+ FileName: string = ' <string>' ): string;
4580
4588
begin
4581
- Result := Run_CommandAsString( command, eval_input );
4589
+ Result := Run_CommandAsString(command, eval_input, FileName );
4582
4590
end ;
4583
4591
4584
- function TPythonEngine.EvalString (const command : AnsiString) : PPyObject;
4592
+ function TPythonEngine.EvalString (const command: AnsiString; const FileName:
4593
+ string = ' <string>' ): PPyObject;
4585
4594
begin
4586
- Result := Run_CommandAsObject( command, eval_input );
4595
+ Result := Run_CommandAsObject(command, eval_input, FileName );
4587
4596
end ;
4588
4597
4589
- procedure TPythonEngine.ExecString (const command : AnsiString);
4598
+ procedure TPythonEngine.ExecString (const command: AnsiString; const FileName:
4599
+ string = ' <string>' );
4590
4600
begin
4591
- Py_XDecRef( Run_CommandAsObject( command, file_input ) );
4601
+ Py_XDecRef(Run_CommandAsObject(command, file_input, FileName) );
4592
4602
end ;
4593
4603
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;
4595
4606
var
4596
- v : PPyObject;
4607
+ PRes : PPyObject;
4597
4608
begin
4598
4609
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 );
4602
4613
end ;
4603
4614
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;
4605
4617
begin
4606
- Result := Run_CommandAsObjectWithDict(command, mode, nil , nil );
4618
+ Result := Run_CommandAsObjectWithDict(command, mode, nil , nil , FileName );
4607
4619
end ;
4608
4620
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
+ }
4610
4632
var
4611
4633
m : PPyObject;
4612
- _locals, _globals : PPyObject;
4634
+ _locals, _globals, Code : PPyObject;
4613
4635
begin
4614
4636
CheckPython;
4615
4637
Result := nil ;
@@ -4635,7 +4657,11 @@ function TPythonEngine.Run_CommandAsObjectWithDict(const command : AnsiString; m
4635
4657
_globals := _locals;
4636
4658
4637
4659
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 );
4639
4665
if Result = nil then
4640
4666
CheckError(False);
4641
4667
except
@@ -4646,40 +4672,62 @@ function TPythonEngine.Run_CommandAsObjectWithDict(const command : AnsiString; m
4646
4672
end ;
4647
4673
end ;
4648
4674
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 ;
4649
4680
4650
- procedure TPythonEngine.ExecStrings ( strings : TStrings );
4681
+ function TPythonEngine.EvalStrings (strings: TStrings; const FileName: string =
4682
+ ' <string>' ): PPyObject;
4651
4683
begin
4652
- Py_XDecRef( Run_CommandAsObject( strings.Text, file_input ) );
4684
+ Result := Run_CommandAsObject(EncodeString( strings.Text) , eval_input, FileName );
4653
4685
end ;
4654
4686
4655
- function TPythonEngine.EvalStrings ( strings : TStrings ) : PPyObject;
4687
+ procedure TPythonEngine.ExecFile (const FileName: string; locals,
4688
+ globals: PPyObject);
4689
+ var
4690
+ SL: TStringList;
4656
4691
begin
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 ;
4658
4699
end ;
4659
4700
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>' );
4661
4703
begin
4662
- Py_XDecRef( Run_CommandAsObjectWithDict( command, file_input, locals, globals ) );
4704
+ Py_XDecRef(Run_CommandAsObjectWithDict(command, file_input, locals, globals, FileName) );
4663
4705
end ;
4664
4706
4665
- procedure TPythonEngine.ExecStrings ( strings : TStrings; locals, globals : PPyObject );
4707
+ procedure TPythonEngine.ExecStrings (strings: TStrings; locals, globals:
4708
+ PPyObject; const FileName: string = ' <string>' );
4666
4709
begin
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));
4668
4712
end ;
4669
4713
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;
4671
4716
begin
4672
- Result := Run_CommandAsObjectWithDict( command, eval_input, locals, globals );
4717
+ Result := Run_CommandAsObjectWithDict(command, eval_input, locals, globals, FileName );
4673
4718
end ;
4674
4719
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;
4676
4722
begin
4677
- Result := Run_CommandAsObjectWithDict( strings.Text, eval_input, locals, globals );
4723
+ Result := Run_CommandAsObjectWithDict(EncodeString(strings.Text),
4724
+ eval_input, locals, globals, FileName);
4678
4725
end ;
4679
4726
4680
- function TPythonEngine.EvalStringsAsStr ( strings : TStrings ) : String;
4727
+ function TPythonEngine.EvalStringsAsStr (strings: TStrings; const FileName:
4728
+ string = ' <string>' ): string;
4681
4729
begin
4682
- Result := Run_CommandAsString( strings.Text, eval_input );
4730
+ Result := Run_CommandAsString(EncodeString( strings.Text) , eval_input, FileName );
4683
4731
end ;
4684
4732
4685
4733
function TPythonEngine.CheckEvalSyntax ( const str : AnsiString ) : Boolean;
@@ -5035,6 +5083,27 @@ function TPythonEngine.FindClient( const aName : AnsiString ) : TEngineClient;
5035
5083
end ;
5036
5084
end ;
5037
5085
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
+
5038
5107
function TPythonEngine.TypeByName ( const aTypeName : AnsiString ) : PPyTypeObject;
5039
5108
var
5040
5109
i : Integer;
0 commit comments