Skip to content

Commit 9e2e8c6

Browse files
committed
Address review comments
1 parent c806840 commit 9e2e8c6

2 files changed

Lines changed: 78 additions & 53 deletions

File tree

‎src/runtime/modules/BuiltinsModule.cpp‎

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,33 @@ PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interprete
7171
{
7272
auto *separator = PyString::create(" ").unwrap();
7373
auto *end = PyString::create("\n").unwrap();
74-
// TODO: handle error case?
75-
PyObject *file =
76-
PyObject::from(interpreter.get_imported_module(PyString::create("sys").unwrap())
77-
->symbol_table()
78-
->map()
79-
.at(String{ "stdout" }))
80-
.unwrap();
81-
// sys.stdout may be None when FILE* stdout isn't connected
82-
if (!file || file == py_none()) { return Ok(py_none()); }
74+
// an explicit file argument wins; file=None (or no file argument) falls
75+
// back to sys.stdout
76+
PyObject *file = nullptr;
77+
static const Value file_keyword = String{ "file" };
78+
if (kwargs) {
79+
if (auto it = kwargs->map().find(file_keyword); it != kwargs->map().end()) {
80+
auto file_ = PyObject::from(it->second);
81+
if (file_.is_err()) { return file_; }
82+
if (file_.unwrap() != py_none()) { file = file_.unwrap(); }
83+
}
84+
}
85+
if (!file) {
86+
// TODO: handle error case?
87+
file = PyObject::from(interpreter.get_imported_module(PyString::create("sys").unwrap())
88+
->symbol_table()
89+
->map()
90+
.at(String{ "stdout" }))
91+
.unwrap();
92+
// sys.stdout may be None when FILE* stdout isn't connected
93+
if (!file || file == py_none()) { return Ok(py_none()); }
94+
}
8395
// TODO: flush should be false, but for now we keep it as true, since there is no flush at
8496
// interpreter shutdown
8597
bool flush = true;
8698
if (kwargs) {
8799
static const Value separator_keyword = String{ "sep" };
88100
static const Value end_keyword = String{ "end" };
89-
static const Value file_keyword = String{ "file" };
90101
static const Value flush_keyword = String{ "flush" };
91102

92103
if (auto it = kwargs->map().find(separator_keyword); it != kwargs->map().end()) {
@@ -111,11 +122,6 @@ PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interprete
111122
}
112123
end = PyString::create(std::get<String>(maybe_str).s).unwrap();
113124
}
114-
if (auto it = kwargs->map().find(file_keyword); it != kwargs->map().end()) {
115-
auto file_ = PyObject::from(it->second);
116-
if (file_.is_err()) { return file_; }
117-
file = file_.unwrap();
118-
}
119125
if (auto it = kwargs->map().find(flush_keyword); it != kwargs->map().end()) {
120126
auto flush_ = PyObject::from(it->second);
121127
if (flush_.is_err()) { return flush_; }

‎src/runtime/modules/IOModule.cpp‎

Lines changed: 57 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -990,12 +990,17 @@ struct Buffered
990990
{
991991
if (auto err = check_initialized(); err.is_err()) return Err(err.unwrap_err());
992992

993-
if (n < -1) { return Err(value_error("read length must be non-negative or -1")); }
993+
if (n < 0) {
994+
// TODO: determine actual buffer size
995+
n = 4096;
996+
}
994997

995998
if (is_closed()) { return Err(value_error("read of closed file")); }
996999

1000+
if (n == 0) { return PyBytes::create(); }
1001+
9971002
const auto have = readahead();
998-
if (have > 0) { return static_cast<T *>(this)->readfast(n); }
1003+
if (have > 0) { return static_cast<T *>(this)->readfast(std::min<int64_t>(n, have)); }
9991004
Bytes b;
10001005
b.b.resize(n);
10011006
auto result = static_cast<T *>(this)->raw_read(std::span{ b.b.begin(), b.b.end() });
@@ -1150,8 +1155,8 @@ class BufferedReader
11501155
if (static_cast<int64_t>(n) <= current_size) {
11511156
ASSERT(buffer);
11521157
std::vector<std::byte> data;
1153-
data.resize(current_size);
1154-
buffer->sgetn(::bit_cast<char *>(data.begin().base()), current_size);
1158+
data.resize(n);
1159+
buffer->sgetn(::bit_cast<char *>(data.begin().base()), n);
11551160
return PyBytes::create(Bytes{ std::move(data) });
11561161
}
11571162
return Ok(py_none());
@@ -2207,16 +2212,14 @@ class FileIO : public RawIOBase
22072212
{
22082213
if (!m_filebuffer.is_open()) { return Err(value_error("I/O operation on closed file")); }
22092214

2210-
m_filebuffer.pubseekpos(0, std::ios::in);
2211-
22122215
// TODO: if (m_filestream.fail()) { TODO(); }
22132216
const auto initial_position = m_filebuffer.pubseekoff(0, std::ios::cur, std::ios::in);
22142217
if (initial_position == -1) { TODO(); }
22152218

22162219
const auto end_position = m_filebuffer.pubseekoff(0, std::ios::end, std::ios::in);
22172220
if (end_position == -1) { TODO(); }
22182221

2219-
m_filebuffer.pubseekpos(0, std::ios::in);
2222+
m_filebuffer.pubseekpos(initial_position, std::ios::in);
22202223
// TODO: if (m_filestream.fail()) { TODO(); }
22212224

22222225
const auto file_size = end_position - initial_position;
@@ -3118,45 +3121,61 @@ class TextIOWrapper : public TextIOBase
31183121
std::numeric_limits<size_t>::max());
31193122
if (result.is_err()) { return Err(result.unwrap_err()); }
31203123
auto [limit] = result.unwrap();
3124+
return readline_impl(limit);
3125+
}
31213126

3127+
PyResult<PyObject *> readline_impl(size_t limit)
3128+
{
31223129
if (auto result = writeflush(); result.is_err()) { return Err(result.unwrap_err()); }
31233130

31243131
// Lines in the input can end in \'\\n\', \'\\r\', or \'\\r\\n\'
31253132
auto is_line_end = [](std::string_view remaining) {
3126-
if (remaining.starts_with("\n") || remaining.starts_with("\r")) {
3127-
return 1;
3128-
} else if (remaining.starts_with("\r\n")) {
3129-
return 2;
3130-
}
3133+
if (remaining.starts_with("\r\n")) { return 2; }
3134+
if (remaining.starts_with("\n") || remaining.starts_with("\r")) { return 1; }
31313135
return 0;
31323136
};
3133-
while (!m_buffer_bytes.has_value() || m_buffer_bytes->b.empty()) {
3134-
auto chunk = read_chunk(0);
3135-
if (chunk.is_err()) { return Err(chunk.unwrap_err()); }
3136-
if (chunk.unwrap() == 0) { break; }
3137-
}
3138-
3139-
std::string_view remaining{ bit_cast<const char *>(m_buffer_bytes->b.data()) + m_position,
3140-
m_buffer_bytes->b.size() - m_position };
3141-
3142-
// TODO: use utf8 codepoints
3143-
const char *start = remaining.data();
3144-
size_t len = 0;
3145-
limit = std::min(limit, remaining.size());
3146-
for (; len < limit; ++len) {
3147-
if (auto chars = is_line_end(remaining)) {
3148-
// TODO: what if len becomes greater than limit due to \r\n?
3149-
len += chars;
3150-
break;
3137+
std::string line;
3138+
bool found_nl = false;
3139+
while (!found_nl && line.size() < limit) {
3140+
if (!m_buffer_bytes.has_value() || m_buffer_bytes->b.empty()) {
3141+
auto chunk = read_chunk(0);
3142+
if (chunk.is_err()) { return Err(chunk.unwrap_err()); }
3143+
if (!chunk.unwrap()) { break; }
3144+
}
3145+
3146+
// a trailing '\r' may be the first byte of a "\r\n" that straddles the
3147+
// chunk boundary; read ahead so is_line_end can see the full terminator
3148+
while (m_buffer_bytes->b.back() == std::byte{ '\r' }) {
3149+
auto chunk = read_chunk(0);
3150+
if (chunk.is_err()) { return Err(chunk.unwrap_err()); }
3151+
if (!chunk.unwrap()) { break; }
31513152
}
3152-
remaining = remaining.substr(1);
3153-
}
3154-
std::string line{ start, start + len };
3155-
std::rotate(
3156-
m_buffer_bytes->b.begin(), m_buffer_bytes->b.begin() + len, m_buffer_bytes->b.end());
3157-
m_position = 0;
3158-
m_buffer_bytes->b.resize(m_buffer_bytes->b.size() - line.size());
31593153

3154+
std::string_view remaining{ bit_cast<const char *>(m_buffer_bytes->b.data())
3155+
+ m_position,
3156+
m_buffer_bytes->b.size() - m_position };
3157+
3158+
// TODO: use utf8 codepoints
3159+
const char *start = remaining.data();
3160+
size_t len = 0;
3161+
const size_t budget = limit - line.size();
3162+
const size_t scan_limit = std::min(budget, remaining.size());
3163+
for (; len < scan_limit; ++len) {
3164+
if (auto chars = is_line_end(remaining)) {
3165+
// the terminator may extend past the caller's limit (e.g. a
3166+
// "\r\n" straddling it); consume only up to the limit and
3167+
// leave the rest buffered for the next call
3168+
len = std::min(len + chars, budget);
3169+
found_nl = true;
3170+
break;
3171+
}
3172+
remaining = remaining.substr(1);
3173+
}
3174+
line.insert(line.end(), start, start + len);
3175+
m_buffer_bytes->b.erase(
3176+
m_buffer_bytes->b.begin(), m_buffer_bytes->b.begin() + m_position + len);
3177+
m_position = 0;
3178+
}
31603179
return PyString::create(std::move(line));
31613180
}
31623181

@@ -3167,7 +3186,7 @@ class TextIOWrapper : public TextIOBase
31673186
if (result_.is_err()) { return result_; }
31683187
auto *result = result_.unwrap();
31693188
while (m_position < m_buffer_bytes->b.size()) {
3170-
auto line = readline(nullptr, nullptr);
3189+
auto line = readline_impl(std::numeric_limits<size_t>::max());
31713190
if (line.is_err()) { return line; }
31723191
if (auto appended = result->append(line.unwrap()); appended.is_err()) {
31733192
return Err(appended.unwrap_err());

0 commit comments

Comments
 (0)