Have tests recover gracefully when File::Spec->tmpdir gives us lemons
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / _Util.pm
1 package # hide from PAUSE
2   DBIx::Class::_Util;
3
4 use warnings;
5 use strict;
6
7 use constant SPURIOUS_VERSION_CHECK_WARNINGS => ($] < 5.010 ? 1 : 0);
8
9 BEGIN {
10   package # hide from pause
11     DBIx::Class::_ENV_;
12
13   use Config;
14
15   use constant {
16
17     # but of course
18     BROKEN_FORK => ($^O eq 'MSWin32') ? 1 : 0,
19
20     HAS_ITHREADS => $Config{useithreads} ? 1 : 0,
21
22     # ::Runmode would only be loaded by DBICTest, which in turn implies t/
23     DBICTEST => eval { DBICTest::RunMode->is_author } ? 1 : 0,
24
25     # During 5.13 dev cycle HELEMs started to leak on copy
26     PEEPEENESS =>
27       # request for all tests would force "non-leaky" illusion and vice-versa
28       defined $ENV{DBICTEST_ALL_LEAKS}                                              ? !$ENV{DBICTEST_ALL_LEAKS}
29       # otherwise confess that this perl is busted ONLY on smokers
30     : eval { DBICTest::RunMode->is_smoker } && ($] >= 5.013005 and $] <= 5.013006)  ? 1
31       # otherwise we are good
32                                                                                     : 0
33     ,
34
35     ASSERT_NO_INTERNAL_WANTARRAY => $ENV{DBIC_ASSERT_NO_INTERNAL_WANTARRAY} ? 1 : 0,
36
37     IV_SIZE => $Config{ivsize},
38
39     OS_NAME => $^O,
40   };
41
42   if ($] < 5.009_005) {
43     require MRO::Compat;
44     constant->import( OLD_MRO => 1 );
45   }
46   else {
47     require mro;
48     constant->import( OLD_MRO => 0 );
49   }
50 }
51
52 # FIXME - this is not supposed to be here
53 # Carp::Skip to the rescue soon
54 use DBIx::Class::Carp '^DBIx::Class|^DBICTest';
55
56 use Carp 'croak';
57 use Scalar::Util qw(weaken blessed reftype);
58
59 use base 'Exporter';
60 our @EXPORT_OK = qw(sigwarn_silencer modver_gt_or_eq fail_on_internal_wantarray refcount hrefaddr is_exception);
61
62 sub sigwarn_silencer ($) {
63   my $pattern = shift;
64
65   croak "Expecting a regexp" if ref $pattern ne 'Regexp';
66
67   my $orig_sig_warn = $SIG{__WARN__} || sub { CORE::warn(@_) };
68
69   return sub { &$orig_sig_warn unless $_[0] =~ $pattern };
70 }
71
72 sub hrefaddr ($) { sprintf '0x%x', &Scalar::Util::refaddr }
73
74 sub refcount ($) {
75   croak "Expecting a reference" if ! length ref $_[0];
76
77   require B;
78   # No tempvars - must operate on $_[0], otherwise the pad
79   # will count as an extra ref
80   B::svref_2object($_[0])->REFCNT;
81 }
82
83 sub is_exception ($) {
84   my $e = $_[0];
85
86   my ($not_blank, $suberror);
87   {
88     local $@;
89     eval {
90       $not_blank = ($e ne '') ? 1 : 0;
91       1;
92     } or $suberror = $@;
93   }
94
95   if (defined $suberror) {
96     if (length (my $class = blessed($e) )) {
97       carp_unique( sprintf(
98         'External exception object %s=%s(%s) implements partial (broken) '
99       . 'overloading preventing it from being used in simple ($x eq $y) '
100       . 'comparisons. Given Perl\'s "globally cooperative" exception '
101       . 'handling this type of brokenness is extremely dangerous on '
102       . 'exception objects, as it may (and often does) result in silent '
103       . '"exception substitution". DBIx::Class tries to work around this '
104       . 'as much as possible, but other parts of your software stack may '
105       . 'not be even aware of this. Please submit a bugreport against the '
106       . 'distribution containing %s and in the meantime apply a fix similar '
107       . 'to the one shown at %s, in order to ensure your exception handling '
108       . 'is saner application-wide. What follows is the actual error text '
109       . "as generated by Perl itself:\n\n%s\n ",
110         $class,
111         reftype $e,
112         hrefaddr $e,
113         $class,
114         'http://v.gd/DBIC_overload_tempfix/',
115         $suberror,
116       ));
117
118       # workaround, keeps spice flowing
119       $not_blank = ("$e" ne '') ? 1 : 0;
120     }
121     else {
122       # not blessed yet failed the 'ne'... this makes 0 sense...
123       # just throw further
124       die $suberror
125     }
126   }
127
128   return $not_blank;
129 }
130
131 sub modver_gt_or_eq ($$) {
132   my ($mod, $ver) = @_;
133
134   croak "Nonsensical module name supplied"
135     if ! defined $mod or ! length $mod;
136
137   croak "Nonsensical minimum version supplied"
138     if ! defined $ver or $ver =~ /[^0-9\.\_]/;
139
140   local $SIG{__WARN__} = sigwarn_silencer( qr/\Qisn't numeric in subroutine entry/ )
141     if SPURIOUS_VERSION_CHECK_WARNINGS;
142
143   local $@;
144   eval { $mod->VERSION($ver) } ? 1 : 0;
145 }
146
147 {
148   my $list_ctx_ok_stack_marker;
149
150   sub fail_on_internal_wantarray {
151     return if $list_ctx_ok_stack_marker;
152
153     if (! defined wantarray) {
154       croak('fail_on_internal_wantarray() needs a tempvar to save the stack marker guard');
155     }
156
157     my $cf = 1;
158     while ( ( (caller($cf+1))[3] || '' ) =~ / :: (?:
159
160       # these are public API parts that alter behavior on wantarray
161       search | search_related | slice | search_literal
162
163         |
164
165       # these are explicitly prefixed, since we only recognize them as valid
166       # escapes when they come from the guts of CDBICompat
167       CDBICompat .*? :: (?: search_where | retrieve_from_sql | retrieve_all )
168
169     ) $/x ) {
170       $cf++;
171     }
172
173     if (
174       (caller($cf))[0] =~ /^(?:DBIx::Class|DBICx::)/
175     ) {
176       my $obj = shift;
177
178       DBIx::Class::Exception->throw( sprintf (
179         "Improper use of %s(%s) instance in list context at %s line %d\n\n\tStacktrace starts",
180         ref($obj), hrefaddr($obj), (caller($cf))[1,2]
181       ), 'with_stacktrace');
182     }
183
184     my $mark = [];
185     weaken ( $list_ctx_ok_stack_marker = $mark );
186     $mark;
187   }
188 }
189
190 1;