1.1 --- a/lib/__builtins__/file.py Thu Dec 08 23:36:43 2016 +0100
1.2 +++ b/lib/__builtins__/file.py Thu Dec 08 23:50:56 2016 +0100
1.3 @@ -69,7 +69,11 @@
1.4 check_string(s)
1.5 native._fwrite(self.__data__, s)
1.6
1.7 - def close(self): pass
1.8 + def close(self):
1.9 +
1.10 + "Close the stream."
1.11 +
1.12 + native._fclose(self.__data__)
1.13
1.14 class file(filestream):
1.15
2.1 --- a/lib/native.py Thu Dec 08 23:36:43 2016 +0100
2.2 +++ b/lib/native.py Thu Dec 08 23:50:56 2016 +0100
2.3 @@ -100,16 +100,18 @@
2.4
2.5 # Input/output.
2.6
2.7 +def _fclose(fp): IOError
2.8 def _fopen(filename, mode): IOError
2.9 def _fdopen(fd, mode): IOError
2.10 +def _close(fd): IOError
2.11 def _read(fd, n): IOError
2.12 -def _write(fd, str): pass
2.13 +def _write(fd, str): IOError
2.14
2.15 -def _fread(fd, n):
2.16 +def _fread(fp, n):
2.17 IOError
2.18 EOFError
2.19
2.20 -def _fwrite(fd, str):
2.21 +def _fwrite(fp, str):
2.22 IOError
2.23 EOFError
2.24
3.1 --- a/lib/posix/io.py Thu Dec 08 23:36:43 2016 +0100
3.2 +++ b/lib/posix/io.py Thu Dec 08 23:50:56 2016 +0100
3.3 @@ -47,7 +47,13 @@
3.4 "Write string 's' to the file."
3.5
3.6 check_string(s)
3.7 - write(self.fd, s)
3.8 + return write(self.fd, s)
3.9 +
3.10 + def close(self):
3.11 +
3.12 + "Close the file."
3.13 +
3.14 + close(self.fd)
3.15
3.16 class sysstream(filestream):
3.17
3.18 @@ -68,7 +74,12 @@
3.19
3.20 # Input/output functions.
3.21
3.22 -def close(fd): pass
3.23 +def close(fd):
3.24 +
3.25 + "Close the file descriptor 'fd'."
3.26 +
3.27 + native._close(fd)
3.28 +
3.29 def closerange(fd_low, fd_high): pass
3.30 def dup(fd): pass
3.31 def dup2(old_fd, new_fd): pass
3.32 @@ -127,7 +138,7 @@
3.33
3.34 check_int(fd)
3.35 check_string(s)
3.36 - native._write(fd, s)
3.37 + return native._write(fd, s)
3.38
3.39 # Constants.
3.40
4.1 --- a/templates/native.c Thu Dec 08 23:36:43 2016 +0100
4.2 +++ b/templates/native.c Thu Dec 08 23:50:56 2016 +0100
4.3 @@ -680,6 +680,19 @@
4.4
4.5 /* Input/output. */
4.6
4.7 +__attr __fn_native__fclose(__attr __args[])
4.8 +{
4.9 + __attr * const fp = &__args[1];
4.10 + /* fp interpreted as FILE reference */
4.11 + FILE *f = (FILE *) fp->datavalue;
4.12 +
4.13 + errno = 0;
4.14 + if (fclose(f))
4.15 + __raise_io_error(__new_int(errno));
4.16 +
4.17 + return __builtins___none_None;
4.18 +}
4.19 +
4.20 __attr __fn_native__fopen(__attr __args[])
4.21 {
4.22 __attr * const filename = &__args[1];
4.23 @@ -791,6 +804,19 @@
4.24 return __builtins___none_None;
4.25 }
4.26
4.27 +__attr __fn_native__close(__attr __args[])
4.28 +{
4.29 + __attr * const fd = &__args[1];
4.30 + /* fd.__data__ interpreted as int */
4.31 + int i = __load_via_object(fd->value, __pos___data__).intvalue;
4.32 +
4.33 + errno = 0;
4.34 + if (close(i) == -1)
4.35 + __raise_io_error(__new_int(errno));
4.36 +
4.37 + return __builtins___none_None;
4.38 +}
4.39 +
4.40 __attr __fn_native__read(__attr __args[])
4.41 {
4.42 __attr * const fd = &__args[1];
4.43 @@ -808,14 +834,12 @@
4.44
4.45 if (have_read == -1)
4.46 __raise_io_error(__new_int(errno));
4.47 - else
4.48 - {
4.49 - /* Reserve space for a new string. */
4.50 +
4.51 + /* Reserve space for a new string. */
4.52
4.53 - s = __ALLOCATE(have_read + 1, 1);
4.54 - strncpy(s, (char *) buf, have_read); /* does not null terminate but final byte should be zero */
4.55 - return __new_str(s);
4.56 - }
4.57 + s = __ALLOCATE(have_read + 1, 1);
4.58 + strncpy(s, (char *) buf, have_read); /* does not null terminate but final byte should be zero */
4.59 + return __new_str(s);
4.60 }
4.61
4.62 __attr __fn_native__write(__attr __args[])
4.63 @@ -826,9 +850,15 @@
4.64 int i = __load_via_object(fd->value, __pos___data__).intvalue;
4.65 /* str.__data__ interpreted as string */
4.66 char *s = __load_via_object(str->value, __pos___data__).strvalue;
4.67 + ssize_t have_written;
4.68
4.69 - write(i, s, sizeof(char) * strlen(s));
4.70 - return __builtins___none_None;
4.71 + errno = 0;
4.72 + have_written = write(i, s, sizeof(char) * strlen(s));
4.73 +
4.74 + if (have_written == -1)
4.75 + __raise_io_error(__new_int(errno));
4.76 +
4.77 + return __new_int(have_written);
4.78 }
4.79
4.80 /* Module initialisation. */
5.1 --- a/templates/native.h Thu Dec 08 23:36:43 2016 +0100
5.2 +++ b/templates/native.h Thu Dec 08 23:50:56 2016 +0100
5.3 @@ -100,10 +100,12 @@
5.4
5.5 /* Input/output. */
5.6
5.7 +__attr __fn_native__fclose(__attr __args[]);
5.8 __attr __fn_native__fopen(__attr __args[]);
5.9 __attr __fn_native__fdopen(__attr __args[]);
5.10 __attr __fn_native__fread(__attr __args[]);
5.11 __attr __fn_native__fwrite(__attr __args[]);
5.12 +__attr __fn_native__close(__attr __args[]);
5.13 __attr __fn_native__read(__attr __args[]);
5.14 __attr __fn_native__write(__attr __args[]);
5.15