Blender  V2.93
Macros
py_capi_utils.c File Reference
#include <Python.h>
#include <frameobject.h>
#include "BLI_utildefines.h"
#include "py_capi_utils.h"
#include "python_utildefines.h"
#include "MEM_guardedalloc.h"
#include "BLI_string.h"
#include "BLI_string_utf8.h"

Go to the source code of this file.

Macros

#define PY_SSIZE_T_CLEAN
 

Functions

Fast Python to C Array Conversion for Primitive Types
int PyC_AsArray_FAST (void *array, PyObject *value_fast, const Py_ssize_t length, const PyTypeObject *type, const bool is_double, const char *error_prefix)
 
int PyC_AsArray (void *array, PyObject *value, const Py_ssize_t length, const PyTypeObject *type, const bool is_double, const char *error_prefix)
 
Typed Tuple Packing
Note
See #PyC_Tuple_Pack_* macros that take multiple arguments.
PyObject * PyC_Tuple_PackArray_F32 (const float *array, uint len)
 
PyObject * PyC_Tuple_PackArray_F64 (const double *array, uint len)
 
PyObject * PyC_Tuple_PackArray_I32 (const int *array, uint len)
 
PyObject * PyC_Tuple_PackArray_I32FromBool (const int *array, uint len)
 
PyObject * PyC_Tuple_PackArray_Bool (const bool *array, uint len)
 
Tuple/List Filling
void PyC_Tuple_Fill (PyObject *tuple, PyObject *value)
 
void PyC_List_Fill (PyObject *list, PyObject *value)
 
Bool/Enum Argument Parsing
int PyC_ParseBool (PyObject *o, void *p)
 
int PyC_ParseStringEnum (PyObject *o, void *p)
 
const char * PyC_StringEnum_FindIDFromValue (const struct PyC_StringEnumItems *items, const int value)
 
int PyC_CheckArgs_DeepCopy (PyObject *args)
 
Simple Printing (for debugging)

These are useful to run directly from a debugger to be able to inspect the state.

void PyC_ObSpit (const char *name, PyObject *var)
 
void PyC_ObSpitStr (char *result, size_t result_len, PyObject *var)
 
void PyC_LineSpit (void)
 
void PyC_StackSpit (void)
 
Access Current Frame File Name & Line Number
void PyC_FileAndNum (const char **r_filename, int *r_lineno)
 
void PyC_FileAndNum_Safe (const char **r_filename, int *r_lineno)
 
Object Access Utilities
PyObject * PyC_Object_GetAttrStringArgs (PyObject *o, Py_ssize_t n,...)
 
Frozen Set Creation
PyObject * PyC_FrozenSetFromStrings (const char **strings)
 
Exception Utilities
PyObject * PyC_Err_Format_Prefix (PyObject *exception_type_prefix, const char *format,...)
 
PyObject * PyC_Err_SetString_Prefix (PyObject *exception_type_prefix, const char *str)
 
void PyC_Err_PrintWithFunc (PyObject *py_func)
 
Exception Buffer Access
static void pyc_exception_buffer_handle_system_exit (PyObject *error_type, PyObject *error_value, PyObject *error_traceback)
 
PyObject * PyC_ExceptionBuffer (void)
 
PyObject * PyC_ExceptionBuffer_Simple (void)
 
Unicode Conversion

In some cases we need to coerce strings, avoid doing this inline.

const char * PyC_UnicodeAsByteAndSize (PyObject *py_str, Py_ssize_t *size, PyObject **coerce)
 
const char * PyC_UnicodeAsByte (PyObject *py_str, PyObject **coerce)
 
PyObject * PyC_UnicodeFromByteAndSize (const char *str, Py_ssize_t size)
 
PyObject * PyC_UnicodeFromByte (const char *str)
 
Name Space Creation/Manipulation
PyObject * PyC_DefaultNameSpace (const char *filename)
 
bool PyC_NameSpace_ImportArray (PyObject *py_dict, const char *imports[])
 
void PyC_MainModule_Backup (PyObject **r_main_mod)
 
