Esempio n. 1
0
 def _warn(self, message, category=Warning, *args, **kwargs):
     if isinstance(message, Warning):
         self.captured_warnings.append(
             warnings.WarningMessage(
                 message=category(message), category=category or Warning,
                 filename="", lineno=0))
     else:
         self.captured_warnings.append(
             warnings.WarningMessage(
                 message=category(message), category=category,
                 filename="", lineno=0))
Esempio n. 2
0
 def showwarning(message,
                 category,
                 filename,
                 lineno,
                 file=None,
                 line=None):
     if issubclass(category, self._category):
         if log is not None:
             log.append(
                 warnings.WarningMessage(message, category, filename,
                                         lineno, file, line))
         if self._handle is not None:
             self._handle(message, category, filename, lineno, file,
                          line)
     else:
         current_filters = warnings.filters
         warnings.filters = self._orig_filters + current_filters
         current_showwarning = warnings.showwarning
         warnings.showwarning = self._orig_showwarning
         # This is not perfect as some arguments are missing
         # Might cause some issues with the warning repetition logic,
         # but should be acceptable in most cases
         warnings.warn_explicit(message, category, filename, lineno)
         warnings.filters = current_filters
         warnings.showwarning = current_showwarning
Esempio n. 3
0
def unserialize_warning_message(data):
    import warnings
    import importlib

    if data["message_module"]:
        mod = importlib.import_module(data["message_module"])
        cls = getattr(mod, data["message_class_name"])
        try:
            message = cls(*data["message_args"])
        except TypeError:
            message_text = "{mod}.{cls}: {msg}".format(
                mod=data["message_module"],
                cls=data["message_class_name"],
                msg=data["message_str"],
            )
            message = Warning(message_text)
    else:
        message = data["message_str"]

    if data["category_module"]:
        mod = importlib.import_module(data["category_module"])
        category = getattr(mod, data["category_class_name"])
    else:
        category = None

    kwargs = {"message": message, "category": category}
    # access private _WARNING_DETAILS because the attributes vary between Python versions
    for attr_name in warnings.WarningMessage._WARNING_DETAILS:
        if attr_name in ("message", "category"):
            continue
        kwargs[attr_name] = data[attr_name]

    return warnings.WarningMessage(**kwargs)
Esempio n. 4
0
    def __parse_section(self,
                        section_text,
                        method,
                        filename=None,
                        section_start=0):
        nl = re.compile("\n\r|\r\n|\n")
        lines = nl.split(section_text)
        comment = False
        for i, line in enumerate(lines):
            line, multiline_comment = self.strip_comments(line, comment)
            # TODO: Return None to have errors with line information anntached
            if not len(line):
                continue

            saved_warnings = []
            with warnings.catch_warnings(record=True) as ws:
                warnings.simplefilter("always")
                result = method(line)

                for w in ws:
                    w_outer = warnings.WarningMessage(
                        message=w.message,
                        category=BiooptParseWarning,
                        filename=filename,
                        lineno=section_start + i + 1,
                        line=line)
                    saved_warnings.append(w_outer)

            for w in saved_warnings:
                warnings.warn_explicit(message=w.message,
                                       category=w.category,
                                       filename=w.filename,
                                       lineno=w.lineno)

            yield result, i
Esempio n. 5
0
        def detailed_show_warning(*args, **kwargs):
            """warnings.showwarning replacement handler."""
            entry = warnings.WarningMessage(*args, **kwargs)

            skip_lines = 0
            entry_line_found = False

            for (_, filename, fileno, _, line, _) in inspect.stack():
                if any(start <= fileno <= end
                       for (_, skip_filename, start, end) in self.skip_list
                       if skip_filename == filename):
                    if entry_line_found:
                        continue
                    else:
                        skip_lines += 1

                if (filename, fileno) == (entry.filename, entry.lineno):
                    if not skip_lines:
                        break
                    entry_line_found = True

                if entry_line_found:
                    if not skip_lines:
                        (entry.filename, entry.lineno) = (filename, fileno)
                        break
                    else:
                        skip_lines -= 1

            log.append(entry)
    def _add_l1_l2_regularization(self, core_network):
        if self.l1_reg > 0 or self.l2_reg > 0:

            # weight norm should not be combined with l1 / l2 regularization
            if self.weight_normalization is True:
                warnings.WarningMessage(
                    "l1 / l2 regularization has no effect when weigh normalization is used"
                )

            weight_vector = tf.concat([
                tf.reshape(param, (-1, ))
                for param in core_network.get_params_internal()
                if '/W' in param.name
            ],
                                      axis=0)
            if self.l2_reg > 0:
                self.l2_reg_loss = self.l2_reg * tf.reduce_sum(weight_vector**
                                                               2)
                tf.losses.add_loss(self.l2_reg_loss,
                                   tf.GraphKeys.REGULARIZATION_LOSSES)
            if self.l1_reg > 0:
                self.l1_reg_loss = self.l1_reg * tf.reduce_sum(
                    tf.abs(weight_vector))
                tf.losses.add_loss(self.l1_reg_loss,
                                   tf.GraphKeys.REGULARIZATION_LOSSES)
