import cffi
import re
import sys

class Migemo:
    def __init__(self, dict_path):
        self.dict_path = dict_path
        self._ffi = self.__setup_ffi()
        self._lib = self.__load_lib(self._ffi)
        self._migemo_struct = self.__load_migemo(self._lib, self.dict_path)

    def query(self, query):
        re_struct = self._lib.migemo_query(self._migemo_struct, query)
        re_str = self._ffi.string(re_struct)
        self._lib.migemo_release(self._migemo_struct, re_struct)
        return re_str

    def __setup_ffi(self):
        ffi = cffi.FFI()
        ffi.cdef('''
typedef struct _migemo migemo;
migemo* migemo_open(const char* dict);
void migemo_release(migemo* object, unsigned char* string);
void migemo_close(migemo* object);
unsigned char* migemo_query(migemo* object, const unsigned char* query);
''')
        return ffi

    def __load_lib(self, ffi):
        if re.search('darwin', sys.platform):
            suffix = 'dylib'
        else:
            suffix = 'so'
        return ffi.dlopen('libmigemo.' + suffix)

    def __load_migemo(self, lib, dict_path):
        return lib.migemo_open(dict_path)

    def __del__(self):
        if self._migemo_struct is not None:
            self._lib.migemo_close(self._migemo_struct)

@@ -5,16 +5,26 @@
 rt cffi%0A
+import os%0A
 import r
@@ -274,32 +274,134 @@
  self.dict_path)
+%0A        if not os.path.exists(dict_path):%0A            raise IOError(%22No such file: '%25s'%22 %25 dict_path)
 %0A%0A    def query(

Validate dictionary path