@@ -6351,7 +6351,61 @@ static PyTypeObject PyDateTime_DateTimeType = {
63516351 * Module methods and initialization.
63526352 */
63536353
6354+ static PyObject *
6355+ _check_invalid_datetime_specs (PyObject * module , PyObject * args )
6356+ {
6357+ PyObject * format = NULL , * specs = NULL , * message = NULL ;
6358+
6359+ if (!PyArg_ParseTuple (args , "OOO" , & format , & specs , & message )) {
6360+ return NULL ;
6361+ }
6362+
6363+ PyObject * found_invalid_specs = PyList_New (0 );
6364+ if (found_invalid_specs == NULL ) {
6365+ return NULL ;
6366+ }
6367+
6368+ for (Py_ssize_t pos = 0 ; pos < PyTuple_GET_SIZE (specs ); pos ++ ) {
6369+ PyObject * spec = PyTuple_GET_ITEM (specs , pos );
6370+ if (PySequence_Contains (format , spec )) {
6371+ PyList_Append (found_invalid_specs , spec );
6372+ }
6373+ }
6374+
6375+ Py_ssize_t specs_length = PyList_GET_SIZE (found_invalid_specs );
6376+ if (specs_length ) {
6377+ PyObject * suffix = PyUnicode_FromString (specs_length > 1 ? "are" : "is" );
6378+ PyObject * separator = PyUnicode_FromString (", " );
6379+ PyObject * left_part = PyUnicode_Join (separator , found_invalid_specs );
6380+
6381+ PyObject * error_message = PyObject_CallMethod (message ,
6382+ "format" , "OO" ,
6383+ left_part , suffix , NULL );
6384+ Py_DECREF (suffix );
6385+ Py_DECREF (left_part );
6386+ Py_DECREF (separator );
6387+ Py_DECREF (found_invalid_specs );
6388+ PyErr_SetObject (PyExc_ValueError , error_message );
6389+ Py_DECREF (error_message );
6390+ return NULL ;
6391+ }
6392+
6393+ Py_DECREF (found_invalid_specs );
6394+ return Py_INCREF (Py_True ), Py_True ;
6395+ }
6396+
6397+ PyDoc_STRVAR (
6398+ _check_invalid_datetime_specs__doc__ ,
6399+ "_check_invalid_datetime_specs(format, specs, message)"
6400+ );
6401+
63546402static PyMethodDef module_methods [] = {
6403+ {
6404+ "_check_invalid_datetime_specs" ,
6405+ (PyCFunction ) _check_invalid_datetime_specs ,
6406+ METH_VARARGS ,
6407+ _check_invalid_datetime_specs__doc__
6408+ },
63556409 {NULL , NULL }
63566410};
63576411
0 commit comments