Example #1
0
def _write_gw_cpp(f, interface):
    if interface.name[0] == "I":
        gname = 'PyG' + interface.name[1:]
    else:
        gname = 'PyG' + interface.name
    name = interface.name
    if interface.base == "IUnknown" or interface.base == "IDispatch":
        base_name = "PyGatewayBase"
    else:
        if interface.base[0] == "I":
            base_name = 'PyG' + interface.base[1:]
        else:
            base_name = 'PyG' + interface.base
    f.write('''\
// ---------------------------------------------------
//
// Gateway Implementation
''' % {
        'name': name,
        'gname': gname,
        'base_name': base_name
    })

    for method in interface.methods:
        f.write(\
    '''\
STDMETHODIMP %s::%s(
''' % (gname, method.name))

        if method.args:
            for arg in method.args[:-1]:
                inoutstr = ']['.join(arg.inout)
                f.write("\t\t/* [%s] */ %s,\n" %
                        (inoutstr, arg.GetRawDeclaration()))

            arg = method.args[-1]
            inoutstr = ']['.join(arg.inout)
            f.write("\t\t/* [%s] */ %s)\n" %
                    (inoutstr, arg.GetRawDeclaration()))
        else:
            f.write('\t\tvoid)\n')

        f.write("{\n\tPY_GATEWAY_METHOD;\n")
        cout = 0
        codePre = codePost = codeVars = ""
        argStr = ""
        needConversion = 0
        formatChars = ""
        if method.args:
            for arg in method.args:
                if arg.HasAttribute("out"):
                    cout = cout + 1
                    if arg.indirectionLevel == 2:
                        f.write("\tif (%s==NULL) return E_POINTER;\n" %
                                arg.name)
                if arg.HasAttribute("in"):
                    try:
                        argCvt = makegwparse.make_arg_converter(arg)
                        argCvt.SetGatewayMode()
                        formatchar = argCvt.GetFormatChar()
                        needConversion = needConversion or argCvt.NeedUSES_CONVERSION(
                        )

                        if formatchar:
                            formatChars = formatChars + formatchar
                            codeVars = codeVars + argCvt.DeclareParseArgTupleInputConverter(
                            )
                            argStr = argStr + ", " + argCvt.GetBuildValueArg()
                        codePre = codePre + argCvt.GetBuildForGatewayPreCode()
                        codePost = codePost + argCvt.GetBuildForGatewayPostCode(
                        )
                    except makegwparse.error_not_supported, why:
                        f.write(
                            '// *** The input argument %s of type "%s" was not processed ***\n//   - Please ensure this conversion function exists, and is appropriate\n//   - %s\n'
                            % (arg.name, arg.raw_type, why))
                        f.write('\tPyObject *ob%s = PyObject_From%s(%s);\n' %
                                (arg.name, arg.type, arg.name))
                        f.write(
                            '\tif (ob%s==NULL) return MAKE_PYCOM_GATEWAY_FAILURE_CODE("%s");\n'
                            % (arg.name, method.name))
                        codePost = codePost + "\tPy_DECREF(ob%s);\n" % arg.name
                        formatChars = formatChars + "O"
                        argStr = argStr + ", ob%s" % (arg.name)

        if needConversion: f.write('\tUSES_CONVERSION;\n')
        f.write(codeVars)
        f.write(codePre)
        if cout:
            f.write("\tPyObject *result;\n")
            resStr = "&result"
        else:
            resStr = "NULL"

        if formatChars:
            fullArgStr = '%s, "%s"%s' % (resStr, formatChars, argStr)
        else:
            fullArgStr = resStr

        f.write('\tHRESULT hr=InvokeViaPolicy("%s", %s);\n' %
                (method.name, fullArgStr))
        f.write(codePost)
        if cout:
            f.write("\tif (FAILED(hr)) return hr;\n")
            f.write(
                "\t// Process the Python results, and convert back to the real params\n"
            )
            # process the output arguments.
            formatChars = codePobjects = codePost = argsParseTuple = ""
            needConversion = 0
            for arg in method.args:
                if not arg.HasAttribute("out"):
                    continue
                try:
                    argCvt = makegwparse.make_arg_converter(arg)
                    argCvt.SetGatewayMode()
                    val = argCvt.GetFormatChar()
                    if val:
                        formatChars = formatChars + val
                        argsParseTuple = argsParseTuple + ", " + argCvt.GetParseTupleArg(
                        )
                        codePobjects = codePobjects + argCvt.DeclareParseArgTupleInputConverter(
                        )
                        codePost = codePost + argCvt.GetParsePostCode()
                        needConversion = needConversion or argCvt.NeedUSES_CONVERSION(
                        )
                except makegwparse.error_not_supported, why:
                    f.write(
                        '// *** The output argument %s of type "%s" was not processed ***\n//     %s\n'
                        % (arg.name, arg.raw_type, why))

            if formatChars:  # If I have any to actually process.
                if len(formatChars) == 1:
                    parseFn = "PyArg_Parse"
                else:
                    parseFn = "PyArg_ParseTuple"
                if codePobjects: f.write(codePobjects)
                f.write(
                    '\tif (!%s(result, "%s" %s))\n\t\treturn MAKE_PYCOM_GATEWAY_FAILURE_CODE("%s");\n'
                    % (parseFn, formatChars, argsParseTuple, method.name))
            if codePost:
                f.write('\tBOOL bPythonIsHappy = TRUE;\n')
                f.write(codePost)
                f.write(
                    '\tif (!bPythonIsHappy) hr = MAKE_PYCOM_GATEWAY_FAILURE_CODE("%s");\n'
                    % method.name)
            f.write('\tPy_DECREF(result);\n')
