/* Py_REF_DEBUG also controls the display of refcounts and memory block
* allocations at the interactive prompt and at interpreter shutdown
*/
-PyAPI_FUNC(PyObject *) _PyDebug_XOptionShowRefCount(void);
PyAPI_FUNC(void) _PyDebug_PrintTotalRefs(void);
#else
#define _Py_INC_REFTOTAL
typedef struct {
- int ignore_environment;
- int use_hash_seed;
+ int ignore_environment; /* -E */
+ int use_hash_seed; /* PYTHONHASHSEED=x */
unsigned long hash_seed;
int _disable_importlib; /* Needed by freeze_importlib */
- char *allocator;
- int faulthandler;
- int tracemalloc; /* Number of saved frames, 0=don't trace */
- int importtime; /* -X importtime */
+ const char *allocator; /* Memory allocator: _PyMem_SetupAllocators() */
int dev_mode; /* -X dev */
+ int faulthandler; /* -X faulthandler */
+ int tracemalloc; /* -X tracemalloc=N */
+ int import_time; /* -X importtime */
+ int show_ref_count; /* -X showrefcount */
+ int show_alloc_count; /* -X showalloccount */
} _PyCoreConfig;
#define _PyCoreConfig_INIT \
.hash_seed = 0, \
._disable_importlib = 0, \
.allocator = NULL, \
+ .dev_mode = 0, \
.faulthandler = 0, \
.tracemalloc = 0, \
- .importtime = 0, \
- .dev_mode = 0}
+ .import_time = 0, \
+ .show_ref_count = 0, \
+ .show_alloc_count = 0}
/* Placeholders while working on the new configuration API
*
}
core_config->allocator = Py_GETENV("PYTHONMALLOC");
+ /* -X options */
+ if (pymain_get_xoption(pymain, L"showrefcount")) {
+ core_config->show_ref_count = 1;
+ }
+ if (pymain_get_xoption(pymain, L"showalloccount")) {
+ core_config->show_alloc_count = 1;
+ }
+
/* More complex options: env var and/or -X option */
if (pymain_get_env_var("PYTHONFAULTHANDLER")
|| pymain_get_xoption(pymain, L"faulthandler")) {
}
if (pymain_get_env_var("PYTHONPROFILEIMPORTTIME")
|| pymain_get_xoption(pymain, L"importtime")) {
- core_config->importtime = 1;
+ core_config->import_time = 1;
}
if (pymain_init_tracemalloc(pymain) < 0) {
return -1;
static void
show_alloc(void)
{
- PyObject *xoptions, *value;
- _Py_IDENTIFIER(showalloccount);
-
- xoptions = PySys_GetXOptions();
- if (xoptions == NULL)
- return;
- value = _PyDict_GetItemId(xoptions, &PyId_showalloccount);
- if (value != Py_True)
+ PyInterpreterState *interp = PyThreadState_GET()->interp;
+ if (!inter->core_config.show_alloc_count) {
return;
+ }
fprintf(stderr, "List allocations: %" PY_FORMAT_SIZE_T "d\n",
count_alloc);
return total;
}
-PyObject *
-_PyDebug_XOptionShowRefCount(void)
-{
- PyObject *xoptions = PySys_GetXOptions();
- if (xoptions == NULL)
- return NULL;
-
- _Py_IDENTIFIER(showrefcount);
- return _PyDict_GetItemId(xoptions, &PyId_showrefcount);
-}
-
void
_PyDebug_PrintTotalRefs(void) {
fprintf(stderr,
void
dump_counts(FILE* f)
{
- PyTypeObject *tp;
- PyObject *xoptions, *value;
- _Py_IDENTIFIER(showalloccount);
-
- xoptions = PySys_GetXOptions();
- if (xoptions == NULL)
- return;
- value = _PyDict_GetItemId(xoptions, &PyId_showalloccount);
- if (value != Py_True)
+ PyInterpreterState *interp = PyThreadState_GET()->interp;
+ if (!inter->core_config.show_alloc_count) {
return;
+ }
for (tp = type_list; tp; tp = tp->tp_next)
fprintf(f, "%s alloc'd: %" PY_FORMAT_SIZE_T "d, "
static void
show_track(void)
{
- PyObject *xoptions, *value;
- _Py_IDENTIFIER(showalloccount);
-
- xoptions = PySys_GetXOptions();
- if (xoptions == NULL)
- return;
- value = _PyDict_GetItemId(xoptions, &PyId_showalloccount);
- if (value != Py_True)
+ PyInterpreterState *interp = PyThreadState_GET()->interp;
+ if (!inter->core_config.show_alloc_count) {
return;
+ }
fprintf(stderr, "Tuples created: %" PY_FORMAT_SIZE_T "d\n",
count_tracked + count_untracked);
}
}
else {
- /* 1 -- true, 0 -- false, -1 -- not initialized */
- int importtime = interp->core_config.importtime;
+ int import_time = interp->core_config.import_time;
static int import_level;
static _PyTime_t accumulated;
* Anyway, importlib._find_and_load is much slower than
* _PyDict_GetItemIdWithError().
*/
- if (importtime) {
+ if (import_time) {
static int header = 1;
if (header) {
fputs("import time: self [us] | cumulative | imported package\n",
PyDTrace_IMPORT_FIND_LOAD_DONE(PyUnicode_AsUTF8(abs_name),
mod != NULL);
- if (importtime) {
+ if (import_time) {
_PyTime_t cum = _PyTime_GetPerfCounter() - t1;
import_level--;
/* nothing */;
#endif
-#ifdef Py_REF_DEBUG
- PyObject *showrefcount = _PyDebug_XOptionShowRefCount();
-#endif
-
/* Destroy all modules */
PyImport_Cleanup();
_PyHash_Fini();
#ifdef Py_REF_DEBUG
- if (showrefcount == Py_True)
- _PyDebug_PrintTotalRefs();
+ if (interp->core_config.show_ref_count) {
+ _PyDebug_PrintTotalRefs();
+ }
#endif
#ifdef Py_TRACE_REFS
int ret, err;
PyCompilerFlags local_flags;
int nomem_count = 0;
+#ifdef Py_REF_DEBUG
+ int show_ref_count = PyThreadState_GET()->interp->core_config.show_ref_count;
+#endif
filename = PyUnicode_DecodeFSDefault(filename_str);
if (filename == NULL) {
nomem_count = 0;
}
#ifdef Py_REF_DEBUG
- if (_PyDebug_XOptionShowRefCount() == Py_True)
+ if (show_ref_count) {
_PyDebug_PrintTotalRefs();
+ }
#endif
} while (ret != E_EOF);
Py_DECREF(filename);