@@ -181,14 +181,29 @@ TPythonVersionProp = record
181
181
// Delphi equivalent used by TPyObject
182
182
TRichComparisonOpcode = (pyLT, pyLE, pyEQ, pyNE, pyGT, pyGE);
183
183
184
+
185
+ // C long is 8 bytes in non-Windows 64-bit operating systems
186
+ // Same Delphi's LongInt but not fpc LongInt which is always 4 bytes
187
+ // Hence the following
184
188
{ $IFDEF MSWINDOWS}
185
189
C_Long = Integer;
186
190
C_ULong = Cardinal;
187
191
{ $ELSE}
188
- C_Long= NativeInt;
192
+ C_Long = NativeInt;
189
193
C_ULong = NativeUInt;
190
194
{ $ENDIF}
191
195
196
+ // wchar_t is 4 bytes on Linux/OS X/Android but 2 bytes on Windows
197
+ { $IFDEF POSIX}
198
+ PWCharT = PUCSChar;
199
+ PPWCharT = PUCSChar^;
200
+ WCharTString = UCS4String;
201
+ { $ELSE}
202
+ PWCharT = PWideChar;
203
+ PPWCharT = PPWideChar;
204
+ WCharTString = UnicodeString;
205
+ { $ENDIF}
206
+
192
207
const
193
208
{
194
209
Type flags (tp_flags)
@@ -1380,7 +1395,7 @@ TPythonInterface=class(TDynamicDll)
1380
1395
PyRun_SimpleString: function( str: PAnsiChar): Integer; cdecl;
1381
1396
PyBytes_AsString: function( ob: PPyObject): PAnsiChar; cdecl;
1382
1397
PyBytes_AsStringAndSize: function( ob: PPyObject; var buffer: PAnsiChar; var size: NativeInt): integer; cdecl;
1383
- PySys_SetArgv: procedure( argc: Integer; argv: PPWideChar ); cdecl;
1398
+ PySys_SetArgv: procedure( argc: Integer; argv: PPWCharT ); cdecl;
1384
1399
1385
1400
PyCFunction_NewEx: function(md:PPyMethodDef;self, ob:PPyObject):PPyObject; cdecl;
1386
1401
// Removed. Use PyEval_CallObjectWithKeywords with third argument nil
@@ -1539,11 +1554,11 @@ TPythonInterface=class(TDynamicDll)
1539
1554
PyType_GenericAlloc:function(atype: PPyTypeObject; nitems:NativeInt) : PPyObject; cdecl;
1540
1555
PyType_GenericNew:function(atype: PPyTypeObject; args, kwds : PPyObject) : PPyObject; cdecl;
1541
1556
PyType_Ready:function(atype: PPyTypeObject) : integer; cdecl;
1542
- PyUnicode_FromWideChar:function (const w:PWideChar ; size:NativeInt):PPyObject; cdecl;
1557
+ PyUnicode_FromWideChar:function (const w:PWCharT ; size:NativeInt):PPyObject; cdecl;
1543
1558
PyUnicode_FromString:function (s:PAnsiChar):PPyObject; cdecl;
1544
1559
PyUnicode_FromStringAndSize:function (s:PAnsiChar;i:NativeInt):PPyObject; cdecl;
1545
1560
PyUnicode_FromKindAndData:function (kind:integer;const buffer:pointer;size:NativeInt):PPyObject; cdecl;
1546
- PyUnicode_AsWideChar:function (unicode: PPyObject; w:PWideChar ; size:NativeInt):integer; cdecl;
1561
+ PyUnicode_AsWideChar:function (unicode: PPyObject; w:PWCharT ; size:NativeInt):integer; cdecl;
1547
1562
PyUnicode_AsUTF8:function (unicode: PPyObject):PAnsiChar; cdecl;
1548
1563
PyUnicode_AsUTF8AndSize:function (unicode: PPyObject; size: PNativeInt):PAnsiChar; cdecl;
1549
1564
PyUnicode_Decode:function (const s:PAnsiChar; size: NativeInt; const encoding : PAnsiChar; const errors: PAnsiChar):PPyObject; cdecl;
@@ -1570,8 +1585,8 @@ TPythonInterface=class(TDynamicDll)
1570
1585
Py_GetCopyright : function : PAnsiChar; cdecl;
1571
1586
Py_GetExecPrefix : function : PAnsiChar; cdecl;
1572
1587
Py_GetPath : function : PAnsiChar; cdecl;
1573
- Py_SetPythonHome : procedure (home : PWideChar ); cdecl;
1574
- Py_GetPythonHome : function : PWideChar ; cdecl;
1588
+ Py_SetPythonHome : procedure (home : PWCharT ); cdecl;
1589
+ Py_GetPythonHome : function : PWCharT ; cdecl;
1575
1590
Py_GetPrefix : function : PAnsiChar; cdecl;
1576
1591
Py_GetProgramName : function : PAnsiChar; cdecl;
1577
1592
@@ -1580,7 +1595,7 @@ TPythonInterface=class(TDynamicDll)
1580
1595
PyErr_NewException : function ( name : PAnsiChar; base, dict : PPyObject ) : PPyObject; cdecl;
1581
1596
PyMem_Malloc : function ( size : NativeUInt ) : Pointer;
1582
1597
1583
- Py_SetProgramName : procedure( name : PWideChar ); cdecl;
1598
+ Py_SetProgramName : procedure( name : PWCharT ); cdecl;
1584
1599
Py_IsInitialized : function : integer; cdecl;
1585
1600
Py_GetProgramFullPath : function : PAnsiChar; cdecl;
1586
1601
Py_NewInterpreter : function : PPyThreadState; cdecl;
@@ -1754,8 +1769,8 @@ TPythonEngine = class(TPythonInterface)
1754
1769
FClients: TList;
1755
1770
FExecModule: AnsiString;
1756
1771
FAutoFinalize: Boolean;
1757
- FProgramName: UnicodeString ;
1758
- FPythonHome: UnicodeString ;
1772
+ FProgramName: WCharTString ;
1773
+ FPythonHome: WCharTString ;
1759
1774
FInitThreads: Boolean;
1760
1775
FOnPathInitialization: TPathInitializationEvent;
1761
1776
FOnSysPathInit: TSysPathInitEvent;
@@ -1775,6 +1790,8 @@ TPythonEngine = class(TPythonInterface)
1775
1790
FPyDateTime_TZInfoType: PPyObject;
1776
1791
FPyDateTime_TimeTZType: PPyObject;
1777
1792
FPyDateTime_DateTimeTZType: PPyObject;
1793
+ function GetPythonHome : UnicodeString;
1794
+ function GetProgramName : UnicodeString;
1778
1795
1779
1796
protected
1780
1797
procedure Initialize ;
@@ -1892,8 +1909,8 @@ TPythonEngine = class(TPythonInterface)
1892
1909
property LocalVars : PPyObject read FLocalVars Write SetLocalVars;
1893
1910
property GlobalVars : PPyObject read FGlobalVars Write SetGlobalVars;
1894
1911
property IOPythonModule: TObject read FIOPythonModule; { TPythonModule}
1895
- property PythonHome: UnicodeString read FPythonHome write SetPythonHome;
1896
- property ProgramName: UnicodeString read FProgramName write SetProgramName;
1912
+ property PythonHome: UnicodeString read GetPythonHome write SetPythonHome;
1913
+ property ProgramName: UnicodeString read GetProgramName write SetProgramName;
1897
1914
published
1898
1915
property AutoFinalize: Boolean read FAutoFinalize write FAutoFinalize default True;
1899
1916
property VenvPythonExe: string read FVenvPythonExe write FVenvPythonExe;
@@ -4202,12 +4219,12 @@ procedure TPythonEngine.Initialize;
4202
4219
if Assigned(Py_SetProgramName) then
4203
4220
begin
4204
4221
if FProgramName = ' ' then
4205
- FProgramName := UnicodeString(ParamStr(0 ));
4206
- Py_SetProgramName(PWideChar (FProgramName));
4222
+ ProgramName := UnicodeString(ParamStr(0 ));
4223
+ Py_SetProgramName(PWCharT (FProgramName));
4207
4224
end ;
4208
4225
AssignPyFlags;
4209
- if FPythonHome <> ' ' then
4210
- Py_SetPythonHome(PWideChar (FPythonHome));
4226
+ if Length( FPythonHome) > 0 then
4227
+ Py_SetPythonHome(PWCharT (FPythonHome));
4211
4228
Py_Initialize;
4212
4229
if Assigned(Py_IsInitialized) then
4213
4230
FInitialized := Py_IsInitialized() <> 0
@@ -4359,34 +4376,25 @@ procedure TPythonEngine.CheckRegistry;
4359
4376
procedure TPythonEngine.SetProgramArgs ;
4360
4377
var
4361
4378
i, argc : Integer;
4362
- wargv : array of PWideChar;
4363
- { $IFDEF POSIX}
4364
- UCS4L : array of UCS4String;
4365
- { $ELSE}
4366
- WL : array of UnicodeString;
4367
- { $ENDIF}
4379
+ wargv : array of PWCharT;
4380
+ WL : array of WCharTString;
4368
4381
begin
4369
4382
// we build a string list of the arguments, because ParamStr returns a volatile string
4370
4383
// and we want to build an array of PAnsiChar, pointing to valid strings.
4371
4384
argc := ParamCount;
4372
4385
SetLength(wargv, argc + 1 );
4373
4386
// build the PWideChar array
4374
- { $IFDEF POSIX}
4375
- // Note that Linux uses UCS4 strings, whereas it declares using UCS2 strings!!!
4376
- SetLength(UCS4L, argc+1 );
4377
- for i := 0 to argc do begin
4378
- UCS4L[i] := WideStringToUCS4String(ParamStr(i));
4379
- wargv[i] := @UCS4L[i][0 ];
4380
- end ;
4381
- { $ELSE}
4382
4387
SetLength(WL, argc+1 );
4383
4388
for i := 0 to argc do begin
4389
+ { $IFDEF POSIX}
4390
+ WL := UnicodeStringToUCS4String(ParamStr(i));
4391
+ { $ELSE}
4384
4392
WL[i] := UnicodeString(ParamStr(i));
4385
- wargv[i] := PWideChar(WL[i]);
4393
+ { $ENDIF}
4394
+ wargv[i] := PWCharT(WL[i]);
4386
4395
end ;
4387
- { $ENDIF}
4388
4396
// set the argv list of the sys module with the application arguments
4389
- PySys_SetArgv( argc + 1 , PPWideChar (wargv) );
4397
+ PySys_SetArgv( argc + 1 , PPWCharT (wargv) );
4390
4398
end ;
4391
4399
4392
4400
procedure TPythonEngine.InitWinConsole ;
@@ -4451,14 +4459,40 @@ procedure TPythonEngine.SetPyFlags(const Value: TPythonFlags);
4451
4459
end ; // of if
4452
4460
end ;
4453
4461
4462
+ function TPythonEngine.GetPythonHome : UnicodeString;
4463
+ begin
4464
+ { $IFDEF POSIX}
4465
+ Result := UCS4StringToUnicodeString(FPythonHome);
4466
+ { $ELSE}
4467
+ Result := FPythonHome;
4468
+ { $ENDIF}
4469
+ end ;
4470
+
4471
+ function TPythonEngine.GetProgramName : UnicodeString;
4472
+ begin
4473
+ { $IFDEF POSIX}
4474
+ Result := UCS4StringToUnicodeString(FProgramName);
4475
+ { $ELSE}
4476
+ Result := FProgramName;
4477
+ { $ENDIF}
4478
+ end ;
4479
+
4454
4480
procedure TPythonEngine.SetPythonHome (const PythonHome: UnicodeString);
4455
4481
begin
4456
- FPythonHome := PythonHome;
4482
+ { $IFDEF POSIX}
4483
+ FPythonHome := UnicodeStringToUCS4String(PythonHome);
4484
+ { $ELSE}
4485
+ FPythonHome := PythonHome;
4486
+ { $ENDIF}
4457
4487
end ;
4458
4488
4459
4489
procedure TPythonEngine.SetProgramName (const ProgramName: UnicodeString);
4460
4490
begin
4491
+ { $IFDEF POSIX}
4492
+ FProgramName := UnicodeStringToUCS4String(ProgramName);
4493
+ { $ELSE}
4461
4494
FProgramName := ProgramName;
4495
+ { $ENDIF}
4462
4496
end ;
4463
4497
4464
4498
function TPythonEngine.IsType (ob: PPyObject; obt: PPyTypeObject): Boolean;
@@ -5638,7 +5672,7 @@ function TPythonEngine.PyUnicodeAsString( obj : PPyObject ) : UnicodeString;
5638
5672
if PyUnicode_AsWideChar(obj, @_ucs4Str[0 ], _size) <> _size then
5639
5673
raise EPythonError.Create(' Could not copy the whole Unicode string into its buffer' );
5640
5674
Result := UCS4StringToWideString(_ucs4Str);
5641
- // remove trailing zeros (needed by Kylix1)
5675
+ // remove trailing zeros
5642
5676
while (Length(Result) > 0 ) and (Result[Length(Result)] = #0 ) do
5643
5677
Delete(Result, Length(Result), 1 );
5644
5678
{ $ELSE}
@@ -5683,9 +5717,9 @@ function TPythonEngine.PyUnicodeFromString(const AString : UnicodeString) : PPyO
5683
5717
{ $IFDEF POSIX}
5684
5718
// Note that Linux uses UCS4 strings, whereas it declares using UCS2 strings!!!
5685
5719
_ucs4Str := WideStringToUCS4String(AString);
5686
- Result := PyUnicode_FromWideChar( { PWideChar } ( @_ucs4Str[0 ]) , Length(_ucs4Str)-1 { trim trailing zero} );
5720
+ Result := PyUnicode_FromWideChar(@_ucs4Str[0 ], Length(_ucs4Str)-1 { trim trailing zero} );
5687
5721
{ $ELSE}
5688
- Result := PyUnicode_FromWideChar( PWideChar(AString), Length(AString) );
5722
+ Result := PyUnicode_FromWideChar(PWideChar(AString), Length(AString));
5689
5723
{ $ENDIF}
5690
5724
end ;
5691
5725
0 commit comments