Jump to content
Sign in to follow this  
DennisTW

How to use PyArg_ParseTuple to define a function of String

Recommended Posts

from the demo, (PyArg_ParseTuple( args, 'Lii',@psort, @i, @j) <> 0) defines a procedure of (handle, i, j : integer).

    function TThreadSortForm.Module_Swap( pself, args : PPyObject ) : PPyObject; cdecl;
    var
      psort: NativeInt;
      i, j: Integer;
    begin
      with GetPythonEngine do
      begin
        if (PyErr_Occurred() = nil) and
    {$IFDEF CPU64BITS}
          (PyArg_ParseTuple( args, 'Lii',@psort, @i, @j) <> 0)
    {$ELSE}
          (PyArg_ParseTuple( args, 'iii',@psort, @i, @j) <> 0)
    {$ENDIF}
        then
        begin
          TSortThread(psort).VisualSwap(i,j);
          Result := ReturnNone;
        end else
          Result := nil;
      end;
    end;
    
How do I define a function (TheKey, TheValue : String) : String;
I cannot find the definitions of the second parameter of PyArg_ParseTuple .

Share this post


Link to post

This is a code fragment that shows you how to convert python strings to Delphi strings with PyArg_ParseTuple


 

function ShowMessage_Wrapper(pself, args: PPyObject): PPyObject; cdecl;
var
  LMsg: PAnsiChar;
begin
  with GetPythonEngine do
  begin
    if PyArg_ParseTuple(args, 's:ShowMessage', @LMsg) <> 0 then
    begin
      ShowMessage(Utf8ToString(LMsg));
      Result := GetPythonEngine.ReturnNone;
    end else
      Result := nil;
  end;
end;

 

Also have a look at the PyArg_ParseTuple documentation.

 

 

Using such methods is the low level approach to exposing Delphi code to python.    Please have a look at WrapDelphi demos and tutorials for a high-level approach that that does not require you to use PyArg_ParseTuple or worry about reference counting and the like.  

Edited by pyscripter
  • Like 2

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×