You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by is...@apache.org on 2025/01/24 09:50:30 UTC
(ignite-3) branch main updated: IGNITE-24297 DB API: Accept str value as argument for connect() (#5114)
This is an automated email from the ASF dual-hosted git repository.
isapego pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git
The following commit(s) were added to refs/heads/main by this push:
new 175fd9e5177 IGNITE-24297 DB API: Accept str value as argument for connect() (#5114)
175fd9e5177 is described below
commit 175fd9e5177ed4d689dcad9c14cb8fb5dfc0a832
Author: Igor Sapego <is...@apache.org>
AuthorDate: Fri Jan 24 13:50:24 2025 +0400
IGNITE-24297 DB API: Accept str value as argument for connect() (#5114)
---
modules/platforms/python/cpp_module/module.cpp | 34 +++---
modules/platforms/python/cpp_module/py_object.h | 134 ++++++++++++++++++++++++
modules/platforms/python/cpp_module/py_string.h | 73 +++++++++++++
modules/platforms/python/tests/test_connect.py | 34 ++++--
4 files changed, 256 insertions(+), 19 deletions(-)
diff --git a/modules/platforms/python/cpp_module/module.cpp b/modules/platforms/python/cpp_module/module.cpp
index f7646b24620..22ed9f8f451 100644
--- a/modules/platforms/python/cpp_module/module.cpp
+++ b/modules/platforms/python/cpp_module/module.cpp
@@ -17,6 +17,7 @@
#include "py_connection.h"
#include "py_cursor.h"
+#include "py_string.h"
#include "utils.h"
#include <ignite/odbc/sql_environment.h>
@@ -24,7 +25,6 @@
#include <ignite/common/detail/defer.h>
#include <memory>
-#include <cmath>
#include <Python.h>
@@ -91,28 +91,39 @@ static PyObject* pyignite3_connect(PyObject* self, PyObject* args, PyObject* kwa
auto item = PyList_GetItem(address, idx);
if (!PyUnicode_Check(item)) {
PyErr_SetString(py_get_module_interface_error_class(),
- "Only list of string values is allowed in 'address' parameter");
+ "Only a string or a list of strings are allowed in 'address' parameter");
return nullptr;
}
- auto str_array = PyUnicode_AsUTF8String(item);
- if (!str_array) {
+ auto item_str = py_string::try_from_py_utf8(item);
+ if (!item_str) {
PyErr_SetString(py_get_module_interface_error_class(), "Can not convert address string to UTF-8");
return nullptr;
}
- // To be called when the scope is left.
- auto str_array_guard = ignite::detail::defer([&] { Py_DECREF(str_array); });
- auto *data = PyBytes_AsString(str_array);
- auto len = PyBytes_Size(str_array);
- std::string_view view(data, len);
-
- address_builder << view;
+ address_builder << *item_str;
if ((idx + 1) < size) {
address_builder << ',';
}
}
+ } else if (PyUnicode_Check(address)) {
+ auto item_str = py_string::try_from_py_utf8(address);
+ if (!item_str) {
+ PyErr_SetString(py_get_module_interface_error_class(), "Can not convert address string to UTF-8");
+ return nullptr;
+ }
+ address_builder << *item_str;
+ } else {
+ PyErr_SetString(py_get_module_interface_error_class(),
+ "Only a string or a list of strings are allowed in 'address' parameter");
+ return nullptr;
+ }
+
+ auto addrs_str = address_builder.str();
+ if (addrs_str.empty()) {
+ PyErr_SetString(py_get_module_interface_error_class(), "No addresses provided to connect");
+ return nullptr;
}
using namespace ignite;
@@ -124,7 +135,6 @@ static PyObject* pyignite3_connect(PyObject* self, PyObject* args, PyObject* kwa
return nullptr;
configuration cfg;
- auto addrs_str = address_builder.str();
cfg.set_address(addrs_str);
if (schema)
diff --git a/modules/platforms/python/cpp_module/py_object.h b/modules/platforms/python/cpp_module/py_object.h
new file mode 100644
index 00000000000..77bf1e5c1fb
--- /dev/null
+++ b/modules/platforms/python/cpp_module/py_object.h
@@ -0,0 +1,134 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include <Python.h>
+
+/**
+ * Safer wrapper of PyObject.
+ */
+class py_object {
+public:
+ // Default
+ py_object() = default;
+
+ // Delete.
+ py_object(const py_object&) = delete;
+ py_object& operator=(const py_object&) = delete;
+
+ /**
+ * Move constructor.
+ *
+ * @param another Another Object.
+ */
+ py_object(py_object&& another) noexcept
+ : m_obj(another.m_obj) {
+ another.m_obj = nullptr;
+ }
+
+ /**
+ * Move operator.
+ *
+ * @param another Another Object.
+ * @return This.
+ */
+ py_object& operator=(py_object&& another) noexcept {
+ std::swap(m_obj, another.m_obj);
+ another.reset();
+
+ return *this;
+ }
+
+ /**
+ * Basic constructor.
+ *
+ * @param obj Python Object.
+ */
+ py_object(PyObject* obj) : m_obj(obj) {}
+
+ /**
+ * Destructor.
+ */
+ ~py_object() {
+ reset();
+ }
+
+ /**
+ * Check whether the object is valid.
+ */
+ bool is_valid() const { return m_obj != nullptr; }
+
+ /**
+ * Check whether the object is valid.
+ */
+ operator bool() const { return is_valid(); }
+
+ /**
+ * Change the value of the python object.
+ *
+ * @param obj New value.
+ */
+ void reset(PyObject* obj = nullptr) {
+ Py_XDECREF(m_obj);
+ m_obj = obj;
+ }
+
+ /**
+ * Get Pointer.
+ */
+ PyObject* get() { return m_obj; }
+
+ /**
+ * Get Pointer.
+ */
+ const PyObject* get() const { return m_obj; }
+
+private:
+ /** Reference to the python object. */
+ PyObject* m_obj{nullptr};
+};
+
+/**
+ * Class to be used to inherit the basic and safe behavior of py_object.
+ */
+class py_object_based {
+public:
+ py_object_based() = default;
+
+ /**
+ * Check whether the object is valid.
+ */
+ bool is_valid() const { return m_obj.is_valid(); }
+
+ /**
+ * Check whether the object is valid.
+ */
+ operator bool() const { return is_valid(); }
+
+protected:
+ /**
+ * Constructor.
+ *
+ * @param obj Object.
+ */
+ explicit py_object_based(py_object &&obj) : m_obj(std::move(obj)) {}
+
+ /** Object. */
+ py_object m_obj{};
+};
+
diff --git a/modules/platforms/python/cpp_module/py_string.h b/modules/platforms/python/cpp_module/py_string.h
new file mode 100644
index 00000000000..b45ff383366
--- /dev/null
+++ b/modules/platforms/python/cpp_module/py_string.h
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#pragma once
+
+#include "py_object.h"
+
+#include <string>
+
+#include <Python.h>
+
+class py_string : public py_object_based {
+public:
+ // Default
+ py_string() = default;
+
+ /**
+ * Make a string from python object.
+ *
+ * @param obj Python Object.
+ * @return A new instance of the class. Can be invalid if the object was not a valid utf-8 string.
+ *
+ * @warning Error is set if an invalid object is returned.
+ */
+ static py_string try_from_py_utf8(PyObject* obj) {
+ py_object val{PyUnicode_AsUTF8String(obj)};
+ if (!val)
+ return {};
+
+ return py_string{std::move(val)};
+ }
+
+ /**
+ * Get string data.
+ *
+ * @return String data.
+ */
+ std::string_view get_data() const { return m_data; }
+
+ /**
+ * Get string data.
+ *
+ * @return String data.
+ */
+ std::string_view operator*() const { return get_data(); }
+
+private:
+ /**
+ * Constructor.
+ *
+ * @param obj Object.
+ */
+ explicit py_string(py_object &&obj)
+ : py_object_based(std::move(obj))
+ , m_data(PyBytes_AsString(m_obj.get()), PyBytes_Size(m_obj.get())) {}
+
+ /** The actual string data. */
+ std::string_view m_data;
+};
diff --git a/modules/platforms/python/tests/test_connect.py b/modules/platforms/python/tests/test_connect.py
index 25b5805abd8..f0e251373aa 100644
--- a/modules/platforms/python/tests/test_connect.py
+++ b/modules/platforms/python/tests/test_connect.py
@@ -18,14 +18,16 @@ import pyignite3
from tests.util import server_addresses_invalid, server_addresses_basic
-def test_connection_success():
- conn = pyignite3.connect(address=server_addresses_basic, timeout=1)
+@pytest.mark.parametrize('address', [server_addresses_basic, server_addresses_basic[0]])
+def test_connection_success(address):
+ conn = pyignite3.connect(address=address, timeout=1)
assert conn is not None
conn.close()
-def test_connection_get_cursor():
- with pyignite3.connect(address=server_addresses_basic, timeout=1) as conn:
+@pytest.mark.parametrize('address', [server_addresses_basic, server_addresses_basic[0]])
+def test_connection_get_cursor(address):
+ with pyignite3.connect(address=address, timeout=1) as conn:
assert conn is not None
cursor = conn.cursor()
@@ -33,7 +35,25 @@ def test_connection_get_cursor():
cursor.close()
-def test_connection_fail():
+@pytest.mark.parametrize('address', [server_addresses_invalid, server_addresses_invalid[0]])
+def test_connection_fail(address):
with pytest.raises(pyignite3.OperationalError) as err:
- pyignite3.connect(address=server_addresses_invalid, timeout=1)
- assert err.match("Failed to establish connection with the host.")
+ pyignite3.connect(address=address, timeout=1)
+ assert err.match('Failed to establish connection with the host.')
+
+
+ERR_MSG_WRONG_TYPE = "Only a string or a list of strings are allowed in 'address' parameter"
+ERR_MSG_EMPTY = "No addresses provided to connect"
+
+@pytest.mark.parametrize('address,err_msg', [
+ (123, ERR_MSG_WRONG_TYPE),
+ ([123], ERR_MSG_WRONG_TYPE),
+ ([server_addresses_basic[0], 123], ERR_MSG_WRONG_TYPE),
+ ([], ERR_MSG_EMPTY),
+ ('', ERR_MSG_EMPTY),
+ ([''], ERR_MSG_EMPTY),
+])
+def test_connection_wrong_arg(address, err_msg):
+ with pytest.raises(pyignite3.InterfaceError) as err:
+ pyignite3.connect(address=address, timeout=1)
+ assert err.match(err_msg)