Ejemplo n.º 1
0
 def update(self, v):
     """Add `v` to the hash, recursively if needed."""
     self.md5.update(to_bytes(str(type(v))))
     if isinstance(v, string_class):
         self.md5.update(to_bytes(v))
     elif isinstance(v, bytes):
         self.md5.update(v)
     elif v is None:
         pass
     elif isinstance(v, (int, float)):
         self.md5.update(to_bytes(str(v)))
     elif isinstance(v, (tuple, list)):
         for e in v:
             self.update(e)
     elif isinstance(v, dict):
         keys = v.keys()
         for k in sorted(keys):
             self.update(k)
             self.update(v[k])
     else:
         for k in dir(v):
             if k.startswith('__'):
                 continue
             a = getattr(v, k)
             if inspect.isroutine(a):
                 continue
             self.update(k)
             self.update(a)
Ejemplo n.º 2
0
 def update(self, v):
     """Add `v` to the hash, recursively if needed."""
     self.md5.update(to_bytes(str(type(v))))
     if isinstance(v, unicode_class):
         self.md5.update(v.encode('utf8'))
     elif isinstance(v, bytes):
         self.md5.update(v)
     elif v is None:
         pass
     elif isinstance(v, (int, float)):
         self.md5.update(to_bytes(str(v)))
     elif isinstance(v, (tuple, list)):
         for e in v:
             self.update(e)
     elif isinstance(v, dict):
         keys = v.keys()
         for k in sorted(keys):
             self.update(k)
             self.update(v[k])
     else:
         for k in dir(v):
             if k.startswith('__'):
                 continue
             a = getattr(v, k)
             if inspect.isroutine(a):
                 continue
             self.update(k)
             self.update(a)
     self.md5.update(b'.')
Ejemplo n.º 3
0
    def update(self, v):
        self.md5.update(to_bytes(str(type(v))))
        if isinstance(v, string_class):
            self.md5.update(to_bytes(v))
        elif isinstance(v, (int, float)):
            self.update(str(v))
        elif isinstance(v, (tuple, list)):
            for e in v:
                self.update(e)

        elif isinstance(v, dict):
            keys = v.keys()
            for k in sorted(keys):
                self.update(k)
                self.update(v[k])

        else:
            for k in dir(v):
                if k.startswith('__'):
                    continue
                a = getattr(v, k)
                if inspect.isroutine(a):
                    continue
                self.update(k)
                self.update(a)
Ejemplo n.º 4
0
    def make_file(self, filename, text="", newline=None):
        """Create a file for testing.

        `filename` is the relative path to the file, including directories if
        desired, which will be created if need be.

        `text` is the content to create in the file, a native string (bytes in
        Python 2, unicode in Python 3).

        If `newline` is provided, it is a string that will be used as the line
        endings in the created file, otherwise the line endings are as provided
        in `text`.

        Returns `filename`.

        """
        # Tests that call `make_file` should be run in a temp environment.
        assert self.run_in_temp_dir
        self.class_behavior().test_method_made_any_files = True

        text = textwrap.dedent(text)
        if newline:
            text = text.replace("\n", newline)

        # Make sure the directories are available.
        dirs, _ = os.path.split(filename)
        if dirs and not os.path.exists(dirs):
            os.makedirs(dirs)

        # Create the file.
        with open(filename, 'wb') as f:
            f.write(to_bytes(text))

        return filename
Ejemplo n.º 5
0
    def make_file(self, filename, text="", newline=None):
        """Create a temp file.

        `filename` is the path to the file, including directories if desired,
        and `text` is the content. If `newline` is provided, it is a string
        that will be used as the line endings in the created file.

        Returns the path to the file.

        """
        # Tests that call `make_file` should be run in a temp environment.
        assert self.run_in_temp_dir
        text = textwrap.dedent(text)
        if newline:
            text = text.replace("\n", newline)

        # Make sure the directories are available.
        dirs, _ = os.path.split(filename)
        if dirs and not os.path.exists(dirs):
            os.makedirs(dirs)

        # Create the file.
        f = open(filename, 'wb')
        try:
            f.write(to_bytes(text))
        finally:
            f.close()

        return filename
