Skip to content

Commit 1d671de

Browse files
committed
Some fixes.
1 parent b0654aa commit 1d671de

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

src_c/event.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -903,9 +903,10 @@ _very_bad_hash(const char *str)
903903
{
904904
// Rarely works, but for existing names no collisions!
905905
unsigned int sum = 0;
906+
int size = (int)strlen(str);
906907
int off = 1;
907908

908-
for (int i = 0; i < strlen(str); i++) {
909+
for (int i = 0; i < size; i++) {
909910
sum += str[i];
910911
sum = sum ^ (sum >> off);
911912
off += 1;
@@ -978,7 +979,9 @@ _pg_eventtype_from_name_hash(const char *name)
978979
else:
979980
text += f" case {hashes_db[line[0]]}" \
980981
f": //{line[0]}\n " \
981-
f"return {line[1]};\n" return text
982+
f"return {line[1]};\n"
983+
984+
return text
982985
983986
984987
if __name__ == "__main__":
@@ -2058,8 +2061,9 @@ _register_user_event_class(PyObject *e_class)
20582061
e_typeo = PyLong_FromLong(_custom_event++);
20592062
}
20602063
else {
2061-
RAISE(pgExc_SDLError,
2062-
"Exceeded maximimum number of allowed user-defined types.");
2064+
PyErr_SetString(
2065+
pgExc_SDLError,
2066+
"Exceeded maximimum number of allowed user-defined types.");
20632067
return -1;
20642068
}
20652069

@@ -2134,10 +2138,14 @@ pgEventMeta_Call(PyObject *type, PyObject *args, PyObject *kwds)
21342138
Uint32 e_type = 0;
21352139
PyObject *e_dict = NULL;
21362140

2137-
if (!PyArg_ParseTuple(args, "I|O!", &e_type, &PyDict_Type, &e_dict)) {
2141+
if (!PyArg_ParseTuple(args, "i|O!", &e_type, &PyDict_Type, &e_dict)) {
21382142
return NULL;
21392143
}
21402144

2145+
if (e_type < 0 || e_type >= PG_NUMEVENTS) {
2146+
return RAISE(PyExc_ValueError, "event type out of range");
2147+
}
2148+
21412149
if (kwds && !e_dict) {
21422150
e_dict = PyDict_New();
21432151
if (!e_dict)
@@ -2164,10 +2172,9 @@ pgEventMeta_Call(PyObject *type, PyObject *args, PyObject *kwds)
21642172
// metaclasses to override pygame.event.Event(...) can be dropped in favor of
21652173
// tp_vectorcall.
21662174
static PyTypeObject pgEventMeta_Type = {
2167-
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "pygame.event._EventMeta",
2175+
PyVarObject_HEAD_INIT(&PyType_Type, 0).tp_name = "pygame.event._EventMeta",
21682176
.tp_basicsize = 0,
21692177
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
2170-
.tp_base = &PyType_Type,
21712178
.tp_call = pgEventMeta_Call,
21722179
};
21732180

@@ -3007,33 +3014,34 @@ pg_event__dir__(PyObject *self, PyObject *args)
30073014
}
30083015

30093016
PyObject *result = PyDict_Keys(dict);
3017+
Py_ssize_t res_size = PyList_Size(result);
30103018
Py_DECREF(dict);
30113019

30123020
if (!result) {
30133021
return NULL;
30143022
}
30153023

3016-
PyObject *dir = PyList_New(PyList_Size(result) + length);
3024+
PyObject *dir = PyList_New(res_size + length);
30173025

30183026
if (!dir) {
30193027
Py_DECREF(result);
30203028
return NULL;
30213029
}
30223030

3023-
for (Py_ssize_t idx = 0; idx < PyList_Size(result); idx++) {
3031+
for (Py_ssize_t idx = 0; idx < res_size; idx++) {
30243032
PyObject *item = PyList_GetItem(result, idx);
30253033
Py_INCREF(item);
30263034
PyList_SetItem(dir, idx, item);
30273035
}
30283036
Py_DECREF(result);
30293037

3030-
for (Py_ssize_t idx = 0; idx < length; idx++) {
3038+
for (size_t idx = 0; idx < length; idx++) {
30313039
PyObject *item = PyUnicode_FromString(_event_names[idx]);
30323040
if (!item) {
30333041
Py_DECREF(dir);
30343042
return NULL;
30353043
}
3036-
PyList_SetItem(dir, idx + PyList_Size(result), item);
3044+
PyList_SetItem(dir, idx + res_size, item);
30373045
}
30383046

30393047
return dir;
@@ -3106,6 +3114,7 @@ MODINIT_DEFINE(event)
31063114
}
31073115

31083116
/* type preparation */
3117+
pgEventMeta_Type.tp_base = &PyType_Type;
31093118
if (PyType_Ready(&pgEventMeta_Type) < 0) {
31103119
return NULL;
31113120
}

test/midi_test.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,7 @@ def test_midis2events(self):
398398
midi_event = midi_events[i]
399399
midi_event_data = midi_event[MIDI_DATA]
400400

401-
# Can't directly check event instance as pygame.event.Event is
402-
# a function.
403-
# self.assertIsInstance(pg_event, pygame.event.Event)
404-
self.assertEqual(pg_event.__class__.__name__, "Event")
401+
self.assertIsInstance(pg_event, pygame.Event)
405402
self.assertEqual(pg_event.type, pygame.MIDIIN)
406403
self.assertEqual(pg_event.status, midi_event_data[MD_STATUS])
407404
self.assertEqual(pg_event.data1, midi_event_data[MD_DATA1])

0 commit comments

Comments
 (0)