From 25dfcec09930b68fd80d5e536fb28aa076aa12d8 Mon Sep 17 00:00:00 2001 From: Bas Schoenmaeckers Date: Thu, 4 Jan 2024 12:00:37 +0100 Subject: [PATCH] Trust value to be not null --- source/encoder.c | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-) diff --git a/source/encoder.c b/source/encoder.c index e6a1333..0bd67e2 100644 --- a/source/encoder.c +++ b/source/encoder.c @@ -1042,34 +1042,32 @@ CBOREncoder_encode_date(CBOREncoderObject *self, PyObject *value) PyObject *tmp, *ret = NULL; const char *buf; Py_ssize_t length; - if (value) { - if (self->date_as_datetime) { - tmp = PyDateTimeAPI->DateTime_FromDateAndTime( - PyDateTime_GET_YEAR(value), - PyDateTime_GET_MONTH(value), - PyDateTime_GET_DAY(value), - 0, 0, 0, 0, self->tz, - PyDateTimeAPI->DateTimeType); - if (tmp) - ret = CBOREncoder_encode_datetime(self, tmp); + if (self->date_as_datetime) { + tmp = PyDateTimeAPI->DateTime_FromDateAndTime( + PyDateTime_GET_YEAR(value), + PyDateTime_GET_MONTH(value), + PyDateTime_GET_DAY(value), + 0, 0, 0, 0, self->tz, + PyDateTimeAPI->DateTimeType); + if (tmp) + ret = CBOREncoder_encode_datetime(self, tmp); + } + else if (self->timestamp_format) { + tmp = PyObject_CallMethodObjArgs( + value, _CBOR2_str_toordinal, NULL); + if (tmp && fp_write(self, "\xD8\x64", 2) == 0) { + ret = CBOREncoder_encode_int(self, PyNumber_Subtract(tmp, PyLong_FromLong(719163))); } - else if (self->timestamp_format) { - tmp = PyObject_CallMethodObjArgs( - value, _CBOR2_str_toordinal, NULL); - if (tmp && fp_write(self, "\xD8\x64", 2) == 0) { - ret = CBOREncoder_encode_int(self, PyNumber_Subtract(tmp, PyLong_FromLong(719163))); - } - } else { - tmp = PyObject_CallMethodObjArgs( - value, _CBOR2_str_isoformat, NULL); - if (tmp && fp_write(self, "\xD9\x03\xEC", 3) == 0) { - ret = CBOREncoder_encode_string(self, tmp); - } + } else { + tmp = PyObject_CallMethodObjArgs( + value, _CBOR2_str_isoformat, NULL); + if (tmp && fp_write(self, "\xD9\x03\xEC", 3) == 0) { + ret = CBOREncoder_encode_string(self, tmp); } - Py_XDECREF(tmp); } + Py_XDECREF(tmp); return ret; -} +}3 // A variant of fp_classify for the decimal.Decimal type