@@ -220,26 +220,21 @@ static PyObject *s_cbor_encoder_write_pyobject(struct aws_cbor_encoder *encoder,
220220 result = s_cbor_encoder_write_pydict (encoder , py_object );
221221 } else {
222222 /* Check for datetime using stable ABI (slower, so checked last) */
223- int is_dt = aws_py_is_datetime_instance ( py_object ) ;
224- if (is_dt < 0 ) {
223+ bool is_datetime = false ;
224+ if (aws_py_is_datetime_instance ( py_object , & is_datetime ) != AWS_OP_SUCCESS ) {
225225 /* Error occurred during datetime check */
226226 result = NULL ;
227- } else if (is_dt > 0 ) {
227+ } else if (is_datetime ) {
228228 /* Convert datetime to CBOR epoch time (tag 1) */
229- PyObject * timestamp_method = PyObject_GetAttrString (py_object , "timestamp" );
230- if (timestamp_method ) {
231- PyObject * timestamp = PyObject_CallNoArgs (timestamp_method );
232- Py_DECREF (timestamp_method );
233- if (timestamp ) {
234- /* Write CBOR tag 1 (epoch time) + timestamp */
235- aws_cbor_encoder_write_tag (encoder , AWS_CBOR_TAG_EPOCH_TIME );
236- result = s_cbor_encoder_write_pyobject_as_float (encoder , timestamp );
237- Py_DECREF (timestamp );
238- } else {
239- result = NULL ; /* timestamp() call failed */
240- }
229+ /* Call timestamp() method - PyObject_CallMethod is more idiomatic and compatible with Python 3.8+ */
230+ PyObject * timestamp = PyObject_CallMethod (py_object , "timestamp" , NULL );
231+ if (timestamp ) {
232+ /* Write CBOR tag 1 (epoch time) + timestamp */
233+ aws_cbor_encoder_write_tag (encoder , AWS_CBOR_TAG_EPOCH_TIME );
234+ result = s_cbor_encoder_write_pyobject_as_float (encoder , timestamp );
235+ Py_DECREF (timestamp );
241236 } else {
242- result = NULL ; /* Failed to get timestamp method */
237+ result = NULL ; /* timestamp() call failed */
243238 }
244239 } else {
245240 /* Unsupported type */
0 commit comments