Ejemplo n.º 6
0
    def make_file(self, filename, text="", newline=None):
        """Create a temp file.

        `filename` is the path to the file, including directories if desired,
        and `text` is the content. If `newline` is provided, it is a string
        that will be used as the line endings in the created file.

        Returns the path to the file.

        """
        # Tests that call `make_file` should be run in a temp environment.
        assert self.run_in_temp_dir
        self.class_behavior().test_method_made_any_files = True

        text = textwrap.dedent(text)
        if newline:
            text = text.replace("\n", newline)

        # Make sure the directories are available.
        dirs, _ = os.path.split(filename)
        if dirs and not os.path.exists(dirs):
            os.makedirs(dirs)

        # Create the file.
        f = open(filename, 'wb')
        try:
            f.write(to_bytes(text))
        finally:
            f.close()

        return filename
Ejemplo n.º 7
0
    def make_file(self, filename, text="", newline=None):
        """Create a file for testing.

        `filename` is the relative path to the file, including directories if
        desired, which will be created if need be.

        `text` is the content to create in the file, a native string (bytes in
        Python 2, unicode in Python 3).

        If `newline` is provided, it is a string that will be used as the line
        endings in the created file, otherwise the line endings are as provided
        in `text`.

        Returns `filename`.

        """
        # Tests that call `make_file` should be run in a temp environment.
        assert self.run_in_temp_dir
        self.class_behavior().test_method_made_any_files = True

        text = textwrap.dedent(text)
        if newline:
            text = text.replace("\n", newline)

        # Make sure the directories are available.
        dirs, _ = os.path.split(filename)
        if dirs and not os.path.exists(dirs):
            os.makedirs(dirs)

        # Create the file.
        with open(filename, 'wb') as f:
            f.write(to_bytes(text))

        return filename
Ejemplo n.º 8
0
    def dumps(self):
        """Serialize the current data to a byte string.

        The format of the serialized data is not documented. It is only
        suitable for use with :meth:`loads` in the same version of
        coverage.py.

        Returns:
            A byte string of serialized data.

        .. versionadded:: 5.0

        """
        if self._debug.should('dataio'):
            self._debug.write("Dumping data from data file {!r}".format(self._filename))
        with self._connect() as con:
            return b'z' + zlib.compress(to_bytes(con.dump()))
Ejemplo n.º 9
0
    def dumps(self):
        """Serialize the current data to a byte string.

        The format of the serialized data is not documented. It is only
        suitable for use with :meth:`loads` in the same version of
        coverage.py.

        Note that this serialization is not what gets stored in coverage data
        files.  This method is meant to produce bytes that can be transmitted
        elsewhere and then deserialized with :meth:`loads`.

        Returns:
            A byte string of serialized data.

        .. versionadded:: 5.0

        """
        if self._debug.should('dataio'):
            self._debug.write("Dumping data from data file {!r}".format(self._filename))
        with self._connect() as con:
            return b'z' + zlib.compress(to_bytes(con.dump()))
Ejemplo n.º 10
0
 def test_make_file_non_ascii(self):
     self.make_file("unicode.txt", "tabblo: «ταБЬℓσ»")
     self.assertEqual(
         open("unicode.txt", "rb").read(),
         to_bytes("tabblo: «ταБЬℓσ»")
         )
Ejemplo n.º 11
0
 def test_make_file_non_ascii(self):
     self.make_file("unicode.txt", "tabblo: «ταБЬℓσ»")
     with open("unicode.txt", "rb") as f:
         text = f.read()
     self.assertEqual(text, to_bytes("tabblo: «ταБЬℓσ»"))
Ejemplo n.º 12
0
 def dumps(self):
     with self._connect() as con:
         return b'z' + zlib.compress(to_bytes(con.dump()))
Ejemplo n.º 13
0
 def test_make_file_non_ascii(self):
     self.make_file("unicode.txt", "tabblo: «ταБЬℓσ»")
     with open("unicode.txt", "rb") as f:
         text = f.read()
     self.assertEqual(text, to_bytes("tabblo: «ταБЬℓσ»"))