Esempio n. 7
0
def _formatwarning(message, category, *args, **kwargs):
    if category == CollectionWarning:
        msg = f'{category.__name__}: {message}\n'  # Much cleaner
    else:
        import warnings
        msg_form = warnings.WarningMessage(message, category, *args, **kwargs)
        msg = warnings._formatwarnmsg_impl(msg_form)  # Default
    return msg
Esempio n. 8
0
def _formatwarning(message, category, *args, **kwargs):
    if hasattr(category, 'UAFGEOTOOLS'):
        msg = f'{category.__name__}: {message}\n'  # Much cleaner
    else:
        import warnings
        msg_form = warnings.WarningMessage(message, category, *args, **kwargs)
        msg = warnings._formatwarnmsg_impl(msg_form)  # Default
    return msg
Esempio n. 9
0
 def orig_showwarning(message,
                      category,
                      filename,
                      lineno,
                      file=None,
                      line=None):
     msg = warnings.WarningMessage(message, category, filename,
                                   lineno, file, line)
     warnings._showwarnmsg_impl(msg)
def make_warning_message(message,
                         category,
                         filename=None,
                         lineno=None,
                         line=None):
    return warnings.WarningMessage(message=message,
                                   category=category,
                                   filename=filename,
                                   lineno=lineno,
                                   line=line)
Esempio n. 11
0
def _showwarning_custom(message,
                        category,
                        filename,
                        lineno,
                        file=None,
                        line=None):
    """Use to replace warnings.showwarning.

    """
    msg = warnings.WarningMessage(message, category, filename, lineno, file,
                                  line)
    text = warnings._formatwarnmsg_impl(msg)
    _log_settings['default_logger'].warn(text)
Esempio n. 12
0
    def __init__(self,pos,tp=-2):
        super().__init__(pos,tp)
        self.pos = pos
        self.name = self.name+'_adhoc'+str(self.id)

        logger.debug("Adhoc agent initialized  {}".format(self))
        if ((pos[0]<0 or pos[0]>global_defs.GRID_SIZE-1) and (pos[1]<0 or pos[1]>global_defs.GRID_SIZE-1)):
            warnings.warn("Init positions out of bounds",UserWarning)
            logging.warning("Init positions out of bounds")
        self.is_adhoc=True
        self.tool = None #Start off with no tool.
        self.knowledge = Knowledge()
        warnings.WarningMessage("Make sure tracking agent is registered")
Esempio n. 13
0
 def showwarning(message,
                 category,
                 filename,
                 lineno,
                 file=None,
                 line=None):
     if issubclass(category, RuntimeWarning) \
        and str.endswith(str(message), ' was never awaited'):
         detected.append(
             warnings.WarningMessage(message, category, filename,
                                     lineno, file, line))
     else:
         showwarning_orig(message, category, filename, lineno, file,
                          line)
Esempio n. 14
0
def showwarning_with_traceback(message,
                               category,
                               filename,
                               lineno,
                               file=None,
                               line=None):
    msg = warnings.WarningMessage(message, category, filename, lineno, file,
                                  line)

    msg.formatted_traceback = traceback.format_stack()
    msg.traceback = traceback.extract_stack()

    if hasattr(warnings, "_showwarnmsg_impl"):
        warnings._showwarnmsg_impl(msg)
    else:  # python 2
        warnings._show_warning(message, category, filename, lineno, file, line)
