python vs modules

This commit is contained in:
Vitor Santos Costa 2017-06-15 22:40:55 +01:00
parent 3a06d4fecb
commit 97aad19cd0
15 changed files with 116 additions and 103 deletions

View File

@ -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

View File

@ -1,4 +1,3 @@
/*************************************************************************
* *
* YAP Prolog *

View File

@ -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

View File

@ -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();
}

View File

@ -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<YAPTerm> 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
};
/// @}

View File

@ -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) {

View File

@ -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)

View File

@ -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

View File

@ -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);

View File

@ -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;

View File

@ -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

View File

@ -1,4 +1,5 @@
#include "py4yap.h"
#include <VFS.h>
@ -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();

View File

@ -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()
$<TARGET_FILE:tries>
$<TARGET_FILE:itries>
$<TARGET_FILE:sys>
$<TARGET_FILE:yap_random>
$<TARGET_FILE:YAPPython>
$<TARGET_FILE:yap_random>
$<TARGET_FILE:YAP++>
$<TARGET_FILE:YAPPython>
)
if (TARGET real)
list(APPEND python_dlls $<TARGET_FILE:real>
)
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

View File

@ -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*')

View File

@ -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',