# HG changeset patch # User Paul Boddie # Date 1481237456 -3600 # Node ID 2ba43e72f7f427d41e72fa5521171bcc1d5136b1 # Parent c2bdc5b1a125b68ce87a5d3eae8ff56943d66e08 Added support for closing files and streams. Made the low-level write operation return the number of bytes written, also declaring the IOError that can be raised. diff -r c2bdc5b1a125 -r 2ba43e72f7f4 lib/__builtins__/file.py --- a/lib/__builtins__/file.py Thu Dec 08 23:36:43 2016 +0100 +++ b/lib/__builtins__/file.py Thu Dec 08 23:50:56 2016 +0100 @@ -69,7 +69,11 @@ check_string(s) native._fwrite(self.__data__, s) - def close(self): pass + def close(self): + + "Close the stream." + + native._fclose(self.__data__) class file(filestream): diff -r c2bdc5b1a125 -r 2ba43e72f7f4 lib/native.py --- a/lib/native.py Thu Dec 08 23:36:43 2016 +0100 +++ b/lib/native.py Thu Dec 08 23:50:56 2016 +0100 @@ -100,16 +100,18 @@ # Input/output. +def _fclose(fp): IOError def _fopen(filename, mode): IOError def _fdopen(fd, mode): IOError +def _close(fd): IOError def _read(fd, n): IOError -def _write(fd, str): pass +def _write(fd, str): IOError -def _fread(fd, n): +def _fread(fp, n): IOError EOFError -def _fwrite(fd, str): +def _fwrite(fp, str): IOError EOFError diff -r c2bdc5b1a125 -r 2ba43e72f7f4 lib/posix/io.py --- a/lib/posix/io.py Thu Dec 08 23:36:43 2016 +0100 +++ b/lib/posix/io.py Thu Dec 08 23:50:56 2016 +0100 @@ -47,7 +47,13 @@ "Write string 's' to the file." check_string(s) - write(self.fd, s) + return write(self.fd, s) + + def close(self): + + "Close the file." + + close(self.fd) class sysstream(filestream): @@ -68,7 +74,12 @@ # Input/output functions. -def close(fd): pass +def close(fd): + + "Close the file descriptor 'fd'." + + native._close(fd) + def closerange(fd_low, fd_high): pass def dup(fd): pass def dup2(old_fd, new_fd): pass @@ -127,7 +138,7 @@ check_int(fd) check_string(s) - native._write(fd, s) + return native._write(fd, s) # Constants. diff -r c2bdc5b1a125 -r 2ba43e72f7f4 templates/native.c --- a/templates/native.c Thu Dec 08 23:36:43 2016 +0100 +++ b/templates/native.c Thu Dec 08 23:50:56 2016 +0100 @@ -680,6 +680,19 @@ /* Input/output. */ +__attr __fn_native__fclose(__attr __args[]) +{ + __attr * const fp = &__args[1]; + /* fp interpreted as FILE reference */ + FILE *f = (FILE *) fp->datavalue; + + errno = 0; + if (fclose(f)) + __raise_io_error(__new_int(errno)); + + return __builtins___none_None; +} + __attr __fn_native__fopen(__attr __args[]) { __attr * const filename = &__args[1]; @@ -791,6 +804,19 @@ return __builtins___none_None; } +__attr __fn_native__close(__attr __args[]) +{ + __attr * const fd = &__args[1]; + /* fd.__data__ interpreted as int */ + int i = __load_via_object(fd->value, __pos___data__).intvalue; + + errno = 0; + if (close(i) == -1) + __raise_io_error(__new_int(errno)); + + return __builtins___none_None; +} + __attr __fn_native__read(__attr __args[]) { __attr * const fd = &__args[1]; @@ -808,14 +834,12 @@ if (have_read == -1) __raise_io_error(__new_int(errno)); - else - { - /* Reserve space for a new string. */ + + /* 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); - } + 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); } __attr __fn_native__write(__attr __args[]) @@ -826,9 +850,15 @@ int i = __load_via_object(fd->value, __pos___data__).intvalue; /* str.__data__ interpreted as string */ char *s = __load_via_object(str->value, __pos___data__).strvalue; + ssize_t have_written; - write(i, s, sizeof(char) * strlen(s)); - return __builtins___none_None; + errno = 0; + have_written = write(i, s, sizeof(char) * strlen(s)); + + if (have_written == -1) + __raise_io_error(__new_int(errno)); + + return __new_int(have_written); } /* Module initialisation. */ diff -r c2bdc5b1a125 -r 2ba43e72f7f4 templates/native.h --- a/templates/native.h Thu Dec 08 23:36:43 2016 +0100 +++ b/templates/native.h Thu Dec 08 23:50:56 2016 +0100 @@ -100,10 +100,12 @@ /* Input/output. */ +__attr __fn_native__fclose(__attr __args[]); __attr __fn_native__fopen(__attr __args[]); __attr __fn_native__fdopen(__attr __args[]); __attr __fn_native__fread(__attr __args[]); __attr __fn_native__fwrite(__attr __args[]); +__attr __fn_native__close(__attr __args[]); __attr __fn_native__read(__attr __args[]); __attr __fn_native__write(__attr __args[]);