void PyC_MainModule_Restore (PyObject *main_mod)
 
bool PyC_IsInterpreterActive (void)
 
#Py_SetPythonHome Wrapper
void PyC_RunQuicky (const char *filepath, int n,...)
 
void * PyC_RNA_AsPointer (PyObject *value, const char *type_name)
 
Flag Set Utilities (#PyC_FlagSet)

Convert to/from Python set of strings to an int flag.

PyObject * PyC_FlagSet_AsString (PyC_FlagSet *item)
 
int PyC_FlagSet_ValueFromID_int (PyC_FlagSet *item, const char *identifier, int *r_value)
 
int PyC_FlagSet_ValueFromID (PyC_FlagSet *item, const char *identifier, int *r_value, const char *error_prefix)
 
int PyC_FlagSet_ToBitfield (PyC_FlagSet *items, PyObject *value, int *r_value, const char *error_prefix)
 
PyObject * PyC_FlagSet_FromBitfield (PyC_FlagSet *items, int flag)
 
Run String (Evaluate to Primitive Types)
bool PyC_RunString_AsNumber (const char *imports[], const char *expr, const char *filename, double *r_value)
 
bool PyC_RunString_AsIntPtr (const char *imports[], const char *expr, const char *filename, intptr_t *r_value)
 
bool PyC_RunString_AsStringAndSize (const char *imports[], const char *expr, const char *filename, char **r_value, size_t *r_value_size)
 
bool PyC_RunString_AsString (const char *imports[], const char *expr, const char *filename, char **r_value)
 
Int Conversion
Note
Python doesn't provide overflow checks for specific bit-widths.
int PyC_Long_AsBool (PyObject *value)
 
int8_t PyC_Long_AsI8 (PyObject *value)
 
int16_t PyC_Long_AsI16 (PyObject *value)
 
uint8_t PyC_Long_AsU8 (PyObject *value)
 
uint16_t PyC_Long_AsU16 (PyObject *value)
 
uint32_t PyC_Long_AsU32 (PyObject *value)
 
Py_buffer Utils
char PyC_StructFmt_type_from_str (const char *typestr)
 
bool PyC_StructFmt_type_is_float_any (char format)
 
bool PyC_StructFmt_type_is_int_any (char format)
 
bool PyC_StructFmt_type_is_byte (char format)
 
bool PyC_StructFmt_type_is_bool (char format)
 

Detailed Description

Extend upon CPython's API, filling in some gaps, these functions use PyC_ prefix to distinguish them apart from CPython.

Note
This module should only depend on CPython, however it currently uses BLI_string_utf8() for unicode conversion.

Definition in file py_capi_utils.c.

Macro Definition Documentation

◆ PY_SSIZE_T_CLEAN

#define PY_SSIZE_T_CLEAN

Definition at line 29 of file py_capi_utils.c.

Function Documentation

◆ PyC_AsArray()

int PyC_AsArray ( void *  array,
PyObject *  value,
const Py_ssize_t  length,
const PyTypeObject *  type,
const bool  is_double,
const char *  error_prefix 
)

◆ PyC_AsArray_FAST()

int PyC_AsArray_FAST ( void *  array,
PyObject *  value_fast,
const Py_ssize_t  length,
const PyTypeObject *  type,
const bool  is_double,
const char *  error_prefix 
)

◆ PyC_CheckArgs_DeepCopy()

int PyC_CheckArgs_DeepCopy ( PyObject *  args)

◆ PyC_DefaultNameSpace()

PyObject* PyC_DefaultNameSpace ( const char *  filename)

◆ PyC_Err_Format_Prefix()

PyObject* PyC_Err_Format_Prefix ( PyObject *  exception_type_prefix,
const char *  format,
  ... 
)

Similar to #PyErr_Format(),

Implementation - we cant actually prepend the existing exception, because it could have any arguments given to it, so instead we get its __str__ output and raise our own exception including it.

Definition at line 530 of file py_capi_utils.c.

References NULL.

Referenced by bpy_class_call(), PyC_Err_SetString_Prefix(), and pyrna_py_to_prop().

◆ PyC_Err_PrintWithFunc()

void PyC_Err_PrintWithFunc ( PyObject *  py_func)

◆ PyC_Err_SetString_Prefix()

PyObject* PyC_Err_SetString_Prefix ( PyObject *  exception_type_prefix,
const char *  str 
)

Definition at line 564 of file py_capi_utils.c.

References PyC_Err_Format_Prefix(), and str.

Referenced by bpy_app_debug_value_set().

◆ pyc_exception_buffer_handle_system_exit()

static void pyc_exception_buffer_handle_system_exit ( PyObject *  error_type,
PyObject *  error_value,
PyObject *  error_traceback 
)
static

Definition at line 594 of file py_capi_utils.c.

Referenced by PyC_ExceptionBuffer(), and PyC_ExceptionBuffer_Simple().

◆ PyC_ExceptionBuffer()

PyObject* PyC_ExceptionBuffer ( void  )

◆ PyC_ExceptionBuffer_Simple()

PyObject* PyC_ExceptionBuffer_Simple ( void  )

◆ PyC_FileAndNum()

void PyC_FileAndNum ( const char **  r_filename,
int *  r_lineno 
)

Definition at line 402 of file py_capi_utils.c.

References mod(), and NULL.

Referenced by BPy_errors_to_report_ex(), PyC_FileAndNum_Safe(), PyC_LineSpit(), and pyrna_func_call().

◆ PyC_FileAndNum_Safe()

void PyC_FileAndNum_Safe ( const char **  r_filename,
int *  r_lineno 
)

Definition at line 452 of file py_capi_utils.c.

References PyC_FileAndNum(), and PyC_IsInterpreterActive().

◆ PyC_FlagSet_AsString()

PyObject* PyC_FlagSet_AsString ( PyC_FlagSet item)

Definition at line 1133 of file py_capi_utils.c.

References PyC_FlagSet::identifier.

Referenced by PyC_FlagSet_ValueFromID().

◆ PyC_FlagSet_FromBitfield()

PyObject* PyC_FlagSet_FromBitfield ( PyC_FlagSet items,
int  flag 
)

Definition at line 1217 of file py_capi_utils.c.

References PyC_StringEnum::items, NULL, ret, and PyC_StringEnumItems::value.

Referenced by bpy_bmesh_select_mode_get().

◆ PyC_FlagSet_ToBitfield()

int PyC_FlagSet_ToBitfield ( PyC_FlagSet items,
PyObject *  value,
int *  r_value,
const char *  error_prefix 
)

◆ PyC_FlagSet_ValueFromID()

int PyC_FlagSet_ValueFromID ( PyC_FlagSet item,
const char *  identifier,
int *  r_value,
const char *  error_prefix 
)

◆ PyC_FlagSet_ValueFromID_int()

int PyC_FlagSet_ValueFromID_int ( PyC_FlagSet item,
const char *  identifier,
int *  r_value 
)

Definition at line 1144 of file py_capi_utils.c.

References PyC_FlagSet::identifier, STREQ, and PyC_FlagSet::value.

Referenced by PyC_FlagSet_ValueFromID().

◆ PyC_FrozenSetFromStrings()

PyObject* PyC_FrozenSetFromStrings ( const char **  strings)

Definition at line 501 of file py_capi_utils.c.

References NULL, ret, and str.

Referenced by BPyInit__bpy_path().

◆ PyC_IsInterpreterActive()

bool PyC_IsInterpreterActive ( void  )

◆ PyC_LineSpit()

void PyC_LineSpit ( void  )

Definition at line 364 of file py_capi_utils.c.

References PyC_FileAndNum(), and PyC_IsInterpreterActive().

Referenced by _RNA_warning().

◆ PyC_List_Fill()

void PyC_List_Fill ( PyObject *  list,
PyObject *  value 
)

Definition at line 219 of file py_capi_utils.c.

◆ PyC_Long_AsBool()

int PyC_Long_AsBool ( PyObject *  value)

◆ PyC_Long_AsI16()

int16_t PyC_Long_AsI16 ( PyObject *  value)

Definition at line 1442 of file py_capi_utils.c.

References INT16_MAX, and UNLIKELY.

Referenced by bpy_app_debug_value_set().

◆ PyC_Long_AsI8()

int8_t PyC_Long_AsI8 ( PyObject *  value)

Definition at line 1432 of file py_capi_utils.c.

References INT8_MAX, and UNLIKELY.

◆ PyC_Long_AsU16()

uint16_t PyC_Long_AsU16 ( PyObject *  value)

Definition at line 1467 of file py_capi_utils.c.

References UINT16_MAX, and UNLIKELY.

◆ PyC_Long_AsU32()

uint32_t PyC_Long_AsU32 ( PyObject *  value)

Definition at line 1477 of file py_capi_utils.c.

References UINT32_MAX, and UNLIKELY.

Referenced by C_BVHTree_FromPolygons(), pygpu_framebuffer_clear(), and pygpu_IndexBuf__tp_new().

◆ PyC_Long_AsU8()

uint8_t PyC_Long_AsU8 ( PyObject *  value)

Definition at line 1457 of file py_capi_utils.c.

References UINT8_MAX, and UNLIKELY.

◆ PyC_MainModule_Backup()

void PyC_MainModule_Backup ( PyObject **  r_main_mod)

◆ PyC_MainModule_Restore()

void PyC_MainModule_Restore ( PyObject *  main_mod)

◆ PyC_NameSpace_ImportArray()

bool PyC_NameSpace_ImportArray ( PyObject *  py_dict,
const char *  imports[] 
)

◆ PyC_Object_GetAttrStringArgs()

PyObject* PyC_Object_GetAttrStringArgs ( PyObject *  o,
Py_ssize_t  n,
  ... 
)

Definition at line 468 of file py_capi_utils.c.

◆ PyC_ObSpit()

void PyC_ObSpit ( const char *  name,
PyObject *  var 
)

Definition at line 314 of file py_capi_utils.c.

References NULL, and type.

Referenced by bpy_class_free(), PyC_RunQuicky(), and pyrna_subtype_set_rna().

◆ PyC_ObSpitStr()

void PyC_ObSpitStr ( char *  result,
size_t  result_len,
PyObject *  var 
)

A version of PyC_ObSpit that writes into a string (and doesn't take a name argument). Use for logging.

Definition at line 336 of file py_capi_utils.c.

References BLI_snprintf(), NULL, result, and type.

Referenced by pyrna_srna_ExternalType().

◆ PyC_ParseBool()

int PyC_ParseBool ( PyObject *  o,
void *  p 
)

◆ PyC_ParseStringEnum()

int PyC_ParseStringEnum ( PyObject *  o,
void *  p 
)

◆ PyC_RNA_AsPointer()

void* PyC_RNA_AsPointer ( PyObject *  value,
const char *  type_name 
)

◆ PyC_RunQuicky()

void PyC_RunQuicky ( const char *  filepath,
int  n,
  ... 
)

Definition at line 944 of file py_capi_utils.c.

References NULL, ptr, PyC_DefaultNameSpace(), PyC_ObSpit(), and ret.

◆ PyC_RunString_AsIntPtr()

bool PyC_RunString_AsIntPtr ( const char *  imports[],
const char *  expr,
const char *  filename,
intptr_t r_value 
)

◆ PyC_RunString_AsNumber()

bool PyC_RunString_AsNumber ( const char *  imports[],
const char *  expr,
const char *  filename,
double r_value 
)
Returns
success
Note
it is caller's responsibility to acquire & release GIL!

Definition at line 1244 of file py_capi_utils.c.

References CCL_NAMESPACE_BEGIN::isfinite(), mod(), NULL, PyC_DefaultNameSpace(), PyC_MainModule_Backup(), PyC_MainModule_Restore(), and PyC_NameSpace_ImportArray().

Referenced by BPY_run_string_as_number(), and bpyunits_to_value().

◆ PyC_RunString_AsString()

bool PyC_RunString_AsString ( const char *  imports[],
const char *  expr,
const char *  filename,
char **  r_value 
)

Definition at line 1393 of file py_capi_utils.c.

References PyC_RunString_AsStringAndSize().

◆ PyC_RunString_AsStringAndSize()

bool PyC_RunString_AsStringAndSize ( const char *  imports[],
const char *  expr,
const char *  filename,
char **  r_value,
size_t *  r_value_size 
)

◆ PyC_StackSpit()

void PyC_StackSpit ( void  )

Definition at line 382 of file py_capi_utils.c.

References PyC_IsInterpreterActive().

◆ PyC_StringEnum_FindIDFromValue()

const char* PyC_StringEnum_FindIDFromValue ( const struct PyC_StringEnumItems items,
const int  value 
)

◆ PyC_StructFmt_type_from_str()

char PyC_StructFmt_type_from_str ( const char *  typestr)

◆ PyC_StructFmt_type_is_bool()

bool PyC_StructFmt_type_is_bool ( char  format)

Definition at line 1561 of file py_capi_utils.c.

◆ PyC_StructFmt_type_is_byte()

bool PyC_StructFmt_type_is_byte ( char  format)

Definition at line 1549 of file py_capi_utils.c.

Referenced by gl_buffer_type_from_py_buffer().

◆ PyC_StructFmt_type_is_float_any()

bool PyC_StructFmt_type_is_float_any ( char  format)

◆ PyC_StructFmt_type_is_int_any()

bool PyC_StructFmt_type_is_int_any ( char  format)

◆ PyC_Tuple_Fill()

void PyC_Tuple_Fill ( PyObject *  tuple,
PyObject *  value 
)

Caller needs to ensure tuple is uninitialized. Handy for filling a tuple with None for eg.

Definition at line 208 of file py_capi_utils.c.

Referenced by kdtree_nearest_to_py_and_check(), py_bvhtree_nearest_to_py_none(), and py_bvhtree_raycast_to_py_none().

◆ PyC_Tuple_PackArray_Bool()

PyObject* PyC_Tuple_PackArray_Bool ( const bool *  array,
uint  len 
)

Definition at line 189 of file py_capi_utils.c.

References len.

Referenced by bpy_prop_boolean_array_set_fn().

◆ PyC_Tuple_PackArray_F32()

PyObject* PyC_Tuple_PackArray_F32 ( const float array,
uint  len 
)

◆ PyC_Tuple_PackArray_F64()

PyObject* PyC_Tuple_PackArray_F64 ( const double array,
uint  len 
)

Definition at line 162 of file py_capi_utils.c.

References len.

◆ PyC_Tuple_PackArray_I32()

PyObject* PyC_Tuple_PackArray_I32 ( const int *  array,
uint  len 
)

Definition at line 171 of file py_capi_utils.c.

References len.

Referenced by bpy_prop_int_array_set_fn().

◆ PyC_Tuple_PackArray_I32FromBool()

PyObject* PyC_Tuple_PackArray_I32FromBool ( const int *  array,
uint  len 
)

Definition at line 180 of file py_capi_utils.c.

References len.

◆ PyC_UnicodeAsByte()

const char* PyC_UnicodeAsByte ( PyObject *  py_str,
PyObject **  coerce 
)

Definition at line 813 of file py_capi_utils.c.

References NULL, and result.

Referenced by pyrna_py_to_prop(), and pyrna_WindowManager_clipboard_set().

◆ PyC_UnicodeAsByteAndSize()

const char* PyC_UnicodeAsByteAndSize ( PyObject *  py_str,
Py_ssize_t *  size,
PyObject **  coerce 
)

Definition at line 786 of file py_capi_utils.c.

References NULL, result, and size().

Referenced by idp_from_PyUnicode().

◆ PyC_UnicodeFromByte()

PyObject* PyC_UnicodeFromByte ( const char *  str)

◆ PyC_UnicodeFromByteAndSize()

PyObject* PyC_UnicodeFromByteAndSize ( const char *  str,
Py_ssize_t  size 
)