gt:             if (ret != null) {
context: // Copyright (c) Corporation for National Research Initiatives
  package org.python.core;
  
  import java.io.ByteArrayOutputStream;
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  /**
   * Utility functions for "import" support.
   */
  public class imp {
      private static final String IMPORT_LOG = "import";
  
      private static final String UNKNOWN_SOURCEFILE = "<unknown>";
  
      public static final int APIVersion = 12;
  
      private static Object syspathJavaLoaderLock = new Object();
  
      private static ClassLoader syspathJavaLoader = null;
  
      public static ClassLoader getSyspathJavaLoader() {
          synchronized (syspathJavaLoaderLock) {
              if (syspathJavaLoader == null) {
                  syspathJavaLoader = new SyspathJavaLoader();
              }
          }
          return syspathJavaLoader;
      }
  
      private imp() {
          ;
      }
  
      /**
       * If the given name is found in sys.modules, the entry from there is
       * returned. Otherwise a new PyModule is created for the name and added to
       * sys.modules
       */
      public static PyModule addModule(String name) {
          name = name.intern();
          PyObject modules = Py.getSystemState().modules;
          PyModule module = (PyModule) modules.__finditem__(name);
          if (module != null) {
              return module;
          }
          module = new PyModule(name, null);
          modules.__setitem__(name, module);
          return module;
      }
  
      private static byte[] readBytes(InputStream fp) {
          try {
              return FileUtil.readBytes(fp);
          } catch(IOException ioe) {
              throw Py.IOError(ioe);
          } finally {
              try {
                  fp.close();
              } catch(IOException e) {
                  throw Py.IOError(e);
              }
          }
      }
  
      private static InputStream makeStream(File file) {
          try {
              return new FileInputStream(file);
          } catch (IOException ioe) {
              throw Py.IOError(ioe);
          }
      }
  
      static PyObject createFromPyClass(String name, InputStream fp,
              boolean testing, String fileName) {
          byte[] data = readBytes(fp);
          int n = data.length;
  
          int api = (data[n - 4] << 24) + (data[n - 3] << 16)
                  + (data[n - 2] << 8) + data[n - 1];
          if (api != APIVersion) {
              if (testing) {
                  return null;
              } else {
                  throw Py.ImportError("invalid api version(" + api + " != "
                          + APIVersion + ") in: " + name);
              }
          }
          PyCode code;
          try {
              code = BytecodeLoader.makeCode(name + "$py", data, fileName);
          } catch (Throwable t) {
              if (testing) {
                  return null;
              } else {
                  throw Py.JavaError(t);
              }
          }
  
          Py.writeComment(IMPORT_LOG, "'" + name + "' as " + fileName);
  
          return createFromCode(name, code, fileName);
      }
  
      public static byte[] compileSource(String name, File file, String sourceFilename,
              String compiledFilename) {
          if (sourceFilename == null) {
              sourceFilename = file.toString();
          }
          return compileSource(name, makeStream(file), sourceFilename);
      }
  
      private static String makeCompiledFilename(String filename) {
          return filename.substring(0, filename.length() - 3)
                  + "$py.class";
      }
      
      /**
       * Stores the bytes in compiledSource in compiledFilename.
       * 
       * If compiledFilename is null it's set to the results of
       * makeCompiledFilename(sourcefileName)
       * 
       * If sourceFilename is null or set to UNKNOWN_SOURCEFILE null is returned
       * 
       * @return the compiledFilename eventually used or null if a
       *         compiledFilename couldn't be determined of if an error was thrown
       *         while writing to the cache file.
       */
      public static String cacheCompiledSource(String sourceFilename,
                                                String compiledFilename,
                                                byte[] compiledSource) {
          if(compiledFilename == null){
              if(sourceFilename == null || sourceFilename.equals(UNKNOWN_SOURCEFILE)){
                 return null; 
              }
              compiledFilename = makeCompiledFilename(sourceFilename);
          }
          FileOutputStream fop = null;
          try {
              fop = new FileOutputStream(compiledFilename);
              fop.write(compiledSource);
              fop.close();
              return compiledFilename;
          } catch(IOException exc) {
              // If we can't write the cache file, just log and continue
              Py.writeDebug(IMPORT_LOG, "Unable to write to source cache file '"
                      + compiledFilename + "' due to " + exc);
              return null;
          } finally {
              if(fop != null) {
                  try {
                      fop.close();
                  } catch(IOException e) {
                      Py.writeDebug(IMPORT_LOG,
                                    "Unable to close source cache file '"
                                            + compiledFilename + "' due to " + e);
                  }
              }
          }
      }
  
      static byte[] compileSource(String name,
                                  InputStream fp,
                                  String filename) {
          ByteArrayOutputStream ofp = new ByteArrayOutputStream();
          try {
              if(filename == null) {
                  filename = UNKNOWN_SOURCEFILE;
              }
              org.python.parser.ast.modType node;
              try {
                  node = parser.parse(fp, "exec", filename, Py.getCompilerFlags());
              } finally {
                  fp.close();
              }
              org.python.compiler.Module.compile(node,
                                                 ofp,
                                                 name + "$py",
                                                 filename,
                                                 true,
                                                 false,
                                                 true,
                                                 Py.getCompilerFlags());
              return ofp.toByteArray();
          } catch(Throwable t) {
              throw parser.fixParseError(null, t, filename);
          }
      }
  
      public static PyObject createFromSource(String name, InputStream fp,
              String filename) {
          return createFromSource(name, fp, filename, null);
      }
      
      static PyObject createFromSource(String name, InputStream fp,
              String filename, String outFilename) {
          byte[] bytes = compileSource(name, fp, filename);
          outFilename = cacheCompiledSource(filename, outFilename, bytes);
  
          Py.writeComment(IMPORT_LOG, "'" + name + "' as " + filename);
  
          PyCode code = BytecodeLoader.makeCode(name + "$py", bytes, filename);
          return createFromCode(name, code, filename);
      }
      
      /**
       * Returns a module with the given name whose contents are the results of
       * running c. __file__ is set to whatever is in c.
       */
      static PyObject createFromCode(String name, PyCode c){
          return createFromCode(name, c, null);
      }
      
      /*
       * Returns a module with the given name whose contents are the results of
       * running c. Sets __file__ on the module to be moduleLocation unless
       * moduleLocation is null. If c comes from a local .py file or compiled
       * $py.class class moduleLocation should be the result of running new
       * File(moduleLocation).getAbsoultePath(). If c comes from a remote file or
       * is a jar moduleLocation should be the full uri for c.
       */
      static PyObject createFromCode(String name, PyCode c, String moduleLocation) {
          PyModule module = addModule(name);
  
          PyTableCode code = null;
          if (c instanceof PyTableCode) {
              code = (PyTableCode) c;
          }
          try {
              PyFrame f = new PyFrame(code, module.__dict__, module.__dict__, null);
              code.call(f);
          } catch (RuntimeException t) {
              Py.getSystemState().modules.__delitem__(name.intern());
              throw t;
          }
          if(moduleLocation != null) {
              module.__setattr__("__file__",
                                 new PyString(moduleLocation));
          }else{
              Py.writeDebug(IMPORT_LOG, "No fileName known to set __file__ for " + name + ".");
          }
          return module;
      }
  
      static PyObject createFromClass(String name, Class c) {
          // Two choices. c implements PyRunnable or c is Java package
          if(PyRunnable.class.isAssignableFrom(c)) {
              try {
                  return createFromCode(name,
                                        ((PyRunnable)c.newInstance()).getMain());
              } catch(InstantiationException e) {
                  throw Py.JavaError(e);
              } catch(IllegalAccessException e) {
                  throw Py.JavaError(e);
              }
          }
          return PyJavaClass.lookup(c); // xxx?
      }
  
      static PyObject getPathImporter(PyObject cache, PyList hooks, PyObject p) {
  
          // attempt to get an importer for the path
          // use null as default value since Py.None is
          // a valid value in the cache for the default
          // importer
          PyObject importer = cache.__finditem__(p);
          if (importer != null) {
              return importer;
          }
  
          // nothing in the cache, so check all hooks
          PyObject iter = hooks.__iter__();
          for (PyObject hook; (hook = iter.__iternext__()) != null;) {
              try {
                  importer = hook.__call__(p);
                  break;
              } catch (PyException e) {
                  if (!Py.matchException(e, Py.ImportError)) {
                      throw e;
                  }
              }
          }
  
          importer = (importer == null ? Py.None : importer);
          cache.__setitem__(p, importer);
  
          return importer;
      }
  
      static PyObject replacePathItem(PyObject path) {
          if (path instanceof SyspathArchive) {
              // already an archive
              return null;
          }
  
          try {
              // this has the side affect of adding the jar to the PackageManager
              // during the initialization of the SyspathArchive
              return new SyspathArchive(path.toString());
          } catch (Exception e) {
              return null;
          }
      }
  
      static PyObject find_module(String name, String moduleName, PyList path) {
  
          PyObject loader = Py.None;
          PySystemState sys = Py.getSystemState();
          PyObject metaPath = sys.meta_path;
  
          /*
           * Needed to convert all entries on the path to SyspathArchives if
           * necessary.
           */
          PyList ppath = path == null ? sys.path : path;
          for (int i = 0; i < ppath.__len__(); i++) {
              PyObject p = ppath.__getitem__(i);
              PyObject q = replacePathItem(p);
              if (q == null) {
                  continue;
              }
              ppath.__setitem__(i, q);
          }
  
          PyObject iter = metaPath.__iter__();
          for (PyObject importer; (importer = iter.__iternext__()) != null;) {
              PyObject findModule = importer.__getattr__("find_module");
              loader = findModule.__call__(new PyObject[] {
                      new PyString(moduleName), path == null ? Py.None : path });
              if (loader != Py.None) {
                  return loadFromLoader(loader, moduleName);
              }
          }
  
          PyObject ret = loadBuiltin(moduleName);
          if (ret != null) {
              return ret;
          }
  
          path = path == null ? sys.path : path;
          for (int i = 0; i < path.__len__(); i++) {
              PyObject p = path.__getitem__(i);
              // System.err.println("find_module (" + name + ", " + moduleName +
              // ") Path: " + path);
              PyObject importer = getPathImporter(sys.path_importer_cache,
                      sys.path_hooks, p);
              if (importer != Py.None) {
                  PyObject findModule = importer.__getattr__("find_module");
                  loader = findModule.__call__(new PyObject[] { new PyString(
                          moduleName) });
                  if (loader != Py.None) {
                      return loadFromLoader(loader, moduleName);
                  }
              }
              ret = loadFromSource(name, moduleName, p);
  