Changeset 5300:99bff9d7cc08 in livinglogic.python.xist

Show
Ignore:
Timestamp:
01/26/13 16:44:28 (7 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Remove variable self again.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • test/test_ul4.py

    r5297 r5300  
    387387        raise RuntimeError((stderr or stdout).splitlines()[0]) 
    388388    return ul4on.loads(stdout) 
     389 
     390 
     391def call_java_interpretedtemplate_by_python(__, *, keepws=True, **variables): 
     392    """ 
     393    Compile the function from the source ``__``, and generate Java source that 
     394    recreates the function from the Python generated dump and executes the 
     395    function with the variables ``variables``. 
     396 
     397    (this requires an installed Java compiler and the Java UL4 jar) 
     398    """ 
     399 
     400    codetemplate = """ 
     401    com.livinglogic.ul4.InterpretedFunction function = %(function)s; 
     402    java.util.Map<String, Object> variables = %(variables)s; 
     403    Object output = function.call(variables); 
     404    // We can't use ``System.out.print`` here, because this gives us no control over the encoding 
     405    // Use ``System.out.write`` to make sure the output is in UTF-8 
     406    byte[] outputBytes = com.livinglogic.ul4on.Utils.dumps(output).getBytes("utf-8"); 
     407    System.out.write(outputBytes, 0, outputBytes.length); 
     408    """ 
     409 
     410    f = sys._getframe(1) 
     411    print("Testing Java InterpretedFunction (compiled by Python) ({}, line {}):".format(f.f_code.co_filename, f.f_lineno)) 
     412    functionsource = ul4c.Function(__, keepws=keepws).javasource() 
     413    java = codetemplate % dict(variables=misc.javaexpr(variables), function=functionsource) 
     414    return ul4on.loads(java_runsource(java)) 
     415 
     416 
     417def call_java_interpretedtemplate_by_java(__, keepws=True, **variables): 
     418    """ 
     419    Generate Java source that compiles the function source ``__`` and executes the 
     420    function with the variables ``variables``. 
     421 
     422    (this requires an installed Java compiler and the Java UL4 jar) 
     423    """ 
     424 
     425    codetemplate = """ 
     426    com.livinglogic.ul4.InterpretedFunction function = new com.livinglogic.ul4.InterpretedFunction(%(source)s, %(keepws)s); 
     427    java.util.Map<String, Object> variables = %(variables)s; 
     428    Object output = function.call(variables); 
     429    // We can't use ``System.out.print`` here, because this gives us no control over the encoding 
     430    // Use ``System.out.write`` to make sure the output is in UTF-8 
     431    byte[] outputBytes = com.livinglogic.ul4on.Utils.dumps(output).getBytes("utf-8"); 
     432    System.out.write(outputBytes, 0, outputBytes.length); 
     433    """ 
     434 
     435    f = sys._getframe(1) 
     436    print("Testing Java InterpretedFunction (compiled by Java) ({}, line {}):".format(f.f_code.co_filename, f.f_lineno)) 
     437    java = codetemplate % dict(source=misc.javaexpr(__), variables=misc.javaexpr(variables), keepws=misc.javaexpr(keepws)) 
     438    return ul4on.loads(java_runsource(java)) 
    389439 
    390440 
     
    29653015@pytest.mark.ul4 
    29663016def test_self(r): 
    2967     assert "True" == r("<?print istemplate(self)?>") 
    2968     assert "x" == r("<?template x?><?print self.name?><?end template?><?render x.render()?>") 
    29693017    with raises("can't assign to self"): 
    29703018        assert "42" == r("<?code self = 42?><?print self?>") 
     
    32073255@pytest.mark.ul4 
    32083256def test_function_name(c): 
    3209     assert "f" == c("<?function f?><?return self.name?><?end function?><?return f()?>") 
     3257    assert "f" == c("<?function f?><?return f.name?><?end function?><?return f(f=f)?>") 
    32103258 
    32113259