gh-108777: Split _PyTime tests from _testinternalcapi.c (gh-108787)

This commit is contained in:
Sam Gross 2023-09-02 00:46:36 +00:00 committed by GitHub
parent 76ce537fb1
commit aa52888e6a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 313 additions and 262 deletions

View file

@ -2800,6 +2800,7 @@ MODULE__SHA3_DEPS=$(srcdir)/Modules/hashlib.h $(LIBHACL_HEADERS) Modules/_hacl/H
MODULE__SOCKET_DEPS=$(srcdir)/Modules/socketmodule.h $(srcdir)/Modules/addrinfo.h $(srcdir)/Modules/getaddrinfo.c $(srcdir)/Modules/getnameinfo.c
MODULE__SSL_DEPS=$(srcdir)/Modules/_ssl.h $(srcdir)/Modules/_ssl/cert.c $(srcdir)/Modules/_ssl/debughelpers.c $(srcdir)/Modules/_ssl/misc.c $(srcdir)/Modules/_ssl_data.h $(srcdir)/Modules/_ssl_data_111.h $(srcdir)/Modules/_ssl_data_300.h $(srcdir)/Modules/socketmodule.h
MODULE__TESTCAPI_DEPS=$(srcdir)/Modules/_testcapi/testcapi_long.h $(srcdir)/Modules/_testcapi/parts.h $(srcdir)/Modules/_testcapi/util.h
MODULE__TESTINTERNALCAPI_DEPS=$(srcdir)/Modules/_testinternalcapi/parts.h
MODULE__SQLITE3_DEPS=$(srcdir)/Modules/_sqlite/connection.h $(srcdir)/Modules/_sqlite/cursor.h $(srcdir)/Modules/_sqlite/microprotocols.h $(srcdir)/Modules/_sqlite/module.h $(srcdir)/Modules/_sqlite/prepare_protocol.h $(srcdir)/Modules/_sqlite/row.h $(srcdir)/Modules/_sqlite/util.h
CODECS_COMMON_HEADERS=$(srcdir)/Modules/cjkcodecs/multibytecodec.h $(srcdir)/Modules/cjkcodecs/cjkcodecs.h

View file

@ -158,7 +158,7 @@
@MODULE_XXSUBTYPE_TRUE@xxsubtype xxsubtype.c
@MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c _testinternalcapi/pytime.c
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/unicode.c _testcapi/dict.c _testcapi/getargs.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyatomic.c _testcapi/pyos.c _testcapi/immortal.c _testcapi/heaptype_relative.c _testcapi/gc.c
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
@MODULE__TESTCLINIC_LIMITED_TRUE@_testclinic_limited _testclinic_limited.c

View file

