Skip to content

Commit b3bc5a1

Browse files
committed
Changed CleanString from method to function and added an overload.
1 parent 3815a98 commit b3bc5a1

File tree

2 files changed

+37
-45
lines changed

2 files changed

+37
-45
lines changed

PythonForDelphi/Components/Sources/Core/PythonEngine.pas

Lines changed: 36 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,6 @@ interface
498498
getattrofunc = function( ob1,ob2: PPyObject): PPyObject; cdecl;
499499
setattrofunc = function( ob1,ob2,ob3: PPyObject): integer; cdecl;
500500

501-
/// jah 29-sep-2000 : updated for python 2.0
502-
/// added from object.h
503501
getreadbufferproc = function ( ob1: PPyObject; i: NativeInt; ptr: Pointer): NativeInt; cdecl;
504502
getwritebufferproc= function ( ob1: PPyObject; i: NativeInt; ptr: Pointer): NativeInt; cdecl;
505503
getsegcountproc = function ( ob1: PPyObject; i: NativeInt): NativeInt; cdecl;
@@ -542,8 +540,7 @@ interface
542540
nb_oct : unaryfunc;
543541
nb_hex : unaryfunc;
544542

545-
/// jah 29-sep-2000 : updated for python 2.0
546-
/// added from .h
543+
// Updated for python 2.0
547544
nb_inplace_add : binaryfunc;
548545
nb_inplace_subtract : binaryfunc;
549546
nb_inplace_multiply : binaryfunc;
@@ -573,9 +570,6 @@ interface
573570
sq_slice : ssizessizeargfunc;
574571
sq_ass_item : ssizeobjargproc;
575572
sq_ass_slice : ssizessizeobjargproc;
576-
577-
/// jah 29-sep-2000 : updated for python 2.0
578-
/// added from .h
579573
sq_contains : objobjproc;
580574
sq_inplace_concat : binaryfunc;
581575
sq_inplace_repeat : ssizeargfunc;
@@ -589,8 +583,6 @@ interface
589583
end;
590584
PPyMappingMethods = ^PyMappingMethods;
591585

592-
/// jah 29-sep-2000 : updated for python 2.0
593-
/// added from .h
594586
PyBufferProcs = {$IFNDEF CPUX64}packed{$ENDIF} record
595587
bf_getreadbuffer : getreadbufferproc;
596588
bf_getwritebuffer : getwritebufferproc;
@@ -777,10 +769,10 @@ interface
777769
// Methods to implement standard operations
778770

779771
tp_dealloc: pydestructor;
780-
tp_print: printfunc;
772+
tp_print: printfunc; // not available and replaced in Python 3.x
781773
tp_getattr: getattrfunc;
782774
tp_setattr: setattrfunc;
783-
tp_compare: cmpfunc;
775+
tp_compare: cmpfunc; // not available and replaced in Python 3.x
784776
tp_repr: reprfunc;
785777

786778
// Method suites for standard classes
@@ -797,8 +789,6 @@ interface
797789
tp_getattro: getattrofunc;
798790
tp_setattro: setattrofunc;
799791

800-
/// jah 29-sep-2000 : updated for python 2.0
801-
802792
// Functions to access object as input/output buffer
803793
tp_as_buffer: PPyBufferProcs;
804794
// Flags to define presence of optional/expanded features
@@ -1297,9 +1287,6 @@ EPyExecError = class(EPythonError);
12971287

12981288
// Standard exception classes of Python
12991289

