summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-06-05 23:54:21 +0300
committerPaul Sokolovsky <pfalcon@users.sourceforge.net>2017-06-09 17:33:01 +0300
commit07241cd37a732e4219257e6b2fc67a84085b37f6 (patch)
tree7c41bb31dfaffd45e11dd0ed21a52cb145b71542 /tests
parentb24ccfc639416c9427640b5f38e64a1476c1c704 (diff)
py/objstringio: If created from immutable object, follow copy on write policy.
Don't create copy of immutable object's contents until .write() is called on BytesIO.
Diffstat (limited to 'tests')
-rw-r--r--tests/io/bytesio_cow.py20
-rw-r--r--tests/micropython/heapalloc_bytesio2.py20
-rw-r--r--tests/micropython/heapalloc_bytesio2.py.exp1
3 files changed, 41 insertions, 0 deletions
diff --git a/tests/io/bytesio_cow.py b/tests/io/bytesio_cow.py
new file mode 100644
index 000000000..92654a000
--- /dev/null
+++ b/tests/io/bytesio_cow.py
@@ -0,0 +1,20 @@
+# Make sure that write operations on io.BytesIO don't
+# change original object it was constructed from.
+try:
+ import uio as io
+except ImportError:
+ import io
+
+b = b"foobar"
+
+a = io.BytesIO(b)
+a.write(b"1")
+print(b)
+print(a.getvalue())
+
+b = bytearray(b"foobar")
+
+a = io.BytesIO(b)
+a.write(b"1")
+print(b)
+print(a.getvalue())
diff --git a/tests/micropython/heapalloc_bytesio2.py b/tests/micropython/heapalloc_bytesio2.py
new file mode 100644
index 000000000..cd76f5807
--- /dev/null
+++ b/tests/micropython/heapalloc_bytesio2.py
@@ -0,0 +1,20 @@
+# Creating BytesIO from immutable object should not immediately
+# copy its content.
+try:
+ import uio
+ import micropython
+ micropython.mem_total
+except (ImportError, AttributeError):
+ print("SKIP")
+ raise SystemExit
+
+
+data = b"1234" * 256
+
+before = micropython.mem_total()
+
+buf = uio.BytesIO(data)
+
+after = micropython.mem_total()
+
+print(after - before < len(data))
diff --git a/tests/micropython/heapalloc_bytesio2.py.exp b/tests/micropython/heapalloc_bytesio2.py.exp
new file mode 100644
index 000000000..0ca95142b
--- /dev/null
+++ b/tests/micropython/heapalloc_bytesio2.py.exp
@@ -0,0 +1 @@
+True