Skip to content

Commit 3815a98

Browse files
committed
Added PyString_FromDelphiString to avoid unnecessary string conversions.
Replaced calls to PyString_FromString with calls to PyString_FromDelphi string Refactored WrapDelphi units
1 parent 6b88fc2 commit 3815a98

13 files changed

+2301
-2739
lines changed

PythonForDelphi/Components/Sources/Core/PythonEngine.pas

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2132,7 +2132,6 @@ TPythonInterface=class(TDynamicDll)
21322132
function Py_InitModule( const AName : PAnsiChar; md : PPyMethodDef) : PPyObject;
21332133
function Py_InitModule3000( const md : PyModuleDef) : PPyObject;
21342134
function PyString_FromString( str: PAnsiChar): PPyObject; virtual; abstract;
2135-
function PyString_AsDelphiString( ob: PPyObject): string; virtual; abstract;
21362135
procedure Py_FlushLine; cdecl;
21372136

21382137
// Constructors & Destructors
@@ -2335,7 +2334,8 @@ TPythonEngine = class(TPythonInterface)
23352334
function PyTZInfo_CheckExact( obj : PPyObject ) : Boolean;
23362335
{ end date/time functions }
23372336
function PyString_FromString( str: PAnsiChar): PPyObject; override;
2338-
function PyString_AsDelphiString( ob: PPyObject): string; override;
2337+
function PyString_FromDelphiString(str : string): PPyObject;
2338+
function PyString_AsDelphiString( ob: PPyObject): string;
23392339
function PyString_AsAnsiString( ob: PPyObject): AnsiString;
23402340
function PyString_AsWideString( ob: PPyObject): UnicodeString;
23412341

@@ -6390,7 +6390,7 @@ function TPythonEngine.StringsToPyList( strings : TStrings ) : PPyObject;
63906390
raise EPythonError.Create('Could not create a new list object');
63916391
for i := 0 to strings.Count - 1 do
63926392
PyList_SetItem( Result, i,
6393-
PyString_FromString( PAnsiChar(AnsiString(strings.Strings[i])) ) );
6393+
PyString_FromDelphiString( strings.Strings[i]) );
63946394
end;
63956395

63966396
function TPythonEngine.StringsToPyTuple( strings : TStrings ) : PPyObject;
@@ -6402,7 +6402,7 @@ function TPythonEngine.StringsToPyTuple( strings : TStrings ) : PPyObject;
64026402
raise EPythonError.Create('Could not create a new tuple object');
64036403
for i := 0 to strings.Count - 1 do
64046404
PyTuple_SetItem( Result, i,
6405-
PyString_FromString( PAnsiChar(AnsiString(strings.Strings[i])) ) );
6405+
PyString_FromDelphiString( strings.Strings[i]) );
64066406
end;
64076407

64086408
procedure TPythonEngine.PyListToStrings( list : PPyObject; strings : TStrings );
@@ -6673,6 +6673,16 @@ function TPythonEngine.PyString_AsWideString( ob: PPyObject): UnicodeString;
66736673
Result := UnicodeString(PyString_AsString(ob));
66746674
end;
66756675

6676+
function TPythonEngine.PyString_FromDelphiString(str: string): PPyObject;
6677+
begin
6678+
if IsPython3000 then
6679+
begin
6680+
Result := PyUnicode_FromWideString(str);
6681+
end
6682+
else
6683+
Result := DLL_PyString_FromString(PAnsiChar(AnsiString(str)));
6684+
end;
6685+
66766686
function TPythonEngine.PyString_FromString( str: PAnsiChar): PPyObject;
66776687
var
66786688
_text : UnicodeString;
@@ -7585,8 +7595,7 @@ procedure TPythonModule.DefineDocString;
75857595
if DocString.Text <> '' then
75867596
begin
75877597
doc :=
7588-
PyString_FromString(PAnsiChar(CleanString(EncodeString(FDocString.Text),
7589-
False)) );
7598+
PyString_FromDelphiString(AdjustLineBreaks(FDocString.Text,tlbsLF));
75907599
PyObject_SetAttrString( FModule, '__doc__', doc );
75917600
Py_XDecRef(doc);
75927601
CheckError(False);
@@ -7924,9 +7933,9 @@ function TPyObject.SetAttr(key : PAnsiChar; value : PPyObject) : Integer;
79247933

79257934
function TPyObject.Repr : PPyObject;
79267935
begin
7927-
with GetPythonEngine do
7928-
Result :=
7929-
PyString_FromString( PAnsiChar(AnsiString(Format('<%s at %x>', [PythonType.TypeName, NativeInt(self)]))) );
7936+
Result :=
7937+
GetPythonEngine.PyString_FromDelphiString( Format('<%s at %x>',
7938+
[PythonType.TypeName, NativeInt(self)]) );
79307939
end;
79317940

