def test_warningToFile(self): """ L{twisted.python.log.showwarning} passes warnings with an explicit file target on to the underlying Python warning system. """ # log.showwarning depends on _oldshowwarning being set, which only # happens in startLogging(), which doesn't happen if you're not # running under trial. So this test only passes by accident of runner # environment. if log._oldshowwarning is None: raise unittest.SkipTest("Currently this test only runs under trial.") message = "another unique message" category = FakeWarning filename = "warning-filename.py" lineno = 31 output = StringIO() log.showwarning(message, category, filename, lineno, file=output) self.assertEqual( output.getvalue(), warnings.formatwarning(message, category, filename, lineno)) # In Python 2.6, warnings.showwarning accepts a "line" argument which # gives the source line the warning message is to include. if sys.version_info >= (2, 6): line = "hello world" output = StringIO() log.showwarning(message, category, filename, lineno, file=output, line=line) self.assertEqual( output.getvalue(), warnings.formatwarning(message, category, filename, lineno, line))
def _showwarning(message, category, filename, lineno, file=None, line=None): """ Implementation of showwarnings which redirects to logging, which will first check to see if the file parameter is None. If a file is specified, it will delegate to the original warnings implementation of showwarning. Otherwise, it will call warnings.formatwarning and will log the resulting string to a warnings logger named "py.warnings" with level logging.WARNING. """ if file is not None: if logging._warnings_showwarning is not None: if PY26: _warnings_showwarning(message, category, filename, lineno, file, line) else: # Python 2.5 and below don't support the line argument _warnings_showwarning(message, category, filename, lineno, file) else: if PY26: s = warnings.formatwarning(message, category, filename, lineno, line) else: s = warnings.formatwarning(message, category, filename, lineno) logger = logging.getLogger("py.warnings") if not logger.handlers: logger.addHandler(NullHandler()) logger.warning("%s", s)
def test_warningToFile(self): """ L{twisted.python.log.showwarning} passes warnings with an explicit file target on to the underlying Python warning system. """ message = "another unique message" category = FakeWarning filename = "warning-filename.py" lineno = 31 output = StringIO() log.showwarning(message, category, filename, lineno, file=output) self.assertEqual( output.getvalue(), warnings.formatwarning(message, category, filename, lineno)) # In Python 2.6 and higher, warnings.showwarning accepts # a "line" argument which gives the source line the warning # message is to include. line = "hello world" output = StringIO() log.showwarning(message, category, filename, lineno, file=output, line=line) self.assertEqual( output.getvalue(), warnings.formatwarning(message, category, filename, lineno, line))
def showwarning(message, category, filename, lineno, file=None): """Hook to write a warning to a file; replace if you like.""" if file is None: file = sys.stderr try: file.write(warnings.formatwarning(message, category, filename, lineno)) except: file = open ('warnings.log', 'a') file.write(warnings.formatwarning(message, category, filename, lineno)) file.close()
def customwarn(message, category, filename, lineno, *args, **kwargs): """Use the nc2map.warning logger for categories being out of Nc2MapWarning and Nc2MapCritical and the default warnings.showwarning function for all the others.""" if category is Nc2MapWarning: logger.warning(warnings.formatwarning( "\n%s" % message, category, filename, lineno)) elif category is Nc2MapCritical: logger.critical(warnings.formatwarning( "\n%s" % message, category, filename, lineno)) else: old_showwarning(message, category, filename, lineno, *args, **kwargs)
def customwarn(message, category, filename, lineno, *args, **kwargs): """Use the psyplot.warning logger for categories being out of PsyPlotWarning and PsyPlotCritical and the default warnings.showwarning function for all the others.""" if category is PsyPlotWarning: logger.warning(warnings.formatwarning( "\n%s" % message, category, filename, lineno)) elif category is PsyPlotCritical: logger.critical(warnings.formatwarning( "\n%s" % message, category, filename, lineno), exc_info=True) else: old_showwarning(message, category, filename, lineno, *args, **kwargs)
def _showwarning(message, category, filename, lineno, file=None, line=None): """ Redirect warnings into logging. """ fmtmsg = warnings.formatwarning(message, category, filename, lineno, line) LOG.warning(fmtmsg)
def ShowWarning(self, message, category, filename, lineno, file = None): import logmodule string = warnings.formatwarning(message, category, filename, lineno) logmodule.LogTraceback(extraText=string, severity=logmodule.LGWARN, nthParent=3) if not file: file = sys.stderr print >> file, string
def test_filteredOnceWarning(self): """ L{deprecate.warnAboutFunction} emits a warning that will be filtered once if L{warnings.filterwarning} is called with the module name of the deprecated function and an action of once. """ # Clean up anything *else* that might spuriously filter out the warning, # such as the "always" simplefilter set up by unittest._collectWarnings. # We'll also rely on trial to restore the original filters afterwards. del warnings.filters[:] warnings.filterwarnings( action="module", module="twisted_private_helper") from twisted_private_helper import module module.callTestFunction() module.callTestFunction() warningsShown = self.flushWarnings() self.assertEqual(len(warningsShown), 1) message = warningsShown[0]['message'] category = warningsShown[0]['category'] filename = warningsShown[0]['filename'] lineno = warningsShown[0]['lineno'] msg = warnings.formatwarning(message, category, filename, lineno) self.assertTrue( msg.endswith("module.py:9: DeprecationWarning: A Warning String\n" " return a\n"), "Unexpected warning string: %r" % (msg,))
def warning_record_to_str(warning_message): """Convert a warnings.WarningMessage to a string, taking in account a lot of unicode shenaningans in Python 2. When Python 2 support is dropped this function can be greatly simplified. """ warn_msg = warning_message.message unicode_warning = False if compat._PY2 and any(isinstance(m, compat.UNICODE_TYPES) for m in warn_msg.args): new_args = [] for m in warn_msg.args: new_args.append( compat.ascii_escaped(m) if isinstance(m, compat.UNICODE_TYPES) else m ) unicode_warning = list(warn_msg.args) != new_args warn_msg.args = new_args msg = warnings.formatwarning( warn_msg, warning_message.category, warning_message.filename, warning_message.lineno, warning_message.line, ) if unicode_warning: warnings.warn( "Warning is using unicode non convertible to ascii, " "converting to a safe representation:\n {!r}".format(compat.safe_str(msg)), UnicodeWarning, ) return msg
def warn_to_stdout(message, category, filename, lineno, file=None, line=None): """A function to replace warnings.showwarning so that warnings are printed to STDOUT instead of STDERR. Usage: warnings.showwarning = warn_to_stdout """ sys.stdout.write(warnings.formatwarning(message,category,filename,lineno))
def _showwarning(message, category, filename, lineno, file=None, line=None): if file is not None: if _warnings_showwarning is not None: _warnings_showwarning(message, category, filename, lineno, file, line) else: s = warnings.formatwarning(message, category, filename, lineno) warnlog.warning(s)
def _showwarning(message, category, filename, lineno, file=None, line=None): """ cerelog-adapted warnings.showwarning function. If cerelog.captureWarnings is enabled, this function will receive warnings. The default behaviour is to log warnings with the initialized logger, with level WARNING. If no logger is initialized, or warnings.showwarnings is called with a `file' argument that is not `None', the default warnings.showwarning function is called. See L{warnings.showwarning} for documentation of the arguments. """ logger = _logger_instance # PY25 compability - only include line if it's actually given. kw = dict() if line is not None: kw['line'] = line if file is not None and logger is not None: if _warnings_showwarning is not None: _warnings_showwarning(message, category, filename, lineno, file, **kw) else: s = warnings.formatwarning(message, category, filename, lineno, **kw) logger.warning("%s", s)
def warn_with_traceback(message, category, filename, lineno, line=None): print(u'***startwarning***') traceback.print_stack() print print(warnings.formatwarning(message, category, filename, lineno, line)) print(u'***endwarning***')
def _show_warning(message, category, filename, lineno, file=None, line=None): if file is not None: warnings._showwarning(message, category, filename, lineno, file, line) else: s = warnings.formatwarning(message, category, filename, lineno, line) logger = logging.getLogger("py.warnings") logger.warning("%s", s)
def idle_showwarning(message, category, filename, lineno, file=None, line=None): if file is None: file = warning_stream try: file.write(warnings.formatwarning(message, category, filename, lineno, file=file, line=line)) except IOError: pass ## file (probably __stderr__) is invalid, warning dropped.
def log_warnings(message, category, filename, lineno, file=None, line=None): WARNLOG = logging.getLogger("Python") logging.debug("message=%s message=%r category=%r", message, message, category) warning_string = warnings.formatwarning(message, category, filename, lineno) WARNLOG.debug("%s", warning_string)
def customwarn(message, category, filename, lineno, file=None, line=None): """ Override warnings.showwarning to avoid importing gtk when it fails to open the display. """ sys.stdout.write(warnings.formatwarning(message, category, filename, lineno)) if "could not open display" in message: raise ImportError("Could not open display")
def showWarning(message, category, filename, lineno, file=None, line=None): stk = "" for s in traceback.format_stack()[:-2]: stk += s.decode('utf-8', 'replace') QgsMessageLog.logMessage( "warning:%s\ntraceback:%s" % ( warnings.formatwarning(message, category, filename, lineno), stk), QCoreApplication.translate( "Python", "Python warning" ) )
def showwarning(self, message, category, filename, lineno, file=None, line=None): """ Called when a warning is generated. The default behaviour is to write it on stderr, which would lead to it being shown as an error. """ warn = warnings.formatwarning(message, category, filename, lineno, line) logging.warning(warn)
def custom_show_warning(message, category, filename, lineno, file=None, line=None): """ Override warnings.showwarning to raise an exception if GTK cannot open the DISPLAY. open the display. """ sys.stdout.write(warnings.formatwarning(message, category, filename, lineno)) if "could not open display" in message: raise RuntimeError("Error: Could not open display. Spinic needs a $DISPLAY.")
def __show_colored_warning(message, category, filename, lineno, file = None, line = None): if file is None: file = sys.stderr str = warnings.formatwarning(message, category, filename, lineno, line) if category == DeprecationWarning and termcolor.canDisplayColor(file): file.write(termcolor.makeColoredMessage(str, termcolor.BLUE)) else: file.write(str)
def showwarning(self, message, category, filename, lineno, file=sys.stderr, line=None): """ Make sure messages sent through python's warnings module get logged. The warnings mechanism is used by some libraries we use, notably pykickstart. """ self.anaconda_logger.warning("%s", warnings.formatwarning( message, category, filename, lineno, line))
def _showwarning(message, category, filename, lineno, file=warndest, line=None): if file is None: file = sys.stderr try: file.write(warnings.formatwarning(message, category, filename, lineno, line)) except IOError: pass
def _warning( message, category = galpyWarning, filename = '', lineno = -1): if issubclass(category,galpyWarning): print("galpyWarning: "+str(message)) else: print(warnings.formatwarning(message,category,filename,lineno))
def recv_msg(self, message): """ This method receives messages from the Orchestrator, parses them, and calls the appropriate notification methods defined below. :param message: Message received from the Orchestrator. :type message: Message """ # Control messages. if message.message_type == MessageType.MSG_TYPE_CONTROL: # This UI plugin must be started. if message.message_code == MessageCode.MSG_CONTROL_START_UI: self.start_ui() # This UI plugin must be shut down. elif message.message_code == MessageCode.MSG_CONTROL_STOP_UI: self.stop_ui() # An audit has started. elif message.message_code == MessageCode.MSG_CONTROL_START_AUDIT: self.notify_stage(message.audit_name, "start") # An audit has finished. elif message.message_code == MessageCode.MSG_CONTROL_STOP_AUDIT: self.notify_stage(message.audit_name, "finish" if message.message_info else "cancel") # A plugin has sent a log message. elif message.message_code == MessageCode.MSG_CONTROL_LOG: plugin_name = self.get_plugin_name(message) (text, level, is_error) = message.message_info if is_error: self.notify_error(message.audit_name, plugin_name, text, level) else: self.notify_log(message.audit_name, plugin_name, text, level) # A plugin has sent an error message. elif message.message_code == MessageCode.MSG_CONTROL_ERROR: plugin_name = self.get_plugin_name(message) (description, traceback) = message.message_info text = "Error: " + description self.notify_error(message.audit_name, plugin_name, text, Logger.STANDARD) text = "Exception raised: %s\n%s" % (description, traceback) self.notify_error(message.audit_name, plugin_name, text, Logger.MORE_VERBOSE) # A plugin has sent a warning message. elif message.message_code == MessageCode.MSG_CONTROL_WARNING: plugin_name = self.get_plugin_name(message) for w in message.message_info: formatted = warnings.formatwarning(w.message, w.category, w.filename, w.lineno, w.line) text = "Warning: " + w.message self.notify_warning(message.audit_name, plugin_name, text, Logger.STANDARD) text = "Warning details: " + formatted self.notify_warning(message.audit_name, plugin_name, text, Logger.MORE_VERBOSE)
def warn_with_traceback(message, category, filename, lineno, file=None, line=None): """ Alternate warning printer which shows a traceback with the warning. To use, set *warnings.showwarning = warn_with_traceback*. """ traceback.print_stack() log = file if hasattr(file, 'write') else sys.stderr log.write(warnings.formatwarning(message, category, filename, lineno, line))
def _showwarning(message, category, filename, lineno, file=None, line=None): """ Version of warnings.showwarning that won't attempt to print out the line at the location of the warning if the line text is not explicitly specified. """ if file is None: file = sys.stderr if line is None: line = '' file.write(warnings.formatwarning(message, category, filename, lineno, line))
def _showwarning(message, category, filename, lineno, file = None, line = None): global _warnings_showwarning if file is not None: if _warnings_showwarning is not None: _warnings_showwarning(message, category, filename, lineno, file, line) else: s = warnings.formatwarning(message, category, filename, lineno, line) logger = getLogger('py.warnings') if not logger.handlers: logger.addHandler(NullHandler()) logger.warning('%s', s)
def catch_warnings_for_item(item): """ catches the warnings generated during setup/call/teardown execution of the given item and after it is done posts them as warnings to this item. """ args = item.config.getoption("pythonwarnings") or [] inifilters = item.config.getini("filterwarnings") with warnings.catch_warnings(record=True) as log: for arg in args: warnings._setoption(arg) for arg in inifilters: _setoption(warnings, arg) for mark in item.iter_markers(name="filterwarnings"): for arg in mark.args: warnings._setoption(arg) yield for warning in log: warn_msg = warning.message unicode_warning = False if ( compat._PY2 and any(isinstance(m, compat.UNICODE_TYPES) for m in warn_msg.args) ): new_args = [] for m in warn_msg.args: new_args.append( compat.ascii_escaped(m) if isinstance(m, compat.UNICODE_TYPES) else m ) unicode_warning = list(warn_msg.args) != new_args warn_msg.args = new_args msg = warnings.formatwarning( warn_msg, warning.category, warning.filename, warning.lineno, warning.line, ) item.warn("unused", msg) if unicode_warning: warnings.warn( "Warning is using unicode non convertible to ascii, " "converting to a safe representation:\n %s" % msg, UnicodeWarning, )
def showWarning(message, category, filename, lineno, file=None, line=None): stk = "" for s in traceback.format_stack()[:-2]: if hasattr(s, 'decode'): stk += s.decode(sys.getfilesystemencoding()) else: stk += s if hasattr(filename, 'decode'): decoded_filename = filename.decode(sys.getfilesystemencoding()) else: decoded_filename = filename QgsMessageLog.logMessage( u"warning:{}\ntraceback:{}".format( warnings.formatwarning(message, category, decoded_filename, lineno), stk), QCoreApplication.translate("Python", "Python warning"))
def warn_with_traceback(message, category, filename, lineno, file=None, line=None): log = file if hasattr(file,'write') else sys.stderr traceback.print_stack(file=log) log.write(warnings.formatwarning(message, category, filename, lineno, line)) return
def warn_with_traceback(message, category, filename, lineno, file=None, line=None): """ Add a traceback to warnings to help locate where it being triggered source: https://stackoverflow.com/a/22376126/9567306 Add the following code import warnings warnings.showwarning = make_dummy_data.warn_with_traceback Use warnings.resetwarnings() to reset """ log = file if hasattr(file,'write') else sys.stderr traceback.print_stack(file=log) log.write(warnings.formatwarning(message, category, filename, lineno, line))
def catch_warnings_for_item(item): """ catches the warnings generated during setup/call/teardown execution of the given item and after it is done posts them as warnings to this item. """ args = item.config.getoption('pythonwarnings') or [] inifilters = item.config.getini("filterwarnings") with warnings.catch_warnings(record=True) as log: for arg in args: warnings._setoption(arg) for arg in inifilters: _setoption(warnings, arg) mark = item.get_marker('filterwarnings') if mark: for arg in mark.args: warnings._setoption(arg) yield for warning in log: warn_msg = warning.message unicode_warning = False if compat._PY2 and any( isinstance(m, compat.UNICODE_TYPES) for m in warn_msg.args): new_args = [] for m in warn_msg.args: new_args.append( compat.ascii_escaped(m) if isinstance( m, compat.UNICODE_TYPES) else m) unicode_warning = list(warn_msg.args) != new_args warn_msg.args = new_args msg = warnings.formatwarning(warn_msg, warning.category, warning.filename, warning.lineno, warning.line) item.warn("unused", msg) if unicode_warning: warnings.warn( "Warning is using unicode non convertible to ascii, " "converting to a safe representation:\n %s" % msg, UnicodeWarning)
def __send_warnings_to_log__(message, category, filename, lineno, file=None, line=None): try: warnings except: import warnings try: pyto except: import pyto _message = warnings.formatwarning(message, category, filename, lineno, line) try: pyto.PyOutputHelper.printWarning(_message, script=threading.current_thread().script_path) except AttributeError: pyto.PyOutputHelper.printWarning(_message, script=None) return
def showwarning_as_comment(message, category, filename, lineno, file=None, line=None): # Based on warnings._showwarnmsg_impl text = warnings.formatwarning(message, category, filename, lineno, line) text = indent(text, '# ') if file is None: file = sys.stderr if file is None: return try: file.write(text) except OSError: pass
def handle_warning(message, category, filename, lineno, file=None, line=None): """ Implementation of showwarnings which redirects to logging, which will first check to see if the file parameter is None. If a file is specified, it will delegate to the original warnings implementation of showwarning. Otherwise, it will call warnings.formatwarning and will log the resulting string to a warnings logger named "py.warnings" with level logging.WARNING. """ if file is not None: # I don't actually know how this can be not None if _warnings_showwarning is not None: # pragma: no cover _warnings_showwarning(message, category, filename, lineno, file, line) else: s = warnings.formatwarning(message, category, filename, lineno, line) logger = getLogger("py.warnings") logger.warning("%s", s)
def recv_msg(self, message): if not isinstance(message, Message): raise TypeError("Expected Message, got %r instead" % type(message)) print "-" * 79 print "Message:" print " Timestamp: %s" % time.ctime(message.timestamp) print " Audit: %s" % message.audit_name print " Plugin: %s" % message.plugin_id print " Type: %s" % MessageType.get_name_from_value( message.message_type) print " Code: %s" % MessageCode.get_name_from_value_and_type( message.message_code, message.message_type) print " Priority: %s" % MessagePriority.get_name_from_value( message.priority) print " Payload: %r" % (message.message_info, ) print if message.message_type == MessageType.MSG_TYPE_CONTROL: if message.message_code == MessageCode.MSG_CONTROL_STOP_AUDIT: if get_audit_count() == 1: Config._context.send_msg( message_type=MessageType.MSG_TYPE_CONTROL, message_code=MessageCode.MSG_CONTROL_STOP, message_info=True, priority=MessagePriority.MSG_PRIORITY_LOW) elif message.message_code == MessageCode.MSG_CONTROL_LOG: (text, level, is_error) = message.message_info if is_error: print colorize(text, "magenta") else: print colorize(text, "cyan") elif message.message_code == MessageCode.MSG_CONTROL_ERROR: (description, traceback) = message.message_info print colorize(description, "magenta") print colorize(traceback, "magenta") elif message.message_code == MessageCode.MSG_CONTROL_WARNING: for w in message.message_info: formatted = warnings.formatwarning(w.message, w.category, w.filename, w.lineno, w.line) print colorize(formatted, "yellow")
def _load_requests_redis() -> typing.List[Link]: """Load link from the :mod:`requests` database. The function reads the ``queue_requests`` database. Returns: List of loaded links from the :mod:`requests` database. Note: At runtime, the function will load links with maximum number at :data:`~darc.db.MAX_POOL` to limit the memory usage. """ now = time.time() if TIME_CACHE is None: sec_delta = 0 max_score = now else: sec_delta = TIME_CACHE.total_seconds() max_score = now - sec_delta try: with _redis_get_lock('lock_queue_requests', blocking_timeout=LOCK_TIMEOUT): link_pool = [ pickle.loads(link) for link in _redis_command('zrangebyscore', 'queue_requests', min=0, max=max_score, start=0, num=MAX_POOL) ] if TIME_CACHE is not None: new_score = now + sec_delta _save_requests_redis(link_pool, score=new_score) # force update records except redis_lock.LockError: warning = warnings.formatwarning( f'[REQUESTS] Failed to acquire Redis lock after {LOCK_TIMEOUT} second(s)', LockWarning, __file__, 250, "_redis_get_lock('lock_queue_requests')") print(render_error(warning, stem.util.term.Color.YELLOW), end='', file=sys.stderr) # pylint: disable=no-member link_pool = list() return link_pool
def tor_bootstrap(): """Bootstrap wrapper for Tor. The function will bootstrap the Tor proxy. It will retry for :data:`~darc.proxy.tor.TOR_RETRY` times in case of failure. Also, it will **NOT** re-bootstrap the proxy as is guaranteed by :data:`~darc.proxy.tor._TOR_BS_FLAG`. Warns: TorBootstrapFailed: If failed to bootstrap Tor proxy. See Also: * :func:`darc.proxy.tor._tor_bootstrap` * :data:`darc.proxy.tor.TOR_RETRY` * :data:`darc.proxy.tor._TOR_BS_FLAG` """ # don't re-bootstrap if _TOR_BS_FLAG: return print( stem.util.term.format('-*- Tor Bootstrap -*-', stem.util.term.Color.MAGENTA)) # pylint: disable=no-member for _ in range(TOR_RETRY + 1): try: _tor_bootstrap() break except Exception as error: if DEBUG: message = '[Error bootstraping Tor proxy]' + os.linesep + traceback.format_exc( ) print(render_error(message, stem.util.term.Color.RED), end='', file=sys.stderr) # pylint: disable=no-member warning = warnings.formatwarning(error, TorBootstrapFailed, __file__, 170, 'tor_bootstrap()') print(render_error(warning, stem.util.term.Color.YELLOW), end='', file=sys.stderr) # pylint: disable=no-member print( stem.util.term.format('-' * shutil.get_terminal_size().columns, stem.util.term.Color.MAGENTA)) # pylint: disable=no-member
def _showWarning(self, message, category, filename, lineno, file=None, line=None): # Don't display repeat warnings if str(message) in self.logged_warnings: return m = warnings.formatwarning(message, category, filename, lineno, line) self.log.warning(m) self.logged_warnings.add(str(message)) # Log this warning, but never display it to the user; it is # nearly un-actionable. if category == requestsexceptions.InsecurePlatformWarning: return # Disable InsecureRequestWarning when certificate validation is disabled if not self.config.verify_ssl: if category == requestsexceptions.InsecureRequestWarning: return self.error_queue.put(('Warning', m)) os.write(self.error_pipe, six.b('error\n'))
def warn_with_traceback(message, category, filename, lineno, file=None, line=None): """Get full tracebacks when warning is raised by setting warnings.showwarning = warn_with_traceback See also -------- http://stackoverflow.com/questions/22373927/get-traceback-of-warnings """ import traceback traceback.print_stack() log = file if hasattr(file, 'write') else sys.stderr settings.write( warnings.formatwarning(message, category, filename, lineno, line))
def log_warning(msg, typ, script, lineno, **kwargs): if option_set('general', 'debug'): print('Logging warning "%s"' % str(msg)) warning = { 'type': typ.__name__, 'message': str(msg), 'script': script, 'lineno': lineno } # Update object in DB db = open_or_create_db() db.update(append("warnings", warning, no_duplicates=True), eids=[RUN_ID]) db.close() # Done logging, print warning to stderr sys.stderr.write(warnings.formatwarning(msg, typ, script, lineno))
def renew_tor_session(): """Renew Tor session.""" global _TOR_CTRL try: # Tor controller process if _TOR_CTRL is None: _TOR_CTRL = stem.control.Controller.from_port(port=int(TOR_CTRL)) _TOR_CTRL.authenticate(TOR_PASS) _TOR_CTRL.signal(stem.Signal.NEWNYM) # pylint: disable=no-member except Exception as error: warning = warnings.formatwarning( error, TorRenewFailed, __file__, 88, '_TOR_CTRL = stem.control.Controller.from_port(port=int(TOR_CTRL))' ) print(render_error(warning, stem.util.term.Color.YELLOW), end='', file=sys.stderr) # pylint: disable=no-member
def pytest_runtest_call(item): wrec = WarningsRecorder() def showwarning(message, category, filename, lineno, file=None, line=None): frame = inspect.currentframe() if '/_pytest/recwarn' in frame.f_back.f_code.co_filename: # we are in test recorder, so this warning is already handled return wrec._list.append( RecordedWarning(message, category, filename, lineno, file, line)) # still perform old showwarning functionality wrec._showwarning(message, category, filename, lineno, file=file, line=line) args = item.config.getoption('pythonwarnings') or [] inifilters = item.config.getini("filterwarnings") with wrec: _showwarning = wrec._showwarning warnings.showwarning = showwarning wrec._module.simplefilter('once') for arg in args: wrec._module._setoption(arg) for arg in inifilters: _setoption(wrec._module, arg) yield wrec._showwarning = _showwarning for warning in wrec.list: msg = warnings.formatwarning(warning.message, warning.category, os.path.relpath(warning.filename), warning.lineno, warning.line) fslocation = getattr(item, "location", None) if fslocation is None: fslocation = getattr(item, "fspath", None) else: fslocation = "%s:%s" % fslocation[:2] fslocation = "in %s the following warning was recorded:\n" % fslocation item.config.warn("W0", msg, fslocation=fslocation)
def warn_with_traceback(message, category, filename, lineno, file=None, line=None): """ :param message: :param category: :param filename: :param lineno: :param file: :param line: """ log = file if hasattr(file, 'write') else sys.stderr log.write( warnings.formatwarning(message, category, filename, lineno, line)) # noinspection PyProtectedMember better_exchook.print_tb(sys._getframe(), file=log)
def test_warning_format(caplog, warning: Union[str, Warning], category: Type[Warning], filename: str, lineno: int): caplog.set_level(logging.WARNING) WarningsManager.apply_config({}) warnings.resetwarnings() warnings.filterwarnings("default", category=Warning) warnings.warn_explicit(warning, category, filename, lineno) if isinstance(warning, InmantaWarning): assert caplog.record_tuples == [ ("inmanta.warnings", logging.WARNING, "%s: %s" % (category.__name__, warning)) ] else: assert caplog.record_tuples == [( "py.warnings", logging.WARNING, warnings.formatwarning(warning, category, filename, lineno), # type: ignore )]
def _redis_command(command: str, *args: 'Any', **kwargs: 'Any') -> 'Any': """Wrapper function for Redis command. Args: command: Command name. *args: Arbitrary arguments for the Redis command. Keyword Args: **kwargs: Arbitrary keyword arguments for the Redis command. Return: Values returned from the Redis command. Warns: RedisCommandFailed: Warns at each round when the command failed. See Also: Between each retry, the function sleeps for :data:`~darc.db.RETRY_INTERVAL` second(s) if such value is **NOT** :data:`None`. """ _arg_msg = None method = getattr(REDIS, command) while True: try: value = method(*args, **kwargs) except Exception as error: if _arg_msg is None: _arg_msg = _gen_arg_msg(*args, **kwargs) warning = warnings.formatwarning( str(error), RedisCommandFailed, __file__, 123, f'value = redis.{command}({_arg_msg})') print(render_error(warning, stem.util.term.Color.YELLOW), end='', file=sys.stderr) # pylint: disable=no-member if RETRY_INTERVAL is not None: time.sleep(RETRY_INTERVAL) continue break return value
def _showwarning( cls, message: Union[str, Warning], category: Type[Warning], filename: str, lineno: int, file: Optional[TextIO] = None, line: Optional[str] = None, ) -> None: """ Shows a warning. :param message: The warning to show. :param category: The type of the warning. :param filename: Required for compatibility but will be ignored. :param lineno: Required for compatibility but will be ignored. :param file: The file to write the warning to. Defaults to stderr. :param line: Required for compatibility but will be ignored. """ # implementation based on warnings._showwarnmsg_impl and logging._showwarning text: str logger: logging.Logger if issubclass(category, InmantaWarning): text = "%s: %s" % (category.__name__, message) logger = logging.getLogger("inmanta.warnings") else: text = warnings.formatwarning( # ignore type check because warnings.formatwarning accepts Warning instance but it's type definition doesn't message, # type: ignore category, filename, lineno, line, ) logger = logging.getLogger("py.warnings") if file is not None: try: # This code path is currently not used in our code base file.write(f"{text}\n") except OSError: pass else: logger.warning("%s", text)
def warn_with_traceback(message, category, filename, lineno, file=None, line=None): """Use this method in place of `warnings.showwarning` to include traceback information. Use: `warnings.showwarning = warn_with_traceback` Taken from: http://stackoverflow.com/a/22376126/230468 """ import traceback traceback.print_stack() log = file if hasattr(file, 'write') else sys.stderr log.write(warnings.formatwarning(message, category, filename, lineno, line)) return
def load_selenium(check: bool = CHECK) -> typing.List[Link]: """Load link from the :mod:`selenium` database. Args: check: If perform checks on loaded links, default to :data:`~darc.const.CHECK`. Returns: List of loaded links from the :mod:`selenium` database. Note: At runtime, the function will load links with maximum number at :data:`~darc.db.MAX_POOL` to limit the memory usage. See Also: * :func:`darc.db._load_selenium_db` * :func:`darc.db._load_selenium_redis` """ if FLAG_DB: with database.connection_context(): try: link_pool = _load_selenium_db() except Exception as error: warning = warnings.formatwarning(str(error), DatabaseOperaionFailed, __file__, 983, '_load_selenium_db()') print(render_error(warning, stem.util.term.Color.YELLOW), end='', file=sys.stderr) # pylint: disable=no-member link_pool = list() else: link_pool = _load_selenium_redis() if check: link_pool = _check(link_pool) if VERBOSE: print(stem.util.term.format('-*- [SELENIUM] LINK POOL -*-', stem.util.term.Color.MAGENTA)) # pylint: disable=no-member print(render_error(pprint.pformat(sorted(link.url for link in link_pool)), stem.util.term.Color.MAGENTA)) # pylint: disable=no-member print(stem.util.term.format('-' * shutil.get_terminal_size().columns, stem.util.term.Color.MAGENTA)) # pylint: disable=no-member return link_pool
def showwarning(message, category, filename, lineno, file=None, line=None): """Display ArchiveWarning in a somewhat more user friendly manner. All other warnings are formatted the standard way. """ # This is a modified version of the function of the same name from # the Python standard library warnings module. if file is None: file = sys.stderr if file is None: # sys.stderr is None when run with pythonw.exe - warnings get lost return try: if issubclass(category, ArchiveWarning): s = "%s: %s\n" % (argparser.prog, message) else: s = warnings.formatwarning(message, category, filename, lineno, line) file.write(s) except OSError: pass # the file (probably stderr) is invalid - this warning gets lost.
def save_requests(entries: typing.Union[Link, typing.List[Link]], single: bool = False, # pylint: disable=inconsistent-return-statements score: typing.Optional[float] = None, nx: bool = False, xx: bool = False) -> None: """Save link to the :mod:`requests` database. The function updates the ``queue_requests`` database. Args: entries: Links to be added to the :mod:`requests` database. It can be either a :obj:`list` of links, or a single link string (if ``single`` set as :data:`True`). single: Indicate if ``entries`` is a :obj:`list` of links or a single link string. score: Score to for the Redis sorted set. nx: Only create new elements and not to update scores for elements that already exist. xx: Only update scores of elements that already exist. New elements will not be added. Notes: The ``entries`` will be dumped through :mod:`pickle` so that :mod:`darc` do not need to parse them again. When ``entries`` is a list of :class:`~darc.link.Link` instances, we tries to perform *bulk* update to easy the memory consumption. The *bulk* size is defined by :data:`~darc.db.BULK_SIZE`. See Also: * :func:`darc.db._save_requests_db` * :func:`darc.db._save_requests_redis` """ if FLAG_DB: with database.connection_context(): try: return _save_requests_db(entries, single, score, nx, xx) except Exception as error: warning = warnings.formatwarning(str(error), DatabaseOperaionFailed, __file__, 505, '_save_requests_db(...)') print(render_error(warning, stem.util.term.Color.YELLOW), end='', file=sys.stderr) # pylint: disable=no-member return return _save_requests_redis(entries, single, score, nx, xx)
def drop_selenium(link: Link) -> None: # pylint: disable=inconsistent-return-statements """Remove link from the :mod:`selenium` database. Args: link: Link to be removed. See Also: * :func:`darc.db._drop_selenium_db` * :func:`darc.db._drop_selenium_redis` """ if FLAG_DB: with database.connection_context(): try: return _drop_selenium_db(link) except Exception as error: warning = warnings.formatwarning(str(error), DatabaseOperaionFailed, __file__, 433, f'_drop_selenium_db({link})') print(render_error(warning, stem.util.term.Color.YELLOW), end='', file=sys.stderr) # pylint: disable=no-member return return _drop_selenium_redis(link)
def warn_with_traceback(message, category, filename, lineno, file=None, line=None): """ Promts all warnings to throw errors. :param message: :param category: :param filename: :param lineno: :param file: :param line: :return: """ log = file if hasattr(file, 'write') else sys.stderr traceback.print_stack(file=log) log.write(warnings.formatwarning(message, category, filename, lineno, line))
def warn_with_traceback( message: str, category: Type[Warning], filename: str, lineno: int, file: TextIO = None, line: str = None, ) -> None: """A function to use with `warnings.showwarning` to show a traceback. Args: message (str): A message that will be included in the warning. category (Type[Warning]): A category (subclass) of the warning. filename (str): Name of the file that raises the warning lineno (int): Number of line in the file that causes this warning. file (TextIO, optional): A file object for recording the log. Defaults to None. line (str, optional): A line of source code to be included in the warning message. Defaults to None. """ log = file if hasattr(file, "write") else sys.stderr traceback.print_stack(file=log) log.write(warnings.formatwarning(message, category, filename, lineno, line))
def main(): parser = get_parser() args = parser.parse_args() target_list = list() for target in args.target: try: module = importlib.import_module(f'pcapkit.vendor.{target}') target_list.extend( getattr(module, name) for name in module.__all__) except ImportError: warn = warnings.formatwarning(f'invalid vendor updater: {target}', InvalidVendorWarning, __file__, 0, ' '.join(sys.argv)) print(warn, file=sys.stderr) if not target_list: target_list.extend(getattr(vendor_module, name) for name in vendor_all) with multiprocessing.Pool() as pool: pool.map(run, target_list)
def zeronet_bootstrap() -> None: """Bootstrap wrapper for ZeroNet. The function will bootstrap the ZeroNet proxy. It will retry for :data:`~darc.proxy.zeronet.ZERONET_RETRY` times in case of failure. Also, it will **NOT** re-bootstrap the proxy as is guaranteed by :data:`~darc.proxy.zeronet._ZERONET_BS_FLAG`. Warns: ZeroNetBootstrapFailed: If failed to bootstrap ZeroNet proxy. Raises: :exc:`UnsupportedPlatform`: If the system is not supported, i.e. not macOS or Linux. See Also: * :func:`darc.proxy.zeronet._zeronet_bootstrap` * :data:`darc.proxy.zeronet.ZERONET_RETRY` * :data:`darc.proxy.zeronet._ZERONET_BS_FLAG` """ # don't re-bootstrap if _ZERONET_BS_FLAG: return print(stem.util.term.format('-*- ZeroNet Bootstrap -*-', stem.util.term.Color.MAGENTA)) # pylint: disable=no-member for _ in range(ZERONET_RETRY+1): try: _zeronet_bootstrap() break except Exception as error: if DEBUG: message = '[Error bootstraping ZeroNet proxy]' + os.linesep + traceback.format_exc() print(render_error(message, stem.util.term.Color.RED), end='', file=sys.stderr) # pylint: disable=no-member warning = warnings.formatwarning(str(error), ZeroNetBootstrapFailed, __file__, 133, 'zeronet_bootstrap()') print(render_error(warning, stem.util.term.Color.YELLOW), end='', file=sys.stderr) # pylint: disable=no-member print(stem.util.term.format('-' * shutil.get_terminal_size().columns, stem.util.term.Color.MAGENTA)) # pylint: disable=no-member
def _showwarning(message, category, filename, lineno, file=None, line=None): """Implementation of showwarnings which redirects to logging. It will first check to see if the file parameter is None. If a file is specified, it will delegate to the original warnings implementation of showwarning. Otherwise, it will call warnings.formatwarning and will log the resulting string to a warnings logger named "py.warnings" with level logging.WARNING. """ if sys.hexversion >= 0x2070000: raise RuntimeError("_showwarning() should not be used on Python 2.7+") if file is not None: if _warnings_showwarning is not None: _warnings_showwarning(message, category, filename, lineno, file, line) else: s = warnings.formatwarning(message, category, filename, lineno, line) logger = logging.getLogger("py.warnings") if not logger.handlers: logger.addHandler(NullHandler()) logger.warning("%s", s)
def warn_with_traceback(message, category, filename, lineno, file=None, line=None): """ See: https://stackoverflow.com/a/22376126/230468 Add this to use: `warnings.showwarning = warn_with_traceback` """ import traceback import warnings import sys log = file if hasattr(file, 'write') else sys.stderr traceback.print_stack(file=log) log.write(warnings.formatwarning(message, category, filename, lineno, line)) return