165 that are convenient for coverage.py. |
165 that are convenient for coverage.py. |
166 |
166 |
167 To record data for contexts, use :meth:`set_context` to set a context to |
167 To record data for contexts, use :meth:`set_context` to set a context to |
168 be used for subsequent :meth:`add_lines` and :meth:`add_arcs` calls. |
168 be used for subsequent :meth:`add_lines` and :meth:`add_arcs` calls. |
169 |
169 |
170 To add a source file without any measured data, use :meth:`touch_file`. |
170 To add a source file without any measured data, use :meth:`touch_file`, |
|
171 or :meth:`touch_files` for a list of such files. |
171 |
172 |
172 Write the data to its file with :meth:`write`. |
173 Write the data to its file with :meth:`write`. |
173 |
174 |
174 You can clear the data in memory with :meth:`erase`. Two data collections |
175 You can clear the data in memory with :meth:`erase`. Two data collections |
175 can be combined by using :meth:`update` on one :class:`CoverageData`, |
176 can be combined by using :meth:`update` on one :class:`CoverageData`, |
534 """Ensure that `filename` appears in the data, empty if needed. |
535 """Ensure that `filename` appears in the data, empty if needed. |
535 |
536 |
536 `plugin_name` is the name of the plugin responsible for this file. It is used |
537 `plugin_name` is the name of the plugin responsible for this file. It is used |
537 to associate the right filereporter, etc. |
538 to associate the right filereporter, etc. |
538 """ |
539 """ |
|
540 self.touch_files([filename], plugin_name) |
|
541 |
|
542 def touch_files(self, filenames, plugin_name=""): |
|
543 """Ensure that `filenames` appear in the data, empty if needed. |
|
544 |
|
545 `plugin_name` is the name of the plugin responsible for these files. It is used |
|
546 to associate the right filereporter, etc. |
|
547 """ |
539 if self._debug.should('dataop'): |
548 if self._debug.should('dataop'): |
540 self._debug.write("Touching %r" % (filename,)) |
549 self._debug.write("Touching %r" % (filenames,)) |
541 self._start_using() |
550 self._start_using() |
542 if not self._has_arcs and not self._has_lines: |
551 with self._connect(): # Use this to get one transaction. |
543 raise CoverageException("Can't touch files in an empty CoverageData") |
552 if not self._has_arcs and not self._has_lines: |
544 |
553 raise CoverageException("Can't touch files in an empty CoverageData") |
545 self._file_id(filename, add=True) |
554 |
546 if plugin_name: |
555 for filename in filenames: |
547 # Set the tracer for this file |
556 self._file_id(filename, add=True) |
548 self.add_file_tracers({filename: plugin_name}) |
557 if plugin_name: |
|
558 # Set the tracer for this file |
|
559 self.add_file_tracers({filename: plugin_name}) |
549 |
560 |
550 def update(self, other_data, aliases=None): |
561 def update(self, other_data, aliases=None): |
551 """Update this data with data from several other :class:`CoverageData` instances. |
562 """Update this data with data from several other :class:`CoverageData` instances. |
552 |
563 |
553 If `aliases` is provided, it's a `PathAliases` object that is used to |
564 If `aliases` is provided, it's a `PathAliases` object that is used to |
1043 """Same as :meth:`python:sqlite3.Connection.execute`.""" |
1054 """Same as :meth:`python:sqlite3.Connection.execute`.""" |
1044 if self.debug: |
1055 if self.debug: |
1045 tail = " with {!r}".format(parameters) if parameters else "" |
1056 tail = " with {!r}".format(parameters) if parameters else "" |
1046 self.debug.write("Executing {!r}{}".format(sql, tail)) |
1057 self.debug.write("Executing {!r}{}".format(sql, tail)) |
1047 try: |
1058 try: |
1048 return self.con.execute(sql, parameters) |
1059 try: |
|
1060 return self.con.execute(sql, parameters) |
|
1061 except Exception: |
|
1062 # In some cases, an error might happen that isn't really an |
|
1063 # error. Try again immediately. |
|
1064 # https://github.com/nedbat/coveragepy/issues/1010 |
|
1065 return self.con.execute(sql, parameters) |
1049 except sqlite3.Error as exc: |
1066 except sqlite3.Error as exc: |
1050 msg = str(exc) |
1067 msg = str(exc) |
1051 try: |
1068 try: |
1052 # `execute` is the first thing we do with the database, so try |
1069 # `execute` is the first thing we do with the database, so try |
1053 # hard to provide useful hints if something goes wrong now. |
1070 # hard to provide useful hints if something goes wrong now. |