79327941
function TPyObject.Compare( obj: PPyObject) : Integer;
@@ -9429,8 +9438,8 @@ function TPyVar.Repr : PPyObject;
94299438
obj := GetValue;
94309439
try
94319440
Result :=
9432-
PyString_FromString( PAnsiChar(AnsiString(Format('<%s: %s>',
9433-
[PythonType.TypeName, PyObjectAsString(obj)]))) );
9441+
PyString_FromDelphiString( Format('<%s: %s>',
9442+
[PythonType.TypeName, PyObjectAsString(obj)]) );
94349443
finally
94359444
Py_XDecRef(obj);
94369445
end;

PythonForDelphi/Components/Sources/Core/WrapDelphiActnList.pas

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,20 +96,16 @@ function TPyDelphiCustomActionList.GetDelphiObject: TCustomActionList;
9696

9797
function TPyDelphiCustomActionList.Get_ActionCount(AContext: Pointer): PPyObject;
9898
begin
99-
with GetPythonEngine do begin
100-
Adjust(@Self);
101-
Result := PyInt_FromLong(DelphiObject.ActionCount);
102-
end;
99+
Adjust(@Self);
100+
Result := GetPythonEngine.PyInt_FromLong(DelphiObject.ActionCount);
103101
end;
104102

105103
function TPyDelphiCustomActionList.Get_Actions(AContext: Pointer): PPyObject;
106104
begin
107-
with GetPythonEngine do begin
108-
Adjust(@Self);
109-
Result := PyDelphiWrapper.DefaultContainerType.CreateInstance;
110-
with PythonToDelphi(Result) as TPyDelphiContainer do
111-
Setup(Self.PyDelphiWrapper, TActionListAccess.Create(Self.PyDelphiWrapper, Self.DelphiObject));
112-
end;
105+
Adjust(@Self);
106+
Result := PyDelphiWrapper.DefaultContainerType.CreateInstance;
107+
with PythonToDelphi(Result) as TPyDelphiContainer do
108+
Setup(Self.PyDelphiWrapper, TActionListAccess.Create(Self.PyDelphiWrapper, Self.DelphiObject));
113109
end;
114110

