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

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 | parsetimetargs(PyObject *args, char *format, timet *pwhen) | { | PyObject *ot = NULL; | timet whent; || if (!PyArgParseTuple(args, format, &ot)) | return 0; | if (ot == NULL || ot == PyNone) { | whent = time(NULL); | } | else { | if (PyTimeObjectToTimet(ot, &whent) == -1) | return 0; | } | *pwhen = whent; | return 1; | } `----

and used like this:

,---- | static PyObject * | timelocaltime(PyObject *self, PyObject *args) | { | timet when; | struct tm buf; || if (!parsetimetargs(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 timetconverter(CConverter): | type = 'timet' | converter = 'timetconverter' | default = None | pydefault = 'None' | cdefault = 'time(NULL)' | converter = 'PyTimeObjectToTimet' | [python start generated code]*/ || /*[clinic input] | time.localtime || seconds: timet | / || 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 stateterminal | self.function.docstring = self.formatdocstring() | File "Tools/clinic/clinic.py", line 3344, in formatdocstring | 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