Bytecode.pm

filter: Statementstmt 64.9% 176 / 271 Branchbran 60.5% 63 / 104 Conditioncond 83.3% 10 / 12 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 40.6% 24 / 59 Podpod 14.2% 4 / 28 totaltotal 58.6% 282 / 481 CC 60 SCAR 21.7
CC
60
Cov
64%
r_object
33.4
disassemble
12.1
r_complex
12.0
CRAP 8.7
↑ Summary Bytecode/v21.pm »
1 package Python::Bytecode;
2 1 ▶use 5.6.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 our $VERSION = "2.7";
7  
8 0 ▶use overload '""' => sub { my $obj = shift;
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=6.9
9 0 "<Code object ".$obj->{name}.", file ".$obj->{filename}." line ".$obj->{lineno}." at ".sprintf('0x%x>',0+$obj);
10 0 ▶ }, "0+" => sub { $_[0] }, fallback => 1;
Subroutine __ANON__: not called CC=1 SCAR=0.2
11  
12 2 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod undocumented
13 2 my ($class, $fh) = (@_);
14 2 my $self = bless { };
15 2 ▶ if (ref $fh) { $self->{fh} = $fh; }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $fh) { } 2 0 2
16 0 else { $self->{stuff} = [ split //, $fh ] }
17  
18 2 my $magic = $self->r_long();
19 2 my $data = _get_data_by_magic($magic);
20 2 $self->{version} = $Python::Bytecode::versions{$magic};
21 # What we use to read words from the source. May be r_short or r_long
22 2 *Python::Bytecode::r_word = $Python::Bytecode::readword{$magic};
23 2 $self->r_long(); # Second magic number
24 2 $self->{mainobj} = $self->r_object();
25 2 $self->_init($data);
26 2 return $self;
27 }
28  
29 2 ▶sub _get_data_by_magic {
Subroutine _get_data_by_magic: called CC=2 SCAR=7.8
30 2 require Python::Bytecode::v21;
31 2 require Python::Bytecode::v22;
32 2 require Python::Bytecode::v23;
33 2 my $magic = shift;
34 2 ▶ unless (exists $Python::Bytecode::data{$magic}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $Python::Bytecode::data{$magic}) 0 2 2
35 0 require Carp;
36 Carp::croak("Unrecognised magic number $magic; Only know Python versions "
37 0 . join ", ", map { "$_ ($Python::Bytecode::versions{$_})" } keys %Python::Bytecode::versions
38 );
39 }
40 2 return $Python::Bytecode::data{$magic};
41 }
42  
43 170 ▶sub r_byte {
Subroutine r_byte: called CC=2 SCAR=8.4
Pod undocumented
44 170 my $self = shift;
45 0 ▶ if (exists $self->{stuff}) { ord shift @{$self->{stuff}};}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'stuff'}) { } 0 170 170
46 170 else { ord getc $self->{fh} }
47 }
48  
49 33 ▶sub r_long {
Subroutine r_long: called CC=1 SCAR=0.0
Pod undocumented
50 1 ▶ use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 33 my $self = shift;
52 33 my $x = $self->r_byte;
53 33 $x |= $self->r_byte << 8;
54 33 $x |= $self->r_byte << 16;
55 33 $x |= $self->r_byte << 24;
56 33 return $x;
57 }
58  
59 5 ▶sub r_short {
Subroutine r_short: called CC=1 SCAR=0.0
Pod undocumented
60 5 my $self = shift;
61 5 my $x = $self->r_byte;
62 5 $x |= $self->r_byte << 8;
63 5 $x |= -($x & 0x8000);
64 5 return $x;
65 }
66  
67 12 ▶sub r_string {
Subroutine r_string: called CC=2 SCAR=7.5
Pod undocumented
68 12 my $self = shift;
69 12 my $length = $self->r_long;
70 12 my $buf;
71 12 ▶ if ( exists $self->{stuff}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'stuff'}) { } 0 12 12
72 0 $buf = join "", splice ( @{$self->{stuff}},0,$length,() );
73 } else {
74 12 read $self->{fh}, $buf, $length;
75 }
76 12 return $buf;
77 }
78  
79 # This really ought to return a real unicode string, rather than a plain
80 # binary string that we fib about
81 0 ▶sub r_unicode {
Subroutine r_unicode: not called CC=2 SCAR=17.9
Pod undocumented
82 0 my $self = shift;
83 0 my $length = $self->r_long;
84 0 my $buf;
85 0 ▶ if ( exists $self->{stuff}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'stuff'}) { } 0 0 0
86 0 $buf = join "", splice ( @{$self->{stuff}},0,$length,() );
87 } else {
88 0 read $self->{fh}, $buf, $length;
89 }
90 0 return $buf;
91 }
92  
93 0 ▶sub r_float {
Subroutine r_float: not called CC=2 SCAR=17.9
Pod undocumented
94 0 my $self = shift;
95 0 my $length = $self->r_byte;
96 0 my $buf;
97 0 ▶ if ( exists $self->{stuff}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'stuff'}) { } 0 0 0
98 0 $buf = join "", splice ( @{$self->{stuff}},0,$length,() );
99 } else {
100 0 read $self->{fh}, $buf, $length;
101 }
102 0 $buf += 0;
103 0 return $buf;
104 }
105  
106 0 ▶sub r_complex {
Subroutine r_complex: not called CC=3 SCAR=24.8
Pod undocumented
107 0 my $self = shift;
108 0 my $length = $self->r_byte;
109 0 my $real;
110 0 ▶ if ( exists $self->{stuff}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'stuff'}) { } 0 0 0
111 0 $real = join "", splice ( @{$self->{stuff}},0,$length,() );
112 } else {
113 0 read $self->{fh}, $real, $length;
114 }
115 0 $real += 0;
116  
117 0 $length = $self->r_byte;
118 0 my $imag;
119 0 ▶ if ( exists $self->{stuff}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'stuff'}) { } 0 0 0
120 0 $imag = join "", splice ( @{$self->{stuff}},0,$length,() );
121 } else {
122 0 read $self->{fh}, $imag, $length;
123 }
124 0 $imag += 0;
125 0 return bless([$real, $imag], "Python::Bytecode::Complex");
126 }
127  
128 28 ▶sub r_object {
Subroutine r_object: called CC=15 SCAR=35.1
Pod undocumented
129 28 my $self = shift;
130 28 my $cooked = shift;
131 28 my $type = chr $self->r_byte();
132 28 ▶ return $self->r_code() if $type eq "c";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "c" 2 26 28
133 26 ▶ if ($cooked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cooked) { } 6 20 26
134 6 ▶ return bless \($self->r_string()), "Python::Bytecode::String" if $type eq "s";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "s" 0 6 6
135 6 ▶ return bless \($self->r_long()), "Python::Bytecode::Long" if $type eq "i";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "i" 2 4 6
136 4 ▶ return bless \do{my $x=undef}, "Python::Bytecode::Undef" if $type eq "N";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "N" 2 2 4
137 } else {
138 20 ▶ return $self->r_string if $type eq "s";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "s" 12 8 20
139 8 ▶ return $self->r_long() if $type eq "i";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "i" 0 8 8
140 8 ▶ return undef if $type eq "N"; # None indeed.
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "N" 0 8 8
141 }
142 10 ▶ if ($type eq "(") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "(") 10 0 10
143 10 my @tuple = $self->r_tuple($cooked);
144 10 ▶ return bless [@tuple], "Python::Bytecode::Tuple" unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 10 0 10
145 0 return @tuple;
146 }
147 0 ▶ if ($type eq 'u') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "u") 0 0 0
148 0 return bless\($self->r_unicode()), "Python::Bytecode::Unicode";
149 }
150 0 ▶ if ($type eq 'l') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "l") 0 0 0
151 0 return bless \($self->r_extralong()), "Python::Bytecode::Extralong";
152 }
153 0 ▶ if ($type eq 'f') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "f") 0 0 0
154 0 return bless \($self->r_float()), "Python::Bytecode::Float";
155 }
156 0 ▶ if ($type eq 'x') {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "x") 0 0 0
157 0 return $self->r_complex();
158 }
159 0 die "Oops! I didn't implement ".ord($type) . " (". length($type) . " bytes)";
160 }
161  
162 10 ▶sub r_tuple {
Subroutine r_tuple: called CC=3 SCAR=11.0
Pod undocumented
163 10 my $self = shift;
164 10 my $cooked = shift;
165 10 my $n = $self->r_long;
166 10 ▶ return () unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 4 6 10
167 6 my @rv;
168 6 push @rv, scalar $self->r_object($cooked) for (1..$n);
169 6 return @rv;
170 }
171  
172 # This is an extended precision long read. It'll likely be incorrect if the size of the
173 # long exceeds the precision of perl's double, and it really ought to generate a
174 # Math::BigInt instead.
175 0 ▶sub r_extralong {
Subroutine r_extralong: not called CC=3 SCAR=24.8
Pod undocumented
176 0 my $self = shift;
177 0 my $n = $self->r_long;
178 0 ▶ my $size = $n<0 ? -$n : $n;
Branch: 1 branch
BranchTrueFalseTotal
$n < 0 ? : 0 0 0
179 0 my $num = 0;
180  
181 0 foreach (1..$size) {
182 0 my $digit = $self->r_short();
183 0 $num *= 32768;
184 0 $num += $digit;
185 }
186  
187 0 return $num;
188  
189 }
190  
191 2 ▶sub r_code {
Subroutine r_code: called CC=9 SCAR=23.2
Pod undocumented
192 2 my $file = shift;
193 2 my $self = bless {bytecode => $file}, 'Python::Bytecode::Codeobj';
194 2 $self->{argcount} = $file->r_word; #
195 2 $self->{nlocals} = $file->r_word; #
196 2 $self->{stacksize}= $file->r_word; #
197 2 $self->{flags} = $file->r_word; #
198 2 ▶ if ($self->{code} = $file->r_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"code"} = $file->r_object) 2 0 2
199 2 ▶ if ($self->{constants}= $file->r_object(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"constants"} = $file->r_object(1)) 2 0 2
200 2 ▶ if ($self->{names} = $file->r_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"names"} = $file->r_object) 2 0 2
201 2 ▶ if ($self->{varnames} = $file->r_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"varnames"} = $file->r_object) 2 0 2
202 2 ▶ if ($self->{freevars} = $file->r_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"freevars"} = $file->r_object) 2 0 2
203 2 ▶ if ($self->{cellvars} = $file->r_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"cellvars"} = $file->r_object) 2 0 2
204 2 ▶ if ($self->{filename} = $file->r_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"filename"} = $file->r_object) 2 0 2
205 2 ▶ if ($self->{name} = $file->r_object) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"name"} = $file->r_object) 2 0 2
206 2 $self->{lineno} = $file->r_word; #
207 2 $self->{lnotab} = $file->r_object;
208 }}}}}}}}
209 2 return $self;
210 }
211  
212 1 for (qw(argcount nlocals stacksize flags code constants names
213 varnames freevars cellvars filename name lineno lnotab)) {
214 1 ▶ no strict q/subs/;
Subroutine BEGIN: called CC=1 SCAR=0.0
215 0 ▶ eval "sub $_ { return \$_[0]->{mainobj}->$_ }";
Subroutine argcount: not called CC=1 SCAR=5.0
Subroutine cellvars: not called CC=1 SCAR=5.0
Subroutine code: not called CC=1 SCAR=5.0
Subroutine constants: not called CC=1 SCAR=5.0
Subroutine filename: called CC=1 SCAR=5.0
Subroutine flags: not called CC=1 SCAR=5.0
Subroutine freevars: not called CC=1 SCAR=5.0
Subroutine lineno: not called CC=1 SCAR=5.0
Subroutine lnotab: not called CC=1 SCAR=5.0
Subroutine name: not called CC=1 SCAR=5.0
Subroutine names: not called CC=1 SCAR=5.0
Subroutine nlocals: not called CC=1 SCAR=5.0
Subroutine stacksize: not called CC=1 SCAR=5.0
Subroutine varnames: not called CC=1 SCAR=5.0
Pod undocumented
Pod undocumented
Pod undocumented
Pod documented
Pod documented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod undocumented
Pod documented
216 }
217  
218 0 ▶sub version {
Subroutine version: not called CC=1 SCAR=6.9
Pod undocumented
219 0 return $_[0]->{version};
220 }
221  
222 1 $Parrot::Bytecode::DATA = <<EOF;
223  
224 # This'll amuse you. It's actually lifted directly from dis.py :)
225 # Instruction opcodes for compiled code
226  
227 def_op('STOP_CODE', 0)
228 def_op('POP_TOP', 1)
229 def_op('ROT_TWO', 2)
230 def_op('ROT_THREE', 3)
231 def_op('DUP_TOP', 4)
232 def_op('ROT_FOUR', 5)
233  
234 def_op('UNARY_POSITIVE', 10)
235 def_op('UNARY_NEGATIVE', 11)
236 def_op('UNARY_NOT', 12)
237 def_op('UNARY_CONVERT', 13)
238  
239 def_op('UNARY_INVERT', 15)
240  
241 def_op('BINARY_POWER', 19)
242  
243 def_op('BINARY_MULTIPLY', 20)
244 def_op('BINARY_DIVIDE', 21)
245 def_op('BINARY_MODULO', 22)
246 def_op('BINARY_ADD', 23)
247 def_op('BINARY_SUBTRACT', 24)
248 def_op('BINARY_SUBSCR', 25)
249  
250 def_op('SLICE+0', 30)
251 def_op('SLICE+1', 31)
252 def_op('SLICE+2', 32)
253 def_op('SLICE+3', 33)
254  
255 def_op('STORE_SLICE+0', 40)
256 def_op('STORE_SLICE+1', 41)
257 def_op('STORE_SLICE+2', 42)
258 def_op('STORE_SLICE+3', 43)
259  
260 def_op('DELETE_SLICE+0', 50)
261 def_op('DELETE_SLICE+1', 51)
262 def_op('DELETE_SLICE+2', 52)
263 def_op('DELETE_SLICE+3', 53)
264  
265 def_op('INPLACE_ADD', 55)
266 def_op('INPLACE_SUBTRACT', 56)
267 def_op('INPLACE_MULTIPLY', 57)
268 def_op('INPLACE_DIVIDE', 58)
269 def_op('INPLACE_MODULO', 59)
270 def_op('STORE_SUBSCR', 60)
271 def_op('DELETE_SUBSCR', 61)
272  
273 def_op('BINARY_LSHIFT', 62)
274 def_op('BINARY_RSHIFT', 63)
275 def_op('BINARY_AND', 64)
276 def_op('BINARY_XOR', 65)
277 def_op('BINARY_OR', 66)
278 def_op('INPLACE_POWER', 67)
279  
280 def_op('PRINT_EXPR', 70)
281 def_op('PRINT_ITEM', 71)
282 def_op('PRINT_NEWLINE', 72)
283 def_op('PRINT_ITEM_TO', 73)
284 def_op('PRINT_NEWLINE_TO', 74)
285 def_op('INPLACE_LSHIFT', 75)
286 def_op('INPLACE_RSHIFT', 76)
287 def_op('INPLACE_AND', 77)
288 def_op('INPLACE_XOR', 78)
289 def_op('INPLACE_OR', 79)
290 def_op('BREAK_LOOP', 80)
291  
292 def_op('LOAD_LOCALS', 82)
293 def_op('RETURN_VALUE', 83)
294 def_op('IMPORT_STAR', 84)
295 def_op('EXEC_STMT', 85)
296  
297 def_op('POP_BLOCK', 87)
298 def_op('END_FINALLY', 88)
299 def_op('BUILD_CLASS', 89)
300  
301 HAVE_ARGUMENT = 90 # Opcodes from here have an argument:
302  
303 name_op('STORE_NAME', 90) # Index in name list
304 name_op('DELETE_NAME', 91) # ""
305 def_op('UNPACK_SEQUENCE', 92) # Number of tuple items
306  
307 name_op('STORE_ATTR', 95) # Index in name list
308 name_op('DELETE_ATTR', 96) # ""
309 name_op('STORE_GLOBAL', 97) # ""
310 name_op('DELETE_GLOBAL', 98) # ""
311 def_op('DUP_TOPX', 99) # number of items to duplicate
312 def_op('LOAD_CONST', 100) # Index in const list
313 hasconst.append(100)
314 name_op('LOAD_NAME', 101) # Index in name list
315 def_op('BUILD_TUPLE', 102) # Number of tuple items
316 def_op('BUILD_LIST', 103) # Number of list items
317 def_op('BUILD_MAP', 104) # Always zero for now
318 name_op('LOAD_ATTR', 105) # Index in name list
319 def_op('COMPARE_OP', 106) # Comparison operator
320 hascompare.append(106)
321 name_op('IMPORT_NAME', 107) # Index in name list
322 name_op('IMPORT_FROM', 108) # Index in name list
323  
324 jrel_op('JUMP_FORWARD', 110) # Number of bytes to skip
325 jrel_op('JUMP_IF_FALSE', 111) # ""
326 jrel_op('JUMP_IF_TRUE', 112) # ""
327 jabs_op('JUMP_ABSOLUTE', 113) # Target byte offset from beginning of code
328 jrel_op('FOR_LOOP', 114) # Number of bytes to skip
329  
330 name_op('LOAD_GLOBAL', 116) # Index in name list
331  
332 jrel_op('SETUP_LOOP', 120) # Distance to target address
333 jrel_op('SETUP_EXCEPT', 121) # ""
334 jrel_op('SETUP_FINALLY', 122) # ""
335  
336 def_op('LOAD_FAST', 124) # Local variable number
337 haslocal.append(124)
338 def_op('STORE_FAST', 125) # Local variable number
339 haslocal.append(125)
340 def_op('DELETE_FAST', 126) # Local variable number
341 haslocal.append(126)
342  
343 def_op('SET_LINENO', 127) # Current line number
344 SET_LINENO = 127
345  
346 def_op('RAISE_VARARGS', 130) # Number of raise arguments (1, 2, or 3)
347 def_op('CALL_FUNCTION', 131) # #args + (#kwargs << 8)
348 def_op('MAKE_FUNCTION', 132) # Number of args with default values
349 def_op('BUILD_SLICE', 133) # Number of items
350  
351 def_op('CALL_FUNCTION_VAR', 140) # #args + (#kwargs << 8)
352 def_op('CALL_FUNCTION_KW', 141) # #args + (#kwargs << 8)
353 def_op('CALL_FUNCTION_VAR_KW', 142) # #args + (#kwargs << 8)
354  
355 def_op('EXTENDED_ARG', 143)
356 EXTENDED_ARG = 143
357  
358 EOF
359  
360 # Set up op code data structures
361 2 ▶sub _init {
Subroutine _init: called CC=8 SCAR=20.8
362 2 my $self = shift;
363 2 my $data = shift;
364 2 my @opnames;
365 my %c; # Natty constants.
366 2 my %has;
367 2 for (split /\n/, $data) { # This ought to come predigested, but I am lazy
368 292 ▶ next if /^#/ or not /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if /^#/ or not /\S/ 49 243 292
Condition100%3/3
/^#/ or not /\S/
l!l&&r!l&&!r
148243
MC/DC100%2/2
/^#/ or not /\S/
/^#/ not /\S/
Truth table100%3/3
A or not(B)
A= /^#/B= not /\S/
ABresult
1X1
011
000
369 243 ▶ if (/^def_op\('([^']+)', (\d+)\)/) { $opnames[$2]=$1; }
Branch: 4 branches
BranchTrueFalseTotal
if (/^def_op\('([^']+)', (\d+)\)/) { } 180 63 243
elsif (/^(jrel|jabs|name)_op\('([^']+)', (\d+)\)/) { } 41 22 63
elsif (/(\w+)\s*=\s*(\d+)/) { } 6 16 22
elsif (/^has(\w+)\.append\((\d+)\)/) { } 16 0 16
370 41 elsif (/^(jrel|jabs|name)_op\('([^']+)', (\d+)\)/) { $opnames[$3]=$2; $has{$1}{$3}++ }
371 6 elsif (/(\w+)\s*=\s*(\d+)/) { $c{$1}=$2; }
372 16 elsif (/^has(\w+)\.append\((\d+)\)/) { $has{$1}{$2}++ }
373 }
374 2 $self->{opnames} = \@opnames;
375 2 $self->{has} = \%has;
376 2 $self->{c} = \%c;
377 }
378  
379 2 ▶sub disassemble {
Subroutine disassemble: called CC=1 SCAR=0.0
Pod documented
380 2 my $self = shift;
381 2 return $self->{mainobj}->disassemble;
382 }
383  
384 # Now we've read in the op tree, disassemble it.
385 package Python::Bytecode::Codeobj;
386  
387 0 ▶use overload '""' => sub { my $obj = shift;
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=6.9
388 0 "<Code object ".$obj->{name}.", file ".$obj->{filename}." line ".$obj->{lineno}." at ".sprintf('0x%x>',0+$obj);
389 0 ▶ }, "0+" => sub { $_[0] }, fallback => 1;
Subroutine __ANON__: not called CC=1 SCAR=0.2
390  
391 1 for (qw(argcount nlocals stacksize flags code constants names
392 varnames freevars cellvars filename name lineno lnotab)) {
393 1 ▶ no strict q/subs/;
Subroutine BEGIN: called CC=1 SCAR=0.0
394 0 ▶ eval "sub $_ { return \$_[0]->{$_} }";
Subroutine argcount: not called CC=1 SCAR=5.0
Subroutine cellvars: not called CC=1 SCAR=5.0
Subroutine code: not called CC=1 SCAR=5.0
Subroutine constants: not called CC=1 SCAR=5.0
Subroutine filename: called CC=1 SCAR=5.0
Subroutine flags: not called CC=1 SCAR=5.0
Subroutine freevars: not called CC=1 SCAR=5.0
Subroutine lineno: not called CC=1 SCAR=5.0
Subroutine lnotab: not called CC=1 SCAR=5.0
Subroutine name: not called CC=1 SCAR=5.0
Subroutine names: not called CC=1 SCAR=5.0
Subroutine nlocals: not called CC=1 SCAR=5.0
Subroutine stacksize: not called CC=1 SCAR=5.0
Subroutine varnames: not called CC=1 SCAR=5.0
395 }
396  
397  
398 2 ▶sub findlabels {
Subroutine findlabels: called CC=5 SCAR=16.1
399 2 my $self = shift;
400 2 my $bytecode = $self->{bytecode};
401 2 my %labels = ();
402 2 my @code = @_;
403 2 my $offset = 0;
404 2 ▶ while (@code) {
Branch: 1 branch
BranchTrueFalseTotal
if (@code) 28 2 30
405 28 my $c = shift @code;
406 28 $offset++;
407 28 ▶ if ($c>=$bytecode->{c}{HAVE_ARGUMENT}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c >= $bytecode->{"c"}{"HAVE_ARGUMENT"}) 18 10 28
408 18 my $arg = shift @code;
409 18 $arg += (256 * shift (@code));
410 18 $offset += 2;
411 18 ▶ if ($bytecode->{has}{jrel}{$c}) { $labels{$offset + $arg}++ };
Branch: 1 branch
BranchTrueFalseTotal
if ($bytecode->{"has"}{"jrel"}{$c}) 4 14 18
412 0 ▶ if ($bytecode->{has}{jabs}{$c}) { $labels{$offset}++ };
Branch: 1 branch
BranchTrueFalseTotal
if ($bytecode->{"has"}{"jabs"}{$c}) 0 18 18
413 }
414 }
415 2 return %labels;
416 }
417  
418 1 my @cmp_op = ('<', '<=', '==', '!=', '>', '>=', 'in', 'not in', 'is', 'is not', 'exception match', 'BAD');
419  
420 4 ▶sub __printconst {
Subroutine __printconst: called CC=4 SCAR=14.2
421 4 my $thing = shift;
422  
423 4 my $class = ref $thing;
424 4 ▶ if ($class =~ /String/ || $class =~ /Long/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /String/ or $class =~ /Long/) 2 2 4
Condition66%2/3
$class =~ /String/ or $class =~ /Long/
l!l&&r!l&&!r
022
MC/DC50%1/2
$class =~ /String/ or $class =~ /Long/
$class =~ /String/ $class =~ /Long/
Truth table66%2/3
A or B
A= $class =~ /String/B= $class =~ /Long/
ABresult
1X1
011
000
425 2 return $$thing;
426 }
427 2 ▶ if ($class =~ /Undef/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class =~ /Undef/) 2 0 2
428 2 return "";
429 }
430 0 return $thing;
431
432 }
433  
434 2 ▶sub disassemble {
Subroutine disassemble: called CC=12 SCAR=24.9
435 2 my $self = shift;
436 2 my $bytecode = $self->{bytecode};
437 2 my @code = map { ord } split //, $self->{code};
438 2 my %labels = $self->findlabels(@code);
439 2 my $offset = 0;
440 2 my $extarg = 0;
441 2 my @dis;
442 2 ▶ while (@code) {
Branch: 1 branch
BranchTrueFalseTotal
if (@code) 28 2 30
443 28 my $c = shift @code;
444 28 ▶ my $text = (($labels{$offset}) ? ">>" : " ");
Branch: 1 branch
BranchTrueFalseTotal
$labels{$offset} ? : 4 24 28
445 28 $text .= sprintf "%4i", $offset;
446 28 $text .= sprintf "%20s", $self->opname($c);
447 28 $offset++;
448 28 my $arg;
449 28 ▶ if ($c>=$bytecode->{c}{HAVE_ARGUMENT}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c >= $bytecode->{"c"}{"HAVE_ARGUMENT"}) 18 10 28
450 18 $arg = shift @code;
451 18 $arg += (256 * shift (@code)) + $extarg;
452 18 $extarg = 0;
453 18 ▶ $extarg = $arg * 65535 if ($c == $bytecode->{c}{EXTENDED_ARG});
Branch: 1 branch
BranchTrueFalseTotal
if $c == $bytecode->{"c"}{"EXTENDED_ARG"} 0 18 18
454 18 $offset+=2;
455 18 $text .= sprintf "%5i", $arg;
456 18 ▶ $text .= " (".__printconst($self->{constants}->[$arg]).")" if (ref $self->{constants}->[$arg] && $bytecode->{has}{const} && $bytecode->{has}{const}{$c});
Branch: 1 branch
BranchTrueFalseTotal
if ref $self->{"constants"}[$arg] and $bytecode->{"has"}{"const"} and $bytecode->{"has"}{"const"}{$c} 4 14 18
Condition83%5/6
ref $self->{"constants"}[$arg] and $bytecode->{"has"}{"const"} and $bytecode->{"has"}{"const"}{$c}
!ll&&!rl&&r
4104
ref $self->{"constants"}[$arg] and $bytecode->{"has"}{"const"}
!ll&&!rl&&r
4014
MC/DC66%2/3
ref $self->{"constants"}[$arg] and $bytecode->{"has"}{"const"} and $bytecode->{"has"}{"const"}{$c}
ref $self->{"constants"}[$arg] $bytecode->{"has"}{"const"} $bytecode->{"has"}{"const"}{$c}
Truth table75%3/4
A and B and C
A= ref $self->{"constants"}[$arg]B= $bytecode->{"has"}{"const"}C= $bytecode->{"has"}{"const"}{$c}
ABCresult
0XX0
10X0
1100
1111
457 18 ▶ $text .= " (".$self->{varnames}->[$arg].")" if ($bytecode->{has}{"local"}{$c});
Branch: 1 branch
BranchTrueFalseTotal
if $bytecode->{"has"}{"local"}{$c} 0 18 18
458 18 ▶ $text .= " [".$self->{names}->[$arg]."]" if ($bytecode->{has}{name}{$c});
Branch: 1 branch
BranchTrueFalseTotal
if $bytecode->{"has"}{"name"}{$c} 6 12 18
459 18 ▶ $text .= " [".$cmp_op[$arg]."]" if ($bytecode->{has}{compare}{$c});
Branch: 1 branch
BranchTrueFalseTotal
if $bytecode->{"has"}{"compare"}{$c} 0 18 18
460 18 ▶ $text .= " (to ".($offset+$arg).")" if ($bytecode->{has}{jrel}{$c});
Branch: 1 branch
BranchTrueFalseTotal
if $bytecode->{"has"}{"jrel"}{$c} 4 14 18
461 }
462 28 push @dis, [$text, $c, $arg];
463 }
464 2 return @dis;
465 }
466  
467 28 ▶sub opname { $_[0]->{bytecode}{opnames}[$_[1]] }
Subroutine opname: called CC=1 SCAR=0.0
468  
469 1 1;
470  
471 =head1 NAME
472  
473 Python::Bytecode - Disassemble and investigate Python bytecode
474  
475 =head1 SYNOPSIS
476  
477 use Python::Bytecode
478 my $bytecode = Python::Bytecode->new($bytecode);
479 my $bytecode = Python::Bytecode->new(FH);
480 for ($bytecode->disassemble) {
481 print $_->[0],"\n"; # Textual representation of disassembly
482 }
483  
484 foreach my $constant (@{$bytecode->constants()}) {
485 if ($constant->can('disassemble')) {
486 print "code constant:\n";
487 for ($constant->disassemble) {
488 print $_->[0], "\n";
489 }
490 }
491 }
492  
493 =head1 DESCRIPTION
494  
495 C<Python::Bytecode> accepts a string or filehandle contain Python
496 bytecode and puts it into a format you can manipulate.
497  
498 =head1 METHODS
499  
500 =over 3
501  
502 =item C<disassemble>
503  
504 This is the basic method for getting at the actual code. It returns an
505 array representing the individual operations in the bytecode stream.
506 Each element is a reference to a three-element array containing
507 a textual representation of the disassembly, the opcode number, (the
508 C<opname()> function can be used to turn this into an op name) and
509 the argument to the op, if any.
510  
511 =item C<constants>
512  
513 This returns an array reflecting the constants table of the code object.
514 Some operations such as C<LOAD_CONST> refer to constants by index in
515 this array.
516  
517 =item C<labels>
518  
519 Similar to C<constants>, some operations branch to labels by index
520 in this table.
521  
522 =item C<varnames>
523  
524 Again, when variables are referred to by name, the names are stored
525 as an index into this table.
526  
527 =item C<filename>
528  
529 The filename from which this compiled bytecode is derived.
530  
531 =back
532  
533 There are other methods, but you can read the code to find them. It's
534 not hard, and besides, it's probably easiest to work off the textual
535 representation of the disassembly anyway.
536  
537 =head1 STRUCTURE
538  
539 The structure of the decoded bytecode file is reasonably simple.
540  
541 The output of the C<new> method is an object that represents the fully
542 parsed bytecode file. This object contains the information about the
543 bytecode file, as well as the top-level code object for the file.
544  
545 Each python code object in the bytecode file has its own perl object
546 that represents it. This object can be disassembled, has its own
547 constants (which themselves may be code objects) and its own variables.
548  
549 The module completely decodes the bytecode object when the bytecode
550 file is handed to the C<new> method, but to get all the pieces of the
551 bytecode may require digging into the constants of each code object.
552  
553 =head1 PERPETRATORS
554  
555 Simon Cozens, C<simon@cpan.org>. Mutation for Python 2.3 by Dan
556 Sugalski C<dan@sidhe.org>
557  
558 =head1 LICENSE
559  
560 This code is licensed under the same terms as Perl itself.
561  
↑ Summary Bytecode/v21.pm »