forked from jupyter-xeus/xeus-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxinput.cpp
67 lines (55 loc) · 2.39 KB
/
xinput.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/***************************************************************************
* Copyright (c) 2018, Martin Renou, Johan Mabille, Sylvain Corlay, and *
* Wolf Vollprecht *
* Copyright (c) 2018, QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include <stdexcept>
#include <string>
#include "xeus/xinterpreter.hpp"
#include "xeus/xinput.hpp"
#include "pybind11/functional.h"
#include "pybind11/pybind11.h"
#include "xinput.hpp"
#include "xeus-python/xutils.hpp"
namespace py = pybind11;
namespace xpyt
{
std::string cpp_input(const std::string& prompt)
{
return xeus::blocking_input_request(prompt, false);
}
std::string cpp_getpass(const std::string& prompt)
{
return xeus::blocking_input_request(prompt, true);
}
void notimplemented(const std::string&)
{
throw std::runtime_error("This frontend does not support input requests");
}
input_redirection::input_redirection(bool allow_stdin)
{
// Forward input()
py::module builtins = py::module::import("builtins");
m_sys_input = builtins.attr("input");
builtins.attr("input") = allow_stdin ? py::cpp_function(&cpp_input, py::arg("prompt") = "")
: py::cpp_function(¬implemented, py::arg("prompt") = "");
// Forward getpass()
py::module getpass = py::module::import("getpass");
m_sys_getpass = getpass.attr("getpass");
getpass.attr("getpass") = allow_stdin ? py::cpp_function(&cpp_getpass, py::arg("prompt") = "")
: py::cpp_function(¬implemented, py::arg("prompt") = "");
}
input_redirection::~input_redirection()
{
// Restore input()
py::module builtins = py::module::import("builtins");
builtins.attr("input") = m_sys_input;
// Restore getpass()
py::module getpass = py::module::import("getpass");
getpass.attr("getpass") = m_sys_getpass;
}
}