[Issue 4911] New: Bad error messages from attempts to write into read-only File

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Sep 21 17:46:26 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4911

           Summary: Bad error messages from attempts to write into
                    read-only File
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Keywords: diagnostic
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-09-21 17:45:47 PDT ---
This is a wrong Python 2 program, it tries to write on a read mode file:

f = file("test.raw", "r")
f.write("hello")


Python 2.6.5 gives the run-time error:

Traceback (most recent call last):
  File "...\test.py", line 2, in <module>
    f.write("hello")
IOError: File not open for writing

-----------------------------

This is a similar D2 program:

import std.stdio: File;
void main() {
    auto f = File("test.raw", "r");
    f.write("hello");
}


It generates the run-time error (dmd 2.049):

std.exception.ErrnoException at ...\dmd\src\phobos\std\stdio.d(1060):  (No error)

This error is useless, it doesn't show the line count and file of the problem,
the cause of the problem (the file is read-only) and it doesn't even clearly
show it's a I/O error.

-----------------------------

A similarly wrong D2 program:


import std.stdio: File;
void main() {
    double[3] data = [0.5, 1.5, 2.5];
    auto f = File("test.raw", "r");
    f.rawWrite(data);
}


The error it raises, far still from being a good error message:

std.exception.ErrnoException at ...\dmd\src\phobos\std\stdio.d(508): Wrote 0
instead of 3 objects of type double to file `test.raw' (No error)


A much better error message may be this, that gives the line number and file
name of the module that has produced the error, shows that it's an I/O error,
explains that the bug comes from trying to write in a read-only file, and it
gives the name of the file:

FileIOException at test.d(5): attempt to write into read-only file "test.raw".


(Even better, a typestate system is able to spot this bug at compile-time, see
bug 4571 ).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list