Edit File by line
/home/barbar84/www/wp-conte.../plugins/sujqvwi/AnonR/smanonr..../lib64/python3..../site-pac.../cffi
File: cparser.py
from . import model
[0] Fix | Delete
from .commontypes import COMMON_TYPES, resolve_common_type
[1] Fix | Delete
from .error import FFIError, CDefError
[2] Fix | Delete
try:
[3] Fix | Delete
from . import _pycparser as pycparser
[4] Fix | Delete
except ImportError:
[5] Fix | Delete
import pycparser
[6] Fix | Delete
import weakref, re, sys
[7] Fix | Delete
[8] Fix | Delete
try:
[9] Fix | Delete
if sys.version_info < (3,):
[10] Fix | Delete
import thread as _thread
[11] Fix | Delete
else:
[12] Fix | Delete
import _thread
[13] Fix | Delete
lock = _thread.allocate_lock()
[14] Fix | Delete
except ImportError:
[15] Fix | Delete
lock = None
[16] Fix | Delete
[17] Fix | Delete
CDEF_SOURCE_STRING = "<cdef source string>"
[18] Fix | Delete
_r_comment = re.compile(r"/\*.*?\*/|//([^\n\\]|\\.)*?$",
[19] Fix | Delete
re.DOTALL | re.MULTILINE)
[20] Fix | Delete
_r_define = re.compile(r"^\s*#\s*define\s+([A-Za-z_][A-Za-z_0-9]*)"
[21] Fix | Delete
r"\b((?:[^\n\\]|\\.)*?)$",
[22] Fix | Delete
re.DOTALL | re.MULTILINE)
[23] Fix | Delete
_r_partial_enum = re.compile(r"=\s*\.\.\.\s*[,}]|\.\.\.\s*\}")
[24] Fix | Delete
_r_enum_dotdotdot = re.compile(r"__dotdotdot\d+__$")
[25] Fix | Delete
_r_partial_array = re.compile(r"\[\s*\.\.\.\s*\]")
[26] Fix | Delete
_r_words = re.compile(r"\w+|\S")
[27] Fix | Delete
_parser_cache = None
[28] Fix | Delete
_r_int_literal = re.compile(r"-?0?x?[0-9a-f]+[lu]*$", re.IGNORECASE)
[29] Fix | Delete
_r_stdcall1 = re.compile(r"\b(__stdcall|WINAPI)\b")
[30] Fix | Delete
_r_stdcall2 = re.compile(r"[(]\s*(__stdcall|WINAPI)\b")
[31] Fix | Delete
_r_cdecl = re.compile(r"\b__cdecl\b")
[32] Fix | Delete
_r_extern_python = re.compile(r'\bextern\s*"'
[33] Fix | Delete
r'(Python|Python\s*\+\s*C|C\s*\+\s*Python)"\s*.')
[34] Fix | Delete
_r_star_const_space = re.compile( # matches "* const "
[35] Fix | Delete
r"[*]\s*((const|volatile|restrict)\b\s*)+")
[36] Fix | Delete
_r_int_dotdotdot = re.compile(r"(\b(int|long|short|signed|unsigned|char)\s*)+"
[37] Fix | Delete
r"\.\.\.")
[38] Fix | Delete
_r_float_dotdotdot = re.compile(r"\b(double|float)\s*\.\.\.")
[39] Fix | Delete
[40] Fix | Delete
def _get_parser():
[41] Fix | Delete
global _parser_cache
[42] Fix | Delete
if _parser_cache is None:
[43] Fix | Delete
_parser_cache = pycparser.CParser()
[44] Fix | Delete
return _parser_cache
[45] Fix | Delete
[46] Fix | Delete
def _workaround_for_old_pycparser(csource):
[47] Fix | Delete
# Workaround for a pycparser issue (fixed between pycparser 2.10 and
[48] Fix | Delete
# 2.14): "char*const***" gives us a wrong syntax tree, the same as
[49] Fix | Delete
# for "char***(*const)". This means we can't tell the difference
[50] Fix | Delete
# afterwards. But "char(*const(***))" gives us the right syntax
[51] Fix | Delete
# tree. The issue only occurs if there are several stars in
[52] Fix | Delete
# sequence with no parenthesis inbetween, just possibly qualifiers.
[53] Fix | Delete
# Attempt to fix it by adding some parentheses in the source: each
[54] Fix | Delete
# time we see "* const" or "* const *", we add an opening
[55] Fix | Delete
# parenthesis before each star---the hard part is figuring out where
[56] Fix | Delete
# to close them.
[57] Fix | Delete
parts = []
[58] Fix | Delete
while True:
[59] Fix | Delete
match = _r_star_const_space.search(csource)
[60] Fix | Delete
if not match:
[61] Fix | Delete
break
[62] Fix | Delete
#print repr(''.join(parts)+csource), '=>',
[63] Fix | Delete
parts.append(csource[:match.start()])
[64] Fix | Delete
parts.append('('); closing = ')'
[65] Fix | Delete
parts.append(match.group()) # e.g. "* const "
[66] Fix | Delete
endpos = match.end()
[67] Fix | Delete
if csource.startswith('*', endpos):
[68] Fix | Delete
parts.append('('); closing += ')'
[69] Fix | Delete
level = 0
[70] Fix | Delete
i = endpos
[71] Fix | Delete
while i < len(csource):
[72] Fix | Delete
c = csource[i]
[73] Fix | Delete
if c == '(':
[74] Fix | Delete
level += 1
[75] Fix | Delete
elif c == ')':
[76] Fix | Delete
if level == 0:
[77] Fix | Delete
break
[78] Fix | Delete
level -= 1
[79] Fix | Delete
elif c in ',;=':
[80] Fix | Delete
if level == 0:
[81] Fix | Delete
break
[82] Fix | Delete
i += 1
[83] Fix | Delete
csource = csource[endpos:i] + closing + csource[i:]
[84] Fix | Delete
#print repr(''.join(parts)+csource)
[85] Fix | Delete
parts.append(csource)
[86] Fix | Delete
return ''.join(parts)
[87] Fix | Delete
[88] Fix | Delete
def _preprocess_extern_python(csource):
[89] Fix | Delete
# input: `extern "Python" int foo(int);` or
[90] Fix | Delete
# `extern "Python" { int foo(int); }`
[91] Fix | Delete
# output:
[92] Fix | Delete
# void __cffi_extern_python_start;
[93] Fix | Delete
# int foo(int);
[94] Fix | Delete
# void __cffi_extern_python_stop;
[95] Fix | Delete
#
[96] Fix | Delete
# input: `extern "Python+C" int foo(int);`
[97] Fix | Delete
# output:
[98] Fix | Delete
# void __cffi_extern_python_plus_c_start;
[99] Fix | Delete
# int foo(int);
[100] Fix | Delete
# void __cffi_extern_python_stop;
[101] Fix | Delete
parts = []
[102] Fix | Delete
while True:
[103] Fix | Delete
match = _r_extern_python.search(csource)
[104] Fix | Delete
if not match:
[105] Fix | Delete
break
[106] Fix | Delete
endpos = match.end() - 1
[107] Fix | Delete
#print
[108] Fix | Delete
#print ''.join(parts)+csource
[109] Fix | Delete
#print '=>'
[110] Fix | Delete
parts.append(csource[:match.start()])
[111] Fix | Delete
if 'C' in match.group(1):
[112] Fix | Delete
parts.append('void __cffi_extern_python_plus_c_start; ')
[113] Fix | Delete
else:
[114] Fix | Delete
parts.append('void __cffi_extern_python_start; ')
[115] Fix | Delete
if csource[endpos] == '{':
[116] Fix | Delete
# grouping variant
[117] Fix | Delete
closing = csource.find('}', endpos)
[118] Fix | Delete
if closing < 0:
[119] Fix | Delete
raise CDefError("'extern \"Python\" {': no '}' found")
[120] Fix | Delete
if csource.find('{', endpos + 1, closing) >= 0:
[121] Fix | Delete
raise NotImplementedError("cannot use { } inside a block "
[122] Fix | Delete
"'extern \"Python\" { ... }'")
[123] Fix | Delete
parts.append(csource[endpos+1:closing])
[124] Fix | Delete
csource = csource[closing+1:]
[125] Fix | Delete
else:
[126] Fix | Delete
# non-grouping variant
[127] Fix | Delete
semicolon = csource.find(';', endpos)
[128] Fix | Delete
if semicolon < 0:
[129] Fix | Delete
raise CDefError("'extern \"Python\": no ';' found")
[130] Fix | Delete
parts.append(csource[endpos:semicolon+1])
[131] Fix | Delete
csource = csource[semicolon+1:]
[132] Fix | Delete
parts.append(' void __cffi_extern_python_stop;')
[133] Fix | Delete
#print ''.join(parts)+csource
[134] Fix | Delete
#print
[135] Fix | Delete
parts.append(csource)
[136] Fix | Delete
return ''.join(parts)
[137] Fix | Delete
[138] Fix | Delete
def _preprocess(csource):
[139] Fix | Delete
# Remove comments. NOTE: this only work because the cdef() section
[140] Fix | Delete
# should not contain any string literal!
[141] Fix | Delete
csource = _r_comment.sub(' ', csource)
[142] Fix | Delete
# Remove the "#define FOO x" lines
[143] Fix | Delete
macros = {}
[144] Fix | Delete
for match in _r_define.finditer(csource):
[145] Fix | Delete
macroname, macrovalue = match.groups()
[146] Fix | Delete
macrovalue = macrovalue.replace('\\\n', '').strip()
[147] Fix | Delete
macros[macroname] = macrovalue
[148] Fix | Delete
csource = _r_define.sub('', csource)
[149] Fix | Delete
#
[150] Fix | Delete
if pycparser.__version__ < '2.14':
[151] Fix | Delete
csource = _workaround_for_old_pycparser(csource)
[152] Fix | Delete
#
[153] Fix | Delete
# BIG HACK: replace WINAPI or __stdcall with "volatile const".
[154] Fix | Delete
# It doesn't make sense for the return type of a function to be
[155] Fix | Delete
# "volatile volatile const", so we abuse it to detect __stdcall...
[156] Fix | Delete
# Hack number 2 is that "int(volatile *fptr)();" is not valid C
[157] Fix | Delete
# syntax, so we place the "volatile" before the opening parenthesis.
[158] Fix | Delete
csource = _r_stdcall2.sub(' volatile volatile const(', csource)
[159] Fix | Delete
csource = _r_stdcall1.sub(' volatile volatile const ', csource)
[160] Fix | Delete
csource = _r_cdecl.sub(' ', csource)
[161] Fix | Delete
#
[162] Fix | Delete
# Replace `extern "Python"` with start/end markers
[163] Fix | Delete
csource = _preprocess_extern_python(csource)
[164] Fix | Delete
#
[165] Fix | Delete
# Replace "[...]" with "[__dotdotdotarray__]"
[166] Fix | Delete
csource = _r_partial_array.sub('[__dotdotdotarray__]', csource)
[167] Fix | Delete
#
[168] Fix | Delete
# Replace "...}" with "__dotdotdotNUM__}". This construction should
[169] Fix | Delete
# occur only at the end of enums; at the end of structs we have "...;}"
[170] Fix | Delete
# and at the end of vararg functions "...);". Also replace "=...[,}]"
[171] Fix | Delete
# with ",__dotdotdotNUM__[,}]": this occurs in the enums too, when
[172] Fix | Delete
# giving an unknown value.
[173] Fix | Delete
matches = list(_r_partial_enum.finditer(csource))
[174] Fix | Delete
for number, match in enumerate(reversed(matches)):
[175] Fix | Delete
p = match.start()
[176] Fix | Delete
if csource[p] == '=':
[177] Fix | Delete
p2 = csource.find('...', p, match.end())
[178] Fix | Delete
assert p2 > p
[179] Fix | Delete
csource = '%s,__dotdotdot%d__ %s' % (csource[:p], number,
[180] Fix | Delete
csource[p2+3:])
[181] Fix | Delete
else:
[182] Fix | Delete
assert csource[p:p+3] == '...'
[183] Fix | Delete
csource = '%s __dotdotdot%d__ %s' % (csource[:p], number,
[184] Fix | Delete
csource[p+3:])
[185] Fix | Delete
# Replace "int ..." or "unsigned long int..." with "__dotdotdotint__"
[186] Fix | Delete
csource = _r_int_dotdotdot.sub(' __dotdotdotint__ ', csource)
[187] Fix | Delete
# Replace "float ..." or "double..." with "__dotdotdotfloat__"
[188] Fix | Delete
csource = _r_float_dotdotdot.sub(' __dotdotdotfloat__ ', csource)
[189] Fix | Delete
# Replace all remaining "..." with the same name, "__dotdotdot__",
[190] Fix | Delete
# which is declared with a typedef for the purpose of C parsing.
[191] Fix | Delete
return csource.replace('...', ' __dotdotdot__ '), macros
[192] Fix | Delete
[193] Fix | Delete
def _common_type_names(csource):
[194] Fix | Delete
# Look in the source for what looks like usages of types from the
[195] Fix | Delete
# list of common types. A "usage" is approximated here as the
[196] Fix | Delete
# appearance of the word, minus a "definition" of the type, which
[197] Fix | Delete
# is the last word in a "typedef" statement. Approximative only
[198] Fix | Delete
# but should be fine for all the common types.
[199] Fix | Delete
look_for_words = set(COMMON_TYPES)
[200] Fix | Delete
look_for_words.add(';')
[201] Fix | Delete
look_for_words.add(',')
[202] Fix | Delete
look_for_words.add('(')
[203] Fix | Delete
look_for_words.add(')')
[204] Fix | Delete
look_for_words.add('typedef')
[205] Fix | Delete
words_used = set()
[206] Fix | Delete
is_typedef = False
[207] Fix | Delete
paren = 0
[208] Fix | Delete
previous_word = ''
[209] Fix | Delete
for word in _r_words.findall(csource):
[210] Fix | Delete
if word in look_for_words:
[211] Fix | Delete
if word == ';':
[212] Fix | Delete
if is_typedef:
[213] Fix | Delete
words_used.discard(previous_word)
[214] Fix | Delete
look_for_words.discard(previous_word)
[215] Fix | Delete
is_typedef = False
[216] Fix | Delete
elif word == 'typedef':
[217] Fix | Delete
is_typedef = True
[218] Fix | Delete
paren = 0
[219] Fix | Delete
elif word == '(':
[220] Fix | Delete
paren += 1
[221] Fix | Delete
elif word == ')':
[222] Fix | Delete
paren -= 1
[223] Fix | Delete
elif word == ',':
[224] Fix | Delete
if is_typedef and paren == 0:
[225] Fix | Delete
words_used.discard(previous_word)
[226] Fix | Delete
look_for_words.discard(previous_word)
[227] Fix | Delete
else: # word in COMMON_TYPES
[228] Fix | Delete
words_used.add(word)
[229] Fix | Delete
previous_word = word
[230] Fix | Delete
return words_used
[231] Fix | Delete
[232] Fix | Delete
[233] Fix | Delete
class Parser(object):
[234] Fix | Delete
[235] Fix | Delete
def __init__(self):
[236] Fix | Delete
self._declarations = {}
[237] Fix | Delete
self._included_declarations = set()
[238] Fix | Delete
self._anonymous_counter = 0
[239] Fix | Delete
self._structnode2type = weakref.WeakKeyDictionary()
[240] Fix | Delete
self._options = {}
[241] Fix | Delete
self._int_constants = {}
[242] Fix | Delete
self._recomplete = []
[243] Fix | Delete
self._uses_new_feature = None
[244] Fix | Delete
[245] Fix | Delete
def _parse(self, csource):
[246] Fix | Delete
csource, macros = _preprocess(csource)
[247] Fix | Delete
# XXX: for more efficiency we would need to poke into the
[248] Fix | Delete
# internals of CParser... the following registers the
[249] Fix | Delete
# typedefs, because their presence or absence influences the
[250] Fix | Delete
# parsing itself (but what they are typedef'ed to plays no role)
[251] Fix | Delete
ctn = _common_type_names(csource)
[252] Fix | Delete
typenames = []
[253] Fix | Delete
for name in sorted(self._declarations):
[254] Fix | Delete
if name.startswith('typedef '):
[255] Fix | Delete
name = name[8:]
[256] Fix | Delete
typenames.append(name)
[257] Fix | Delete
ctn.discard(name)
[258] Fix | Delete
typenames += sorted(ctn)
[259] Fix | Delete
#
[260] Fix | Delete
csourcelines = []
[261] Fix | Delete
csourcelines.append('# 1 "<cdef automatic initialization code>"')
[262] Fix | Delete
for typename in typenames:
[263] Fix | Delete
csourcelines.append('typedef int %s;' % typename)
[264] Fix | Delete
csourcelines.append('typedef int __dotdotdotint__, __dotdotdotfloat__,'
[265] Fix | Delete
' __dotdotdot__;')
[266] Fix | Delete
# this forces pycparser to consider the following in the file
[267] Fix | Delete
# called <cdef source string> from line 1
[268] Fix | Delete
csourcelines.append('# 1 "%s"' % (CDEF_SOURCE_STRING,))
[269] Fix | Delete
csourcelines.append(csource)
[270] Fix | Delete
fullcsource = '\n'.join(csourcelines)
[271] Fix | Delete
if lock is not None:
[272] Fix | Delete
lock.acquire() # pycparser is not thread-safe...
[273] Fix | Delete
try:
[274] Fix | Delete
ast = _get_parser().parse(fullcsource)
[275] Fix | Delete
except pycparser.c_parser.ParseError as e:
[276] Fix | Delete
self.convert_pycparser_error(e, csource)
[277] Fix | Delete
finally:
[278] Fix | Delete
if lock is not None:
[279] Fix | Delete
lock.release()
[280] Fix | Delete
# csource will be used to find buggy source text
[281] Fix | Delete
return ast, macros, csource
[282] Fix | Delete
[283] Fix | Delete
def _convert_pycparser_error(self, e, csource):
[284] Fix | Delete
# xxx look for "<cdef source string>:NUM:" at the start of str(e)
[285] Fix | Delete
# and interpret that as a line number. This will not work if
[286] Fix | Delete
# the user gives explicit ``# NUM "FILE"`` directives.
[287] Fix | Delete
line = None
[288] Fix | Delete
msg = str(e)
[289] Fix | Delete
match = re.match(r"%s:(\d+):" % (CDEF_SOURCE_STRING,), msg)
[290] Fix | Delete
if match:
[291] Fix | Delete
linenum = int(match.group(1), 10)
[292] Fix | Delete
csourcelines = csource.splitlines()
[293] Fix | Delete
if 1 <= linenum <= len(csourcelines):
[294] Fix | Delete
line = csourcelines[linenum-1]
[295] Fix | Delete
return line
[296] Fix | Delete
[297] Fix | Delete
def convert_pycparser_error(self, e, csource):
[298] Fix | Delete
line = self._convert_pycparser_error(e, csource)
[299] Fix | Delete
[300] Fix | Delete
msg = str(e)
[301] Fix | Delete
if line:
[302] Fix | Delete
msg = 'cannot parse "%s"\n%s' % (line.strip(), msg)
[303] Fix | Delete
else:
[304] Fix | Delete
msg = 'parse error\n%s' % (msg,)
[305] Fix | Delete
raise CDefError(msg)
[306] Fix | Delete
[307] Fix | Delete
def parse(self, csource, override=False, packed=False, dllexport=False):
[308] Fix | Delete
prev_options = self._options
[309] Fix | Delete
try:
[310] Fix | Delete
self._options = {'override': override,
[311] Fix | Delete
'packed': packed,
[312] Fix | Delete
'dllexport': dllexport}
[313] Fix | Delete
self._internal_parse(csource)
[314] Fix | Delete
finally:
[315] Fix | Delete
self._options = prev_options
[316] Fix | Delete
[317] Fix | Delete
def _internal_parse(self, csource):
[318] Fix | Delete
ast, macros, csource = self._parse(csource)
[319] Fix | Delete
# add the macros
[320] Fix | Delete
self._process_macros(macros)
[321] Fix | Delete
# find the first "__dotdotdot__" and use that as a separator
[322] Fix | Delete
# between the repeated typedefs and the real csource
[323] Fix | Delete
iterator = iter(ast.ext)
[324] Fix | Delete
for decl in iterator:
[325] Fix | Delete
if decl.name == '__dotdotdot__':
[326] Fix | Delete
break
[327] Fix | Delete
else:
[328] Fix | Delete
assert 0
[329] Fix | Delete
current_decl = None
[330] Fix | Delete
#
[331] Fix | Delete
try:
[332] Fix | Delete
self._inside_extern_python = '__cffi_extern_python_stop'
[333] Fix | Delete
for decl in iterator:
[334] Fix | Delete
current_decl = decl
[335] Fix | Delete
if isinstance(decl, pycparser.c_ast.Decl):
[336] Fix | Delete
self._parse_decl(decl)
[337] Fix | Delete
elif isinstance(decl, pycparser.c_ast.Typedef):
[338] Fix | Delete
if not decl.name:
[339] Fix | Delete
raise CDefError("typedef does not declare any name",
[340] Fix | Delete
decl)
[341] Fix | Delete
quals = 0
[342] Fix | Delete
if (isinstance(decl.type.type, pycparser.c_ast.IdentifierType) and
[343] Fix | Delete
decl.type.type.names[-1].startswith('__dotdotdot')):
[344] Fix | Delete
realtype = self._get_unknown_type(decl)
[345] Fix | Delete
elif (isinstance(decl.type, pycparser.c_ast.PtrDecl) and
[346] Fix | Delete
isinstance(decl.type.type, pycparser.c_ast.TypeDecl) and
[347] Fix | Delete
isinstance(decl.type.type.type,
[348] Fix | Delete
pycparser.c_ast.IdentifierType) and
[349] Fix | Delete
decl.type.type.type.names[-1].startswith('__dotdotdot')):
[350] Fix | Delete
realtype = self._get_unknown_ptr_type(decl)
[351] Fix | Delete
else:
[352] Fix | Delete
realtype, quals = self._get_type_and_quals(
[353] Fix | Delete
decl.type, name=decl.name, partial_length_ok=True)
[354] Fix | Delete
self._declare('typedef ' + decl.name, realtype, quals=quals)
[355] Fix | Delete
elif decl.__class__.__name__ == 'Pragma':
[356] Fix | Delete
pass # skip pragma, only in pycparser 2.15
[357] Fix | Delete
else:
[358] Fix | Delete
raise CDefError("unexpected <%s>: this construct is valid "
[359] Fix | Delete
"C but not valid in cdef()" %
[360] Fix | Delete
decl.__class__.__name__, decl)
[361] Fix | Delete
except CDefError as e:
[362] Fix | Delete
if len(e.args) == 1:
[363] Fix | Delete
e.args = e.args + (current_decl,)
[364] Fix | Delete
raise
[365] Fix | Delete
except FFIError as e:
[366] Fix | Delete
msg = self._convert_pycparser_error(e, csource)
[367] Fix | Delete
if msg:
[368] Fix | Delete
e.args = (e.args[0] + "\n *** Err: %s" % msg,)
[369] Fix | Delete
raise
[370] Fix | Delete
[371] Fix | Delete
def _add_constants(self, key, val):
[372] Fix | Delete
if key in self._int_constants:
[373] Fix | Delete
if self._int_constants[key] == val:
[374] Fix | Delete
return # ignore identical double declarations
[375] Fix | Delete
raise FFIError(
[376] Fix | Delete
"multiple declarations of constant: %s" % (key,))
[377] Fix | Delete
self._int_constants[key] = val
[378] Fix | Delete
[379] Fix | Delete
def _add_integer_constant(self, name, int_str):
[380] Fix | Delete
int_str = int_str.lower().rstrip("ul")
[381] Fix | Delete
neg = int_str.startswith('-')
[382] Fix | Delete
if neg:
[383] Fix | Delete
int_str = int_str[1:]
[384] Fix | Delete
# "010" is not valid oct in py3
[385] Fix | Delete
if (int_str.startswith("0") and int_str != '0'
[386] Fix | Delete
and not int_str.startswith("0x")):
[387] Fix | Delete
int_str = "0o" + int_str[1:]
[388] Fix | Delete
pyvalue = int(int_str, 0)
[389] Fix | Delete
if neg:
[390] Fix | Delete
pyvalue = -pyvalue
[391] Fix | Delete
self._add_constants(name, pyvalue)
[392] Fix | Delete
self._declare('macro ' + name, pyvalue)
[393] Fix | Delete
[394] Fix | Delete
def _process_macros(self, macros):
[395] Fix | Delete
for key, value in macros.items():
[396] Fix | Delete
value = value.strip()
[397] Fix | Delete
if _r_int_literal.match(value):
[398] Fix | Delete
self._add_integer_constant(key, value)
[399] Fix | Delete
elif value == '...':
[400] Fix | Delete
self._declare('macro ' + key, value)
[401] Fix | Delete
else:
[402] Fix | Delete
raise CDefError(
[403] Fix | Delete
'only supports one of the following syntax:\n'
[404] Fix | Delete
' #define %s ... (literally dot-dot-dot)\n'
[405] Fix | Delete
' #define %s NUMBER (with NUMBER an integer'
[406] Fix | Delete
' constant, decimal/hex/octal)\n'
[407] Fix | Delete
'got:\n'
[408] Fix | Delete
' #define %s %s'
[409] Fix | Delete
% (key, key, key, value))
[410] Fix | Delete
[411] Fix | Delete
def _declare_function(self, tp, quals, decl):
[412] Fix | Delete
tp = self._get_type_pointer(tp, quals)
[413] Fix | Delete
if self._options.get('dllexport'):
[414] Fix | Delete
tag = 'dllexport_python '
[415] Fix | Delete
elif self._inside_extern_python == '__cffi_extern_python_start':
[416] Fix | Delete
tag = 'extern_python '
[417] Fix | Delete
elif self._inside_extern_python == '__cffi_extern_python_plus_c_start':
[418] Fix | Delete
tag = 'extern_python_plus_c '
[419] Fix | Delete
else:
[420] Fix | Delete
tag = 'function '
[421] Fix | Delete
self._declare(tag + decl.name, tp)
[422] Fix | Delete
[423] Fix | Delete
def _parse_decl(self, decl):
[424] Fix | Delete
node = decl.type
[425] Fix | Delete
if isinstance(node, pycparser.c_ast.FuncDecl):
[426] Fix | Delete
tp, quals = self._get_type_and_quals(node, name=decl.name)
[427] Fix | Delete
assert isinstance(tp, model.RawFunctionType)
[428] Fix | Delete
self._declare_function(tp, quals, decl)
[429] Fix | Delete
else:
[430] Fix | Delete
if isinstance(node, pycparser.c_ast.Struct):
[431] Fix | Delete
self._get_struct_union_enum_type('struct', node)
[432] Fix | Delete
elif isinstance(node, pycparser.c_ast.Union):
[433] Fix | Delete
self._get_struct_union_enum_type('union', node)
[434] Fix | Delete
elif isinstance(node, pycparser.c_ast.Enum):
[435] Fix | Delete
self._get_struct_union_enum_type('enum', node)
[436] Fix | Delete
elif not decl.name:
[437] Fix | Delete
raise CDefError("construct does not declare any variable",
[438] Fix | Delete
decl)
[439] Fix | Delete
#
[440] Fix | Delete
if decl.name:
[441] Fix | Delete
tp, quals = self._get_type_and_quals(node,
[442] Fix | Delete
partial_length_ok=True)
[443] Fix | Delete
if tp.is_raw_function:
[444] Fix | Delete
self._declare_function(tp, quals, decl)
[445] Fix | Delete
elif (tp.is_integer_type() and
[446] Fix | Delete
hasattr(decl, 'init') and
[447] Fix | Delete
hasattr(decl.init, 'value') and
[448] Fix | Delete
_r_int_literal.match(decl.init.value)):
[449] Fix | Delete
self._add_integer_constant(decl.name, decl.init.value)
[450] Fix | Delete
elif (tp.is_integer_type() and
[451] Fix | Delete
isinstance(decl.init, pycparser.c_ast.UnaryOp) and
[452] Fix | Delete
decl.init.op == '-' and
[453] Fix | Delete
hasattr(decl.init.expr, 'value') and
[454] Fix | Delete
_r_int_literal.match(decl.init.expr.value)):
[455] Fix | Delete
self._add_integer_constant(decl.name,
[456] Fix | Delete
'-' + decl.init.expr.value)
[457] Fix | Delete
elif (tp is model.void_type and
[458] Fix | Delete
decl.name.startswith('__cffi_extern_python_')):
[459] Fix | Delete
# hack: `extern "Python"` in the C source is replaced
[460] Fix | Delete
# with "void __cffi_extern_python_start;" and
[461] Fix | Delete
# "void __cffi_extern_python_stop;"
[462] Fix | Delete
self._inside_extern_python = decl.name
[463] Fix | Delete
else:
[464] Fix | Delete
if self._inside_extern_python !='__cffi_extern_python_stop':
[465] Fix | Delete
raise CDefError(
[466] Fix | Delete
"cannot declare constants or "
[467] Fix | Delete
"variables with 'extern \"Python\"'")
[468] Fix | Delete
if (quals & model.Q_CONST) and not tp.is_array_type:
[469] Fix | Delete
self._declare('constant ' + decl.name, tp, quals=quals)
[470] Fix | Delete
else:
[471] Fix | Delete
self._declare('variable ' + decl.name, tp, quals=quals)
[472] Fix | Delete
[473] Fix | Delete
def parse_type(self, cdecl):
[474] Fix | Delete
return self.parse_type_and_quals(cdecl)[0]
[475] Fix | Delete
[476] Fix | Delete
def parse_type_and_quals(self, cdecl):
[477] Fix | Delete
ast, macros = self._parse('void __dummy(\n%s\n);' % cdecl)[:2]
[478] Fix | Delete
assert not macros
[479] Fix | Delete
exprnode = ast.ext[-1].type.args.params[0]
[480] Fix | Delete
if isinstance(exprnode, pycparser.c_ast.ID):
[481] Fix | Delete
raise CDefError("unknown identifier '%s'" % (exprnode.name,))
[482] Fix | Delete
return self._get_type_and_quals(exprnode.type)
[483] Fix | Delete
[484] Fix | Delete
def _declare(self, name, obj, included=False, quals=0):
[485] Fix | Delete
if name in self._declarations:
[486] Fix | Delete
prevobj, prevquals = self._declarations[name]
[487] Fix | Delete
if prevobj is obj and prevquals == quals:
[488] Fix | Delete
return
[489] Fix | Delete
if not self._options.get('override'):
[490] Fix | Delete
raise FFIError(
[491] Fix | Delete
"multiple declarations of %s (for interactive usage, "
[492] Fix | Delete
"try cdef(xx, override=True))" % (name,))
[493] Fix | Delete
assert '__dotdotdot__' not in name.split()
[494] Fix | Delete
self._declarations[name] = (obj, quals)
[495] Fix | Delete
if included:
[496] Fix | Delete
self._included_declarations.add(obj)
[497] Fix | Delete
[498] Fix | Delete
def _extract_quals(self, type):
[499] Fix | Delete
12
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function