4 library(janus): Call Python from Prolog
All Application Manual Name SummaryHelp

  • Documentation
    • Reference manual
    • Packages
      • SWI-Prolog Python interface
        • library(janus): Call Python from Prolog
          • py_version/0
          • py_call/1
          • py_call/2
          • py_call/3
          • py_iter/2
          • py_iter/3
          • py_setattr/3
          • py_is_object/1
          • py_is_dict/1
          • py_free/1
          • py_with_gil/1
          • py_gil_owner/1
          • py_func/3
          • py_func/4
          • py_dot/3
          • py_dot/4
          • values/3
          • keys/2
          • key/2
          • items/2
          • py_shell/0
          • py_pp/1
          • py_pp/2
          • py_pp/3
          • py_object_dir/2
          • py_object_dict/2
          • py_obj_dir/2
          • py_obj_dict/2
          • py_type/2
          • py_isinstance/2
          • py_module_exists/1
          • py_hasattr/2
          • py_import/2
          • py_module/2
          • py_initialize/3
          • py_lib_dirs/1
          • py_add_lib_dir/1
          • py_add_lib_dir/2
          • Handling Python errors in Prolog
          • Calling and data translation errors
          • Janus and virtual environments (venv)

4.2 Calling and data translation errors

Errors may occur when converting Prolog terms to Python objects as defined in section 2. These errors are reported as instantiation_error, type_error(Type, Culprit) or domain_error(Domain, Culprit).

Defined domains are:

py_constant
In a term @(Constant), Constant is not true, false or none. For example, py_call(print(@error)).
py_keyword_arg
In a call to Python, a non keyword argument follows a keyword argument. For example, py_call(m:f(1,x=2,3), R)
py_string_as
The value for a py_string_as(As) option is invalid. For example, py_call(m:f(), R, [py_string_as(float)])
py_dict_as
The value for a py_dict_as(As) option is invalid. For example, py_call(m:f(), R, [py_dict_as(list)])
py_term
A term being translated to Python is unsupported. For example, py_call(m:f(point(1,2)), R).

Defined types are:

py_object
A Python object reference was expected. For example, py_free(42)
rational
A Python fraction instance is converted to a Prolog rational number, but the textual conversion does not produce a valid rational number. This can happen if the Python fraction is subclassed and the __str__() method does not produce a correct string.
py_key_value
Inside a {k:v, ...} representation for a dictionary we find a term that is not a key-value pair. For example, py_call(m:f({a:1, x}), R)
py_set
Inside a py_set(Elements), Elements is not a list. For example, py_call(m:f(py_set(42)), R).
py_target
In py_call(Target:FuncOrAttrOrMethod), Target is not a module (atom) or Python object reference. For example, py_call(7:f(), R).
py_callable
In py_call(Target:FuncOrAttrOrMethod), FuncOrAttrOrMethod is not an atom or compound. For example, py_call(m:7, R).