From 445ff7e6c98b9203780b434fa2dbc4dda1340e3c Mon Sep 17 00:00:00 2001 From: c-holtermann Date: Sat, 6 Apr 2019 07:51:15 +0200 Subject: [PATCH] check for argument type. Also allow int. --- bindings/python/time64.i | 51 ++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/bindings/python/time64.i b/bindings/python/time64.i index 82736186e6..e7553b5048 100644 --- a/bindings/python/time64.i +++ b/bindings/python/time64.i @@ -33,13 +33,23 @@ // require time64 as an argument %typemap(in) time64 { PyDateTime_IMPORT; - struct tm time = {PyDateTime_DATE_GET_SECOND($input), - PyDateTime_DATE_GET_MINUTE($input), - PyDateTime_DATE_GET_HOUR($input), - PyDateTime_GET_DAY($input), - PyDateTime_GET_MONTH($input) - 1, - PyDateTime_GET_YEAR($input) - 1900}; - $1 = gnc_mktime(&time); + + if (!PyDateTime_Check($input) && !PyInt_Check($input)) { + PyErr_SetString(PyExc_ValueError,"datetime or integer expected"); + return NULL; + } + + if (PyDateTime_Check($input)) { + struct tm time = {PyDateTime_DATE_GET_SECOND($input), + PyDateTime_DATE_GET_MINUTE($input), + PyDateTime_DATE_GET_HOUR($input), + PyDateTime_GET_DAY($input), + PyDateTime_GET_MONTH($input) - 1, + PyDateTime_GET_YEAR($input) - 1900}; + $1 = gnc_mktime(&time); + } else { + $1 = PyInt_AsLong($input); + } } // A typemap for converting python dates to time64 *, for functions that @@ -61,14 +71,25 @@ // Mark Jenkins %typemap(in) time64 * (time64 secs) { PyDateTime_IMPORT; - struct tm time = {PyDateTime_DATE_GET_SECOND($input), - PyDateTime_DATE_GET_MINUTE($input), - PyDateTime_DATE_GET_HOUR($input), - PyDateTime_GET_DAY($input), - PyDateTime_GET_MONTH($input) - 1, - PyDateTime_GET_YEAR($input) - 1900}; - time64 secs = gnc_mktime(&time); - $1 = &secs; + + if (!PyDateTime_Check($input) && !PyInt_Check($input)) { + PyErr_SetString(PyExc_ValueError,"datetime or integer expected"); + return NULL; + } + + if (PyDateTime_Check($input)) { + struct tm time = {PyDateTime_DATE_GET_SECOND($input), + PyDateTime_DATE_GET_MINUTE($input), + PyDateTime_DATE_GET_HOUR($input), + PyDateTime_GET_DAY($input), + PyDateTime_GET_MONTH($input) - 1, + PyDateTime_GET_YEAR($input) - 1900}; + time64 secs = gnc_mktime(&time); + $1 = &secs; + } else { + time64 secs = PyInt_AsLong($input); + $1 = &secs; + } } // A typemap for converting time64 values returned from functions to