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 | |
37873f78 |
37 | IV_SIZE => $Config{ivsize}, |
00882d2c |
38 | |
39 | OS_NAME => $^O, |
37873f78 |
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 | |
841efcb3 |
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'; |
bf302897 |
57 | use Scalar::Util qw(weaken blessed reftype); |
3705e3b2 |
58 | use List::Util qw(first); |
b1dbf716 |
59 | |
7f9a3f70 |
60 | # DO NOT edit away without talking to riba first, he will just put it back |
61 | # BEGIN pre-Moo2 import block |
62 | BEGIN { |
63 | my $initial_fatal_bits = (${^WARNING_BITS}||'') & $warnings::DeadBits{all}; |
64 | local $ENV{PERL_STRICTURES_EXTRA} = 0; |
65 | require Sub::Quote; Sub::Quote->import('quote_sub'); |
66 | ${^WARNING_BITS} &= ( $initial_fatal_bits | ~ $warnings::DeadBits{all} ); |
67 | } |
68 | sub qsub ($) { goto "e_sub } # no point depping on new Moo just for this |
69 | # END pre-Moo2 import block |
70 | |
b1dbf716 |
71 | use base 'Exporter'; |
3705e3b2 |
72 | our @EXPORT_OK = qw( |
77c3a5dc |
73 | sigwarn_silencer modver_gt_or_eq |
74 | fail_on_internal_wantarray fail_on_internal_call |
8433421f |
75 | refdesc refcount hrefaddr is_exception |
7f9a3f70 |
76 | quote_sub qsub perlstring |
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 | |
8433421f |
94 | sub hrefaddr ($) { sprintf '0x%x', &Scalar::Util::refaddr||0 } |
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], |
104 | Scalar::Util::refaddr($_[0]), |
105 | ; |
106 | } |
bf302897 |
107 | |
108 | sub refcount ($) { |
dac7972a |
109 | croak "Expecting a reference" if ! length ref $_[0]; |
110 | |
111 | require B; |
112 | # No tempvars - must operate on $_[0], otherwise the pad |
113 | # will count as an extra ref |
114 | B::svref_2object($_[0])->REFCNT; |
115 | } |
116 | |
841efcb3 |
117 | sub is_exception ($) { |
118 | my $e = $_[0]; |
119 | |
a0414138 |
120 | # this is not strictly correct - an eval setting $@ to undef |
121 | # is *not* the same as an eval setting $@ to '' |
122 | # but for the sake of simplicity assume the following for |
123 | # the time being |
124 | return 0 unless defined $e; |
125 | |
841efcb3 |
126 | my ($not_blank, $suberror); |
127 | { |
128 | local $@; |
129 | eval { |
130 | $not_blank = ($e ne '') ? 1 : 0; |
131 | 1; |
132 | } or $suberror = $@; |
133 | } |
134 | |
135 | if (defined $suberror) { |
136 | if (length (my $class = blessed($e) )) { |
137 | carp_unique( sprintf( |
9bea2000 |
138 | 'External exception class %s implements partial (broken) overloading ' |
139 | . 'preventing its instances from being used in simple ($x eq $y) ' |
841efcb3 |
140 | . 'comparisons. Given Perl\'s "globally cooperative" exception ' |
141 | . 'handling this type of brokenness is extremely dangerous on ' |
142 | . 'exception objects, as it may (and often does) result in silent ' |
143 | . '"exception substitution". DBIx::Class tries to work around this ' |
144 | . 'as much as possible, but other parts of your software stack may ' |
145 | . 'not be even aware of this. Please submit a bugreport against the ' |
146 | . 'distribution containing %s and in the meantime apply a fix similar ' |
147 | . 'to the one shown at %s, in order to ensure your exception handling ' |
148 | . 'is saner application-wide. What follows is the actual error text ' |
149 | . "as generated by Perl itself:\n\n%s\n ", |
9bea2000 |
150 | $class, |
841efcb3 |
151 | $class, |
152 | 'http://v.gd/DBIC_overload_tempfix/', |
153 | $suberror, |
154 | )); |
155 | |
156 | # workaround, keeps spice flowing |
157 | $not_blank = ("$e" ne '') ? 1 : 0; |
158 | } |
159 | else { |
160 | # not blessed yet failed the 'ne'... this makes 0 sense... |
161 | # just throw further |
162 | die $suberror |
163 | } |
164 | } |
165 | |
166 | return $not_blank; |
167 | } |
168 | |
bf302897 |
169 | sub modver_gt_or_eq ($$) { |
b1dbf716 |
170 | my ($mod, $ver) = @_; |
171 | |
172 | croak "Nonsensical module name supplied" |
173 | if ! defined $mod or ! length $mod; |
174 | |
175 | croak "Nonsensical minimum version supplied" |
176 | if ! defined $ver or $ver =~ /[^0-9\.\_]/; |
177 | |
052a832c |
178 | local $SIG{__WARN__} = sigwarn_silencer( qr/\Qisn't numeric in subroutine entry/ ) |
179 | if SPURIOUS_VERSION_CHECK_WARNINGS; |
b1dbf716 |
180 | |
56270bba |
181 | croak "$mod does not seem to provide a version (perhaps it never loaded)" |
182 | unless $mod->VERSION; |
183 | |
b1dbf716 |
184 | local $@; |
185 | eval { $mod->VERSION($ver) } ? 1 : 0; |
186 | } |
187 | |
a9da9b6a |
188 | { |
189 | my $list_ctx_ok_stack_marker; |
190 | |
e89c7968 |
191 | sub fail_on_internal_wantarray () { |
a9da9b6a |
192 | return if $list_ctx_ok_stack_marker; |
193 | |
194 | if (! defined wantarray) { |
195 | croak('fail_on_internal_wantarray() needs a tempvar to save the stack marker guard'); |
196 | } |
197 | |
198 | my $cf = 1; |
199 | while ( ( (caller($cf+1))[3] || '' ) =~ / :: (?: |
200 | |
201 | # these are public API parts that alter behavior on wantarray |
202 | search | search_related | slice | search_literal |
203 | |
204 | | |
205 | |
206 | # these are explicitly prefixed, since we only recognize them as valid |
207 | # escapes when they come from the guts of CDBICompat |
208 | CDBICompat .*? :: (?: search_where | retrieve_from_sql | retrieve_all ) |
209 | |
210 | ) $/x ) { |
211 | $cf++; |
212 | } |
213 | |
e89c7968 |
214 | my ($fr, $want, $argdesc); |
215 | { |
216 | package DB; |
217 | $fr = [ caller($cf) ]; |
218 | $want = ( caller($cf-1) )[5]; |
219 | $argdesc = ref $DB::args[0] |
220 | ? DBIx::Class::_Util::refdesc($DB::args[0]) |
221 | : 'non ' |
222 | ; |
223 | }; |
224 | |
a9da9b6a |
225 | if ( |
e89c7968 |
226 | $want and $fr->[0] =~ /^(?:DBIx::Class|DBICx::)/ |
a9da9b6a |
227 | ) { |
a9da9b6a |
228 | DBIx::Class::Exception->throw( sprintf ( |
e89c7968 |
229 | "Improper use of %s instance in list context at %s line %d\n\n Stacktrace starts", |
230 | $argdesc, @{$fr}[1,2] |
a9da9b6a |
231 | ), 'with_stacktrace'); |
232 | } |
233 | |
234 | my $mark = []; |
235 | weaken ( $list_ctx_ok_stack_marker = $mark ); |
236 | $mark; |
237 | } |
238 | } |
239 | |
77c3a5dc |
240 | sub fail_on_internal_call { |
241 | my ($fr, $argdesc); |
242 | { |
243 | package DB; |
244 | $fr = [ caller(1) ]; |
245 | $argdesc = ref $DB::args[0] |
246 | ? DBIx::Class::_Util::refdesc($DB::args[0]) |
247 | : undef |
248 | ; |
249 | }; |
250 | |
251 | if ( |
252 | $argdesc |
253 | and |
254 | $fr->[0] =~ /^(?:DBIx::Class|DBICx::)/ |
255 | and |
256 | $fr->[1] !~ /\b(?:CDBICompat|ResultSetProxy)\b/ # no point touching there |
257 | ) { |
258 | DBIx::Class::Exception->throw( sprintf ( |
259 | "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", |
260 | $fr->[3], $argdesc, @{$fr}[1,2], ( $fr->[6] || do { |
261 | require B::Deparse; |
262 | no strict 'refs'; |
263 | B::Deparse->new->coderef2text(\&{$fr->[3]}) |
264 | }), |
265 | ), 'with_stacktrace'); |
266 | } |
267 | } |
268 | |
b1dbf716 |
269 | 1; |