Ejemplo n.º 1
0
 def testReadImportsInfo(self):
     """Test reading an imports_info file into ImportsInfo."""
     with tempfile.NamedTemporaryFile() as fi:
         fi.write(
             compat.bytestring(
                 textwrap.dedent("""
     a/b/__init__.py prefix/1/a/b/__init__.py~
     a/b/b.py prefix/1/a/b/b.py~suffix
     a/b/c.pyi prefix/1/a/b/c.pyi~
     a/b/d.py prefix/1/a/b/d.py~
     a/b/e.py 2/a/b/e1.py~
     a/b/e 2/a/b/e2.py~
     a/b/e 2/a/b/foo/#2.py~
   """)))
         fi.seek(0)  # ready for reading
         six.assertCountEqual(
             self,
             imports_map_loader._read_imports_map(fi.name).items(), [
                 ("a/b/__init__", ["prefix/1/a/b/__init__.py~"]),
                 ("a/b/b", ["prefix/1/a/b/b.py~suffix"]),
                 ("a/b/c", ["prefix/1/a/b/c.pyi~"]),
                 ("a/b/d", ["prefix/1/a/b/d.py~"]),
                 ("a/b/e",
                  ["2/a/b/foo/#2.py~", "2/a/b/e1.py~", "2/a/b/e2.py~"]),
             ])
Ejemplo n.º 2
0
 def _init_byte_offsets(self):
   offset = 0
   for line in self._lines:
     self._offsets.append(offset)
     # convert line to bytes
     bytes_ = compat.bytestring(line)
     offset += len(bytes_) + 1  # account for the \n
Ejemplo n.º 3
0
 def _get_literal_value(self, pyval):
     if pyval == self.vm.lookup_builtin("__builtin__.True"):
         return True
     elif pyval == self.vm.lookup_builtin("__builtin__.False"):
         return False
     elif isinstance(pyval, str):
         prefix, value = parser_constants.STRING_RE.match(
             pyval).groups()[:2]
         value = value[1:-1]  # remove quotation marks
         if "b" in prefix and not self.vm.PY2:
             value = compat.bytestring(value)
         elif "u" in prefix and self.vm.PY2:
             value = compat.UnicodeType(value)
         return value
     else:
         return pyval
Ejemplo n.º 4
0
def _hash_dict(vardict, names):
    """Hash a dictionary.

  This contains the keys and the full hashes of the data in the values.

  Arguments:
    vardict: A dictionary mapping str to Variable.
    names: If this is non-None, the snapshot will include only those
      dictionary entries whose keys appear in names.

  Returns:
    A hash of the dictionary.
  """
    if names is not None:
        vardict = {name: vardict[name] for name in names.intersection(vardict)}
    m = hashlib.md5()
    for name, var in sorted(vardict.items()):
        m.update(compat.bytestring(name))
        for value in var.bindings:
            m.update(value.data.get_fullhash())
    return m.digest()