Cleanup: code-comment formatting, remove commented prints

This commit is contained in:
Campbell Barton
2023-09-19 15:00:21 +10:00
parent 085b094f18
commit 393c365daf

View File

@@ -45,7 +45,7 @@
#include "BKE_appdir.h"
#include "BKE_context.h"
#include "BKE_global.h" /* only for script checking */
#include "BKE_global.h" /* Only for script checking. */
#include "BKE_main.h"
#include "BKE_text.h"
@@ -59,7 +59,7 @@
#include "../generic/py_capi_utils.h"
/* inittab initialization functions */
/* `inittab` initialization functions. */
#include "../bmesh/bmesh_py_api.h"
#include "../generic/bgl.h"
#include "../generic/bl_math_py_api.h"
@@ -75,36 +75,41 @@ CLG_LOGREF_DECLARE_GLOBAL(BPY_LOG_CONTEXT, "bpy.context");
CLG_LOGREF_DECLARE_GLOBAL(BPY_LOG_INTERFACE, "bpy.interface");
CLG_LOGREF_DECLARE_GLOBAL(BPY_LOG_RNA, "bpy.rna");
/* for internal use, when starting and ending python scripts */
/* For internal use, when starting and ending Python scripts. */
/* In case a python script triggers another python call,
* stop bpy_context_clear from invalidating. */
/* In case a Python script triggers another Python call,
* stop #bpy_context_clear from invalidating. */
static int py_call_level = 0;
/* Set by command line arguments before Python starts. */
static bool py_use_system_env = false;
// #define TIME_PY_RUN /* simple python tests. prints on exit. */
// #define TIME_PY_RUN /* Simple python tests. prints on exit. */
#ifdef TIME_PY_RUN
# include "PIL_time.h"
static int bpy_timer_count = 0;
static double bpy_timer; /* time since python starts */
static double bpy_timer_run; /* time for each python script run */
static double bpy_timer_run_tot; /* accumulate python runs */
/** Time since python starts. */
static double bpy_timer;
/** Time for each Python script run. */
static double bpy_timer_run;
/** Accumulate Python runs. */
static double bpy_timer_run_tot;
#endif
void BPY_context_update(bContext *C)
{
/* don't do this from a non-main (e.g. render) thread, it can cause a race
* condition on C->data.recursion. ideal solution would be to disable
* context entirely from non-main threads, but that's more complicated */
/* Don't do this from a non-main (e.g. render) thread, it can cause a race
* condition on `C->data.recursion`. Ideal solution would be to disable
* context entirely from non-main threads, but that's more complicated. */
if (!BLI_thread_is_main()) {
return;
}
BPY_context_set(C);
BPY_modules_update(); /* can give really bad results if this isn't here */
/* Can give really bad results if this isn't here. */
BPY_modules_update();
}
void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
@@ -120,7 +125,7 @@ void bpy_context_set(bContext *C, PyGILState_STATE *gilstate)
#ifdef TIME_PY_RUN
if (bpy_timer_count == 0) {
/* record time from the beginning */
/* Record time from the beginning. */
bpy_timer = PIL_check_seconds_timer();
bpy_timer_run = bpy_timer_run_tot = 0.0;
}
@@ -143,8 +148,8 @@ void bpy_context_clear(bContext * /*C*/, const PyGILState_STATE *gilstate)
fprintf(stderr, "ERROR: Python context internal state bug. this should not happen!\n");
}
else if (py_call_level == 0) {
/* XXX: Calling classes currently won't store the context :\,
* can't set nullptr because of this. but this is very flaky still. */
/* NOTE: Unfortunately calling classes currently won't store the context.
* Can't set nullptr because of this - but this is very flaky still. */
#if 0
BPY_context_set(nullptr);
#endif
@@ -219,13 +224,15 @@ void BPY_text_free_code(Text *text)
void BPY_modules_update()
{
#if 0 /* slow, this runs all the time poll, draw etc 100's of time a sec. */
/* Correct but slow, this runs all the time operator poll, panel draw etc
* (100's of time a second). */
#if 0
PyObject *mod = PyImport_ImportModuleLevel("bpy", nullptr, nullptr, nullptr, 0);
PyModule_AddObject(mod, "data", BPY_rna_module());
PyModule_AddObject(mod, "types", BPY_rna_types()); /* This does not need updating. */
#endif
/* refreshes the main struct */
/* Refreshes the main struct. */
BPY_update_rna_module();
}
@@ -240,17 +247,17 @@ void BPY_context_set(bContext *C)
}
#ifdef WITH_FLUID
/* defined in manta module */
/* Defined in `manta` module. */
extern "C" PyObject *Manta_initPython(void);
#endif
#ifdef WITH_AUDASPACE_PY
/* defined in AUD_C-API.cpp */
/* Defined in `AUD_C-API.cpp`. */
extern "C" PyObject *AUD_initPython(void);
#endif
#ifdef WITH_CYCLES
/* defined in cycles module */
/* Defined in `cycles` module. */
static PyObject *CCL_initPython()
{
return (PyObject *)CCL_python_module_init();
@@ -258,7 +265,7 @@ static PyObject *CCL_initPython()
#endif
#ifdef WITH_HYDRA
/* defined in render_hydra module */
/* Defined in `render_hydra` module. */
PyObject *BPyInit_hydra();
#endif
@@ -499,7 +506,7 @@ void BPY_python_start(bContext *C, int argc, const char **argv)
PyErr_Print();
PyErr_Clear();
}
// Py_DECREF(mod); /* ideally would decref, but in this case we never want to free */
// Py_DECREF(mod); /* Ideally would decref, but in this case we never want to free. */
}
}
#endif
@@ -513,8 +520,8 @@ void BPY_python_start(bContext *C, int argc, const char **argv)
pyrna_alloc_types();
#ifndef WITH_PYTHON_MODULE
/* py module runs atexit when bpy is freed */
BPY_atexit_register(); /* this can init any time */
/* Python module runs `atexit` when `bpy` is freed. */
BPY_atexit_register(); /* This can initialize any time. */
/* Free the lock acquired (implicitly) when Python is initialized. */
PyEval_ReleaseThread(PyGILState_GetThisThreadState());
@@ -530,13 +537,12 @@ void BPY_python_start(bContext *C, int argc, const char **argv)
void BPY_python_end()
{
// fprintf(stderr, "Ending Python!\n");
PyGILState_STATE gilstate;
/* finalizing, no need to grab the state, except when we are a module */
/* Finalizing, no need to grab the state, except when we are a module. */
gilstate = PyGILState_Ensure();
/* Frees the python-driver name-space & cached data. */
/* Frees the Python-driver name-space & cached data. */
BPY_driver_exit();
/* Clear Python values in the context so freeing the context after Python exits doesn't crash. */
@@ -545,20 +551,21 @@ void BPY_python_end()
/* Decrement user counts of all callback functions. */
BPY_rna_props_clear_all();
/* free other python data. */
/* Free other Python data. */
pyrna_free_types();
BPY_rna_exit();
/* clear all python data from structs */
/* Clear all Python data from structs. */
bpy_intern_string_exit();
/* bpy.app modules that need cleanup */
/* `bpy.app` modules that need cleanup. */
BPY_app_translations_end();
#ifndef WITH_PYTHON_MODULE
BPY_atexit_unregister(); /* without this we get recursive calls to WM_exit */
/* Without this we get recursive calls to #WM_exit. */
BPY_atexit_unregister();
Py_Finalize();
@@ -568,7 +575,7 @@ void BPY_python_end()
#endif
#ifdef TIME_PY_RUN
/* measure time since py started */
/* Measure time since Python started. */
bpy_timer = PIL_check_seconds_timer() - bpy_timer;
printf("*bpy stats* - ");
@@ -583,15 +590,12 @@ void BPY_python_end()
}
printf("\n");
// fprintf(stderr, "Ending Python Done!\n");
#endif
}
void BPY_python_reset(bContext *C)
{
/* unrelated security stuff */
/* Unrelated security stuff. */
G.f &= ~(G_FLAG_SCRIPT_AUTOEXEC_FAIL | G_FLAG_SCRIPT_AUTOEXEC_FAIL_QUIET);
G.autoexec_fail[0] = '\0';
@@ -614,8 +618,8 @@ void BPY_python_backtrace(FILE *fp)
if (!_PyThreadState_UncheckedGet()) {
return;
}
PyFrameObject *frame;
if (!(frame = PyEval_GetFrame())) {
PyFrameObject *frame = PyEval_GetFrame();
if (frame == nullptr) {
return;
}
do {
@@ -651,13 +655,12 @@ void BPY_modules_load_user(bContext *C)
Main *bmain = CTX_data_main(C);
Text *text;
/* can happen on file load */
/* Can happen on file load. */
if (bmain == nullptr) {
return;
}
/* update pointers since this can run from a nested script
* on file load */
/* Update pointers since this can run from a nested script on file load. */
if (py_call_level) {
BPY_context_update(C);
}
@@ -709,7 +712,7 @@ int BPY_context_member_get(bContext *C, const char *member, bContextDataResult *
item = PyDict_GetItemString(pyctx, member);
if (item == nullptr) {
/* pass */
/* Pass. */
}
else if (item == Py_None) {
done = true;
@@ -806,16 +809,17 @@ struct dealloc_obj {
PyObject *mod;
};
/* call once __file__ is set */
/* Call once `__file__` is set. */
static void bpy_module_delay_init(PyObject *bpy_proxy)
{
const int argc = 1;
const char *argv[2];
/* updating the module dict below will lose the reference to __file__ */
/* Updating the module dict below will lose the reference to `__file__`. */
PyObject *filepath_obj = PyModule_GetFilenameObject(bpy_proxy);
const char *filepath_rel = PyUnicode_AsUTF8(filepath_obj); /* can be relative */
/* The file can be a relative path. */
const char *filepath_rel = PyUnicode_AsUTF8(filepath_obj);
char filepath_abs[1024];
STRNCPY(filepath_abs, filepath_rel);
@@ -825,11 +829,9 @@ static void bpy_module_delay_init(PyObject *bpy_proxy)
argv[0] = filepath_abs;
argv[1] = nullptr;
// printf("module found %s\n", argv[0]);
main_python_enter(argc, argv);
/* initialized in BPy_init_modules() */
/* Initialized in #BPy_init_modules(). */
PyDict_Update(PyModule_GetDict(bpy_proxy), PyModule_GetDict(bpy_package_py));
}
@@ -882,12 +884,13 @@ static void dealloc_obj_dealloc(PyObject *self);
static PyTypeObject dealloc_obj_Type;
/* use our own dealloc so we can free a property if we use one */
/* Use our own `dealloc` so we can free a property if we use one. */
static void dealloc_obj_dealloc(PyObject *self)
{
bpy_module_delay_init(((dealloc_obj *)self)->mod);
/* NOTE: for subclassed PyObjects we can't just call PyObject_DEL() directly or it will crash. */
/* NOTE: for sub-classed `PyObject` objects
* we can't call #PyObject_DEL() directly or it will crash. */
dealloc_obj_Type.tp_free(self);
}
@@ -902,23 +905,23 @@ PyMODINIT_FUNC PyInit_bpy()
PyObject *bpy_proxy = PyModule_Create(&bpy_proxy_def);
/* Problem:
* 1) this init function is expected to have a private member defined - `md_def`
* but this is only set for C defined modules (not py packages)
* so we can't return 'bpy_package_py' as is.
* 1) This initializing function is expected to have a private member defined - `md_def`
* but this is only set for CAPI defined modules (not Python packages)
* so we can't return `bpy_package_py` as is.
*
* 2) there is a 'bpy' C module for python to load which is basically all of blender,
* 2) There is a `bpy` CAPI module for python to load which is basically all of blender,
* and there is `scripts/bpy/__init__.py`,
* we may end up having to rename this module so there is no naming conflict here eg:
* 'from blender import bpy'
* `from blender import bpy`
*
* 3) we don't know the filepath at this point, workaround by assigning a dummy value
* 3) We don't know the file-path at this point, workaround by assigning a dummy value
* which calls back when its freed so the real loading can take place.
*/
/* assign an object which is freed after __file__ is assigned */
/* Assign an object which is freed after `__file__` is assigned. */
dealloc_obj *dob;
/* assign dummy type */
/* Assign dummy type. */
dealloc_obj_Type.tp_name = "dealloc_obj";
dealloc_obj_Type.tp_basicsize = sizeof(dealloc_obj);
dealloc_obj_Type.tp_dealloc = dealloc_obj_dealloc;
@@ -944,9 +947,7 @@ static void bpy_module_free(void * /*mod*/)
bool BPY_string_is_keyword(const char *str)
{
/* list is from...
* ", ".join(['"%s"' % kw for kw in __import__("keyword").kwlist])
*/
/* List is from: `", ".join(['"%s"' % kw for kw in __import__("keyword").kwlist])`. */
const char *kwlist[] = {
"False", "None", "True", "and", "as", "assert", "async", "await", "break",
"class", "continue", "def", "del", "elif", "else", "except", "finally", "for",