Commits

Shashank Bharadwaj committed c7218ee Draft

new test case

Comments (0)

Files changed (1)

fix-asm-via-cfg.patch

      private static volatile PythonCompiler compiler = loadDefaultCompiler();
  
      public static void setCompiler(PythonCompiler compiler) {
-@@ -22,7 +25,8 @@
+@@ -22,9 +25,15 @@
      }
  
      private static PythonCompiler loadDefaultCompiler() {
 +        return new JDK7Compiler();
      }
  
++    /**
++     * Generate a PyCode by compiling the input module
++     *
++     * @return the compiled PyCode object
++     */
      public static PyCode compile(mod node, String name, String filename,
-@@ -36,4 +40,17 @@
+             boolean linenumbers, boolean printResults, CompilerFlags cflags) {
+         try {
+@@ -36,4 +45,21 @@
              throw ParserFacade.fixParseError(null, t, filename);
          }
      }
 +
++    /**
++     * Compile the incoming module (node) into java byte code. Then write the
++     * bytes of the generated Java classfile into the OutputStream os.
++     */
 +    public static void compile(mod node, OutputStream os, String name,
 +    		String filename, boolean linenumbers, boolean printResults,
 +    		CompilerFlags cflags, long mtime) {
 +        try {
 +            PythonCodeBundle bundle = compiler.compile(node, name, filename,
 +                    linenumbers, printResults, cflags);
-+            bundle.saveCode(Options.proxyDebugDirectory);
++            //bundle.saveCode(Options.proxyDebugDirectory);
 +            bundle.writeTo(os);
 +        } catch (Throwable t) {
 +            throw ParserFacade.fixParseError(null, t, filename);
 new file mode 100644
 --- /dev/null
 +++ b/tmp/test.py
-@@ -0,0 +1,25 @@
+@@ -0,0 +1,64 @@
 +import imp
 +import os
 +import sys
++import shutil
 +
 +COMPILED_SUFFIX = '.pyc'
++dir = os.path.abspath(os.path.dirname(__file__))
 +
 +if sys.platform.startswith('java'):
 +    COMPILED_SUFFIX = '$py.class'
 +
-+def test_load_compiled():
-+    compiled = os.__file__
-+    if compiled.endswith('.py'):
-+        compiled = compiled[:-3] + COMPILED_SUFFIX
-+
-+    os.__doc__ = 'foo'
-+    assert os == imp.load_compiled("os", compiled)
-+    print os.__doc__ == 'foo'
-+    with open(compiled, 'rb') as fp:
-+        os.__doc__ = 'foo'
-+        assert os == imp.load_compiled("os", compiled, fp)
-+        print os.__doc__ == 'foo'
-+
-+
-+
-+test_load_compiled()
++
++def read(filename):
++    """Read data from filename"""
++    fp = open(filename)
++    data = fp.read()
++    fp.close()
++    return data
++
++
++
++def test_dunder_init():
++     os.mkdir('foo')
++
++     # typical import: foo.__init__$py.class is actually compiled
++     # with a class name of foo
++     init = os.path.join('foo', '__init__.py')
++     fp = open(init, 'w')
++     fp.write("bar = 'test'")
++     fp.close()
++     module_obj = __import__('foo')
++     #assert module_obj.__file__ == init
++     assert module_obj.bar == 'test'
++
++     init_compiled = init[:-3] + COMPILED_SUFFIX
++     assert os.path.exists(init_compiled)
++     bytecode = read(init_compiled)
++
++     # trigger an abnormal import of foo.__init__; ask for it by the
++     # mismatched __init__ name
++     fp = open(os.path.join('foo', 'test.py'), 'w')
++     fp.write("import __init__; baz = __init__.bar + 'test'; "
++              "init_file = __init__.__file__")
++     fp.close()
++     module_obj = __import__('foo.test')
++     assert module_obj.test.baz == 'testtest'
++     assert module_obj.test.init_file == os.path.join(dir, 'foo', '__init__' + COMPILED_SUFFIX), module_obj.test.init_file + '\n' + os.path.join(dir, 'foo', '__init__' + COMPILED_SUFFIX) 
++     
++     # Ensure a recompile of __init__$py.class wasn't triggered to
++     # satisfy the abnormal import
++     assert bytecode == read(init_compiled), 'bytecode was recompiled'
++
++     # Ensure load_module can still load it as foo (doesn't
++     # recompile)
++     module_obj = imp.load_module('foo', *imp.find_module('foo'))
++     assert module_obj.bar == 'test'
++
++     # Again ensure we didn't recompile
++     assert bytecode == read(init_compiled), 'bytecode was recompiled'
++
++try:
++    test_dunder_init()
++finally:
++    shutil.rmtree('foo')