@@ -1045,6 +1045,54 @@ test_with_docstring(PyObject *self)
10451045 Py_RETURN_NONE ;
10461046}
10471047
1048+ /* Test PyOS_string_to_double. */
1049+ static PyObject *
1050+ test_string_to_double (PyObject * self ) {
1051+ double result ;
1052+ char * msg ;
1053+
1054+ #define CHECK_STRING (STR , expected ) \
1055+ result = PyOS_string_to_double(STR, NULL, NULL); \
1056+ if (result == -1.0 && PyErr_Occurred()) \
1057+ return NULL; \
1058+ if (result != expected) { \
1059+ msg = "conversion of " STR " to float failed"; \
1060+ goto fail; \
1061+ }
1062+
1063+ #define CHECK_INVALID (STR ) \
1064+ result = PyOS_string_to_double(STR, NULL, NULL); \
1065+ if (result == -1.0 && PyErr_Occurred()) { \
1066+ if (PyErr_ExceptionMatches(PyExc_ValueError)) \
1067+ PyErr_Clear(); \
1068+ else \
1069+ return NULL; \
1070+ } \
1071+ else { \
1072+ msg = "conversion of " STR " didn't raise ValueError"; \
1073+ goto fail; \
1074+ }
1075+
1076+ CHECK_STRING ("0.1" , 0.1 );
1077+ CHECK_STRING ("1.234" , 1.234 );
1078+ CHECK_STRING ("-1.35" , -1.35 );
1079+ CHECK_STRING (".1e01" , 1.0 );
1080+ CHECK_STRING ("2.e-2" , 0.02 );
1081+
1082+ CHECK_INVALID (" 0.1" );
1083+ CHECK_INVALID ("\t\n-3" );
1084+ CHECK_INVALID (".123 " );
1085+ CHECK_INVALID ("3\n" );
1086+ CHECK_INVALID ("123abc" );
1087+
1088+ Py_RETURN_NONE ;
1089+ fail :
1090+ return raiseTestError ("test_string_to_double" , msg );
1091+ #undef CHECK_STRING
1092+ #undef CHECK_INVALID
1093+ }
1094+
1095+
10481096#ifdef HAVE_GETTIMEOFDAY
10491097/* Profiling of integer performance */
10501098static void print_delta (int test , struct timeval * s , struct timeval * e )
@@ -1223,6 +1271,7 @@ static PyMethodDef TestMethods[] = {
12231271 {"test_empty_argparse" , (PyCFunction )test_empty_argparse ,METH_NOARGS },
12241272 {"test_null_strings" , (PyCFunction )test_null_strings , METH_NOARGS },
12251273 {"test_string_from_format" , (PyCFunction )test_string_from_format , METH_NOARGS },
1274+ {"test_string_to_double" , (PyCFunction )test_string_to_double , METH_NOARGS },
12261275 {"test_with_docstring" , (PyCFunction )test_with_docstring , METH_NOARGS ,
12271276 PyDoc_STR ("This is a pretty normal docstring." )},
12281277
0 commit comments