45
45
# http://www.opensource.org/licenses/bsd-license.php
46
46
#
47
47
48
+ from __future__ import print_function
49
+
48
50
49
51
from collections import deque
50
52
import os
69
71
# Controls warning_print
70
72
print_warnings = 1
71
73
# Controls debug_print
72
- debug = 0
74
+ debug = 1 if os . environ . get ( "CPPHEADERPARSER_DEBUG" ) == "1" else 0
73
75
# Controls trace_print
74
76
debug_trace = 0
75
77
@@ -82,25 +84,31 @@ def raise_exc(e, src_e):
82
84
raise e
83
85
84
86
85
- def error_print (arg ):
87
+ def error_print (fmt , * args ):
86
88
if print_errors :
87
- print (("[%4d] %s" % (inspect .currentframe ().f_back .f_lineno , arg )))
89
+ fmt = "[%4d] " + fmt
90
+ args = (inspect .currentframe ().f_back .f_lineno ,) + args
91
+ print (fmt % args )
88
92
89
93
90
- def warning_print (arg ):
94
+ def warning_print (fmt , * args ):
91
95
if print_warnings :
92
- print (("[%4d] %s" % (inspect .currentframe ().f_back .f_lineno , arg )))
96
+ fmt = "[%4d] " + fmt
97
+ args = (inspect .currentframe ().f_back .f_lineno ,) + args
98
+ print (fmt % args )
93
99
94
100
95
- def debug_print (arg ):
101
+ def debug_print (fmt , * args ):
96
102
if debug :
97
- print (("[%4d] %s" % (inspect .currentframe ().f_back .f_lineno , arg )))
103
+ fmt = "[%4d] " + fmt
104
+ args = (inspect .currentframe ().f_back .f_lineno ,) + args
105
+ print (fmt % args )
98
106
99
107
100
- def trace_print (* arg ):
108
+ def trace_print (* args ):
101
109
if debug_trace :
102
110
sys .stdout .write ("[%s] " % (inspect .currentframe ().f_back .f_lineno ))
103
- for a in arg :
111
+ for a in args :
104
112
sys .stdout .write ("%s " % a )
105
113
sys .stdout .write ("\n " )
106
114
@@ -392,7 +400,7 @@ def _consume_parens(stack):
392
400
393
401
394
402
def _parse_template_decl (stack ):
395
- debug_print ("_parse_template_decl: %s" % stack )
403
+ debug_print ("_parse_template_decl: %s" , stack )
396
404
params = []
397
405
param = CppTemplateParam ()
398
406
i = 0
@@ -492,7 +500,7 @@ def _parse_cppclass_name(c, stack):
492
500
493
501
494
502
def _parse_cpp_base (stack ):
495
- debug_print ("Parsing base: %s" % stack )
503
+ debug_print ("Parsing base: %s" , stack )
496
504
inherits = []
497
505
i = 0
498
506
sl = len (stack )
@@ -634,8 +642,8 @@ def __init__(self, nameStack, curTemplate, doxygen, location):
634
642
self ._public_forward_declares = []
635
643
self ["namespace" ] = ""
636
644
637
- debug_print ("Class: %s" % nameStack )
638
- debug_print ("Template: %s" % curTemplate )
645
+ debug_print ("Class: %s" , nameStack )
646
+ debug_print ("Template: %s" , curTemplate )
639
647
640
648
if len (nameStack ) < 2 :
641
649
nameStack .insert (1 , "" ) # anonymous struct
@@ -911,8 +919,8 @@ def show(self):
911
919
return "\n \t \t " .join (r )
912
920
913
921
def __init__ (self , nameStack , curClass , methinfo , curTemplate , doxygen , location ):
914
- debug_print ("Method: %s" % nameStack )
915
- debug_print ("Template: %s" % curTemplate )
922
+ debug_print ("Method: %s" , nameStack )
923
+ debug_print ("Template: %s" , curTemplate )
916
924
917
925
if doxygen :
918
926
self ["doxygen" ] = doxygen
@@ -994,10 +1002,10 @@ def __init__(self, nameStack, curClass, methinfo, curTemplate, doxygen, location
994
1002
995
1003
paramsStack = self ._params_helper1 (nameStack )
996
1004
997
- debug_print ("curTemplate: %s" % curTemplate )
1005
+ debug_print ("curTemplate: %s" , curTemplate )
998
1006
if curTemplate :
999
1007
self ["template" ] = curTemplate
1000
- debug_print ("SET self['template'] to `%s`" % self ["template" ])
1008
+ debug_print ("SET self['template'] to `%s`" , self ["template" ])
1001
1009
1002
1010
params = []
1003
1011
# See if there is a doxygen comment for the variable
@@ -1118,7 +1126,7 @@ class CppVariable(_CppVariable):
1118
1126
Vars = []
1119
1127
1120
1128
def __init__ (self , nameStack , doxygen , location , ** kwargs ):
1121
- debug_print ("trace %s" % nameStack )
1129
+ debug_print ("trace %s" , nameStack )
1122
1130
if len (nameStack ) and nameStack [0 ] == "extern" :
1123
1131
self ["extern" ] = True
1124
1132
del nameStack [0 ]
@@ -1130,7 +1138,7 @@ def __init__(self, nameStack, doxygen, location, **kwargs):
1130
1138
arrayStack = nameStack [nameStack .index ("[" ) :]
1131
1139
if nameStack .count ("[" ) > 1 :
1132
1140
debug_print ("Multi dimensional array" )
1133
- debug_print ("arrayStack=%s" % arrayStack )
1141
+ debug_print ("arrayStack=%s" , arrayStack )
1134
1142
nums = [x for x in arrayStack if x .isdigit ()]
1135
1143
# Calculate size by multiplying all dimensions
1136
1144
p = 1
@@ -1153,7 +1161,7 @@ def __init__(self, nameStack, doxygen, location, **kwargs):
1153
1161
if doxygen :
1154
1162
self ["doxygen" ] = doxygen
1155
1163
1156
- debug_print ("Variable: %s" % nameStack )
1164
+ debug_print ("Variable: %s" , nameStack )
1157
1165
1158
1166
set_location_info (self , location )
1159
1167
self ["function_pointer" ] = 0
@@ -1163,7 +1171,7 @@ def __init__(self, nameStack, doxygen, location, **kwargs):
1163
1171
self ["type" ] = nameStack [0 ]
1164
1172
self ["name" ] = ""
1165
1173
else :
1166
- error_print (_stack_ )
1174
+ error_print ("%s" , _stack_ )
1167
1175
assert 0
1168
1176
1169
1177
elif is_function_pointer_stack (nameStack ): # function pointer
@@ -1596,7 +1604,7 @@ def finalize_vars(self):
1596
1604
var ["fundamental" ] = True
1597
1605
1598
1606
elif var ["parent" ]:
1599
- warning_print ("WARN unresolved %s" % _tag )
1607
+ warning_print ("WARN unresolved %s" , _tag )
1600
1608
var ["ctypes_type" ] = "ctypes.c_void_p"
1601
1609
var ["unresolved" ] = True
1602
1610
@@ -1710,7 +1718,7 @@ def finalize_vars(self):
1710
1718
elif tag .startswith (
1711
1719
"_"
1712
1720
): # assume starting with underscore is not important for wrapping
1713
- warning_print ("WARN unresolved %s" % _tag )
1721
+ warning_print ("WARN unresolved %s" , _tag )
1714
1722
var ["ctypes_type" ] = "ctypes.c_void_p"
1715
1723
var ["unresolved" ] = True
1716
1724
@@ -1803,9 +1811,7 @@ def finalize_vars(self):
1803
1811
trace_print ("Adding #include %s" % macro )
1804
1812
self .includes .append (re .split ("[\t ]+" , macro , 1 )[1 ].strip ())
1805
1813
else :
1806
- debug_print (
1807
- "Cant detect what to do with precomp macro '%s'" % macro
1808
- )
1814
+ debug_print ("Cant detect what to do with precomp macro '%s'" , macro )
1809
1815
except :
1810
1816
pass
1811
1817
self ._precomp_macro_buf = None
@@ -1887,7 +1893,7 @@ def finalize(self):
1887
1893
klass = self .classes [b ]
1888
1894
meth ["returns_class" ] = a + "::" + b
1889
1895
elif "<" in b and ">" in b :
1890
- warning_print ("WARN-can not return template: %s" % b )
1896
+ warning_print ("WARN-can not return template: %s" , b )
1891
1897
meth ["returns_unknown" ] = True
1892
1898
elif b in self .global_enums :
1893
1899
enum = self .global_enums [b ]
@@ -2250,8 +2256,9 @@ def _evaluate_property_stack(self, clearStack=True, addToVar=None):
2250
2256
):
2251
2257
self .nameStack .insert (0 , filteredParseHistory [- 1 ]["item" ]["name" ])
2252
2258
debug_print (
2253
- "DEANONYMOIZING %s to type '%s'"
2254
- % (self .nameStack [1 ], self .nameStack [0 ])
2259
+ "DEANONYMOIZING %s to type '%s'" ,
2260
+ self .nameStack [1 ],
2261
+ self .nameStack [0 ],
2255
2262
)
2256
2263
if "," in self .nameStack : # Maybe we have a variable list
2257
2264
# Figure out what part is the variable separator but remember templates of function pointer
@@ -2333,7 +2340,7 @@ def _evaluate_class_stack(self):
2333
2340
else : # struct
2334
2341
self .curAccessSpecifier = "public"
2335
2342
debug_print (
2336
- "curAccessSpecifier changed/defaulted to %s" % self .curAccessSpecifier
2343
+ "curAccessSpecifier changed/defaulted to %s" , self .curAccessSpecifier
2337
2344
)
2338
2345
if self .nameStack [0 ] == "union" :
2339
2346
newClass = CppUnion (
@@ -2518,7 +2525,7 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
2518
2525
self .accessSpecifierStack = []
2519
2526
self .accessSpecifierScratch = []
2520
2527
debug_print (
2521
- "curAccessSpecifier changed/defaulted to %s" % self .curAccessSpecifier
2528
+ "curAccessSpecifier changed/defaulted to %s" , self .curAccessSpecifier
2522
2529
)
2523
2530
self .initextra ()
2524
2531
# Old namestacks for a given level
@@ -2600,7 +2607,7 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
2600
2607
if locEnd :
2601
2608
# Strip it out but keep the linecount the same so line numbers are right
2602
2609
match_str = headerFileStr [locStart :locEnd ]
2603
- debug_print ("Striping out '%s'" % match_str )
2610
+ debug_print ("Striping out '%s'" , match_str )
2604
2611
num_newlines = len ([a for a in match_str if a == "\n " ])
2605
2612
headerFileStr = headerFileStr .replace (
2606
2613
headerFileStr [locStart :locEnd ], "\n " * num_newlines
@@ -2637,7 +2644,7 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
2637
2644
):
2638
2645
self .anon_union_counter [1 ] -= 1
2639
2646
tok .value = TagStr (tok .value , location = tok .location )
2640
- # debug_print("TOK: %s"% tok)
2647
+ # debug_print("TOK: %s", tok)
2641
2648
if tok .type == "NAME" :
2642
2649
if tok .value in self .IGNORE_NAMES :
2643
2650
continue
@@ -2662,7 +2669,7 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
2662
2669
self .stack .append (tok .value )
2663
2670
2664
2671
if tok .type in ("PRECOMP_MACRO" , "PRECOMP_MACRO_CONT" ):
2665
- debug_print ("PRECOMP: %s" % tok )
2672
+ debug_print ("PRECOMP: %s" , tok )
2666
2673
self ._precomp_macro_buf .append (tok .value )
2667
2674
self .stack = []
2668
2675
self .nameStack = []
@@ -2723,7 +2730,7 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
2723
2730
# self.stack = []; print 'BRACE DEPTH', self.braceDepth, 'NS', len(self.nameSpaces)
2724
2731
if self .curClass :
2725
2732
debug_print (
2726
- "CURBD %s" % self ._classes_brace_level [self .curClass ]
2733
+ "CURBD %s" , self ._classes_brace_level [self .curClass ]
2727
2734
)
2728
2735
2729
2736
if (self .braceDepth == 0 ) or (
@@ -2746,7 +2753,7 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
2746
2753
pass
2747
2754
elif tok .type in _namestack_str_tokens :
2748
2755
if tok .value in ignoreSymbols :
2749
- debug_print ("Ignore symbol %s" % tok .value )
2756
+ debug_print ("Ignore symbol %s" , tok .value )
2750
2757
elif tok .value == "class" :
2751
2758
self .nameStack .append (tok .value )
2752
2759
elif tok .value in supportedAccessSpecifier :
@@ -2764,8 +2771,8 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
2764
2771
self .curAccessSpecifier = tok .value
2765
2772
self .accessSpecifierScratch .append (tok .value )
2766
2773
debug_print (
2767
- "curAccessSpecifier updated to %s"
2768
- % self .curAccessSpecifier
2774
+ "curAccessSpecifier updated to %s" ,
2775
+ self .curAccessSpecifier ,
2769
2776
)
2770
2777
self .stack = []
2771
2778
else :
@@ -2831,7 +2838,8 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
2831
2838
)
2832
2839
else :
2833
2840
msg = "Error parsing %s%s\n Error around: %s" % (
2834
- self .headerFileName , context ,
2841
+ self .headerFileName ,
2842
+ context ,
2835
2843
" " .join (self .nameStack ),
2836
2844
)
2837
2845
@@ -2956,8 +2964,10 @@ def _evaluate_stack(self, token=None):
2956
2964
nameStackCopy = self .nameStack [:]
2957
2965
2958
2966
debug_print (
2959
- "Evaluating stack %s\n BraceDepth: %s (called from %d)"
2960
- % (self .nameStack , self .braceDepth , inspect .currentframe ().f_back .f_lineno )
2967
+ "Evaluating stack %s\n BraceDepth: %s (called from %d)" ,
2968
+ self .nameStack ,
2969
+ self .braceDepth ,
2970
+ inspect .currentframe ().f_back .f_lineno ,
2961
2971
)
2962
2972
2963
2973
# Handle special case of overloading operator ()
@@ -2968,9 +2978,9 @@ def _evaluate_stack(self, token=None):
2968
2978
self .nameStack [operator_index ] = "operator()"
2969
2979
2970
2980
if len (self .curClass ):
2971
- debug_print ("%s (%s) " % ( self .curClass , self .curAccessSpecifier ) )
2981
+ debug_print ("%s (%s) " , self .curClass , self .curAccessSpecifier )
2972
2982
else :
2973
- debug_print ("<anonymous> (%s) " % self .curAccessSpecifier )
2983
+ debug_print ("<anonymous> (%s) " , self .curAccessSpecifier )
2974
2984
2975
2985
# Filter special case of array with casting in it
2976
2986
try :
@@ -2981,7 +2991,7 @@ def _evaluate_stack(self, token=None):
2981
2991
self .nameStack = (
2982
2992
self .nameStack [: bracePos + 1 ] + self .nameStack [endParen + 1 :]
2983
2993
)
2984
- debug_print ("Filtered namestack to=%s" % self .nameStack )
2994
+ debug_print ("Filtered namestack to=%s" , self .nameStack )
2985
2995
except :
2986
2996
pass
2987
2997
0 commit comments