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)
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'.')
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)
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
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
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
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()))
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()))
def test_make_file_non_ascii(self): self.make_file("unicode.txt", "tabblo: «ταБЬℓσ»") self.assertEqual( open("unicode.txt", "rb").read(), to_bytes("tabblo: «ταБЬℓσ»") )
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: «ταБЬℓσ»"))
def dumps(self): with self._connect() as con: return b'z' + zlib.compress(to_bytes(con.dump()))