Skip to content

Commit

Permalink
[GH #911] Provide strerror for unknown filehandle errors
Browse files Browse the repository at this point in the history
Unify printed type: filehandle => FileHandle, to sync with generic Parrot_io_open
"Cannot open %s, no path", vtable->name errmsg.
  • Loading branch information
Reini Urban committed Jan 7, 2013
1 parent b323780 commit e93a009
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
13 changes: 9 additions & 4 deletions src/io/filehandle.c
@@ -1,5 +1,5 @@
/*
Copyright (C) 2001-2012, Parrot Foundation.
Copyright (C) 2001-2013, Parrot Foundation.
=head1 NAME
Expand Down Expand Up @@ -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.
Expand Down
28 changes: 26 additions & 2 deletions src/pmc/filehandle.pmc
@@ -1,5 +1,5 @@
/*
Copyright (C) 2008-2011, Parrot Foundation.
Copyright (C) 2008-2013, Parrot Foundation.

=head1 NAME

Expand Down Expand Up @@ -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

*/
Expand All @@ -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);
Expand Down
25 changes: 24 additions & 1 deletion 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;
Expand Down Expand Up @@ -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' );
Expand Down

0 comments on commit e93a009

Please sign in to comment.