[Python-Dev] Using argument clinic to replace timemodule.c:parse_time_t_args() (original) (raw)
Nikolaus Rath Nikolaus at rath.org
Sun Jan 19 04:42:35 CET 2014
- Previous message: [Python-Dev] Using argument clinic to replace timemodule.c:parse_time_t_args()
- Next message: [Python-Dev] Using argument clinic to replace timemodule.c:parse_time_t_args()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Hi Ryan,
Ryan Smith-Roberts <rmsr at lab.net> writes:
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 parsetimetargs into a converter. However, it won't work. The problem is that parsetimetargs 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.
I don't quite follow. My approach was to drop parse_time_t_args() completely and use _PyTime_ObjectToTime_t() as the conversion function (which only needs to be called if the user supplied something).
In other words, I would have expected
,---- | /*[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]*/ `----
to produce something like this:
static PyObject * time_localtime(PyObject *self, PyObject *args) { PyObject *obj = NULL; time_t seconds; struct tm buf;
if (!PyArg_ParseTuple(args, "|O:localtime", &obj))
return NULL;
if (obj == NULL || obj == Py_None)
seconds = time(NULL);
else {
if (_PyTime_ObjectToTime_t(obj, &seconds) == -1)
return NULL;
}
return time_localtime_impl(self, seconds);
}
Apart from getting an error from clinic.py, it seems to me that this should in principle be possible.
Best, Nikolaus
So, the best idea is to * Remove the PyArgsParseTuple code from parsetimetargs * Declare seconds as a plain object in Argument Clinic * Call the modified parsetimetargs on seconds first thing in the impl functions
On Sat, Jan 18, 2014 at 4:56 PM, Nikolaus Rath <Nikolaus at rath.org> wrote: Hello,
I'm trying to convert functions using parsetimetargs() (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 PyTimeObjectToTimet on it to convert it to timet, 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.«
Python-Dev mailing list Python-Dev at python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/rmsr%40lab.net
-- 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.«
- Previous message: [Python-Dev] Using argument clinic to replace timemodule.c:parse_time_t_args()
- Next message: [Python-Dev] Using argument clinic to replace timemodule.c:parse_time_t_args()
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]