Example #1
0
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)
Example #2
0
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)
Example #3
0
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)
Example #4
0
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)
Example #5
0
 
""" + '\n'.join(f"E({d:03X})" for d in range(0, 0xFFF)),
               include_dirs=[build_xlcall.src_dir])

ffi.embedding_init_code(r"""
import sys
import os.path

from xll.output import OutputDebugStringWriter

# send all output to the debugging console
sys.stderr = sys.stdout = OutputDebugStringWriter()

# if we have a PYDEVD port set, then attach to the debugger
if 'PYDEVD_PORT' in os.environ:
    import pydevd
    pydevd.settrace(
        host=os.getenv('PYDEVD_HOST', 'localhost'),
        port=int(os.getenv('PYDEVD_PORT')),
        suspend=False,
        stdoutToServer=True,
        stderrToServer=True
    )

# defer to the python module
import xll.auto
""")

if __name__ == '__main__':
    ffi.compile(target=os.path.join(os.path.dirname(__file__), 'python.xll'))
Example #6
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()
Example #8
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 *);'
    )
    # 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))
Example #9
0
     extra_link_args = ['/DEBUG']
)

ffi.embedding_init_code('''
    from test_xll import ffi

    from xlcall._xlcall import ffi as xlcall
    xlcall = xlcall.dlopen('XLCALL32')

    # we should look for the module we want to expose, and def_extern all of them here?
    # then the xlAutoOpen implementation needs to loop over all the def_externs..

    @ffi.def_extern(error=0)
    def xlAutoOpen():
        import os
        for key, value in os.environ.items():
            print('{key:<32s} {value}'.format(key=key, value=value))
        import sys
        print(sys.path)
        print(sys.prefix)
        print(sys.executable)
        print('XlCallVer: {:d}'.format(xlcall.XLCallVer()))
        return 1

    @ffi.def_extern(error=0)
    def xlAutoClose():
        return 1
''')


if __name__ == '__main__':
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()
Example #11
0
    assert(pNtHeaders32->Signature == 0x4550);
    
    assert(pNtHeaders32->OptionalHeader.DataDirectory[0].VirtualAddress != 0);
    assert(pNtHeaders32->OptionalHeader.DataDirectory[0].Size != 0);
        
    pExportDirectory = 
        (IMAGE_EXPORT_DIRECTORY*)     
        (pNtHeaders32->OptionalHeader.DataDirectory[0].VirtualAddress + pRVABase);
        
    _cffi_initialize_python();                  
    
    return TRUE;    
}
""")

ffi.embedding_init_code(open("scratch-init.py", "r").read())

ffi.compile(target="scratch.dll", verbose=True)

def test_it():
    ffi = FFI()
    ffi.cdef("""
        int _0(int, int);
    """)

    api = ffi.dlopen('scratch.dll')

    # why no api call here?

    print(api._0(1, 10))
Example #12
0
   """,
    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
''')

if __name__ == '__main__':
    ffi.compile(target='python.xll')