Cleanup: Py API naming

Use BPY_execute_* prefix for all Python execution commands
This commit is contained in:
Campbell Barton
2015-12-31 21:15:29 +11:00
parent 16e1bbf1db
commit 0ffc603553
12 changed files with 53 additions and 52 deletions

View File

@@ -2272,7 +2272,7 @@ static bool ui_set_but_string_eval_num_unit(bContext *C, uiBut *but, const char
bUnit_ReplaceString(str_unit_convert, sizeof(str_unit_convert), but->drawstr,
ui_get_but_scale_unit(but, 1.0), but->block->unit->system, RNA_SUBTYPE_UNIT_VALUE(unit_type));
return (BPY_button_exec(C, str_unit_convert, value, true) != -1);
return BPY_execute_string_as_number(C, str_unit_convert, value, true);
}
#endif /* WITH_PYTHON */
@@ -2287,7 +2287,7 @@ bool ui_but_string_set_eval_num(bContext *C, uiBut *but, const char *str, double
if (str[0] != '\0') {
bool is_unit_but = (ui_but_is_float(but) && ui_but_is_unit(but));
/* only enable verbose if we won't run again with units */
if (BPY_button_exec(C, str, value, is_unit_but == false) != -1) {
if (BPY_execute_string_as_number(C, str, value, is_unit_but == false)) {
/* if the value parsed ok without unit conversion this button may still need a unit multiplier */
if (is_unit_but) {
char str_new[128];

View File

@@ -59,7 +59,7 @@ static int run_pyfile_exec(bContext *C, wmOperator *op)
char path[512];
RNA_string_get(op->ptr, "filepath", path);
#ifdef WITH_PYTHON
if (BPY_filepath_exec(C, path, op->reports)) {
if (BPY_execute_filepath(C, path, op->reports)) {
ARegion *ar = CTX_wm_region(C);
ED_region_tag_redraw(ar);
return OPERATOR_FINISHED;
@@ -125,7 +125,7 @@ static int script_reload_exec(bContext *C, wmOperator *op)
/* TODO, this crashes on netrender and keying sets, need to look into why
* disable for now unless running in debug mode */
WM_cursor_wait(1);
BPY_string_exec(C, "__import__('bpy').utils.load_scripts(reload_scripts=True)");
BPY_execute_string(C, "__import__('bpy').utils.load_scripts(reload_scripts=True)");
WM_cursor_wait(0);
WM_event_add_notifier(C, NC_WINDOW, NULL);
return OPERATOR_FINISHED;

View File

@@ -598,7 +598,7 @@ static int text_run_script(bContext *C, ReportList *reports)
void *curl_prev = text->curl;
int curc_prev = text->curc;
if (BPY_text_exec(C, text, reports, !is_live)) {
if (BPY_execute_text(C, text, reports, !is_live)) {
if (is_live) {
/* for nice live updates */
WM_event_add_notifier(C, NC_WINDOW | NA_EDITED, NULL);

View File

@@ -496,7 +496,7 @@ bool handleNumInput(bContext *C, NumInput *n, const wmEvent *event)
n->unit_sys, n->unit_type[idx]);
/* Note: with angles, we always get values as radians here... */
if (BPY_button_exec(C, str_unit_convert, &val, false) != -1) {
if (BPY_execute_string_as_number(C, str_unit_convert, &val, false)) {
n->val[idx] = (float)val;
n->val_flag[idx] &= ~NUM_INVALID;
}

View File

@@ -78,22 +78,22 @@ public:
BKE_reports_clear(reports);
char *fn = const_cast<char*>(filename.c_str());
#if 0
int status = BPY_filepath_exec(_context, fn, reports);
bool ok = BPY_execute_filepath(_context, fn, reports);
#else
int status;
bool ok;
Text *text = BKE_text_load(&_freestyle_bmain, fn, G.main->name);
if (text) {
status = BPY_text_exec(_context, text, reports, false);
ok = BPY_execute_text(_context, text, reports, false);
BKE_text_unlink(&_freestyle_bmain, text);
BKE_libblock_free(&_freestyle_bmain, text);
}
else {
BKE_reportf(reports, RPT_ERROR, "Cannot open file: %s", fn);
status = 0;
ok = false;
}
#endif
if (status != 1) {
if (ok == false) {
cerr << "\nError executing Python script from PythonInterpreter::interpretFile" << endl;
cerr << "File: " << fn << endl;
cerr << "Errors: " << endl;
@@ -113,7 +113,7 @@ public:
BKE_reports_clear(reports);
if (BPY_string_exec(_context, str.c_str()) != 0) {
if (!BPY_execute_string(_context, str.c_str())) {
BPy_errors_to_report(reports);
cerr << "\nError executing Python script from PythonInterpreter::interpretString" << endl;
cerr << "Name: " << name << endl;
@@ -133,7 +133,7 @@ public:
BKE_reports_clear(reports);
if (!BPY_text_exec(_context, text, reports, false)) {
if (!BPY_execute_text(_context, text, reports, false)) {
cerr << "\nError executing Python script from PythonInterpreter::interpretText" << endl;
cerr << "Name: " << name << endl;
cerr << "Errors: " << endl;

View File

@@ -72,10 +72,12 @@ void BPY_thread_restore(BPy_ThreadStatePtr tstate);
#define BPy_BEGIN_ALLOW_THREADS { BPy_ThreadStatePtr _bpy_saved_tstate = BPY_thread_save(); (void)0
#define BPy_END_ALLOW_THREADS BPY_thread_restore(_bpy_saved_tstate); } (void)0
bool BPY_execute_filepath(struct bContext *C, const char *filepath, struct ReportList *reports);
bool BPY_execute_text(struct bContext *C, struct Text *text, struct ReportList *reports, const bool do_jump);
bool BPY_execute_string_as_number(struct bContext *C, const char *expr, double *value, const bool verbose);
bool BPY_execute_string_ex(struct bContext *C, const char *expr, bool use_eval);
bool BPY_execute_string(struct bContext *C, const char *expr);
/* 2.5 UI Scripts */
int BPY_filepath_exec(struct bContext *C, const char *filepath, struct ReportList *reports);
int BPY_text_exec(struct bContext *C, struct Text *text, struct ReportList *reports, const bool do_jump);
void BPY_text_free_code(struct Text *text);
void BPY_modules_update(struct bContext *C); // XXX - annoying, need this for pointers that get out of date
void BPY_modules_load_user(struct bContext *C);
@@ -85,10 +87,6 @@ void BPY_app_handlers_reset(const short do_all);
void BPY_driver_reset(void);
float BPY_driver_exec(struct ChannelDriver *driver, const float evaltime);
int BPY_button_exec(struct bContext *C, const char *expr, double *value, const bool verbose);
int BPY_string_exec_ex(struct bContext *C, const char *expr, bool use_eval);
int BPY_string_exec(struct bContext *C, const char *expr);
void BPY_DECREF(void *pyob_ptr); /* Py_DECREF() */
void BPY_DECREF_RNA_INVALIDATE(void *pyob_ptr);
int BPY_context_member_get(struct bContext *C, const char *member, struct bContextDataResult *result);

View File

@@ -971,14 +971,14 @@ PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag)
/**
* \return -1 on error, else 0
* \return success
*
* \note it is caller's responsibility to acquire & release GIL!
*/
int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename)
bool PyC_RunString_AsNumber(const char *expr, double *value, const char *filename)
{
PyObject *py_dict, *mod, *retval;
int error_ret = 0;
bool ok = true;
PyObject *main_mod = NULL;
PyC_MainModule_Backup(&main_mod);
@@ -998,7 +998,7 @@ int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename
retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
if (retval == NULL) {
error_ret = -1;
ok = false;
}
else {
double val;
@@ -1024,7 +1024,7 @@ int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename
Py_DECREF(retval);
if (val == -1 && PyErr_Occurred()) {
error_ret = -1;
ok = false;
}
else if (!finite(val)) {
*value = 0.0;
@@ -1036,7 +1036,7 @@ int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename
PyC_MainModule_Restore(main_mod);
return error_ret;
return ok;
}
#endif /* #ifndef MATH_STANDALONE */

View File

@@ -79,7 +79,7 @@ int PyC_FlagSet_ValueFromID(PyC_FlagSet *item, const char *identifier, int
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);
int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename);
bool PyC_RunString_AsNumber(const char *expr, double *value, const char *filename);
int PyC_ParseBool(PyObject *o, void *p);

View File

@@ -424,8 +424,9 @@ typedef struct {
} PyModuleObject;
#endif
static int python_script_exec(bContext *C, const char *fn, struct Text *text,
struct ReportList *reports, const bool do_jump)
static bool python_script_exec(
bContext *C, const char *fn, struct Text *text,
struct ReportList *reports, const bool do_jump)
{
Main *bmain_old = CTX_data_main(C);
PyObject *main_mod = NULL;
@@ -543,13 +544,13 @@ static int python_script_exec(bContext *C, const char *fn, struct Text *text,
}
/* Can run a file or text block */
int BPY_filepath_exec(bContext *C, const char *filepath, struct ReportList *reports)
bool BPY_execute_filepath(bContext *C, const char *filepath, struct ReportList *reports)
{
return python_script_exec(C, filepath, NULL, reports, false);
}
int BPY_text_exec(bContext *C, struct Text *text, struct ReportList *reports, const bool do_jump)
bool BPY_execute_text(bContext *C, struct Text *text, struct ReportList *reports, const bool do_jump)
{
return python_script_exec(C, NULL, text, reports, do_jump);
}
@@ -572,24 +573,26 @@ void BPY_DECREF_RNA_INVALIDATE(void *pyob_ptr)
PyGILState_Release(gilstate);
}
/* return -1 on error, else 0 */
int BPY_button_exec(bContext *C, const char *expr, double *value, const bool verbose)
/**
* \return success
*/
bool BPY_execute_string_as_number(bContext *C, const char *expr, double *value, const bool verbose)
{
PyGILState_STATE gilstate;
int error_ret = 0;
bool ok = true;
if (!value || !expr) return -1;
if (expr[0] == '\0') {
*value = 0.0;
return error_ret;
return ok;
}
bpy_context_set(C, &gilstate);
error_ret = PyC_RunString_AsNumber(expr, value, "<blender button>");
ok = PyC_RunString_AsNumber(expr, value, "<blender button>");
if (error_ret) {
if (ok == false) {
if (verbose) {
BPy_errors_to_report_ex(CTX_wm_reports(C), false, false);
}
@@ -600,21 +603,21 @@ int BPY_button_exec(bContext *C, const char *expr, double *value, const bool ver
bpy_context_clear(C, &gilstate);
return error_ret;
return ok;
}
int BPY_string_exec_ex(bContext *C, const char *expr, bool use_eval)
bool BPY_execute_string_ex(bContext *C, const char *expr, bool use_eval)
{
PyGILState_STATE gilstate;
PyObject *main_mod = NULL;
PyObject *py_dict, *retval;
int error_ret = 0;
bool ok = true;
Main *bmain_back; /* XXX, quick fix for release (Copy Settings crash), needs further investigation */
if (!expr) return -1;
if (expr[0] == '\0') {
return error_ret;
return ok;
}
bpy_context_set(C, &gilstate);
@@ -631,7 +634,7 @@ int BPY_string_exec_ex(bContext *C, const char *expr, bool use_eval)
bpy_import_main_set(bmain_back);
if (retval == NULL) {
error_ret = -1;
ok = false;
BPy_errors_to_report(CTX_wm_reports(C));
}
@@ -643,12 +646,12 @@ int BPY_string_exec_ex(bContext *C, const char *expr, bool use_eval)
bpy_context_clear(C, &gilstate);
return error_ret;
return ok;
}
int BPY_string_exec(bContext *C, const char *expr)
bool BPY_execute_string(bContext *C, const char *expr)
{
return BPY_string_exec_ex(C, expr, true);
return BPY_execute_string_ex(C, expr, true);
}
void BPY_modules_load_user(bContext *C)

View File

@@ -201,7 +201,7 @@ static PyObject *bpyunits_to_value(PyObject *UNUSED(self), PyObject *args, PyObj
bUnit_ReplaceString(str, (int)str_len, uref, scale, usys, ucat);
if (PyC_RunString_AsNumber(str, &result, "<bpy_units_api>") != 0) {
if (!PyC_RunString_AsNumber(str, &result, "<bpy_units_api>")) {
if (PyErr_Occurred()) {
PyErr_Print();
PyErr_Clear();

View File

@@ -458,7 +458,7 @@ static void wm_file_read_post(bContext *C, bool is_startup_file)
/* possible python hasn't been initialized */
if (CTX_py_init_get(C)) {
/* sync addons, these may have changed from the defaults */
BPY_string_exec(C, "__import__('addon_utils').reset_all()");
BPY_execute_string(C, "__import__('addon_utils').reset_all()");
BPY_python_reset(C);
addons_loaded = true;

View File

@@ -1365,7 +1365,7 @@ static int run_python_file(int argc, const char **argv, void *data)
BLI_strncpy(filename, argv[1], sizeof(filename));
BLI_path_cwd(filename, sizeof(filename));
BPY_CTX_SETUP(BPY_filepath_exec(C, filename, NULL));
BPY_CTX_SETUP(BPY_execute_filepath(C, filename, NULL));
return 1;
}
@@ -1391,7 +1391,7 @@ static int run_python_text(int argc, const char **argv, void *data)
struct Text *text = (struct Text *)BKE_libblock_find_name(ID_TXT, argv[1]);
if (text) {
BPY_CTX_SETUP(BPY_text_exec(C, text, NULL, false));
BPY_CTX_SETUP(BPY_execute_text(C, text, NULL, false));
return 1;
}
else {
@@ -1417,7 +1417,7 @@ static int run_python_expr(int argc, const char **argv, void *data)
/* workaround for scripts not getting a bpy.context.scene, causes internal errors elsewhere */
if (argc > 1) {
BPY_CTX_SETUP(BPY_string_exec_ex(C, argv[1], false));
BPY_CTX_SETUP(BPY_execute_string_ex(C, argv[1], false));
return 1;
}
else {
@@ -1436,7 +1436,7 @@ static int run_python_console(int UNUSED(argc), const char **argv, void *data)
#ifdef WITH_PYTHON
bContext *C = data;
BPY_CTX_SETUP(BPY_string_exec(C, "__import__('code').interact()"));
BPY_CTX_SETUP(BPY_execute_string(C, "__import__('code').interact()"));
return 0;
#else
@@ -1462,7 +1462,7 @@ static int set_addons(int argc, const char **argv, void *data)
BLI_snprintf(str, slen, script_str, argv[1]);
BLI_assert(strlen(str) + 1 == slen);
BPY_CTX_SETUP(BPY_string_exec_ex(C, str, false));
BPY_CTX_SETUP(BPY_execute_string_ex(C, str, false));
free(str);
#else
UNUSED_VARS(argv, data);