-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpyiostream.cpp
50 lines (37 loc) · 962 Bytes
/
pyiostream.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
#include "pyiostream.h"
namespace pyiostream {
streambuf::streambuf(PyObject *s) {
m_stream_obj = s;
Py_XINCREF(m_stream_obj);
m_str = 0;
m_count = 0;
}
streambuf::~streambuf() {
Py_XDECREF(m_stream_obj);
Py_XDECREF(m_str);
}
std::streambuf::int_type streambuf::underflow() {
Py_XDECREF(m_str);
m_str = PyObject_CallMethod(m_stream_obj, "read", "L", 4096);
Py_XINCREF(m_str);
Py_ssize_t sz;
char *str = 0;
if (PyUnicode_Check(m_str)) {
str = (char *)PyUnicode_AsUTF8AndSize(m_str, &sz);
} else if (PyBytes_Check(m_str)) {
PyBytes_AsStringAndSize(m_str, &str, &sz);
} else {
fprintf(stdout, "Unknown\n");
}
if (sz == 0) {
return traits_type::eof();
} else {
setg(str, str, str+sz);
return traits_type::to_int_type(str[0]);
}
}
istream::istream(PyObject *stream) : std::istream(&m_buf), m_buf(stream) {
}
istream::~istream() {
}
}