[Bf-blender-cvs] [0ce6ed47533] master: Cleanup: variable/argument naming for Python exception access

Campbell Barton noreply at git.blender.org
Mon Mar 28 08:08:15 CEST 2022


Commit: 0ce6ed47533f715a72b139cb47a4f2dfb5a0bb39
Author: Campbell Barton
Date:   Mon Mar 28 16:54:31 2022 +1100
Branches: master
https://developer.blender.org/rB0ce6ed47533f715a72b139cb47a4f2dfb5a0bb39

Cleanup: variable/argument naming for Python exception access

Names filename/filepath/fn were used interchangeably.

===================================================================

M	source/blender/python/intern/bpy_interface_run.c
M	source/blender/python/intern/bpy_traceback.c
M	source/blender/python/intern/bpy_traceback.h

===================================================================

diff --git a/source/blender/python/intern/bpy_interface_run.c b/source/blender/python/intern/bpy_interface_run.c
index 37abf930cff..14d03bf7e2b 100644
--- a/source/blender/python/intern/bpy_interface_run.c
+++ b/source/blender/python/intern/bpy_interface_run.c
@@ -47,10 +47,20 @@ static void python_script_error_jump_text(Text *text, const char *filepath)
   }
 }
 
-/* returns a dummy filename for a textblock so we can tell what file a text block comes from */
-static void bpy_text_filename_get(char *fn, const Main *bmain, size_t fn_len, const Text *text)
+/**
+ * Generate a `filepath` from a text-block so we can tell what file a text block comes from.
+ */
+static void bpy_text_filepath_get(char *filepath,
+                                  const size_t filepath_maxlen,
+                                  const Main *bmain,
+                                  const Text *text)
 {
-  BLI_snprintf(fn, fn_len, "%s%c%s", ID_BLEND_PATH(bmain, &text->id), SEP, text->id.name + 2);
+  BLI_snprintf(filepath,
+               filepath_maxlen,
+               "%s%c%s",
+               ID_BLEND_PATH(bmain, &text->id),
+               SEP,
+               text->id.name + 2);
 }
 
 /* Very annoying! Undo #_PyModule_Clear(), see T23871. */
