From 8a726f56c4dcdf6f8b9ca2a33b6544b36d3dca69 Mon Sep 17 00:00:00 2001 From: Norayr Chilingarian Date: Mon, 13 Mar 2023 05:28:09 +0400 Subject: [PATCH 1/3] changes to fix Lazarus and FreePascal compilation issues. --- Source/PythonEngine.pas | 6 +++++- Source/WrapDelphi.pas | 3 +++ Source/WrapDelphiClasses.pas | 19 +++++++++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Source/PythonEngine.pas b/Source/PythonEngine.pas index 4ce8aba2..c065676a 100644 --- a/Source/PythonEngine.pas +++ b/Source/PythonEngine.pas @@ -6066,7 +6066,11 @@ function TPythonEngine.PyUnicodeAsString(obj : PPyObject): UnicodeString; Exit; // The second argument is the size of the destination (Result) including #0 - NewSize := Utf8ToUnicode(PChar(Result), Cardinal(Size + 1), Buffer, Cardinal(Size)); + {$IFDEF FPC} + NewSize := Utf8ToUnicode(PUnicodeChar(Result), Cardinal(Size + 1), Buffer, Cardinal(Size)); + {$ELSE} + NewSize := Utf8ToUnicode(PChar(Result), Cardinal(Size + 1), Buffer, Cardinal(Size)); + {$ENDIF} // NewSize includes #0 SetLength(Result, NewSize - 1); end diff --git a/Source/WrapDelphi.pas b/Source/WrapDelphi.pas index a6fdbf05..a4f732d7 100644 --- a/Source/WrapDelphi.pas +++ b/Source/WrapDelphi.pas @@ -2571,6 +2571,9 @@ function TPyDelphiObject.Dir_Wrapper(args: PPyObject): PPyObject; var PyType: PPyTypeObject; + {$IFDEF FPC} + i: longint; + {$ENDIF} {$IFDEF EXTENDED_RTTI} Context: TRttiContext; RttiType: TRTTIType; diff --git a/Source/WrapDelphiClasses.pas b/Source/WrapDelphiClasses.pas index 23ca2d56..44ee1d17 100644 --- a/Source/WrapDelphiClasses.pas +++ b/Source/WrapDelphiClasses.pas @@ -5,7 +5,8 @@ interface uses - Classes, SysUtils, PythonEngine, WrapDelphi; + Classes, SysUtils, PythonEngine, WrapDelphi + {$IFDEF FPC}, bufstream{$ENDIF}; type { @@ -367,6 +368,7 @@ implementation uses TypInfo {$IFNDEF FPC}, System.Rtti{$ENDIF}; + {$IFNDEF FPC} type TPyReader = class(TReader) @@ -2223,7 +2225,12 @@ TBufferedFileStreamClass = class of TBufferedFileStream; DelphiObject := TBufferedFileStreamClass(DelphiObjectClass).Create(String(LFileName), LMode, LBufferSize); end else if (LArgCount = 3) then begin if (APythonType.Engine.PyArg_ParseTupleAndKeywords(args, kwds, 'sHI|i:Create', @LKwArgs2[0], @LFileName, @LMode, @LRights, @LBufferSize) <> 0) then - DelphiObject := TBufferedFileStreamClass(DelphiObjectClass).Create(String(LFileName), LMode, LRights, LBufferSize); + {$IFDEF FPC} + DelphiObject := TBufferedFileStreamClass(DelphiObjectClass).Create(String(LFileName), LMode, LRights); + DelphiObject.Size:= LBufferSize; + {$ELSE} + DelphiObject := TBufferedFileStreamClass(DelphiObjectClass).Create(String(LFileName), LMode, LRights, LBufferSize); + {$ENDIF} end; //Maybe it was created on the next attempt... @@ -2385,14 +2392,22 @@ TResourceStreamClass = class of TResourceStream; {$ELSE} if APythonType.Engine.PyArg_ParseTuple(args, 'Iss:Create', @LHandle, @LResName, @LResType) <> 0 then {$ENDIF} + {$IFDEF FPC} + DelphiObject := TResourceStreamClass(DelphiObjectClass).Create(LHandle, String(LResName), PChar(String(LResType))) + {$ELSE} DelphiObject := TResourceStreamClass(DelphiObjectClass).Create(LHandle, String(LResName), PWideChar(String(LResType))) + {$ENDIF} else {$IFDEF CPUX64} if APythonType.Engine.PyArg_ParseTuple(args, 'Kis:Create', @LHandle, @LResId, @LResType) <> 0 then {$ELSE} if APythonType.Engine.PyArg_ParseTuple(args, 'Iis:Create', @LHandle, @LResId, @LResType) <> 0 then {$ENDIF} + {$IFDEF FPC} + DelphiObject := TResourceStreamClass(DelphiObjectClass).CreateFromID(LHandle, LResId, PChar(String(LResType))); + {$ELSE} DelphiObject := TResourceStreamClass(DelphiObjectClass).CreateFromID(LHandle, LResId, PWideChar(String(LResType))); + {$ENDIF} except on E: Exception do with GetPythonEngine do From 62eab068cff182010034fde2de2b09bdc7597911 Mon Sep 17 00:00:00 2001 From: Norayr Chilingarian Date: Tue, 21 Mar 2023 05:10:33 +0400 Subject: [PATCH 2/3] addressed code review comments by pyscripter. --- Source/PythonEngine.pas | 7 ++----- Source/WrapDelphi.pas | 2 ++ Source/WrapDelphiClasses.pas | 12 ++---------- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/Source/PythonEngine.pas b/Source/PythonEngine.pas index c065676a..64813974 100644 --- a/Source/PythonEngine.pas +++ b/Source/PythonEngine.pas @@ -6066,11 +6066,8 @@ function TPythonEngine.PyUnicodeAsString(obj : PPyObject): UnicodeString; Exit; // The second argument is the size of the destination (Result) including #0 - {$IFDEF FPC} - NewSize := Utf8ToUnicode(PUnicodeChar(Result), Cardinal(Size + 1), Buffer, Cardinal(Size)); - {$ELSE} - NewSize := Utf8ToUnicode(PChar(Result), Cardinal(Size + 1), Buffer, Cardinal(Size)); - {$ENDIF} + NewSize := Utf8ToUnicode(PWideChar(Result), Cardinal(Size + 1), Buffer, Cardinal(Size)); + // NewSize includes #0 SetLength(Result, NewSize - 1); end diff --git a/Source/WrapDelphi.pas b/Source/WrapDelphi.pas index a4f732d7..56daf4b0 100644 --- a/Source/WrapDelphi.pas +++ b/Source/WrapDelphi.pas @@ -2573,6 +2573,8 @@ function TPyDelphiObject.Dir_Wrapper(args: PPyObject): PPyObject; PyType: PPyTypeObject; {$IFDEF FPC} i: longint; + {$ELSE} + i: integer; {$ENDIF} {$IFDEF EXTENDED_RTTI} Context: TRttiContext; diff --git a/Source/WrapDelphiClasses.pas b/Source/WrapDelphiClasses.pas index 44ee1d17..7c1d8867 100644 --- a/Source/WrapDelphiClasses.pas +++ b/Source/WrapDelphiClasses.pas @@ -2392,22 +2392,14 @@ TResourceStreamClass = class of TResourceStream; {$ELSE} if APythonType.Engine.PyArg_ParseTuple(args, 'Iss:Create', @LHandle, @LResName, @LResType) <> 0 then {$ENDIF} - {$IFDEF FPC} - DelphiObject := TResourceStreamClass(DelphiObjectClass).Create(LHandle, String(LResName), PChar(String(LResType))) - {$ELSE} - DelphiObject := TResourceStreamClass(DelphiObjectClass).Create(LHandle, String(LResName), PWideChar(String(LResType))) - {$ENDIF} + DelphiObject := TResourceStreamClass(DelphiObjectClass).Create(LHandle, String(LResName), PChar(String(LResType))) else {$IFDEF CPUX64} if APythonType.Engine.PyArg_ParseTuple(args, 'Kis:Create', @LHandle, @LResId, @LResType) <> 0 then {$ELSE} if APythonType.Engine.PyArg_ParseTuple(args, 'Iis:Create', @LHandle, @LResId, @LResType) <> 0 then {$ENDIF} - {$IFDEF FPC} - DelphiObject := TResourceStreamClass(DelphiObjectClass).CreateFromID(LHandle, LResId, PChar(String(LResType))); - {$ELSE} - DelphiObject := TResourceStreamClass(DelphiObjectClass).CreateFromID(LHandle, LResId, PWideChar(String(LResType))); - {$ENDIF} + DelphiObject := TResourceStreamClass(DelphiObjectClass).CreateFromID(LHandle, LResId, PChar(String(LResType))); except on E: Exception do with GetPythonEngine do From 6b248612180b3fabeda0dcfa87e3741e171e8506 Mon Sep 17 00:00:00 2001 From: Norayr Chilingarian Date: Tue, 21 Mar 2023 14:52:25 +0400 Subject: [PATCH 3/3] These changes are in response to PR comments --- Source/WrapDelphi.pas | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/Source/WrapDelphi.pas b/Source/WrapDelphi.pas index 56daf4b0..523f8c8d 100644 --- a/Source/WrapDelphi.pas +++ b/Source/WrapDelphi.pas @@ -2571,17 +2571,14 @@ function TPyDelphiObject.Dir_Wrapper(args: PPyObject): PPyObject; var PyType: PPyTypeObject; - {$IFDEF FPC} - i: longint; - {$ELSE} - i: integer; - {$ENDIF} + {$IFDEF EXTENDED_RTTI} Context: TRttiContext; RttiType: TRTTIType; {$ELSE} _PropList: PPropList; _propCount : Integer; + i: Integer; {$ENDIF} begin Adjust(@Self);