From 9f0a97d2836f1abd9269455c7052e4cf417c2f89 Mon Sep 17 00:00:00 2001 From: depetrol Date: Sun, 21 Jul 2024 11:19:05 -0700 Subject: [PATCH 1/3] move custom serialize to pythontarget --- python/include/pythontarget.h | 23 ++++++++++++++ python/lib/pythontarget.c | 60 +++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/python/include/pythontarget.h b/python/include/pythontarget.h index a828e0689..f6dd9666e 100644 --- a/python/include/pythontarget.h +++ b/python/include/pythontarget.h @@ -185,6 +185,29 @@ PyObject* convert_C_action_to_py(void* action); */ PyObject* get_python_function(string module, string class, int instance_id, string func); +/** + * Load the Serializer class from package name + * @param package_name Name of the python package to load + * @return Initialized Serializer class + */ +PyObject* load_serializer(string package_name); + +/** + * Serialize Python object to a bytes object using external serializer + * @param obj The Python object to serialize + * @param custom_serializer The custom Serializer class + * @return Serialized Python bytes object + */ +PyObject* custom_serialize( PyObject* obj, PyObject* custom_serializer); + +/** + * Deserialize Python object from a bytes object using external serializer + * @param serialized_pyobject The serialized bytes Python object + * @param custom_serializer The custom Serializer class + * @return Deserialized Python object + */ +PyObject* custom_deserialize(PyObject* serialized_pyobject, PyObject* custom_serializer); + /* * The Python runtime will call this function to initialize the module. * The name of this function is dynamically generated to follow diff --git a/python/lib/pythontarget.c b/python/lib/pythontarget.c index 3a3e7b2b4..738a4ab7f 100644 --- a/python/lib/pythontarget.c +++ b/python/lib/pythontarget.c @@ -695,3 +695,63 @@ PyObject* get_python_function(string module, string class, int instance_id, stri PyGILState_Release(gstate); return Py_None; } + +/** + * Load the Serializer class from package name + * @param package_name Name of the python package to load + * @return Initialized Serializer class + */ +PyObject* load_serializer(string package_name) { + // import package_name + PyObject* pName = PyUnicode_DecodeFSDefault(package_name); + PyObject* pModule = PyImport_Import(pName); + Py_DECREF(pName); + if (PyErr_Occurred()) PyErr_Print(); + if (pModule == NULL) lf_print_error_and_exit("Could not load the custom serializer package '%s'.", package_name); + // Get the Serializer class + PyObject* SerializerClass = PyObject_GetAttrString(pModule, "Serializer"); + if (PyErr_Occurred()) PyErr_Print(); + if (SerializerClass == NULL) lf_print_error_and_exit("Could not find class 'Serializer' in module '%s'.", package_name); + // Instanciate and initialize Serializer class + PyObject* custom_serializer = PyObject_CallObject(SerializerClass, NULL); + if (PyErr_Occurred()) PyErr_Print(); + if (custom_serializer == NULL) lf_print_error_and_exit("Could not instantiate class 'Serializer' in module '%s'.", package_name); + lf_print_log("Successfully loaded custom serializer package '%s'.\n", package_name); + return custom_serializer; +} + +/** + * Serialize Python object to a bytes object using external serializer + * @param obj The Python object to serialize + * @param custom_serializer The custom Serializer class + * @return Serialized Python bytes object + */ +PyObject* custom_serialize( PyObject* obj, PyObject* custom_serializer) { + if (custom_serializer == NULL) lf_print_error_and_exit("Serializer is null."); + PyObject *serializer_serialize = PyObject_GetAttrString(custom_serializer, "serialize"); + PyObject *args = PyTuple_Pack(1, obj); + PyObject *serialized_pyobject = PyObject_CallObject(serializer_serialize, args); + Py_XDECREF(serializer_serialize); + Py_XDECREF(args); + if (PyErr_Occurred()) PyErr_Print(); + if (serialized_pyobject == NULL) lf_print_error_and_exit("Could not serialize object."); + return serialized_pyobject; +} + +/** + * Deserialize Python object from a bytes object using external serializer + * @param serialized_pyobject The serialized bytes Python object + * @param custom_serializer The custom Serializer class + * @return Deserialized Python object + */ +PyObject* custom_deserialize(PyObject* serialized_pyobject, PyObject* custom_serializer) { + if (custom_serializer == NULL) lf_print_error_and_exit("Serializer is null."); + PyObject *serializer_deserialize = PyObject_GetAttrString(custom_serializer, "deserialize"); + PyObject *args = PyTuple_Pack(1, serialized_pyobject); + PyObject *deserialized_obj = PyObject_CallObject(serializer_deserialize, args); + Py_XDECREF(serializer_deserialize); + Py_XDECREF(args); + if (PyErr_Occurred()) PyErr_Print(); + if (deserialized_obj == NULL) lf_print_error_and_exit("Could not deserialize deserialized_obj."); + return deserialized_obj; +} \ No newline at end of file From 2493f10288e22d8626bc3810d6adf3e4d1e80e11 Mon Sep 17 00:00:00 2001 From: depetrol Date: Sun, 21 Jul 2024 11:19:47 -0700 Subject: [PATCH 2/3] format --- python/include/pythontarget.h | 2 +- python/lib/pythontarget.c | 82 ++++++++++++++++++++--------------- 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/python/include/pythontarget.h b/python/include/pythontarget.h index f6dd9666e..b9bcefd15 100644 --- a/python/include/pythontarget.h +++ b/python/include/pythontarget.h @@ -198,7 +198,7 @@ PyObject* load_serializer(string package_name); * @param custom_serializer The custom Serializer class * @return Serialized Python bytes object */ -PyObject* custom_serialize( PyObject* obj, PyObject* custom_serializer); +PyObject* custom_serialize(PyObject* obj, PyObject* custom_serializer); /** * Deserialize Python object from a bytes object using external serializer diff --git a/python/lib/pythontarget.c b/python/lib/pythontarget.c index 738a4ab7f..8ff471048 100644 --- a/python/lib/pythontarget.c +++ b/python/lib/pythontarget.c @@ -702,22 +702,28 @@ PyObject* get_python_function(string module, string class, int instance_id, stri * @return Initialized Serializer class */ PyObject* load_serializer(string package_name) { - // import package_name - PyObject* pName = PyUnicode_DecodeFSDefault(package_name); - PyObject* pModule = PyImport_Import(pName); - Py_DECREF(pName); - if (PyErr_Occurred()) PyErr_Print(); - if (pModule == NULL) lf_print_error_and_exit("Could not load the custom serializer package '%s'.", package_name); - // Get the Serializer class - PyObject* SerializerClass = PyObject_GetAttrString(pModule, "Serializer"); - if (PyErr_Occurred()) PyErr_Print(); - if (SerializerClass == NULL) lf_print_error_and_exit("Could not find class 'Serializer' in module '%s'.", package_name); - // Instanciate and initialize Serializer class - PyObject* custom_serializer = PyObject_CallObject(SerializerClass, NULL); - if (PyErr_Occurred()) PyErr_Print(); - if (custom_serializer == NULL) lf_print_error_and_exit("Could not instantiate class 'Serializer' in module '%s'.", package_name); - lf_print_log("Successfully loaded custom serializer package '%s'.\n", package_name); - return custom_serializer; + // import package_name + PyObject* pName = PyUnicode_DecodeFSDefault(package_name); + PyObject* pModule = PyImport_Import(pName); + Py_DECREF(pName); + if (PyErr_Occurred()) + PyErr_Print(); + if (pModule == NULL) + lf_print_error_and_exit("Could not load the custom serializer package '%s'.", package_name); + // Get the Serializer class + PyObject* SerializerClass = PyObject_GetAttrString(pModule, "Serializer"); + if (PyErr_Occurred()) + PyErr_Print(); + if (SerializerClass == NULL) + lf_print_error_and_exit("Could not find class 'Serializer' in module '%s'.", package_name); + // Instanciate and initialize Serializer class + PyObject* custom_serializer = PyObject_CallObject(SerializerClass, NULL); + if (PyErr_Occurred()) + PyErr_Print(); + if (custom_serializer == NULL) + lf_print_error_and_exit("Could not instantiate class 'Serializer' in module '%s'.", package_name); + lf_print_log("Successfully loaded custom serializer package '%s'.\n", package_name); + return custom_serializer; } /** @@ -726,16 +732,19 @@ PyObject* load_serializer(string package_name) { * @param custom_serializer The custom Serializer class * @return Serialized Python bytes object */ -PyObject* custom_serialize( PyObject* obj, PyObject* custom_serializer) { - if (custom_serializer == NULL) lf_print_error_and_exit("Serializer is null."); - PyObject *serializer_serialize = PyObject_GetAttrString(custom_serializer, "serialize"); - PyObject *args = PyTuple_Pack(1, obj); - PyObject *serialized_pyobject = PyObject_CallObject(serializer_serialize, args); - Py_XDECREF(serializer_serialize); - Py_XDECREF(args); - if (PyErr_Occurred()) PyErr_Print(); - if (serialized_pyobject == NULL) lf_print_error_and_exit("Could not serialize object."); - return serialized_pyobject; +PyObject* custom_serialize(PyObject* obj, PyObject* custom_serializer) { + if (custom_serializer == NULL) + lf_print_error_and_exit("Serializer is null."); + PyObject* serializer_serialize = PyObject_GetAttrString(custom_serializer, "serialize"); + PyObject* args = PyTuple_Pack(1, obj); + PyObject* serialized_pyobject = PyObject_CallObject(serializer_serialize, args); + Py_XDECREF(serializer_serialize); + Py_XDECREF(args); + if (PyErr_Occurred()) + PyErr_Print(); + if (serialized_pyobject == NULL) + lf_print_error_and_exit("Could not serialize object."); + return serialized_pyobject; } /** @@ -745,13 +754,16 @@ PyObject* custom_serialize( PyObject* obj, PyObject* custom_serializer) { * @return Deserialized Python object */ PyObject* custom_deserialize(PyObject* serialized_pyobject, PyObject* custom_serializer) { - if (custom_serializer == NULL) lf_print_error_and_exit("Serializer is null."); - PyObject *serializer_deserialize = PyObject_GetAttrString(custom_serializer, "deserialize"); - PyObject *args = PyTuple_Pack(1, serialized_pyobject); - PyObject *deserialized_obj = PyObject_CallObject(serializer_deserialize, args); - Py_XDECREF(serializer_deserialize); - Py_XDECREF(args); - if (PyErr_Occurred()) PyErr_Print(); - if (deserialized_obj == NULL) lf_print_error_and_exit("Could not deserialize deserialized_obj."); - return deserialized_obj; + if (custom_serializer == NULL) + lf_print_error_and_exit("Serializer is null."); + PyObject* serializer_deserialize = PyObject_GetAttrString(custom_serializer, "deserialize"); + PyObject* args = PyTuple_Pack(1, serialized_pyobject); + PyObject* deserialized_obj = PyObject_CallObject(serializer_deserialize, args); + Py_XDECREF(serializer_deserialize); + Py_XDECREF(args); + if (PyErr_Occurred()) + PyErr_Print(); + if (deserialized_obj == NULL) + lf_print_error_and_exit("Could not deserialize deserialized_obj."); + return deserialized_obj; } \ No newline at end of file From 76ec485017ae682a7f00aa9bc5609410c638c703 Mon Sep 17 00:00:00 2001 From: depetrol Date: Mon, 29 Jul 2024 15:02:12 -0700 Subject: [PATCH 3/3] fix: remote duplicate comment in pythontarget.c --- python/lib/pythontarget.c | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/python/lib/pythontarget.c b/python/lib/pythontarget.c index 8ff471048..ae43959d3 100644 --- a/python/lib/pythontarget.c +++ b/python/lib/pythontarget.c @@ -696,11 +696,6 @@ PyObject* get_python_function(string module, string class, int instance_id, stri return Py_None; } -/** - * Load the Serializer class from package name - * @param package_name Name of the python package to load - * @return Initialized Serializer class - */ PyObject* load_serializer(string package_name) { // import package_name PyObject* pName = PyUnicode_DecodeFSDefault(package_name); @@ -726,12 +721,6 @@ PyObject* load_serializer(string package_name) { return custom_serializer; } -/** - * Serialize Python object to a bytes object using external serializer - * @param obj The Python object to serialize - * @param custom_serializer The custom Serializer class - * @return Serialized Python bytes object - */ PyObject* custom_serialize(PyObject* obj, PyObject* custom_serializer) { if (custom_serializer == NULL) lf_print_error_and_exit("Serializer is null."); @@ -747,12 +736,6 @@ PyObject* custom_serialize(PyObject* obj, PyObject* custom_serializer) { return serialized_pyobject; } -/** - * Deserialize Python object from a bytes object using external serializer - * @param serialized_pyobject The serialized bytes Python object - * @param custom_serializer The custom Serializer class - * @return Deserialized Python object - */ PyObject* custom_deserialize(PyObject* serialized_pyobject, PyObject* custom_serializer) { if (custom_serializer == NULL) lf_print_error_and_exit("Serializer is null.");