Esempio n. 15
0
def formatwarning_with_traceback(message,
                                 category,
                                 filename,
                                 lineno,
                                 line=None):
    """Function to format a warning the standard way."""
    msg = warnings.WarningMessage(message, category, filename, lineno, None,
                                  line)

    msg.formatted_traceback = traceback.format_stack()
    msg.traceback = traceback.extract_stack()

    if hasattr(warnings, "_formatwarnmsg_impl"):
        return warnings._formatwarnmsg_impl(msg)
    else:
        return warnings.default_formatwarning(message, category, filename,
                                              lineno, line)
Esempio n. 16
0
        def detailed_show_warning(*args, **kwargs):
            """Replacement handler for warnings.showwarning."""
            warn_msg = warnings.WarningMessage(*args, **kwargs)

            skip_frames = 0
            a_frame_has_matched_warn_msg = False

            # The following for-loop will adjust the warn_msg only if the
            # warning does not match the skip_list.
            for (_, frame_filename, frame_lineno, _, _, _) in inspect.stack():
                if any(start <= frame_lineno <= end
                       for (_, skip_filename, start, end) in self.skip_list
                       if skip_filename == frame_filename):
                    # this frame matches to one of the items in the skip_list
                    if a_frame_has_matched_warn_msg:
                        continue
                    else:
                        skip_frames += 1

                if (
                    frame_filename == warn_msg.filename
                    and frame_lineno == warn_msg.lineno
                ):
                    if not skip_frames:
                        break
                    a_frame_has_matched_warn_msg = True

                if a_frame_has_matched_warn_msg:
                    if not skip_frames:
                        # adjust the warn_msg
                        warn_msg.filename = frame_filename
                        warn_msg.lineno = frame_lineno
                        break
                    else:
                        skip_frames -= 1

            # Ignore socket IO warnings (T183696, T184996)
            if (PYTHON_VERSION >= (3, 2)
                    and issubclass(warn_msg.category, ResourceWarning)
                    and any(str(warn_msg.message).startswith(msg) for msg in (
                        'unclosed <ssl.SSLSocket', 'unclosed <socket.socket'))
                    and warn_msg.filename.rpartition('/')[2] in (
                        'cookiejar.py', 'inspect.py', 'socket.py')):
                return

            log.append(warn_msg)
Esempio n. 17
0
def processInput(input: str):
    data = []
    signature = ""

    for id, line in enumerate(read_data(input)):
        line.replace('\n', ' ').replace('\r', '')
        if id % 2 == 0:
            signature = line
        elif id % 2 == 1:
            function_name = extractFunctionName(signature)
            body = line
            if body.strip() != "{}":
                methodDescription = DataElem(function_name, signature, body, extractFunctionId(signature), extractLabel(signature))
                data.append(methodDescription)
            else:
                warnings.WarningMessage("Found an empty body for method: " + function_name, category=ImportWarning, filename=DataElem, lineno=0)

    print("Processed " + str(len(data)) + ' methods.')
    return data
Esempio n. 18
0
        def detailed_show_warning(*args, **kwargs):
            """Replacement handler for warnings.showwarning."""
            warn_msg = warnings.WarningMessage(*args, **kwargs)

            skip_frames = 0
            a_frame_has_matched_warn_msg = False

            # The following for-loop will adjust the warn_msg only if the
            # warning does not match the skip_list.
            for (_, frame_filename, frame_lineno, _, _, _) in inspect.stack():
                if any(start <= frame_lineno <= end
                       for (_, skip_filename, start, end) in self.skip_list
                       if skip_filename == frame_filename):
                    # this frame matches to one of the items in the skip_list
                    if a_frame_has_matched_warn_msg:
                        continue
                    else:
                        skip_frames += 1

                if (frame_filename == warn_msg.filename
                        and frame_lineno == warn_msg.lineno):
                    if not skip_frames:
                        break
                    a_frame_has_matched_warn_msg = True

                if a_frame_has_matched_warn_msg:
                    if not skip_frames:
                        # adjust the warn_msg
                        warn_msg.filename = frame_filename
                        warn_msg.lineno = frame_lineno
                        break
                    else:
                        skip_frames -= 1

            # Avoid failures because cryptography is mentioning Python 2.6
            # is outdated
            if PYTHON_VERSION < (2, 7):
                if (isinstance(warn_msg, DeprecationWarning)
                        and str(warn_msg.message) == PYTHON_26_CRYPTO_WARN):
                    return

            log.append(warn_msg)
