Commit | Line | Data |
b1dbf716 |
1 | package # hide from PAUSE |
2 | DBIx::Class::_Util; |
3 | |
4 | use warnings; |
5 | use strict; |
6 | |
8bb84304 |
7 | use constant SPURIOUS_VERSION_CHECK_WARNINGS => ( |
8 | ( $ENV{DBICTEST_VERSION_WARNS_INDISCRIMINATELY} or $] < 5.010 ) |
9 | ? 1 |
10 | : 0 |
11 | ); |
b1dbf716 |
12 | |
37873f78 |
13 | BEGIN { |
14 | package # hide from pause |
15 | DBIx::Class::_ENV_; |
16 | |
17 | use Config; |
18 | |
19 | use constant { |
20 | |
21 | # but of course |
22 | BROKEN_FORK => ($^O eq 'MSWin32') ? 1 : 0, |
23 | |
8d73fcd4 |
24 | BROKEN_GOTO => ($] < '5.008003') ? 1 : 0, |
25 | |
37873f78 |
26 | HAS_ITHREADS => $Config{useithreads} ? 1 : 0, |
27 | |
2bbdb85a |
28 | UNSTABLE_DOLLARAT => ( "$]" < 5.013002 ) ? 1 : 0, |
29 | |
d931aae1 |
30 | DBICTEST => $INC{"DBICTest/Util.pm"} ? 1 : 0, |
37873f78 |
31 | |
32 | # During 5.13 dev cycle HELEMs started to leak on copy |
6cfb1d2f |
33 | # add an escape for these perls ON SMOKERS - a user will still get death |
34 | PEEPEENESS => ( eval { DBICTest::RunMode->is_smoker } && ($] >= 5.013005 and $] <= 5.013006) ), |
37873f78 |
35 | |
1b658919 |
36 | SHUFFLE_UNORDERED_RESULTSETS => $ENV{DBIC_SHUFFLE_UNORDERED_RESULTSETS} ? 1 : 0, |
37 | |
37873f78 |
38 | ASSERT_NO_INTERNAL_WANTARRAY => $ENV{DBIC_ASSERT_NO_INTERNAL_WANTARRAY} ? 1 : 0, |
39 | |
77c3a5dc |
40 | ASSERT_NO_INTERNAL_INDIRECT_CALLS => $ENV{DBIC_ASSERT_NO_INTERNAL_INDIRECT_CALLS} ? 1 : 0, |
41 | |
b195e46c |
42 | STRESSTEST_UTF8_UPGRADE_GENERATED_COLLAPSER_SOURCE => $ENV{DBIC_STRESSTEST_UTF8_UPGRADE_GENERATED_COLLAPSER_SOURCE} ? 1 : 0, |
43 | |
37873f78 |
44 | IV_SIZE => $Config{ivsize}, |
00882d2c |
45 | |
46 | OS_NAME => $^O, |
37873f78 |
47 | }; |
48 | |
49 | if ($] < 5.009_005) { |
50 | require MRO::Compat; |
51 | constant->import( OLD_MRO => 1 ); |
52 | } |
53 | else { |
54 | require mro; |
55 | constant->import( OLD_MRO => 0 ); |
56 | } |
57 | } |
58 | |
841efcb3 |
59 | # FIXME - this is not supposed to be here |
60 | # Carp::Skip to the rescue soon |
61 | use DBIx::Class::Carp '^DBIx::Class|^DBICTest'; |
62 | |
14ae8b98 |
63 | use B (); |
841efcb3 |
64 | use Carp 'croak'; |
14ae8b98 |
65 | use Storable 'nfreeze'; |
87f4bab0 |
66 | use Scalar::Util qw(weaken blessed reftype refaddr); |
3705e3b2 |
67 | use List::Util qw(first); |
8ab47674 |
68 | use Sub::Quote qw(qsub quote_sub); |
7f9a3f70 |
69 | |
b1dbf716 |
70 | use base 'Exporter'; |
3705e3b2 |
71 | our @EXPORT_OK = qw( |
9a3d73e9 |
72 | sigwarn_silencer modver_gt_or_eq modver_gt_or_eq_and_lt |
77c3a5dc |
73 | fail_on_internal_wantarray fail_on_internal_call |
2bbdb85a |
74 | refdesc refcount hrefaddr |
75 | scope_guard is_exception detected_reinvoked_destructor |
b34d9331 |
76 | quote_sub qsub perlstring serialize |
facd0e8e |
77 | UNRESOLVABLE_CONDITION |
3705e3b2 |
78 | ); |
052a832c |
79 | |
facd0e8e |
80 | use constant UNRESOLVABLE_CONDITION => \ '1 = 0'; |
81 | |
bf302897 |
82 | sub sigwarn_silencer ($) { |
052a832c |
83 | my $pattern = shift; |
84 | |
85 | croak "Expecting a regexp" if ref $pattern ne 'Regexp'; |
86 | |
87 | my $orig_sig_warn = $SIG{__WARN__} || sub { CORE::warn(@_) }; |
88 | |
89 | return sub { &$orig_sig_warn unless $_[0] =~ $pattern }; |
90 | } |
b1dbf716 |
91 | |
01b25f12 |
92 | sub perlstring ($) { q{"}. quotemeta( shift ). q{"} }; |
93 | |
87f4bab0 |
94 | sub hrefaddr ($) { sprintf '0x%x', &refaddr||0 } |
8433421f |
95 | |
96 | sub refdesc ($) { |
97 | croak "Expecting a reference" if ! length ref $_[0]; |
98 | |
99 | # be careful not to trigger stringification, |
100 | # reuse @_ as a scratch-pad |
101 | sprintf '%s%s(0x%x)', |
102 | ( defined( $_[1] = blessed $_[0]) ? "$_[1]=" : '' ), |
103 | reftype $_[0], |
87f4bab0 |
104 | refaddr($_[0]), |
8433421f |
105 | ; |
106 | } |
bf302897 |
107 | |
108 | sub refcount ($) { |
dac7972a |
109 | croak "Expecting a reference" if ! length ref $_[0]; |
110 | |
dac7972a |
111 | # No tempvars - must operate on $_[0], otherwise the pad |
112 | # will count as an extra ref |
113 | B::svref_2object($_[0])->REFCNT; |
114 | } |
115 | |
b34d9331 |
116 | sub serialize ($) { |
b34d9331 |
117 | local $Storable::canonical = 1; |
14ae8b98 |
118 | nfreeze($_[0]); |
b34d9331 |
119 | } |
120 | |
2bbdb85a |
121 | sub scope_guard (&) { |
122 | croak 'Calling scope_guard() in void context makes no sense' |
123 | if ! defined wantarray; |
124 | |
125 | # no direct blessing of coderefs - DESTROY is buggy on those |
126 | bless [ $_[0] ], 'DBIx::Class::_Util::ScopeGuard'; |
127 | } |
128 | { |
129 | package # |
130 | DBIx::Class::_Util::ScopeGuard; |
131 | |
132 | sub DESTROY { |
133 | &DBIx::Class::_Util::detected_reinvoked_destructor; |
134 | |
135 | local $@ if DBIx::Class::_ENV_::UNSTABLE_DOLLARAT; |
136 | |
137 | eval { |
138 | $_[0]->[0]->(); |
139 | 1; |
140 | } or do { |
141 | Carp::cluck "Execution of scope guard $_[0] resulted in the non-trappable exception:\n\n$@"; |
142 | }; |
143 | } |
144 | } |
145 | |
841efcb3 |
146 | sub is_exception ($) { |
147 | my $e = $_[0]; |
148 | |
a0414138 |
149 | # this is not strictly correct - an eval setting $@ to undef |
150 | # is *not* the same as an eval setting $@ to '' |
151 | # but for the sake of simplicity assume the following for |
152 | # the time being |
153 | return 0 unless defined $e; |
154 | |
841efcb3 |
155 | my ($not_blank, $suberror); |
156 | { |
157 | local $@; |
158 | eval { |
159 | $not_blank = ($e ne '') ? 1 : 0; |
160 | 1; |
161 | } or $suberror = $@; |
162 | } |
163 | |
164 | if (defined $suberror) { |
165 | if (length (my $class = blessed($e) )) { |
166 | carp_unique( sprintf( |
9bea2000 |
167 | 'External exception class %s implements partial (broken) overloading ' |
168 | . 'preventing its instances from being used in simple ($x eq $y) ' |
841efcb3 |
169 | . 'comparisons. Given Perl\'s "globally cooperative" exception ' |
170 | . 'handling this type of brokenness is extremely dangerous on ' |
171 | . 'exception objects, as it may (and often does) result in silent ' |
172 | . '"exception substitution". DBIx::Class tries to work around this ' |
173 | . 'as much as possible, but other parts of your software stack may ' |
174 | . 'not be even aware of this. Please submit a bugreport against the ' |
175 | . 'distribution containing %s and in the meantime apply a fix similar ' |
176 | . 'to the one shown at %s, in order to ensure your exception handling ' |
177 | . 'is saner application-wide. What follows is the actual error text ' |
178 | . "as generated by Perl itself:\n\n%s\n ", |
9bea2000 |
179 | $class, |
841efcb3 |
180 | $class, |
181 | 'http://v.gd/DBIC_overload_tempfix/', |
182 | $suberror, |
183 | )); |
184 | |
185 | # workaround, keeps spice flowing |
186 | $not_blank = ("$e" ne '') ? 1 : 0; |
187 | } |
188 | else { |
189 | # not blessed yet failed the 'ne'... this makes 0 sense... |
190 | # just throw further |
191 | die $suberror |
192 | } |
193 | } |
194 | |
195 | return $not_blank; |
196 | } |
197 | |
87f4bab0 |
198 | { |
199 | my $destruction_registry = {}; |
200 | |
201 | sub CLONE { |
202 | $destruction_registry = { map |
203 | { defined $_ ? ( refaddr($_) => $_ ) : () } |
204 | values %$destruction_registry |
205 | }; |
206 | } |
207 | |
208 | # This is almost invariably invoked from within DESTROY |
209 | # throwing exceptions won't work |
210 | sub detected_reinvoked_destructor { |
211 | |
212 | # quick "garbage collection" pass - prevents the registry |
213 | # from slowly growing with a bunch of undef-valued keys |
214 | defined $destruction_registry->{$_} or delete $destruction_registry->{$_} |
215 | for keys %$destruction_registry; |
216 | |
217 | if (! length ref $_[0]) { |
218 | printf STDERR '%s() expects a blessed reference %s', |
219 | (caller(0))[3], |
220 | Carp::longmess, |
221 | ; |
222 | return undef; # don't know wtf to do |
223 | } |
224 | elsif (! defined $destruction_registry->{ my $addr = refaddr($_[0]) } ) { |
225 | weaken( $destruction_registry->{$addr} = $_[0] ); |
226 | return 0; |
227 | } |
228 | else { |
229 | carp_unique ( sprintf ( |
230 | 'Preventing *MULTIPLE* DESTROY() invocations on %s - an *EXTREMELY ' |
231 | . 'DANGEROUS* condition which is *ALMOST CERTAINLY GLOBAL* within your ' |
232 | . 'application, affecting *ALL* classes without active protection against ' |
233 | . 'this. Diagnose and fix the root cause ASAP!!!%s', |
234 | refdesc $_[0], |
235 | ( ( $INC{'Devel/StackTrace.pm'} and ! do { local $@; eval { Devel::StackTrace->VERSION(2) } } ) |
236 | ? " (likely culprit Devel::StackTrace\@@{[ Devel::StackTrace->VERSION ]} found in %INC, http://is.gd/D_ST_refcap)" |
237 | : '' |
238 | ) |
239 | )); |
240 | |
241 | return 1; |
242 | } |
243 | } |
244 | } |
245 | |
bf302897 |
246 | sub modver_gt_or_eq ($$) { |
b1dbf716 |
247 | my ($mod, $ver) = @_; |
248 | |
249 | croak "Nonsensical module name supplied" |
250 | if ! defined $mod or ! length $mod; |
251 | |
252 | croak "Nonsensical minimum version supplied" |
253 | if ! defined $ver or $ver =~ /[^0-9\.\_]/; |
254 | |
052a832c |
255 | local $SIG{__WARN__} = sigwarn_silencer( qr/\Qisn't numeric in subroutine entry/ ) |
256 | if SPURIOUS_VERSION_CHECK_WARNINGS; |
b1dbf716 |
257 | |
56270bba |
258 | croak "$mod does not seem to provide a version (perhaps it never loaded)" |
259 | unless $mod->VERSION; |
260 | |
b1dbf716 |
261 | local $@; |
262 | eval { $mod->VERSION($ver) } ? 1 : 0; |
263 | } |
264 | |
9a3d73e9 |
265 | sub modver_gt_or_eq_and_lt ($$$) { |
266 | my ($mod, $v_ge, $v_lt) = @_; |
267 | |
268 | croak "Nonsensical maximum version supplied" |
269 | if ! defined $v_lt or $v_lt =~ /[^0-9\.\_]/; |
270 | |
271 | return ( |
272 | modver_gt_or_eq($mod, $v_ge) |
273 | and |
274 | ! modver_gt_or_eq($mod, $v_lt) |
275 | ) ? 1 : 0; |
276 | } |
277 | |
a9da9b6a |
278 | { |
279 | my $list_ctx_ok_stack_marker; |
280 | |
e89c7968 |
281 | sub fail_on_internal_wantarray () { |
a9da9b6a |
282 | return if $list_ctx_ok_stack_marker; |
283 | |
284 | if (! defined wantarray) { |
285 | croak('fail_on_internal_wantarray() needs a tempvar to save the stack marker guard'); |
286 | } |
287 | |
288 | my $cf = 1; |
289 | while ( ( (caller($cf+1))[3] || '' ) =~ / :: (?: |
290 | |
291 | # these are public API parts that alter behavior on wantarray |
292 | search | search_related | slice | search_literal |
293 | |
294 | | |
295 | |
296 | # these are explicitly prefixed, since we only recognize them as valid |
297 | # escapes when they come from the guts of CDBICompat |
298 | CDBICompat .*? :: (?: search_where | retrieve_from_sql | retrieve_all ) |
299 | |
300 | ) $/x ) { |
301 | $cf++; |
302 | } |
303 | |
e89c7968 |
304 | my ($fr, $want, $argdesc); |
305 | { |
306 | package DB; |
307 | $fr = [ caller($cf) ]; |
308 | $want = ( caller($cf-1) )[5]; |
309 | $argdesc = ref $DB::args[0] |
310 | ? DBIx::Class::_Util::refdesc($DB::args[0]) |
311 | : 'non ' |
312 | ; |
313 | }; |
314 | |
a9da9b6a |
315 | if ( |
e89c7968 |
316 | $want and $fr->[0] =~ /^(?:DBIx::Class|DBICx::)/ |
a9da9b6a |
317 | ) { |
a9da9b6a |
318 | DBIx::Class::Exception->throw( sprintf ( |
e89c7968 |
319 | "Improper use of %s instance in list context at %s line %d\n\n Stacktrace starts", |
320 | $argdesc, @{$fr}[1,2] |
a9da9b6a |
321 | ), 'with_stacktrace'); |
322 | } |
323 | |
324 | my $mark = []; |
325 | weaken ( $list_ctx_ok_stack_marker = $mark ); |
326 | $mark; |
327 | } |
328 | } |
329 | |
77c3a5dc |
330 | sub fail_on_internal_call { |
331 | my ($fr, $argdesc); |
332 | { |
333 | package DB; |
334 | $fr = [ caller(1) ]; |
335 | $argdesc = ref $DB::args[0] |
336 | ? DBIx::Class::_Util::refdesc($DB::args[0]) |
337 | : undef |
338 | ; |
339 | }; |
340 | |
341 | if ( |
342 | $argdesc |
343 | and |
344 | $fr->[0] =~ /^(?:DBIx::Class|DBICx::)/ |
345 | and |
346 | $fr->[1] !~ /\b(?:CDBICompat|ResultSetProxy)\b/ # no point touching there |
347 | ) { |
348 | DBIx::Class::Exception->throw( sprintf ( |
349 | "Illegal internal call of indirect proxy-method %s() with argument %s: examine the last lines of the proxy method deparse below to determine what to call directly instead at %s on line %d\n\n%s\n\n Stacktrace starts", |
350 | $fr->[3], $argdesc, @{$fr}[1,2], ( $fr->[6] || do { |
351 | require B::Deparse; |
352 | no strict 'refs'; |
353 | B::Deparse->new->coderef2text(\&{$fr->[3]}) |
354 | }), |
355 | ), 'with_stacktrace'); |
356 | } |
357 | } |
358 | |
b1dbf716 |
359 | 1; |