Thread overview
[Issue 4911] New: Bad error messages from attempts to write into read-only File
September 22, 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@puremagic.com
        ReportedBy: bearophile_hugs@eml.cc


--- Comment #0 from bearophile_hugs@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@...\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@...\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@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: -------
September 22, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4911



--- Comment #1 from bearophile_hugs@eml.cc 2010-09-21 18:06:34 PDT ---
I think in Phobos there is not (yet) a function to find the line number and module name of the precedent stack frame, of the caller, so probably the "test.d(5)" can't be generated in the error message, andthe programmer has to read the stack trace.

So a more realistic error message (plus stack trace) is:

FileIOException@...\dmd\src\phobos\std\stdio.d(508): attempt to write into
read-only file "test.raw".

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 26, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4911



--- Comment #2 from bearophile_hugs@eml.cc 2011-06-26 05:20:49 PDT ---
In DMD 2.053 this program:

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

Gives the error message:

std.exception.ErrnoException@std\stdio.d(286): Cannot open file `test.raw' in
mode `r' (No such file or directory)
----------------
...\test.d(8): void test.bar()
...\test.d(10): _Dmain
----------------


I think this is as good as it gets, unless a function to find the line number and module name of the precedent stack frame is used at the exception point, so I close this bug report.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 26, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4911


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED


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