Commit | Line | Data |
b1dbf716 |
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 | |
37873f78 |
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 | |
8d73fcd4 |
20 | BROKEN_GOTO => ($] < '5.008003') ? 1 : 0, |
21 | |
37873f78 |
22 | HAS_ITHREADS => $Config{useithreads} ? 1 : 0, |
23 | |
24 | # ::Runmode would only be loaded by DBICTest, which in turn implies t/ |
25 | DBICTEST => eval { DBICTest::RunMode->is_author } ? 1 : 0, |
26 | |
27 | # During 5.13 dev cycle HELEMs started to leak on copy |
6cfb1d2f |
28 | # add an escape for these perls ON SMOKERS - a user will still get death |
29 | PEEPEENESS => ( eval { DBICTest::RunMode->is_smoker } && ($] >= 5.013005 and $] <= 5.013006) ), |
37873f78 |
30 | |
1b658919 |
31 | SHUFFLE_UNORDERED_RESULTSETS => $ENV{DBIC_SHUFFLE_UNORDERED_RESULTSETS} ? 1 : 0, |
32 | |
37873f78 |
33 | ASSERT_NO_INTERNAL_WANTARRAY => $ENV{DBIC_ASSERT_NO_INTERNAL_WANTARRAY} ? 1 : 0, |
34 | |
77c3a5dc |
35 | ASSERT_NO_INTERNAL_INDIRECT_CALLS => $ENV{DBIC_ASSERT_NO_INTERNAL_INDIRECT_CALLS} ? 1 : 0, |
36 | |
2fdeef65 |
37 | STRESSTEST_UTF8_UPGRADE_GENERATED_COLLAPSER_SOURCE => $ENV{DBIC_STRESSTEST_UTF8_UPGRADE_GENERATED_COLLAPSER_SOURCE} ? 1 : 0, |
38 | |
37873f78 |
39 | IV_SIZE => $Config{ivsize}, |
00882d2c |
40 | |
41 | OS_NAME => $^O, |
37873f78 |
42 | }; |
43 | |
44 | if ($] < 5.009_005) { |
45 | require MRO::Compat; |
46 | constant->import( OLD_MRO => 1 ); |
47 | } |
48 | else { |
49 | require mro; |
50 | constant->import( OLD_MRO => 0 ); |
51 | } |
52 | } |
53 | |
841efcb3 |
54 | # FIXME - this is not supposed to be here |
55 | # Carp::Skip to the rescue soon |
56 | use DBIx::Class::Carp '^DBIx::Class|^DBICTest'; |
57 | |
d7d45bdc |
58 | use B (); |
841efcb3 |
59 | use Carp 'croak'; |
d7d45bdc |
60 | use Storable 'nfreeze'; |
bf302897 |
61 | use Scalar::Util qw(weaken blessed reftype); |
3705e3b2 |
62 | use List::Util qw(first); |
b1dbf716 |
63 | |
7f9a3f70 |
64 | # DO NOT edit away without talking to riba first, he will just put it back |
65 | # BEGIN pre-Moo2 import block |
66 | BEGIN { |
67 | my $initial_fatal_bits = (${^WARNING_BITS}||'') & $warnings::DeadBits{all}; |
cbd7f87a |
68 | |
7f9a3f70 |
69 | local $ENV{PERL_STRICTURES_EXTRA} = 0; |
cbd7f87a |
70 | # load all of these now, so that lazy-loading does not escape |
71 | # the current PERL_STRICTURES_EXTRA setting |
72 | require Sub::Quote; |
73 | require Sub::Defer; |
74 | |
75 | Sub::Quote->import('quote_sub'); |
7f9a3f70 |
76 | ${^WARNING_BITS} &= ( $initial_fatal_bits | ~ $warnings::DeadBits{all} ); |
77 | } |
78 | sub qsub ($) { goto "e_sub } # no point depping on new Moo just for this |
79 | # END pre-Moo2 import block |
80 | |
b1dbf716 |
81 | use base 'Exporter'; |
3705e3b2 |
82 | our @EXPORT_OK = qw( |
77c3a5dc |
83 | sigwarn_silencer modver_gt_or_eq |
84 | fail_on_internal_wantarray fail_on_internal_call |
8433421f |
85 | refdesc refcount hrefaddr is_exception |
b34d9331 |
86 | quote_sub qsub perlstring serialize |
facd0e8e |
87 | UNRESOLVABLE_CONDITION |
3705e3b2 |
88 | ); |
052a832c |
89 | |
facd0e8e |
90 | use constant UNRESOLVABLE_CONDITION => \ '1 = 0'; |
91 | |
bf302897 |
92 | sub sigwarn_silencer ($) { |
052a832c |
93 | my $pattern = shift; |
94 | |
95 | croak "Expecting a regexp" if ref $pattern ne 'Regexp'; |
96 | |
97 | my $orig_sig_warn = $SIG{__WARN__} || sub { CORE::warn(@_) }; |
98 | |
99 | return sub { &$orig_sig_warn unless $_[0] =~ $pattern }; |
100 | } |
b1dbf716 |
101 | |
01b25f12 |
102 | sub perlstring ($) { q{"}. quotemeta( shift ). q{"} }; |
103 | |
8433421f |
104 | sub hrefaddr ($) { sprintf '0x%x', &Scalar::Util::refaddr||0 } |
105 | |
106 | sub refdesc ($) { |
107 | croak "Expecting a reference" if ! length ref $_[0]; |
108 | |
109 | # be careful not to trigger stringification, |
110 | # reuse @_ as a scratch-pad |
111 | sprintf '%s%s(0x%x)', |
112 | ( defined( $_[1] = blessed $_[0]) ? "$_[1]=" : '' ), |
113 | reftype $_[0], |
114 | Scalar::Util::refaddr($_[0]), |
115 | ; |
116 | } |
bf302897 |
117 | |
118 | sub refcount ($) { |
dac7972a |
119 | croak "Expecting a reference" if ! length ref $_[0]; |
120 | |
dac7972a |
121 | # No tempvars - must operate on $_[0], otherwise the pad |
122 | # will count as an extra ref |
123 | B::svref_2object($_[0])->REFCNT; |
124 | } |
125 | |
b34d9331 |
126 | sub serialize ($) { |
b34d9331 |
127 | local $Storable::canonical = 1; |
d7d45bdc |
128 | nfreeze($_[0]); |
b34d9331 |
129 | } |
130 | |
841efcb3 |
131 | sub is_exception ($) { |
132 | my $e = $_[0]; |
133 | |
a0414138 |
134 | # this is not strictly correct - an eval setting $@ to undef |
135 | # is *not* the same as an eval setting $@ to '' |
136 | # but for the sake of simplicity assume the following for |
137 | # the time being |
138 | return 0 unless defined $e; |
139 | |
841efcb3 |
140 | my ($not_blank, $suberror); |
141 | { |
142 | local $@; |
143 | eval { |
144 | $not_blank = ($e ne '') ? 1 : 0; |
145 | 1; |
146 | } or $suberror = $@; |
147 | } |
148 | |
149 | if (defined $suberror) { |
150 | if (length (my $class = blessed($e) )) { |
151 | carp_unique( sprintf( |
9bea2000 |
152 | 'External exception class %s implements partial (broken) overloading ' |
153 | . 'preventing its instances from being used in simple ($x eq $y) ' |
841efcb3 |
154 | . 'comparisons. Given Perl\'s "globally cooperative" exception ' |
155 | . 'handling this type of brokenness is extremely dangerous on ' |
156 | . 'exception objects, as it may (and often does) result in silent ' |
157 | . '"exception substitution". DBIx::Class tries to work around this ' |
158 | . 'as much as possible, but other parts of your software stack may ' |
159 | . 'not be even aware of this. Please submit a bugreport against the ' |
160 | . 'distribution containing %s and in the meantime apply a fix similar ' |
161 | . 'to the one shown at %s, in order to ensure your exception handling ' |
162 | . 'is saner application-wide. What follows is the actual error text ' |
163 | . "as generated by Perl itself:\n\n%s\n ", |
9bea2000 |
164 | $class, |
841efcb3 |
165 | $class, |
166 | 'http://v.gd/DBIC_overload_tempfix/', |
167 | $suberror, |
168 | )); |
169 | |
170 | # workaround, keeps spice flowing |
171 | $not_blank = ("$e" ne '') ? 1 : 0; |
172 | } |
173 | else { |
174 | # not blessed yet failed the 'ne'... this makes 0 sense... |
175 | # just throw further |
176 | die $suberror |
177 | } |
178 | } |
179 | |
180 | return $not_blank; |
181 | } |
182 | |
bf302897 |
183 | sub modver_gt_or_eq ($$) { |
b1dbf716 |
184 | my ($mod, $ver) = @_; |
185 | |
186 | croak "Nonsensical module name supplied" |
187 | if ! defined $mod or ! length $mod; |
188 | |
189 | croak "Nonsensical minimum version supplied" |
190 | if ! defined $ver or $ver =~ /[^0-9\.\_]/; |
191 | |
052a832c |
192 | local $SIG{__WARN__} = sigwarn_silencer( qr/\Qisn't numeric in subroutine entry/ ) |
193 | if SPURIOUS_VERSION_CHECK_WARNINGS; |
b1dbf716 |
194 | |
56270bba |
195 | croak "$mod does not seem to provide a version (perhaps it never loaded)" |
196 | unless $mod->VERSION; |
197 | |
b1dbf716 |
198 | local $@; |
199 | eval { $mod->VERSION($ver) } ? 1 : 0; |
200 | } |
201 | |
a9da9b6a |
202 | { |
203 | my $list_ctx_ok_stack_marker; |
204 | |
e89c7968 |
205 | sub fail_on_internal_wantarray () { |
a9da9b6a |
206 | return if $list_ctx_ok_stack_marker; |
207 | |
208 | if (! defined wantarray) { |
209 | croak('fail_on_internal_wantarray() needs a tempvar to save the stack marker guard'); |
210 | } |
211 | |
212 | my $cf = 1; |
213 | while ( ( (caller($cf+1))[3] || '' ) =~ / :: (?: |
214 | |
215 | # these are public API parts that alter behavior on wantarray |
216 | search | search_related | slice | search_literal |
217 | |
218 | | |
219 | |
220 | # these are explicitly prefixed, since we only recognize them as valid |
221 | # escapes when they come from the guts of CDBICompat |
222 | CDBICompat .*? :: (?: search_where | retrieve_from_sql | retrieve_all ) |
223 | |
224 | ) $/x ) { |
225 | $cf++; |
226 | } |
227 | |
e89c7968 |
228 | my ($fr, $want, $argdesc); |
229 | { |
230 | package DB; |
231 | $fr = [ caller($cf) ]; |
232 | $want = ( caller($cf-1) )[5]; |
233 | $argdesc = ref $DB::args[0] |
234 | ? DBIx::Class::_Util::refdesc($DB::args[0]) |
235 | : 'non ' |
236 | ; |
237 | }; |
238 | |
a9da9b6a |
239 | if ( |
e89c7968 |
240 | $want and $fr->[0] =~ /^(?:DBIx::Class|DBICx::)/ |
a9da9b6a |
241 | ) { |
a9da9b6a |
242 | DBIx::Class::Exception->throw( sprintf ( |
e89c7968 |
243 | "Improper use of %s instance in list context at %s line %d\n\n Stacktrace starts", |
244 | $argdesc, @{$fr}[1,2] |
a9da9b6a |
245 | ), 'with_stacktrace'); |
246 | } |
247 | |
248 | my $mark = []; |
249 | weaken ( $list_ctx_ok_stack_marker = $mark ); |
250 | $mark; |
251 | } |
252 | } |
253 | |
77c3a5dc |
254 | sub fail_on_internal_call { |
255 | my ($fr, $argdesc); |
256 | { |
257 | package DB; |
258 | $fr = [ caller(1) ]; |
259 | $argdesc = ref $DB::args[0] |
260 | ? DBIx::Class::_Util::refdesc($DB::args[0]) |
261 | : undef |
262 | ; |
263 | }; |
264 | |
265 | if ( |
266 | $argdesc |
267 | and |
268 | $fr->[0] =~ /^(?:DBIx::Class|DBICx::)/ |
269 | and |
270 | $fr->[1] !~ /\b(?:CDBICompat|ResultSetProxy)\b/ # no point touching there |
271 | ) { |
272 | DBIx::Class::Exception->throw( sprintf ( |
273 | "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", |
274 | $fr->[3], $argdesc, @{$fr}[1,2], ( $fr->[6] || do { |
275 | require B::Deparse; |
276 | no strict 'refs'; |
277 | B::Deparse->new->coderef2text(\&{$fr->[3]}) |
278 | }), |
279 | ), 'with_stacktrace'); |
280 | } |
281 | } |
282 | |
b1dbf716 |
283 | 1; |