# HG changeset patch # User Paul Boddie # Date 1481052753 -3600 # Node ID d6bd77febdbaa7259b11612089af3da76e8d0053 # Parent fe62bfdc0d277d991d66ba13cdced058d524153d Fixed the native read function, allocating new strings from the input buffer. Added a value to IOError instances and fixed IOError raising in the native functions and helper functions. Added a test of reading data and updated the test runner to send some default input into every test. diff -r fe62bfdc0d27 -r d6bd77febdba lib/__builtins__/exception/io.py --- a/lib/__builtins__/exception/io.py Tue Dec 06 18:18:47 2016 +0100 +++ b/lib/__builtins__/exception/io.py Tue Dec 06 20:32:33 2016 +0100 @@ -3,7 +3,7 @@ """ Input/output exception objects. -Copyright (C) 2015 Paul Boddie +Copyright (C) 2015, 2016 Paul Boddie This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software @@ -20,7 +20,17 @@ """ class EOFError(Exception): pass -class IOError(Exception): pass + +class IOError(Exception): + + "An input/output error." + + def __init__(self, value): + + "Initialise the exception with the given 'value'." + + self.value = value + class KeyboardInterrupt(Exception): pass # vim: tabstop=4 expandtab shiftwidth=4 diff -r fe62bfdc0d27 -r d6bd77febdba templates/native.c --- a/templates/native.c Tue Dec 06 18:18:47 2016 +0100 +++ b/templates/native.c Tue Dec 06 20:32:33 2016 +0100 @@ -663,7 +663,7 @@ /* Produce an exception if the operation failed. */ if (f == NULL) - __raise_io_error(errno); + __raise_io_error(__new_int(errno)); else { attr.context = 0; @@ -682,18 +682,19 @@ int to_read = __load_via_object(n->value, __pos___data__).intvalue; void *buf[to_read + 1]; ssize_t have_read; + char *s; errno = 0; have_read = read(i, buf, to_read); if (have_read == -1) - __raise_io_error(errno); + __raise_io_error(__new_int(errno)); else { - /* Zero terminate the string. */ - - buf[have_read] = 0; - return __new_str((char *) buf); + /* Reserve space for a new string. */ + s = __ALLOCATE(have_read + 1, 1); + strncpy(s, (char *) buf, have_read); /* does not null terminate but final byte should be zero */ + return __new_str(s); } } diff -r fe62bfdc0d27 -r d6bd77febdba templates/progops.c --- a/templates/progops.c Tue Dec 06 18:18:47 2016 +0100 +++ b/templates/progops.c Tue Dec 06 20:32:33 2016 +0100 @@ -80,9 +80,9 @@ /* Helpers for raising errors within common operations. */ #ifdef __HAVE___builtins___exception_io_IOError -void __raise_io_error() +void __raise_io_error(__attr value) { - __attr args[1]; + __attr args[2] = {{0, 0}, value}; __attr exc = __new___builtins___exception_io_IOError(args); __Raise(exc); } diff -r fe62bfdc0d27 -r d6bd77febdba templates/progops.h --- a/templates/progops.h Tue Dec 06 18:18:47 2016 +0100 +++ b/templates/progops.h Tue Dec 06 20:32:33 2016 +0100 @@ -18,7 +18,7 @@ /* Exception raising. */ #ifdef __HAVE___builtins___exception_io_IOError -void __raise_io_error(); +void __raise_io_error(__attr value); #endif /* __HAVE___builtins___exception_io_IOError */ void __raise_memory_error(); diff -r fe62bfdc0d27 -r d6bd77febdba test_all.sh --- a/test_all.sh Tue Dec 06 18:18:47 2016 +0100 +++ b/test_all.sh Tue Dec 06 20:32:33 2016 +0100 @@ -35,6 +35,8 @@ fi fi +TESTINPUT="tests/testinput.txt" + # Perform each test. for FILENAME in tests/* ; do @@ -50,6 +52,12 @@ fi fi + # Skip non-program files. + + if [ `basename "$FILENAME"` = "$TESTNAME" ]; then + continue + fi + # Run tests without an existing cache. echo "$FILENAME..." 1>&2 @@ -96,7 +104,7 @@ fi echo " (run)..." 1>&2 - if ! "_generated/main" > "$OUTLOG" ; then + if ! "_generated/main" > "$OUTLOG" < "$TESTINPUT" ; then exit 1 fi fi diff -r fe62bfdc0d27 -r d6bd77febdba tests/read.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/read.py Tue Dec 06 20:32:33 2016 +0100 @@ -0,0 +1,9 @@ +from posix.io import read + +try: + s = read(3, 10) +except IOError, exc: + print "read(3, 10): input/output error condition", exc.value + +s = read(0, 10) +print s diff -r fe62bfdc0d27 -r d6bd77febdba tests/testinput.txt --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/testinput.txt Tue Dec 06 20:32:33 2016 +0100 @@ -0,0 +1,1 @@ +Hello world!