示例#1
0
文件: types_test.py 项目: google/clif
def _GenerateHeaderHelper(t_pp,
                          t_ns,
                          u_ns=None,
                          private='',
                          is_extended_from_python=False):
    m = pyext.Module(f'fq.py.{private}path')
    ns = 'clif::name::'
    w = ns + 'wrapper'
    m.types = [
        types.ClassType('c::name::cpp_name',
                        t_pp,
                        w,
                        w + '_Type',
                        ns,
                        can_copy=False,
                        can_move=False,
                        can_destruct=True,
                        virtual='',
                        ns=t_ns)
    ]
    if u_ns is not None:
        m.types.append(
            types.ClassType('other::cpp_name',
                            'py.path.u',
                            w,
                            w + '_Type',
                            ns,
                            can_copy=False,
                            can_move=False,
                            can_destruct=True,
                            virtual='',
                            ns=u_ns))
    return '\n'.join(
        m.GenerateHeader('fq/py/my.clif', 'fq/my.h', {},
                         is_extended_from_python)) + '\n'
示例#2
0
 def setUp(self):
   super().setUp()
   tm = ast_pb2.Typemap()
   text_format.Parse("""
       lang_type: "str"
       postconversion: "SomeFunc"
     """, tm)
   self.m = pyext.Module('my.test', typemap=[tm])
示例#3
0
 def assertNsEqual(self, proto, ns):
     pytd = textwrap.dedent(proto)
     ast = _ParseFile(pytd, type_headers=['clif/python/types.h'])
     m = pyext.Module('my.path.py.ext')
     for d in ast.decls:
         list(m.WrapDecl(d))
     if m.types:
         different_ns = set(types.Namespace(t) for t in m.types)
         self.assertEqual(set(ns), different_ns)
     else:
         self.assertFalse(ns)
示例#4
0
    def testClass1Header(self):
        m = pyext.Module('fq.py.path', for_py3=str is not bytes)
        ns = 'clif::name::'
        w = ns + 'wrapper'
        t = types.ClassType('c::name::cpp_name',
                            'py.path',
                            w,
                            w + '_Type',
                            ns,
                            can_copy=False,
                            can_move=False,
                            can_destruct=True,
                            virtual='',
                            ns='c::name')
        m.types = [t]
        header = '\n'.join(m.GenerateHeader('fq/py/my.clif', 'fq/my.h',
                                            {})) + '\n'
        self.assertMultiLineEqual(
            header,
            textwrap.dedent("""\
      //////////////////////////////////////////////////////////////////////
      // This file was automatically generated by CLIF to run under Python %d
      // Version 0.3
      //////////////////////////////////////////////////////////////////////
      // source: fq/py/my.clif

      #include <memory>
      #include "absl/types/optional.h"
      #include "fq/my.h"
      #include "clif/python/postconv.h"

      namespace c { namespace name {
      using namespace ::clif;

      // CLIF use `c::name::cpp_name` as py.path
      bool Clif_PyObjAs(PyObject* input, c::name::cpp_name** output);
      bool Clif_PyObjAs(PyObject* input, std::shared_ptr<c::name::cpp_name>* output);
      bool Clif_PyObjAs(PyObject* input, std::unique_ptr<c::name::cpp_name>* output);
      PyObject* Clif_PyObjFrom(c::name::cpp_name*, py::PostConv);
      PyObject* Clif_PyObjFrom(std::shared_ptr<c::name::cpp_name>, py::PostConv);
      PyObject* Clif_PyObjFrom(std::unique_ptr<c::name::cpp_name>, py::PostConv);
      template<typename T>
      typename std::enable_if<std::is_same<T, c::name::cpp_name>::value>::type Clif_PyObjFrom(const c::name::cpp_name*, py::PostConv) = delete;
      template<typename T>
      typename std::enable_if<std::is_same<T, c::name::cpp_name>::value>::type Clif_PyObjFrom(const c::name::cpp_name&, py::PostConv) = delete;

      } }  // namespace c::name

      // CLIF init_module if (PyObject* m = PyImport_ImportModule("fq.py.path")) Py_DECREF(m);
      // CLIF init_module else goto err;
    """ % (3 if m.py3output else 2)))
示例#5
0
 def assertNsEqual(self, proto, ns):
   pytd = textwrap.dedent(proto)
   with open(TMP_FILE, 'w') as pytd_file:
     pytd_file.write(pytd)
   p = pytd2proto.Postprocessor(
       config_headers=['clif/python/types.h'],
       include_paths=[os.environ['CLIF_DIR']])
   with open(TMP_FILE, 'r') as pytd_file:
     ast = p.Translate(pytd_file)
   m = pyext.Module('my.path.py.ext')
   for d in ast.decls:
     list(m.WrapDecl(d))
   if m.types:
     different_ns = set(types.Namespace(t) for t in m.types)
     self.assertEqual(set(ns), different_ns)
   else:
     self.assertFalse(ns)
