Skip to content

Commit 5664a7f

Browse files
committed
Added PyUnicodeAsUTF8String
1 parent a0661e8 commit 5664a7f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Source/PythonEngine.pas

+21
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 PyUnicodeAsUTF8String( obj : PPyObject ) : RawByteString;
18791880
function PyBytesAsAnsiString( obj : PPyObject ) : AnsiString;
18801881

18811882
// Public Properties
@@ -5616,6 +5617,26 @@ function TPythonEngine.PyUnicodeAsString( obj : PPyObject ) : UnicodeString;
56165617
raise EPythonError.Create('PyUnicodeAsString expects a Unicode Python object');
56175618
end;
56185619

5620+
function TPythonEngine.PyUnicodeAsUTF8String( obj : PPyObject ) : RawByteString;
5621+
var
5622+
buffer: PAnsiChar;
5623+
size: NativeInt;
5624+
begin
5625+
if PyUnicode_Check(obj) then
5626+
begin
5627+
Result := '';
5628+
buffer := PyUnicode_AsUTF8AndSize(obj, @size);
5629+
if Assigned(buffer) then
5630+
SetString(Result, buffer, size)
5631+
else
5632+
Result := '';
5633+
SetCodePage(Result, CP_UTF8, False);
5634+
end
5635+
else
5636+
raise EPythonError.Create('PyUnicodeAsUTF8String expects a Unicode Python object');
5637+
end;
5638+
5639+
56195640
function TPythonEngine.PyUnicodeFromString(const AString : UnicodeString) : PPyObject;
56205641
{$IFDEF POSIX}
56215642
var

0 commit comments

Comments
 (0)