def main(defs_file, parser_file, verbose): ffi = FFI() with open(defs_file) as f: ffi.embedding_api(f.read()) ffi.set_source('parser_interface', '#include "%s"' % defs_file) with open(parser_file) as f: ffi.embedding_init_code(f.read()) interpreter, _, _ = os.path.basename(sys.executable).partition('.') ffi.compile(target='libplease_parser_%s.*' % interpreter, verbose=verbose)
def main(defs_file, parser_file, verbose): ffi = FFI() with open(defs_file) as f: ffi.embedding_api(f.read()) ldflags = None if platform.python_implementation() == 'CPython' and platform.system( ) == 'Darwin': ldflags = ['-L' + os.path.join(sys.prefix, 'lib')] ffi.set_source('parser_interface', '#include "%s"' % defs_file, extra_link_args=ldflags) with open(parser_file) as f: ffi.embedding_init_code(f.read()) interpreter, _, _ = os.path.basename(sys.executable).partition('.') ffi.compile(target='libplease_parser_%s.*' % interpreter, verbose=verbose)
def main(defs_file, parser_file, verbose): ffi = FFI() with open(defs_file) as f: ffi.embedding_api(f.read()) ldflags = None # Support downloading portable PyPy on Linux. if platform.python_implementation() == 'PyPy' and platform.system( ) == 'Linux': ldflags = ['-Wl,-rpath=./plz-out/gen/_remote/_pypy/bin'] ffi.set_source('parser_interface', '#include "%s"' % defs_file, extra_link_args=ldflags) with open(parser_file) as f: ffi.embedding_init_code(f.read()) interpreter, _, _ = os.path.basename(sys.executable).partition('.') ffi.compile(target='libplease_parser_%s.*' % interpreter, verbose=verbose)
DWORD AddressOfNameOrdinals; } IMAGE_EXPORT_DIRECTORY,*PIMAGE_EXPORT_DIRECTORY; DWORD pRVABase; IMAGE_EXPORT_DIRECTORY* pExportDirectory; void* PointerFromAddress(DWORD Address); DWORD AddressFromPointer(void* Pointer); ''') ffi.embedding_api(''' extern "Python" int __stdcall xlAutoOpen(void); extern "Python" int __stdcall xlAutoClose(void); extern "Python" int __stdcall xlAutoAdd(void); extern "Python" int __stdcall xlAutoRemove(void); extern "Python" void __stdcall xlAutoFree12(LPXLOPER12); extern "Python" LPXLOPER12 __stdcall _xlfCaller(); extern "Python" LPXLOPER12 __stdcall py_eval(const char* expression); ''') ffi.set_source("_python_xll", r""" #include <assert.h> #include <WINDOWS.H> #include <XLCALL.H> #pragma comment(linker, "/export:xlAutoOpen=_xlAutoOpen@0") #pragma comment(linker, "/export:xlAutoClose=_xlAutoClose@0") #pragma comment(linker, "/export:xlAutoAdd=_xlAutoAdd@0")
def generate_c(action): clingo_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) ffi = FFI() cnt = [] with open(f'{clingo_dir}/libclingo/clingo.h') as f: for line in f: if not re.match(r' *(#|//|extern *"C" *{|}$|$)', line): cnt.append( line.replace('CLINGO_VISIBILITY_DEFAULT ', '').strip()) # callbacks cnt.append( 'extern "Python" bool pyclingo_solve_event_callback(clingo_solve_event_type_t type, void *event, void *data, bool *goon);' ) cnt.append( 'extern "Python" void pyclingo_logger_callback(clingo_warning_t code, char const *message, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_ground_callback(clingo_location_t const *location, char const *name, clingo_symbol_t const *arguments, size_t arguments_size, void *data, clingo_symbol_callback_t symbol_callback, void *symbol_callback_data);' ) # propagator callbacks cnt.append( 'extern "Python" bool pyclingo_propagator_init(clingo_propagate_init_t *init, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_propagator_propagate(clingo_propagate_control_t *control, clingo_literal_t const *changes, size_t size, void *data);' ) cnt.append( 'extern "Python" void pyclingo_propagator_undo(clingo_propagate_control_t const *control, clingo_literal_t const *changes, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_propagator_check(clingo_propagate_control_t *control, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_propagator_decide(clingo_id_t thread_id, clingo_assignment_t const *assignment, clingo_literal_t fallback, void *data, clingo_literal_t *decision);' ) # observer callbacks cnt.append( 'extern "Python" bool pyclingo_observer_init_program(bool incremental, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_begin_step(void *data);') cnt.append('extern "Python" bool pyclingo_observer_end_step(void *data);') cnt.append( 'extern "Python" bool pyclingo_observer_rule(bool choice, clingo_atom_t const *head, size_t head_size, clingo_literal_t const *body, size_t body_size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_weight_rule(bool choice, clingo_atom_t const *head, size_t head_size, clingo_weight_t lower_bound, clingo_weighted_literal_t const *body, size_t body_size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_minimize(clingo_weight_t priority, clingo_weighted_literal_t const* literals, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_project(clingo_atom_t const *atoms, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_output_atom(clingo_symbol_t symbol, clingo_atom_t atom, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_output_term(clingo_symbol_t symbol, clingo_literal_t const *condition, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_output_csp(clingo_symbol_t symbol, int value, clingo_literal_t const *condition, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_external(clingo_atom_t atom, clingo_external_type_t type, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_assume(clingo_literal_t const *literals, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_heuristic(clingo_atom_t atom, clingo_heuristic_type_t type, int bias, unsigned priority, clingo_literal_t const *condition, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_acyc_edge(int node_u, int node_v, clingo_literal_t const *condition, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_theory_term_number(clingo_id_t term_id, int number, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_theory_term_string(clingo_id_t term_id, char const *name, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_theory_term_compound(clingo_id_t term_id, int name_id_or_type, clingo_id_t const *arguments, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_theory_element(clingo_id_t element_id, clingo_id_t const *terms, size_t terms_size, clingo_literal_t const *condition, size_t condition_size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_theory_atom(clingo_id_t atom_id_or_zero, clingo_id_t term_id, clingo_id_t const *elements, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_theory_atom_with_guard(clingo_id_t atom_id_or_zero, clingo_id_t term_id, clingo_id_t const *elements, size_t size, clingo_id_t operator_id, clingo_id_t right_hand_side_id, void *data);' ) # application callbacks cnt.append( 'extern "Python" char const *pyclingo_application_program_name(void *data);' ) cnt.append( 'extern "Python" char const *pyclingo_application_version(void *data);' ) cnt.append( 'extern "Python" unsigned pyclingo_application_message_limit(void *data);' ) cnt.append( 'extern "Python" bool pyclingo_application_main(clingo_control_t *control, char const *const * files, size_t size, void *data);' ) cnt.append( 'extern "Python" void pyclingo_application_logger(clingo_warning_t code, char const *message, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_application_print_model(clingo_model_t const *model, clingo_default_model_printer_t printer, void *printer_data, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_application_register_options(clingo_options_t *options, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_application_validate_options(void *data);' ) # application options callbacks cnt.append( 'extern "Python" bool pyclingo_application_options_parse(char const *value, void *data);' ) # ast callbacks cnt.append( 'extern "Python" bool pyclingo_ast_callback(clingo_ast_t const *, void *);' ) if action == "embed": ffi.embedding_api('''\ bool pyclingo_execute_(void *loc, char const *code, void *data); bool pyclingo_call_(void *loc, char const *name, void *arguments, size_t size, void *symbol_callback, void *symbol_callback_data, void *data); bool pyclingo_callable_(char const * name, bool *ret, void *data); bool pyclingo_main_(void *ctl, void *data); ''') ffi.embedding_init_code(f"""\ from collections.abc import Iterable from traceback import format_exception import __main__ from clingo._internal import _ffi, _handle_error, _lib from clingo.control import Control from clingo.symbol import Symbol def _cb_error_top_level(exception, exc_value, traceback): msg = "".join(format_exception(exception, exc_value, traceback)) _lib.clingo_set_error(_lib.clingo_error_runtime, msg.encode()) return False @_ffi.def_extern(onerror=_cb_error_top_level) def pyclingo_execute_(loc, code, data): exec(_ffi.string(code).decode(), __main__.__dict__, __main__.__dict__) return True @_ffi.def_extern(onerror=_cb_error_top_level) def pyclingo_call_(loc, name, arguments, size, symbol_callback, symbol_callback_data, data): symbol_callback = _ffi.cast('clingo_symbol_callback_t', symbol_callback) arguments = _ffi.cast('clingo_symbol_t*', arguments) context = _ffi.from_handle(data).data if data != _ffi.NULL else None py_name = _ffi.string(name).decode() fun = getattr(__main__ if context is None else context, py_name) args = [] for i in range(size): args.append(Symbol(arguments[i])) ret = fun(*args) symbols = list(ret) if isinstance(ret, Iterable) else [ret] c_symbols = _ffi.new('clingo_symbol_t[]', len(symbols)) for i, sym in enumerate(symbols): c_symbols[i] = sym._rep _handle_error(symbol_callback(c_symbols, len(symbols), symbol_callback_data)) return True @_ffi.def_extern(onerror=_cb_error_top_level) def pyclingo_callable_(name, ret, data): py_name = _ffi.string(name).decode() ret[0] = py_name in __main__.__dict__ and callable(__main__.__dict__[py_name]) return True @_ffi.def_extern(onerror=_cb_error_top_level) def pyclingo_main_(ctl, data): __main__.main(Control(_ffi.cast('clingo_control_t*', ctl))) return True """) if action != "header": ffi.set_source('_clingo', '#include <clingo.h>') ffi.cdef('\n'.join(cnt)) ffi.emit_c_code('_clingo.c') else: with open('_clingo.cdef', 'w') as f: f.write(''.join(f'{line}\n' for line in cnt))
from cffi import FFI ffi = FFI() ffi.embedding_api(""" int factorial(int n); """) ffi.embedding_init_code(""" from liv2 import ffi @ffi.def_extern() def factorial(n): if n == 0: return 1 else: return n * factorial(n-1) """) ffi.set_source("liv2", "") ffi.compile()
def generate_c(action): clingo_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) ffi = FFI() cnt = [] with open(f'{clingo_dir}/libclingo/clingo.h') as f: for line in f: if not re.match(r' *(#|//|extern *"C" *{|}$|$)', line): cnt.append( line.replace('CLINGO_VISIBILITY_DEFAULT ', '').strip()) # callbacks cnt.append( 'extern "Python" bool pyclingo_solve_event_callback(clingo_solve_event_type_t type, void *event, void *data, bool *goon);' ) cnt.append( 'extern "Python" void pyclingo_logger_callback(clingo_warning_t code, char const *message, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_ground_callback(clingo_location_t const *location, char const *name, clingo_symbol_t const *arguments, size_t arguments_size, void *data, clingo_symbol_callback_t symbol_callback, void *symbol_callback_data);' ) # propagator callbacks cnt.append( 'extern "Python" bool pyclingo_propagator_init(clingo_propagate_init_t *init, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_propagator_propagate(clingo_propagate_control_t *control, clingo_literal_t const *changes, size_t size, void *data);' ) cnt.append( 'extern "Python" void pyclingo_propagator_undo(clingo_propagate_control_t const *control, clingo_literal_t const *changes, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_propagator_check(clingo_propagate_control_t *control, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_propagator_decide(clingo_id_t thread_id, clingo_assignment_t const *assignment, clingo_literal_t fallback, void *data, clingo_literal_t *decision);' ) # observer callbacks cnt.append( 'extern "Python" bool pyclingo_observer_init_program(bool incremental, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_begin_step(void *data);') cnt.append('extern "Python" bool pyclingo_observer_end_step(void *data);') cnt.append( 'extern "Python" bool pyclingo_observer_rule(bool choice, clingo_atom_t const *head, size_t head_size, clingo_literal_t const *body, size_t body_size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_weight_rule(bool choice, clingo_atom_t const *head, size_t head_size, clingo_weight_t lower_bound, clingo_weighted_literal_t const *body, size_t body_size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_minimize(clingo_weight_t priority, clingo_weighted_literal_t const* literals, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_project(clingo_atom_t const *atoms, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_output_atom(clingo_symbol_t symbol, clingo_atom_t atom, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_output_term(clingo_symbol_t symbol, clingo_literal_t const *condition, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_output_csp(clingo_symbol_t symbol, int value, clingo_literal_t const *condition, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_external(clingo_atom_t atom, clingo_external_type_t type, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_assume(clingo_literal_t const *literals, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_heuristic(clingo_atom_t atom, clingo_heuristic_type_t type, int bias, unsigned priority, clingo_literal_t const *condition, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_acyc_edge(int node_u, int node_v, clingo_literal_t const *condition, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_theory_term_number(clingo_id_t term_id, int number, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_theory_term_string(clingo_id_t term_id, char const *name, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_theory_term_compound(clingo_id_t term_id, int name_id_or_type, clingo_id_t const *arguments, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_theory_element(clingo_id_t element_id, clingo_id_t const *terms, size_t terms_size, clingo_literal_t const *condition, size_t condition_size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_theory_atom(clingo_id_t atom_id_or_zero, clingo_id_t term_id, clingo_id_t const *elements, size_t size, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_observer_theory_atom_with_guard(clingo_id_t atom_id_or_zero, clingo_id_t term_id, clingo_id_t const *elements, size_t size, clingo_id_t operator_id, clingo_id_t right_hand_side_id, void *data);' ) # application callbacks cnt.append( 'extern "Python" char const *pyclingo_application_program_name(void *data);' ) cnt.append( 'extern "Python" char const *pyclingo_application_version(void *data);' ) cnt.append( 'extern "Python" unsigned pyclingo_application_message_limit(void *data);' ) cnt.append( 'extern "Python" bool pyclingo_application_main(clingo_control_t *control, char const *const * files, size_t size, void *data);' ) cnt.append( 'extern "Python" void pyclingo_application_logger(clingo_warning_t code, char const *message, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_application_print_model(clingo_model_t const *model, clingo_default_model_printer_t printer, void *printer_data, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_application_register_options(clingo_options_t *options, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_application_validate_options(void *data);' ) # application options callbacks cnt.append( 'extern "Python" bool pyclingo_application_options_parse(char const *value, void *data);' ) # ast callbacks cnt.append( 'extern "Python" bool pyclingo_ast_callback(clingo_ast_t const *, void *);' ) # script callbacks cnt.append( 'extern "Python" bool pyclingo_script_execute(clingo_location_t *loc, char const *code, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_script_call(clingo_location_t *loc, char const *name, void *arguments, size_t size, void *symbol_callback, void *symbol_callback_data, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_script_callable(char const * name, bool *ret, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_script_main(clingo_control_t *ctl, void *data);' ) code = '' if action == "embed": ffi.embedding_api('''\ bool pyclingo_execute(void *loc, char const *code, void *data); bool pyclingo_call(void *loc, char const *name, void *arguments, size_t size, void *symbol_callback, void *symbol_callback_data, void *data); bool pyclingo_callable(char const * name, bool *ret, void *data); bool pyclingo_main(void *ctl, void *data); ''') ffi.embedding_init_code(f"""\ import os import sys import clingo.script sys.path.insert(0, os.getcwd()) """) code = '''\ #ifdef CFFI_DLLEXPORT #undef CFFI_DLLEXPORT #define CFFI_DLLEXPORT #endif #ifdef PYPY_VERSION void pyclingo_finalize() { } #else void pyclingo_finalize() { if (Py_IsInitialized()) { PyGILState_Ensure(); Py_Finalize(); } } #endif ''' else: cnt.append( 'extern "Python" bool pyclingo_execute(void *loc, char const *code, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_call(void *loc, char const *name, void *arguments, size_t size, void *symbol_callback, void *symbol_callback_data, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_callable(char const * name, bool *ret, void *data);' ) cnt.append( 'extern "Python" bool pyclingo_main(void *ctl, void *data);') if action != "header": ffi.set_source('_clingo', f'''\ #include <clingo.h> {code} ''') ffi.cdef('\n'.join(cnt)) ffi.emit_c_code('_clingo.c') else: with open('_clingo.cdef', 'w') as f: f.write(''.join(f'{line}\n' for line in cnt))
from cffi import FFI from pathlib import Path sdk_dir = (Path(__file__).parent / '..' / '..' / 'Excel2013XLLSDK' ).resolve() ffi = FFI() # note need cffi 1.5.1 in order to support __stdcall ffi.embedding_api(''' extern int __stdcall xlAutoOpen(void); extern int __stdcall xlAutoClose(void); ''' ) # office 2016 seems to break AttachConsole/AllocConsole. Nasty # works in office 2013. # but how do we pick up the right virtualenv? # if we are in develop mode it's a problem: need to find the base of the tree for PYTHONPATH? # need to find the location of python.dll, and insall the right virtualenv PATH around it # not sure that the embedded install does that? or are we just picking upt he wrong one. # VIRTUAL_ENV can specify where, if not we use the ffi.set_source('test_xll', ''' #include <WINDOWS.H> #include <XLCALL.H> #include <stdio.h>
push _callback_types[{i}] push _callback_codes[{i}] // invoke the callback call _callback_funcs[{i}] // restore stack pointers stored in the pre-amble by msvc mov esp, ebp pop ebp // get original return location from the stack pop ecx // remove all the arguments, according to the size calculated add esp, _callback_sizes[{i}] // jmp to the location, no need to RET jmp ecx }} }} """ for i in range(0, MAX_CALLBACKS)), include_dirs=['src'] ) # for some reason we need this to trigger windows.h... ffi.embedding_api('') if __name__ == '__main__': ffi.compile(target='xlcall.pyd')
from cffi import FFI includes = open('./Py_code/liv.h').read() source = open('./Py_code/liv.py').read() ffi = FFI() ffi.embedding_api(includes) ffi.set_source('liv4', includes) ffi.embedding_init_code(source) ffi.compile()
ffi.set_source( '_xll', f""" #include <XLCALLDEF.h> """, include_dirs=['src'], extra_link_args=['/DEF:_xll.def'], #extra_compile_args=['/FAc'], sources=['src/DllMain.cpp'], ) ffi.embedding_api(''' extern "Python" int __stdcall xlAutoOpen(void); extern "Python" int __stdcall xlAutoClose(void); extern "Python" void __stdcall xlAutoFree12(LPXLOPER12); extern "Python" int __stdcall xlAutoAdd(void); extern "Python" int __stdcall xlAutoRemove(void); extern "Python" double __stdcall TheNumberOneInPy(); extern "Python" LPXLOPER12 __stdcall test_c_string(wchar_t*); extern "Python" LPXLOPER12 __stdcall os_environ(LPXLOPER12); ''') ffi.embedding_init_code(''' import sys try: import xll.addin except: sys.excepthook(*sys.exc_info()) raise ''')