- Notifications
You must be signed in to change notification settings - Fork 6
runtime: support sys.stdout and sys.stderr and implement proper print#29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Regression test: closing a path-opened FileIO after reading must not crash. | ||
| # Previously FileIO.close() called ferror() on the underlying FILE* *after* | ||
| # closing it (which resets the pointer to NULL), segfaulting on ferror(NULL). | ||
| # This is the same path the import machinery uses to read a module's source. | ||
| # Run with cwd == integration/ (as the integration runner does). | ||
| DATA = "tests/file_io_data.txt" | ||
| # 1. read inside a `with` block -> __exit__ closes the file (the crashing path). | ||
| with open(DATA, "rb") as f: | ||
| data = f.read() | ||
| assert data == b"line1\nline2\n", data | ||
| # 2. explicit close, and close() must be idempotent (callable more than once). | ||
| g = open(DATA, "rb") | ||
| assert g.read() == b"line1\nline2\n" | ||
| g.close() | ||
| g.close() | ||
| print("file_io: ok") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| line1 | ||
| line2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -67,10 +67,20 @@ static PyModule *s_builtin_module = nullptr; | ||
| namespace { | ||
| PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interpreter &) | ||
| PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interpreter &interpreter) | ||
| { | ||
| std::string separator = " "; | ||
| std::string end = "\n"; | ||
| auto *separator = PyString::create(" ").unwrap(); | ||
| auto *end = PyString::create("\n").unwrap(); | ||
| // TODO: handle error case? | ||
| PyObject *file = | ||
| PyObject::from(interpreter.get_imported_module(PyString::create("sys").unwrap()) | ||
| ->symbol_table() | ||
| ->map() | ||
| .at(String{ "stdout" })) | ||
| .unwrap(); | ||
| // sys.stdout may be None when FILE* stdout isn't connected | ||
| if (!file || file == py_none()) { return Ok(py_none()); } | ||
| bool flush = true; | ||
| if (kwargs) { | ||
| static const Value separator_keyword = String{ "sep" }; | ||
| static const Value end_keyword = String{ "end" }; | ||
| @@ -84,7 +94,7 @@ PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interprete | ||
| return Err(type_error( | ||
| "sep must be None or a string, not {}", obj.unwrap()->type()->name())); | ||
| } | ||
| separator = std::get<String>(maybe_str).s; | ||
| separator = PyString::create(std::get<String>(maybe_str).s).unwrap(); | ||
| } | ||
| if (auto it = kwargs->map().find(end_keyword); it != kwargs->map().end()) { | ||
| auto maybe_str = it->second; | ||
| @@ -95,9 +105,12 @@ PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interprete | ||
| return Err(type_error( | ||
| "end must be None or a string, not {}", obj.unwrap()->type()->name())); | ||
| } | ||
| end = std::get<String>(maybe_str).s; | ||
| end = PyString::create(std::get<String>(maybe_str).s).unwrap(); | ||
| } | ||
| } | ||
| auto file_write_ = file->get_method(PyString::create("write").unwrap()); | ||
| if (file_write_.is_err()) { return file_write_; } | ||
| auto file_write = file_write_.unwrap(); | ||
| auto strfunc = [](const PyResult<PyObject *> &arg) -> PyResult<PyString *> { | ||
| if (arg.is_err()) return Err(arg.unwrap_err()); | ||
| return arg.unwrap()->str(); | ||
| @@ -106,7 +119,16 @@ PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interprete | ||
| auto arg_it = args->begin(); | ||
| auto arg_it_end = args->end(); | ||
| if (arg_it == arg_it_end) { | ||
| std::cout << std::endl; | ||
| if (auto result = | ||
| file_write->call(PyTuple::create(PyString::create(end).unwrap()).unwrap(), nullptr); | ||
| result.is_err()) { | ||
| return result; | ||
| } | ||
| if (flush) { | ||
| auto file_flush_ = file->get_method(PyString::create("flush").unwrap()); | ||
| if (file_flush_.is_err()) { return file_flush_; } | ||
| return file_flush_.unwrap()->call(nullptr, nullptr); | ||
| } | ||
| return Ok(py_none()); | ||
| } | ||
| --arg_it_end; | ||
| @@ -117,7 +139,14 @@ PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interprete | ||
| if (reprobj_.is_err()) { return reprobj_; } | ||
| auto reprobj = reprobj_.unwrap(); | ||
| spdlog::debug("repr result: {}", reprobj->value()); | ||
| std::cout << reprobj->value() << separator; | ||
| if (auto result = file_write->call(PyTuple::create(reprobj).unwrap(), nullptr); | ||
| result.is_err()) { | ||
| return result; | ||
| } | ||
gf712 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (auto result = file_write->call(PyTuple::create(separator).unwrap(), nullptr); | ||
| result.is_err()) { | ||
| return result; | ||
| } | ||
| std::advance(arg_it, 1); | ||
| } | ||
| @@ -126,8 +155,21 @@ PyResult<PyObject *> print(const PyTuple *args, const PyDict *kwargs, Interprete | ||
| if (reprobj_.is_err()) { return reprobj_; } | ||
| auto reprobj = reprobj_.unwrap(); | ||
| spdlog::debug("repr result: {}", reprobj->value()); | ||
| std::cout << reprobj->value() << end; | ||
| if (auto result = file_write->call(PyTuple::create(reprobj).unwrap(), nullptr); | ||
| result.is_err()) { | ||
| return result; | ||
| } | ||
| if (auto result = | ||
| file_write->call(PyTuple::create(PyString::create(end).unwrap()).unwrap(), nullptr); | ||
| result.is_err()) { | ||
| return result; | ||
| } | ||
| if (flush) { | ||
| auto file_flush_ = file->get_method(PyString::create("flush").unwrap()); | ||
| if (file_flush_.is_err()) { return file_flush_; } | ||
| return file_flush_.unwrap()->call(nullptr, nullptr); | ||
| } | ||
| return Ok(py_none()); | ||
| } | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.