1300-
/// jah 29-sep-2000 : updated for python 2.0
1301-
/// base classes updated according python documentation
1302-
13031290
{ Hierarchy of Python exceptions, Python 2.3, copied from <INSTALL>\Python\exceptions.c
13041291
13051292
Exception\n\
@@ -1994,7 +1981,6 @@ TPythonInterface=class(TDynamicDll)
19941981
PyParser_SimpleParseStringFlags : function ( str : PAnsiChar; start, flags : Integer) : PNode; cdecl;
19951982
PyNode_Free : procedure( n : PNode ); cdecl;
19961983
PyErr_NewException : function ( name : PAnsiChar; base, dict : PPyObject ) : PPyObject; cdecl;
1997-
Py_Malloc : function ( size : NativeInt ) : Pointer;
19981984
PyMem_Malloc : function ( size : NativeInt ) : Pointer;
19991985

20001986
{New exported Objects in Python 1.5}
@@ -2268,7 +2254,6 @@ TPythonEngine = class(TPythonInterface)
22682254
procedure SetProgramName(const ProgramName: string);
22692255
function IsType(ob: PPyObject; obt: PPyTypeObject): Boolean;
22702256
function GetAttrString(obj: PPyObject; AName: PAnsiChar):PAnsiChar;
2271-
function CleanString(const s : AnsiString; AppendLF : Boolean = True) : AnsiString;
22722257
function Run_CommandAsString(const command : AnsiString; mode : Integer) : String;
22732258
function Run_CommandAsObject(const command : AnsiString; mode : Integer) : PPyObject;
22742259
function Run_CommandAsObjectWithDict(const command : AnsiString; mode : Integer; locals, globals : PPyObject) : PPyObject;
@@ -3186,6 +3171,11 @@ function IsPythonVersionRegistered(PythonVersion : string;
31863171
*)
31873172
procedure MaskFPUExceptions(ExceptionsMasked : boolean;
31883173
MatchPythonPrecision : Boolean = True);
3174+
(*
3175+
Converts line breaks to LF and optionally adds a line break at the end
3176+
*)
3177+
function CleanString(const s : AnsiString; AppendLF : Boolean = True) : AnsiString; overload;
3178+
function CleanString(const s : string; AppendLF : Boolean = True) : string; overload;
31893179

31903180
//#######################################################
31913181
//## ##
@@ -4119,12 +4109,7 @@ procedure TPythonInterface.MapDll;
41194109
PyParser_SimpleParseStringFlags := Import('PyParser_SimpleParseStringFlags');
41204110
PyNode_Free :=Import('PyNode_Free');
41214111
PyErr_NewException :=Import('PyErr_NewException');
4122-
/// jah 29-sep-2000 : updated for python 2.0
4123-
/// replaced Py_Malloc with PyMem_Malloc
4124-
///--- @Py_Malloc := Import ('Py_Malloc');
4125-
///+++ @Py_Malloc := Import ('PyMem_Malloc');
41264112
try
4127-
Py_Malloc := Import ('PyMem_Malloc');
41284113
PyMem_Malloc := Import ('PyMem_Malloc');
41294114
except
41304115
end;
@@ -5231,23 +5216,6 @@ function TPythonEngine.GetAttrString(obj: PPyObject; AName: PAnsiChar):PAnsiChar
52315216
PyErr_Clear;
52325217
end;
52335218

5234-
function TPythonEngine.CleanString(const s : AnsiString; AppendLF : Boolean) : AnsiString;
5235-
var
5236-
i : Integer;
5237-
begin
5238-
result := s;
5239-
if s = '' then
5240-
Exit;
5241-
i := Pos(AnsiString(CR),s);
5242-
while i > 0 do
5243-
begin
5244-
Delete( result, i, 1 );
5245-
i := PosEx(AnsiString(CR),result, i);
5246-
end;
5247-
if AppendLF and (result[length(result)] <> LF) then
5248-
Insert( LF, result, length(result)+1 );
5249-
end;
5250-
52515219
function TPythonEngine.EvalPyFunction(pyfunc, pyargs:PPyObject): Variant;
52525220
var presult :PPyObject;
52535221
begin
@@ -6819,7 +6787,7 @@ function TEventDef.GetDocString : AnsiString;
68196787
begin
68206788
Owner.Container.CheckEngine;
68216789
FTmpDocString :=
6822-
Owner.Container.Engine.CleanString(AnsiString(FDocString.Text), False);
6790+
Owner.Container.Engine.EncodeString(CleanString(FDocString.Text, False));
68236791
Result := fTmpDocString;
68246792
end;
68256793

@@ -7595,7 +7563,7 @@ procedure TPythonModule.DefineDocString;
75957563
if DocString.Text <> '' then
75967564
begin
75977565
doc :=
7598-
PyString_FromDelphiString(AdjustLineBreaks(FDocString.Text,tlbsLF));
7566+
PyString_FromDelphiString(CleanString(FDocString.Text, False));
75997567
PyObject_SetAttrString( FModule, '__doc__', doc );
76007568
Py_XDecRef(doc);
76017569
CheckError(False);
@@ -8857,9 +8825,9 @@ procedure TPythonType.InitServices;
88578825
begin
88588826
// Basic services
88598827
if FDocString.Count > 0 then
8860-
With Engine do
8828+
with Engine do
88618829
begin
8862-
FCurrentDocString := CleanString(EncodeString(FDocString.Text), False);
8830+
FCurrentDocString := EncodeString(CleanString(FDocString.Text, False));
88638831
tp_doc := PAnsiChar(FCurrentDocString);
88648832
end;
88658833
tp_dealloc := @PyObjectDestructor;
@@ -9875,6 +9843,30 @@ procedure MaskFPUExceptions(ExceptionsMasked : boolean;
98759843
{$ENDIF !NEXTGEN}{$WARN SYMBOL_PLATFORM ON}
98769844
end;
98779845

9846+
function CleanString(const s : AnsiString; AppendLF : Boolean) : AnsiString;
9847+
var
9848+
i : Integer;
9849+
begin
9850+
result := s;
9851+
if s = '' then
9852+
Exit;
9853+
i := Pos(AnsiString(CR),s);
9854+
while i > 0 do
9855+
begin
9856+
Delete( result, i, 1 );
9857+
i := PosEx(AnsiString(CR),result, i);
9858+
end;
9859+
if AppendLF and (result[length(result)] <> LF) then
9860+
Result := Result + LF;
9861+
end;
9862+
9863+
function CleanString(const s : string; AppendLF : Boolean) : string;
9864+
begin
9865+
Result := AdjustLineBreaks(s, tlbsLF);
9866+
if AppendLF and (result[length(result)] <> LF) then
9867+
Result := Result + LF;
9868+
end;
9869+
98789870
{$IFDEF MSWINDOWS}
98799871
function IsPythonVersionRegistered(PythonVersion : string;
98809872
out InstallPath: string; out AllUserInstall: Boolean) : Boolean;

PythonForDelphi/Components/Sources/Core/WrapDelphiClasses.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,7 +1304,7 @@ function TPyDelphiStrings.Get_Text(AContext: Pointer): PPyObject;
13041304
begin
13051305
Adjust(@Self);
13061306
Result := GetPythonEngine.PyString_FromDelphiString(
1307-
AdjustLineBreaks(DelphiObject.Text, tlbsLF));
1307+
CleanString(DelphiObject.Text, False));
13081308
end;
13091309

13101310
function TPyDelphiStrings.IndexOf_Wrapper(args: PPyObject): PPyObject;

0 commit comments

Comments
 (0)