示例#6
0
文件: pyclif.py 项目: yijunyu/clif
def GenerateFrom(ast):
    """Traverse ast and generate output files."""
    inc_headers = list(ast.usertype_includes)
    api_header = _GetHeaders(ast)
    modname = FLAGS.modname or StripExt(os.path.basename(ast.source)).replace(
        '-', '_')
    m = pyext.Module(modname,
                     ast.typemaps,
                     for_py3=FLAGS.py3output,
                     indent=FLAGS.indent)
    inc_headers.append(os.path.basename(FLAGS.header_out))
    # Order of generators is important.
    if api_header:
        with open(FLAGS.ccdeps_out, 'w') as cout:
            gen.WriteTo(cout, m.GenerateBase(ast, api_header, inc_headers))
        with open(FLAGS.ccinit_out, 'w') as iout:
            gen.WriteTo(iout, m.GenerateInit(ast.source))
    with open(FLAGS.header_out, 'w') as hout:
        gen.WriteTo(hout, m.GenerateHeader(ast.source, api_header, ast.macros))
示例#7
0
 def setUp(self):
     self.m = pyext.Module('my.test', for_py3=str is not bytes)
示例#8
0
    def testClassHeader(self):
        m = pyext.Module('fq.py.path', for_py3=str is not bytes)
        ns = 'clif::name::'
        w = ns + 'wrapper'
        t = types.ClassType('c::name::cpp_name',
                            'py.path.t',
                            w,
                            w + '_Type',
                            ns,
                            can_copy=False,
                            can_destruct=True,
                            down_cast=None,
                            virtual='',
                            ns='c')
        u = types.ClassType('other::cpp_name',
                            'py.path.u',
                            w,
                            w + '_Type',
                            ns,
                            can_copy=False,
                            can_destruct=True,
                            down_cast=None,
                            virtual='',
                            ns='other')
        m.types = [t, u]
        header = '\n'.join(m.GenerateHeader('fq/py/my.clif', 'fq/my.h',
                                            {})) + '\n'
        self.assertMultiLineEqual(
            header,
            textwrap.dedent("""\
      //////////////////////////////////////////////////////////////////////
      // This file was automatically generated by CLIF to run under Python %d
      // Version 0.2
      //////////////////////////////////////////////////////////////////////
      // source: fq/py/my.clif

      #include <memory>
      #include "clif/python/optional.h"
      #include "fq/my.h"
      #include "clif/python/postconv.h"

      namespace c {
      using namespace ::clif;

      // CLIF use `c::name::cpp_name` as py.path.t
      bool Clif_PyObjAs(PyObject* input, c::name::cpp_name** output);
      bool Clif_PyObjAs(PyObject* input, std::shared_ptr<c::name::cpp_name>* output);
      bool Clif_PyObjAs(PyObject* input, std::unique_ptr<c::name::cpp_name>* output);
      PyObject* Clif_PyObjFrom(c::name::cpp_name*, py::PostConv);
      PyObject* Clif_PyObjFrom(std::unique_ptr<c::name::cpp_name>, py::PostConv);
      PyObject* Clif_PyObjFrom(std::shared_ptr<c::name::cpp_name>, py::PostConv);
      PyObject* Clif_PyObjFrom(const c::name::cpp_name*, py::PostConv) = delete;
      PyObject* Clif_PyObjFrom(const c::name::cpp_name&, py::PostConv) = delete;

      }  // namespace c

      namespace other {
      using namespace ::clif;

      // CLIF use `other::cpp_name` as py.path.u
      bool Clif_PyObjAs(PyObject* input, other::cpp_name** output);
      bool Clif_PyObjAs(PyObject* input, std::shared_ptr<other::cpp_name>* output);
      bool Clif_PyObjAs(PyObject* input, std::unique_ptr<other::cpp_name>* output);
      PyObject* Clif_PyObjFrom(other::cpp_name*, py::PostConv);
      PyObject* Clif_PyObjFrom(std::unique_ptr<other::cpp_name>, py::PostConv);
      PyObject* Clif_PyObjFrom(std::shared_ptr<other::cpp_name>, py::PostConv);
      PyObject* Clif_PyObjFrom(const other::cpp_name*, py::PostConv) = delete;
      PyObject* Clif_PyObjFrom(const other::cpp_name&, py::PostConv) = delete;

      }  // namespace other

      // CLIF init_module if (PyObject* m = PyImport_ImportModule("fq.py.path")) Py_DECREF(m);
      // CLIF init_module else goto err;
    """ % (3 if m.py3output else 2)))
示例#9
0
 def setUp(self):
     super().setUp()
     self.m = pyext.Module('my.test')
示例#10
0
 def setUp(self):
   super(ModuleTest, self).setUp()
   self.m = pyext.Module('my.test', for_py3=str is not bytes)