Skip to content

Commit a0661e8

Browse files
committed
Added PyBytesAsAnsiString
Fixed Python 3.10 support
1 parent 74a70a7 commit a0661e8

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

Source/PythonEngine.pas

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,6 +1876,7 @@ TPythonEngine = class(TPythonInterface)
18761876
function PyUnicodeFromString(const AString : UnicodeString) : PPyObject; overload;
18771877
function PyUnicodeFromString(const AString: AnsiString): PPyObject; overload;
18781878
function PyUnicodeAsString( obj : PPyObject ) : UnicodeString;
1879+
function PyBytesAsAnsiString( obj : PPyObject ) : AnsiString;
18791880

18801881
// Public Properties
18811882
property ClientCount : Integer read GetClientCount;
@@ -2693,7 +2694,8 @@ procedure PyObjectDestructor( pSelf : PPyObject); cdecl;
26932694
procedure FreeSubtypeInst(ob:PPyObject); cdecl;
26942695
procedure Register;
26952696
function PyType_HasFeature(AType : PPyTypeObject; AFlag : Integer) : Boolean;
2696-
function GetPythonVersionFromDLLName(const DLLFileName : string): string;
2697+
function SysVersionFromDLLName(const DLLFileName : string): string;
2698+
procedure PythonVersionFromDLLName(const LibName: string; out MajorVersion, MinorVersion: integer);
26972699

26982700
{ Helper functions}
26992701
(*
@@ -2716,8 +2718,6 @@ procedure MaskFPUExceptions(ExceptionsMasked : boolean;
27162718
function CleanString(const s : AnsiString; AppendLF : Boolean = True) : AnsiString; overload;
27172719
function CleanString(const s : UnicodeString; AppendLF : Boolean = True) : UnicodeString; overload;
27182720

2719-
procedure DetectPythonVersionFromLibName(const LibName: string; out MajorVersion, MinorVersion: integer);
2720-
27212721
implementation
27222722

27232723
uses
@@ -3121,7 +3121,7 @@ constructor TPythonInterface.Create(AOwner: TComponent);
31213121
procedure TPythonInterface.AfterLoad;
31223122
begin
31233123
inherited;
3124-
DetectPythonVersionFromLibName(DLLName, FMajorVersion, FMinorVersion);
3124+
PythonVersionFromDLLName(DLLName, FMajorVersion, FMinorVersion);
31253125

31263126
FBuiltInModuleName := 'builtins';
31273127

@@ -3641,7 +3641,7 @@ function TPythonInterface.PyTuple_CheckExact(obj: PPyObject): Boolean;
36413641

36423642
function TPythonInterface.PyClass_Check( obj : PPyObject ) : Boolean;
36433643
begin
3644-
Result := Assigned( obj ) and (PyObject_IsInstance(obj, PPyObject(PyType_Type)) <> 0);
3644+
Result := Assigned( obj ) and (PyObject_IsInstance(obj, PPyObject(PyType_Type)) = 1);
36453645
end;
36463646

36473647
function TPythonInterface.PyType_CheckExact( obj : PPyObject ) : Boolean;
@@ -4071,7 +4071,7 @@ procedure TPythonEngine.DoOpenDll(const aDllName : string);
40714071
end;
40724072
end
40734073
else
4074-
RegVersion := GetPythonVersionFromDLLName(aDllName);
4074+
RegVersion := SysVersionFromDLLName(aDllName);
40754075
inherited;
40764076
end;
40774077

@@ -4851,7 +4851,8 @@ function TPythonEngine.PyObjectAsString( obj : PPyObject ) : string;
48514851
end;
48524852
S := PyObject_Str( obj );
48534853
if Assigned(S) and PyUnicode_Check(S) then
4854-
Result := PyUnicodeAsString(S);
4854+
W := PyUnicodeAsString(S);
4855+
Result := string(W);
48554856
Py_XDECREF(S);
48564857
end;
48574858

@@ -5567,6 +5568,20 @@ procedure TPythonEngine.PyTupleToStrings( tuple: PPyObject; strings : TStrings )
55675568
strings.Add( PyObjectAsString( PyTuple_GetItem( tuple, i ) ) );
55685569
end;
55695570

5571+
function TPythonEngine.PyBytesAsAnsiString(obj: PPyObject): AnsiString;
5572+
var
5573+
buffer: PAnsiChar;
5574+
size: NativeInt;
5575+
begin
5576+
if PyBytes_Check(obj) then
5577+
begin
5578+
PyBytes_AsStringAndSize(obj, buffer, size);
5579+
SetString(Result, buffer, size);
5580+
end
5581+
else
5582+
raise EPythonError.Create('PyBytesAsAnsiString expects a Bytes Python object');
5583+
end;
5584+
55705585
function TPythonEngine.PyUnicodeAsString( obj : PPyObject ) : UnicodeString;
55715586
var
55725587
_size : Integer;
@@ -8832,9 +8847,12 @@ procedure Register;
88328847
TPythonType, TPythonModule, TPythonDelphiVar]);
88338848
end;
88348849

8835-
function GetPythonVersionFromDLLName(const DLLFileName : string): string;
8850+
function SysVersionFromDLLName(const DLLFileName : string): string;
8851+
var
8852+
Minor, Major: integer;
88368853
begin
8837-
Result := DLLFileName[{$IFDEF MSWINDOWS}7{$ELSE}10{$ENDIF}] + '.' + DLLFileName[{$IFDEF MSWINDOWS}8{$ELSE}11{$ENDIF}];
8854+
PythonVersionFromDLLName(DLLFileName, Major, Minor);
8855+
Result := Format('%d.%d', [Major, Minor]);
88388856
end;
88398857

88408858
function PyType_HasFeature(AType : PPyTypeObject; AFlag : Integer) : Boolean;
@@ -8960,7 +8978,7 @@ function IsPythonVersionRegistered(PythonVersion : string;
89608978
end;
89618979
{$ENDIF}
89628980

8963-
procedure DetectPythonVersionFromLibName(const LibName: string; out MajorVersion, MinorVersion: integer);
8981+
procedure PythonVersionFromDLLName(const LibName: string; out MajorVersion, MinorVersion: integer);
89648982
var
89658983
NPos: integer;
89668984
S: String;

Source/PythonVersions.pas

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ implementation
9292
function TPythonVersion.GetDLLName: string;
9393
begin
9494
{$IFDEF MSWINDOWS}
95-
Result := 'python' + SysVersion[1] + SysVersion[3] + '.dll';
95+
Result := SysVersion;
96+
Delete(Result, 2, 1);
97+
Result := 'python' + Result + '.dll';
9698
{$ELSE}
9799
Result := 'libpython' + SysVersion + '.so';
98100
{$ENDIF}
@@ -506,7 +508,7 @@ function PythonVersionFromPath(const Path: string; out PythonVersion: TPythonVer
506508
end;
507509
PythonVersion.DLLPath := DLLPath;
508510

509-
SysVersion := GetPythonVersionFromDLLName(DLLFileName);
511+
SysVersion := SysVersionFromDLLName(DLLFileName);
510512

511513
PythonVersion.SysVersion := SysVersion;
512514
PythonVersion.fSysArchitecture := PythonVersion.ExpectedArchitecture;

0 commit comments

Comments
 (0)