Skip to content

Commit

Permalink
Item14152: Merge commit 'ed0744222c351c641b466903948ee3a84cde8feb' in…
Browse files Browse the repository at this point in the history
…to Item14152

* commit 'ed0744222c351c641b466903948ee3a84cde8feb':
  Item13897: Fixed cloning of non-HASH blessed objects.
  Item13897: Don't attempt to finalize login manager in global destruction.
  Item13897: A bit of improvement to stringify()
  Item13897: Overloading of stringification operator by Foswiki::Object.
  Item13897: Completing the previous commit
  Item13897: createNewFoswikiApp() is now initializing cfg using cfgParams
  • Loading branch information
vrurg committed Dec 16, 2016
2 parents 5c01e2f + ed07442 commit 6dccd69
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 18 deletions.
7 changes: 6 additions & 1 deletion UnitTestContrib/lib/Unit/FoswikiTestRole.pm
Expand Up @@ -618,7 +618,12 @@ sub createNewFoswikiApp {
$app->cfg->data->{Store}{Implementation} ||= 'Foswiki::Store::PlainFile';

$params{env} //= $app->cloneEnv;
$params{cfg} //= $app->cfg->clone;
unless ( exists $params{cfgParams} ) {
my %cfgData = %{ $app->cfg->clone };
delete $cfgData{app};
$params{cfgParams} = \%cfgData;
}

my $newApp = Unit::TestApp->new(%params);

$this->app($newApp);
Expand Down
14 changes: 10 additions & 4 deletions core/lib/Foswiki/App.pm
Expand Up @@ -100,9 +100,10 @@ instance.
=cut

has cfg => (
is => 'rw',
lazy => 1,
builder => '_prepareConfig',
is => 'rw',
lazy => 1,
predicate => 1,
builder => '_prepareConfig',
isa => Foswiki::Object::isaCLASS( 'cfg', 'Foswiki::Config', noUndef => 1, ),
);
has env => (
Expand Down Expand Up @@ -430,8 +431,13 @@ sub DEMOLISH {

# Make sure not to do this if incomplete initialization happened or we're
# doomed for "(in cleanup)" messages.
# Skip it over global destruction stage too.
$this->users->loginManager->complete
if $this->users && $this->users->has_loginManager;
if !$in_global
&& $this->has_users
&& $this->users
&& $this->users->has_loginManager
&& $this->users->loginManager;
}

=begin TML
Expand Down
23 changes: 13 additions & 10 deletions core/lib/Foswiki/Exception.pm
Expand Up @@ -57,8 +57,6 @@ use Foswiki::Class;
extends qw(Foswiki::Object);
with 'Throwable';

use overload '""' => 'to_str';

our $EXCEPTION_TRACE = 0;

=begin TML
Expand Down Expand Up @@ -174,26 +172,31 @@ sub BUILD {
if DEBUG && $EXCEPTION_TRACE;
}

sub stringify {
sub stringifyPostfix {
my $this = shift;

return $this->text
. (
return (
DEBUG
? "\n" . $this->stacktrace
: ' at ' . $this->file . ' line ' . $this->line
);
);
}

sub stringify {
my $this = shift;

return $this->text . $this->stringifyPostfix;
}

sub to_str {
around to_str => sub {
my $orig = shift;
my $this = shift;

my $boundary = '-' x 60;
my $msg = join( "\n",
$boundary, map( { " " . $_ } split /\n/, $this->stringify ),
$boundary );
return $msg;
}
};

# We must not get into this. But if we do then let's not hide a error but let it
# thru to the end user via JsonRPC interfaces.
Expand Down Expand Up @@ -563,7 +566,7 @@ around stringify => sub {
$res->print( $this->status . " "
. $this->header . "\n\n"
. $this->text
. ( DEBUG ? $this->stacktrace : '' ) );
. $this->stringifyPostfix );
}

return $orig->($this);
Expand Down
41 changes: 38 additions & 3 deletions core/lib/Foswiki/Object.pm
Expand Up @@ -22,10 +22,12 @@ features.
require Carp;
require Foswiki::Exception;
use Try::Tiny;
use Scalar::Util qw(blessed refaddr weaken isweak);
use Scalar::Util qw(blessed refaddr reftype weaken isweak);

use Foswiki::Class;

use overload fallback => 1, '""' => 'to_str';

use Assert;

=begin TML
Expand Down Expand Up @@ -239,8 +241,24 @@ sub _cloneData {
# cloning as a hash and blessing the resulting hashref into
# $val's class.
# SMELL Pretty much unreliable for complex classes.
$cloned =
$this->_cloneData( {%$val}, "$attr.blessed($class)" );
my $reftype = reftype($val);
if ( $reftype eq 'HASH' ) {
$cloned =
$this->_cloneData( {%$val}, "$attr.blessed($class)" );
}
elsif ( $reftype eq 'ARRAY' ) {
$cloned =
$this->_cloneData( [@$val], "$attr.blessed($class)" );
}
elsif ( $reftype eq 'SCALAR' ) {
$cloned =
$this->_cloneData( \$$val, "$attr.blessed($class)" );
}
else {
# Cannot clone unknown datatypes, just copy the original
# ref.
$cloned = $val;
}
bless $cloned, ref($val)
if $cloned != $val;
}
Expand Down Expand Up @@ -363,6 +381,23 @@ sub clone {
return $newObj;
}

=begin TML
---++ ObjectMethod to_str => $string
This method is used to overload stringification operator "" (see
[[CPAN:overload][=perldoc overload=]]).
The default is to return object itself in order to preserve system default
behavior.
=cut

sub to_str {
my @c = caller;
return $_[0];
}

# Fixes __orig_file and __orig_line to bypass ::create() and point directly to
# where it was called.
# $level parameter – how many stack frames to skip.
Expand Down

0 comments on commit 6dccd69

Please sign in to comment.