<div dir="ltr">Hi Nikolaus. I also started a conversion of timemodule, but dropped it when I saw in the issue that you had taken over that conversion. I also tried to turn parse_time_t_args into a converter. However, it won't work. The problem is that parse_time_t_args must be called whether or not the user supplies an argument to the function, but an Argument Clinic converter only gets called if the user actually supplies something, and not on the default value.<div>

<br></div><div>So, the best idea is to</div><div><br></div><div>* Remove the PyArgs_ParseTuple code from parse_time_t_args</div><div>* Declare seconds as a plain object in Argument Clinic</div><div>* Call the modified parse_time_t_args on seconds first thing in the _impl functions</div>

</div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sat, Jan 18, 2014 at 4:56 PM, Nikolaus Rath <span dir="ltr"><<a href="mailto:Nikolaus@rath.org" target="_blank">Nikolaus@rath.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
I'm trying to convert functions using parse_time_t_args() (from<br>
timemodule.c) for argument parsing to argument clinic.<br>
<br>
The function is defined as:<br>
<br>
,----<br>
| static int<br>
| parse_time_t_args(PyObject *args, char *format, time_t *pwhen)<br>
| {<br>
|     PyObject *ot = NULL;<br>
|     time_t whent;<br>
|<br>
|     if (!PyArg_ParseTuple(args, format, &ot))<br>
|         return 0;<br>
|     if (ot == NULL || ot == Py_None) {<br>
|         whent = time(NULL);<br>
|     }<br>
|     else {<br>
|         if (_PyTime_ObjectToTime_t(ot, &whent) == -1)<br>
|             return 0;<br>
|     }<br>
|     *pwhen = whent;<br>
|     return 1;<br>
| }<br>
`----<br>
<br>
and used like this:<br>
<br>
,----<br>
| static PyObject *<br>
| time_localtime(PyObject *self, PyObject *args)<br>
| {<br>
|     time_t when;<br>
|     struct tm buf;<br>
|<br>
|     if (!parse_time_t_args(args, "|O:localtime", &when))<br>
|         return NULL;<br>
|     if (pylocaltime(&when, &buf) == -1)<br>
|         return NULL;<br>
|     return tmtotuple(&buf);<br>
| }<br>
`----<br>
<br>
In other words, if any Python object is passed to it, it calls<br>
_PyTime_ObjectToTime_t on it to convert it to time_t, and otherwise uses<br>
time(NULL) as the default value.<br>
<br>
May first attempt to implement something similar in argument clinic was:<br>
<br>
,----<br>
| /*[python input]<br>
| class time_t_converter(CConverter):<br>
|     type = 'time_t'<br>
|     converter = 'time_t_converter'<br>
|     default = None<br>
|     py_default = 'None'<br>
|     c_default = 'time(NULL)'<br>
|     converter = '_PyTime_ObjectToTime_t'<br>
| [python start generated code]*/<br>
|<br>
| /*[clinic input]<br>
| time.localtime<br>
|<br>
|     seconds: time_t<br>
|     /<br>
|<br>
| bla.<br>
| [clinic start generated code]*/<br>
`----<br>
<br>
However, running clinic.py on this file gives:<br>
<br>
,----<br>
| $ Tools/clinic/clinic.py Modules/timemodule.c<br>
| Error in file "Modules/timemodule.c" on line 529:<br>
| Exception raised during parsing:<br>
| Traceback (most recent call last):<br>
|   File "Tools/clinic/clinic.py", line 1445, in parse<br>
|     parser.parse(block)<br>
|   File "Tools/clinic/clinic.py", line 2738, in parse<br>
|     self.state(None)<br>
|   File "Tools/clinic/clinic.py", line 3468, in state_terminal<br>
|     self.function.docstring = self.format_docstring()<br>
|   File "Tools/clinic/clinic.py", line 3344, in format_docstring<br>
|     s += "".join(a)<br>
| TypeError: sequence item 2: expected str instance, NoneType found<br>
`----<br>
<br>
What am I doing wrong?<br>
<br>
<br>
Best,<br>
Nikolaus<br>
<span class="HOEnZb"><font color="#888888"><br>
--<br>
Encrypted emails preferred.<br>
PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C<br>
<br>
             »Time flies like an arrow, fruit flies like a Banana.«<br>
_______________________________________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" target="_blank">https://mail.python.org/mailman/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/rmsr%40lab.net" target="_blank">https://mail.python.org/mailman/options/python-dev/rmsr%40lab.net</a><br>
</font></span></blockquote></div><br></div>