<commit_before>my class IO::Path { ... }
my class IO::Special { ... }

my class IO::Handle does IO {
    has $.path;
    has $!PIO;
    has int $.ins;
    has $.chomp = Bool::True;
    has Bool $!isDir;

    proto method open(|) { * }
    multi method open(
      :$r is copy,
      :$w is copy,
      :$rw,
      :$a,
      :$p,
      :$bin,
      :$chomp = True,
      :$enc   = 'utf8',
    ) {

        if $!path eq '-' {
            $!path =
              IO::Special.new(:what( $w ?? << <STDOUT> >> !! << <STDIN> >> ));
        }

        if $!path ~~ IO::Special {
            given $!path.what {
                when << <STDIN> >> {
                    $!PIO := nqp::getstdin();
                }
                when << <STDOUT> >> {
                    $!PIO := nqp::getstdout();
                }
                when << <STDERR> >> {
                    $!PIO := nqp::getstderr();
                }
                default {
                    die "Don't know how to open '$_' especially";
                }
            }
            return self;
        }

        fail (X::IO::Directory.new(:$!path, :trying<open>)) if $!path.d;

        $!isDir = $!path.d // False;  # TEMPORARY
        $r = $w = True if $rw;

#?if parrot
        my $mode =  $p ?? ($w ||  $a ?? 'wp' !! 'rp') !!
                   ($w ?? 'w' !! ($a ?? 'wa' !! 'r' ));
        # TODO: catch error, and fail()
        $!PIO := nqp::open(nqp::unbox_s($!path.abspath), nqp::unbox_s($mode));
#?endif
#?if !parrot
        if $p {
            #~ my $mode =  $p ?? ($w ||  $a ?? 'wp' !! 'rp');

            my Mu $hash-with-containers :=
              nqp::getattr(%*ENV, EnumMap, '$!storage');
            my Mu $hash-without := nqp::hash();
            my Mu $enviter := nqp::iterator($hash-with-containers);

            my $envelem;
            while $enviter {
                $envelem := nqp::shift($enviter);
                nqp::bindkey(
                  $hash-without,<commit_msg>Make sure standard handles have proper encoding<commit_after>my class IO::Path { ... }
my class IO::Special { ... }

my class IO::Handle does IO {
    has $.path;
    has $!PIO;
    has int $.ins;
    has $.chomp = Bool::True;
    has Bool $!isDir;

    method open(
      :$r is copy,
      :$w is copy,
      :$rw,
      :$a,
      :$p,
      :$bin,
      :$chomp = True,
      :$enc   = 'utf8',
    ) {

        if $!path eq '-' {
            $!path =
              IO::Special.new(:what( $w ?? << <STDOUT> >> !! << <STDIN> >> ));
        }

        if $!path ~~ IO::Special {
            given $!path.what {
                when << <STDIN> >> {
                    $!PIO := nqp::getstdin();
                }
                when << <STDOUT> >> {
                    $!PIO := nqp::getstdout();
                }
                when << <STDERR> >> {
                    $!PIO := nqp::getstderr();
                }
                default {
                    die "Don't know how to open '$_' especially";
                }
            }
            nqp::setencoding($!PIO, NORMALIZE_ENCODING($enc)) unless $bin;
            return self;
        }

        fail (X::IO::Directory.new(:$!path, :trying<open>)) if $!path.d;

        $!isDir = $!path.d // False;  # TEMPORARY
        $r = $w = True if $rw;

#?if parrot
        my $mode =  $p ?? ($w ||  $a ?? 'wp' !! 'rp') !!
                   ($w ?? 'w' !! ($a ?? 'wa' !! 'r' ));
        # TODO: catch error, and fail()
        $!PIO := nqp::open(nqp::unbox_s($!path.abspath), nqp::unbox_s($mode));
#?endif
#?if !parrot
        if $p {
            #~ my $mode =  $p ?? ($w ||  $a ?? 'wp' !! 'rp');

            my Mu $hash-with-containers :=
              nqp::getattr(%*ENV, EnumMap, '$!storage');
            my Mu $hash-without := nqp::hash();
            my Mu $enviter := nqp::iterator($hash-with-containers);

            my $envelem;
            while $enviter {
                $envelem := nqp::shift($enviter);
                nqp::bindkey(
                  $hash-without,
{'fraction_non_alphanumeric': 0.1904047976011994, 'fraction_numerical': 0.0004997501249375312, 'mean_word_length': 1.3112009237875288, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1838327', 'n_tokens_mistral': 1379, 'n_tokens_neox': 1309, 'n_words': 303}