[Python-Dev] Using argument clinic to replace timemodule.c:parse_time_t_args()

Nikolaus Rath Nikolaus at rath.org
Sun Jan 19 01:56:36 CET 2014


Hello,

I'm trying to convert functions using parse_time_t_args() (from
timemodule.c) for argument parsing to argument clinic.

The function is defined as:

,----
| static int
| parse_time_t_args(PyObject *args, char *format, time_t *pwhen)
| {
|     PyObject *ot = NULL;
|     time_t whent;
| 
|     if (!PyArg_ParseTuple(args, format, &ot))
|         return 0;
|     if (ot == NULL || ot == Py_None) {
|         whent = time(NULL);
|     }
|     else {
|         if (_PyTime_ObjectToTime_t(ot, &whent) == -1)
|             return 0;
|     }
|     *pwhen = whent;
|     return 1;
| }
`----

and used like this:

,----
| static PyObject *
| time_localtime(PyObject *self, PyObject *args)
| {
|     time_t when;
|     struct tm buf;
| 
|     if (!parse_time_t_args(args, "|O:localtime", &when))
|         return NULL;
|     if (pylocaltime(&when, &buf) == -1)
|         return NULL;
|     return tmtotuple(&buf);
| }
`----

In other words, if any Python object is passed to it, it calls
_PyTime_ObjectToTime_t on it to convert it to time_t, and otherwise uses
time(NULL) as the default value.

May first attempt to implement something similar in argument clinic was:

,----
| /*[python input]
| class time_t_converter(CConverter):
|     type = 'time_t'
|     converter = 'time_t_converter'
|     default = None
|     py_default = 'None'
|     c_default = 'time(NULL)'
|     converter = '_PyTime_ObjectToTime_t'
| [python start generated code]*/
|     
| /*[clinic input]
| time.localtime
| 
|     seconds: time_t
|     /
| 
| bla.
| [clinic start generated code]*/
`----

However, running clinic.py on this file gives:

,----
| $ Tools/clinic/clinic.py Modules/timemodule.c 
| Error in file "Modules/timemodule.c" on line 529:
| Exception raised during parsing:
| Traceback (most recent call last):
|   File "Tools/clinic/clinic.py", line 1445, in parse
|     parser.parse(block)
|   File "Tools/clinic/clinic.py", line 2738, in parse
|     self.state(None)
|   File "Tools/clinic/clinic.py", line 3468, in state_terminal
|     self.function.docstring = self.format_docstring()
|   File "Tools/clinic/clinic.py", line 3344, in format_docstring
|     s += "".join(a)
| TypeError: sequence item 2: expected str instance, NoneType found
`----

What am I doing wrong?


Best,
Nikolaus

-- 
Encrypted emails preferred.
PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C

             »Time flies like an arrow, fruit flies like a Banana.«


More information about the Python-Dev mailing list