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

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; timet when; } clinictimet; #define DEFAULTCLINICTIMET {0, 0} [...] /*[python input] class clinictimetconverter(CConverter): type = 'clinictimet' converter = 'parseclinictimet' cdefault = 'DEFAULTCLINICTIMET' [python start generated code]*/ /[python end generated code: checksum=...]/ Now you can use clinictimet. Parameters declared clinictimet 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