From e93a009c431988ecde3e323f60fe546610425c93 Mon Sep 17 00:00:00 2001 From: Reini Urban Date: Fri, 4 Jan 2013 13:37:14 -0600 Subject: [PATCH] [GH #911] Provide strerror for unknown filehandle errors Unify printed type: filehandle => FileHandle, to sync with generic Parrot_io_open "Cannot open %s, no path", vtable->name errmsg. --- src/io/filehandle.c | 13 +++++++++---- src/pmc/filehandle.pmc | 28 ++++++++++++++++++++++++++-- t/pmc/filehandle.t | 25 ++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/io/filehandle.c b/src/io/filehandle.c index abb01c9f8d..8167f3a173 100644 --- a/src/io/filehandle.c +++ b/src/io/filehandle.c @@ -1,5 +1,5 @@ /* -Copyright (C) 2001-2012, Parrot Foundation. +Copyright (C) 2001-2013, Parrot Foundation. =head1 NAME @@ -469,9 +469,14 @@ io_filehandle_open(PARROT_INTERP, ARGMOD(PMC *handle), ARGIN(STRING *path), INTV os_handle = Parrot_io_internal_open(interp, path, flags); - if (os_handle == PIO_INVALID_HANDLE) - Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR, - "Unable to open filehandle from path '%Ss'", path); + if (os_handle == PIO_INVALID_HANDLE) { + if (errno) + Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR, + "Unable to open filehandle from path '%Ss': %s(%d)", path, strerror(errno), errno); + else + Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_PIO_ERROR, + "Unable to open filehandle from path '%Ss'", path); + } /* Set generic flag here if is a terminal then * FileHandle can know how to setup buffering. diff --git a/src/pmc/filehandle.pmc b/src/pmc/filehandle.pmc index 0ab2356f28..731a671e98 100644 --- a/src/pmc/filehandle.pmc +++ b/src/pmc/filehandle.pmc @@ -1,5 +1,5 @@ /* -Copyright (C) 2008-2011, Parrot Foundation. +Copyright (C) 2008-2013, Parrot Foundation. =head1 NAME @@ -249,6 +249,30 @@ invocant is modified and becomes an open filehandle. A copy of the invocant is also returned by the method (some subclasses may create this as the primary filehandle, rather than modifying the invocant). +Exceptions: + +EXCEPTION_PIO_ERROR with the following messages: + +Empty filename + + "Cannot open FileHandle, no path" + +Already open filehandle + + "Cannot reopen already open FileHandle" + +Invalid handle, no errno as with ISDIR: + + "Unable to open filehandle from path '$path'" + +Invalid handle (fd < 0), or other error: + + "Unable to open filehandle from path '$path': $strerror($errno)" + +EXCEPTION_INVALID_OPERATION with: + + "Invalid mode for file open" + =cut */ @@ -259,7 +283,7 @@ filehandle, rather than modifying the invocant). if (!Parrot_io_is_closed(INTERP, SELF)) Parrot_ex_throw_from_c_args(INTERP, NULL, EXCEPTION_PIO_ERROR, - "Cannot reopen already open filehandle"); + "Cannot reopen already open FileHandle"); if (!got_mode || STRING_IS_NULL(mode)) GET_ATTR_mode(INTERP, SELF, mode); diff --git a/t/pmc/filehandle.t b/t/pmc/filehandle.t index 1bba507114..56328fc37a 100644 --- a/t/pmc/filehandle.t +++ b/t/pmc/filehandle.t @@ -1,5 +1,5 @@ #!perl -# Copyright (C) 2006-2011, Parrot Foundation. +# Copyright (C) 2006-2013, Parrot Foundation. use strict; use warnings; @@ -137,11 +137,34 @@ pir_output_is( <<'CODE', <<'OUT', 'wrong open' ); finalize eh reportreopen: say i + + i = 0 + set_label eh, catchother + fh.'open'('noREADME.pod') + goto reportother + catchother: + i = 1 + finalize eh + reportother: + say i + + i = 0 + set_label eh, catchdir + fh.'open'('t') + goto reportdir + catchdir: + i = 1 + finalize eh + reportdir: + say i + pop_eh .end CODE 1 1 +1 +1 OUT pir_output_is( <<'CODE', <<'OUT', 'isatty' );