115111
class procedure TPyDelphiCustomActionList.RegisterGetSets(

PythonForDelphi/Components/Sources/Core/WrapDelphiClasses.pas

Lines changed: 44 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ function ShiftToPython(AShift : TShiftState) : PPyObject;
292292
begin
293293
with GetPythonEngine do
294294
begin
295-
_item := PyString_FromString(PAnsiChar(AnsiString(AString)));
295+
_item := PyString_FromDelphiString(AString);
296296
PyList_Append(AList, _item);
297297
Py_XDecRef(_item);
298298
end;
@@ -338,15 +338,12 @@ function TPyDelphiPersistent.Assign_Wrapper(args: PPyObject): PPyObject;
338338
var
339339
_obj : PPyObject;
340340
begin
341-
with GetPythonEngine do
342-
begin
343-
// We adjust the transmitted self argument
344-
Adjust(@Self);
345-
if PyArg_ParseTuple( args, 'O:Assign',@_obj ) <> 0 then
346-
Result := Self.Assign(_obj)
347-
else
348-
Result := nil;
349-
end;
341+
// We adjust the transmitted self argument
342+
Adjust(@Self);
343+
if GetPythonEngine.PyArg_ParseTuple( args, 'O:Assign',@_obj ) <> 0 then
344+
Result := Self.Assign(_obj)
345+
else
346+
Result := nil;
350347
end;
351348

352349
class function TPyDelphiPersistent.DelphiObjectClass: TClass;
@@ -362,11 +359,11 @@ function TPyDelphiPersistent.GetDelphiObject: TPersistent;
362359
function TPyDelphiPersistent.GetNamePath_Wrapper(
363360
args: PPyObject): PPyObject;
364361
begin
362+
// We adjust the transmitted self argument
363+
Adjust(@Self);
365364
with GetPythonEngine do begin
366-
// We adjust the transmitted self argument
367-
Adjust(@Self);
368365
if PyArg_ParseTuple( args, ':GetNamePath' ) <> 0 then begin
369-
Result := PyString_FromString(PAnsiChar(AnsiString(DelphiObject.GetNamePath)))
366+
Result := PyString_FromDelphiString(DelphiObject.GetNamePath)
370367
end else
371368
Result := nil;
372369
end;
@@ -536,28 +533,22 @@ function TPyDelphiCollection.GetDelphiObject: TCollection;
536533

537534
function TPyDelphiCollection.Get_Count(AContext: Pointer): PPyObject;
538535
begin
539-
with GetPythonEngine do begin
540-
Adjust(@Self);
541-
Result := PyInt_FromLong(DelphiObject.Count);
542-
end;
536+
Adjust(@Self);
537+
Result := GetPythonEngine.PyInt_FromLong(DelphiObject.Count);
543538
end;
544539

545540
function TPyDelphiCollection.Get_Items(AContext: Pointer): PPyObject;
546541
begin
547-
with GetPythonEngine do begin
548-
Adjust(@Self);
549-
Result := Self.PyDelphiWrapper.DefaultContainerType.CreateInstance;
550-
with PythonToDelphi(Result) as TPyDelphiContainer do
551-
Setup(Self.PyDelphiWrapper, Self.ContainerAccess.Clone);
552-
end;
542+
Adjust(@Self);
543+
Result := Self.PyDelphiWrapper.DefaultContainerType.CreateInstance;
544+
with PythonToDelphi(Result) as TPyDelphiContainer do
545+
Setup(Self.PyDelphiWrapper, Self.ContainerAccess.Clone);
553546
end;
554547

555548
function TPyDelphiCollection.Get_Owner(AContext: Pointer): PPyObject;
556549
begin
557-
with GetPythonEngine do begin
558-
Adjust(@Self);
559-
Result := Wrap(DelphiObject.Owner);
560-
end;
550+
Adjust(@Self);
551+
Result := Wrap(DelphiObject.Owner);
561552
end;
562553

563554
function TPyDelphiCollection.Insert_Wrapper(args: PPyObject): PPyObject;
@@ -730,10 +721,10 @@ function TPyDelphiComponent.BindMethodsToEvents(args: PPyObject): PPyObject;
730721
_bindings : PPyObject;
731722
_type : PPyTypeObject;
732723
begin
724+
Adjust(@Self);
733725
_prefix := 'handle_';
734726
with GetPythonEngine do begin
735727
// We adjust the transmitted self argument
736-
Adjust(@Self);
737728
Result := nil;
738729
s := nil;
739730
if PyArg_ParseTuple( args, '|O:BindMethodsToEvents',@s ) <> 0 then
@@ -808,8 +799,8 @@ function TPyDelphiComponent.BindMethodsToEvents(args: PPyObject): PPyObject;
808799
else
809800
begin
810801
_pair := PyTuple_New(3);
811-
PyTuple_SetItem(_pair, 0, PyString_FromString(PAnsiChar(AnsiString(_compName))));
812-
PyTuple_SetItem(_pair, 1, PyString_FromString(PAnsiChar(AnsiString(_eventName))));
802+
PyTuple_SetItem(_pair, 0, PyString_FromDelphiString(_compName));
803+
PyTuple_SetItem(_pair, 1, PyString_FromDelphiString(_eventName));
813804
PyTuple_SetItem(_pair, 2, objMethod);
814805
PyList_Append(_bindings, _pair);
815806
end;
@@ -839,36 +830,30 @@ function TPyDelphiComponent.BindMethodsToEvents(args: PPyObject): PPyObject;
839830

840831
function TPyDelphiComponent.Get_ComponentCount(AContext: Pointer): PPyObject;
841832
begin
842-
with GetPythonEngine do begin
843-
Adjust(@Self);
844-
Result := PyInt_FromLong(DelphiObject.ComponentCount);
845-
end;
833+
Adjust(@Self);
834+
Result := GetPythonEngine.PyInt_FromLong(DelphiObject.ComponentCount);
846835
end;
847836

848837
function TPyDelphiComponent.Get_Components(AContext: Pointer): PPyObject;
849838
begin
850-
with GetPythonEngine do begin
851-
Adjust(@Self);
852-
Result := Self.PyDelphiWrapper.DefaultContainerType.CreateInstance;
853-
with PythonToDelphi(Result) as TPyDelphiContainer do
854-
Setup(Self.PyDelphiWrapper, Self.ContainerAccess.Clone);
855-
end;
839+
Adjust(@Self);
840+
Result := Self.PyDelphiWrapper.DefaultContainerType.CreateInstance;
841+
with PythonToDelphi(Result) as TPyDelphiContainer do
842+
Setup(Self.PyDelphiWrapper, Self.ContainerAccess.Clone);
856843
end;
857844

858845
function TPyDelphiComponent.Get_Owner(AContext: Pointer): PPyObject;
859846
begin
860-
with GetPythonEngine do begin
861-
Adjust(@Self);
862-
Result := Wrap(DelphiObject.Owner);
863-
end;
847+
Adjust(@Self);
848+
Result := Wrap(DelphiObject.Owner);
864849
end;
865850

866851
function TPyDelphiComponent.GetParentComponent_Wrapper(
867852
args: PPyObject): PPyObject;
868853
begin
854+
Adjust(@Self);
869855
with GetPythonEngine do begin
870856
// We adjust the transmitted self argument
871-
Adjust(@Self);
872857
if PyArg_ParseTuple( args, ':GetParentComponent') <> 0 then begin
873858
Result := Wrap(DelphiObject.GetParentComponent)
874859
end else
@@ -878,9 +863,9 @@ function TPyDelphiComponent.GetParentComponent_Wrapper(
878863

879864
function TPyDelphiComponent.HasParent_Wrapper(args: PPyObject): PPyObject;
880865
begin
866+
Adjust(@Self);
881867
with GetPythonEngine do begin
882868
// We adjust the transmitted self argument
883-
Adjust(@Self);
884869
if PyArg_ParseTuple( args, ':HasParent') <> 0 then begin
885870
Result := VariantAsPyObject(DelphiObject.HasParent)
886871
end else
@@ -1071,7 +1056,7 @@ function TStringsAccess.GetContainer: TStrings;
10711056

10721057
function TStringsAccess.GetItem(AIndex: Integer): PPyObject;
10731058
begin
1074-
Result := GetPythonEngine.PyString_FromString( PAnsiChar(AnsiString(Container[AIndex])) );
1059+
Result := GetPythonEngine.PyString_FromDelphiString( Container[AIndex] );
10751060
end;
10761061

10771062
function TStringsAccess.GetSize: Integer;
@@ -1302,28 +1287,24 @@ function TPyDelphiStrings.GetDelphiObject: TStrings;
13021287

13031288
function TPyDelphiStrings.Get_Capacity(AContext: Pointer): PPyObject;
13041289
begin
1305-
with GetPythonEngine do begin
1306-
Adjust(@Self);
1307-
Result := PyInt_FromLong(DelphiObject.Capacity);
1308-
end;
1290+
Adjust(@Self);
1291+
Result := GetPythonEngine.PyInt_FromLong(DelphiObject.Capacity);
13091292
end;
13101293

13111294
function TPyDelphiStrings.Get_Objects(AContext: Pointer): PPyObject;
13121295
begin
1313-
with GetPythonEngine do begin
1314-
Adjust(@Self);
1315-
Result := Self.PyDelphiWrapper.DefaultContainerType.CreateInstance;
1316-
with PythonToDelphi(Result) as TPyDelphiContainer do
1317-
Setup(Self.PyDelphiWrapper, TStringsObjectsAccess.Create(Self.PyDelphiWrapper, Self.DelphiObject));
1318-
end;
1296+
Adjust(@Self);
1297+
Result := Self.PyDelphiWrapper.DefaultContainerType.CreateInstance;
1298+
with PythonToDelphi(Result) as TPyDelphiContainer do
1299+
Setup(Self.PyDelphiWrapper, TStringsObjectsAccess.Create(Self.PyDelphiWrapper,
1300+
Self.DelphiObject));
13191301
end;
13201302

13211303
function TPyDelphiStrings.Get_Text(AContext: Pointer): PPyObject;
13221304
begin
1323-
with GetPythonEngine do begin
1324-
Adjust(@Self);
1325-
Result := PyString_FromString(PAnsiChar(GetPythonEngine.CleanString(AnsiString(DelphiObject.Text))));
1326-
end;
1305+
Adjust(@Self);
1306+
Result := GetPythonEngine.PyString_FromDelphiString(
1307+
AdjustLineBreaks(DelphiObject.Text, tlbsLF));
13271308
end;
13281309

13291310
function TPyDelphiStrings.IndexOf_Wrapper(args: PPyObject): PPyObject;
@@ -1438,9 +1419,8 @@ class procedure TPyDelphiStrings.RegisterMethods(PythonType: TPythonType);
14381419

14391420
function TPyDelphiStrings.Repr: PPyObject;
14401421
begin
1441-
with GetPythonEngine do
1442-
Result := PyString_FromString( PAnsiChar(AnsiString(Format('<Delphi TStrings at %x>',
1443-
[NativeInt(self)]))) );
1422+
Result := GetPythonEngine.PyString_FromDelphiString( Format('<Delphi TStrings at %x>',
1423+
[NativeInt(self)]) );
14441424
end;
14451425

14461426
function TPyDelphiStrings.SaveToFile_Wrapper(args: PPyObject): PPyObject;

0 commit comments

Comments
 (0)