From 97aad19cd020ee29c03c7d0005348e16fe34c77d Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Thu, 15 Jun 2017 22:40:55 +0100 Subject: [PATCH] python vs modules --- C/c_interface.c | 2 + C/save.c | 1 - CXX/yapdb.hh | 47 ------------------ CXX/yapi.cpp | 13 ++--- CXX/yapt.hh | 74 ++++++++++++++++++++++++----- library/dialect/swi/fli/swi.c | 4 +- packages/python/CMakeLists.txt | 18 ++++++- packages/python/py2pl.c | 9 +--- packages/python/py4yap.h | 2 +- packages/python/pybips.c | 4 +- packages/python/pyload.c | 2 +- packages/python/python.c | 4 +- packages/python/swig/CMakeLists.txt | 17 ++++--- packages/python/swig/__init__.py | 7 +-- packages/python/swig/setup.py.in | 15 ++++-- 15 files changed, 116 insertions(+), 103 deletions(-) diff --git a/C/c_interface.c b/C/c_interface.c index 4d70a7b7a..8968a0295 100755 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -77,6 +77,8 @@ struct foreign_context { struct PL_local_data *engine; /* invoking engine */ }; +X_API bool python_in_python; + X_API int YAP_Reset(yap_reset_t mode); #if !HAVE_STRNCPY diff --git a/C/save.c b/C/save.c index 5e3326316..a5ab5e4cf 100755 --- a/C/save.c +++ b/C/save.c @@ -1,4 +1,3 @@ - /************************************************************************* * * * YAP Prolog * diff --git a/CXX/yapdb.hh b/CXX/yapdb.hh index 600442d63..7fddf1375 100644 --- a/CXX/yapdb.hh +++ b/CXX/yapdb.hh @@ -74,53 +74,6 @@ public: virtual YAPModule module() { return YAPModule(m->AtomOfME); }; }; -/** - * @brief YAPFunctor represents Prolog functors Name/Arity - */ -class X_API YAPFunctor : public YAPProp { - friend class YAPApplTerm; - friend class YAPTerm; - friend class YAPPredicate; - friend class YAPQuery; - Functor f; - /// Constructor: receives Prolog functor and casts it to YAPFunctor - /// - /// Notice that this is designed for internal use only. - inline YAPFunctor(Functor ff) { f = ff; } - -public: - /// Constructor: receives name as an atom, plus arity - /// - /// This is the default method, and the most popular - YAPFunctor(YAPAtom at, uintptr_t arity) { f = Yap_MkFunctor(at.a, arity); } - - /// Constructor: receives name as a string plus arity - /// - /// Notice that this is designed for ISO-LATIN-1 right now - /// Note: Python confuses the 3 constructors, - /// use YAPFunctorFromString - inline YAPFunctor(const char *s, uintptr_t arity, bool isutf8 = true) { - f = Yap_MkFunctor(Yap_LookupAtom(s), arity); - } - /// Constructor: receives name as a wide string plus arity - /// - /// Notice that this is designed for UNICODE right now - /// - /// Note: Python confuses the 3 constructors, - /// use YAPFunctorFromWideString - inline YAPFunctor(const wchar_t *s, uintptr_t arity) { - CACHE_REGS f = Yap_MkFunctor(UTF32ToAtom(s PASS_REGS), arity); - } - /// Getter: extract name of functor as an atom - /// - /// this is for external usage. - YAPAtom name(void) { return YAPAtom(NameOfFunctor(f)); } - - /// Getter: extract arity of functor as an unsigned integer - /// - /// this is for external usage. - uintptr_t arity(void) { return ArityOfFunctor(f); } -}; /** * @brief Predicates diff --git a/CXX/yapi.cpp b/CXX/yapi.cpp index 383c23520..a78fcfdcc 100644 --- a/CXX/yapi.cpp +++ b/CXX/yapi.cpp @@ -195,8 +195,6 @@ YAPApplTerm::YAPApplTerm(YAPFunctor f) : YAPTerm() RECOVER_H(); } -YAPFunctor YAPApplTerm::getFunctor() { return YAPFunctor(FunctorOfTerm(gt())); } - Term &YAPTerm::operator[](arity_t i) { BACKUP_MACHINE_REGS(); @@ -408,12 +406,6 @@ YAPListTerm::YAPListTerm(YAPTerm ts[], arity_t n) } } -YAPVarTerm::YAPVarTerm() -{ - CACHE_REGS - mk(MkVarTerm()); -} - const char *YAPAtom::getName(void) { return Yap_AtomToUTF8Text(a, nullptr); } void YAPQuery::openQuery(Term t, Term *ts) @@ -1127,8 +1119,9 @@ YAPEngine::YAPEngine(int argc, char *argv[], /* ignore flags for now */ BACKUP_MACHINE_REGS(); Yap_RebootHandles(worker_id); - while (B->cp_b) + while (B && B->cp_b) B = B->cp_b; + if (B) { P = FAILCODE; Yap_exec_absmi(true, YAP_EXEC_ABSMI); /* recover stack space */ @@ -1138,7 +1131,7 @@ YAPEngine::YAPEngine(int argc, char *argv[], DEPTH = B->cp_depth; #endif /* DEPTH_LIMIT */ YENV = ENV = B->cp_env; - + } RECOVER_MACHINE_REGS(); } diff --git a/CXX/yapt.hh b/CXX/yapt.hh index 670b2f0c7..74dc5537a 100644 --- a/CXX/yapt.hh +++ b/CXX/yapt.hh @@ -256,6 +256,54 @@ public: inline bool initialized() { return t != 0; }; }; +/** + * @brief YAPFunctor represents Prolog functors Name/Arity + */ +class X_API YAPFunctor : public YAPProp { + friend class YAPApplTerm; + friend class YAPTerm; + friend class YAPPredicate; + friend class YAPQuery; + Functor f; + /// Constructor: receives Prolog functor and casts it to YAPFunctor + /// + /// Notice that this is designed for internal use only. + inline YAPFunctor(Functor ff) { f = ff; } + +public: + /// Constructor: receives name as an atom, plus arity + /// + /// This is the default method, and the most popular + YAPFunctor(YAPAtom at, uintptr_t arity) { f = Yap_MkFunctor(at.a, arity); } + + /// Constructor: receives name as a string plus arity + /// + /// Notice that this is designed for ISO-LATIN-1 right now + /// Note: Python confuses the 3 constructors, + /// use YAPFunctorFromString + inline YAPFunctor(const char *s, uintptr_t arity, bool isutf8 = true) { + f = Yap_MkFunctor(Yap_LookupAtom(s), arity); + } + /// Constructor: receives name as a wide string plus arity + /// + /// Notice that this is designed for UNICODE right now + /// + /// Note: Python confuses the 3 constructors, + /// use YAPFunctorFromWideString + inline YAPFunctor(const wchar_t *s, uintptr_t arity) { + CACHE_REGS f = Yap_MkFunctor(UTF32ToAtom(s PASS_REGS), arity); + } + /// Getter: extract name of functor as an atom + /// + /// this is for external usage. + YAPAtom name(void) { return YAPAtom(NameOfFunctor(f)); } + + /// Getter: extract arity of functor as an unsigned integer + /// + /// this is for external usage. + uintptr_t arity(void) { return ArityOfFunctor(f); } +}; + /** * @brief Compound Term */ @@ -273,7 +321,9 @@ public: YAPApplTerm(YAPFunctor f, YAPTerm ts[]); YAPApplTerm(const std::string s, std::vector ts); YAPApplTerm(YAPFunctor f); - YAPFunctor getFunctor(); + inline Functor functor() { return FunctorOfTerm(gt()); } + inline YAPFunctor getFunctor() { return YAPFunctor(FunctorOfTerm(gt())); } + Term getArg(arity_t i) { BACKUP_MACHINE_REGS(); Term t0 = gt(); @@ -480,7 +530,7 @@ class X_API YAPVarTerm : public YAPTerm { public: /// constructor - YAPVarTerm(); + YAPVarTerm() {mk(MkVarTerm()); }; /// get the internal representation CELL *getVar() { return VarOfTerm(gt()); } /// is the variable bound to another one @@ -490,16 +540,16 @@ public: } } bool unbound() { return IsUnboundVar(VarOfTerm(gt())); } - virtual bool isVar() { return true; } /// type check for unbound - virtual bool isAtom() { return false; } /// type check for atom - virtual bool isInteger() { return false; } /// type check for integer - virtual bool isFloat() { return false; } /// type check for floating-point - virtual bool isString() { return false; } /// type check for a string " ... " - virtual bool isCompound() { return false; } /// is a primitive term - virtual bool isAppl() { return false; } /// is a structured term - virtual bool isPair() { return false; } /// is a pair term - virtual bool isGround() { return false; } /// term is ground - virtual bool isList() { return false; } /// term is a list + inline bool isVar() { return true; } /// type check for unbound + inline bool isAtom() { return false; } /// type check for atom + inline bool isInteger() { return false; } /// type check for integer + inline bool isFloat() { return false; } /// type check for floating-point + inline bool isString() { return false; } /// type check for a string " ... " + inline bool isCompound() { return false; } /// is a primitive term + inline bool isAppl() { return false; } /// is a structured term + inline bool isPair() { return false; } /// is a pair term + inline bool isGround() { return false; } /// term is ground + inline bool isList() { return false; } /// term is a list }; /// @} diff --git a/library/dialect/swi/fli/swi.c b/library/dialect/swi/fli/swi.c index 3ba43176c..97d880d42 100755 --- a/library/dialect/swi/fli/swi.c +++ b/library/dialect/swi/fli/swi.c @@ -1,4 +1,4 @@ -/* xsswi.c */ +/* swi.c */ /* * Project: jpl for Yap Prolog * Author: Steve Moyle and Vitor Santos Costa @@ -800,7 +800,7 @@ X_API int PL_get_head(term_t ts, term_t h) { * */ -/** @brief t unifies with the true/false value in a. +/*b* @brief t unifies with the true/false value in a. * */ X_API int PL_unify_bool(term_t t, int a) { diff --git a/packages/python/CMakeLists.txt b/packages/python/CMakeLists.txt index 9a54f22d1..265aa38b1 100644 --- a/packages/python/CMakeLists.txt +++ b/packages/python/CMakeLists.txt @@ -9,11 +9,27 @@ set (CMAKE_POSITION_INDEPENDENT_CODE TRUE) include_directories( ${CMAKE_BINARY_DIR} ${CMAKE_SOURCE_DIR}/include ) -add_library (Obj4Py OBJECT ${PYTHON_SOURCES} ${PYTHON_HEADERS}) if (WIN32) +add_library (Py4YAP OBJECT ${PYTHON_SOURCES} ${PYTHON_HEADERS}) add_library (YAPPython SHARED pyload.c ${PYTHON_HEADERS} ) +else() +add_library (Py4YAP SHARED ${PYTHON_SOURCES} ${PYTHON_HEADERS}) +add_library (YAPPython SHARED pyload.c ${PYTHON_SOURCES} ${PYTHON_HEADERS}) + +# arithmetic hassle. +set_property(TARGET Py4YAP PROPERTY CXX_STANDARD 11) +set_property(TARGET Py4YAP PROPERTY CXX_STANDARD_REQUIRED ON) + +target_link_libraries(Py4YAP libYap ${PYTHON_LIBRARIES}) +target_link_libraries(YAPPython Py4YAP) + + MY_install(TARGETS Py4YAP + LIBRARY DESTINATION ${libdir} + RUNTIME DESTINATION ${dlls} + ARCHIVE DESTINATION ${libdir} ) endif() + # arithmetic hassle. set_property(TARGET YAPPython PROPERTY CXX_STANDARD 11) set_property(TARGET YAPPython PROPERTY CXX_STANDARD_REQUIRED ON) diff --git a/packages/python/py2pl.c b/packages/python/py2pl.c index 39a97e3e9..115dacdeb 100644 --- a/packages/python/py2pl.c +++ b/packages/python/py2pl.c @@ -30,13 +30,8 @@ foreign_t python_to_term(PyObject *pVal, term_t t) { //fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs(" >>***\n",stderr); rc= PL_unify_atom(t, ATOM_none); //fputs("<<*** ",stderr);Yap_DebugPlWrite(YAP_GetFromSlot(t)); fputs(" >>***\n",stderr); - } - if (PyBool_Check(pVal)) { - if (PyObject_IsTrue(pVal)) { - rc = rc && PL_unify_atom(t, ATOM_true); - } else { - rc = rc && PL_unify_atom(t, ATOM_false); - } + } else if (PyBool_Check(pVal)) { + rc = rc && PL_unify_bool(t, PyObject_IsTrue(pVal)); } else if (PyLong_Check(pVal)) { rc = rc && PL_unify_int64(t, PyLong_AsLong(pVal)); #if PY_MAJOR_VERSION < 3 diff --git a/packages/python/py4yap.h b/packages/python/py4yap.h index 8f7f05726..1e3ddb0de 100644 --- a/packages/python/py4yap.h +++ b/packages/python/py4yap.h @@ -76,7 +76,7 @@ extern PyObject *py_F2P; extern PyObject *py_Sys; extern PyObject *py_ModDict; -extern bool python_in_python; +extern X_API bool python_in_python; extern bool python_release_GIL(term_t state); extern term_t python_acquire_GIL(void); diff --git a/packages/python/pybips.c b/packages/python/pybips.c index 425fdd198..0cab4508b 100644 --- a/packages/python/pybips.c +++ b/packages/python/pybips.c @@ -1,5 +1,3 @@ -#include "py4yap.h" - /** * * @section Python Built-Ins @@ -10,7 +8,7 @@ //@{ -#include "python.h" +#include "py4yap.h" static PyObject *finalLookup(PyObject *i, const char *s) { PyObject *rc; diff --git a/packages/python/pyload.c b/packages/python/pyload.c index 8c1457ca2..0127ae185 100644 --- a/packages/python/pyload.c +++ b/packages/python/pyload.c @@ -8,7 +8,7 @@ X_API bool init_python_dll(void); X_API bool init_python_dll(void) { do_init_python(); - + install_pypreds(); return 1; } #ifdef _WIN32 diff --git a/packages/python/python.c b/packages/python/python.c index e47c4adbb..9899b1161 100644 --- a/packages/python/python.c +++ b/packages/python/python.c @@ -1,4 +1,5 @@ + #include "py4yap.h" #include @@ -109,7 +110,7 @@ init_python_stream(void) X_API PyObject *py_F2P; -bool python_in_python; +extern X_API bool python_in_python; static void add_modules(void) { py_Main = PyImport_AddModule("__main__"); @@ -199,7 +200,6 @@ X_API bool do_init_python(void) { Py_Initialize(); install_py_constants(); PL_reset_term_refs(t); - install_pypreds(); install_pl2pl(); // PyGILState_Release(gstate); add_modules(); diff --git a/packages/python/swig/CMakeLists.txt b/packages/python/swig/CMakeLists.txt index 20c959b86..dc3aa6bd2 100644 --- a/packages/python/swig/CMakeLists.txt +++ b/packages/python/swig/CMakeLists.txt @@ -26,8 +26,6 @@ if (WIN32) set (SYS_DLLS ${GMP_LIBRARIES}) -message(STATUS "SYS_ = ${tmp}") - set (SYS_DLLS c:/msys64/mingw64/bin/libgmp-10.dll) endif() @@ -37,9 +35,9 @@ endif() # # # INSTALL ( TARGETS ${SWIG_MODULE_Py2YAP_REAL_NAME} - # RUNTIME DESTINATION ${PYTHON_MODULE_PATH} - # ARCHIVE DESTINATION ${PYTHON_MODULE_PATH} - # LIBRARY DESTINATION ${PYTHON_MODULE_PATH} + # RUNTIME DESTINATION ${PYTHON_MODULE_PATH} + # ARCHIVE DESTINATION ${PYTHON_MODULE_PATH} + # LIBRARY DESTINATION ${PYTHON_MODULE_PATH} # ) @@ -49,19 +47,20 @@ endif() $ $ $ - $ - $ + $ + $ + $ ) if (TARGET real) list(APPEND python_dlls $ ) - message(STATUS "${python_dlls}") endif() set (PL ${pl_library} ${PROLOG_SOURCES} ) add_custom_target( YAP4PY_SETUP - COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/packages/swig/yap.i ${CMAKE_CURRENT_BINARY_DIR} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/packages/swig/yap.i ${CMAKE_CURRENT_BINARY_DIR} + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/config.h ${CMAKE_CURRENT_BINARY_DIR} COMMAND ${CMAKE_COMMAND} -E copy ${PYTHON_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/yap4py COMMAND ${CMAKE_COMMAND} -E copy ${python_dlls} ${SYS_DLLS} ${CMAKE_BINARY_DIR}/libYap${CMAKE_SHARED_LIBRARY_SUFFIX} ${CMAKE_BINARY_DIR}/${YAP_STARTUP} ${PYTHON_SOURCES} ${CMAKE_CURRENT_BINARY_DIR}/yap4py COMMAND ${CMAKE_COMMAND} -E copy ${PL} ${CMAKE_CURRENT_BINARY_DIR}/yap4py/prolog diff --git a/packages/python/swig/__init__.py b/packages/python/swig/__init__.py index 6584da5f1..a394860ea 100644 --- a/packages/python/swig/__init__.py +++ b/packages/python/swig/__init__.py @@ -14,11 +14,12 @@ if platform.system() == 'Windows': dll = glob.glob(os.path.join(yap_lib_path,dll))[0] dll = os.path.abspath(dll) ctypes.WinDLL(dll) + load('libYap*') else: def load( dll ): dll = glob.glob(os.path.join(yap_lib_path,dll))[0] dll = os.path.abspath(dll) ctypes.CDLL(dll, mode=ctypes.RTLD_GLOBAL) - - -load('libYap*') + load('libYAP+*') + load('libPy4YAP*') + load('libYap*') diff --git a/packages/python/swig/setup.py.in b/packages/python/swig/setup.py.in index 913045c78..4d46fb5e6 100644 --- a/packages/python/swig/setup.py.in +++ b/packages/python/swig/setup.py.in @@ -6,7 +6,7 @@ from __future__ import print_function from setuptools import setup -from setuptools.extension import Extension +from setuptools.extension import Extension from codecs import open from os import path, makedirs, walk from shutil import copytree, rmtree, copy2, move @@ -54,31 +54,38 @@ pkg_root = pjoin(here, name) my_extra_link_args = [] if platform.system() == 'Darwin': my_extra_link_args = ['-Wl,-rpath', '-Wl,${_ABS_PYTHON_MODULE_PATH}'] + win_libs = [] + local_libs = ['YAP++','Py4YAP'] # or dll in glob('yap/dlls/*'): # move( dll ,'lib' ) elif platform.system() == 'Windows': + local_libs = [] + win_libs = ['wsock32','ws2_32'] my_extra_link_args = ['-Wl,-export-all-symbols'] cplus = [''] -bpy2yap = \ +bpy2yap = [] native_sources = ['yap.i'] here = path.abspath(path.dirname(__file__)) # Get the long description from the README file + + extensions = [Extension('_yap', native_sources, define_macros=[('MAJOR_VERSION', '1'), ('MINOR_VERSION', '0'), ('_YAP_NOT_INSTALLED_', '1'), + ('YAP_PYTHON', '1'), ('_GNU_SOURCE', '1')], runtime_library_dirs=['yap4py', '${libdir}', '${bindir}'], swig_opts=['-modern', '-c++', '-py3', '-DX_API', '-I${CMAKE_SOURCE_DIR}/CXX', '-I${CMAKE_SOURCE_DIR}/include', '-I${CMAKE_SOURCE_DIR}/H', '-I${CMAKE_SOURCE_DIR}/H/generated', '-I${CMAKE_SOURCE_DIR}/os', '-I${CMAKE_SOURCE_DIR}/OPTYap', '-I../../..'], - library_dirs=['../../..', '../../../CXX', '../../packages/python', "${dlls}", "${bindir}", '.'], + library_dirs=['../../..', '../../../CXX', '..', "${dlls}", "${bindir}", '.'], extra_link_args=my_extra_link_args, - libraries=['Yap','${PYTHON_LIBRARIES}','${GMP_LIBRARIES}','wsock32','ws2_32'], + libraries=['Yap','${PYTHON_LIBRARIES}','${GMP_LIBRARIES}']+win_libs+local_libs, include_dirs=['../../..', '${GMP_INCLUDE_DIRS}', '${CMAKE_SOURCE_DIR}/H',