Esempio n. 19
0
        def detailed_show_warning(*args, **kwargs):
            """Replacement handler for warnings.showwarning."""
            warn_msg = warnings.WarningMessage(*args, **kwargs)

            skip_frames = 0
            a_frame_has_matched_warn_msg = False

            # The following for-loop will adjust the warn_msg only if the
            # warning does not match the skip_list.
            for _, frame_filename, frame_lineno, *_ in inspect.stack():
                if any(start <= frame_lineno <= end
                       for (_, skip_filename, start, end) in self.skip_list
                       if skip_filename == frame_filename):
                    # this frame matches to one of the items in the skip_list
                    if a_frame_has_matched_warn_msg:
                        continue

                    skip_frames += 1

                if frame_filename == warn_msg.filename \
                   and frame_lineno == warn_msg.lineno:
                    if not skip_frames:
                        break
                    a_frame_has_matched_warn_msg = True

                if a_frame_has_matched_warn_msg:
                    if not skip_frames:
                        # adjust the warn_msg
                        warn_msg.filename = frame_filename
                        warn_msg.lineno = frame_lineno
                        break

                    skip_frames -= 1

            # Ignore socket IO warnings (T183696, T184996)
            if issubclass(warn_msg.category, ResourceWarning) \
               and str(warn_msg.message).startswith(
                   ('unclosed <ssl.SSLSocket', 'unclosed <socket.socket')):
                return None

            log.append(warn_msg)
Esempio n. 20
0
def unserialize_warning_message(data):
    import warnings
    import importlib

    if data["message_module"]:
        mod = importlib.import_module(data["message_module"])
        cls = getattr(mod, data["message_class_name"])
        message = None
        if data["message_args"] is not None:
            try:
                message = cls(*data["message_args"])
            except TypeError:
                pass
        if message is None:
            # could not recreate the original warning instance;
            # create a generic Warning instance with the original
            # message at least
            message_text = "{mod}.{cls}: {msg}".format(
                mod=data["message_module"],
                cls=data["message_class_name"],
                msg=data["message_str"],
            )
            message = Warning(message_text)
    else:
        message = data["message_str"]

    if data["category_module"]:
        mod = importlib.import_module(data["category_module"])
        category = getattr(mod, data["category_class_name"])
    else:
        category = None

    kwargs = {"message": message, "category": category}
    # access private _WARNING_DETAILS because the attributes vary between Python versions
    for attr_name in warnings.WarningMessage._WARNING_DETAILS:  # type: ignore[attr-defined]
        if attr_name in ("message", "category"):
            continue
        kwargs[attr_name] = data[attr_name]

    return warnings.WarningMessage(**kwargs)  # type: ignore[arg-type]
Esempio n. 21
0
    def encode_decode(self, r0, itrs):

        # r0 images (B, C, h, w)
        if self.itrs < itrs:
            warnings.WarningMessage(
                "Specified Iterations > Training Iterations")

        # run in inference mode
        with torch.no_grad():

            # init variables
            r = r0
            dec = None
            h_e = h_d = None

            # extract original image dimensions
            img_size = r0.size()[2:]

            # covert images to patches
            r0 = f.sliding_window(input=r0,
                                  kernel_size=self.p_s,
                                  stride=self.p_s)

            for i in range(itrs):
                if i == 0:
                    # binary inpainting
                    enc, h_e = self.inpaint_encode(r, h_e)
                    dec, h_d = self.inpaint_decode(enc, h_d)
                else:
                    enc, h_e = self.res_encoder(r, h_e)
                    b = self.res_bin(enc)
                    dec, h_d = self.res_decoder(b, h_d)

                # calculate residual error
                r = r0 - dec

            # reshape patches to images
            dec = f.refactor_windows(windows=dec, output_size=img_size)

        return dec
        def detailed_show_warning(*args, **kwargs):
            """Replacement handler for warnings.showwarning."""
            entry = warnings.WarningMessage(*args, **kwargs)

            skip_lines = 0
            entry_line_found = False

            for (_, filename, fileno, _, line, _) in inspect.stack():
                if any(start <= fileno <= end
                       for (_, skip_filename, start, end) in self.skip_list
                       if skip_filename == filename):
                    if entry_line_found:
                        continue
                    else:
                        skip_lines += 1

                if (filename, fileno) == (entry.filename, entry.lineno):
                    if not skip_lines:
                        break
                    entry_line_found = True

                if entry_line_found:
                    if not skip_lines:
                        (entry.filename, entry.lineno) = (filename, fileno)
                        break
                    else:
                        skip_lines -= 1

            # Avoid failures because cryptography is mentioning Python 2.6
            # is outdated
            if PYTHON_VERSION < (2, 7):
                if (isinstance(entry, DeprecationWarning)
                        and str(entry.message) == PYTHON_26_CRYPTO_WARN):
                    return

            log.append(entry)
