Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chdir appears to throw exception immediately #5921

Closed
p6rt opened this issue Dec 27, 2016 · 7 comments
Closed

chdir appears to throw exception immediately #5921

p6rt opened this issue Dec 27, 2016 · 7 comments

Comments

@p6rt
Copy link

p6rt commented Dec 27, 2016

Migrated from rt.perl.org#130418 (status was 'resolved')

Searchable as RT130418$

@p6rt
Copy link
Author

p6rt commented Dec 27, 2016

From @briandfoy

Using chdir with a non-existent directory appear to throw the exception
immediately rather than using fail. The docs say
(https://docs.perl6.org/type/IO$COLON$COLONPath#routine_chdir):

  If the directory change is successful then the IO​::Path representing
  the target directory is returned, otherwise it will fail with X​::IO​::Chdir.

Here's what I was doing. I expected to get a Failure object back from
chdir, which would be "handled" by being evaluated in Boolean context.
It would return False and run the unless block​:

  put 'Current working directory is ' ~ $*CWD;

  my $file = '/not-there.txt';
  put "$file is there​: " ~ $file.IO.e;

  unless my $r = chdir $file {
  my $e = $r.exception;
  put "UNLESS​: chdir failed​: " ~ $e.^name ~ "\n" ~ $e.message;
  exit;
  }
  CATCH {
  put "CATCH​: chdir failed​: " ~ .^name ~ "\n" ~ .message;
  exit;
  }

  say "Cwd is now " ~ $*CWD;

Instead, CATCH handles the immediately thrown exception​:

  Current working directory is /Users/brian/Dropbox/~~Writing/Perl
Writing/LearningPerl6/learning_perl_6/scratch
  CATCH​: chdir failed​: X​::IO​::Chdir
  Failed to change the working directory to '/not-there.txt'​: does not exist
  Failed to change the working directory to '/not-there.txt'​: does not exist
  in block <unit> at /Users/brian/Dropbox/~~Writing/Perl
Writing/LearningPerl6/learning_perl_6/scratch/files.p6 line 8

  Actually thrown at​:
  in any at gen/moar/m-Metamodel.nqp line 3096
  in block <unit> at /Users/brian/Dropbox/~~Writing/Perl
Writing/LearningPerl6/learning_perl_6/scratch/files.p6 line 8

@p6rt
Copy link
Author

p6rt commented Dec 28, 2016

From @zoffixznet

On Tue, 27 Dec 2016 13​:28​:19 -0800, comdog wrote​:

Using chdir with a non-existent directory appear to throw the
exception
immediately rather than using fail.

A bit of a discussion for why chdir throws​: https://irclog.perlgeek.de/perl6-dev/2016-12-28#i_13810888

But on further inspection, I see most of IO routines intercept Failures and throw them; search for ".throw" in https://github.com/rakudo/rakudo/blob/nom/src/core/IO/Path.pm and and https://github.com/rakudo/rakudo/blob/nom/src/core/io_operators.pm

Yet the docs say they fail() and not throw.

So it makes me wonder if it's a design decision to abandoning the Failure mechanism on all of IO routines?

@p6rt
Copy link
Author

p6rt commented Dec 28, 2016

The RT System itself - Status changed from 'new' to 'open'

@p6rt
Copy link
Author

p6rt commented Dec 28, 2016

From @zoffixznet

Some comments from moritz​: https://irclog.perlgeek.de/perl6-dev/2016-12-28#i_13813983
And a short comment from TimToady on the topic​: https://irclog.perlgeek.de/perl6-dev/2016-12-28#i_13814034

I also notice our *subs* throw, while *methods* fail(). The subs call the methods, check for failures and throw if detected. Based on what I see in the code sub move() appears to throw on anything which is not X​::IO​::Copy|X​::IO​::Unlink, and for those two it fails... and IO​::Path doesn't provide a .move method at all.

So it all seems to be pretty messy and in need of consistency. We just need to decide what the behaviour should be.

@p6rt
Copy link
Author

p6rt commented Dec 28, 2016

From @lizmat

Welcome to the world of the newio branch

On 28 Dec 2016, at 20​:03, Zoffix Znet via RT <perl6-bugs-followup@​perl.org> wrote​:

Some comments from moritz​: https://irclog.perlgeek.de/perl6-dev/2016-12-28#i_13813983
And a short comment from TimToady on the topic​: https://irclog.perlgeek.de/perl6-dev/2016-12-28#i_13814034

I also notice our *subs* throw, while *methods* fail(). The subs call the methods, check for failures and throw if detected. Based on what I see in the code sub move() appears to throw on anything which is not X​::IO​::Copy|X​::IO​::Unlink, and for those two it fails... and IO​::Path doesn't provide a .move method at all.

So it all seems to be pretty messy and in need of consistency. We just need to decide what the behaviour should be.

@p6rt
Copy link
Author

p6rt commented Apr 24, 2017

From @zoffixznet

On Tue, 27 Dec 2016 13​:28​:19 -0800, comdog wrote​:

Using chdir with a non-existent directory appear to throw the
exception
immediately rather than using fail. The docs say
(https://docs.perl6.org/type/IO$COLON$COLONPath#routine_chdir):

If the directory change is successful then the IO​::Path representing
the target directory is returned, otherwise it will fail with
X​::IO​::Chdir.

Here's what I was doing. I expected to get a Failure object back from
chdir, which would be "handled" by being evaluated in Boolean context.
It would return False and run the unless block​:

put 'Current working directory is ' ~ $*CWD;

my $file = '/not-there.txt';
put "$file is there​: " ~ $file.IO.e;

unless my $r = chdir $file {
my $e = $r.exception;
put "UNLESS​: chdir failed​: " ~ $e.^name ~ "\n" ~ $e.message;
exit;
}
CATCH {
put "CATCH​: chdir failed​: " ~ .^name ~ "\n" ~ .message;
exit;
}

say "Cwd is now " ~ $*CWD;

Instead, CATCH handles the immediately thrown exception​:

Current working directory is /Users/brian/Dropbox/~~Writing/Perl
Writing/LearningPerl6/learning_perl_6/scratch
CATCH​: chdir failed​: X​::IO​::Chdir
Failed to change the working directory to '/not-there.txt'​: does not
exist
Failed to change the working directory to '/not-there.txt'​: does not
exist
in block <unit> at /Users/brian/Dropbox/~~Writing/Perl
Writing/LearningPerl6/learning_perl_6/scratch/files.p6 line 8

Actually thrown at​:
in any at gen/moar/m-Metamodel.nqp line 3096
in block <unit> at /Users/brian/Dropbox/~~Writing/Perl
Writing/LearningPerl6/learning_perl_6/scratch/files.p6 line 8

Thanks for the report. This was fixed some time in 2017.04 release.

- IO grant

@p6rt
Copy link
Author

p6rt commented Apr 24, 2017

@zoffixznet - Status changed from 'open' to 'resolved'

@p6rt p6rt closed this as completed Apr 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant