@@ -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;
26932694procedure FreeSubtypeInst (ob:PPyObject); cdecl;
26942695procedure Register ;
26952696function 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;
27162718function CleanString (const s : AnsiString; AppendLF : Boolean = True) : AnsiString; overload;
27172719function CleanString (const s : UnicodeString; AppendLF : Boolean = True) : UnicodeString; overload;
27182720
2719- procedure DetectPythonVersionFromLibName (const LibName: string; out MajorVersion, MinorVersion: integer);
2720-
27212721implementation
27222722
27232723uses
@@ -3121,7 +3121,7 @@ constructor TPythonInterface.Create(AOwner: TComponent);
31213121procedure TPythonInterface.AfterLoad ;
31223122begin
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
36423642function TPythonInterface.PyClass_Check ( obj : PPyObject ) : Boolean;
36433643begin
3644- Result := Assigned( obj ) and (PyObject_IsInstance(obj, PPyObject(PyType_Type)) <> 0 );
3644+ Result := Assigned( obj ) and (PyObject_IsInstance(obj, PPyObject(PyType_Type)) = 1 );
36453645end ;
36463646
36473647function 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 ;
40764076end ;
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);
48564857end ;
48574858
@@ -5567,6 +5568,20 @@ procedure TPythonEngine.PyTupleToStrings( tuple: PPyObject; strings : TStrings )
55675568 strings.Add( PyObjectAsString( PyTuple_GetItem( tuple, i ) ) );
55685569end ;
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+
55705585function TPythonEngine.PyUnicodeAsString ( obj : PPyObject ) : UnicodeString;
55715586var
55725587 _size : Integer;
@@ -8832,9 +8847,12 @@ procedure Register;
88328847 TPythonType, TPythonModule, TPythonDelphiVar]);
88338848end ;
88348849
8835- function GetPythonVersionFromDLLName (const DLLFileName : string): string;
8850+ function SysVersionFromDLLName (const DLLFileName : string): string;
8851+ var
8852+ Minor, Major: integer;
88368853begin
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]);
88388856end ;
88398857
88408858function PyType_HasFeature (AType : PPyTypeObject; AFlag : Integer) : Boolean;
@@ -8960,7 +8978,7 @@ function IsPythonVersionRegistered(PythonVersion : string;
89608978end ;
89618979{ $ENDIF}
89628980
8963- procedure DetectPythonVersionFromLibName (const LibName: string; out MajorVersion, MinorVersion: integer);
8981+ procedure PythonVersionFromDLLName (const LibName: string; out MajorVersion, MinorVersion: integer);
89648982var
89658983 NPos: integer;
89668984 S: String;
0 commit comments