Example #2
0
File: makegw.py Project: hkfr/data
def _write_gw_cpp(f, interface):
  if interface.name[0] == "I":
    gname = 'PyG' + interface.name[1:]
  else:
    gname = 'PyG' + interface.name
  name = interface.name
  if interface.base == "IUnknown" or interface.base == "IDispatch":
    base_name = "PyGatewayBase"
  else:
    if interface.base[0] == "I":
      base_name = 'PyG' + interface.base[1:]
    else:
      base_name = 'PyG' + interface.base
  f.write('''\
// ---------------------------------------------------
//
// Gateway Implementation
''' % {'name':name, 'gname':gname, 'base_name':base_name})

  for method in interface.methods:
    f.write(\
'''\
STDMETHODIMP %s::%s(
''' % (gname, method.name))

    if method.args:
      for arg in method.args[:-1]:
        inoutstr = string.join(arg.inout, '][')
        f.write("\t\t/* [%s] */ %s,\n" % (inoutstr, arg.GetRawDeclaration()))
        
      arg = method.args[-1]
      inoutstr = string.join(arg.inout, '][')
      f.write("\t\t/* [%s] */ %s)\n" % (inoutstr, arg.GetRawDeclaration()))
    else:
      f.write('\t\tvoid)\n')

    f.write("{\n\tPY_GATEWAY_METHOD;\n")
    cout = 0
    codePre = codePost = codeVars = ""
    argStr = ""
    needConversion = 0
    formatChars = ""
    if method.args:
      for arg in method.args:
        if arg.HasAttribute("out"):
          cout = cout + 1
          if arg.indirectionLevel ==2 :
            f.write("\tif (%s==NULL) return E_POINTER;\n" % arg.name)
        if arg.HasAttribute("in"):
          try:
            argCvt = makegwparse.make_arg_converter(arg)
            argCvt.SetGatewayMode()
            formatchar = argCvt.GetFormatChar();
            needConversion = needConversion or argCvt.NeedUSES_CONVERSION()

            if formatchar:
              formatChars = formatChars + formatchar
              codeVars = codeVars + argCvt.DeclareParseArgTupleInputConverter()
              argStr = argStr + ", " + argCvt.GetBuildValueArg()
            codePre = codePre + argCvt.GetBuildForGatewayPreCode()
            codePost = codePost + argCvt.GetBuildForGatewayPostCode()
          except makegwparse.error_not_supported, why:
            f.write('// *** The input argument %s of type "%s" was not processed ***\n//   - Please ensure this conversion function exists, and is appropriate\n//   - %s\n' % (arg.name, arg.raw_type, why))
            f.write('\tPyObject *ob%s = PyObject_From%s(%s);\n' % (arg.name, arg.type, arg.name))
            f.write('\tif (ob%s==NULL) return MAKE_PYCOM_GATEWAY_FAILURE_CODE("%s");\n' % (arg.name, method.name))
            codePost = codePost + "\tPy_DECREF(ob%s);\n" % arg.name
            formatChars = formatChars + "O"
            argStr = argStr + ", ob%s" % (arg.name)
    
    if needConversion: f.write('\tUSES_CONVERSION;\n')
    f.write(codeVars)
    f.write(codePre)
    if cout:
      f.write("\tPyObject *result;\n")
      resStr = "&result"
    else:
      resStr = "NULL"
      
    if formatChars:
      fullArgStr = '%s, "%s"%s' % (resStr, formatChars, argStr)
    else:
      fullArgStr = resStr

    f.write('\tHRESULT hr=InvokeViaPolicy("%s", %s);\n' % (method.name, fullArgStr))
    f.write(codePost)
    if cout:
      f.write("\tif (FAILED(hr)) return hr;\n")
      f.write("\t// Process the Python results, and convert back to the real params\n")
      # process the output arguments.
      formatChars = codePobjects = codePost = argsParseTuple = ""
      needConversion = 0
      for arg in method.args:
        if not arg.HasAttribute("out"):
          continue
        try:
          argCvt = makegwparse.make_arg_converter(arg)
          argCvt.SetGatewayMode()
          val = argCvt.GetFormatChar()
          if val:
            formatChars = formatChars + val
            argsParseTuple = argsParseTuple + ", " + argCvt.GetParseTupleArg()
            codePobjects = codePobjects + argCvt.DeclareParseArgTupleInputConverter()
            codePost = codePost + argCvt.GetParsePostCode()
            needConversion = needConversion or argCvt.NeedUSES_CONVERSION()
        except makegwparse.error_not_supported, why:
          f.write('// *** The output argument %s of type "%s" was not processed ***\n//     %s\n' % (arg.name, arg.raw_type, why))

      if formatChars: # If I have any to actually process.
        if len(formatChars)==1:
          parseFn = "PyArg_Parse"
        else:
          parseFn = "PyArg_ParseTuple"
        if codePobjects: f.write(codePobjects)
        f.write('\tif (!%s(result, "%s" %s))\n\t\treturn MAKE_PYCOM_GATEWAY_FAILURE_CODE("%s");\n' % (parseFn, formatChars, argsParseTuple, method.name))
      if codePost: 
        f.write('\tBOOL bPythonIsHappy = TRUE;\n')
        f.write(codePost)
        f.write('\tif (!bPythonIsHappy) hr = MAKE_PYCOM_GATEWAY_FAILURE_CODE("%s");\n' % method.name)
      f.write('\tPy_DECREF(result);\n');
