From 117158fcd560a84318a0b9cb5332c5599acdb84b Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 23 Dec 2015 22:37:02 +0000 Subject: [PATCH] tests: Add tests for stream IO errors. --- tests/io/file1.py | 32 ++++++++++++++++++++++++++++++++ tests/io/file_readinto.py | 7 +++++++ tests/io/file_readline.py | 8 ++++++++ tests/io/file_seek.py | 9 +++++++++ 4 files changed, 56 insertions(+) diff --git a/tests/io/file1.py b/tests/io/file1.py index c46c081b80..8f9e4ef6e0 100644 --- a/tests/io/file1.py +++ b/tests/io/file1.py @@ -12,3 +12,35 @@ f = open("io/data/file1",mode="r") print(f.readlines()) f = open("io/data/file1",mode="rb") print(f.readlines()) + +# write() error +f = open('io/data/file1', 'r') +try: + f.write('x') +except OSError: + print('OSError') +f.close() + +# read(n) error on binary file +f = open('io/data/file1', 'ab') +try: + f.read(1) +except OSError: + print('OSError') +f.close() + +# read(n) error on text file +f = open('io/data/file1', 'at') +try: + f.read(1) +except OSError: + print('OSError') +f.close() + +# readall() error (call read() for compat with CPy) +f = open('io/data/file1', 'ab') +try: + f.read() +except OSError: + print('OSError') +f.close() diff --git a/tests/io/file_readinto.py b/tests/io/file_readinto.py index 7a0603377a..cbefc6e040 100644 --- a/tests/io/file_readinto.py +++ b/tests/io/file_readinto.py @@ -5,3 +5,10 @@ print(b) f = open("io/data/file2", "rb") print(f.readinto(b)) print(b) + +# readinto() on writable file +f = open('io/data/file1', 'ab') +try: + f.readinto(bytearray(4)) +except OSError: + print('OSError') diff --git a/tests/io/file_readline.py b/tests/io/file_readline.py index c6a67d0e15..25e76597b1 100644 --- a/tests/io/file_readline.py +++ b/tests/io/file_readline.py @@ -4,3 +4,11 @@ print(f.readline(3)) print(f.readline(4)) print(f.readline(5)) print(f.readline()) + +# readline() on writable file +f = open('io/data/file1', 'ab') +try: + f.readline() +except OSError: + print('OSError') +f.close() diff --git a/tests/io/file_seek.py b/tests/io/file_seek.py index d6be662cbf..10fb1fd06f 100644 --- a/tests/io/file_seek.py +++ b/tests/io/file_seek.py @@ -23,3 +23,12 @@ print(f.seek(6)) print(f.read(5)) print(f.tell()) f.close() + +# seek closed file +f = open('io/data/file1', 'r') +f.close() +try: + f.seek(1) +except (OSError, ValueError): + # CPy raises ValueError, uPy raises OSError + print('OSError or ValueError')