@@ -74,21 +84,24 @@ typedef struct {
  *
  * \note Share a function for this since setup/cleanup logic is the same.
  */
-static bool 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 *filepath,
+                               struct Text *text,
+                               struct ReportList *reports,
+                               const bool do_jump)
 {
   Main *bmain_old = CTX_data_main(C);
   PyObject *main_mod = NULL;
   PyObject *py_dict = NULL, *py_result = NULL;
   PyGILState_STATE gilstate;
 
-  char fn_dummy[FILE_MAX];
+  char filepath_dummy[FILE_MAX];
   /** The `__file__` added into the name-space. */
-  const char *fn_namespace = NULL;
+  const char *filepath_namespace = NULL;
 
-  BLI_assert(fn || text);
+  BLI_assert(filepath || text);
 
-  if (fn == NULL && text == NULL) {
+  if (filepath == NULL && text == NULL) {
     return 0;
   }
 
@@ -97,41 +110,41 @@ static bool python_script_exec(
   PyC_MainModule_Backup(&main_mod);
 
   if (text) {
-    bpy_text_filename_get(fn_dummy, bmain_old, sizeof(fn_dummy), text);
-    fn_namespace = fn_dummy;
+    bpy_text_filepath_get(filepath_dummy, sizeof(filepath_dummy), bmain_old, text);
+    filepath_namespace = filepath_dummy;
 
     if (text->compiled == NULL) { /* if it wasn't already compiled, do it now */
       char *buf;
-      PyObject *fn_dummy_py;
+      PyObject *filepath_dummy_py;
 
-      fn_dummy_py = PyC_UnicodeFromByte(fn_dummy);
+      filepath_dummy_py = PyC_UnicodeFromByte(filepath_dummy);
 
       size_t buf_len_dummy;
       buf = txt_to_buf(text, &buf_len_dummy);
-      text->compiled = Py_CompileStringObject(buf, fn_dummy_py, Py_file_input, NULL, -1);
+      text->compiled = Py_CompileStringObject(buf, filepath_dummy_py, Py_file_input, NULL, -1);
       MEM_freeN(buf);
 
-      Py_DECREF(fn_dummy_py);
+      Py_DECREF(filepath_dummy_py);
 
       if (PyErr_Occurred()) {
         if (do_jump) {
-          python_script_error_jump_text(text, fn_namespace);
+          python_script_error_jump_text(text, filepath_dummy);
         }
         BPY_text_free_code(text);
       }
     }
 
     if (text->compiled) {
-      py_dict = PyC_DefaultNameSpace(fn_dummy);
+      py_dict = PyC_DefaultNameSpace(filepath_dummy);
       py_result = PyEval_EvalCode(text->compiled, py_dict, py_dict);
     }
   }
   else {
-    FILE *fp = BLI_fopen(fn, "r");
-    fn_namespace = fn;
+    FILE *fp = BLI_fopen(filepath, "r");
+    filepath_namespace = filepath;
 
     if (fp) {
-      py_dict = PyC_DefaultNameSpace(fn);
+      py_dict = PyC_DefaultNameSpace(filepath);
 
 #ifdef _WIN32
       /* Previously we used PyRun_File to run directly the code on a FILE
@@ -158,13 +171,13 @@ static bool python_script_exec(
         py_result = PyRun_String(pystring, Py_file_input, py_dict, py_dict);
       }
 #else
-      py_result = PyRun_File(fp, fn, Py_file_input, py_dict, py_dict);
+      py_result = PyRun_File(fp, filepath, Py_file_input, py_dict, py_dict);
       fclose(fp);
 #endif
     }
     else {
       PyErr_Format(
-          PyExc_IOError, "Python file \"%s\" could not be opened: %s", fn, strerror(errno));
+          PyExc_IOError, "Python file \"%s\" could not be opened: %s", filepath, strerror(errno));
       py_result = NULL;
     }
   }
@@ -175,7 +188,7 @@ static bool python_script_exec(
         /* ensure text is valid before use, the script may have freed itself */
         Main *bmain_new = CTX_data_main(C);
         if ((bmain_old == bmain_new) && (BLI_findindex(&bmain_new->texts, text) != -1)) {
-          python_script_error_jump_text(text, fn_namespace);
+          python_script_error_jump_text(text, filepath_namespace);
         }
       }
     }
diff --git a/source/blender/python/intern/bpy_traceback.c b/source/blender/python/intern/bpy_traceback.c
index fb89925400e..87e6375c7d1 100644
--- a/source/blender/python/intern/bpy_traceback.c
+++ b/source/blender/python/intern/bpy_traceback.c
@@ -115,13 +115,13 @@ finally:
 }
 /* end copied function! */
 
-void python_script_error_jump(const char *filepath, int *lineno, int *offset)
+void python_script_error_jump(const char *filepath, int *r_lineno, int *r_offset)
 {
   PyObject *exception, *value;
   PyTracebackObject *tb;
 
-  *lineno = -1;
-  *offset = 0;
+  *r_lineno = -1;
+  *r_offset = 0;
 
   PyErr_Fetch(&exception, &value, (PyObject **)&tb);
   if (exception == NULL) {
@@ -129,27 +129,27 @@ void python_script_error_jump(const char *filepath, int *lineno, int *offset)
   }
 
   if (PyErr_GivenExceptionMatches(exception, PyExc_SyntaxError)) {
-    /* no trace-back available when `SyntaxError`.
-     * python has no API's to this. reference #parse_syntax_error() from pythonrun.c */
+    /* No trace-back available when `SyntaxError`.
+     * Python has no API's to this. reference #parse_syntax_error() from `pythonrun.c`. */
     PyErr_NormalizeException(&exception, &value, (PyObject **)&tb);
 
-    if (value) { /* should always be true */
+    if (value) { /* Should always be true. */
       PyObject *message;
-      PyObject *filename_py, *text_py;
+      PyObject *filepath_exc_py, *text_py;
 
-      if (parse_syntax_error(value, &message, &filename_py, lineno, offset, &text_py)) {
-        const char *filename = PyUnicode_AsUTF8(filename_py);
+      if (parse_syntax_error(value, &message, &filepath_exc_py, r_lineno, r_offset, &text_py)) {
+        const char *filepath_exc = PyUnicode_AsUTF8(filepath_exc_py);
         /* python adds a '/', prefix, so check for both */
-        if ((BLI_path_cmp(filename, filepath) == 0) ||
-            (ELEM(filename[0], '\\', '/') && BLI_path_cmp(filename + 1, filepath) == 0)) {
+        if ((BLI_path_cmp(filepath_exc, filepath) == 0) ||
+            (ELEM(filepath_exc[0], '\\', '/') && BLI_path_cmp(filepath_exc + 1, filepath) == 0)) {
           /* good */
         }
         else {
-          *lineno = -1;
+          *r_lineno = -1;
         }
       }
       else {
-        *lineno = -1;
+        *r_lineno = -1;
       }
     }
     PyErr_Restore(exception, value, (PyObject *)tb); /* takes away reference! */
@@ -170,7 +170,7 @@ void python_script_error_jump(const char *filepath, int *lineno, int *offset)
       Py_DECREF(coerce);
 
       if (match) {
-        *lineno = tb->tb_lineno;
+        *r_lineno = tb->tb_lineno;
         /* used to break here, but better find the inner most line */
       }
     }
diff --git a/source/blender/python/intern/bpy_traceback.h b/source/blender/python/intern/bpy_traceback.h
index 99e032f3594..c96f8751989 100644
--- a/source/blender/python/intern/bpy_traceback.h
+++ b/source/blender/python/intern/bpy_traceback.h
@@ -10,7 +10,7 @@
 extern "C" {
 #endif
 
-void python_script_error_jump(const char *filepath, int *lineno, int *offset);
+void python_script_error_jump(const char *filepath, int *r_lineno, int *r_offset);
 
 #ifdef __cplusplus
 }



More information about the Bf-blender-cvs mailing list