7.1. Pika object PikaObj
7.1.2. Overview
The Object API provides a series of interfaces for accessing Python objects in C. Most frequently used in module development.
An object consists of two parts: properties and methods, so the object API is also divided into two parts: properties and methods.
The data type of the object itself is PikaObj, which is used by all Python objects when accessed in C.
/* list */
Args* list;
};
typedef struct PikaObj_t PikaObj;
PikaObj internally maintains a parameter table, which contains attribute information, class information, method information, etc. Be careful not to directly access the parameter table inside PikaObj, please use the object API to access PikaObj. This is because the object API, as an external interface, is stable for a long time, and the internal implementation will change frequently with the iteration of the kernel code. Directly operating the interior of PikaObj will greatly lose backward compatibility.
7.1.4. Object Properties API
This part of the API provides access to Python object properties.
PikaObj objects are dynamic, so new properties can be added to the object at any time (the properties of static objects are determined at construction time).
The APIs for primitive type properties are as follows:
/* set API */
int32_t obj_setInt(PikaObj* self, char* argPath, int64_t val);
int32_t obj_setPtr(PikaObj* self, char* argPath, void* pointer);
int32_t obj_setStr(PikaObj* self, char* argPath, char* str);
/* get API */
float obj_getFloat(PikaObj* self, char* argPath);
char* obj_getStr(PikaObj* self, char* argPath);
int64_t obj_getInt(PikaObj* self, char* argPath);
Primitive type properties are named as obj_set[Type] and obj_get[Type].
The first input parameter is the object pointer to be manipulated. The second input parameter is attribute name/attribute address.
PikaObj supports object nesting and can access properties of sub-objects. When accessing properties of sub-objects, the second parameter is the property address, and when accessing properties of this object, the second value is property name.
The third input parameter of the set method is the written property value, and the return value of the get method is the read property value. The return value of the set method is an error code, 0 means no error occurred.
int32_t obj_setArg(PikaObj* self, char* argPath, Arg* arg);
Generic properties need to be converted to primitive types when used.
Use the following API to determine the current type of a generic property.
ArgType arg_getType(Arg* self);
Use the following API to convert generic properties to primitive types.
- Determine whether an attribute exists, and the return value is 1 to indicate existence.
int32_t obj_isArgExist(PikaObj* self, char* argPath);
- Delete an attribute
int32_t obj_removeArg(PikaObj* self, char* argPath);
The return value is an error code, 0 means success.
The object method API is divided into two parts: method registration and method invocation. The method registration part is proxied by the precompiler, and the module developer only needs to use the method to call the API.
obj_run is a versatile API that can directly run Python scripts and supports multi-line scripts. The first entry parameter is a pointer to the object, and the second entry parameter is the Python script as a string. Note that when passing in a multi-line script, you should pass in a complete block of code.
7.1.6. Throw an exception
/* set Error Code, if the errCode is not 0, an exaption would be throw out */
void obj_setErrorCode(PikaObj* self, int32_t errCode);
void obj_setSysOut(PikaObj* self, char* str);