Files
RustPython/extra_tests/snippets/stdlib_io_stringio.py
Kwak Byoung Min 62f4c9895a Add StringIO newline tracking (#8539)
* Report observed StringIO newline types

Constraint: Match CPython StringIO newline reporting without changing configured newline translation.
Rejected: Separate newline-tracking representation | reuse the existing SeenNewline bitflags.
Confidence: high
Scope-risk: narrow
Directive: Keep observed newline state separate from the configured newline mode.
Tested: prek run --all-files; test_memoryio; cargo clippy -p rustpython-vm --lib -- -D warnings; workspace tests excluding the macOS C-API baseline SIGSEGV.
Not-tested: Full macOS workspace suite is blocked by the existing rustpython-capi SIGSEGV; the Linux suite is running.
Assisted-by: Codex:gpt-5.6-sol

* Preserve StringIO state boundaries

Reset observed newline state when StringIO is reinitialized and keep the property subject to closed-stream checks.

Constraint: Match CPython StringIO reinitialization and closed-stream behavior.
Rejected: Returning stale or cached newline state | violates StringIO lifecycle semantics.
Confidence: high
Scope-risk: narrow
Directive: Keep observed newline state tied to the current open StringIO contents.
Tested: cargo test --workspace --exclude rustpython_wasm --exclude rustpython-venvlauncher; cargo run --release -- extra_tests/snippets/stdlib_io_stringio.py; cargo run --release -- -m test test_memoryio; prek run --all-files; cargo clippy -p rustpython-vm --lib -- -D warnings.
Assisted-by: Codex:gpt-5.6-sol
2026-08-18 12:38:16 +09:00

94 lines
1.7 KiB
Python

from io import StringIO
def test_01():
"""
Test that the constructor and getvalue
method return expected values
"""
string = "Test String 1"
f = StringIO()
f.write(string)
assert f.tell() == len(string)
assert f.getvalue() == string
def test_02():
"""
Test that the read method (no arg)
results the expected value
"""
string = "Test String 2"
f = StringIO(string)
assert f.read() == string
assert f.read() == ""
def test_03():
"""
Tests that the read method (integer arg)
returns the expected value
"""
string = "Test String 3"
f = StringIO(string)
assert f.read(1) == "T"
assert f.read(1) == "e"
assert f.read(1) == "s"
assert f.read(1) == "t"
def test_04():
"""
Tests that the read method increments the
cursor position and the seek method moves
the cursor to the appropriate position
"""
string = "Test String 4"
f = StringIO(string)
assert f.read(4) == "Test"
assert f.tell() == 4
assert f.seek(0) == 0
assert f.read(4) == "Test"
def test_05():
"""
Tests readline
"""
string = "Test String 6\nnew line is here\nfinished"
f = StringIO(string)
assert f.readline() == "Test String 6\n"
assert f.readline() == "new line is here\n"
assert f.readline() == "finished"
assert f.readline() == ""
def test_06():
f = StringIO(newline=None)
f.write("\r")
f.__init__("x\n", newline=None)
assert f.newlines == "\n"
f.close()
try:
f.newlines
except ValueError:
pass
else:
assert False
if __name__ == "__main__":
test_01()
test_02()
test_03()
test_04()
test_05()
test_06()