@ -33,9 +33,8 @@
#include "clinic/_testinternalcapi.c.h"
#ifdef MS_WINDOWS
# include <winsock2.h> // struct timeval
#endif
// Include test definitions from _testinternalcapi/
#include "_testinternalcapi/parts.h"
#define MODULE_NAME "_testinternalcapi"
@ -1123,250 +1122,6 @@ pending_identify(PyObject *self, PyObject *args)
return res;
}
static PyObject *
test_pytime_fromseconds(PyObject *self, PyObject *args)
{
int seconds;
if (!PyArg_ParseTuple(args, "i", &seconds)) {
return NULL;
}
_PyTime_t ts = _PyTime_FromSeconds(seconds);
return _PyTime_AsNanosecondsObject(ts);
}
static int
check_time_rounding(int round)
{
if (round != _PyTime_ROUND_FLOOR
&& round != _PyTime_ROUND_CEILING
&& round != _PyTime_ROUND_HALF_EVEN
&& round != _PyTime_ROUND_UP)
{
PyErr_SetString(PyExc_ValueError, "invalid rounding");
return -1;
}
return 0;
}
static PyObject *
test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
{
PyObject *obj;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
_PyTime_t ts;
if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) {
return NULL;
}
return _PyTime_AsNanosecondsObject(ts);
}
static PyObject *
test_pytime_assecondsdouble(PyObject *self, PyObject *args)
{
PyObject *obj;
if (!PyArg_ParseTuple(args, "O", &obj)) {
return NULL;
}
_PyTime_t ts;
if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) {
return NULL;
}
double d = _PyTime_AsSecondsDouble(ts);
return PyFloat_FromDouble(d);
}
static PyObject *
test_PyTime_AsTimeval(PyObject *self, PyObject *args)
{
PyObject *obj;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
_PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
return NULL;
}
struct timeval tv;
if (_PyTime_AsTimeval(t, &tv, round) < 0) {
return NULL;
}
PyObject *seconds = PyLong_FromLongLong(tv.tv_sec);
if (seconds == NULL) {
return NULL;
}
return Py_BuildValue("Nl", seconds, (long)tv.tv_usec);
}
static PyObject *
test_PyTime_AsTimeval_clamp(PyObject *self, PyObject *args)
{
PyObject *obj;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
_PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
return NULL;
}
struct timeval tv;
_PyTime_AsTimeval_clamp(t, &tv, round);
PyObject *seconds = PyLong_FromLongLong(tv.tv_sec);
if (seconds == NULL) {
return NULL;
}
return Py_BuildValue("Nl", seconds, (long)tv.tv_usec);
}
#ifdef HAVE_CLOCK_GETTIME
static PyObject *
test_PyTime_AsTimespec(PyObject *self, PyObject *args)
{
PyObject *obj;
if (!PyArg_ParseTuple(args, "O", &obj)) {
return NULL;
}
_PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
return NULL;
}
struct timespec ts;
if (_PyTime_AsTimespec(t, &ts) == -1) {
return NULL;
}
return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec);
}
static PyObject *
test_PyTime_AsTimespec_clamp(PyObject *self, PyObject *args)
{
PyObject *obj;
if (!PyArg_ParseTuple(args, "O", &obj)) {
return NULL;
}
_PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
return NULL;
}
struct timespec ts;
_PyTime_AsTimespec_clamp(t, &ts);
return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec);
}
#endif
static PyObject *
test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
{
PyObject *obj;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
_PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
_PyTime_t ms = _PyTime_AsMilliseconds(t, round);
_PyTime_t ns = _PyTime_FromNanoseconds(ms);
return _PyTime_AsNanosecondsObject(ns);
}
static PyObject *
test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
{
PyObject *obj;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
_PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
_PyTime_t us = _PyTime_AsMicroseconds(t, round);
_PyTime_t ns = _PyTime_FromNanoseconds(us);
return _PyTime_AsNanosecondsObject(ns);
}
static PyObject *
test_pytime_object_to_time_t(PyObject *self, PyObject *args)
{
PyObject *obj;
time_t sec;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1) {
return NULL;
}
return _PyLong_FromTime_t(sec);
}
static PyObject *
test_pytime_object_to_timeval(PyObject *self, PyObject *args)
{
PyObject *obj;
time_t sec;
long usec;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1) {
return NULL;
}
return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec);
}
static PyObject *
test_pytime_object_to_timespec(PyObject *self, PyObject *args)
{
PyObject *obj;
time_t sec;
long nsec;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1) {
return NULL;
}
return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec);
}
static PyObject *
tracemalloc_get_traceback(PyObject *self, PyObject *args)
{
@ -1731,20 +1486,6 @@ static PyMethodDef module_functions[] = {
{"pending_threadfunc", _PyCFunction_CAST(pending_threadfunc),
METH_VARARGS | METH_KEYWORDS},
{"pending_identify", pending_identify, METH_VARARGS, NULL},
{"_PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS},
{"_PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS},
{"_PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
#ifdef HAVE_CLOCK_GETTIME
{"_PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS},
{"_PyTime_AsTimespec_clamp", test_PyTime_AsTimespec_clamp, METH_VARARGS},
#endif
{"_PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS},
{"_PyTime_AsTimeval_clamp", test_PyTime_AsTimeval_clamp, METH_VARARGS},
{"_PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS},
{"_PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS},
{"_PyTime_ObjectToTime_t", test_pytime_object_to_time_t, METH_VARARGS},
{"_PyTime_ObjectToTimespec", test_pytime_object_to_timespec, METH_VARARGS},
{"_PyTime_ObjectToTimeval", test_pytime_object_to_timeval, METH_VARARGS},
{"_PyTraceMalloc_GetTraceback", tracemalloc_get_traceback, METH_VARARGS},
{"test_tstate_capi", test_tstate_capi, METH_NOARGS, NULL},
{"_PyUnicode_TransformDecimalAndSpaceToASCII", unicode_transformdecimalandspacetoascii, METH_O},
@ -1771,6 +1512,10 @@ static PyMethodDef module_functions[] = {
static int
module_exec(PyObject *module)
{
if (_PyTestInternalCapi_Init_PyTime(module) < 0) {
return 1;
}
if (PyModule_Add(module, "SIZEOF_PYGC_HEAD",
PyLong_FromSsize_t(sizeof(PyGC_Head))) < 0) {
return 1;

View file

@ -0,0 +1,5 @@
Tests in this directory are compiled into the _testinternalcapi extension.
The main file for the extension is Modules/_testinternalcapimodule.c, which
calls `_PyTestInternalCapi_Init_*` from these functions.
See Modules/_testcapi/README.txt for guideline when writing C test code.

View file

@ -0,0 +1,15 @@
#ifndef Py_TESTINTERNALCAPI_PARTS_H
#define Py_TESTINTERNALCAPI_PARTS_H
// Always enable assertions
#undef NDEBUG
#ifndef Py_BUILD_CORE_BUILTIN
# define Py_BUILD_CORE_MODULE 1
#endif
#include "Python.h"
int _PyTestInternalCapi_Init_PyTime(PyObject *module);
#endif // Py_TESTINTERNALCAPI_PARTS_H

View file

@ -0,0 +1,279 @@
/* Test pycore_time.h */
#include "parts.h"
#include "pycore_time.h"
#ifdef MS_WINDOWS
# include <winsock2.h> // struct timeval
#endif
static PyObject *
test_pytime_fromseconds(PyObject *self, PyObject *args)
{
int seconds;
if (!PyArg_ParseTuple(args, "i", &seconds)) {
return NULL;
}
_PyTime_t ts = _PyTime_FromSeconds(seconds);
return _PyTime_AsNanosecondsObject(ts);
}
static int
check_time_rounding(int round)
{
if (round != _PyTime_ROUND_FLOOR
&& round != _PyTime_ROUND_CEILING
&& round != _PyTime_ROUND_HALF_EVEN
&& round != _PyTime_ROUND_UP)
{
PyErr_SetString(PyExc_ValueError, "invalid rounding");
return -1;
}
return 0;
}
static PyObject *
test_pytime_fromsecondsobject(PyObject *self, PyObject *args)
{
PyObject *obj;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
_PyTime_t ts;
if (_PyTime_FromSecondsObject(&ts, obj, round) == -1) {
return NULL;
}
return _PyTime_AsNanosecondsObject(ts);
}
static PyObject *
test_pytime_assecondsdouble(PyObject *self, PyObject *args)
{
PyObject *obj;
if (!PyArg_ParseTuple(args, "O", &obj)) {
return NULL;
}
_PyTime_t ts;
if (_PyTime_FromNanosecondsObject(&ts, obj) < 0) {
return NULL;
}
double d = _PyTime_AsSecondsDouble(ts);
return PyFloat_FromDouble(d);
}
static PyObject *
test_PyTime_AsTimeval(PyObject *self, PyObject *args)
{
PyObject *obj;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
_PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
return NULL;
}
struct timeval tv;
if (_PyTime_AsTimeval(t, &tv, round) < 0) {
return NULL;
}
PyObject *seconds = PyLong_FromLongLong(tv.tv_sec);
if (seconds == NULL) {
return NULL;
}
return Py_BuildValue("Nl", seconds, (long)tv.tv_usec);
}
static PyObject *
test_PyTime_AsTimeval_clamp(PyObject *self, PyObject *args)
{
PyObject *obj;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
_PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
return NULL;
}
struct timeval tv;
_PyTime_AsTimeval_clamp(t, &tv, round);
PyObject *seconds = PyLong_FromLongLong(tv.tv_sec);
if (seconds == NULL) {
return NULL;
}
return Py_BuildValue("Nl", seconds, (long)tv.tv_usec);
}
#ifdef HAVE_CLOCK_GETTIME
static PyObject *
test_PyTime_AsTimespec(PyObject *self, PyObject *args)
{
PyObject *obj;
if (!PyArg_ParseTuple(args, "O", &obj)) {
return NULL;
}
_PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
return NULL;
}
struct timespec ts;
if (_PyTime_AsTimespec(t, &ts) == -1) {
return NULL;
}
return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec);
}
static PyObject *
test_PyTime_AsTimespec_clamp(PyObject *self, PyObject *args)
{
PyObject *obj;
if (!PyArg_ParseTuple(args, "O", &obj)) {
return NULL;
}
_PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
return NULL;
}
struct timespec ts;
_PyTime_AsTimespec_clamp(t, &ts);
return Py_BuildValue("Nl", _PyLong_FromTime_t(ts.tv_sec), ts.tv_nsec);
}
#endif
static PyObject *
test_PyTime_AsMilliseconds(PyObject *self, PyObject *args)
{
PyObject *obj;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
_PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
_PyTime_t ms = _PyTime_AsMilliseconds(t, round);
_PyTime_t ns = _PyTime_FromNanoseconds(ms);
return _PyTime_AsNanosecondsObject(ns);
}
static PyObject *
test_PyTime_AsMicroseconds(PyObject *self, PyObject *args)
{
PyObject *obj;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
_PyTime_t t;
if (_PyTime_FromNanosecondsObject(&t, obj) < 0) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
_PyTime_t us = _PyTime_AsMicroseconds(t, round);
_PyTime_t ns = _PyTime_FromNanoseconds(us);
return _PyTime_AsNanosecondsObject(ns);
}
static PyObject *
test_pytime_object_to_time_t(PyObject *self, PyObject *args)
{
PyObject *obj;
time_t sec;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
if (_PyTime_ObjectToTime_t(obj, &sec, round) == -1) {
return NULL;
}
return _PyLong_FromTime_t(sec);
}
static PyObject *
test_pytime_object_to_timeval(PyObject *self, PyObject *args)
{
PyObject *obj;
time_t sec;
long usec;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
if (_PyTime_ObjectToTimeval(obj, &sec, &usec, round) == -1) {
return NULL;
}
return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), usec);
}
static PyObject *
test_pytime_object_to_timespec(PyObject *self, PyObject *args)
{
PyObject *obj;
time_t sec;
long nsec;
int round;
if (!PyArg_ParseTuple(args, "Oi", &obj, &round)) {
return NULL;
}
if (check_time_rounding(round) < 0) {
return NULL;
}
if (_PyTime_ObjectToTimespec(obj, &sec, &nsec, round) == -1) {
return NULL;
}
return Py_BuildValue("Nl", _PyLong_FromTime_t(sec), nsec);
}
static PyMethodDef TestMethods[] = {
{"_PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS},
{"_PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS},
{"_PyTime_AsSecondsDouble", test_pytime_assecondsdouble, METH_VARARGS},
#ifdef HAVE_CLOCK_GETTIME
{"_PyTime_AsTimespec", test_PyTime_AsTimespec, METH_VARARGS},
{"_PyTime_AsTimespec_clamp", test_PyTime_AsTimespec_clamp, METH_VARARGS},
#endif
{"_PyTime_AsTimeval", test_PyTime_AsTimeval, METH_VARARGS},
{"_PyTime_AsTimeval_clamp", test_PyTime_AsTimeval_clamp, METH_VARARGS},
{"_PyTime_FromSeconds", test_pytime_fromseconds, METH_VARARGS},
{"_PyTime_FromSecondsObject", test_pytime_fromsecondsobject, METH_VARARGS},
{"_PyTime_ObjectToTime_t", test_pytime_object_to_time_t, METH_VARARGS},
{"_PyTime_ObjectToTimespec", test_pytime_object_to_timespec, METH_VARARGS},
{"_PyTime_ObjectToTimeval", test_pytime_object_to_timeval, METH_VARARGS},
{NULL, NULL} /* sentinel */
};
int
_PyTestInternalCapi_Init_PyTime(PyObject *m)
{
if (PyModule_AddFunctions(m, TestMethods) < 0) {
return -1;
}
return 0;
}

View file

@ -94,6 +94,7 @@
</PropertyGroup>
<ItemGroup>
<ClCompile Include="..\Modules\_testinternalcapi.c" />
<ClCompile Include="..\Modules\_testinternalcapi\pytime.c" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\PC\python_nt.rc" />

View file

@ -12,6 +12,9 @@
<ClCompile Include="..\Modules\_testinternalcapi.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Modules\_testinternalcapi\pytime.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\PC\python_nt.rc">

1
configure generated vendored
View file

@ -26553,6 +26553,7 @@ SRCDIRS="\
Modules/_sqlite \
Modules/_sre \
Modules/_testcapi \
Modules/_testinternalcapi \
Modules/_xxtestfuzz \
Modules/cjkcodecs \
Modules/expat \

View file

@ -6470,6 +6470,7 @@ SRCDIRS="\
Modules/_sqlite \
Modules/_sre \
Modules/_testcapi \
Modules/_testinternalcapi \
Modules/_xxtestfuzz \
Modules/cjkcodecs \
Modules/expat \