Esempio n. 23
0
 def showwarning(*args, **kwargs):
     self.log.append(warnings.WarningMessage(*args, **kwargs))
Esempio n. 24
0
 def _warn_explicit(self, message, category, *args, **kwargs):
     self.captured_warnings.append(
         warnings.WarningMessage(message=category(message),
                                 category=category,
                                 filename="",
                                 lineno=0))
Esempio n. 25
0
def _formatwarning(msg, category, filename, lineno, line=None):
    """Replace filename with __name__ to avoid printing the warn() call"""
    msg = warnings.WarningMessage(msg, category, __name__, lineno, None, None)
    return warnings._formatwarnmsg_impl(msg)
Esempio n. 26
0
            def showwarning(*args, **kwargs):
                if super_showwarning is not None:
                    super_showwarning(*args, **kwargs)

                self._warning_log.append(
                    warnings.WarningMessage(*args, **kwargs))
Esempio n. 27
0
    def showwarning(
        self,
        message: Warning,
        category: Type[Warning],
        filename: str,
        lineno: int,
        file: Optional[IO] = None,
        line: Optional[str] = None,
    ) -> None:
        """
        Hook to write a warning to a file using the built-in `warnings` lib.

        In [the documentation](https://docs.python.org/3/library/warnings.html)
        for the built-in `warnings` library, there are a few recommended ways of
        customizing the printing of warning messages.

        This method can override the `warnings.showwarning` function,
        which is called as part of the `warnings` library's workflow to print
        warning messages, e.g., when using `warnings.warn()`.
        Originally, it prints warning messages to `stderr`.
        This method will also print warning messages to `stderr` by calling
        `warnings._showwarning_orig()` or `warnings._showwarnmsg_impl()`.
        The first function will be called if the issued warning is not recognized
        as an [`OptimadeWarning`][optimade.server.warnings.OptimadeWarning].
        This is equivalent to "standard behaviour".
        The second function will be called _after_ an
        [`OptimadeWarning`][optimade.server.warnings.OptimadeWarning] has been handled.

        An [`OptimadeWarning`][optimade.server.warnings.OptimadeWarning] will be
        translated into an OPTIMADE Warnings JSON object in accordance with
        [the specification](https://github.com/Materials-Consortia/OPTIMADE/blob/v1.0.0/optimade.rst#json-response-schema-common-fields).
        This process is similar to the [Exception handlers][optimade.server.exception_handlers].

        Parameters:
            message: The `Warning` object to show and possibly handle.
            category: `Warning` type being warned about. This amounts to `type(message)`.
            filename: Name of the file, where the warning was issued.
            lineno: Line number in the file, where the warning was issued.
            file: A file-like object to which the warning should be written.
            line: Source content of the line that issued the warning.

        """
        assert isinstance(
            message, Warning
        ), "'message' is expected to be a Warning or subclass thereof."

        if not isinstance(message, OptimadeWarning):
            # If the Warning is not an OptimadeWarning or subclass thereof,
            # use the regular 'showwarning' function.
            warnings._showwarning_orig(message, category, filename, lineno,
                                       file, line)
            return

        # Format warning
        try:
            title = str(message.title)
        except AttributeError:
            title = str(message.__class__.__name__)

        try:
            detail = str(message.detail)
        except AttributeError:
            detail = str(message)

        if CONFIG.debug:
            if line is None:
                # All this is taken directly from the warnings library.
                # See 'warnings._formatwarnmsg_impl()' for the original code.
                try:
                    import linecache

                    line = linecache.getline(filename, lineno)
                except Exception:
                    # When a warning is logged during Python shutdown, linecache
                    # and the import machinery don't work anymore
                    line = None
                    linecache = None
            meta = {
                "filename": filename,
                "lineno": lineno,
            }
            if line:
                meta["line"] = line.strip()

        if CONFIG.debug:
            new_warning = Warnings(title=title, detail=detail, meta=meta)
        else:
            new_warning = Warnings(title=title, detail=detail)

        # Add new warning to self._warnings
        self._warnings.append(new_warning.dict(exclude_unset=True))

        # Show warning message as normal in sys.stderr
        warnings._showwarnmsg_impl(
            warnings.WarningMessage(message, category, filename, lineno, file,
                                    line))
