@@ -1876,6 +1876,7 @@ TPythonEngine = class(TPythonInterface)
1876
1876
function PyUnicodeFromString (const AString : UnicodeString) : PPyObject; overload;
1877
1877
function PyUnicodeFromString (const AString: AnsiString): PPyObject; overload;
1878
1878
function PyUnicodeAsString ( obj : PPyObject ) : UnicodeString;
1879
+ function PyBytesAsAnsiString ( obj : PPyObject ) : AnsiString;
1879
1880
1880
1881
// Public Properties
1881
1882
property ClientCount : Integer read GetClientCount;
@@ -2693,7 +2694,8 @@ procedure PyObjectDestructor( pSelf : PPyObject); cdecl;
2693
2694
procedure FreeSubtypeInst (ob:PPyObject); cdecl;
2694
2695
procedure Register ;
2695
2696
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);
2697
2699
2698
2700
{ Helper functions}
2699
2701
(*
@@ -2716,8 +2718,6 @@ procedure MaskFPUExceptions(ExceptionsMasked : boolean;
2716
2718
function CleanString (const s : AnsiString; AppendLF : Boolean = True) : AnsiString; overload;
2717
2719
function CleanString (const s : UnicodeString; AppendLF : Boolean = True) : UnicodeString; overload;
2718
2720
2719
- procedure DetectPythonVersionFromLibName (const LibName: string; out MajorVersion, MinorVersion: integer);
2720
-
2721
2721
implementation
2722
2722
2723
2723
uses
@@ -3121,7 +3121,7 @@ constructor TPythonInterface.Create(AOwner: TComponent);
3121
3121
procedure TPythonInterface.AfterLoad ;
3122
3122
begin
3123
3123
inherited ;
3124
- DetectPythonVersionFromLibName (DLLName, FMajorVersion, FMinorVersion);
3124
+ PythonVersionFromDLLName (DLLName, FMajorVersion, FMinorVersion);
3125
3125
3126
3126
FBuiltInModuleName := ' builtins' ;
3127
3127
@@ -3641,7 +3641,7 @@ function TPythonInterface.PyTuple_CheckExact(obj: PPyObject): Boolean;
3641
3641
3642
3642
function TPythonInterface.PyClass_Check ( obj : PPyObject ) : Boolean;
3643
3643
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 );
3645
3645
end ;
3646
3646
3647
3647
function TPythonInterface.PyType_CheckExact ( obj : PPyObject ) : Boolean;
@@ -4071,7 +4071,7 @@ procedure TPythonEngine.DoOpenDll(const aDllName : string);
4071
4071
end ;
4072
4072
end
4073
4073
else
4074
- RegVersion := GetPythonVersionFromDLLName (aDllName);
4074
+ RegVersion := SysVersionFromDLLName (aDllName);
4075
4075
inherited ;
4076
4076
end ;
4077
4077
@@ -4851,7 +4851,8 @@ function TPythonEngine.PyObjectAsString( obj : PPyObject ) : string;
4851
4851
end ;
4852
4852
S := PyObject_Str( obj );
4853
4853
if Assigned(S) and PyUnicode_Check(S) then
4854
- Result := PyUnicodeAsString(S);
4854
+ W := PyUnicodeAsString(S);
4855
+ Result := string(W);
4855
4856
Py_XDECREF(S);
4856
4857
end ;
4857
4858
@@ -5567,6 +5568,20 @@ procedure TPythonEngine.PyTupleToStrings( tuple: PPyObject; strings : TStrings )
5567
5568
strings.Add( PyObjectAsString( PyTuple_GetItem( tuple, i ) ) );
5568
5569
end ;
5569
5570
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
+
5570
5585
function TPythonEngine.PyUnicodeAsString ( obj : PPyObject ) : UnicodeString;
5571
5586
var
5572
5587
_size : Integer;
@@ -8832,9 +8847,12 @@ procedure Register;
8832
8847
TPythonType, TPythonModule, TPythonDelphiVar]);
8833
8848
end ;
8834
8849
8835
- function GetPythonVersionFromDLLName (const DLLFileName : string): string;
8850
+ function SysVersionFromDLLName (const DLLFileName : string): string;
8851
+ var
8852
+ Minor, Major: integer;
8836
8853
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]);
8838
8856
end ;
8839
8857
8840
8858
function PyType_HasFeature (AType : PPyTypeObject; AFlag : Integer) : Boolean;
@@ -8960,7 +8978,7 @@ function IsPythonVersionRegistered(PythonVersion : string;
8960
8978
end ;
8961
8979
{ $ENDIF}
8962
8980
8963
- procedure DetectPythonVersionFromLibName (const LibName: string; out MajorVersion, MinorVersion: integer);
8981
+ procedure PythonVersionFromDLLName (const LibName: string; out MajorVersion, MinorVersion: integer);
8964
8982
var
8965
8983
NPos: integer;
8966
8984
S: String;
0 commit comments