Skip Menu |

Preferred bug tracker

Please visit the preferred bug tracker to report your issue.

This queue is for tickets about the Data-Printer CPAN distribution.

Report information
The Basics
Id: 68630
Status: resolved
Priority: 0/
Queue: Data-Printer

People
Owner: Nobody in particular
Requestors: szabgab [...] gmail.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: 0.19
Fixed in: (no value)



Subject: Data-Printer-0.16 on Strawberry Perl 5.12.3 test failures
Date: Fri, 3 Jun 2011 16:08:39 +0300
To: bug-Data-Printer [...] rt.cpan.org
From: Gabor Szabo <szabgab [...] gmail.com>

Message body is not shown because it is too large.

Thanks for the report! I think it's an issue with the "am I being piped?" detection system (-t *STDERR). I'll try to reproduce and fix this as soon as I have access to a Windows box.
Still broken in 0.19.
Well, the real problem here is that D::P doesn't respect ANSI_COLORS_DISABLED. Furthermore, it even doesn't preserve it's value inside a call to _data_printer. Even for it's own test. Like the following: 1) A_C_D gets set/unset at the beginning of the test 2) then it get overwritten in 1st call to _data_printer to whatever it thinks is suitable 3) ..and is overwritten inside each call to _data_printer I've tried some straightforward patches (for A_C_D checks only), but they tend to break things on *nix. So real solutions seams to be a wrapper to colored(), that checks internal state in $p (precomputed) and never touches A_C_D. And run tests that are written using A_C_D=undef using {colored=>0} instead.
Next patch :) Removes overriding ща A_C_D, uses _colored() proxy instead, making things more consistent. Some tests updated to force colored output. As a side effect :), fixes tests under windows.
Subject: dp-wintest.diff
--- lib/Data/Printer.pm +++ lib/Data/Printer.pm @@ -1,7 +1,7 @@ package Data::Printer; use strict; use warnings; -use Term::ANSIColor; +use Term::ANSIColor qw(); #to prevent accidental direct call use Scalar::Util; use Sort::Naturally; use Class::MOP; @@ -152,15 +152,15 @@ } # colors only if we're not being piped + my $out = ''; if ( !$p->{colored} or ($p->{colored} eq 'auto' and not -t *STDERR) ) { - $ENV{ANSI_COLORS_DISABLED} = 1; + $p->{_colored} = 0; } else { - delete $ENV{ANSI_COLORS_DISABLED}; + $p->{_colored} = 1; + $out .= Term::ANSIColor::color('reset'); } - my $out = color('reset'); - if ( $p->{caller_info} and $p->{_depth} == 0 ) { $out .= _get_info_message($p); } @@ -182,7 +182,7 @@ my $id = _object_id( $item ); if ( exists $p->{_seen}->{$id} ) { if ( not defined $p->{_reftype} ) { - return colored($p->{_seen}->{$id}, $p->{color}->{repeated}); + return _colored($p->{_seen}->{$id}, $p->{color}->{repeated}, $p->{_colored}); } } else { @@ -235,13 +235,13 @@ my $string = ''; if (not defined $$item) { - $string .= colored('undef', $p->{color}->{'undef'}); + $string .= _colored('undef', $p->{color}->{'undef'}, $p->{_colored}); } elsif (Scalar::Util::looks_like_number($$item)) { - $string .= colored($$item, $p->{color}->{'number'}); + $string .= _colored($$item, $p->{color}->{'number'}, $p->{_colored}); } else { - $string .= colored(qq["$$item"], $p->{color}->{'string'}); + $string .= _colored(qq["$$item"], $p->{color}->{'string'}, $p->{_colored}); } $p->{_tie} = ref tied $$item; @@ -271,9 +271,10 @@ my $array_elem = $item->[$i]; $string .= (' ' x $p->{_current_indent}); if ($p->{'index'}) { - $string .= colored( + $string .= _colored( sprintf("%-*s", 3 + length($#{$item}), "[$i]"), - $p->{color}->{'array'} + $p->{color}->{'array'}, + $p->{_colored} ); } @@ -287,7 +288,7 @@ else { $string .= _p( $array_elem, $p ); } - $string .= ' ' . colored('(weak)', $p->{color}->{'weak'}) + $string .= ' ' . _colored('(weak)', $p->{color}->{'weak'}, $p->{_colored}) if $ref and Scalar::Util::isweak($item->[$i]); $string .= ($i == $#{$item} ? '' : ',') . $BREAK; @@ -315,7 +316,7 @@ qw(SCALAR CODE Regexp ARRAY HASH GLOB REF); } $string .= _p($$item, $p); - $string .= ' ' . colored('(weak)', $p->{color}->{'weak'}) if Scalar::Util::isweak($$item); + $string .= ' ' . _colored('(weak)', $p->{color}->{'weak'}, $p->{_colored}) if Scalar::Util::isweak($$item); return $string; } @@ -328,7 +329,7 @@ if ($p->{deparse}) { $code = _deparse( $item, $p ); } - $string .= colored($code, $p->{color}->{'code'}); + $string .= _colored($code, $p->{color}->{'code'}, $p->{_colored}); return $string; } @@ -365,9 +366,10 @@ my $element = $item->{$key}; $string .= (' ' x $p->{_current_indent}) - . colored( + . _colored( sprintf("%-*s", $len, $key), - $p->{color}->{'hash'} + $p->{color}->{'hash'}, + $p->{_colored} ) . $p->{hash_separator} ; @@ -381,7 +383,7 @@ else { $string .= _p( $element, $p ); } - $string .= ' ' . colored('(weak)', $p->{color}->{'weak'}) + $string .= ' ' . _colored('(weak)', $p->{color}->{'weak'}, $p->{_colored}) if $ref and Scalar::Util::isweak($item->{$key}); $string .= (--$total_keys == 0 ? '' : ',') . $BREAK; @@ -409,7 +411,7 @@ # note: we are not validating anything, just grabbing modifiers if ($val =~ m/\(\?\^?([uladxismpogce]*)(?:\-[uladxismpogce]+)?:(.*)\)/s) { my ($modifiers, $val) = ($1, $2); - $string .= colored($val, $p->{color}->{'regex'}); + $string .= _colored($val, $p->{color}->{'regex'}, $p->{_colored}); if ($modifiers) { $string .= " (modifiers: $modifiers)"; } @@ -425,7 +427,7 @@ my ($item, $p) = @_; my $string = ''; - $string .= colored("$$item", $p->{color}->{'glob'}); + $string .= _colored("$$item", $p->{color}->{'glob'}, $p->{_colored}); my $extra = ''; @@ -481,7 +483,7 @@ my $string = ''; $p->{class}{_depth}++; - $string .= colored($ref, $p->{color}->{'class'}); + $string .= _colored($ref, $p->{color}->{'class'}, $p->{_colored}); if ($p->{class}{expand} eq 'all' or $p->{class}{expand} >= $p->{class}{_depth} @@ -496,7 +498,7 @@ if ($p->{class}{parents}) { $string .= (' ' x $p->{_current_indent}) . 'Parents ' - . join(', ', map { colored($_, $p->{color}->{'class'}) } + . join(', ', map { _colored($_, $p->{color}->{'class'}, $p->{_colored}) } @superclasses ) . $BREAK; } @@ -504,7 +506,7 @@ if ($p->{class}{linear_isa}) { $string .= (' ' x $p->{_current_indent}) . 'Linear @ISA ' - . join(', ', map { colored( $_, $p->{color}->{'class'}) } + . join(', ', map { _colored( $_, $p->{color}->{'class'}, $p->{_colored}) } $meta->linearized_isa ) . $BREAK; } @@ -552,6 +554,12 @@ } } +sub _colored { + my ($string, $color, $do_colors) = @_; + + return $string if !$do_colors; + return Term::ANSIColor::colored($string, $color); +} sub _show_methods { my ($ref, $meta, $p) = @_; @@ -588,7 +596,7 @@ $string .= (' ' x $p->{_current_indent}) . "$type methods (" . scalar @list . ')' . (@list ? ' : ' : '') - . join(', ', map { colored($_, $p->{color}->{class}) } + . join(', ', map { _colored($_, $p->{color}->{class}, $p->{_colored}) } @list ) . $BREAK; } @@ -618,7 +626,7 @@ $message =~ s/\b__FILENAME__\b/$caller[1]/g; $message =~ s/\b__LINE__\b/$caller[2]/g; - return colored($message, $p->{color}{caller_info}) . $BREAK; + return _colored($message, $p->{color}{caller_info}, $p->{_colored}) . $BREAK; } --- t/02-colors.t +++ t/02-colors.t @@ -6,7 +6,7 @@ delete $ENV{ANSI_COLORS_DISABLED}; use File::HomeDir::Test; # avoid user's .dataprinter use_ok ('Term::ANSIColor'); - use_ok ('Data::Printer'); + use_ok ('Data::Printer', colored => 1); }; my $number = 3.14; --- t/14-local_conf.t +++ t/14-local_conf.t @@ -6,7 +6,7 @@ delete $ENV{ANSI_COLORS_DISABLED}; use File::HomeDir::Test; # avoid user's .dataprinter use_ok 'Term::ANSIColor'; - use_ok 'Data::Printer'; + use_ok 'Data::Printer', colored => 1; }; my %hash = ( key => 'value' ); --- t/15-rc_file.t +++ t/15-rc_file.t @@ -29,7 +29,7 @@ # file created and in place, let's load up our # module and see if it overrides the default conf # with our .dataprinter RC file - use_ok ('Data::Printer'); + use_ok ('Data::Printer', colored => 1); unlink $file or fail('error removing test file'); }; --- t/16-rc_file2.t +++ t/16-rc_file2.t @@ -33,7 +33,8 @@ color => { hash => 'blue' }, - hash_separator => ' * ', + hash_separator => ' * ', + colored => 1, }); unlink $file or fail('error removing test file'); }; --- t/17-parallel.t +++ t/17-parallel.t @@ -6,17 +6,17 @@ }; package Foo; -use Data::Printer { color => { number => 'green' } }; +use Data::Printer { color => { number => 'green' }, colored => 1 }; sub foo { p($_[0]) } package Bar; -use Data::Printer { color => { number => 'yellow' } }; +use Data::Printer { color => { number => 'yellow' }, colored => 1 }; sub bar { p($_[0]) } package main; use Test::More; -use Data::Printer { color => { number => 'blue' } }; +use Data::Printer { color => { number => 'blue' }, colored => 1 }; delete $ENV{ANSI_COLORS_DISABLED}; my $data = 42; --- t/25-weak.t +++ t/25-weak.t @@ -7,7 +7,7 @@ use File::HomeDir::Test; # avoid user's .dataprinter use_ok ('Term::ANSIColor'); use_ok ('Scalar::Util', qw(weaken)); - use_ok ('Data::Printer'); + use_ok ('Data::Printer', colored => 1); }; my $number = 3.14; @@ -15,7 +15,7 @@ weaken($n_ref); is( p($n_ref), color('reset') . '\\ ' . colored($number, 'bright_blue') - . colored(' (weak)', 'cyan') + . ' ' . colored('(weak)', 'cyan') , 'weakened ref'); @@ -25,7 +25,7 @@ is( p($circular), color('reset') . "\\ [$/ " . colored('[0] ', 'bright_white') . colored('var', 'white on_red') - . colored(' (weak)', 'cyan') + . ' ' . colored('(weak)', 'cyan') . "$/]" , 'weakened circular array ref'); @@ -38,7 +38,7 @@ . colored('key', 'magenta') . ' ' . colored('var', 'white on_red') - . colored(' (weak)', 'cyan') + . ' ' . colored('(weak)', 'cyan') . "$/}" , 'weakened circular hash ref'); @@ -49,14 +49,14 @@ my $obj = Foo->new; -is( p($obj), 'Foo { - public methods (1) : new +is( p($obj), color('reset').colored('Foo', 'bright_green').' { + public methods (1) : '.colored('new', 'bright_green').' private methods (0) internals: [ ' . colored('[0] ', 'bright_white') . colored('var', 'white on_red') - . colored(' (weak)', 'cyan').' + . ' ' . colored('(weak)', 'cyan').' ] }', 'circular weak ref to object' );
On Thu Jun 16 18:52:39 2011, RANDIR wrote: Show quoted text
> Well, the real problem here is that D::P doesn't respect > ANSI_COLORS_DISABLED. Furthermore, it even doesn't preserve it's value > inside a call to _data_printer.
Yes. I felt a bit on the fence about how to behave regarding colors, and this particular behavior has changed a lot over the first few versions, but your statement pretty much covers it for me: there *is* a variable to activate/deactivate colors and Data::Printer *should* respect it. The patch to 0.20 should cover it, and it also fixes the Windows issue. It's somewhat based in your own colorization patch, but keeps using (and respecting) the environment variable unless you force colors with colored => 1. The default is still colored => 'auto' which from now on should do the right thing :) Thanks to everyone involved. I was able to successfully install it on a 64-bit Windows 7 machine. If anyone following this ticket can confirm this in at least one other MS Windows system, I'll close the ticket. Cheers
On Thu Jun 30 21:35:58 2011, GARU wrote: Show quoted text
> > Thanks to everyone involved. I was able to successfully install it on a > 64-bit Windows 7 machine. If anyone following this ticket can confirm > this in at least one other MS Windows system, I'll close the ticket. >
Ok, it's been a while and no one seemed to have had any problems with the patch, so I'm marking this issue as resolved :)