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

Nikolaus Rath Nikolaus at rath.org
Wed Jan 22 04:19:39 CET 2014


Larry Hastings <larry at hastings.org> writes:
> A comment on your approach so far: I'm very much against giving
> "default" a default value in the constructor.

You mean in the definition of the custom converter class?

> I realize that hack saves you having to say "= NULL" in a lot of
> places.  But explicit is better than implicit, and we're going to read
> these signatures a lot more often than we write them, and I want
> Clinic signatures to be easy to read at first glance.
[....]
> All is not lost!  What follows is rough pseudo-C code, hopefully you
> can take it from here.
>
>    typedef struct {
>       int set;
>       time_t when;
>    } clinic_time_t;
>    #define DEFAULT_CLINIC_TIME_T {0, 0}
>
[...]
>
>    /*[python input]
>    class clinic_time_t_converter(CConverter):
>         type = 'clinic_time_t'
>         converter = 'parse_clinic_time_t'
>         c_default = 'DEFAULT_CLINIC_TIME_T'
>    [python start generated code]*/
>    /*[python end generated code: checksum=...]*/
>
> Now you can use clinic_time_t.  Parameters declared clinic_time_t can
> be required, or they can be optional; if they're optional give them a
> default value of None.

That doesn't work. If the default value is declared for the function
rather than in the converter definition, it overwrites the C default:

/*[clinic input]
time.gmtime

    seconds: clinic_time_t=None
    /
*/

gives:

static PyObject *
time_gmtime(PyModuleDef *module, PyObject *args)
{
    PyObject *return_value = NULL;
    clinic_time_t seconds = Py_None;

    if (!PyArg_ParseTuple(args,
        "|O&:gmtime",
        parse_clinic_time_t, &seconds))
        goto exit;
    return_value = time_gmtime_impl(module, seconds);

so the default for seconds is now Py_None instead of
DEFAULT_CLINIC_TIME_T'.


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