Esempio n. 28
0
    def respond(self,obs):
        """
        #First retrieve which station is next.
          -Go to knowledge and ask which one we are working on right now and what's the source. If we have it from QA, then skip inference.
          -If we have it from QA, perform inference, and get the latest estimate.
        #Then navigate.
          - If we have the tool, simply go forward. 
          - Else, go to get the tool first. 
        """
        if obs.timestep == 0:
            #If it's the first timestep, we have no clue. Since we don't even know if we are going to ask questions in the
            #future, we go ahead and init the inference engine for future use.
            self.p_obs = copy.deepcopy(obs)
            self.tracking_stations = self.get_remaining_stations(obs)
            self.inference_engine = inference_engine(self.tracking_agent,self.tracking_stations)
            #And set the knowledge source to inference so the next step we know where to look for in the upcoming step.
            self.knowledge.source[0] = ORIGIN.Inference

            #And pick a target station at random since we have to move forward.
            target_station = np.random.choice(self.tracking_stations) #pick a station at random.

        else:
            curr_k_id = self.knowledge.get_current_job_station_id()

            #Checking what knowledge we have.
            if (self.knowledge.source[curr_k_id]==ORIGIN.Answer):
                #Then we simply work on the station because we have an answer telling us that that's the station to work on.
                target_station = self.knowledge.station_order[curr_k_id]

            elif (self.knowledge.source[curr_k_id] == None):
                #which means we just finished a station in the last time-step. This calls for re-initalizing the inference_engine
                self.tracking_stations = self.get_remaining_stations(obs)
                self.inference_engine = inference_engine(self.tracking_agent,self.tracking_stations)
                target_station = np.random.choice(self.tracking_stations)

            elif (self.knowledge.source[curr_k_id]==ORIGIN.Inference):
                #Which means we have been working on a inference for a station.
                target_station = self.inference_engine.inference_step(self.p_obs,obs)
                self.knowledge.update_knowledge_from_inference(target_station)
                warnings.WarningMessage("Provision resetting inference_engine when a station is finished")

            else:
                #it should never come to this.
                raise Exception("Some mistake around")

        """
        Okay, now that we know which station we should be headed to, we need to ensure the nitty-gritty details.
        Do we have a tool?
             If yes,
                if it matches our target station:
                     destination: station
                else:
                     destination: base
        else:
             destination: base
       
        Are we near our destination?
             Yes:
                Is it the base?
                    Pick up the tool.
                else:
                    execute work action.
             No:
                keep moving. 
        """  

        if self.tool is not None:
            if self.tool == target_station:
                destination = obs.allPos[obs.stationIndices[target_station]]
            else:
                destination = global_defs.TOOL_BASE
        else:
            destination = global_defs.TOOL_BASE

        if utils.is_neighbor(self.pos,destination):
            if destination == global_defs.TOOL_BASE:
                #We are at the base to pick up a tool.
                desired_action = global_defs.Actions.NOOP
                self.tool = target_station
            else:
                #we are the station to work.
                desired_action = global_defs.Actions.WORK
        else:
            #Navigate to destination.
            desired_action = None

        obstacles = copy.deepcopy(obs.allPos).remove(self.pos)
        proposal = utils.generate_proposal(self.pos,destination,obstacles,desired_action)
        return proposal
 def _showwarning(self, *args, **kwargs):
     self.captures.append(warnings.WarningMessage(*args, **kwargs))
Esempio n. 30
0
 def error(self, *warn_msg):
     self.errors.append(warnings.WarningMessage(*warn_msg))