StackTrace/Type/Python/Frame.pm

filter: Statementstmt 94.1% 64 / 68 Branchbran 71.4% 20 / 28 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod 0.0% 0 / 2 totaltotal 85.5% 95 / 111 CC 18 SCAR 19.7
CC
18
Cov
87%
parse
12.4
_check_lines_against_regex
5.0
_run_regexes
3.0
CRAP 7.1
« StackTrace/Type/GDB/Frame.pm ↑ Summary StackTrace/Type/GDB.pm »
1 package Parse::StackTrace::Type::Python::Frame;
2 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Parse::StackTrace::Exceptions;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 extends 'Parse::StackTrace::Frame';
7  
8 2 has 'error_location' => (is => 'ro', isa => 'Int');
9  
10 2 our $FUNCTIONLESS_FRAME = qr/
11 ^\s*File\s*"(.+?)", # 1 file
12 \s*(?:line)?\s*(\d+) # 2 line
13 /x;
14  
15 2 our $FULL_FRAME = qr/
16 $FUNCTIONLESS_FRAME,
17 \s*in\s*(\S+) # 3 function
18 /x;
19
20 2 our $DJANGO_FRAME = qr/
21 ^\s*File\s*"(.+?)" # 1 file
22 \s*in\s+(\S+) # 2 function
23 \s*(\d+)\. # 3 line
24 \s+(.+) # 4 code
25 /x;
26  
27 2 ▶use constant SYNTAX_ERROR_CARET => qr/^\s*\^\s*$/;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 22 ▶sub parse {
Subroutine parse: called CC=11 SCAR=25.2
Pod undocumented
30 22 my ($class, %params) = @_;
31 22 my $lines = $params{'lines'};
32 22 my $debug = $params{'debug'};
33
34 22 my ($parsed, $remaining_lines) = $class->_run_regexes($lines, $debug, [
35 { regex => $FULL_FRAME, fields => [qw(file line function)] },
36 { regex => $FUNCTIONLESS_FRAME, fields => [qw(file line)] },
37 { regex => $DJANGO_FRAME, fields => [qw(file function line code)]},
38 ]);
39
40 22 ▶ if ($parsed and !exists $parsed->{function}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parsed and not exists $parsed->{"function"}) 1 21 22
Condition66%2/3
$parsed and not exists $parsed->{"function"}
!ll&&!rl&&r
0211
MC/DC50%1/2
$parsed and not exists $parsed->{"function"}
$parsed not exists $parsed->{"function"}
Truth table66%2/3
A and not(B)
A= $parsedB= not exists $parsed->{"function"}
ABresult
0X0
100
111
41 1 $parsed->{function} = '';
42 }
43
44 22 ▶ if (!$parsed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed) 0 22 22
45 0 my $text = join("\n", @$lines);
46 0 Parse::StackTrace::Exception::NotAFrame->throw(
47 "Not a valid Python stack frame: $text"
48 );
49 }
50  
51 22 ▶ if (!$parsed->{code}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($parsed->{"code"}) 19 3 22
52 19 my $code_line = shift @$remaining_lines;
53 19 foreach my $line (@$remaining_lines) {
54 1 ▶ if ($line =~ SYNTAX_ERROR_CARET) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^\s*\^\s*$/) 1 0 1
55 1 my $caret_pos = index($line, '^');
56 # Account for leading space on the code line
57 1 ▶ if ($code_line =~ /^(\s+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code_line =~ /^(\s+)/) 1 0 1
58 1 $caret_pos -= length($1);
59 }
60 1 $parsed->{error_location} = $caret_pos;
61 1 ▶ print "Error Location: $caret_pos" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
62 1 last;
63 }
64 0 $code_line .= " $line";
65 }
66
67 19 $code_line = trim($code_line);
68 19 ▶ if ($code_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code_line) 19 0 19
69 19 $parsed->{code} = $code_line;
70 }
71 }
72
73 22 ▶ print STDERR "Parsed As: " . Dumper($parsed) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 22 22
74 22 return $class->new(%$parsed);
75 }
76  
77 22 ▶sub _run_regexes {
Subroutine _run_regexes: called CC=3 SCAR=11.0
78 22 my ($class, $lines, $debug, $tests) = @_;
79
80 22 my (@remaining_lines, $parsed);
81 22 foreach my $test (@$tests) {
82 29 @remaining_lines = @$lines;
83 $parsed = _check_lines_against_regex(\@remaining_lines, $test->{regex},
84 29 $test->{fields}, $debug);
85 29 ▶ return ($parsed, \@remaining_lines) if $parsed;
Branch: 1 branch
BranchTrueFalseTotal
if $parsed 22 7 29
86 }
87 0 return ();
88 }
89  
90 29 ▶sub _check_lines_against_regex {
Subroutine _check_lines_against_regex: called CC=5 SCAR=16.1
91 29 my ($lines, $regex, $fields, $debug) = @_;
92  
93 29 my $text = '';
94 29 ▶ while (my $line = shift @$lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = shift @$lines) 52 7 59
95 52 $text .= " $line";
96 52 ▶ if ($text =~ $regex) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /$regex/) 22 30 52
97 22 my @matches = ($1, $2, $3, $4);
98 22 my %parsed;
99 22 ▶ for (my $i = 0; $i < scalar(@$fields); $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$fields) 68 22 90
100 68 $parsed{$fields->[$i]} = $matches[$i];
101 }
102 22 return \%parsed;
103 }
104 }
105
106 7 ▶ print STDERR "Failed Match Against $regex: [$text]\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 7 7
107
108 7 return undef;
109  
110 }
111  
112 19 ▶sub trim {
Subroutine trim: called CC=2 SCAR=7.0
Pod undocumented
113 19 my $str = shift;
114 19 ▶ return undef if !defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 19 19
115 19 $str =~ s/^\s*//;
116 19 $str =~ s/\s*$//;
117 19 return $str;
118 }
119  
120 2 __PACKAGE__->meta->make_immutable;
121  
122 2 1;
123  
124 __END__
« StackTrace/Type/GDB/Frame.pm ↑ Summary StackTrace/Type/GDB.pm »