std.ffi.python Package

Function Description

The ffi.python package provides the interoperability between Cangjie and Python to be compatible with powerful computing and AI ecosystems.

This package:

  1. Provides a global interpreter object Python of the PythonBuiltins type, which can be used to:

    • Load and unload interpreter resources.
    • Obtain the version of the Python interpreter in use.
    • Import and use third-party modules of Python.

    For the detailed description and use of this package, see Cangjie Programming Language Developer Guide (Cross-Language Interoperability/Interoperability with Python/PythonBuiltins Class).

  2. Provides the conversion between Python data types and Cangjie data types. For example, PyBool, PyLong, PyFloat, PyString, PyTuple, PyList, PyDict, and PySet are inherited from the PyObject type. The PyObject type provides universal interfaces for all types, such as member variable access, function access, and conversion to Cangjie types. The subclasses of the PyObject type provide function interfaces specific to each type.

    For the detailed description and use of the mapping relationship, see Cangjie Programming Language Developer Guide (Cross-Language Interoperability/Interoperability with Python/Type Mapping).

  3. Supports simple function registration and Python's callback of Cangjie functions. Python calls back the Cangjie code through C language and using its third-party libraries, ctypes and _ctypes.

    For the detailed description and use of the mapping relationship, see Cangjie Programming Language Developer Guide (Cross-Language Interoperability/Interoperability with Python/Callback Registration of Cangjie and Python). This section describes the type mapping between parameters and return values for callback, as well as the calling methods.

Note:

  • This package is not supported on Windows.

API List

Constants and Variables

NameDescription
PYLOGSpecifies the ffi.python log object, which is used for printing control and output.
PythonSpecifies the global resource of the Python interpreter.

Interface

NameDescription
CjObjProvides a function for converting a Cangjie type to a Python Object type.
PyFFITypeIndicates the type that supports interoperability with Python.

Class

NameDescription
PyBoolIndicates the Boolean type of Python.
PyCFuncIndicates the C-style function type of Python.
PyDict<K, V> where K <: Hashable & Equatable<K> & PyFFITypeIndicates the dictionary type of Python.
PyFloatIndicates the floating-point number type of Python.
PyList<T> where T <: PyFFITypeIndicates the list type of Python.
PyLongIndicates the integer type of Python.
PyObjIndicates the parent class of all Python types.
PyObjIteratorIndicates the iterator type of PyObj.
PyStringIndicates the string type of Python.
PySet<T> where T <: Hashable & Equatable<T> & PyFFITypeIndicates the set type of Python.
PySlice<T> where T <: Countable<T> & Comparable<T> & Equatable<T> & CjObjIndicates the interval type of Python.
PyTupleIndicates the tuple type of Python.
PythonLoggerIndicates the log type of the ffi.python package.
PythonBuiltinsIndicates the built-in function set of Python.

Exception Class

NameDescription
PythonExceptionIndicates the exception type from the ffi.python package.