mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
time.get_clock_info() uses a namespace instead of structseq
This commit is contained in:
parent
d9738242f8
commit
bda4b8802c
2 changed files with 31 additions and 62 deletions
|
@ -160,30 +160,6 @@ The module defines the following functions and data items:
|
||||||
.. versionadded:: 3.3
|
.. versionadded:: 3.3
|
||||||
|
|
||||||
|
|
||||||
.. class:: clock_info
|
|
||||||
|
|
||||||
Clock information object returned by :func:`get_clock_info`.
|
|
||||||
|
|
||||||
.. attribute:: implementation
|
|
||||||
|
|
||||||
The name of the underlying C function used to get the clock value.
|
|
||||||
|
|
||||||
.. attribute:: monotonic
|
|
||||||
|
|
||||||
``True`` if the clock cannot go backward, ``False`` otherwise.
|
|
||||||
|
|
||||||
.. attribute:: adjusted
|
|
||||||
|
|
||||||
``True`` if the clock can be adjusted (e.g. by a NTP daemon), ``False``
|
|
||||||
otherwise.
|
|
||||||
|
|
||||||
.. attribute:: resolution
|
|
||||||
|
|
||||||
The resolution of the clock in seconds (:class:`float`).
|
|
||||||
|
|
||||||
.. versionadded:: 3.3
|
|
||||||
|
|
||||||
|
|
||||||
.. function:: clock_settime(clk_id, time)
|
.. function:: clock_settime(clk_id, time)
|
||||||
|
|
||||||
Set the time of the specified clock *clk_id*.
|
Set the time of the specified clock *clk_id*.
|
||||||
|
@ -267,7 +243,7 @@ The module defines the following functions and data items:
|
||||||
|
|
||||||
.. function:: get_clock_info(name)
|
.. function:: get_clock_info(name)
|
||||||
|
|
||||||
Get information on the specified clock as a :class:`clock_info` object.
|
Get information on the specified clock as a namespace object.
|
||||||
Supported clock names and the corresponding functions to read their value
|
Supported clock names and the corresponding functions to read their value
|
||||||
are:
|
are:
|
||||||
|
|
||||||
|
@ -277,6 +253,16 @@ The module defines the following functions and data items:
|
||||||
* ``'process_time'``: :func:`time.process_time`
|
* ``'process_time'``: :func:`time.process_time`
|
||||||
* ``'time'``: :func:`time.time`
|
* ``'time'``: :func:`time.time`
|
||||||
|
|
||||||
|
The result has the following attributes:
|
||||||
|
|
||||||
|
- *adjusted*: ``True`` if the clock can be adjusted (e.g. by a NTP daemon),
|
||||||
|
``False`` otherwise
|
||||||
|
- *implementation*: The name of the underlying C function used to get
|
||||||
|
the clock value
|
||||||
|
- *monotonic*: ``True`` if the clock cannot go backward,
|
||||||
|
``False`` otherwise
|
||||||
|
- *resolution*: The resolution of the clock in seconds (:class:`float`)
|
||||||
|
|
||||||
.. versionadded:: 3.3
|
.. versionadded:: 3.3
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1124,35 +1124,12 @@ PyDoc_STRVAR(process_time_doc,
|
||||||
Process time for profiling: sum of the kernel and user-space CPU time.");
|
Process time for profiling: sum of the kernel and user-space CPU time.");
|
||||||
|
|
||||||
|
|
||||||
static PyTypeObject ClockInfoType;
|
|
||||||
|
|
||||||
PyDoc_STRVAR(ClockInfo_docstring,
|
|
||||||
"Clock information");
|
|
||||||
|
|
||||||
static PyStructSequence_Field ClockInfo_fields[] = {
|
|
||||||
{"implementation", "name of the underlying C function "
|
|
||||||
"used to get the clock value"},
|
|
||||||
{"monotonic", "True if the clock cannot go backward, False otherwise"},
|
|
||||||
{"adjusted", "True if the clock can be adjusted "
|
|
||||||
"(e.g. by a NTP daemon), False otherwise"},
|
|
||||||
{"resolution", "resolution of the clock in seconds"},
|
|
||||||
{NULL, NULL}
|
|
||||||
};
|
|
||||||
|
|
||||||
static PyStructSequence_Desc ClockInfo_desc = {
|
|
||||||
"time.clock_info",
|
|
||||||
ClockInfo_docstring,
|
|
||||||
ClockInfo_fields,
|
|
||||||
4,
|
|
||||||
};
|
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
time_get_clock_info(PyObject *self, PyObject *args)
|
time_get_clock_info(PyObject *self, PyObject *args)
|
||||||
{
|
{
|
||||||
char *name;
|
char *name;
|
||||||
PyObject *obj;
|
|
||||||
_Py_clock_info_t info;
|
_Py_clock_info_t info;
|
||||||
PyObject *result;
|
PyObject *obj = NULL, *dict, *ns;
|
||||||
|
|
||||||
if (!PyArg_ParseTuple(args, "s:get_clock_info", &name))
|
if (!PyArg_ParseTuple(args, "s:get_clock_info", &name))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -1191,39 +1168,50 @@ time_get_clock_info(PyObject *self, PyObject *args)
|
||||||
return NULL;
|
return NULL;
|
||||||
Py_DECREF(obj);
|
Py_DECREF(obj);
|
||||||
|
|
||||||
result = PyStructSequence_New(&ClockInfoType);
|
dict = PyDict_New();
|
||||||
if (result == NULL)
|
if (dict == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
assert(info.implementation != NULL);
|
assert(info.implementation != NULL);
|
||||||
obj = PyUnicode_FromString(info.implementation);
|
obj = PyUnicode_FromString(info.implementation);
|
||||||
if (obj == NULL)
|
if (obj == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
PyStructSequence_SET_ITEM(result, 0, obj);
|
if (PyDict_SetItemString(dict, "implementation", obj) == -1)
|
||||||
|
goto error;
|
||||||
|
Py_CLEAR(obj);
|
||||||
|
|
||||||
assert(info.monotonic != -1);
|
assert(info.monotonic != -1);
|
||||||
obj = PyBool_FromLong(info.monotonic);
|
obj = PyBool_FromLong(info.monotonic);
|
||||||
if (obj == NULL)
|
if (obj == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
PyStructSequence_SET_ITEM(result, 1, obj);
|
if (PyDict_SetItemString(dict, "monotonic", obj) == -1)
|
||||||
|
goto error;
|
||||||
|
Py_CLEAR(obj);
|
||||||
|
|
||||||
assert(info.adjusted != -1);
|
assert(info.adjusted != -1);
|
||||||
obj = PyBool_FromLong(info.adjusted);
|
obj = PyBool_FromLong(info.adjusted);
|
||||||
if (obj == NULL)
|
if (obj == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
PyStructSequence_SET_ITEM(result, 2, obj);
|
if (PyDict_SetItemString(dict, "adjusted", obj) == -1)
|
||||||
|
goto error;
|
||||||
|
Py_CLEAR(obj);
|
||||||
|
|
||||||
assert(info.resolution > 0.0);
|
assert(info.resolution > 0.0);
|
||||||
assert(info.resolution <= 1.0);
|
assert(info.resolution <= 1.0);
|
||||||
obj = PyFloat_FromDouble(info.resolution);
|
obj = PyFloat_FromDouble(info.resolution);
|
||||||
if (obj == NULL)
|
if (obj == NULL)
|
||||||
goto error;
|
goto error;
|
||||||
PyStructSequence_SET_ITEM(result, 3, obj);
|
if (PyDict_SetItemString(dict, "resolution", obj) == -1)
|
||||||
|
goto error;
|
||||||
|
Py_CLEAR(obj);
|
||||||
|
|
||||||
return result;
|
ns = _PyNamespace_New(dict);
|
||||||
|
Py_DECREF(dict);
|
||||||
|
return ns;
|
||||||
|
|
||||||
error:
|
error:
|
||||||
Py_DECREF(result);
|
Py_DECREF(dict);
|
||||||
|
Py_XDECREF(obj);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1451,11 +1439,6 @@ PyInit_time(void)
|
||||||
PyStructSequence_InitType(&StructTimeType,
|
PyStructSequence_InitType(&StructTimeType,
|
||||||
&struct_time_type_desc);
|
&struct_time_type_desc);
|
||||||
|
|
||||||
/* initialize ClockInfoType */
|
|
||||||
PyStructSequence_InitType(&ClockInfoType, &ClockInfo_desc);
|
|
||||||
Py_INCREF(&ClockInfoType);
|
|
||||||
PyModule_AddObject(m, "clock_info", (PyObject*)&ClockInfoType);
|
|
||||||
|
|
||||||
#ifdef MS_WINDOWS
|
#ifdef MS_WINDOWS
|
||||||
winver.dwOSVersionInfoSize = sizeof(winver);
|
winver.dwOSVersionInfoSize = sizeof(winver);
|
||||||
if (!GetVersionEx((OSVERSIONINFO*)&winver)) {
|
if (!GetVersionEx((OSVERSIONINFO*)&winver)) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue