Skip to content

Commit 8dba75c

Browse files
martinRenouSylvainCorlay
authored andcommitted
Clean code (jupyter-xeus#43)
* Import right builtins module depending on the Python version * Use const& everywhere * Compile time Python version check
1 parent c901b07 commit 8dba75c

File tree

8 files changed

+52
-54
lines changed

8 files changed

+52
-54
lines changed

src/xcomm.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace nl = nlohmann;
2626

2727
namespace xpyt
2828
{
29-
xcomm::xcomm(py::args /*args*/, py::kwargs kwargs)
29+
xcomm::xcomm(const py::args& /*args*/, const py::kwargs& kwargs)
3030
: m_comm(target(kwargs), id(kwargs))
3131
{
3232
m_comm.open(
@@ -55,7 +55,7 @@ namespace xpyt
5555
return true;
5656
}
5757

58-
void xcomm::close(py::args /*args*/, py::kwargs kwargs)
58+
void xcomm::close(const py::args& /*args*/, const py::kwargs& kwargs)
5959
{
6060
m_comm.close(
6161
kwargs.attr("get")("metadata", py::dict()),
@@ -64,7 +64,7 @@ namespace xpyt
6464
);
6565
}
6666

67-
void xcomm::send(py::args /*args*/, py::kwargs kwargs)
67+
void xcomm::send(const py::args& /*args*/, const py::kwargs& kwargs)
6868
{
6969
m_comm.send(
7070
kwargs.attr("get")("metadata", py::dict()),
@@ -73,23 +73,23 @@ namespace xpyt
7373
);
7474
}
7575

76-
void xcomm::on_msg(python_callback_type callback)
76+
void xcomm::on_msg(const python_callback_type& callback)
7777
{
7878
m_comm.on_message(cpp_callback(callback));
7979
}
8080

81-
void xcomm::on_close(python_callback_type callback)
81+
void xcomm::on_close(const python_callback_type& callback)
8282
{
8383
m_comm.on_close(cpp_callback(callback));
8484
}
8585

86-
xeus::xtarget* xcomm::target(py::kwargs kwargs) const
86+
xeus::xtarget* xcomm::target(const py::kwargs& kwargs) const
8787
{
8888
std::string target_name = kwargs["target_name"].cast<std::string>();
8989
return xeus::get_interpreter().comm_manager().target(target_name);
9090
}
9191

92-
xeus::xguid xcomm::id(py::kwargs kwargs) const
92+
xeus::xguid xcomm::id(const py::kwargs& kwargs) const
9393
{
9494
if (py::hasattr(kwargs, "comm_id"))
9595
{
@@ -102,7 +102,7 @@ namespace xpyt
102102
}
103103
}
104104

105-
auto xcomm::cpp_callback(python_callback_type py_callback) const -> cpp_callback_type
105+
auto xcomm::cpp_callback(const python_callback_type& py_callback) const -> cpp_callback_type
106106
{
107107
return [this, py_callback](const xeus::xmessage& msg) {
108108
py_callback(cppmessage_to_pymessage(msg));
@@ -117,9 +117,9 @@ namespace xpyt
117117
{
118118
}
119119

120-
void register_target(py::str target_name, py::object callback)
120+
void register_target(const py::str& target_name, const py::object& callback)
121121
{
122-
auto target_callback = [target_name, callback] (xeus::xcomm&& comm, const xeus::xmessage& msg) {
122+
auto target_callback = [&callback] (xeus::xcomm&& comm, const xeus::xmessage& msg) {
123123
callback(xcomm(std::move(comm)), cppmessage_to_pymessage(msg));
124124
};
125125

src/xcomm.hpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,24 @@ namespace xpyt
3333
using cpp_callback_type = std::function<void(const xeus::xmessage&)>;
3434
using zmq_buffers_type = std::vector<zmq::message_t>;
3535

36-
xcomm(py::args args, py::kwargs kwargs);
36+
xcomm(const py::args& args, const py::kwargs& kwargs);
3737
xcomm(xeus::xcomm&& comm);
3838
xcomm(xcomm&& comm) = default;
3939
virtual ~xcomm();
4040

4141
std::string comm_id() const;
4242
bool kernel() const;
4343

44-
void close(py::args args, py::kwargs kwargs);
45-
void send(py::args args, py::kwargs kwargs);
46-
void on_msg(python_callback_type callback);
47-
void on_close(python_callback_type callback);
44+
void close(const py::args& args, const py::kwargs& kwargs);
45+
void send(const py::args& args, const py::kwargs& kwargs);
46+
void on_msg(const python_callback_type& callback);
47+
void on_close(const python_callback_type& callback);
4848

4949
private:
5050

51-
xeus::xtarget* target(py::kwargs kwargs) const;
52-
xeus::xguid id(py::kwargs kwargs) const;
53-
cpp_callback_type cpp_callback(python_callback_type callback) const;
51+
xeus::xtarget* target(const py::kwargs& kwargs) const;
52+
xeus::xguid id(const py::kwargs& kwargs) const;
53+
cpp_callback_type cpp_callback(const python_callback_type& callback) const;
5454

5555
xeus::xcomm m_comm;
5656
};

src/xdisplay.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace xpyt
3737
m_execution_count = execution_count;
3838
}
3939

40-
void xdisplayhook::operator()(py::object obj, bool raw = false)
40+
void xdisplayhook::operator()(const py::object& obj, bool raw = false) const
4141
{
4242
auto& interp = xeus::get_interpreter();
4343

@@ -67,7 +67,7 @@ namespace xpyt
6767
}
6868
}
6969

70-
nl::json display_pub_data(py::object obj)
70+
nl::json xdisplayhook::display_pub_data(const py::object& obj) const
7171
{
7272
py::module py_json = py::module::import("json");
7373
nl::json pub_data;

src/xdisplay.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ namespace xpyt
2828
virtual ~xdisplayhook();
2929

3030
void set_execution_count(int execution_count);
31-
void operator()(py::object obj, bool raw);
31+
void operator()(const py::object& obj, bool raw) const;
3232

3333
private:
3434

35+
nl::json display_pub_data(const py::object& obj) const;
36+
3537
int m_execution_count;
3638
};
37-
38-
nl::json display_pub_data(py::object obj);
3939
}
4040

4141
#endif

src/xinput.cpp

+14-23
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "pybind11/functional.h"
1818

1919
#include "xinput.hpp"
20-
#include "xpyt_config.hpp"
20+
#include "xutils.hpp"
2121

2222
namespace py = pybind11;
2323

@@ -68,37 +68,29 @@ namespace xpyt
6868
.def("getpass", getpass, py::arg("prompt") = "")
6969
.def("notimplemented", notimplemented, py::arg("prompt") = "");
7070

71-
if (PY_MAJOR_VERSION == 2)
72-
{
73-
m.def("raw_input", input, py::arg("prompt") = "");
74-
}
71+
#if PY_MAJOR_VERSION == 2
72+
m.def("raw_input", input, py::arg("prompt") = "");
73+
#endif
7574
}
7675

7776
// Implementation of input_redirection
7877

79-
#if PY_MAJOR_VERSION == 2
80-
#define XEUS_PYTHON_BUILTINS __builtin__
81-
#else
82-
#define XEUS_PYTHON_BUILTINS builtins
83-
#endif
84-
8578
input_redirection::input_redirection(bool allow_stdin)
8679
{
8780
py::module xeus_python_input = py::module::import("xeus_python_input");
8881

8982
// Forward input()
90-
py::module builtins = py::module::import(XPYT_STRINGIFY(XEUS_PYTHON_BUILTINS));
83+
py::module builtins = py::module::import(XPYT_BUILTINS);
9184
m_sys_input = builtins.attr("input");
9285
builtins.attr("input") = allow_stdin ? xeus_python_input.attr("input")
9386
: xeus_python_input.attr("notimplemented");
9487

88+
#if PY_MAJOR_VERSION == 2
9589
// Forward raw_input()
96-
if (PY_MAJOR_VERSION == 2)
97-
{
98-
m_sys_raw_input = builtins.attr("raw_input");
99-
builtins.attr("raw_input") = allow_stdin ? xeus_python_input.attr("raw_input")
100-
: xeus_python_input.attr("notimplemented");
101-
}
90+
m_sys_raw_input = builtins.attr("raw_input");
91+
builtins.attr("raw_input") = allow_stdin ? xeus_python_input.attr("raw_input")
92+
: xeus_python_input.attr("notimplemented");
93+
#endif
10294

10395
// Forward getpass()
10496
py::module getpass = py::module::import("getpass");
@@ -110,14 +102,13 @@ namespace xpyt
110102
input_redirection::~input_redirection()
111103
{
112104
// Restore input()
113-
py::module builtins = py::module::import(XPYT_STRINGIFY(XEUS_PYTHON_BUILTINS));
105+
py::module builtins = py::module::import(XPYT_BUILTINS);
114106
builtins.attr("input") = m_sys_input;
115107

108+
#if PY_MAJOR_VERSION == 2
116109
// Restore raw_input()
117-
if (PY_MAJOR_VERSION == 2)
118-
{
119-
builtins.attr("raw_input") = m_sys_raw_input;
120-
}
110+
builtins.attr("raw_input") = m_sys_raw_input;
111+
#endif
121112

122113
// Restore getpass()
123114
py::module getpass = py::module::import("getpass");

src/xinterpreter.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "pybind11/functional.h"
2020

2121
#include "xpyt_config.hpp"
22+
#include "xutils.hpp"
2223
#include "xdisplay.hpp"
2324
#include "xinput.hpp"
2425
#include "xinterpreter.hpp"
@@ -122,7 +123,7 @@ namespace xpyt
122123
{
123124
// Import AST ans builtins modules
124125
py::module ast = py::module::import("ast");
125-
py::module builtins = py::module::import("builtins");
126+
py::module builtins = py::module::import(XPYT_BUILTINS);
126127

127128
// Parse code to AST
128129
py::object code_ast = ast.attr("parse")(code, "<string>", "exec");

src/xutils.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ namespace xpyt
4848
return bufferlist;
4949
}
5050

51-
std::vector<zmq::message_t> pylist_to_zmq_buffers(py::list bufferlist)
51+
std::vector<zmq::message_t> pylist_to_zmq_buffers(const py::list& bufferlist)
5252
{
5353
std::vector<zmq::message_t> buffers;
5454
for (py::handle buffer : bufferlist)
@@ -130,7 +130,7 @@ namespace nlohmann
130130
}
131131
}
132132

133-
json to_json_impl(py::handle obj)
133+
json to_json_impl(const py::handle& obj)
134134
{
135135
if (obj.is_none())
136136
{
@@ -155,7 +155,7 @@ namespace nlohmann
155155
if (py::isinstance<py::tuple>(obj) || py::isinstance<py::list>(obj))
156156
{
157157
json out;
158-
for (py::handle value: obj)
158+
for (const py::handle& value: obj)
159159
{
160160
out.push_back(to_json_impl(value));
161161
}
@@ -164,7 +164,7 @@ namespace nlohmann
164164
if (py::isinstance<py::dict>(obj))
165165
{
166166
json out;
167-
for (py::handle key: obj)
167+
for (const py::handle& key: obj)
168168
{
169169
out[key.cast<std::string>()] = to_json_impl(obj[key]);
170170
}
@@ -179,7 +179,7 @@ namespace nlohmann
179179
return detail::from_json_impl(j);
180180
}
181181

182-
void adl_serializer<py::object>::to_json(json& j, py::object obj)
182+
void adl_serializer<py::object>::to_json(json& j, const py::object& obj)
183183
{
184184
j = detail::to_json_impl(obj);
185185
}

src/xutils.hpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,16 @@
2222
namespace py = pybind11;
2323
namespace nl = nlohmann;
2424

25+
#if PY_MAJOR_VERSION == 2
26+
#define XPYT_BUILTINS "__builtin__"
27+
#else
28+
#define XPYT_BUILTINS "builtins"
29+
#endif
30+
2531
namespace xpyt
2632
{
2733
py::list zmq_buffers_to_pylist(const std::vector<zmq::message_t>& buffers);
28-
std::vector<zmq::message_t> pylist_to_zmq_buffers(py::list bufferlist);
34+
std::vector<zmq::message_t> pylist_to_zmq_buffers(const py::list& bufferlist);
2935

3036
py::object cppmessage_to_pymessage(const xeus::xmessage& msg);
3137
}
@@ -36,7 +42,7 @@ namespace nlohmann
3642
struct adl_serializer<py::object>
3743
{
3844
static py::object from_json(const json& j);
39-
static void to_json(json& j, py::object obj);
45+
static void to_json(json& j, const py::object& obj);
4046
};
4147
}
4248

0 commit comments

Comments
 (0)