Example #1
0
    def _recorded_call(self, fun_id, function, args, kwargs, reentrant=True):
        # Starts the fiber section
        section = fiber.WovenSection()
        section.enter()
        fibdesc = section.descriptor

        # Check if we are in replay mode
        mode = section.state.get(RECMODE_TAG, None)
        if mode == JournalMode.replay:
            fun_id, function = self._resolve_function(fun_id, function)
            return self._call_fun(fun_id, function, args, kwargs)

        # Check if this is the first recording in the fiber section
        recording = section.state.get(RECORDING_TAG, None)
        section_first = recording is None
        result = None

        try:

            entry = section.state.get(JOURNAL_ENTRY_TAG, None)
            mode = section.state.get(RECMODE_TAG, None)
            fiber_first = section_first and section.descriptor.fiber_depth == 0
            fun_id, function = self._resolve_function(fun_id, function)

            if section_first:
                entry = self.journal_keeper.new_entry(self.journal_id, fun_id,
                                                      *args, **kwargs)
                entry.set_fiber_context(fibdesc.fiber_id, fibdesc.fiber_depth)

                section.state[RECORDING_TAG] = True
                section.state[RECMODE_TAG] = JournalMode.recording
                section.state[JOURNAL_ENTRY_TAG] = entry

            if not (fiber_first or reentrant):
                # If not reentrant and it is not the first, it's BAAAAAD.
                raise ReentrantCallError("Recorded functions %s cannot be "
                                         "called from inside the recording "
                                         "section" % (fun_id, ))

            result = self._call_fun(fun_id, function, args, kwargs)

        except Exception as e:

            if not section_first:
                raise

            result = fiber.fail(e)
            error.handle_exception(self, e, "Exception inside recorded "
                                   "function %s", fun_id)

        finally:

            if section_first:
                entry.set_result(result)
                entry.commit()
                result = entry.get_result()
                section.state[RECORDING_TAG] = None
                section.state[JOURNAL_ENTRY_TAG] = None
                section.state[RECMODE_TAG] = None

        return section.exit(result)
Example #2
0
 def _replayed_call(self, fun_id, function, args, kwargs):
     try:
         return self._call_fun(fun_id, function, args, kwargs)
     except Exception as e:
         return fiber.fail(e)