I'm trying to use.embedding_api cffi.api.and getting CDefError: cannot parse

268 views
Skip to first unread message

joni...@gmail.com

unread,
Sep 26, 2016, 6:02:41 AM9/26/16
to python-cffi
Hi

I'm trying the new embedding api but got in trouble right away.

I'm trying to use this code based on the embedding example, I'm trying to make a Far manager plugin and the type declarations come from this plugin.hpp

I'm on Windows 7 Ultimate 64bit, Python 2.7.10 and Visual Studio 2015

PythonFar.py
# file plugin_build.py
import re
import cffi
ffibuilder
= cffi.FFI()

with open('PythonFar.h') as f:
    include
= f.read()

ffibuilder
.embedding_api(include)

ffibuilder
.set_source("PythonFar",
        re
.sub(r'^extern(?=\s)', 'CFFI_DLLEXPORT', include, flags=re.M))


ffibuilder
.embedding_init_code("""
    from PythonFar import ffi

    @ffi.def_extern()
    def adapter_Initialize(Info):
        print("
adapter_Initialize")
        return True
"""
)

ffibuilder
.compile(target="PythonFar.*", verbose=True)


PythonFar.h
typedef unsigned int     size_t;
typedef unsigned short wchar_t;

typedef struct _GUID {
       
unsigned long  Data1;
       
unsigned short Data2;
       
unsigned short Data3;
       
unsigned char  Data4[ 8 ];
} GUID;

enum VERSION_STAGE
{
    VS_RELEASE                      
= 0,
    VS_ALPHA                        
= 1,
    VS_BETA                        
= 2,
    VS_RC                          
= 3,
};

struct VersionInfo
{
  DWORD
Major;
  DWORD
Minor;
  DWORD
Revision;
  DWORD
Build;
 
enum VERSION_STAGE Stage;
};

struct GlobalInfo
{
    size_t
StructSize;
   
struct VersionInfo MinFarVersion;
   
struct VersionInfo Version;
    GUID
Guid;
   
const wchar_t *Title;
   
const wchar_t *Description;
   
const wchar_t *Author;
   
void* Instance;
};

extern BOOL WINAPI adapter_Initialize(GlobalInfo* Info);



D:\Documents\GitHub\Python-support-for-Far-manager\CFFI>py -2.7-32 "PythonFar.py"
Traceback (most recent call last):
 
File "PythonFar.py", line 11, in <module>
    ffibuilder
.embedding_api(include)
 
File "D:\Python27.x86\lib\site-packages\cffi\api.py", line 108, in embedding_api
   
self._cdef(csource, packed=packed, dllexport=True)
 
File "D:\Python27.x86\lib\site-packages\cffi\api.py", line 119, in _cdef
   
self._parser.parse(csource, override=override, **options)
 
File "D:\Python27.x86\lib\site-packages\cffi\cparser.py", line 299, in parse
   
self._internal_parse(csource)
 
File "D:\Python27.x86\lib\site-packages\cffi\cparser.py", line 304, in _internal_parse
    ast
, macros, csource = self._parse(csource)
 
File "D:\Python27.x86\lib\site-packages\cffi\cparser.py", line 262, in _parse
   
self.convert_pycparser_error(e, csource)
 
File "D:\Python27.x86\lib\site-packages\cffi\cparser.py", line 291, in convert_pycparser_error
   
raise api.CDefError(msg)
cffi
.api.CDefError: cannot parse "extern BOOL  volatile volatile const  adapter_Initialize(GlobalInfo* Info);"
:43:68: before: *

If I change the GlobalInfo to for example int it compiles without error and the embedding example works (with a small tweak) so I'm totally lost. Any help?

joni...@gmail.com

unread,
Sep 26, 2016, 6:06:09 AM9/26/16
to python-cffi
I forgot I'm using 32 bit Python as I need it to be a 32-bit .dll

Armin Rigo

unread,
Sep 26, 2016, 7:47:04 AM9/26/16
to pytho...@googlegroups.com
Hi,

On 26 September 2016 at 12:02, <joni...@gmail.com> wrote:
> struct GlobalInfo
> extern BOOL WINAPI adapter_Initialize(GlobalInfo* Info);

These two lines are not compatible. You need either to say 'struct
GlobalInfo' instead of 'GlobalInfo' whenever you use that type, or you
need to use a typedef. (For all I know it might be correct in C++,
but not in C.)


A bientôt,

Armin.

joni...@gmail.com

unread,
Sep 26, 2016, 9:15:54 AM9/26/16
to python-cffi, ar...@tunes.org
On Monday, September 26, 2016 at 1:47:04 PM UTC+2, Armin Rigo wrote:
These two lines are not compatible.  You need either to say 'struct
GlobalInfo' instead of 'GlobalInfo' whenever you use that type, or you
need to use a typedef.  (For all I know it might be correct in C++,
but not in C.)

Thanks, I assumed it was something simple, and it did cross my mind (then I forgot it)  the example code that syntax comes from is C++, but I found no other examples, so I used what I had.

Now I get "distutils.errors.DistutilsPlatformError: Unable to find vcvarsall.bat" but I should be able to fix it again.
Reply all
Reply to author
Forward
0 new messages