Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bpo-28254: Return the previous GC state from the Enable/Disable funct…
…ions to make it easy for users to ('atomically') know whether they actually changed something and what state to go back to later. Also, returning an "int" may make it easier to add error handling later, if that becomes needed at some point.
  • Loading branch information
scoder committed Apr 28, 2021
commit 56465a9c1b8ab8b5c4d0b203abd0881db9187aff
4 changes: 2 additions & 2 deletions Include/objimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ PyAPI_FUNC(PyVarObject *) _PyObject_NewVar(PyTypeObject *, Py_ssize_t);
/* C equivalent of gc.collect() which ignores the state of gc.enabled. */
PyAPI_FUNC(Py_ssize_t) PyGC_Collect(void);
/* C API for controlling the state of the garbage collector */
PyAPI_FUNC(void) PyGC_Enable(void);
PyAPI_FUNC(void) PyGC_Disable(void);
PyAPI_FUNC(int) PyGC_Enable(void);
PyAPI_FUNC(int) PyGC_Disable(void);
PyAPI_FUNC(int) PyGC_IsEnabled(void);

/* Test if a type has a GC head */
Expand Down
27 changes: 19 additions & 8 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,31 @@ test_gc_control(PyObject *self, PyObject *Py_UNUSED(ignored))
{
int orig_enabled = PyGC_IsEnabled();
const char* msg = "ok";
int old_state;

PyGC_Enable();
msg = "enable(1)";
old_state = PyGC_Enable();
msg = "Enable(1)";
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bugs are unlikely. IMO using assert() is good enough for _testcapi tests. You can leave the code as it is ;-)

See for example test_refcount_funcs() which uses assert().

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking that assertions can be compiled out, which is decidedly not intended here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general yes, but _testcapimodule.c starts with:

/* Always enable assertions */
#undef NDEBUG

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know! I'd keep the test as it is though. It doesn't really hurt, I think.

if (old_state != orig_enabled) goto failed;
msg = "IsEnabled(1)";
if (!PyGC_IsEnabled()) goto failed;
Comment thread
pablogsal marked this conversation as resolved.
Outdated
PyGC_Disable();
msg = "disable(1)";

old_state = PyGC_Disable();
msg = "disable(2)";
if (!old_state) goto failed;
msg = "IsEnabled(2)";
if (PyGC_IsEnabled()) goto failed;
Comment thread
pablogsal marked this conversation as resolved.
Outdated
PyGC_Enable();
msg = "enable(2)";

old_state = PyGC_Enable();
msg = "enable(3)";
if (old_state) goto failed;
msg = "IsEnabled(3)";
if (!PyGC_IsEnabled()) goto failed;
Comment thread
pablogsal marked this conversation as resolved.
Outdated

if (!orig_enabled) {
PyGC_Disable();
msg = "disable(2)";
old_state = PyGC_Disable();
msg = "disable(4)";
if (old_state) goto failed;
msg = "IsEnabled(4)";
if (PyGC_IsEnabled()) goto failed;
}

Expand Down
8 changes: 6 additions & 2 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2079,18 +2079,22 @@ PyGC_Collect(void)
}

/* C API for controlling the state of the garbage collector */
void
int
PyGC_Enable(void)
{
GCState *gcstate = get_gc_state();
int old_state = gcstate->enabled;
gcstate->enabled = 1;
return old_state;
}

void
int
PyGC_Disable(void)
{
GCState *gcstate = get_gc_state();
int old_state = gcstate->enabled;
gcstate->enabled = 0;
return old_state;
}

int
Expand Down