def compile_src(src, python_version, python_exe, filename=None, mode="exec"): """Compile a string to pyc, and then load and parse the pyc. Args: src: Python source code. python_version: Python version, (major, minor). python_exe: Path to Python interpreter, or None. filename: The filename the sourcecode is from. mode: "exec", "eval" or "single". Returns: An instance of loadmarshal.CodeType. Raises: UsageError: If python_exe and python_version are mismatched. """ pyc_data = compile_src_string_to_pyc_string(src, filename, python_version, python_exe, mode) code = parse_pyc_string(pyc_data) if code.python_version != python_version: raise utils.UsageError( "python_exe version %s does not match python version %s" % (utils.format_version( code.python_version), utils.format_version(python_version))) visit(code, AdjustFilename(filename)) return code
def read_source_file(input_filename): try: if six.PY3: with open(input_filename, "r", encoding="utf8") as fi: return fi.read() else: with open(input_filename, "rb") as fi: return fi.read().decode("utf8") except IOError: raise utils.UsageError("Could not load input file %s" % input_filename)
def _get_typeshed(): """Get the global Typeshed instance.""" global _typeshed if _typeshed is None: try: _typeshed = Typeshed() except IOError as e: # This happens if typeshed is not available. Which is a setup error # and should be propagated to the user. The IOError is catched further up # in the stack. raise utils.UsageError("Couldn't initalize typeshed:\n %s" % str(e)) return _typeshed
def read_source_file(input_filename): try: with open(input_filename, "r") as fi: return fi.read() except IOError: raise utils.UsageError("Could not load input file %s" % input_filename)