예제 #1
0
파일: pyc.py 프로젝트: gvanrossum/pytype
def compile_src_string_to_pyc_string(src, filename, python_version, python_exe,
                                     mode="exec"):
  """Compile Python source code to pyc data.

  This may use py_compile if the src is for the same version as we're running,
  or else it spawns an external process to produce a .pyc file. The generated
  bytecode (.pyc file) is read and both it and any temporary files are deleted.

  Args:
    src: Python sourcecode
    filename: Name of the source file. For error messages.
    python_version: Python version, (major, minor). E.g. (2, 7). Will be used
      to determine the Python executable to call.
    python_exe: Path to a Python interpreter, or "HOST", or None. If this is
      None, the system "pythonX.X" interpreter will be used.
    mode: Same as __builtin__.compile: "exec" if source consists of a
      sequence of statements, "eval" if it consists of a single expression,
      or "single" if it consists of a single interactive statement.

  Returns:
    The compiled pyc file as a binary string.
  Raises:
    CompileError: If we find a syntax error in the file.
    IOError: If our compile script failed.
  """
  fi = tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=False)

  try:
    fi.write(src)
    fi.close()
    if python_exe == "HOST":
      # We were asked to use the version of Python we're running to compile.
      output = StringIO.StringIO()
      compile_bytecode.compile_to_pyc(fi.name, filename or fi.name,
                                      output, mode)
      bytecode = output.getvalue()
    else:
      # In order to be able to compile pyc files for both Python 2 and Python 3,
      # we spawn an external process.
      if python_exe:
        # Allow python_exe to contain parameters (E.g. "-T")
        exe = python_exe.split() + ["-S"]
      else:
        exe = ["python" + ".".join(map(str, python_version))]
      cmd = exe + ["-", fi.name, filename or fi.name, mode]

      src = utils.load_pytype_file(COMPILE_SCRIPT)

      p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
      bytecode, _ = p.communicate(src)
      assert p.poll() == 0, "Child process failed"
  finally:
    os.unlink(fi.name)
  if bytecode[0] == chr(0):  # compile OK
    return bytecode[1:]
  elif bytecode[0] == chr(1):  # compile error
    raise CompileError(bytecode[1:])
  else:
    raise IOError("_compile.py produced invalid result")
예제 #2
0
파일: typeshed.py 프로젝트: 0dysseas/pytype
 def _load_file(self, path):
     if self._env_home:
         filename = os.path.join(self._env_home, path)
         with open(filename, "r") as f:
             return filename, f.read()
     else:
         data = utils.load_pytype_file(os.path.join(self._root, path))
         return os.path.join(self._root, path), data
예제 #3
0
 def _load_file(self, path):
   if self._env_home:
     filename = os.path.join(self._env_home, path)
     with open(filename, "rb") as f:
       return filename, f.read()
   else:
     # Use typeshed bundled with pytype
     data = utils.load_pytype_file(os.path.join("typeshed", path))
     return os.path.join(self._typeshed_path, path), data
예제 #4
0
파일: typeshed.py 프로젝트: songzcn/pytype
 def read_blacklist(self):
   """Read the typeshed blacklist."""
   if self._env_home:
     raise NotImplementedError("Can't read blacklist outside ./typeshed")
   data = utils.load_pytype_file("typeshed/tests/pytype_blacklist.txt")
   for line in data.splitlines():
     line = line[:line.find("#")].strip()
     if line:
       yield line
예제 #5
0
 def _load_file(self, path):
     if self._env_home:
         filename = os.path.join(self._env_home, path)
         with open(filename, "rb") as f:
             return filename, f.read()
     else:
         # Use typeshed bundled with pytype. Note we don't use self._typeshed_path
         # to load the file, since load_pytype_file has its own path logic.
         data = utils.load_pytype_file(os.path.join("typeshed", path))
         return os.path.join(self._typeshed_path, path), data
예제 #6
0
def GetPredefinedFile(pytd_subdir, module, extension=".pytd"):
    """Get the contents of a predefined PyTD, typically with a file name *.pytd.

  Arguments:
    pytd_subdir: the directory, typically "builtins" or "stdlib"
    module: module name (e.g., "sys" or "__builtins__")
    extension: either ".pytd" or ".py"
  Returns:
    The contents of the file
  Raises:
    IOError: if file not found
  """
    path = os.path.join("pytd", pytd_subdir,
                        os.path.join(*module.split(".")) + extension)
    return utils.load_pytype_file(path)
예제 #7
0
def GetPredefinedFile(pytd_subdir, module, extension=".pytd",
                      as_package=False):
  """Get the contents of a predefined PyTD, typically with a file name *.pytd.

  Arguments:
    pytd_subdir: the directory, typically "builtins" or "stdlib"
    module: module name (e.g., "sys" or "__builtins__")
    extension: either ".pytd" or ".py"
    as_package: try the module as a directory with an __init__ file
  Returns:
    The contents of the file
  Raises:
    IOError: if file not found
  """
  parts = module.split(".")
  if as_package:
    parts.append("__init__")
  mod_path = os.path.join(*parts) + extension
  path = os.path.join("pytd", pytd_subdir, mod_path)
  return path, utils.load_pytype_file(path)