Skip to content

Commit de095ab

Browse files
committed
intern "lower" for str.lower calls
1 parent 6ff6041 commit de095ab

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Modules/hmacmodule.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ typedef struct py_hmac_hinfo {
139139
typedef struct hmacmodule_state {
140140
_Py_hashtable_t *hinfo_table;
141141
PyObject *unknown_hash_error;
142+
/* interned strings */
143+
PyObject *str_lower;
142144
} hmacmodule_state;
143145

144146
static inline hmacmodule_state *
@@ -347,6 +349,21 @@ hmacmodule_init_exceptions(PyObject *module, hmacmodule_state *state)
347349
return 0;
348350
}
349351

352+
static int
353+
hmacmodule_init_strings(hmacmodule_state *state)
354+
{
355+
#define ADD_STR(ATTR, STRING) \
356+
do { \
357+
state->ATTR = PyUnicode_FromString(STRING); \
358+
if (state->ATTR == NULL) { \
359+
return -1; \
360+
} \
361+
} while (0)
362+
ADD_STR(str_lower, "lower");
363+
#undef ADD_STR
364+
return 0;
365+
}
366+
350367
static int
351368
hmacmodule_exec(PyObject *module)
352369
{
@@ -357,6 +374,9 @@ hmacmodule_exec(PyObject *module)
357374
if (hmacmodule_init_exceptions(module, state) < 0) {
358375
return -1;
359376
}
377+
if (hmacmodule_init_strings(state) < 0) {
378+
return -1;
379+
}
360380
return 0;
361381
}
362382

@@ -366,6 +386,7 @@ hmacmodule_traverse(PyObject *mod, visitproc visit, void *arg)
366386
Py_VISIT(Py_TYPE(mod));
367387
hmacmodule_state *state = get_hmacmodule_state(mod);
368388
Py_VISIT(state->unknown_hash_error);
389+
Py_VISIT(state->str_lower);
369390
return 0;
370391
}
371392

@@ -378,6 +399,7 @@ hmacmodule_clear(PyObject *mod)
378399
state->hinfo_table = NULL;
379400
}
380401
Py_CLEAR(state->unknown_hash_error);
402+
Py_CLEAR(state->str_lower);
381403
return 0;
382404
}
383405

0 commit comments

Comments
 (0)