@@ -12,6 +12,7 @@ MODULE_NAME " provides basic warning filtering support.\n"
1212_Py_IDENTIFIER (argv );
1313_Py_IDENTIFIER (stderr );
1414#ifndef Py_DEBUG
15+ _Py_IDENTIFIER (default );
1516_Py_IDENTIFIER (ignore );
1617#endif
1718
@@ -22,8 +23,20 @@ check_matched(PyObject *obj, PyObject *arg)
2223 _Py_IDENTIFIER (match );
2324 int rc ;
2425
26+ /* A 'None' filter always matches */
2527 if (obj == Py_None )
2628 return 1 ;
29+
30+ /* An internal plain text default filter must match exactly */
31+ if (PyUnicode_CheckExact (obj )) {
32+ int cmp_result = PyUnicode_Compare (obj , arg );
33+ if (cmp_result == -1 && PyErr_Occurred ()) {
34+ return -1 ;
35+ }
36+ return !cmp_result ;
37+ }
38+
39+ /* Otherwise assume a regex filter and call its match() method */
2740 result = _PyObject_CallMethodIdObjArgs (obj , & PyId_match , arg , NULL );
2841 if (result == NULL )
2942 return -1 ;
@@ -1158,16 +1171,27 @@ static PyMethodDef warnings_functions[] = {
11581171
11591172#ifndef Py_DEBUG
11601173static PyObject *
1161- create_filter (PyObject * category , _Py_Identifier * id )
1174+ create_filter (PyObject * category , _Py_Identifier * id , const char * modname )
11621175{
1176+ PyObject * modname_obj = NULL ;
11631177 PyObject * action_str = _PyUnicode_FromId (id );
11641178 if (action_str == NULL ) {
11651179 return NULL ;
11661180 }
11671181
1182+ /* Default to "no module name" for initial filter set */
1183+ if (modname != NULL ) {
1184+ modname_obj = PyUnicode_InternFromString (modname );
1185+ if (modname_obj == NULL ) {
1186+ return NULL ;
1187+ }
1188+ } else {
1189+ modname_obj = Py_None ;
1190+ }
1191+
11681192 /* This assumes the line number is zero for now. */
11691193 return PyTuple_Pack (5 , action_str , Py_None ,
1170- category , Py_None , _PyLong_Zero );
1194+ category , modname_obj , _PyLong_Zero );
11711195}
11721196#endif
11731197
@@ -1180,20 +1204,22 @@ init_filters(void)
11801204 return PyList_New (0 );
11811205#else
11821206 /* Other builds ignore a number of warning categories by default */
1183- PyObject * filters = PyList_New (4 );
1207+ PyObject * filters = PyList_New (5 );
11841208 if (filters == NULL ) {
11851209 return NULL ;
11861210 }
11871211
11881212 size_t pos = 0 ; /* Post-incremented in each use. */
11891213 PyList_SET_ITEM (filters , pos ++ ,
1190- create_filter (PyExc_DeprecationWarning , & PyId_ignore ));
1214+ create_filter (PyExc_DeprecationWarning , & PyId_default , "__main__" ));
1215+ PyList_SET_ITEM (filters , pos ++ ,
1216+ create_filter (PyExc_DeprecationWarning , & PyId_ignore , NULL ));
11911217 PyList_SET_ITEM (filters , pos ++ ,
1192- create_filter (PyExc_PendingDeprecationWarning , & PyId_ignore ));
1218+ create_filter (PyExc_PendingDeprecationWarning , & PyId_ignore , NULL ));
11931219 PyList_SET_ITEM (filters , pos ++ ,
1194- create_filter (PyExc_ImportWarning , & PyId_ignore ));
1220+ create_filter (PyExc_ImportWarning , & PyId_ignore , NULL ));
11951221 PyList_SET_ITEM (filters , pos ++ ,
1196- create_filter (PyExc_ResourceWarning , & PyId_ignore ));
1222+ create_filter (PyExc_ResourceWarning , & PyId_ignore , NULL ));
11971223
11981224 for (size_t x = 0 ; x < pos ; x ++ ) {
11991225 if (PyList_GET_ITEM (filters , x ) == NULL ) {
0 commit comments