Example #3
0
def _write_ifc_cpp(f, interface):
    name = interface.name
    f.write(\
  '''\
// ---------------------------------------------------
//
// Interface Implementation

Py%(name)s::Py%(name)s(IUnknown *pdisp):
	Py%(base)s(pdisp)
{
	ob_type = &type;
}

Py%(name)s::~Py%(name)s()
{
}

/* static */ %(name)s *Py%(name)s::GetI(PyObject *self)
{
	return (%(name)s *)Py%(base)s::GetI(self);
}

''' % (interface.__dict__))

    ptr = re.sub('[a-z]', '', interface.name)
    strdict = {'interfacename': interface.name, 'ptr': ptr}
    for method in interface.methods:
        strdict['method'] = method.name
        f.write(\
    '''\
// @pymethod |Py%(interfacename)s|%(method)s|Description of %(method)s.
PyObject *Py%(interfacename)s::%(method)s(PyObject *self, PyObject *args)
{
	%(interfacename)s *p%(ptr)s = GetI(self);
	if ( p%(ptr)s == NULL )
		return NULL;
''' % strdict)
        argsParseTuple = argsCOM = formatChars = codePost = \
                         codePobjects = codeCobjects = cleanup = cleanup_gil = ""
        needConversion = 0
        #    if method.name=="Stat": import win32dbg;win32dbg.brk()
        for arg in method.args:
            try:
                argCvt = makegwparse.make_arg_converter(arg)
                if arg.HasAttribute("in"):
                    val = argCvt.GetFormatChar()
                    if val:
                        f.write('\t' + argCvt.GetAutoduckString() + "\n")
                        formatChars = formatChars + val
                        argsParseTuple = argsParseTuple + ", " + argCvt.GetParseTupleArg(
                        )
                        codePobjects = codePobjects + argCvt.DeclareParseArgTupleInputConverter(
                        )
                        codePost = codePost + argCvt.GetParsePostCode()
                        needConversion = needConversion or argCvt.NeedUSES_CONVERSION(
                        )
                        cleanup = cleanup + argCvt.GetInterfaceArgCleanup()
                        cleanup_gil = cleanup_gil + argCvt.GetInterfaceArgCleanupGIL(
                        )
                comArgName, comArgDeclString = argCvt.GetInterfaceCppObjectInfo(
                )
                if comArgDeclString:  # If we should declare a variable
                    codeCobjects = codeCobjects + "\t%s;\n" % (
                        comArgDeclString)
                argsCOM = argsCOM + ", " + comArgName
            except makegwparse.error_not_supported, why:
                f.write(
                    '// *** The input argument %s of type "%s" was not processed ***\n//     Please check the conversion function is appropriate and exists!\n'
                    % (arg.name, arg.raw_type))

                f.write('\t%s %s;\n\tPyObject *ob%s;\n' %
                        (arg.type, arg.name, arg.name))
                f.write('\t// @pyparm <o Py%s>|%s||Description for %s\n' %
                        (arg.type, arg.name, arg.name))
                codePost = codePost + '\tif (bPythonIsHappy && !PyObject_As%s( ob%s, &%s )) bPythonIsHappy = FALSE;\n' % (
                    arg.type, arg.name, arg.name)

                formatChars = formatChars + "O"
                argsParseTuple = argsParseTuple + ", &ob%s" % (arg.name)

                argsCOM = argsCOM + ", " + arg.name
                cleanup = cleanup + "\tPyObject_Free%s(%s);\n" % (arg.type,
                                                                  arg.name)

        if needConversion: f.write("\tUSES_CONVERSION;\n")
        f.write(codePobjects)
        f.write(codeCobjects)
        f.write(
            '\tif ( !PyArg_ParseTuple(args, "%s:%s"%s) )\n\t\treturn NULL;\n' %
            (formatChars, method.name, argsParseTuple))
        if codePost:
            f.write('\tBOOL bPythonIsHappy = TRUE;\n')
            f.write(codePost)
            f.write('\tif (!bPythonIsHappy) return NULL;\n')
        strdict['argsCOM'] = argsCOM[1:]
        strdict['cleanup'] = cleanup
        strdict['cleanup_gil'] = cleanup_gil
        f.write(\
    '''	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = p%(ptr)s->%(method)s(%(argsCOM)s );
%(cleanup)s
	PY_INTERFACE_POSTCALL;
%(cleanup_gil)s
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, p%(ptr)s, IID_%(interfacename)s );
''' % strdict)
        codePre = codePost = formatChars = codeVarsPass = codeDecl = ""
        for arg in method.args:
            if not arg.HasAttribute("out"):
                continue
            try:
                argCvt = makegwparse.make_arg_converter(arg)
                formatChar = argCvt.GetFormatChar()
                if formatChar:
                    formatChars = formatChars + formatChar
                    codePre = codePre + argCvt.GetBuildForInterfacePreCode()
                    codePost = codePost + argCvt.GetBuildForInterfacePostCode()
                    codeVarsPass = codeVarsPass + ", " + argCvt.GetBuildValueArg(
                    )
                    codeDecl = codeDecl + argCvt.DeclareParseArgTupleInputConverter(
                    )
            except makegwparse.error_not_supported, why:
                f.write(
                    '// *** The output argument %s of type "%s" was not processed ***\n//     %s\n'
                    % (arg.name, arg.raw_type, why))
                continue
