2011-12-20 09:32:09 +00:00
|
|
|
------------------------------------------------------------------------------
|
|
|
|
|
-- G N A T C O L L --
|
|
|
|
|
-- --
|
2019-09-04 20:20:19 +02:00
|
|
|
-- Copyright (C) 2003-2020, AdaCore --
|
2011-12-20 09:32:09 +00:00
|
|
|
-- --
|
|
|
|
|
-- This library is free software; you can redistribute it and/or modify it --
|
|
|
|
|
-- under terms of the GNU General Public License as published by the Free --
|
|
|
|
|
-- Software Foundation; either version 3, or (at your option) any later --
|
|
|
|
|
-- version. This library is distributed in the hope that it will be useful, --
|
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- --
|
|
|
|
|
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE. --
|
|
|
|
|
-- --
|
|
|
|
|
-- As a special exception under Section 7 of GPL version 3, you are granted --
|
|
|
|
|
-- additional permissions described in the GCC Runtime Library Exception, --
|
|
|
|
|
-- version 3.1, as published by the Free Software Foundation. --
|
|
|
|
|
-- --
|
|
|
|
|
-- You should have received a copy of the GNU General Public License and --
|
|
|
|
|
-- a copy of the GCC Runtime Library Exception along with this program; --
|
|
|
|
|
-- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
|
|
|
|
|
-- <http://www.gnu.org/licenses/>. --
|
|
|
|
|
-- --
|
|
|
|
|
------------------------------------------------------------------------------
|
2007-06-11 07:57:59 +00:00
|
|
|
|
|
|
|
|
with System; use System;
|
|
|
|
|
with Interfaces.C.Strings; use Interfaces.C.Strings;
|
2008-08-18 12:53:35 +00:00
|
|
|
with GNAT.OS_Lib; use GNAT.OS_Lib;
|
2007-06-11 07:57:59 +00:00
|
|
|
|
2008-04-17 12:12:00 +00:00
|
|
|
package body GNATCOLL.Python is
|
2007-06-11 07:57:59 +00:00
|
|
|
|
2012-06-20 20:42:11 +00:00
|
|
|
No_Method_Def : constant PyMethodDef :=
|
|
|
|
|
(Name => Null_Ptr,
|
|
|
|
|
Func => null,
|
|
|
|
|
Flags => METH_VARGS or METH_KEYWORDS,
|
|
|
|
|
Doc => Null_Ptr);
|
2007-06-11 07:57:59 +00:00
|
|
|
|
|
|
|
|
type Methods_Access is access PyMethodDef_Array;
|
|
|
|
|
type MethodDef_Access is access PyMethodDef;
|
|
|
|
|
pragma Convention (C, MethodDef_Access);
|
|
|
|
|
|
|
|
|
|
function PyCFunction_New
|
|
|
|
|
(MethodDef : MethodDef_Access;
|
|
|
|
|
Self : PyObject;
|
|
|
|
|
Module : PyObject := null) return PyObject;
|
2012-06-21 14:40:19 +00:00
|
|
|
pragma Import (C, PyCFunction_New, "PyCFunction_NewEx");
|
2007-06-11 07:57:59 +00:00
|
|
|
-- Create a new callable object, which, when called from python, will call
|
|
|
|
|
-- the Ada subprogram.
|
|
|
|
|
-- Self is the first argument that will be passed to the Ada subprogram.
|
2012-06-21 14:40:19 +00:00
|
|
|
-- Module is the value of the __module__ attribute for the new function.
|
2007-06-11 07:57:59 +00:00
|
|
|
|
|
|
|
|
------------------------
|
|
|
|
|
-- PyRun_SimpleString --
|
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
|
|
function PyRun_SimpleString (Cmd : String) return Boolean is
|
|
|
|
|
function Internal (Cmd : String) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "PyRun_SimpleString");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Cmd & ASCII.NUL) = 0;
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyRun_SimpleString;
|
|
|
|
|
|
|
|
|
|
------------------------
|
|
|
|
|
-- PyImport_AddModule --
|
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
|
|
function PyImport_AddModule (Module_Name : String) return PyObject is
|
|
|
|
|
function Internal (Name : String) return PyObject;
|
|
|
|
|
pragma Import (C, Internal, "PyImport_AddModule");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Module_Name & ASCII.NUL);
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyImport_AddModule;
|
|
|
|
|
|
|
|
|
|
---------------------------
|
|
|
|
|
-- PyImport_ImportModule --
|
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
|
|
function PyImport_ImportModule (Module_Name : String) return PyObject is
|
|
|
|
|
function Internal (Name : String) return PyObject;
|
2012-08-21 13:26:46 +00:00
|
|
|
pragma Import (C, Internal, "PyImport_ImportModule");
|
2007-06-11 07:57:59 +00:00
|
|
|
begin
|
2015-03-02 08:52:19 -05:00
|
|
|
return Internal (Module_Name & ASCII.NUL);
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyImport_ImportModule;
|
|
|
|
|
|
|
|
|
|
------------------
|
|
|
|
|
-- PyRun_String --
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
function PyRun_String
|
|
|
|
|
(Str : String;
|
|
|
|
|
Start : Interpreter_State;
|
|
|
|
|
Globals : PyObject;
|
|
|
|
|
Locals : PyObject) return PyObject
|
|
|
|
|
is
|
|
|
|
|
function Internal
|
|
|
|
|
(Str : String;
|
|
|
|
|
Start : Interpreter_State;
|
|
|
|
|
Globals : PyObject;
|
|
|
|
|
Locals : PyObject) return PyObject;
|
|
|
|
|
pragma Import (C, Internal, "PyRun_String");
|
|
|
|
|
begin
|
2015-03-02 08:52:19 -05:00
|
|
|
return Internal (Str & ASCII.LF, Start, Globals, Locals);
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyRun_String;
|
|
|
|
|
|
|
|
|
|
----------------------
|
|
|
|
|
-- PyArg_ParseTuple --
|
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
|
|
function PyArg_ParseTuple
|
|
|
|
|
(Arg : PyObject;
|
|
|
|
|
Format : String;
|
|
|
|
|
Value1 : System.Address) return Boolean
|
|
|
|
|
is
|
|
|
|
|
function Internal
|
|
|
|
|
(Arg : PyObject; Format : String; V1 : System.Address) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_py_arg_parsetuple_ptr");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Arg, Format & ASCII.NUL, Value1) = 1;
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyArg_ParseTuple;
|
|
|
|
|
|
|
|
|
|
function PyArg_ParseTuple
|
|
|
|
|
(Arg : PyObject;
|
|
|
|
|
Format : String;
|
|
|
|
|
Value1, Value2 : System.Address) return Boolean
|
|
|
|
|
is
|
|
|
|
|
function Internal
|
|
|
|
|
(Arg : PyObject; Format : String; V1, V2 : System.Address)
|
|
|
|
|
return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_py_arg_parsetuple_ptr2");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Arg, Format & ASCII.NUL, Value1, Value2) = 1;
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyArg_ParseTuple;
|
|
|
|
|
|
|
|
|
|
function PyArg_ParseTuple
|
|
|
|
|
(Arg : PyObject;
|
|
|
|
|
Format : String;
|
|
|
|
|
Value1, Value2, Value3 : System.Address) return Boolean
|
|
|
|
|
is
|
|
|
|
|
function Internal
|
|
|
|
|
(Arg : PyObject; Format : String; V1, V2, V3 : System.Address)
|
|
|
|
|
return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_py_arg_parsetuple_ptr3");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Arg, Format & ASCII.NUL, Value1, Value2, Value3) = 1;
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyArg_ParseTuple;
|
|
|
|
|
|
|
|
|
|
function PyArg_ParseTuple
|
|
|
|
|
(Arg : PyObject;
|
|
|
|
|
Format : String;
|
|
|
|
|
Value1, Value2, Value3, Value4 : System.Address) return Boolean
|
|
|
|
|
is
|
|
|
|
|
function Internal
|
|
|
|
|
(Arg : PyObject; Format : String; V1, V2, V3, V4 : System.Address)
|
|
|
|
|
return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_py_arg_parsetuple_ptr4");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal
|
2007-06-11 07:57:59 +00:00
|
|
|
(Arg, Format & ASCII.NUL, Value1, Value2, Value3, Value4) = 1;
|
|
|
|
|
end PyArg_ParseTuple;
|
|
|
|
|
|
|
|
|
|
function PyArg_ParseTuple
|
|
|
|
|
(Arg : PyObject;
|
|
|
|
|
Format : String;
|
|
|
|
|
Value1, Value2, Value3, Value4, Value5 : System.Address) return Boolean
|
|
|
|
|
is
|
|
|
|
|
function Internal
|
|
|
|
|
(Arg : PyObject; Format : String; V1, V2, V3, V4, V5 : System.Address)
|
|
|
|
|
return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_py_arg_parsetuple_ptr5");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal
|
2007-06-11 07:57:59 +00:00
|
|
|
(Arg, Format & ASCII.NUL, Value1, Value2, Value3, Value4, Value5) = 1;
|
|
|
|
|
end PyArg_ParseTuple;
|
|
|
|
|
|
|
|
|
|
----------------------
|
|
|
|
|
-- PyFunction_Check --
|
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
|
|
function PyFunction_Check (Func : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pyfunction_check");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Func) = 1;
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyFunction_Check;
|
|
|
|
|
|
2011-11-16 11:55:29 +00:00
|
|
|
----------------------
|
|
|
|
|
-- PyCallable_Check --
|
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
|
|
function PyCallable_Check (Func : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "PyCallable_Check");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Func) = 1;
|
2011-11-16 11:55:29 +00:00
|
|
|
end PyCallable_Check;
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
--------------------
|
|
|
|
|
-- PyString_Check --
|
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
|
|
function PyString_Check (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pystring_check");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Obj) = 1;
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyString_Check;
|
|
|
|
|
|
2011-06-20 11:12:55 +00:00
|
|
|
---------------------
|
|
|
|
|
-- PyUnicode_Check --
|
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
|
function PyUnicode_Check (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pyunicode_check");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Obj) = 1;
|
2011-06-20 11:12:55 +00:00
|
|
|
end PyUnicode_Check;
|
|
|
|
|
|
|
|
|
|
------------------------
|
|
|
|
|
-- PyBaseString_Check --
|
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
|
|
function PyBaseString_Check (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pybasestring_check");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Obj) = 1;
|
2011-06-20 11:12:55 +00:00
|
|
|
end PyBaseString_Check;
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
------------------
|
|
|
|
|
-- PyList_Check --
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
function PyList_Check (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pylist_check");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Obj) = 1;
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyList_Check;
|
|
|
|
|
|
2010-11-15 17:33:14 +00:00
|
|
|
------------------
|
|
|
|
|
-- PyIter_Check --
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
function PyIter_Check (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pyiter_check");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Obj) = 1;
|
2010-11-15 17:33:14 +00:00
|
|
|
end PyIter_Check;
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
-----------------
|
|
|
|
|
-- PyInt_Check --
|
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
|
|
function PyInt_Check (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pyint_check");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Obj) = 1;
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyInt_Check;
|
|
|
|
|
|
2008-11-24 11:52:42 +00:00
|
|
|
-------------------
|
|
|
|
|
-- PyFloat_Check --
|
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
|
|
function PyFloat_Check (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pyfloat_check");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Obj) = 1;
|
2008-11-24 11:52:42 +00:00
|
|
|
end PyFloat_Check;
|
|
|
|
|
|
2010-11-17 16:29:07 +00:00
|
|
|
------------------------
|
|
|
|
|
-- PyBool_FromBoolean --
|
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
|
|
function PyBool_FromBoolean (Value : Boolean) return PyObject is
|
|
|
|
|
function PyTrue return PyObject;
|
|
|
|
|
pragma Import (C, PyTrue, "ada_py_true");
|
|
|
|
|
|
|
|
|
|
function PyFalse return PyObject;
|
|
|
|
|
pragma Import (C, PyFalse, "ada_py_false");
|
|
|
|
|
|
|
|
|
|
Result : PyObject;
|
|
|
|
|
begin
|
|
|
|
|
if Value then
|
|
|
|
|
Result := PyTrue;
|
|
|
|
|
else
|
|
|
|
|
Result := PyFalse;
|
|
|
|
|
end if;
|
|
|
|
|
|
|
|
|
|
Py_INCREF (Result);
|
|
|
|
|
return Result;
|
|
|
|
|
end PyBool_FromBoolean;
|
|
|
|
|
|
2009-11-18 08:34:26 +00:00
|
|
|
------------------
|
|
|
|
|
-- PyBool_Check --
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
function PyBool_Check (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pybool_check");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Obj) = 1;
|
2009-11-18 08:34:26 +00:00
|
|
|
end PyBool_Check;
|
|
|
|
|
|
|
|
|
|
--------------------
|
|
|
|
|
-- PyBool_Is_True --
|
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
|
|
function PyBool_Is_True (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pybool_is_true");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Obj) = 1;
|
2009-11-18 08:34:26 +00:00
|
|
|
end PyBool_Is_True;
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
-------------------
|
|
|
|
|
-- PyTuple_Check --
|
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
|
|
function PyTuple_Check (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pytuple_check");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Obj) = 1;
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyTuple_Check;
|
|
|
|
|
|
2010-11-15 17:33:14 +00:00
|
|
|
----------------------
|
|
|
|
|
-- PyObject_GetItem --
|
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
|
|
function PyObject_GetItem (Obj : PyObject; Key : Integer) return PyObject is
|
|
|
|
|
K : PyObject;
|
|
|
|
|
Result : PyObject;
|
|
|
|
|
begin
|
|
|
|
|
K := PyInt_FromLong (Interfaces.C.long (Key));
|
|
|
|
|
Result := PyObject_GetItem (Obj, K);
|
|
|
|
|
Py_DECREF (K);
|
|
|
|
|
return Result;
|
|
|
|
|
end PyObject_GetItem;
|
|
|
|
|
|
|
|
|
|
----------------------
|
|
|
|
|
-- PyObject_SetItem --
|
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
|
|
procedure PyObject_SetItem
|
|
|
|
|
(Obj : PyObject; Key : Integer; Value : PyObject)
|
|
|
|
|
is
|
|
|
|
|
K : PyObject;
|
|
|
|
|
Result : Integer;
|
|
|
|
|
pragma Unreferenced (Result);
|
|
|
|
|
begin
|
|
|
|
|
K := PyInt_FromLong (Interfaces.C.long (Key));
|
|
|
|
|
Result := PyObject_SetItem (Obj, K, Value);
|
|
|
|
|
Py_DECREF (K);
|
|
|
|
|
end PyObject_SetItem;
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
-----------------------
|
|
|
|
|
-- PyString_AsString --
|
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
|
|
function PyString_AsString (Str : PyObject) return String is
|
2012-06-21 14:40:19 +00:00
|
|
|
function Low (Str : PyObject) return Interfaces.C.Strings.chars_ptr;
|
|
|
|
|
pragma Import (C, Low, "ada_PyString_AsString");
|
|
|
|
|
-- Returns a NULL terminated representation of the contents of string.
|
|
|
|
|
-- Result value must be freed.
|
|
|
|
|
|
2015-05-13 17:57:38 +02:00
|
|
|
C : constant Interfaces.C.Strings.chars_ptr := Low (Str);
|
2007-06-11 07:57:59 +00:00
|
|
|
begin
|
2012-06-21 14:40:19 +00:00
|
|
|
if C = Null_Ptr then
|
|
|
|
|
return "";
|
|
|
|
|
else
|
|
|
|
|
declare
|
|
|
|
|
R : constant String := Value (C);
|
2015-05-12 14:41:24 +02:00
|
|
|
|
|
|
|
|
procedure C_Free (S : chars_ptr);
|
|
|
|
|
pragma Import (C, C_Free, "free");
|
|
|
|
|
|
2012-06-21 14:40:19 +00:00
|
|
|
begin
|
2015-05-12 14:41:24 +02:00
|
|
|
-- Since C was allocated by ada_PyString_AsString via strdup(),
|
|
|
|
|
-- and not via System.Memory, we should not be using
|
|
|
|
|
-- Interfaces.C.Strings.Free which goes through System.Memory.
|
|
|
|
|
-- So we call free() directly instead.
|
|
|
|
|
C_Free (C);
|
2012-06-21 14:40:19 +00:00
|
|
|
return R;
|
|
|
|
|
end;
|
|
|
|
|
end if;
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyString_AsString;
|
|
|
|
|
|
|
|
|
|
-------------------------
|
|
|
|
|
-- PyString_FromString --
|
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
|
|
function PyString_FromString (Str : String) return PyObject is
|
2012-02-28 15:32:47 +00:00
|
|
|
function Internal (Str : String; Size : Integer) return PyObject;
|
|
|
|
|
pragma Import (C, Internal, "PyString_FromStringAndSize");
|
2007-06-11 07:57:59 +00:00
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Str, Str'Length);
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyString_FromString;
|
|
|
|
|
|
2011-06-20 11:12:55 +00:00
|
|
|
--------------------------
|
|
|
|
|
-- PyUnicode_FromString --
|
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
|
|
function PyUnicode_FromString (Str : String) return PyObject is
|
|
|
|
|
function Internal (Str : String) return PyObject;
|
2011-06-20 11:50:30 +00:00
|
|
|
pragma Import (C, Internal, "ada_PyUnicode_FromString");
|
2011-06-20 11:12:55 +00:00
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Str & ASCII.NUL);
|
2011-06-20 11:12:55 +00:00
|
|
|
end PyUnicode_FromString;
|
|
|
|
|
|
|
|
|
|
-------------------------------
|
|
|
|
|
-- PyUnicode_AsEncodedString --
|
|
|
|
|
-------------------------------
|
|
|
|
|
|
|
|
|
|
function PyUnicode_AsEncodedString
|
|
|
|
|
(Unicode : PyObject;
|
|
|
|
|
Encoding : String;
|
|
|
|
|
Errors : Unicode_Error_Handling := Strict)
|
|
|
|
|
return PyObject
|
|
|
|
|
is
|
|
|
|
|
function Internal
|
|
|
|
|
(Unicode : PyObject; Encoding, Errors : String) return PyObject;
|
2011-06-20 11:50:30 +00:00
|
|
|
pragma Import (C, Internal, "ada_PyUnicode_AsEncodedString");
|
2011-06-20 11:12:55 +00:00
|
|
|
begin
|
|
|
|
|
case Errors is
|
|
|
|
|
when Strict =>
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal
|
2011-06-20 11:12:55 +00:00
|
|
|
(Unicode, Encoding & ASCII.NUL, "strict" & ASCII.NUL);
|
|
|
|
|
when Ignore =>
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal
|
2011-06-20 11:12:55 +00:00
|
|
|
(Unicode, Encoding & ASCII.NUL, "ignore" & ASCII.NUL);
|
|
|
|
|
when Replace =>
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal
|
2011-06-20 11:12:55 +00:00
|
|
|
(Unicode, Encoding & ASCII.NUL, "replace" & ASCII.NUL);
|
|
|
|
|
end case;
|
|
|
|
|
end PyUnicode_AsEncodedString;
|
|
|
|
|
|
|
|
|
|
----------------------
|
|
|
|
|
-- Unicode_AsString --
|
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
|
|
function Unicode_AsString
|
|
|
|
|
(Str : PyObject; Encoding : String := "utf-8") return String
|
|
|
|
|
is
|
|
|
|
|
S : constant PyObject := PyUnicode_AsEncodedString
|
|
|
|
|
(Unicode => Str, Encoding => Encoding, Errors => Replace);
|
|
|
|
|
Result : constant String := PyString_AsString (S);
|
|
|
|
|
begin
|
|
|
|
|
Py_DECREF (S);
|
|
|
|
|
return Result;
|
|
|
|
|
end Unicode_AsString;
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
---------------------
|
|
|
|
|
-- PySys_SetObject --
|
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
|
procedure PySys_SetObject (Name : String; Object : PyObject) is
|
|
|
|
|
procedure Internal (Name : String; Object : PyObject);
|
|
|
|
|
pragma Import (C, Internal, "PySys_SetObject");
|
|
|
|
|
begin
|
|
|
|
|
Internal (Name & ASCII.NUL, Object);
|
|
|
|
|
end PySys_SetObject;
|
|
|
|
|
|
|
|
|
|
---------------------
|
|
|
|
|
-- PySys_GetObject --
|
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
|
function PySys_GetObject (Name : String) return PyObject is
|
|
|
|
|
function Internal (Name : String) return PyObject;
|
|
|
|
|
pragma Import (C, Internal, "PySys_GetObject");
|
|
|
|
|
begin
|
2015-03-02 08:52:19 -05:00
|
|
|
return Internal (Name & ASCII.NUL);
|
2007-06-11 07:57:59 +00:00
|
|
|
end PySys_GetObject;
|
|
|
|
|
|
|
|
|
|
-------------------------
|
|
|
|
|
-- PyObject_CallMethod --
|
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
|
|
function PyObject_CallMethod
|
|
|
|
|
(Object : PyObject; Name : String) return PyObject
|
|
|
|
|
is
|
|
|
|
|
function Internal (Object : PyObject; Name : String) return PyObject;
|
|
|
|
|
pragma Import (C, Internal, "ada_py_object_callmethod");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Object, Name & ASCII.NUL);
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyObject_CallMethod;
|
|
|
|
|
|
|
|
|
|
function PyObject_CallMethod
|
|
|
|
|
(Object : PyObject; Name : String; Arg1 : PyObject) return PyObject
|
|
|
|
|
is
|
|
|
|
|
function Internal
|
|
|
|
|
(Object : PyObject; Name : String; Arg : PyObject) return PyObject;
|
|
|
|
|
pragma Import (C, Internal, "ada_py_object_callmethod_obj");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Object, Name & ASCII.NUL, Arg1);
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyObject_CallMethod;
|
|
|
|
|
|
|
|
|
|
function PyObject_CallMethod
|
|
|
|
|
(Object : PyObject; Name : String; Arg1 : Integer) return PyObject
|
|
|
|
|
is
|
|
|
|
|
function Internal
|
|
|
|
|
(Object : PyObject; Name : String; Arg : Integer) return PyObject;
|
|
|
|
|
pragma Import (C, Internal, "ada_py_object_callmethod_int");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Object, Name & ASCII.NUL, Arg1);
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyObject_CallMethod;
|
|
|
|
|
|
|
|
|
|
-----------------------
|
|
|
|
|
-- Py_SetProgramName --
|
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
|
|
procedure Py_SetProgramName (Name : String) is
|
|
|
|
|
procedure Internal (Name : String);
|
|
|
|
|
pragma Import (C, Internal, "Py_SetProgramName");
|
2008-08-18 12:53:35 +00:00
|
|
|
|
2009-03-20 09:05:21 +00:00
|
|
|
Program_Name : constant String_Access := new String'(Name & ASCII.NUL);
|
2008-08-18 12:53:35 +00:00
|
|
|
-- As stated by the Python documentation the string passed to
|
|
|
|
|
-- Py_SetProgramName should be in "static storage whose contents will
|
|
|
|
|
-- not change for the duration of the program's execution"
|
2007-06-11 07:57:59 +00:00
|
|
|
begin
|
2008-08-18 12:53:35 +00:00
|
|
|
Internal (Program_Name.all);
|
2007-06-11 07:57:59 +00:00
|
|
|
end Py_SetProgramName;
|
|
|
|
|
|
2010-10-20 14:58:09 +00:00
|
|
|
----------------------
|
|
|
|
|
-- Py_SetPythonHome --
|
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
|
|
procedure Py_SetPythonHome (Home : String) is
|
|
|
|
|
procedure Internal (Name : String);
|
|
|
|
|
pragma Import (C, Internal, "Py_SetPythonHome");
|
|
|
|
|
|
|
|
|
|
C_Home : constant String_Access := new String'(Home & ASCII.NUL);
|
|
|
|
|
-- As stated by the Python documentation the string passed to
|
|
|
|
|
-- Py_SetPythonHome should be in "static storage whose contents will
|
|
|
|
|
-- not change for the duration of the program's execution"
|
|
|
|
|
begin
|
|
|
|
|
Internal (C_Home.all);
|
|
|
|
|
end Py_SetPythonHome;
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
----------------------
|
|
|
|
|
-- Py_CompileString --
|
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
|
|
function Py_CompileString
|
|
|
|
|
(Cmd : String; Name : String; State : Interpreter_State)
|
|
|
|
|
return PyCodeObject
|
|
|
|
|
is
|
|
|
|
|
function Internal (Cmd, Name : String; State : Interpreter_State)
|
|
|
|
|
return PyCodeObject;
|
|
|
|
|
pragma Import (C, Internal, "Py_CompileString");
|
|
|
|
|
begin
|
2015-03-02 08:52:19 -05:00
|
|
|
return Internal (Cmd & ASCII.NUL, Name & ASCII.NUL, State);
|
2007-06-11 07:57:59 +00:00
|
|
|
end Py_CompileString;
|
|
|
|
|
|
2015-03-31 16:02:33 +03:00
|
|
|
------------------
|
|
|
|
|
-- PyDict_Check --
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
function PyDict_Check (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pydict_check");
|
|
|
|
|
begin
|
|
|
|
|
return Internal (Obj) /= 0;
|
|
|
|
|
end PyDict_Check;
|
|
|
|
|
|
|
|
|
|
--------------------
|
|
|
|
|
-- PyAnySet_Check --
|
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
|
|
function PyAnySet_Check (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pyanyset_check");
|
|
|
|
|
begin
|
|
|
|
|
return Internal (Obj) /= 0;
|
|
|
|
|
end PyAnySet_Check;
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
--------------------------
|
|
|
|
|
-- PyDict_SetItemString --
|
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
|
|
procedure PyDict_SetItemString
|
|
|
|
|
(Dict : PyDictObject; Key : String; Obj : PyObject)
|
|
|
|
|
is
|
|
|
|
|
S : chars_ptr := New_String (Key);
|
|
|
|
|
Result : constant Integer := PyDict_SetItemString (Dict, S, Obj);
|
|
|
|
|
pragma Unreferenced (Result);
|
|
|
|
|
begin
|
|
|
|
|
Free (S);
|
|
|
|
|
end PyDict_SetItemString;
|
|
|
|
|
|
|
|
|
|
------------------------
|
|
|
|
|
-- PyModule_AddObject --
|
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
|
|
function PyModule_AddObject
|
|
|
|
|
(Module : PyObject; Name : String; Object : PyObject) return Integer
|
|
|
|
|
is
|
|
|
|
|
S : chars_ptr := New_String (Name);
|
|
|
|
|
Result : Integer;
|
|
|
|
|
begin
|
|
|
|
|
Result := PyModule_AddObject (Module, S, Object);
|
|
|
|
|
Free (S);
|
|
|
|
|
return Result;
|
|
|
|
|
end PyModule_AddObject;
|
|
|
|
|
|
|
|
|
|
--------------------------
|
|
|
|
|
-- PyDict_GetItemString --
|
|
|
|
|
--------------------------
|
|
|
|
|
|
|
|
|
|
function PyDict_GetItemString
|
|
|
|
|
(Dict : PyDictObject; Key : String) return PyObject
|
|
|
|
|
is
|
|
|
|
|
S : chars_ptr := New_String (Key);
|
|
|
|
|
Result : constant PyObject := PyDict_GetItemString (Dict, S);
|
|
|
|
|
begin
|
|
|
|
|
Free (S);
|
|
|
|
|
return Result;
|
|
|
|
|
end PyDict_GetItemString;
|
|
|
|
|
|
|
|
|
|
------------------
|
|
|
|
|
-- Create_Tuple --
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
function Create_Tuple (Objects : PyObject_Array) return PyObject is
|
|
|
|
|
Tuple : constant PyObject := PyTuple_New (Objects'Length);
|
|
|
|
|
begin
|
|
|
|
|
for O in Objects'Range loop
|
|
|
|
|
PyTuple_SetItem (Tuple, O - Objects'First, Objects (O));
|
|
|
|
|
end loop;
|
|
|
|
|
return Tuple;
|
|
|
|
|
end Create_Tuple;
|
|
|
|
|
|
|
|
|
|
------------------------
|
|
|
|
|
-- PyErr_NewException --
|
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
|
|
function PyErr_NewException
|
|
|
|
|
(Name : String; Base : PyObject := null; Dict : PyObject := null)
|
|
|
|
|
return PyObject
|
|
|
|
|
is
|
|
|
|
|
function Internal (Name : String; Base, Dict : PyObject) return PyObject;
|
|
|
|
|
pragma Import (C, Internal, "PyErr_NewException");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Name & ASCII.NUL, Base, Dict);
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyErr_NewException;
|
|
|
|
|
|
|
|
|
|
---------------------
|
|
|
|
|
-- PyErr_SetString --
|
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
|
procedure PyErr_SetString (Except : PyObject; Msg : String) is
|
|
|
|
|
procedure Internal (Except : PyObject; Msg : String);
|
|
|
|
|
pragma Import (C, Internal, "PyErr_SetString");
|
|
|
|
|
begin
|
|
|
|
|
Internal (Except, Msg & ASCII.NUL);
|
|
|
|
|
end PyErr_SetString;
|
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
-- PyObject_GetAttrString --
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
function PyObject_GetAttrString
|
|
|
|
|
(Object : PyObject; Name : String) return PyObject
|
|
|
|
|
is
|
|
|
|
|
S : chars_ptr := New_String (Name);
|
|
|
|
|
Result : constant PyObject := PyObject_GetAttrString (Object, S);
|
|
|
|
|
begin
|
|
|
|
|
Free (S);
|
|
|
|
|
return Result;
|
|
|
|
|
end PyObject_GetAttrString;
|
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
-- PyObject_HasAttrString --
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
function PyObject_HasAttrString
|
|
|
|
|
(Obj : PyObject; Attr_Name : String) return Boolean
|
|
|
|
|
is
|
|
|
|
|
function Internal (Object : PyObject; S : String) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "PyObject_HasAttrString");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Boolean'Val (Internal (Obj, Attr_Name & ASCII.NUL));
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyObject_HasAttrString;
|
|
|
|
|
|
|
|
|
|
----------------------------
|
|
|
|
|
-- PyObject_SetAttrString --
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
procedure PyObject_SetAttrString
|
|
|
|
|
(Obj : PyObject; Attr_Name : String; Value : PyObject)
|
|
|
|
|
is
|
|
|
|
|
procedure Internal (Obj : PyObject; Name : String; Val : PyObject);
|
|
|
|
|
pragma Import (C, Internal, "PyObject_SetAttrString");
|
|
|
|
|
begin
|
|
|
|
|
Internal (Obj, Attr_Name & ASCII.NUL, Value);
|
|
|
|
|
end PyObject_SetAttrString;
|
|
|
|
|
|
2010-11-19 16:03:39 +00:00
|
|
|
----------------------------
|
|
|
|
|
-- PyObject_SetAttrString --
|
|
|
|
|
----------------------------
|
|
|
|
|
|
|
|
|
|
function PyObject_SetAttrString
|
|
|
|
|
(Obj : PyObject; Attr_Name : String; Value : PyObject) return Integer
|
|
|
|
|
is
|
|
|
|
|
function Internal
|
|
|
|
|
(Obj : PyObject; Name : String; Val : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "PyObject_SetAttrString");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Obj, Attr_Name & ASCII.NUL, Value);
|
2010-11-19 16:03:39 +00:00
|
|
|
end PyObject_SetAttrString;
|
|
|
|
|
|
2012-02-15 13:46:14 +00:00
|
|
|
-----------------------------------
|
|
|
|
|
-- PyObject_GenericSetAttrString --
|
|
|
|
|
-----------------------------------
|
|
|
|
|
|
2012-02-15 17:31:18 +00:00
|
|
|
function PyObject_GenericSetAttrString
|
2012-02-15 13:46:14 +00:00
|
|
|
(Object : PyObject;
|
|
|
|
|
Name : String;
|
2012-02-15 17:31:18 +00:00
|
|
|
Attr : PyObject) return Integer
|
2012-02-15 13:46:14 +00:00
|
|
|
is
|
|
|
|
|
N : constant PyObject := PyString_FromString (Name);
|
|
|
|
|
Result : Integer;
|
|
|
|
|
begin
|
|
|
|
|
Result := PyObject_GenericSetAttr (Object, N, Attr);
|
|
|
|
|
Py_DECREF (N);
|
2012-02-15 17:31:18 +00:00
|
|
|
return Result;
|
2012-02-15 13:46:14 +00:00
|
|
|
end PyObject_GenericSetAttrString;
|
|
|
|
|
|
2015-07-29 12:35:54 +03:00
|
|
|
---------------------
|
|
|
|
|
-- PyDict_Contains --
|
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
|
function PyDict_Contains
|
|
|
|
|
(Dict : PyDictObject; Key : PyObject) return Boolean
|
|
|
|
|
is
|
|
|
|
|
function Internal (Dict : PyObject; Key : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "PyDict_Contains");
|
|
|
|
|
|
|
|
|
|
begin
|
|
|
|
|
return Internal (Dict, Key) = 1;
|
|
|
|
|
end PyDict_Contains;
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
-----------------
|
|
|
|
|
-- PyDict_Next --
|
|
|
|
|
-----------------
|
|
|
|
|
|
|
|
|
|
procedure PyDict_Next
|
|
|
|
|
(Dict : PyObject;
|
|
|
|
|
Pos : in out Integer;
|
|
|
|
|
Key : out PyObject;
|
|
|
|
|
Value : out PyObject)
|
|
|
|
|
is
|
|
|
|
|
function Internal
|
|
|
|
|
(Dict : PyObject; Pos, Key, Value : System.Address) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "PyDict_Next");
|
2013-07-01 20:35:58 +00:00
|
|
|
|
|
|
|
|
P : Interfaces.C.size_t := Interfaces.C.size_t (Pos);
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
begin
|
2013-07-01 20:35:58 +00:00
|
|
|
if Internal (Dict, P'Address, Key'Address, Value'Address) = 0 then
|
2007-06-11 07:57:59 +00:00
|
|
|
Pos := -1;
|
2013-07-01 20:35:58 +00:00
|
|
|
else
|
|
|
|
|
Pos := Integer (P);
|
2007-06-11 07:57:59 +00:00
|
|
|
end if;
|
|
|
|
|
end PyDict_Next;
|
|
|
|
|
|
|
|
|
|
--------------------
|
|
|
|
|
-- Print_Refcount --
|
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
|
|
procedure Print_Refcount (Obj : PyObject; Msg : String) is
|
|
|
|
|
procedure Internal (Obj : PyObject; Msg : String);
|
|
|
|
|
pragma Import (C, Internal, "ada_py_print_refcount");
|
|
|
|
|
begin
|
|
|
|
|
Internal (Obj, Msg & ASCII.NUL);
|
|
|
|
|
end Print_Refcount;
|
|
|
|
|
|
|
|
|
|
------------------------
|
|
|
|
|
-- PyFile_WriteString --
|
|
|
|
|
------------------------
|
|
|
|
|
|
|
|
|
|
function PyFile_WriteString
|
|
|
|
|
(Text : String; File : PyObject) return Boolean
|
|
|
|
|
is
|
|
|
|
|
function Internal (Text : String; File : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "PyFile_WriteString");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Text & ASCII.NUL, File) /= 0;
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyFile_WriteString;
|
|
|
|
|
|
2014-03-03 14:48:00 +00:00
|
|
|
-----------------------
|
|
|
|
|
-- PyFile_FromString --
|
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
|
|
function PyFile_FromString (File_Name, Mode : String) return PyObject is
|
|
|
|
|
function Internal (N, M : String) return PyObject;
|
|
|
|
|
pragma Import (C, Internal, "PyFile_FromString");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (File_Name & ASCII.NUL, Mode & ASCII.NUL);
|
2014-03-03 14:48:00 +00:00
|
|
|
end PyFile_FromString;
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
-------------------
|
|
|
|
|
-- Py_InitModule --
|
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
|
|
function Py_InitModule
|
|
|
|
|
(Module_Name : String;
|
|
|
|
|
Methods : PyMethodDef_Array := No_MethodDef_Array;
|
|
|
|
|
Doc : String := "") return PyObject
|
|
|
|
|
is
|
|
|
|
|
function Internal
|
|
|
|
|
(N : String;
|
|
|
|
|
Methods : System.Address;
|
|
|
|
|
Doc : String;
|
2012-06-19 15:51:12 +00:00
|
|
|
Self : PyObject := null) return PyObject;
|
2007-06-11 07:57:59 +00:00
|
|
|
pragma Import (C, Internal, "ada_Py_InitModule4");
|
|
|
|
|
|
|
|
|
|
M : Methods_Access;
|
|
|
|
|
begin
|
|
|
|
|
if Methods /= No_MethodDef_Array then
|
|
|
|
|
-- ??? Memory is never freed, but Python is not supposed to be killed
|
|
|
|
|
-- before the end of the application
|
|
|
|
|
M := new PyMethodDef_Array'(Methods & No_Method_Def);
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal
|
2007-06-11 07:57:59 +00:00
|
|
|
(Module_Name & ASCII.NUL, M.all'Address,
|
|
|
|
|
Doc & ASCII.NUL);
|
|
|
|
|
|
|
|
|
|
else
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal
|
2007-06-11 07:57:59 +00:00
|
|
|
(Module_Name & ASCII.NUL, System.Null_Address,
|
|
|
|
|
Doc & ASCII.NUL);
|
|
|
|
|
end if;
|
|
|
|
|
end Py_InitModule;
|
|
|
|
|
|
|
|
|
|
----------
|
|
|
|
|
-- Free --
|
|
|
|
|
----------
|
|
|
|
|
|
|
|
|
|
procedure Free (Method : in out PyMethodDef) is
|
|
|
|
|
procedure C_Free (C : Interfaces.C.Strings.chars_ptr);
|
|
|
|
|
pragma Import (C, C_Free, "free");
|
|
|
|
|
|
|
|
|
|
begin
|
|
|
|
|
C_Free (Method.Name);
|
|
|
|
|
C_Free (Method.Doc);
|
|
|
|
|
Method.Name := Null_Ptr;
|
|
|
|
|
Method.Doc := Null_Ptr;
|
|
|
|
|
end Free;
|
|
|
|
|
|
|
|
|
|
----------
|
|
|
|
|
-- Free --
|
|
|
|
|
----------
|
|
|
|
|
|
|
|
|
|
procedure Free (Methods : in out PyMethodDef_Array) is
|
|
|
|
|
begin
|
|
|
|
|
for M in Methods'Range loop
|
|
|
|
|
Free (Methods (M));
|
|
|
|
|
end loop;
|
|
|
|
|
end Free;
|
|
|
|
|
|
2014-04-01 12:49:06 +00:00
|
|
|
------------------
|
|
|
|
|
-- PyModule_New --
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
function PyModule_New (Module_Name : String) return PyObject is
|
|
|
|
|
function Internal (N : String) return PyObject;
|
|
|
|
|
pragma Import (C, Internal, "PyModule_New");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Module_Name & ASCII.NUL);
|
2014-04-01 12:49:06 +00:00
|
|
|
end PyModule_New;
|
|
|
|
|
|
2010-11-15 14:56:33 +00:00
|
|
|
----------------------
|
|
|
|
|
-- PyModule_Getname --
|
|
|
|
|
----------------------
|
|
|
|
|
|
|
|
|
|
function PyModule_Getname (Module : PyObject) return String is
|
|
|
|
|
function Internal (M : PyObject) return Interfaces.C.Strings.chars_ptr;
|
|
|
|
|
pragma Import (C, Internal, "PyModule_GetName");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Value (Internal (Module));
|
2010-11-15 14:56:33 +00:00
|
|
|
end PyModule_Getname;
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
------------------
|
|
|
|
|
-- Add_Function --
|
|
|
|
|
------------------
|
|
|
|
|
|
|
|
|
|
procedure Add_Function
|
|
|
|
|
(Module : PyObject; Func : PyMethodDef; Self : PyObject := null)
|
|
|
|
|
is
|
|
|
|
|
C_Func : PyObject;
|
|
|
|
|
Result : Integer;
|
|
|
|
|
pragma Unreferenced (Result);
|
|
|
|
|
begin
|
|
|
|
|
if Self /= null then
|
|
|
|
|
C_Func := PyCFunction_New
|
2010-11-15 14:56:33 +00:00
|
|
|
(new PyMethodDef'(Func), Self,
|
|
|
|
|
PyString_FromString (PyModule_Getname (Module)));
|
2007-06-11 07:57:59 +00:00
|
|
|
else
|
|
|
|
|
C_Func := PyCFunction_New
|
2010-11-15 14:56:33 +00:00
|
|
|
(new PyMethodDef'(Func), Module,
|
|
|
|
|
PyString_FromString (PyModule_Getname (Module)));
|
2007-06-11 07:57:59 +00:00
|
|
|
end if;
|
|
|
|
|
|
|
|
|
|
if C_Func /= null then
|
|
|
|
|
Result := PyModule_AddObject (Module, Func.Name, C_Func);
|
|
|
|
|
end if;
|
|
|
|
|
end Add_Function;
|
|
|
|
|
|
|
|
|
|
----------------
|
|
|
|
|
-- Add_Method --
|
|
|
|
|
----------------
|
|
|
|
|
|
|
|
|
|
procedure Add_Method
|
2010-11-15 14:56:33 +00:00
|
|
|
(Class : PyObject;
|
2007-06-11 07:57:59 +00:00
|
|
|
Func : PyMethodDef;
|
2010-11-15 14:56:33 +00:00
|
|
|
Self : PyObject := null;
|
|
|
|
|
Module : PyObject)
|
2007-06-11 07:57:59 +00:00
|
|
|
is
|
2012-06-21 14:40:19 +00:00
|
|
|
procedure Add_Method
|
2019-03-23 08:37:17 +01:00
|
|
|
(Func : MethodDef_Access;
|
2012-06-21 14:40:19 +00:00
|
|
|
Self : PyObject;
|
|
|
|
|
Class : PyObject;
|
|
|
|
|
Module : PyObject);
|
2012-06-19 15:51:12 +00:00
|
|
|
pragma Import (C, Add_Method, "ada_py_add_method");
|
2007-06-11 07:57:59 +00:00
|
|
|
begin
|
2012-06-21 14:40:19 +00:00
|
|
|
Add_Method (new PyMethodDef'(Func), Self, Class, Module);
|
2007-06-11 07:57:59 +00:00
|
|
|
end Add_Method;
|
|
|
|
|
|
|
|
|
|
-----------------------
|
|
|
|
|
-- Add_Static_Method --
|
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
|
|
procedure Add_Static_Method
|
2010-11-15 14:56:33 +00:00
|
|
|
(Class : PyObject; Func : PyMethodDef; Self : PyObject := null;
|
|
|
|
|
Module : PyObject)
|
2007-06-11 07:57:59 +00:00
|
|
|
is
|
|
|
|
|
function PyStaticMethod_New (Method : PyObject) return PyObject;
|
|
|
|
|
pragma Import (C, PyStaticMethod_New, "PyStaticMethod_New");
|
|
|
|
|
|
|
|
|
|
Def : constant MethodDef_Access := new PyMethodDef'(Func);
|
|
|
|
|
C_Func : PyObject;
|
|
|
|
|
Static : PyObject;
|
|
|
|
|
Result : Integer;
|
|
|
|
|
pragma Unreferenced (Result);
|
|
|
|
|
begin
|
|
|
|
|
Def.Flags := Def.Flags or METH_STATIC;
|
|
|
|
|
|
2010-11-15 14:56:33 +00:00
|
|
|
C_Func := PyCFunction_New
|
|
|
|
|
(Def, Self, PyString_FromString (PyModule_Getname (Module)));
|
2007-06-11 07:57:59 +00:00
|
|
|
if C_Func /= null then
|
2012-06-19 15:51:12 +00:00
|
|
|
-- ??? Likely not needed for python3
|
2007-06-11 07:57:59 +00:00
|
|
|
Static := PyStaticMethod_New (C_Func);
|
|
|
|
|
Result := PyObject_SetAttrString (Class, Func.Name, Static);
|
2011-01-14 15:10:59 +00:00
|
|
|
Py_DECREF (Static);
|
2007-06-11 07:57:59 +00:00
|
|
|
end if;
|
|
|
|
|
end Add_Static_Method;
|
|
|
|
|
|
|
|
|
|
----------------------
|
|
|
|
|
-- Add_Class_Method --
|
|
|
|
|
----------------------
|
|
|
|
|
|
2010-11-15 14:56:33 +00:00
|
|
|
procedure Add_Class_Method
|
|
|
|
|
(Class : PyObject; Func : PyMethodDef; Module : PyObject)
|
|
|
|
|
is
|
2007-06-11 07:57:59 +00:00
|
|
|
function PyClassMethod_New (Method : PyObject) return PyObject;
|
|
|
|
|
pragma Import (C, PyClassMethod_New, "PyClassMethod_New");
|
|
|
|
|
|
|
|
|
|
Def : constant MethodDef_Access := new PyMethodDef'(Func);
|
|
|
|
|
C_Func : PyObject;
|
|
|
|
|
Result : Integer;
|
2011-01-14 15:10:59 +00:00
|
|
|
Meth : PyObject;
|
2007-06-11 07:57:59 +00:00
|
|
|
pragma Unreferenced (Result);
|
|
|
|
|
begin
|
|
|
|
|
Def.Flags := Def.Flags or METH_CLASS;
|
|
|
|
|
|
2010-11-15 14:56:33 +00:00
|
|
|
C_Func := PyCFunction_New
|
|
|
|
|
(Def, null, PyString_FromString (PyModule_Getname (Module)));
|
2007-06-11 07:57:59 +00:00
|
|
|
if C_Func /= null then
|
2011-01-14 15:10:59 +00:00
|
|
|
Meth := PyClassMethod_New (C_Func);
|
|
|
|
|
Result := PyObject_SetAttrString (Class, Func.Name, Meth);
|
|
|
|
|
Py_DECREF (Meth);
|
2007-06-11 07:57:59 +00:00
|
|
|
end if;
|
|
|
|
|
end Add_Class_Method;
|
|
|
|
|
|
2010-11-18 11:20:51 +00:00
|
|
|
-----------------------
|
|
|
|
|
-- PyDescr_NewGetSet --
|
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
|
|
function PyDescr_NewGetSet
|
|
|
|
|
(Typ : PyObject;
|
|
|
|
|
Name : String;
|
|
|
|
|
Setter : C_Setter := null;
|
|
|
|
|
Getter : C_Getter := null;
|
|
|
|
|
Doc : String := "";
|
|
|
|
|
Closure : System.Address := System.Null_Address) return Boolean
|
|
|
|
|
is
|
|
|
|
|
function To_Callback is new Standard.Ada.Unchecked_Conversion
|
|
|
|
|
(C_Getter, C_Callback);
|
|
|
|
|
function To_Callback is new Standard.Ada.Unchecked_Conversion
|
|
|
|
|
(C_Setter, C_Callback);
|
|
|
|
|
|
|
|
|
|
function Internal
|
|
|
|
|
(Typ : PyObject; Name : chars_ptr;
|
|
|
|
|
Setter, Getter : C_Callback; Doc : chars_ptr;
|
|
|
|
|
Closure : System.Address) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pydescr_newGetSet");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal
|
2010-11-18 11:20:51 +00:00
|
|
|
(Typ, New_String (Name), To_Callback (Setter),
|
|
|
|
|
To_Callback (Getter), New_String (Doc), Closure) /= 0;
|
|
|
|
|
end PyDescr_NewGetSet;
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
-----------------------
|
|
|
|
|
-- Create_Method_Def --
|
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
|
|
function Create_Method_Def
|
|
|
|
|
(Name : String;
|
|
|
|
|
Func : C_Method_Vargs;
|
|
|
|
|
Doc : String := "")
|
|
|
|
|
return PyMethodDef is
|
|
|
|
|
begin
|
|
|
|
|
return (Name => New_String (Name),
|
|
|
|
|
Func => To_Callback (Func),
|
|
|
|
|
Flags => METH_VARGS,
|
|
|
|
|
Doc => New_String (Doc));
|
|
|
|
|
end Create_Method_Def;
|
|
|
|
|
|
|
|
|
|
-----------------------
|
|
|
|
|
-- Create_Method_Def --
|
|
|
|
|
-----------------------
|
|
|
|
|
|
|
|
|
|
function Create_Method_Def
|
|
|
|
|
(Name : String;
|
|
|
|
|
Func : C_Method_Keywords;
|
|
|
|
|
Doc : String := "")
|
|
|
|
|
return PyMethodDef
|
|
|
|
|
is
|
|
|
|
|
D : chars_ptr := Null_Ptr;
|
|
|
|
|
begin
|
|
|
|
|
if Doc /= "" then
|
|
|
|
|
D := New_String (Doc);
|
|
|
|
|
end if;
|
|
|
|
|
|
|
|
|
|
return (Name => New_String (Name),
|
|
|
|
|
Func => To_Callback (Func),
|
2012-06-21 14:40:19 +00:00
|
|
|
Flags => METH_KEYWORDS or METH_VARGS,
|
2007-06-11 07:57:59 +00:00
|
|
|
Doc => D);
|
|
|
|
|
end Create_Method_Def;
|
|
|
|
|
|
2010-11-15 14:56:33 +00:00
|
|
|
-------------------
|
|
|
|
|
-- Lookup_Object --
|
|
|
|
|
-------------------
|
2007-06-11 07:57:59 +00:00
|
|
|
|
2010-11-15 14:56:33 +00:00
|
|
|
function Lookup_Object
|
2007-06-11 07:57:59 +00:00
|
|
|
(Module : String; Name : String) return PyObject is
|
|
|
|
|
begin
|
2010-11-15 14:56:33 +00:00
|
|
|
return Lookup_Object (PyImport_AddModule (Module), Name);
|
|
|
|
|
end Lookup_Object;
|
2007-06-11 07:57:59 +00:00
|
|
|
|
2010-11-15 14:56:33 +00:00
|
|
|
-------------------
|
|
|
|
|
-- Lookup_Object --
|
|
|
|
|
-------------------
|
2007-06-11 07:57:59 +00:00
|
|
|
|
2010-11-15 14:56:33 +00:00
|
|
|
function Lookup_Object
|
2007-06-11 07:57:59 +00:00
|
|
|
(Module : PyObject; Name : String) return PyObject
|
|
|
|
|
is
|
2010-11-15 14:56:33 +00:00
|
|
|
Dict : PyObject;
|
2007-06-11 07:57:59 +00:00
|
|
|
begin
|
2010-11-15 14:56:33 +00:00
|
|
|
if Module /= null then
|
|
|
|
|
Dict := PyModule_GetDict (Module);
|
|
|
|
|
return PyDict_GetItemString (Dict, Name);
|
2007-06-11 07:57:59 +00:00
|
|
|
end if;
|
2010-11-15 14:56:33 +00:00
|
|
|
return null;
|
|
|
|
|
end Lookup_Object;
|
2007-06-11 07:57:59 +00:00
|
|
|
|
2008-11-24 11:52:42 +00:00
|
|
|
-------------
|
|
|
|
|
-- Py_Main --
|
|
|
|
|
-------------
|
|
|
|
|
|
|
|
|
|
function Py_Main return Integer is
|
|
|
|
|
function Internal return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_py_main");
|
|
|
|
|
begin
|
|
|
|
|
return Internal;
|
|
|
|
|
end Py_Main;
|
|
|
|
|
|
2007-06-11 07:57:59 +00:00
|
|
|
---------------------
|
|
|
|
|
-- PyCObject_Check --
|
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
|
function PyCObject_Check (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pycobject_check");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Obj) = 1;
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyCObject_Check;
|
|
|
|
|
|
|
|
|
|
--------------------
|
|
|
|
|
-- PyMethod_Check --
|
|
|
|
|
--------------------
|
|
|
|
|
|
|
|
|
|
function PyMethod_Check (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "ada_pymethod_check");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Obj) = 1;
|
2007-06-11 07:57:59 +00:00
|
|
|
end PyMethod_Check;
|
|
|
|
|
|
2012-06-19 15:51:12 +00:00
|
|
|
-------------------
|
|
|
|
|
-- Py_IsSubclass --
|
|
|
|
|
-------------------
|
2007-06-11 07:57:59 +00:00
|
|
|
|
2012-06-19 15:51:12 +00:00
|
|
|
function Py_IsSubclass
|
2007-06-11 07:57:59 +00:00
|
|
|
(Class : PyObject; Base : PyObject) return Boolean
|
|
|
|
|
is
|
|
|
|
|
function Internal (Class, Base : PyObject) return Integer;
|
2012-06-19 15:51:12 +00:00
|
|
|
pragma Import (C, Internal, "ada_is_subclass");
|
2007-06-11 07:57:59 +00:00
|
|
|
begin
|
2015-03-02 08:52:19 -05:00
|
|
|
return Internal (Class, Base) /= 0;
|
2012-06-19 15:51:12 +00:00
|
|
|
end Py_IsSubclass;
|
2007-06-11 07:57:59 +00:00
|
|
|
|
2010-11-15 14:56:33 +00:00
|
|
|
--------------
|
|
|
|
|
-- Type_New --
|
|
|
|
|
--------------
|
|
|
|
|
|
|
|
|
|
function Type_New
|
|
|
|
|
(Name : String;
|
|
|
|
|
Bases : PyTuple;
|
|
|
|
|
Dict : PyObject;
|
|
|
|
|
Metatype : PyTypeObject := null) return PyObject
|
|
|
|
|
is
|
|
|
|
|
function Internal
|
|
|
|
|
(Meta : PyTypeObject;
|
|
|
|
|
Name : Interfaces.C.Strings.chars_ptr;
|
|
|
|
|
Bases : PyObject;
|
|
|
|
|
Dict : PyObject) return PyObject;
|
|
|
|
|
pragma Import (C, Internal, "ada_type_new");
|
|
|
|
|
|
|
|
|
|
C : chars_ptr := New_String (Name);
|
|
|
|
|
Result : PyObject;
|
(Run_Command): cleanup handling of Hide_Output.
We now longer create and call our custom _gnatcoll class to redirect
sys.stdout, sys.stderr and sys.displayhook. This method was fragile
in the face of multi-tasking (L607-001). It was also hiding too
much in fact, since it was often confusing for users that their
"print" statements did not generate any output (L620-027).
Instead, we now compile with special flags with Py_CompileString,
so that python does not generate the call to displayhook.
Because of this, we cannot use __builtins__._ to look at the result
of the previous command, but we get this output directly from
PyEval_EvalCode when the code is an expression.
One of the changes for users is that we can no longer execute a
string containing a class or function definition or import statement,
and expect to get its output (but in fact there is none, so the code
was suspicious in the first place).
A few other optimizations in Run_Command (logging directly using the
traces module, rather than through the console class), and avoid
string manipulation when possible.
Provide support for logging exception messages in the log file, to
help debug scripts.
Finally, this code is in preparation for support of python3 (L619-031)
git-svn-id: svn+ssh://svn.eu.adacore.com/Dev/trunk/gps@191180 936e1b1b-40f2-da11-902a-00137254ae57
2012-06-20 19:23:03 +00:00
|
|
|
|
2010-11-15 14:56:33 +00:00
|
|
|
begin
|
|
|
|
|
Result := Internal (Metatype, C, Bases, Dict);
|
|
|
|
|
Free (C);
|
|
|
|
|
return Result;
|
|
|
|
|
end Type_New;
|
|
|
|
|
|
2015-03-31 16:02:33 +03:00
|
|
|
---------
|
|
|
|
|
-- Name --
|
|
|
|
|
----------
|
|
|
|
|
|
|
|
|
|
function Name (Obj : PyTypeObject) return String is
|
|
|
|
|
function Internal (Obj : PyTypeObject) return chars_ptr;
|
|
|
|
|
pragma Import (C, Internal, "ada_tp_name");
|
|
|
|
|
|
|
|
|
|
begin
|
|
|
|
|
return Value (Internal (Obj));
|
|
|
|
|
end Name;
|
|
|
|
|
|
2010-11-15 14:56:33 +00:00
|
|
|
-------------------------
|
|
|
|
|
-- PyObject_IsInstance --
|
|
|
|
|
-------------------------
|
|
|
|
|
|
|
|
|
|
function PyObject_IsInstance
|
|
|
|
|
(Inst : PyObject; Cls : PyObject) return Boolean
|
|
|
|
|
is
|
|
|
|
|
function Internal (Inst, Cls : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "PyObject_IsInstance");
|
|
|
|
|
begin
|
2015-03-02 08:52:14 -05:00
|
|
|
return Internal (Inst, Cls) /= 0;
|
2010-11-15 14:56:33 +00:00
|
|
|
end PyObject_IsInstance;
|
|
|
|
|
|
2011-03-21 16:14:36 +00:00
|
|
|
---------------------
|
|
|
|
|
-- PyObject_IsTrue --
|
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
|
|
function PyObject_IsTrue (Obj : PyObject) return Boolean is
|
|
|
|
|
function Internal (Obj : PyObject) return Integer;
|
|
|
|
|
pragma Import (C, Internal, "PyObject_IsTrue");
|
|
|
|
|
Val : Integer;
|
|
|
|
|
begin
|
|
|
|
|
Val := Internal (Obj);
|
|
|
|
|
if Val = -1 then
|
|
|
|
|
return False; -- An error
|
|
|
|
|
else
|
|
|
|
|
return Val /= 0;
|
|
|
|
|
end if;
|
|
|
|
|
end PyObject_IsTrue;
|
|
|
|
|
|
2008-04-17 12:12:00 +00:00
|
|
|
end GNATCOLL.Python;
|