@@ -2088,28 +2088,85 @@ pg_EnvShouldBlendAlphaSDL2(void)
2088
2088
return pg_env_blend_alpha_SDL2 ;
2089
2089
}
2090
2090
2091
+ static inline int
2092
+ intSizeAsStr (int num )
2093
+ {
2094
+ if (num == 0 ) {
2095
+ // printf("i s=1, zero");
2096
+ return 1 ;
2097
+ }
2098
+ else if (num < 0 ) {
2099
+ // printf("i s=%i, neg", ceil(log10(-num + 1)) + 1);
2100
+ return ceil (log10 (- num + 1 )) + 1 ;
2101
+ }
2102
+ return ceil (log10 (num + 1 ));
2103
+ }
2104
+
2091
2105
static int
2092
2106
pgWarn (PyObject * category , const char * message , Py_ssize_t stack_level ,
2093
2107
int urgency )
2094
2108
{
2095
2109
if (pg_warn_filter < urgency )
2096
2110
return 0 ;
2097
- return PyErr_WarnEx (category , message , stack_level );
2111
+
2112
+ const char * extra ;
2113
+
2114
+ switch (urgency ) {
2115
+ case 0 :
2116
+ extra = "urgent: " ;
2117
+ break ;
2118
+ case 1 :
2119
+ extra = "mild: " ;
2120
+ break ;
2121
+ case 2 :
2122
+ extra = "note: " ;
2123
+ break ;
2124
+ default :
2125
+ extra = "" ;
2126
+ }
2127
+
2128
+ // 12 3 4
2129
+ // "message (extra{urgency})\0"
2130
+ printf ("%s (%s%i)\n" , message , extra , urgency );
2131
+ printf ("%lu, %lu, %i\n" , strlen (message ), strlen (extra ),
2132
+ intSizeAsStr (urgency ));
2133
+ size_t str_size =
2134
+ strlen (message ) + strlen (extra ) + intSizeAsStr (urgency ) + 4 ;
2135
+
2136
+ char * formatted = malloc (str_size );
2137
+ if (formatted == NULL ) {
2138
+ PyErr_SetString (PyExc_MemoryError ,
2139
+ "cannot allocate memory to format warning message" );
2140
+ return -1 ;
2141
+ }
2142
+
2143
+ PyOS_snprintf (formatted , str_size , "%s (%s%i)" , message , extra , urgency );
2144
+
2145
+ int ret = PyErr_WarnEx (category , formatted , stack_level );
2146
+ free (formatted );
2147
+ return ret ;
2098
2148
}
2099
2149
2100
2150
static PyObject *
2101
- pg_warn (PyObject * self , PyObject * args )
2151
+ pg_warn (PyObject * self , PyObject * args , PyObject * kwargs )
2102
2152
{
2103
- PyObject * category ;
2153
+ PyObject * category = NULL ;
2104
2154
const char * message ;
2105
- Py_ssize_t stack_level ;
2155
+ Py_ssize_t stack_level = -1 ;
2106
2156
int urgency ;
2157
+ char * kw_names [] = {"message" , "urgency" , "level" , "category" , NULL };
2107
2158
2108
- if (!PyArg_ParseTuple (args , "sOni " , & message , & category , & stack_level ,
2109
- & urgency ))
2159
+ if (!PyArg_ParseTupleAndKeywords (args , kwargs , "si|nO " , kw_names , & message ,
2160
+ & urgency , & stack_level , & category ))
2110
2161
return NULL ;
2111
2162
2112
- if (pgWarn (category , message , stack_level , urgency ) < 0 )
2163
+ if (category == NULL )
2164
+ category = PyExc_UserWarning ;
2165
+
2166
+ if (stack_level < 0 )
2167
+ stack_level = 1 ;
2168
+
2169
+ if (pgWarn (category , message , stack_level + 1 , urgency ) < 0 )
2113
2170
return NULL ;
2114
2171
2115
2172
Py_RETURN_NONE ;
@@ -2246,7 +2303,7 @@ static PyMethodDef _base_methods[] = {
2246
2303
2247
2304
{"get_array_interface" , (PyCFunction )pg_get_array_interface , METH_O ,
2248
2305
"return an array struct interface as an interface dictionary" },
2249
- {"warn" , (PyCFunction )pg_warn , METH_VARARGS ,
2306
+ {"warn" , (PyCFunction )pg_warn , METH_VARARGS | METH_KEYWORDS ,
2250
2307
"throw a warning with a given severity which is only used for filtering" },
2251
2308
{"get_warnings_filter" , (PyCFunction )pg_get_warnings_filter , METH_NOARGS ,
2252
2309
"get value of a warning filter by severity (smaller number - more "
0 commit comments