def handle_use_module_with_library(engine, heap, module, path, imports=None): import os import os.path from prolog.builtin.sourcehelper import get_filehandle newpath = None if path.signature().eq(libsig): arg = path.argument_at(0) if isinstance(arg, Var) or not isinstance( arg, Atom): # XXX throw different errors error.throw_instantiation_error() modulename = arg.name() assert modulename is not None for libpath in engine.modulewrapper.libs: temppath = os.path.join(libpath, modulename) try: assert isinstance(temppath, str) fd, _ = get_filehandle(temppath) except OSError: continue assert 0, "unreachable" else: assert isinstance(fd, int) os.close(fd) # cleanup newpath = Atom(temppath) break if not newpath: error.throw_existence_error("source_sink", arg) else: error.throw_existence_error("source_sink", path) assert isinstance(newpath, Atom) handle_use_module(engine, heap, module, newpath, imports)
def handle_use_module_with_library(engine, heap, module, path, imports=None): import os import os.path from prolog.builtin.sourcehelper import get_filehandle newpath = None if path.signature().eq(libsig): arg = path.argument_at(0) if isinstance(arg, Var) or not isinstance(arg, Atom): # XXX throw different errors error.throw_instantiation_error() modulename = arg.name() assert modulename is not None for libpath in engine.modulewrapper.libs: temppath = os.path.join(libpath, modulename) try: assert isinstance(temppath, str) fd, _ = get_filehandle(temppath) except OSError: continue assert 0, "unreachable" else: assert isinstance(fd, int) os.close(fd) # cleanup newpath = Atom(temppath) break if not newpath: error.throw_existence_error("source_sink", arg) else: error.throw_existence_error("source_sink", path) assert isinstance(newpath, Atom) handle_use_module(engine, heap, module, newpath, imports)
def get_module(self, name, errorterm): module = self._get_module(name, self.version) if module is not None: return module assert isinstance(errorterm, Callable) error.throw_existence_error("procedure", errorterm.get_prolog_signature())
def impl_add_library_dir(engine, heap, path): from os.path import isdir assert path is not None if not isdir(path): error.throw_existence_error("source_sink", Callable.build(path)) libs = engine.modulewrapper.libs for lib in libs: if lib == path: return engine.modulewrapper.libs.append(path)
def impl_see(engine, heap, obj): w = engine.streamwrapper try: stream = w.aliases[obj] impl_set_input(engine, heap, stream) except KeyError: try: stream = PrologInputStream(open_file_as_stream(obj, rwa["read"], -1)) w.streams[stream.fd()] = stream w.aliases["$stream_%d" % stream.fd()] = stream impl_set_input(engine, heap, stream) except OSError: error.throw_existence_error("source_sink", term.Callable.build(obj))
def impl_see(engine, heap, obj): w = engine.streamwrapper try: stream = w.aliases[obj] impl_set_input(engine, heap, stream) except KeyError: try: stream = PrologInputStream( open_file_as_stream(obj, rwa["read"], -1)) w.streams[stream.fd()] = stream w.aliases["$stream_%d" % stream.fd()] = stream impl_set_input(engine, heap, stream) except OSError: error.throw_existence_error("source_sink", term.Callable.build(obj))
def _get_function(self, signature, module, query): function = module.lookup(signature) if function.rulechain is None and self.modulewrapper.system is not None: function = self.modulewrapper.system.lookup(signature) if function.rulechain is None: return error.throw_existence_error( "procedure", query.get_prolog_signature()) return function
def _get_function(self, signature, module, query): function = module.lookup(signature) if function.rulechain is None and self.modulewrapper.system is not None: function = self.modulewrapper.system.lookup(signature) if function.rulechain is None: return error.throw_existence_error("procedure", query.get_prolog_signature()) return function
def get_source(filename): try: assert isinstance(filename, str) fd, actual_filename = get_filehandle(filename, True) except OSError: throw_existence_error("source_sink", Callable.build(filename)) assert 0, "unreachable" # make the flow space happy try: content = [] while 1: s = os.read(fd, 4096) if not s: break content.append(s) file_content = "".join(content) finally: os.close(fd) return file_content, actual_filename
def impl_open_options(engine, heap, srcpath, mode, stream, options): if not isinstance(stream, term.Var): error.throw_type_error("variable", stream) opts = make_option_dict(options) mode = rwa.get(mode, None) if mode is None: error.throw_domain_error("io_mode", term.Callable.build("mode not supported")) else: buffering = opts.get("buffer", "full") if buffering == "full": bufmode = -1 elif buffering == "line": bufmode = 1 elif buffering == "false": bufmode = 0 else: error.throw_domain_error("buffering", term.Callable.build(buffering)) assert 0, "unreachable" try: if mode == "r": prolog_stream = PrologInputStream( open_file_as_stream(srcpath, mode, bufmode)) else: prolog_stream = PrologOutputStream( open_file_as_stream(srcpath, mode, bufmode)) except OSError: error.throw_existence_error("source_sink", term.Callable.build(srcpath)) assert 0, "unreachable" engine.streamwrapper.streams[prolog_stream.fd()] = prolog_stream try: alias = opts["alias"] prolog_stream.alias = alias except KeyError: alias = "$stream_%d" % prolog_stream.fd() engine.streamwrapper.aliases[alias] = prolog_stream stream.unify(term.Callable.build(alias), heap)
def impl_open_options(engine, heap, srcpath, mode, stream, options): if not isinstance(stream, term.Var): error.throw_type_error("variable", stream) opts = make_option_dict(options) mode = rwa.get(mode, None) if mode is None: error.throw_domain_error("io_mode", term.Callable.build( "mode not supported")) else: buffering = opts.get("buffer", "full") if buffering == "full": bufmode = -1 elif buffering == "line": bufmode = 1 elif buffering == "false": bufmode = 0 else: error.throw_domain_error("buffering", term.Callable.build(buffering)) assert 0, "unreachable" try: if mode == "r": prolog_stream = PrologInputStream(open_file_as_stream( srcpath, mode, bufmode)) else: prolog_stream = PrologOutputStream(open_file_as_stream( srcpath, mode, bufmode)) except OSError: error.throw_existence_error("source_sink", term.Callable.build(srcpath)) assert 0, "unreachable" engine.streamwrapper.streams[prolog_stream.fd()] = prolog_stream try: alias = opts["alias"] prolog_stream.alias = alias except KeyError: alias = "$stream_%d" % prolog_stream.fd() engine.streamwrapper.aliases[alias] = prolog_stream stream.unify(term.Callable.build(alias), heap)