@@ -903,9 +903,10 @@ _very_bad_hash(const char *str)
903
903
{
904
904
// Rarely works, but for existing names no collisions!
905
905
unsigned int sum = 0 ;
906
+ int size = (int )strlen (str );
906
907
int off = 1 ;
907
908
908
- for (int i = 0 ; i < strlen ( str ) ; i ++ ) {
909
+ for (int i = 0 ; i < size ; i ++ ) {
909
910
sum += str [i ];
910
911
sum = sum ^ (sum >> off );
911
912
off += 1 ;
@@ -978,7 +979,9 @@ _pg_eventtype_from_name_hash(const char *name)
978
979
else:
979
980
text += f" case {hashes_db[line[0]]}" \
980
981
f": //{line[0]}\n " \
981
- f"return {line[1]};\n" return text
982
+ f"return {line[1]};\n"
983
+
984
+ return text
982
985
983
986
984
987
if __name__ == "__main__":
@@ -2058,8 +2061,9 @@ _register_user_event_class(PyObject *e_class)
2058
2061
e_typeo = PyLong_FromLong (_custom_event ++ );
2059
2062
}
2060
2063
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." );
2063
2067
return -1 ;
2064
2068
}
2065
2069
@@ -2134,10 +2138,14 @@ pgEventMeta_Call(PyObject *type, PyObject *args, PyObject *kwds)
2134
2138
Uint32 e_type = 0 ;
2135
2139
PyObject * e_dict = NULL ;
2136
2140
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 )) {
2138
2142
return NULL ;
2139
2143
}
2140
2144
2145
+ if (e_type < 0 || e_type >= PG_NUMEVENTS ) {
2146
+ return RAISE (PyExc_ValueError , "event type out of range" );
2147
+ }
2148
+
2141
2149
if (kwds && !e_dict ) {
2142
2150
e_dict = PyDict_New ();
2143
2151
if (!e_dict )
@@ -2164,10 +2172,9 @@ pgEventMeta_Call(PyObject *type, PyObject *args, PyObject *kwds)
2164
2172
// metaclasses to override pygame.event.Event(...) can be dropped in favor of
2165
2173
// tp_vectorcall.
2166
2174
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" ,
2168
2176
.tp_basicsize = 0 ,
2169
2177
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE ,
2170
- .tp_base = & PyType_Type ,
2171
2178
.tp_call = pgEventMeta_Call ,
2172
2179
};
2173
2180
@@ -3007,33 +3014,34 @@ pg_event__dir__(PyObject *self, PyObject *args)
3007
3014
}
3008
3015
3009
3016
PyObject * result = PyDict_Keys (dict );
3017
+ Py_ssize_t res_size = PyList_Size (result );
3010
3018
Py_DECREF (dict );
3011
3019
3012
3020
if (!result ) {
3013
3021
return NULL ;
3014
3022
}
3015
3023
3016
- PyObject * dir = PyList_New (PyList_Size ( result ) + length );
3024
+ PyObject * dir = PyList_New (res_size + length );
3017
3025
3018
3026
if (!dir ) {
3019
3027
Py_DECREF (result );
3020
3028
return NULL ;
3021
3029
}
3022
3030
3023
- for (Py_ssize_t idx = 0 ; idx < PyList_Size ( result ) ; idx ++ ) {
3031
+ for (Py_ssize_t idx = 0 ; idx < res_size ; idx ++ ) {
3024
3032
PyObject * item = PyList_GetItem (result , idx );
3025
3033
Py_INCREF (item );
3026
3034
PyList_SetItem (dir , idx , item );
3027
3035
}
3028
3036
Py_DECREF (result );
3029
3037
3030
- for (Py_ssize_t idx = 0 ; idx < length ; idx ++ ) {
3038
+ for (size_t idx = 0 ; idx < length ; idx ++ ) {
3031
3039
PyObject * item = PyUnicode_FromString (_event_names [idx ]);
3032
3040
if (!item ) {
3033
3041
Py_DECREF (dir );
3034
3042
return NULL ;
3035
3043
}
3036
- PyList_SetItem (dir , idx + PyList_Size ( result ) , item );
3044
+ PyList_SetItem (dir , idx + res_size , item );
3037
3045
}
3038
3046
3039
3047
return dir ;
@@ -3106,6 +3114,7 @@ MODINIT_DEFINE(event)
3106
3114
}
3107
3115
3108
3116
/* type preparation */
3117
+ pgEventMeta_Type .tp_base = & PyType_Type ;
3109
3118
if (PyType_Ready (& pgEventMeta_Type ) < 0 ) {
3110
3119
return NULL ;
3111
3120
}
0 commit comments