Skip to content

Commit

Permalink
Apply clang-format check for python wrapper (apache#13418)
Browse files Browse the repository at this point in the history
  • Loading branch information
BewareMyPower authored Dec 21, 2021
1 parent 6e3af7f commit 636d5b4
Show file tree
Hide file tree
Showing 14 changed files with 439 additions and 503 deletions.
2 changes: 2 additions & 0 deletions pulsar-client-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ add_custom_target(format python ${BUILD_SUPPORT_DIR}/run_clang_format.py
${CMAKE_SOURCE_DIR}/examples
${CMAKE_SOURCE_DIR}/tests
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/python/src
${CMAKE_SOURCE_DIR}/wireshark)

# `make check-format` option (for CI test)
Expand All @@ -466,4 +467,5 @@ add_custom_target(check-format python ${BUILD_SUPPORT_DIR}/run_clang_format.py
${CMAKE_SOURCE_DIR}/examples
${CMAKE_SOURCE_DIR}/tests
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/python/src
${CMAKE_SOURCE_DIR}/wireshark)
44 changes: 15 additions & 29 deletions pulsar-client-cpp/python/src/authentication.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,19 @@ AuthenticationWrapper::AuthenticationWrapper(const std::string& dynamicLibPath,
}

struct AuthenticationTlsWrapper : public AuthenticationWrapper {
AuthenticationTlsWrapper(const std::string& certificatePath, const std::string& privateKeyPath) :
AuthenticationWrapper() {
AuthenticationTlsWrapper(const std::string& certificatePath, const std::string& privateKeyPath)
: AuthenticationWrapper() {
this->auth = AuthTls::create(certificatePath, privateKeyPath);
}
};

struct TokenSupplierWrapper {
PyObject* _pySupplier;

TokenSupplierWrapper(py::object pySupplier) :
_pySupplier(pySupplier.ptr()) {
Py_XINCREF(_pySupplier);
}
TokenSupplierWrapper(py::object pySupplier) : _pySupplier(pySupplier.ptr()) { Py_XINCREF(_pySupplier); }

TokenSupplierWrapper(const TokenSupplierWrapper& other) {
_pySupplier= other._pySupplier;
_pySupplier = other._pySupplier;
Py_XINCREF(_pySupplier);
}

Expand All @@ -51,17 +48,15 @@ struct TokenSupplierWrapper {
return *this;
}

virtual ~TokenSupplierWrapper() {
Py_XDECREF(_pySupplier);
}
virtual ~TokenSupplierWrapper() { Py_XDECREF(_pySupplier); }

std::string operator()() {
PyGILState_STATE state = PyGILState_Ensure();

std::string token;
try {
token = py::call<std::string>(_pySupplier);
} catch(const py::error_already_set& e) {
} catch (const py::error_already_set& e) {
PyErr_Print();
}

Expand All @@ -70,10 +65,8 @@ struct TokenSupplierWrapper {
}
};


struct AuthenticationTokenWrapper : public AuthenticationWrapper {
AuthenticationTokenWrapper(py::object token) :
AuthenticationWrapper() {
AuthenticationTokenWrapper(py::object token) : AuthenticationWrapper() {
if (py::extract<std::string>(token).check()) {
// It's a string
std::string tokenStr = py::extract<std::string>(token);
Expand All @@ -86,38 +79,31 @@ struct AuthenticationTokenWrapper : public AuthenticationWrapper {
};

struct AuthenticationAthenzWrapper : public AuthenticationWrapper {
AuthenticationAthenzWrapper(const std::string& authParamsString) :
AuthenticationWrapper() {
AuthenticationAthenzWrapper(const std::string& authParamsString) : AuthenticationWrapper() {
this->auth = AuthAthenz::create(authParamsString);
}
};

struct AuthenticationOauth2Wrapper : public AuthenticationWrapper {
AuthenticationOauth2Wrapper(const std::string& authParamsString) :
AuthenticationWrapper() {
AuthenticationOauth2Wrapper(const std::string& authParamsString) : AuthenticationWrapper() {
this->auth = AuthOauth2::create(authParamsString);
}
};

void export_authentication() {
using namespace boost::python;

class_<AuthenticationWrapper>("Authentication", init<const std::string&, const std::string&>())
;
class_<AuthenticationWrapper>("Authentication", init<const std::string&, const std::string&>());

class_<AuthenticationTlsWrapper, bases<AuthenticationWrapper> >("AuthenticationTLS",
init<const std::string&, const std::string&>())
;
class_<AuthenticationTlsWrapper, bases<AuthenticationWrapper> >(
"AuthenticationTLS", init<const std::string&, const std::string&>());

class_<AuthenticationTokenWrapper, bases<AuthenticationWrapper> >("AuthenticationToken",
init<py::object>())
;
init<py::object>());

class_<AuthenticationAthenzWrapper, bases<AuthenticationWrapper> >("AuthenticationAthenz",
init<const std::string&>())
;
init<const std::string&>());

class_<AuthenticationOauth2Wrapper, bases<AuthenticationWrapper> >("AuthenticationOauth2",
init<const std::string&>())
;
init<const std::string&>());
}
63 changes: 27 additions & 36 deletions pulsar-client-cpp/python/src/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@ Producer Client_createProducer(Client& client, const std::string& topic, const P
Producer producer;
Result res;

Py_BEGIN_ALLOW_THREADS
res = client.createProducer(topic, conf, producer);
Py_BEGIN_ALLOW_THREADS res = client.createProducer(topic, conf, producer);
Py_END_ALLOW_THREADS

CHECK_RESULT(res);
CHECK_RESULT(res);
return producer;
}

Expand All @@ -35,11 +34,10 @@ Consumer Client_subscribe(Client& client, const std::string& topic, const std::s
Consumer consumer;
Result res;

Py_BEGIN_ALLOW_THREADS
res = client.subscribe(topic, subscriptionName, conf, consumer);
Py_BEGIN_ALLOW_THREADS res = client.subscribe(topic, subscriptionName, conf, consumer);
Py_END_ALLOW_THREADS

CHECK_RESULT(res);
CHECK_RESULT(res);
return consumer;
}

Expand All @@ -50,55 +48,50 @@ Consumer Client_subscribe_topics(Client& client, boost::python::list& topics,

std::vector<std::string> topics_vector;

for (int i = 0; i < len(topics); i ++) {
for (int i = 0; i < len(topics); i++) {
std::string content = boost::python::extract<std::string>(topics[i]);
topics_vector.push_back(content);
}

Py_BEGIN_ALLOW_THREADS
res = client.subscribe(topics_vector, subscriptionName, conf, consumer);
Py_BEGIN_ALLOW_THREADS res = client.subscribe(topics_vector, subscriptionName, conf, consumer);
Py_END_ALLOW_THREADS

CHECK_RESULT(res);
CHECK_RESULT(res);
return consumer;
}

Consumer Client_subscribe_pattern(Client& client, const std::string& topic_pattern, const std::string& subscriptionName,
const ConsumerConfiguration& conf) {
Consumer Client_subscribe_pattern(Client& client, const std::string& topic_pattern,
const std::string& subscriptionName, const ConsumerConfiguration& conf) {
Consumer consumer;
Result res;

Py_BEGIN_ALLOW_THREADS
res = client.subscribeWithRegex(topic_pattern, subscriptionName, conf, consumer);
Py_BEGIN_ALLOW_THREADS res = client.subscribeWithRegex(topic_pattern, subscriptionName, conf, consumer);
Py_END_ALLOW_THREADS

CHECK_RESULT(res);
CHECK_RESULT(res);
return consumer;
}

Reader Client_createReader(Client& client, const std::string& topic,
const MessageId& startMessageId,
Reader Client_createReader(Client& client, const std::string& topic, const MessageId& startMessageId,
const ReaderConfiguration& conf) {
Reader reader;
Result res;

Py_BEGIN_ALLOW_THREADS
res = client.createReader(topic, startMessageId, conf, reader);
Py_BEGIN_ALLOW_THREADS res = client.createReader(topic, startMessageId, conf, reader);
Py_END_ALLOW_THREADS

CHECK_RESULT(res);
CHECK_RESULT(res);
return reader;
}

boost::python::list Client_getTopicPartitions(Client& client, const std::string& topic) {
std::vector<std::string> partitions;
Result res;

Py_BEGIN_ALLOW_THREADS
res = client.getPartitionsForTopic(topic, partitions);
Py_BEGIN_ALLOW_THREADS res = client.getPartitionsForTopic(topic, partitions);
Py_END_ALLOW_THREADS

CHECK_RESULT(res);
CHECK_RESULT(res);

boost::python::list pyList;
for (int i = 0; i < partitions.size(); i++) {
Expand All @@ -111,24 +104,22 @@ boost::python::list Client_getTopicPartitions(Client& client, const std::string&
void Client_close(Client& client) {
Result res;

Py_BEGIN_ALLOW_THREADS
res = client.close();
Py_BEGIN_ALLOW_THREADS res = client.close();
Py_END_ALLOW_THREADS

CHECK_RESULT(res);
CHECK_RESULT(res);
}

void export_client() {
using namespace boost::python;

class_<Client>("Client", init<const std::string&, const ClientConfiguration& >())
.def("create_producer", &Client_createProducer)
.def("subscribe", &Client_subscribe)
.def("subscribe_topics", &Client_subscribe_topics)
.def("subscribe_pattern", &Client_subscribe_pattern)
.def("create_reader", &Client_createReader)
.def("get_topic_partitions", &Client_getTopicPartitions)
.def("close", &Client_close)
.def("shutdown", &Client::shutdown)
;
class_<Client>("Client", init<const std::string&, const ClientConfiguration&>())
.def("create_producer", &Client_createProducer)
.def("subscribe", &Client_subscribe)
.def("subscribe_topics", &Client_subscribe_topics)
.def("subscribe_pattern", &Client_subscribe_pattern)
.def("create_reader", &Client_createReader)
.def("get_topic_partitions", &Client_getTopicPartitions)
.def("close", &Client_close)
.def("shutdown", &Client::shutdown);
}
Loading

0 comments on commit 636d5b4

Please sign in to comment.