@@ -880,7 +880,7 @@ def visitModule(self, mod):
880
880
881
881
Py_ssize_t i, numfields = 0;
882
882
int res = -1;
883
- PyObject *key, *value, *fields, *remaining_fields = NULL;
883
+ PyObject *key, *value, *fields, *attributes = NULL, * remaining_fields = NULL;
884
884
if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
885
885
goto cleanup;
886
886
}
@@ -947,22 +947,32 @@ def visitModule(self, mod):
947
947
goto cleanup;
948
948
}
949
949
}
950
- else if (
951
- PyUnicode_CompareWithASCIIString(key, "lineno") != 0 &&
952
- PyUnicode_CompareWithASCIIString(key, "col_offset") != 0 &&
953
- PyUnicode_CompareWithASCIIString(key, "end_lineno") != 0 &&
954
- PyUnicode_CompareWithASCIIString(key, "end_col_offset") != 0
955
- ) {
956
- if (PyErr_WarnFormat(
957
- PyExc_DeprecationWarning, 1,
958
- "%.400s.__init__ got an unexpected keyword argument '%U'. "
959
- "Support for arbitrary keyword arguments is deprecated "
960
- "and will be removed in Python 3.15.",
961
- Py_TYPE(self)->tp_name, key
962
- ) < 0) {
950
+ else {
951
+ // Lazily initialize "attributes"
952
+ if (attributes == NULL) {
953
+ attributes = PyObject_GetAttr((PyObject*)Py_TYPE(self), state->_attributes);
954
+ if (attributes == NULL) {
955
+ res = -1;
956
+ goto cleanup;
957
+ }
958
+ }
959
+ int contains = PySequence_Contains(attributes, key);
960
+ if (contains == -1) {
963
961
res = -1;
964
962
goto cleanup;
965
963
}
964
+ else if (contains == 0) {
965
+ if (PyErr_WarnFormat(
966
+ PyExc_DeprecationWarning, 1,
967
+ "%.400s.__init__ got an unexpected keyword argument '%U'. "
968
+ "Support for arbitrary keyword arguments is deprecated "
969
+ "and will be removed in Python 3.15.",
970
+ Py_TYPE(self)->tp_name, key
971
+ ) < 0) {
972
+ res = -1;
973
+ goto cleanup;
974
+ }
975
+ }
966
976
}
967
977
res = PyObject_SetAttr(self, key, value);
968
978
if (res < 0) {
@@ -1045,6 +1055,7 @@ def visitModule(self, mod):
1045
1055
Py_DECREF(field_types);
1046
1056
}
1047
1057
cleanup:
1058
+ Py_XDECREF(attributes);
1048
1059
Py_XDECREF(fields);
1049
1060
Py_XDECREF(remaining_fields);
1050
1061
return res;
0 commit comments