From: Peter Rabbitson Date: Fri, 30 Sep 2016 07:08:55 +0000 (+0200) Subject: Retire the ASSERT_NO_INTERNAL_WANTARRAY macro X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a580a22749565b3b07f583bc04412e6dc0c84ab2;p=dbsrgits%2FDBIx-Class.git Retire the ASSERT_NO_INTERNAL_WANTARRAY macro It was a good idea for its time, and helped clean up the codebase a lot, but ASSERT_NO_INTERNAL_INDIRECT_CALLS currently covers all its functionality and does so in a way less fragile (stateless) manner Mark several more methods as indirect_sugar, leaving only one forgotten spot for last (see next commit) No functional changes Read under -w --- diff --git a/lib/DBIx/Class/Relationship/Accessor.pm b/lib/DBIx/Class/Relationship/Accessor.pm index e6d4fb4..d8a0d99 100644 --- a/lib/DBIx/Class/Relationship/Accessor.pm +++ b/lib/DBIx/Class/Relationship/Accessor.pm @@ -160,7 +160,6 @@ EOC quote_sub "${class}::${rel}", sprintf( <<'EOC', perlstring $rel ), @qsub_args; DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and DBIx::Class::_Util::fail_on_internal_call; - DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = DBIx::Class::_Util::fail_on_internal_wantarray; shift->related_resultset(%s)->search( @_ ) EOC diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index a274ee7..0a1cc53 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -12,8 +12,7 @@ use Scalar::Util qw( blessed reftype ); use SQL::Abstract 'is_literal_value'; use DBIx::Class::_Util qw( dbic_internal_try dbic_internal_catch dump_value emit_loud_diag - fail_on_internal_wantarray fail_on_internal_call - UNRESOLVABLE_CONDITION DUMMY_ALIASPAIR + fail_on_internal_call UNRESOLVABLE_CONDITION DUMMY_ALIASPAIR ); use DBIx::Class::SQLMaker::Util qw( normalize_sqla_condition extract_equality_conditions ); use DBIx::Class::ResultSource::FromSpec::Util 'find_join_path_to_alias'; @@ -392,27 +391,24 @@ L. =cut sub search { - my $self = shift; - my $rs = $self->search_rs( @_ ); + my $rs = shift->search_rs( @_ ); - if (wantarray) { - DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = fail_on_internal_wantarray; - return $rs->all; - } - elsif (defined wantarray) { - return $rs; - } - else { - # we can be called by a relationship helper, which in - # turn may be called in void context due to some braindead - # overload or whatever else the user decided to be clever - # at this particular day. Thus limit the exception to - # external code calls only - $self->throw_exception ('->search is *not* a mutator, calling it in void context makes no sense') - if (caller)[0] !~ /^\QDBIx::Class::/; - - return (); - } + return $rs->all + if wantarray; + + return $rs + if defined wantarray; + + # we can be called by a relationship helper, which in + # turn may be called in void context due to some braindead + # overload or whatever else the user decided to be clever + # at this particular day. Thus limit the exception to + # external code calls only + $rs->throw_exception ('->search is *not* a mutator, calling it in void context makes no sense') + if (caller)[0] !~ /^\QDBIx::Class::/; + + # we are in void ctx here, but just in case + return (); } =head2 search_rs @@ -699,7 +695,9 @@ Example of how to use C instead of C =cut -sub search_literal { +sub search_literal :DBIC_method_is_indirect_sugar { + DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call; + my ($self, $sql, @bind) = @_; my $attr; if ( @bind && ref($bind[-1]) eq 'HASH' ) { @@ -1192,7 +1190,9 @@ instead. An example conversion is: =cut -sub search_like { +sub search_like :DBIC_method_is_indirect_sugar { + DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call; + my $class = shift; carp_unique ( 'search_like() is deprecated and will be removed in DBIC version 0.09.' @@ -1223,7 +1223,9 @@ three records, call: =cut -sub slice { +sub slice :DBIC_method_is_indirect_sugar { + DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_INDIRECT_CALLS and fail_on_internal_call; + my ($self, $min, $max) = @_; my $attrs = {}; # = { %{ $self->{attrs} || {} } }; $attrs->{offset} = $self->{attrs}{offset} || 0; @@ -3512,12 +3514,7 @@ sub as_subselect_rs { 'Starting with DBIC@0.082900 as_subselect_rs() always returns a ResultSet ' . 'instance regardless of calling context. Please force scalar() context to ' . 'silence this warning' - ) - and - DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY - and - my $sog = fail_on_internal_wantarray - ; + ); my $self = shift; diff --git a/lib/DBIx/Class/ResultSetColumn.pm b/lib/DBIx/Class/ResultSetColumn.pm index bde5f9a..c3c80c9 100644 --- a/lib/DBIx/Class/ResultSetColumn.pm +++ b/lib/DBIx/Class/ResultSetColumn.pm @@ -5,7 +5,7 @@ use warnings; use base 'DBIx::Class'; use DBIx::Class::Carp; -use DBIx::Class::_Util qw( fail_on_internal_wantarray fail_on_internal_call ); +use DBIx::Class::_Util 'fail_on_internal_call'; use namespace::clean; =head1 NAME @@ -414,12 +414,10 @@ sub func :DBIC_method_is_indirect_sugar{ #my ($self,$function) = @_; my $cursor = $_[0]->func_rs($_[1])->cursor; - if( wantarray ) { - DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY and my $sog = fail_on_internal_wantarray; - return map { $_->[ 0 ] } $cursor->all; - } - - return ( $cursor->next )[ 0 ]; + wantarray + ? map { $_->[ 0 ] } $cursor->all + : ( $cursor->next )[ 0 ] + ; } =head2 func_rs @@ -455,12 +453,7 @@ sub func_rs { 'Starting with DBIC@0.082900 func_rs() always returns a ResultSet ' . 'instance regardless of calling context. Please force scalar() context to ' . 'silence this warning' - ) - and - DBIx::Class::_ENV_::ASSERT_NO_INTERNAL_WANTARRAY - and - my $sog = fail_on_internal_wantarray - ; + ); $rs->search_rs( undef, { columns => { $self->{_as} => { $function => $select } } diff --git a/lib/DBIx/Class/_Util.pm b/lib/DBIx/Class/_Util.pm index 147614f..6d9d757 100644 --- a/lib/DBIx/Class/_Util.pm +++ b/lib/DBIx/Class/_Util.pm @@ -48,7 +48,6 @@ BEGIN { { substr($_, 5) => !!( $ENV{$_} ) } qw( DBIC_SHUFFLE_UNORDERED_RESULTSETS - DBIC_ASSERT_NO_INTERNAL_WANTARRAY DBIC_ASSERT_NO_INTERNAL_INDIRECT_CALLS DBIC_ASSERT_NO_ERRONEOUS_METAINSTANCE_USE DBIC_ASSERT_NO_FAILING_SANITY_CHECKS @@ -198,8 +197,7 @@ BEGIN { *deep_clone = \&Storable::dclone } use base 'Exporter'; our @EXPORT_OK = qw( - sigwarn_silencer modver_gt_or_eq modver_gt_or_eq_and_lt - fail_on_internal_wantarray fail_on_internal_call + sigwarn_silencer modver_gt_or_eq modver_gt_or_eq_and_lt fail_on_internal_call refdesc refcount hrefaddr set_subname get_subname describe_class_methods scope_guard detected_reinvoked_destructor emit_loud_diag true false @@ -1073,58 +1071,6 @@ sub mkdir_p ($) { } -{ - my $list_ctx_ok_stack_marker; - - sub fail_on_internal_wantarray () { - return if $list_ctx_ok_stack_marker; - - if (! defined wantarray) { - croak('fail_on_internal_wantarray() needs a tempvar to save the stack marker guard'); - } - - my $cf = 1; - while ( ( (CORE::caller($cf+1))[3] || '' ) =~ / :: (?: - - # these are public API parts that alter behavior on wantarray - search | search_related | slice | search_literal - - | - - # these are explicitly prefixed, since we only recognize them as valid - # escapes when they come from the guts of CDBICompat - CDBICompat .*? :: (?: search_where | retrieve_from_sql | retrieve_all ) - - ) $/x ) { - $cf++; - } - - my ($fr, $want, $argdesc); - { - package DB; - $fr = [ CORE::caller($cf) ]; - $want = ( CORE::caller($cf-1) )[5]; - $argdesc = ref $DB::args[0] - ? DBIx::Class::_Util::refdesc($DB::args[0]) - : 'non ' - ; - }; - - if ( - $want and $fr->[0] =~ /^(?:DBIx::Class|DBICx::)/ - ) { - DBIx::Class::Exception->throw( sprintf ( - "Improper use of %s instance in list context at %s line %d\n\n Stacktrace starts", - $argdesc, @{$fr}[1,2] - ), 'with_stacktrace'); - } - - weaken( $list_ctx_ok_stack_marker = my $mark = [] ); - - $mark; - } -} - sub fail_on_internal_call { my $fr = [ CORE::caller(1) ];