Changeset View
Changeset View
Standalone View
Standalone View
source/blender/python/generic/py_capi_utils.c
| Context not available. | |||||
| #include <Python.h> | #include <Python.h> | ||||
| #include <frameobject.h> | #include <frameobject.h> | ||||
| #include "BLI_string.h" | |||||
| #include "BLI_utildefines.h" /* for bool */ | #include "BLI_utildefines.h" /* for bool */ | ||||
| #include "py_capi_utils.h" | #include "py_capi_utils.h" | ||||
| Context not available. | |||||
| return PyC_UnicodeFromByteAndSize(str, strlen(str)); | return PyC_UnicodeFromByteAndSize(str, strlen(str)); | ||||
| } | } | ||||
| /* convert string from UTF-8 to the file system default encoding */ | |||||
| char *PyC_EncodeFSDefault(const char *str_u8, char *str_fs, const size_t sizeof_str_fs) | |||||
| { | |||||
| PyObject *py_str, *py_bytes; | |||||
| if (!Py_FileSystemDefaultEncoding || strcmp(Py_FileSystemDefaultEncoding, "utf-8")) { | |||||
| py_str = PyUnicode_FromString(str_u8); | |||||
| if (!py_str) { | |||||
| PyErr_Clear(); | |||||
| /* Error condition: a junk input string (not properly encoded in UTF-8) | |||||
| * was given; encoding conversion is not performed */ | |||||
| BLI_strncpy(str_fs, str_u8, sizeof_str_fs); | |||||
| return str_fs; | |||||
| } | |||||
| py_bytes = PyUnicode_EncodeFSDefault(py_str); | |||||
| Py_DECREF(py_str); | |||||
| if (!py_bytes) { | |||||
| PyErr_Clear(); | |||||
| /* Error condition: the given string contains one or more characters that | |||||
| * cannot be represented in the file system default encoding; no encoding | |||||
| * conversion is performed */ | |||||
| BLI_strncpy(str_fs, str_u8, sizeof_str_fs); | |||||
| return str_fs; | |||||
| } | |||||
| BLI_strncpy(str_fs, PyBytes_AS_STRING(py_bytes), sizeof_str_fs); | |||||
| Py_DECREF(py_bytes); | |||||
| } | |||||
| else { | |||||
| /* Normal condition: the file system default encoding is UTF-8; no encoding | |||||
| * conversion is necessary */ | |||||
| BLI_strncpy(str_fs, str_u8, sizeof_str_fs); | |||||
| } | |||||
| return str_fs; | |||||
| } | |||||
| /***************************************************************************** | /***************************************************************************** | ||||
| * Description: This function creates a new Python dictionary object. | * Description: This function creates a new Python dictionary object. | ||||
| * note: dict is owned by sys.modules["__main__"] module, reference is borrowed | * note: dict is owned by sys.modules["__main__"] module, reference is borrowed | ||||
| Context not available. | |||||