예제 #1
0
 def WrapEnum(self, e, unused_ln, cpp_namespace):
     """Process AST.EnumDecl e."""
     # Enum(pyname, ((name, value),...))
     pytype = 'Enum' if e.enum_class else 'IntEnum'
     pystr = 'PyUnicode_FromString' if self.py3output else 'PyString_FromString'
     items = [
         (
             '%s("%s")' % (pystr, m.native or Ident(m.cpp_name)),
             'PyInt_FromLong(\n%s%s)' % (
                 4 * I,
                 types.AsType(
                     types.EnumIntType(e.name.cpp_name),
                     m.cpp_name
                     if ':' in m.cpp_name  # FQN populated by matcher.
                     else e.name.cpp_name + '::' + m.cpp_name)),
         ) for m in e.members
     ]
     assert items, 'matcher should populate enum members'
     wclass = '_' + e.name.native
     genw = 'wrap' + Ident(e.name.cpp_name)
     pyname = '.'.join([f.pyname for f in self.nested] + [e.name.native])
     t = types.EnumType(e.name.cpp_name, pyname, pytype,
                        self.CppName(wclass), cpp_namespace)
     self.types.add(t)
     self.dict.append(
         (e.name.native,
          '(%s=%s())' % (self.CppName(wclass), self.CppName(genw))))
     if not self.enums:
         self.enums = True
         self.init.extend([
             '{PyObject* em = PyImport_ImportModule("enum");',
             ' if (em == nullptr) goto err;',
             ' _Enum = PyObject_GetAttrString(em, "Enum");',
             ' _IntEnum = PyObject_GetAttrString(em, "IntEnum");',
             ' Py_DECREF(em);}', 'if (!_Enum || !_IntEnum) {',
             I + 'Py_XDECREF(_Enum);', I + 'Py_XDECREF(_IntEnum);',
             I + 'goto err;', '}'
         ])
     yield ''
     for s in (t.CreateEnum(self.wrap_namespace, genw, wclass, items,
                            self.py3output)):
         yield s
예제 #2
0
파일: pyext.py 프로젝트: sr-gi/clif
 def WrapConst(self, c, unused_ln, unused_ns, unused_class_ns=''):
     """Process AST.ConstDecl c."""
     self.dict.append((c.name.native, 'Clif_PyObjFrom(%s, %s)' %
                       (types.AsType(c.type.cpp_type, c.name.cpp_name),
                        postconv.Initializer(c.type, self.typemap))))
     return []