Example #4
0
File: makegw.py Project: hkfr/data
def _write_ifc_cpp(f, interface):
  name = interface.name
  f.write(\
'''\
// ---------------------------------------------------
//
// Interface Implementation

Py%(name)s::Py%(name)s(IUnknown *pdisp):
	Py%(base)s(pdisp)
{
	ob_type = &type;
}

Py%(name)s::~Py%(name)s()
{
}

/* static */ %(name)s *Py%(name)s::GetI(PyObject *self)
{
	return (%(name)s *)Py%(base)s::GetI(self);
}

''' % (interface.__dict__))

  ptr = regsub.gsub('[a-z]', '', interface.name)
  strdict = {'interfacename':interface.name, 'ptr': ptr}
  for method in interface.methods:
    strdict['method'] = method.name
    f.write(\
'''\
// @pymethod |Py%(interfacename)s|%(method)s|Description of %(method)s.
PyObject *Py%(interfacename)s::%(method)s(PyObject *self, PyObject *args)
{
	%(interfacename)s *p%(ptr)s = GetI(self);
	if ( p%(ptr)s == NULL )
		return NULL;
''' % strdict)
    argsParseTuple = argsCOM = formatChars = codePost = \
                     codePobjects = codeCobjects = cleanup = cleanup_gil = ""
    needConversion = 0
