# Buggy code
def evalQuery(graph, query, initBindings, base=None):
    ctx = QueryContext(graph)

    ctx.prologue = query.prologue

    if initBindings:
        for k, v in initBindings.iteritems():
            if not isinstance(k, Variable):
                k = Variable(k)
            ctx[k] = v
        # ctx.push()  # nescessary?

    main = query.algebra

    # import pdb; pdb.set_trace()
    if main.datasetClause:
        if ctx.dataset is None:
            raise Exception(
                "Non-conjunctive-graph doesn't know about "
                + "graphs! Try a query without FROM (NAMED)."
            )

        ctx = ctx.clone()  # or push/pop?

        firstDefault = False
        for d in main.datasetClause:
            if d.default:
                if firstDefault:
                    # replace current default graph
                    dg = ctx.dataset.get_context(BNode())
                    ctx = ctx.pushGraph(dg)
                    firstDefault = True

                ctx.load(d.default, default=True)

            elif d.named:
                g = d.named
                ctx.load(g, default=False)

    return evalPart(ctx, main)


# Traceback (UnicodeDecodeError)
from rdflib.util import from_n3
sample = "\"Sample string with trailing backslash\\\"^^xsd:string"
from_n3(sample)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.4/site-packages/rdflib/util.py", line 183, in from_n3
value = value.encode("raw-unicode-escape").decode("unicode-escape")
UnicodeDecodeError: 'unicodeescape' codec can't decode byte 0x5c in position 37: \ at end of string

# Fixed code
def evalQuery(graph, query, initBindings, base=None):
    ctx = QueryContext(graph)

    ctx.prologue = query.prologue

    main = query.algebra

    if initBindings:
        # add initBindings as a values clause

        values = {}  # no dict comprehension in 2.6 :(
        for k, v in initBindings.iteritems():
            if not isinstance(k, Variable):
                k = Variable(k)
            values[k] = v

        main = main.clone()  # clone to not change prepared q
        main["p"] = main.p.clone()
        # Find the right place to insert MultiSet join
        repl = main.p
        if repl.name == "Slice":
            repl["p"] = repl.p.clone()
            repl = repl.p
        if repl.name == "Distinct":
            repl["p"] = repl.p.clone()
            repl = repl.p
        if repl.p.name == "OrderBy":
            repl["p"] = repl.p.clone()
            repl = repl.p
        if repl.p.name == "Extend":
            repl["p"] = repl.p.clone()
            repl = repl.p

        repl["p"] = Join(repl.p, ToMultiSet(Values([values])))

        # TODO: Vars?

    if main.datasetClause:
        if ctx.dataset is None:
            raise Exception(
                "Non-conjunctive-graph doesn't know about "
                + "graphs! Try a query without FROM (NAMED)."
            )

        ctx = ctx.clone()  # or push/pop?

        firstDefault = False
        for d in main.datasetClause:
            if d.default:
                if firstDefault:
                    # replace current default graph
                    dg = ctx.dataset.get_context(BNode())
                    ctx = ctx.pushGraph(dg)
                    firstDefault = True

                ctx.load(d.default, default=True)

            elif d.named:
                g = d.named
                ctx.load(g, default=False)

    return evalPart(ctx, main)