Back to index

python3.2  3.2.2
Functions | Variables
abitype Namespace Reference

Functions

def classify
 Replacement of PyTypeObject static instances ##############.
def get_fields
def make_slots

Variables

tuple tokenizer
 Simplistic C scanner ##################################.
list tokens = []
tuple source = sys.stdin.read()
int pos = 0
tuple m = tokenizer.match(source, pos)
tuple nl = source.find('\n', pos)
list line = source[pos:]
list typeslots
tuple c = classify()
tuple start = m.start()
tuple end = m.end()

Function Documentation

Replacement of PyTypeObject static instances ##############.

Definition at line 42 of file abitype.py.

00042 
00043 def classify():
00044     res = []
00045     for t,v in tokens:
00046         if t == 'other' and v in "={};":
00047             res.append(v)
00048         elif t == 'ident':
00049             if v == 'PyTypeObject':
00050                 res.append('T')
00051             elif v == 'static':
00052                 res.append('S')
00053             else:
00054                 res.append('I')
00055         elif t == 'ws':
00056             res.append('W')
00057         else:
00058             res.append('.')
00059     return ''.join(res)
00060 
00061 # Obtain a list of fields of a PyTypeObject, in declaration order,
00062 # skipping ob_base
00063 # All comments are dropped from the variable (which are typically
00064 # just the slot names, anyway), and information is discarded whether
# the original type was static.
def abitype.get_fields (   start,
  real_end 
)

Definition at line 65 of file abitype.py.

00065 
00066 def get_fields(start, real_end):
00067     pos = start
00068     # static?
00069     if tokens[pos][1] == 'static':
00070         pos += 2
00071     # PyTypeObject
00072     pos += 2
00073     # name
00074     name = tokens[pos][1]
00075     pos += 1
00076     while tokens[pos][1] != '{':
00077         pos += 1
00078     pos += 1
00079     # PyVarObject_HEAD_INIT
00080     while tokens[pos][0] in ('ws', 'comment'):
00081         pos += 1
00082     if tokens[pos][1] != 'PyVarObject_HEAD_INIT':
00083         raise Exception, '%s has no PyVarObject_HEAD_INIT' % name
00084     while tokens[pos][1] != ')':
00085         pos += 1
00086     pos += 1
00087     # field definitions: various tokens, comma-separated
00088     fields = []
00089     while True:
00090         while tokens[pos][0] in ('ws', 'comment'):
00091             pos += 1
00092         end = pos
00093         while tokens[end][1] not in ',}':
00094             if tokens[end][1] == '(':
00095                 nesting = 1
00096                 while nesting:
00097                     end += 1
00098                     if tokens[end][1] == '(': nesting+=1
00099                     if tokens[end][1] == ')': nesting-=1
00100             end += 1
00101         assert end < real_end
00102         # join field, excluding separator and trailing ws
00103         end1 = end-1
00104         while tokens[end1][0] in ('ws', 'comment'):
00105             end1 -= 1
00106         fields.append(''.join(t[1] for t in tokens[pos:end1+1]))
00107         if tokens[end][1] == '}':
00108             break
00109         pos = end+1
00110     return name, fields
00111 
# List of type slots as of Python 3.2, omitting ob_base
def abitype.make_slots (   name,
  fields 
)

Definition at line 162 of file abitype.py.

00162 
00163 def make_slots(name, fields):
00164     res = []
00165     res.append('static PyType_Slot %s_slots[] = {' % name)
00166     # defaults for spec
00167     spec = { 'tp_itemsize':'0' }
00168     for i, val in enumerate(fields):
00169         if val.endswith('0'):
00170             continue
00171         if typeslots[i] in ('tp_name', 'tp_doc', 'tp_basicsize',
00172                          'tp_itemsize', 'tp_flags'):
00173             spec[typeslots[i]] = val
00174             continue
00175         res.append('    {Py_%s, %s},' % (typeslots[i], val))
00176     res.append('};')
00177     res.append('static PyType_Spec %s_spec = {' % name)
00178     res.append('    %s,' % spec['tp_name'])
00179     res.append('    %s,' % spec['tp_basicsize'])
00180     res.append('    %s,' % spec['tp_itemsize'])
00181     res.append('    %s,' % spec['tp_flags'])
00182     res.append('    %s_slots,' % name)
00183     res.append('};\n')
00184     return '\n'.join(res)
00185 
00186 
00187 # Main loop: replace all static PyTypeObjects until
# there are none left.

Here is the call graph for this function:


Variable Documentation

tuple abitype.c = classify()

Definition at line 189 of file abitype.py.

tuple abitype.end = m.end()

Definition at line 194 of file abitype.py.

list abitype.line = source[pos:]

Definition at line 28 of file abitype.py.

tuple abitype.m = tokenizer.match(source, pos)

Definition at line 19 of file abitype.py.

tuple abitype.nl = source.find('\n', pos)

Definition at line 26 of file abitype.py.

Definition at line 17 of file abitype.py.

tuple abitype.source = sys.stdin.read()

Definition at line 16 of file abitype.py.

Definition at line 193 of file abitype.py.

Initial value:
00001 re.compile(
00002     r"(?P<preproc>#.*\n)"
00003     r"|(?P<comment>/\*.*?\*/)"
00004     r"|(?P<ident>[a-zA-Z_][a-zA-Z0-9_]*)"
00005     r"|(?P<ws>[ \t\n]+)"
00006     r"|(?P<other>.)",
00007     re.MULTILINE)

Simplistic C scanner ##################################.

Definition at line 7 of file abitype.py.

list abitype.tokens = []

Definition at line 15 of file abitype.py.

Definition at line 112 of file abitype.py.