Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CLIENT-2760] Allow importing aerospike.exception and aerospike.predicates #573

Draft
wants to merge 13 commits into
base: dev
Choose a base branch
from
3 changes: 3 additions & 0 deletions aerospike/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# aerospike extension module will be stored in this folder (i.e package)
# Same with the exception and predicates extension modules
from .aerospike import *
37 changes: 35 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def clean():
ext_modules=[
Extension(
# Extension Name
'aerospike',
'aerospike.aerospike',

# Source Files
[
Expand Down Expand Up @@ -350,6 +350,38 @@ def clean():
include_dirs=include_dirs,
extra_compile_args=extra_compile_args,

# Link
library_dirs=library_dirs,
libraries=libraries,
extra_objects=extra_objects,
extra_link_args=extra_link_args,
),
Extension(
name="aerospike.exception",
sources=[
'src/main/exception.c',
],
# Compile
include_dirs=include_dirs,
extra_compile_args=extra_compile_args,

# Link
library_dirs=library_dirs,
libraries=libraries,
extra_objects=extra_objects,
extra_link_args=extra_link_args,
),
Extension(
name="aerospike.predicates",
sources=[
'src/main/predicates.c',
'src/main/exception.c',
'src/main/geospatial/dumps.c'
],
# Compile
include_dirs=include_dirs,
extra_compile_args=extra_compile_args,

# Link
library_dirs=library_dirs,
libraries=libraries,
Expand All @@ -367,7 +399,8 @@ def clean():
},
packages=['aerospike_helpers', 'aerospike_helpers.operations', 'aerospike_helpers.batch',
'aerospike_helpers.expressions',
'aerospike-stubs'],
'aerospike-stubs',
'aerospike'],
cmdclass={
'build': CClientBuild,
'clean': CClientClean
Expand Down
8 changes: 7 additions & 1 deletion src/include/exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@

#include <Python.h>

PyObject *AerospikeException_New(void);
void raise_exception(as_error *err);
PyObject *raise_exception_old(as_error *err);
void remove_exception(as_error *err);
void error_to_pyobject(const as_error *err, PyObject **obj);

#define PY_EXCEPTION_CODE 0
#define PY_EXCEPTION_MSG 1
#define PY_EXCEPTION_FILE 2
#define PY_EXCEPTION_LINE 3
#define AS_PY_EXCEPTION_IN_DOUBT 4
3 changes: 1 addition & 2 deletions src/include/geo.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "types.h"
#include "client.h"
#include "geo_dumps.h"

/*******************************************************************************
* FUNCTIONS
Expand All @@ -43,8 +44,6 @@ PyObject *AerospikeGeospatial_Dumps(AerospikeGeospatial *self, PyObject *args,
void store_geodata(AerospikeGeospatial *self, as_error *err,
PyObject *py_geodata);

PyObject *AerospikeGeospatial_DoDumps(PyObject *geo_data, as_error *err);

PyObject *AerospikeGeospatial_DoLoads(PyObject *py_geodata, as_error *err);

AerospikeGeospatial *Aerospike_Set_Geo_Data(PyObject *parent, PyObject *args,
Expand Down
5 changes: 5 additions & 0 deletions src/include/geo_dumps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <Python.h>

#include <aerospike/as_error.h>

PyObject *AerospikeGeospatial_DoDumps(PyObject *geo_data, as_error *err);
21 changes: 0 additions & 21 deletions src/include/predicates.h

This file was deleted.

23 changes: 2 additions & 21 deletions src/main/aerospike.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include "geo.h"
#include "scan.h"
#include "key_ordered_dict.h"
#include "predicates.h"
#include "exceptions.h"
#include "policy.h"
#include "log.h"
Expand Down Expand Up @@ -121,12 +120,10 @@ struct Aerospike_State {

static int Aerospike_Clear(PyObject *aerospike)
{
Py_CLEAR(Aerospike_State(aerospike)->exception);
Py_CLEAR(Aerospike_State(aerospike)->client);
Py_CLEAR(Aerospike_State(aerospike)->query);
Py_CLEAR(Aerospike_State(aerospike)->scan);
Py_CLEAR(Aerospike_State(aerospike)->kdict);
Py_CLEAR(Aerospike_State(aerospike)->predicates);
Py_CLEAR(Aerospike_State(aerospike)->geospatial);
Py_CLEAR(Aerospike_State(aerospike)->null_object);
Py_CLEAR(Aerospike_State(aerospike)->wildcard_object);
Expand All @@ -144,7 +141,7 @@ PyMODINIT_FUNC PyInit_aerospike(void)
int i = 0;

static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT,
"aerospike",
"aerospike.aerospike",
"Aerospike Python Client",
sizeof(struct Aerospike_State),
Aerospike_Methods,
Expand All @@ -163,17 +160,9 @@ PyMODINIT_FUNC PyInit_aerospike(void)

PyModule_AddStringConstant(aerospike, "__version__", version);

PyObject *exception = AerospikeException_New();
Py_INCREF(exception);
int retval = PyModule_AddObject(aerospike, "exception", exception);
if (retval == -1) {
goto CLEANUP;
}
Aerospike_State(aerospike)->exception = exception;

PyTypeObject *client = AerospikeClient_Ready();
Py_INCREF(client);
retval = PyModule_AddObject(aerospike, "Client", (PyObject *)client);
int retval = PyModule_AddObject(aerospike, "Client", (PyObject *)client);
if (retval == -1) {
goto CLEANUP;
}
Expand Down Expand Up @@ -219,14 +208,6 @@ PyMODINIT_FUNC PyInit_aerospike(void)
declare_policy_constants(aerospike);
declare_log_constants(aerospike);

PyObject *predicates = AerospikePredicates_New();
Py_INCREF(predicates);
retval = PyModule_AddObject(aerospike, "predicates", predicates);
if (retval == -1) {
goto CLEANUP;
}
Aerospike_State(aerospike)->predicates = predicates;

PyTypeObject *geospatial = AerospikeGeospatial_Ready();
Py_INCREF(geospatial);
retval = PyModule_AddObject(aerospike, "GeoJSON", (PyObject *)geospatial);
Expand Down
40 changes: 0 additions & 40 deletions src/main/conversions.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,6 @@
#define PY_KEYT_KEY 2
#define PY_KEYT_DIGEST 3

#define PY_EXCEPTION_CODE 0
#define PY_EXCEPTION_MSG 1
#define PY_EXCEPTION_FILE 2
#define PY_EXCEPTION_LINE 3
#define AS_PY_EXCEPTION_IN_DOUBT 4

#define CTX_KEY "ctx"
#define CDT_CTX_ORDER_KEY "order_key"
#define CDT_CTX_PAD_KEY "pad_key"
Expand Down Expand Up @@ -2057,40 +2051,6 @@ as_status metadata_to_pyobject(as_error *err, const as_record *rec,
return err->code;
}

void error_to_pyobject(const as_error *err, PyObject **obj)
{
PyObject *py_file = NULL;
if (err->file) {
py_file = PyUnicode_FromString(err->file);
}
else {
Py_INCREF(Py_None);
py_file = Py_None;
}
PyObject *py_line = NULL;
if (err->line > 0) {
py_line = PyLong_FromLong(err->line);
}
else {
Py_INCREF(Py_None);
py_line = Py_None;
}

PyObject *py_code = PyLong_FromLongLong(err->code);
PyObject *py_message = PyUnicode_FromString(err->message);

PyObject *py_in_doubt = err->in_doubt ? Py_True : Py_False;
Py_INCREF(py_in_doubt);

PyObject *py_err = PyTuple_New(5);
PyTuple_SetItem(py_err, PY_EXCEPTION_CODE, py_code);
PyTuple_SetItem(py_err, PY_EXCEPTION_MSG, py_message);
PyTuple_SetItem(py_err, PY_EXCEPTION_FILE, py_file);
PyTuple_SetItem(py_err, PY_EXCEPTION_LINE, py_line);
PyTuple_SetItem(py_err, AS_PY_EXCEPTION_IN_DOUBT, py_in_doubt);
*obj = py_err;
}

void initialize_bin_for_strictypes(AerospikeClient *self, as_error *err,
PyObject *py_value, as_binop *binop,
char *bin, as_static_pool *static_pool)
Expand Down
38 changes: 35 additions & 3 deletions src/main/exception.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,14 @@
#include <aerospike/as_error.h>
#include <aerospike/as_status.h>

#include "conversions.h"
#include <string.h>
#include <stdlib.h>
#include "exceptions.h"
#include "exception_types.h"
#include "macros.h"

static PyObject *module;

PyObject *AerospikeException_New(void)
PyObject *PyInit_exception(void)
{
static struct PyModuleDef moduledef = {PyModuleDef_HEAD_INIT,
"aerospike.exception",
Expand Down Expand Up @@ -693,3 +691,37 @@ PyObject *raise_exception_old(as_error *err)
}
return py_value;
}

void error_to_pyobject(const as_error *err, PyObject **obj)
{
PyObject *py_file = NULL;
if (err->file) {
py_file = PyUnicode_FromString(err->file);
}
else {
Py_INCREF(Py_None);
py_file = Py_None;
}
PyObject *py_line = NULL;
if (err->line > 0) {
py_line = PyLong_FromLong(err->line);
}
else {
Py_INCREF(Py_None);
py_line = Py_None;
}

PyObject *py_code = PyLong_FromLongLong(err->code);
PyObject *py_message = PyUnicode_FromString(err->message);

PyObject *py_in_doubt = err->in_doubt ? Py_True : Py_False;
Py_INCREF(py_in_doubt);

PyObject *py_err = PyTuple_New(5);
PyTuple_SetItem(py_err, PY_EXCEPTION_CODE, py_code);
PyTuple_SetItem(py_err, PY_EXCEPTION_MSG, py_message);
PyTuple_SetItem(py_err, PY_EXCEPTION_FILE, py_file);
PyTuple_SetItem(py_err, PY_EXCEPTION_LINE, py_line);
PyTuple_SetItem(py_err, AS_PY_EXCEPTION_IN_DOUBT, py_in_doubt);
*obj = py_err;
}
41 changes: 0 additions & 41 deletions src/main/geospatial/dumps.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,9 @@
******************************************************************************/

#include <Python.h>
#include <stdbool.h>

#include <aerospike/as_arraylist.h>
#include <aerospike/as_error.h>

#include "client.h"
#include "conversions.h"
#include "exceptions.h"
#include "geo.h"
#include "policy.h"

PyObject *AerospikeGeospatial_DoDumps(PyObject *geo_data, as_error *err)
{
PyObject *initresult = NULL;
Expand Down Expand Up @@ -54,36 +46,3 @@ PyObject *AerospikeGeospatial_DoDumps(PyObject *geo_data, as_error *err)

return initresult;
}

PyObject *AerospikeGeospatial_Dumps(AerospikeGeospatial *self, PyObject *args,
PyObject *kwds)
{

PyObject *initresult = NULL;
// Aerospike error object
as_error err;
// Initialize error object
as_error_init(&err);

if (!self) {
as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid geospatial data");
goto CLEANUP;
}

initresult = AerospikeGeospatial_DoDumps(self->geo_data, &err);
if (!initresult) {
as_error_update(&err, AEROSPIKE_ERR_CLIENT,
"Unable to call dumps function");
goto CLEANUP;
}

CLEANUP:

// If an error occurred, tell Python.
if (err.code != AEROSPIKE_OK) {
raise_exception(&err);
return NULL;
}

return initresult;
}
33 changes: 33 additions & 0 deletions src/main/geospatial/type.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,3 +362,36 @@ PyObject *AerospikeGeospatial_New(as_error *err, PyObject *value)
Py_XINCREF(self->geo_data);
return (PyObject *)self;
}

PyObject *AerospikeGeospatial_Dumps(AerospikeGeospatial *self, PyObject *args,
PyObject *kwds)
{

PyObject *initresult = NULL;
// Aerospike error object
as_error err;
// Initialize error object
as_error_init(&err);

if (!self) {
as_error_update(&err, AEROSPIKE_ERR_PARAM, "Invalid geospatial data");
goto CLEANUP;
}

initresult = AerospikeGeospatial_DoDumps(self->geo_data, &err);
if (!initresult) {
as_error_update(&err, AEROSPIKE_ERR_CLIENT,
"Unable to call dumps function");
goto CLEANUP;
}

CLEANUP:

// If an error occurred, tell Python.
if (err.code != AEROSPIKE_OK) {
raise_exception(&err);
return NULL;
}

return initresult;
}