Skip to content

Commit

Permalink
upgrade_pythoncapi.py
Browse files Browse the repository at this point in the history
  • Loading branch information
juliannguyen4 committed Oct 22, 2024
1 parent aab9e2c commit 2ea9a9a
Show file tree
Hide file tree
Showing 21 changed files with 1,775 additions and 86 deletions.
1,659 changes: 1,659 additions & 0 deletions src/include/pythoncapi_compat.h

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/main/client/admin.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "pythoncapi_compat.h"

/*******************************************************************************
* Copyright 2013-2021 Aerospike, Inc.
*
Expand Down Expand Up @@ -610,7 +612,7 @@ PyObject *AerospikeClient_Admin_Revoke_Roles(AerospikeClient *self,
goto CLEANUP;
}

if (py_policy == Py_None) {
if (Py_IsNone(py_policy)) {
py_policy = PyDict_New();
}

Expand Down Expand Up @@ -1258,7 +1260,7 @@ PyObject *AerospikeClient_Admin_Set_Whitelist(AerospikeClient *self,
goto CLEANUP;
}
}
else if (py_whitelist == Py_None) {
else if (Py_IsNone(py_whitelist)) {
whitelist_size = 0;
}
else {
Expand Down
6 changes: 4 additions & 2 deletions src/main/client/batch_operate.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "pythoncapi_compat.h"

/*******************************************************************************
* Copyright 2013-2022 Aerospike, Inc.
*
Expand Down Expand Up @@ -224,7 +226,7 @@ static PyObject *AerospikeClient_Batch_Operate_Invoke(
}
}

if (py_ttl == NULL || py_ttl == Py_None) {
if (py_ttl == NULL || Py_IsNone(py_ttl)) {
// If ttl in this transaction's batch write policy isn't set, use the client config's default batch write
// policy ttl
ops.ttl = AS_RECORD_CLIENT_DEFAULT_TTL;
Expand Down Expand Up @@ -373,7 +375,7 @@ PyObject *AerospikeClient_Batch_Operate(AerospikeClient *self, PyObject *args,
goto error;
}

if (py_ttl && py_ttl != Py_None && !PyLong_Check(py_ttl)) {
if (py_ttl && !Py_IsNone(py_ttl) && !PyLong_Check(py_ttl)) {
as_error_update(&err, AEROSPIKE_ERR_PARAM, "ttl should be an integer");
goto error;
}
Expand Down
10 changes: 6 additions & 4 deletions src/main/client/batch_write.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "pythoncapi_compat.h"

/*******************************************************************************
* Copyright 2013-2020 Aerospike, Inc.
*
Expand Down Expand Up @@ -39,7 +41,7 @@
{ \
PyObject *py___policy = \
PyObject_GetAttrString(py_batch_record, FIELD_NAME_BATCH_POLICY); \
if (py___policy != Py_None) { \
if (!Py_IsNone(py___policy)) { \
as_exp *expr = NULL; \
as_exp *expr_p = expr; \
if (py___policy != NULL) { \
Expand Down Expand Up @@ -239,7 +241,7 @@ static PyObject *AerospikeClient_BatchWriteInvoke(AerospikeClient *self,
!PyList_Size(py_ops_list)) {

// batch Read can have None ops if it is using read_all_bins
if ((batch_type == BATCH_TYPE_READ && py_ops_list != Py_None) ||
if ((batch_type == BATCH_TYPE_READ && !Py_IsNone(py_ops_list)) ||
batch_type == BATCH_TYPE_WRITE) {
as_error_update(err, AEROSPIKE_ERR_PARAM,
"py_ops_list is NULL or not a list, %s must be "
Expand All @@ -263,7 +265,7 @@ static PyObject *AerospikeClient_BatchWriteInvoke(AerospikeClient *self,
}

Py_ssize_t py_ops_size = 0;
if (py_ops_list != NULL && py_ops_list != Py_None) {
if (py_ops_list != NULL && !Py_IsNone(py_ops_list)) {
py_ops_size = PyList_Size(py_ops_list);
}

Expand All @@ -272,7 +274,7 @@ static PyObject *AerospikeClient_BatchWriteInvoke(AerospikeClient *self,

as_operations *ops = NULL;
if ((batch_type == AS_BATCH_READ || batch_type == AS_BATCH_WRITE) &&
(py_ops_size || (py_meta != NULL && py_meta != Py_None))) {
(py_ops_size || (py_meta != NULL && !Py_IsNone(py_meta)))) {

ops = as_operations_new(py_ops_size);
garb->ops_to_free = ops;
Expand Down
4 changes: 3 additions & 1 deletion src/main/client/cdt_operation_utils.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "pythoncapi_compat.h"

#include <Python.h>

#include "cdt_operation_utils.h"
Expand Down Expand Up @@ -83,7 +85,7 @@ as_status get_asval(AerospikeClient *self, as_error *err, char *key,
}

/* If the value isn't required, None indicates that it isn't provided */
if (py_val == Py_None && !required) {
if (Py_IsNone(py_val) && !required) {
*val = NULL;
return AEROSPIKE_OK;
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/client/operate.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,15 +174,15 @@ PyObject *create_pylist(PyObject *py_list, long operation, PyObject *py_bin,
int check_type(AerospikeClient *self, PyObject *py_value, int op, as_error *err)
{
if ((!PyLong_Check(py_value) &&
strcmp(py_value->ob_type->tp_name, "aerospike.null")) &&
strcmp(Py_TYPE(py_value)->tp_name, "aerospike.null")) &&
(op == AS_OPERATOR_TOUCH)) {
as_error_update(
err, AEROSPIKE_ERR_PARAM,
"Unsupported operand type(s) for touch : only int or long allowed");
return 1;
}
else if ((!PyLong_Check(py_value) && (!PyFloat_Check(py_value)) &&
strcmp(py_value->ob_type->tp_name, "aerospike.null")) &&
strcmp(Py_TYPE(py_value)->tp_name, "aerospike.null")) &&
op == AS_OPERATOR_INCR) {
as_error_update(
err, AEROSPIKE_ERR_PARAM,
Expand All @@ -191,7 +191,7 @@ int check_type(AerospikeClient *self, PyObject *py_value, int op, as_error *err)
}
else if ((!PyUnicode_Check(py_value) && !PyByteArray_Check(py_value) &&
!PyBytes_Check(py_value) &&
strcmp(py_value->ob_type->tp_name, "aerospike.null")) &&
strcmp(Py_TYPE(py_value)->tp_name, "aerospike.null")) &&
(op == AS_OPERATOR_APPEND || op == AS_OPERATOR_PREPEND)) {
as_error_update(err, AEROSPIKE_ERR_PARAM,
"Cannot concatenate 'str' and 'non-str' objects");
Expand Down Expand Up @@ -572,7 +572,7 @@ as_status add_op(AerospikeClient *self, as_error *err, PyObject *py_val,
}
else {
if (!self->strict_types ||
!strcmp(py_value->ob_type->tp_name, "aerospike.null")) {
!strcmp(Py_TYPE(py_value)->tp_name, "aerospike.null")) {
as_operations *pointer_ops = ops;
as_binop *binop =
&pointer_ops->binops.entries[pointer_ops->binops.size++];
Expand Down Expand Up @@ -605,7 +605,7 @@ as_status add_op(AerospikeClient *self, as_error *err, PyObject *py_val,
}
else {
if (!self->strict_types ||
!strcmp(py_value->ob_type->tp_name, "aerospike.null")) {
!strcmp(Py_TYPE(py_value)->tp_name, "aerospike.null")) {
as_operations *pointer_ops = ops;
as_binop *binop =
&pointer_ops->binops.entries[pointer_ops->binops.size++];
Expand All @@ -632,7 +632,7 @@ as_status add_op(AerospikeClient *self, as_error *err, PyObject *py_val,
}
else {
if (!self->strict_types ||
!strcmp(py_value->ob_type->tp_name, "aerospike.null")) {
!strcmp(Py_TYPE(py_value)->tp_name, "aerospike.null")) {
as_operations *pointer_ops = ops;
as_binop *binop =
&pointer_ops->binops.entries[pointer_ops->binops.size++];
Expand Down
4 changes: 3 additions & 1 deletion src/main/client/query.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "pythoncapi_compat.h"

/*******************************************************************************
* Copyright 2013-2021 Aerospike, Inc.
*
Expand Down Expand Up @@ -171,7 +173,7 @@ static int query_where_add(as_query **query, as_predicate_type predicate,
return 1;
}

if (py_val1 == Py_None || py_val2 == Py_None) {
if (Py_IsNone(py_val1) || Py_IsNone(py_val2)) {
Py_XDECREF(py_ubin);
as_error_update(
err, AEROSPIKE_ERR_PARAM,
Expand Down
4 changes: 3 additions & 1 deletion src/main/client/sec_index.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "pythoncapi_compat.h"

/*******************************************************************************
* Copyright 2013-2021 Aerospike, Inc.
*
Expand Down Expand Up @@ -580,7 +582,7 @@ static PyObject *createIndexWithDataAndCollectionType(
py_ustr_set = PyUnicode_AsUTF8String(py_set);
set_ptr = PyBytes_AsString(py_ustr_set);
}
else if (py_set != Py_None) {
else if (!Py_IsNone(py_set)) {
as_error_update(&err, AEROSPIKE_ERR_PARAM,
"Set should be string, unicode or None");
goto CLEANUP;
Expand Down
4 changes: 3 additions & 1 deletion src/main/client/set_xdr_filter.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "pythoncapi_compat.h"

/*******************************************************************************
* Copyright 2013-2021 Aerospike, Inc.
*
Expand Down Expand Up @@ -91,7 +93,7 @@ PyObject *AerospikeClient_SetXDRFilter(AerospikeClient *self, PyObject *args,
}

//convert filter to base64
if (py_expression_filter == Py_None) {
if (Py_IsNone(py_expression_filter)) {
base64_filter = (char *)DELETE_CURRENT_XDR_FILTER;
}
else {
Expand Down
4 changes: 3 additions & 1 deletion src/main/client/truncate.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "pythoncapi_compat.h"

/*******************************************************************************
* Copyright 2013-2021 Aerospike, Inc.
*
Expand Down Expand Up @@ -108,7 +110,7 @@ PyObject *AerospikeClient_Truncate(AerospikeClient *self, PyObject *args,
goto CLEANUP;
}
}
else if (py_set != Py_None) {
else if (!Py_IsNone(py_set)) {
// If the set is none, this is fine
as_error_update(&err, AEROSPIKE_ERR_PARAM,
"Set must be None, or unicode or string type");
Expand Down
8 changes: 5 additions & 3 deletions src/main/client/type.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "pythoncapi_compat.h"

/*******************************************************************************
* Copyright 2013-2021 Aerospike, Inc.
*
Expand Down Expand Up @@ -733,7 +735,7 @@ static int AerospikeClient_Type_Init(AerospikeClient *self, PyObject *args,
PyDict_GetItemString(py_config, "serialization");
if (py_serializer_option && PyTuple_Check(py_serializer_option)) {
PyObject *py_serializer = PyTuple_GetItem(py_serializer_option, 0);
if (py_serializer && py_serializer != Py_None) {
if (py_serializer && !Py_IsNone(py_serializer)) {
if (!PyCallable_Check(py_serializer)) {
error_code = INIT_SERIALIZE_ERR;
goto CONSTRUCTOR_ERROR;
Expand All @@ -743,7 +745,7 @@ static int AerospikeClient_Type_Init(AerospikeClient *self, PyObject *args,
self->user_serializer_call_info.callback = py_serializer;
}
PyObject *py_deserializer = PyTuple_GetItem(py_serializer_option, 1);
if (py_deserializer && py_deserializer != Py_None) {
if (py_deserializer && !Py_IsNone(py_deserializer)) {
if (!PyCallable_Check(py_deserializer)) {
error_code = INIT_DESERIALIZE_ERR;
goto CONSTRUCTOR_ERROR;
Expand Down Expand Up @@ -1282,7 +1284,7 @@ static void AerospikeClient_Type_Dealloc(PyObject *self)
}
}
}
self->ob_type->tp_free((PyObject *)self);
Py_TYPE(self)->tp_free((PyObject *)self);
}

/*******************************************************************************
Expand Down
30 changes: 16 additions & 14 deletions src/main/conversions.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include "pythoncapi_compat.h"

/*******************************************************************************
* Copyright 2013-2021 Aerospike, Inc.
*
Expand Down Expand Up @@ -1065,13 +1067,13 @@ bool is_pyobj_correct_as_helpers_type(PyObject *obj,
const char *expected_submodule_name,
const char *expected_type_name)
{
if (strcmp(obj->ob_type->tp_name, expected_type_name)) {
if (strcmp(Py_TYPE(obj)->tp_name, expected_type_name)) {
// Expected class name does not match object's class name
return false;
}

PyObject *py_module_name =
PyDict_GetItemString(obj->ob_type->tp_dict, "__module__");
PyDict_GetItemString(Py_TYPE(obj)->tp_dict, "__module__");
if (!py_module_name) {
// Class does not belong to any module
return false;
Expand Down Expand Up @@ -1195,7 +1197,7 @@ as_status pyobject_to_val(AerospikeClient *self, as_error *err,
bytes->type = AS_BYTES_HLL;
}
}
else if (!strcmp(py_obj->ob_type->tp_name, "aerospike.Geospatial")) {
else if (!strcmp(Py_TYPE(py_obj)->tp_name, "aerospike.Geospatial")) {
PyObject *py_parameter = PyUnicode_FromString("geo_data");
PyObject *py_data = PyObject_GenericGetAttr(py_obj, py_parameter);
Py_DECREF(py_parameter);
Expand Down Expand Up @@ -1236,7 +1238,7 @@ as_status pyobject_to_val(AerospikeClient *self, as_error *err,
else if (Py_None == py_obj) {
*val = as_val_reserve(&as_nil);
}
else if (!strcmp(py_obj->ob_type->tp_name, "aerospike.null")) {
else if (!strcmp(Py_TYPE(py_obj)->tp_name, "aerospike.null")) {
*val = (as_val *)as_val_reserve(&as_nil);
}
else if (AS_Matches_Classname(py_obj, AS_CDT_WILDCARD_NAME)) {
Expand Down Expand Up @@ -1325,7 +1327,7 @@ as_status pyobject_to_record(AerospikeClient *self, as_error *err,
value)) { //TODO Change to true bool support post jump version.
switch (self->send_bool_as) {
case SEND_BOOL_AS_AS_BOOL:;
bool converted_value = (value == Py_True);
bool converted_value = (Py_IsTrue(value));
ret_val = as_record_set_bool(rec, name, converted_value);
break;
case SEND_BOOL_AS_INTEGER:;
Expand Down Expand Up @@ -1353,7 +1355,7 @@ as_status pyobject_to_record(AerospikeClient *self, as_error *err,
}
ret_val = as_record_set_int64(rec, name, val);
}
else if (!strcmp(value->ob_type->tp_name, "aerospike.Geospatial")) {
else if (!strcmp(Py_TYPE(value)->tp_name, "aerospike.Geospatial")) {
PyObject *py_geo_string = PyUnicode_FromString("geo_data");
PyObject *py_data =
PyObject_GenericGetAttr(value, py_geo_string);
Expand Down Expand Up @@ -1436,7 +1438,7 @@ as_status pyobject_to_record(AerospikeClient *self, as_error *err,
}
ret_val = as_record_set_map(rec, name, map);
}
else if (!strcmp(value->ob_type->tp_name, "aerospike.null")) {
else if (!strcmp(Py_TYPE(value)->tp_name, "aerospike.null")) {
ret_val = as_record_set_nil(rec, name);
}
else {
Expand Down Expand Up @@ -1467,7 +1469,7 @@ as_status pyobject_to_record(AerospikeClient *self, as_error *err,
}
Py_END_CRITICAL_SECTION();

if (py_meta && py_meta != Py_None) {
if (py_meta && !Py_IsNone(py_meta)) {
if (!PyDict_Check(py_meta)) {
as_error_update(err, AEROSPIKE_ERR_PARAM,
"meta must be a dictionary");
Expand Down Expand Up @@ -1586,7 +1588,7 @@ as_status pyobject_to_key(as_error *err, PyObject *py_keytuple, as_key *key)
ns = (char *)PyUnicode_AsUTF8(py_ns);
}

if (py_set && py_set != Py_None) {
if (py_set && !Py_IsNone(py_set)) {
if (PyUnicode_Check(py_set)) {
set = (char *)PyUnicode_AsUTF8(py_set);
}
Expand All @@ -1598,7 +1600,7 @@ as_status pyobject_to_key(as_error *err, PyObject *py_keytuple, as_key *key)

as_key *returnResult = key;

if (py_key && py_key != Py_None) {
if (py_key && !Py_IsNone(py_key)) {
if (PyUnicode_Check(py_key)) {
PyObject *py_ustr = PyUnicode_AsUTF8String(py_key);
char *k = PyBytes_AsString(py_ustr);
Expand Down Expand Up @@ -1638,7 +1640,7 @@ as_status pyobject_to_key(as_error *err, PyObject *py_keytuple, as_key *key)
as_error_update(err, AEROSPIKE_ERR_PARAM, "key is invalid");
}
}
else if (py_digest && py_digest != Py_None) {
else if (py_digest && !Py_IsNone(py_digest)) {
if (PyByteArray_Check(py_digest)) {
uint32_t sz = (uint32_t)PyByteArray_Size(py_digest);

Expand Down Expand Up @@ -2462,7 +2464,7 @@ void initialize_bin_for_strictypes(AerospikeClient *self, as_error *err,
((as_val *)&binop_bin->value)->type = AS_UNKNOWN;
binop_bin->valuep = (as_bin_value *)map;
}
else if (!strcmp(py_value->ob_type->tp_name, "aerospike.Geospatial")) {
else if (!strcmp(Py_TYPE(py_value)->tp_name, "aerospike.Geospatial")) {
PyObject *geo_data = PyObject_GetAttrString(py_value, "geo_data");
PyObject *geo_data_py_str = AerospikeGeospatial_DoDumps(geo_data, err);
const char *geo_data_str = PyUnicode_AsUTF8(geo_data_py_str);
Expand All @@ -2479,7 +2481,7 @@ void initialize_bin_for_strictypes(AerospikeClient *self, as_error *err,
Py_XDECREF(geo_data_py_str);
Py_XDECREF(geo_data);
}
else if (!strcmp(py_value->ob_type->tp_name, "aerospike.null")) {
else if (!strcmp(Py_TYPE(py_value)->tp_name, "aerospike.null")) {
((as_val *)&binop_bin->value)->type = AS_UNKNOWN;
binop_bin->valuep = (as_bin_value *)&as_nil;
}
Expand Down Expand Up @@ -2597,7 +2599,7 @@ as_status check_and_set_meta(PyObject *py_meta, as_operations *ops,
ops->gen = gen;
}
}
else if (py_meta && (py_meta != Py_None)) {
else if (py_meta && (!Py_IsNone(py_meta))) {
return as_error_update(err, AEROSPIKE_ERR_PARAM,
"Metadata should be of type dictionary");
}
Expand Down
Loading

0 comments on commit 2ea9a9a

Please sign in to comment.