Skip to content

Commit

Permalink
Clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
serhiy-storchaka committed Nov 16, 2024
1 parent 41e60ff commit c76a917
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
5 changes: 3 additions & 2 deletions Lib/test/test_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -2686,13 +2686,14 @@ class Interrupt(Exception):
pass
p = re.compile(pattern)
for n in range(maxcount):
p._fail_after(n, Interrupt)
try:
p._fail_after(n, Interrupt)
p.match(string)
print(n)
return n
except Interrupt:
pass
finally:
p._fail_after(-1, None)

@unittest.skipUnless(hasattr(re.Pattern, '_fail_after'), 'requires debug build')
def test_memory_leaks(self):
Expand Down
52 changes: 21 additions & 31 deletions Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -581,11 +581,6 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
state->must_advance = 0;
state->debug = ((pattern->flags & SRE_FLAG_DEBUG) != 0);

/* SRE_REPEAT pool */
state->repeat = NULL;
state->repeat_pool_used = NULL;
state->repeat_pool_unused = NULL;

state->beginning = ptr;

state->start = (void*) ((char*) ptr + start * state->charsize);
Expand Down Expand Up @@ -613,7 +608,7 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
}

LOCAL(void)
state_fini(SRE_STATE* state, PatternObject *pattern)
state_fini(SRE_STATE* state)
{
if (state->buffer.buf)
PyBuffer_Release(&state->buffer);
Expand All @@ -624,11 +619,6 @@ state_fini(SRE_STATE* state, PatternObject *pattern)
state->mark = NULL;
/* SRE_REPEAT pool */
repeat_pool_clear(state);
#ifdef Py_DEBUG
if (pattern) {
pattern->fail_after_count = -1;
}
#endif
}

/* calculate offset from start of string */
Expand Down Expand Up @@ -804,12 +794,12 @@ _sre_SRE_Pattern_match_impl(PatternObject *self, PyTypeObject *cls,

TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr));
if (PyErr_Occurred()) {
state_fini(&state, self);
state_fini(&state);
return NULL;
}

match = pattern_new_match(module_state, self, &state, status);
state_fini(&state, self);
state_fini(&state);
return match;
}

Expand Down Expand Up @@ -849,12 +839,12 @@ _sre_SRE_Pattern_fullmatch_impl(PatternObject *self, PyTypeObject *cls,

TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr));
if (PyErr_Occurred()) {
state_fini(&state, self);
state_fini(&state);
return NULL;
}

match = pattern_new_match(module_state, self, &state, status);
state_fini(&state, self);
state_fini(&state);
return match;
}

Expand Down Expand Up @@ -894,12 +884,12 @@ _sre_SRE_Pattern_search_impl(PatternObject *self, PyTypeObject *cls,
TRACE(("|%p|%p|END\n", PatternObject_GetCode(self), state.ptr));

if (PyErr_Occurred()) {
state_fini(&state, self);
state_fini(&state);
return NULL;
}

match = pattern_new_match(module_state, self, &state, status);
state_fini(&state, self);
state_fini(&state);
return match;
}

Expand Down Expand Up @@ -928,7 +918,7 @@ _sre_SRE_Pattern_findall_impl(PatternObject *self, PyObject *string,

list = PyList_New(0);
if (!list) {
state_fini(&state, self);
state_fini(&state);
return NULL;
}

Expand Down Expand Up @@ -990,12 +980,12 @@ _sre_SRE_Pattern_findall_impl(PatternObject *self, PyObject *string,
state.start = state.ptr;
}

state_fini(&state, self);
state_fini(&state);
return list;

error:
Py_DECREF(list);
state_fini(&state, self);
state_fini(&state);
return NULL;

}
Expand Down Expand Up @@ -1091,7 +1081,7 @@ _sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *string,

list = PyList_New(0);
if (!list) {
state_fini(&state, self);
state_fini(&state);
return NULL;
}

Expand Down Expand Up @@ -1155,12 +1145,12 @@ _sre_SRE_Pattern_split_impl(PatternObject *self, PyObject *string,
if (status < 0)
goto error;

state_fini(&state, self);
state_fini(&state);
return list;

error:
Py_DECREF(list);
state_fini(&state, self);
state_fini(&state);
return NULL;

}
Expand Down Expand Up @@ -1287,7 +1277,7 @@ pattern_subx(_sremodulestate* module_state,
list = PyList_New(0);
if (!list) {
Py_DECREF(filter);
state_fini(&state, self);
state_fini(&state);
return NULL;
}

Expand Down Expand Up @@ -1373,7 +1363,7 @@ pattern_subx(_sremodulestate* module_state,
goto error;
}

state_fini(&state, self);
state_fini(&state);

Py_DECREF(filter);

Expand Down Expand Up @@ -1405,7 +1395,7 @@ pattern_subx(_sremodulestate* module_state,

error:
Py_DECREF(list);
state_fini(&state, self);
state_fini(&state);
Py_DECREF(filter);
return NULL;

Expand Down Expand Up @@ -1634,7 +1624,6 @@ _sre_compile_impl(PyObject *module, PyObject *pattern, int flags,
#ifdef Py_DEBUG
self->fail_after_count = -1;
self->fail_after_exc = NULL;
self->fail_after_exc = Py_NewRef(PyExc_RuntimeError);
#endif

self->codesize = n;
Expand Down Expand Up @@ -2734,7 +2723,8 @@ pattern_new_match(_sremodulestate* module_state,
if (!match)
return NULL;

match->pattern = (PatternObject*)Py_NewRef(pattern);
Py_INCREF(pattern);
match->pattern = pattern;

match->string = Py_NewRef(state->string);

Expand Down Expand Up @@ -2810,7 +2800,7 @@ scanner_dealloc(ScannerObject* self)
PyTypeObject *tp = Py_TYPE(self);

PyObject_GC_UnTrack(self);
state_fini(&self->state, self->pattern);
state_fini(&self->state);
(void)scanner_clear(self);
tp->tp_free(self);
Py_DECREF(tp);
Expand Down Expand Up @@ -2870,7 +2860,7 @@ _sre_SRE_Scanner_match_impl(ScannerObject *self, PyTypeObject *cls)
return NULL;
}

match = pattern_new_match(module_state, (PatternObject*) self->pattern,
match = pattern_new_match(module_state, self->pattern,
state, status);

if (status == 0)
Expand Down Expand Up @@ -2920,7 +2910,7 @@ _sre_SRE_Scanner_search_impl(ScannerObject *self, PyTypeObject *cls)
return NULL;
}

match = pattern_new_match(module_state, (PatternObject*) self->pattern,
match = pattern_new_match(module_state, self->pattern,
state, status);

if (status == 0)
Expand Down
10 changes: 5 additions & 5 deletions Modules/_sre/sre_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -571,11 +571,11 @@ typedef struct {
# define MAYBE_CHECK_SIGNALS \
do { \
_MAYBE_CHECK_SIGNALS; \
if (state->fail_after_count == 0) { \
PyErr_SetNone(state->fail_after_exc); \
RETURN_ERROR(SRE_ERROR_INTERRUPTED); \
} \
if (state->fail_after_count > 0) { \
if (state->fail_after_count >= 0) { \
if (state->fail_after_count == 0) { \
PyErr_SetNone(state->fail_after_exc); \
RETURN_ERROR(SRE_ERROR_INTERRUPTED); \
} \
state->fail_after_count--; \
} \
} while (0)
Expand Down

0 comments on commit c76a917

Please sign in to comment.