Skip to content

Commit f08cace

Browse files
committed
couple fixes
1 parent 45f62bc commit f08cace

File tree

3 files changed

+35
-29
lines changed

3 files changed

+35
-29
lines changed

source/cbor.c

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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 */

source/module.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,47 @@ static PyObject *s_datetime_class = NULL;
4343

4444
static int s_init_datetime_cache(void) {
4545
if (s_datetime_class) {
46-
return 0; /* Already initialized */
46+
return AWS_OP_SUCCESS; /* Already initialized */
4747
}
4848

4949
/* Import datetime module */
5050
PyObject *datetime_module = PyImport_ImportModule("datetime");
5151
if (!datetime_module) {
52-
return -1;
52+
/* Python exception already set by PyImport_ImportModule */
53+
return aws_py_raise_error();
5354
}
5455

5556
/* Get datetime class - new reference we'll keep */
5657
s_datetime_class = PyObject_GetAttrString(datetime_module, "datetime");
5758
Py_DECREF(datetime_module);
5859

5960
if (!s_datetime_class) {
60-
return -1;
61+
/* Python exception already set by PyObject_GetAttrString */
62+
return aws_py_raise_error();
6163
}
6264

63-
return 0;
65+
return AWS_OP_SUCCESS;
6466
}
6567

6668
// static void s_cleanup_datetime_cache(void) {
6769
// Py_XDECREF(s_datetime_class);
6870
// s_datetime_class = NULL;
6971
// }
7072

71-
int aws_py_is_datetime_instance(PyObject *obj) {
72-
if (!s_datetime_class && s_init_datetime_cache() < 0) {
73-
return -1;
73+
int aws_py_is_datetime_instance(PyObject *obj, bool *out_is_datetime) {
74+
AWS_ASSERT(out_is_datetime);
75+
76+
if (!s_datetime_class && s_init_datetime_cache() != AWS_OP_SUCCESS) {
77+
return AWS_OP_ERR;
78+
}
79+
80+
int result = PyObject_IsInstance(obj, s_datetime_class);
81+
if (result < 0) {
82+
return AWS_OP_ERR; /* PyObject_IsInstance failed */
7483
}
75-
return PyObject_IsInstance(obj, s_datetime_class);
84+
85+
*out_is_datetime = (result != 0);
86+
return AWS_OP_SUCCESS;
7687
}
7788

7889
PyObject *aws_py_init_logging(PyObject *self, PyObject *args) {

source/module.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ struct aws_byte_cursor aws_byte_cursor_from_pybytes(PyObject *py_bytes);
7272

7373
/**
7474
* Check if a PyObject is an instance of datetime.datetime using stable ABI.
75-
* Returns:
76-
* 1 if obj is a datetime instance
77-
* 0 if obj is not a datetime instance
78-
* -1 on error (Python exception set)
75+
*
76+
* @param obj PyObject to check
77+
* @param out_is_datetime Pointer to store result (true if datetime, false otherwise)
78+
* @return AWS_OP_SUCCESS on success, AWS_OP_ERR on error (Python exception set)
7979
*/
80-
int aws_py_is_datetime_instance(PyObject *obj);
80+
int aws_py_is_datetime_instance(PyObject *obj, bool *out_is_datetime);
8181

8282
/* Set current thread's error indicator based on aws_last_error() */
8383
void PyErr_SetAwsLastError(void);

0 commit comments

Comments
 (0)