#    if method.name=="Stat": import win32dbg;win32dbg.brk()
    for arg in method.args:
      try:
        argCvt = makegwparse.make_arg_converter(arg)
        if arg.HasAttribute("in"):
          val = argCvt.GetFormatChar()
          if val:
            f.write ('\t' + argCvt.GetAutoduckString() + "\n")
            formatChars = formatChars + val
            argsParseTuple = argsParseTuple + ", " + argCvt.GetParseTupleArg()
            codePobjects = codePobjects + argCvt.DeclareParseArgTupleInputConverter()
            codePost = codePost + argCvt.GetParsePostCode()
            needConversion = needConversion or argCvt.NeedUSES_CONVERSION()
            cleanup = cleanup + argCvt.GetInterfaceArgCleanup()
            cleanup_gil = cleanup_gil + argCvt.GetInterfaceArgCleanupGIL()
        comArgName, comArgDeclString = argCvt.GetInterfaceCppObjectInfo()
        if comArgDeclString: # If we should declare a variable
          codeCobjects = codeCobjects + "\t%s;\n" % (comArgDeclString)
        argsCOM = argsCOM + ", " + comArgName
      except makegwparse.error_not_supported, why:
        f.write('// *** The input argument %s of type "%s" was not processed ***\n//     Please check the conversion function is appropriate and exists!\n' % (arg.name, arg.raw_type))

        f.write('\t%s %s;\n\tPyObject *ob%s;\n' % (arg.type, arg.name, arg.name))
        f.write('\t// @pyparm <o Py%s>|%s||Description for %s\n' % (arg.type, arg.name, arg.name))
        codePost = codePost + '\tif (bPythonIsHappy && !PyObject_As%s( ob%s, &%s )) bPythonIsHappy = FALSE;\n' % (arg.type, arg.name, arg.name)

        formatChars = formatChars + "O"
        argsParseTuple = argsParseTuple + ", &ob%s" % (arg.name)

        argsCOM = argsCOM + ", " + arg.name
        cleanup = cleanup + "\tPyObject_Free%s(%s);\n" % (arg.type, arg.name)

    if needConversion: f.write("\tUSES_CONVERSION;\n")
    f.write(codePobjects);
    f.write(codeCobjects);
    f.write('\tif ( !PyArg_ParseTuple(args, "%s:%s"%s) )\n\t\treturn NULL;\n' % (formatChars, method.name, argsParseTuple))
    if codePost:
      f.write('\tBOOL bPythonIsHappy = TRUE;\n')
      f.write(codePost);
      f.write('\tif (!bPythonIsHappy) return NULL;\n')
    strdict['argsCOM'] = argsCOM[1:]
    strdict['cleanup'] = cleanup
    strdict['cleanup_gil'] = cleanup_gil
    f.write(\
'''	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = p%(ptr)s->%(method)s(%(argsCOM)s );
%(cleanup)s
	PY_INTERFACE_POSTCALL;
%(cleanup_gil)s
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, p%(ptr)s, IID_%(interfacename)s );
''' % strdict)
    codePre = codePost = formatChars = codeVarsPass = codeDecl = ""
    for arg in method.args:
      if not arg.HasAttribute("out"):
        continue
      try:
        argCvt =  makegwparse.make_arg_converter(arg)
        formatChar = argCvt.GetFormatChar()
        if formatChar:
          formatChars = formatChars + formatChar
          codePre = codePre + argCvt.GetBuildForInterfacePreCode()
          codePost = codePost + argCvt.GetBuildForInterfacePostCode()
          codeVarsPass = codeVarsPass + ", " + argCvt.GetBuildValueArg()
          codeDecl = codeDecl + argCvt.DeclareParseArgTupleInputConverter()
      except makegwparse.error_not_supported, why:
        f.write('// *** The output argument %s of type "%s" was not processed ***\n//     %s\n' % (arg.name, arg.raw_type, why))
        continue