Wimmics/corese

Exception not raised while using python library

NicoRobertIn opened this issue · 1 comments

Issue Description:

While using python library and loading a turtle file with an undefined prefix Corese outputs an error in the console but does not raise an exception on python library side... Is this normal?

In a python script

Steps to Reproduce:

from queue import Queue, Empty
from sys import builtin_module_names
from subprocess import Popen, PIPE, DEVNULL
from threading  import Thread
from time import sleep
from atexit import register
from py4j.java_gateway import JavaGateway

def enqueue_output(out, queue):
    for line in iter(out.readline, b''):
        queue.put(line)
    out.close()

ON_POSIX = 'posix' in builtin_module_names
error_queue = Queue()

def get_error_line():
    try:
        return error_queue.get_nowait()
    except Empty:
        return ""


def get_error_output():
    total_output = []
    current_line = "a"

    while len(current_line) > 0:
        current_line = get_error_line()
        if isinstance(current_line, bytes):
            current_line = current_line.decode('utf-8')
        total_output.append(current_line)

    return "\n".join(total_output).strip()    

# Start java gateway
java_process = Popen(
    ['java', '-jar', '-Dfile.encoding=UTF-8', 'corese-library-python-4.4.1.jar'],
    stdout=PIPE,
    stderr=DEVNULL,
    close_fds=ON_POSIX
)
reader_thread = Thread(target=enqueue_output, args=(java_process.stdout, error_queue))
reader_thread.daemon = True # thread dies with the program
reader_thread.start()

# Waiting for the java server to start up
sleep(1)
gateway = JavaGateway()
register(gateway.shutdown)

# Import of class
Graph = gateway.jvm.fr.inria.corese.core.Graph
Load = gateway.jvm.fr.inria.corese.core.load.Load

def load(path):
    """Load a graph from a local file or a URL

    :param path: local path or a URL or a list of these
    :returns: the graph load
    """

    graph = Graph()

    ld = Load.create(graph)

    try:
        get_error_output()
        ld.parse(path)
        graph = Graph()
        ld = Load.create(graph)
        ld.parse(path)
        syntax_errors = get_error_output()
        print("syntax errors", syntax_errors)

        return graph

    except Exception as e:
        # syntax_errors = parse_syntax_errors()
        print("error", str(e).strip())

load("inconsistant.ttl")

In inconsistant.ttl

@prefix owl: <http://www.w3.org/2002/07/owl#> .

ex:A a oxl:Class .

Expected Behavior:

The print should be "error [Thread-1] ERROR fr.inria.corese.sparql.triple.parser.ASTQuery - Undefined prefix: oxl:Class"

Actual Behavior:

The print is "syntax errors [Thread-1] ERROR fr.inria.corese.sparql.triple.parser.ASTQuery - Undefined prefix: oxl:Class"

I have added a new property called strict mode which enables the raising of this type of exception. To activate it, you need to create a property file with the content STRICT_MODE = true. Then, launch Corese Python using the --init option.

java -jar corese-python.jar --init corese.properties