def setup_warnings(filters=[]): """ Re-initialize warning filters without defaults. This re-does the warnings initialization without using the python defaults. In stead, we initialize the warnings filters with L{sys.warnoptions}, and the L{filters} argument. The latter will typically come from a configuration file. :param list filters: A list of filter strings, (as accepted by `python -W' or `PYTHONWARNINGS', see L{warnings}). The filters are added in reverse priority (the last one takes priority of the first) - just like the `python -W' options or `PYTHONWARNINGS'. :raise ValueError: If a filter string is invalid. """ global _cached_filters filters = list(filters) # Make a copy if _cached_filters is not None and _cached_filters == filters: return # No change warnings.resetwarnings() # TODO: The default warnings module alters the warnings filter based on # these flags. We should look into: # - sys.flags.byteswarning (controls default BytesWarning filter) # - sys.flags.py3k_warning (disables default ignore-filter) # - sys.flags.division_warning (disables default ignore-filter) for warnfilter in filters + sys.warnoptions: try: warnings._setoption(warnfilter) except warnings._OptionError, e: raise ValueError("Invalid warning filter: '%s'" % e)
def test_options(self): # Uses the private _setoption() function to test the parsing # of command-line warning arguments self.assertRaises(warnings._OptionError, warnings._setoption, "1:2:3:4:5:6") self.assertRaises(warnings._OptionError, warnings._setoption, "bogus::Warning") self.assertRaises(warnings._OptionError, warnings._setoption, "ignore:2::4:-5") warnings._setoption("error::Warning::0") self.assertRaises(UserWarning, warnings.warn, "convert to error")
def test_options(self): # Uses the private _setoption() function to test the parsing # of command-line warning arguments self.assertRaises(warnings._OptionError, warnings._setoption, '1:2:3:4:5:6') self.assertRaises(warnings._OptionError, warnings._setoption, 'bogus::Warning') self.assertRaises(warnings._OptionError, warnings._setoption, 'ignore:2::4:-5') warnings._setoption('error::Warning::0') self.assertRaises(UserWarning, warnings.warn, 'convert to error')
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 __set_filters(self, filters): with _filter_lock: filters = list(filters or ()) # Make a copy warnings.resetwarnings() for wf in filters + sys.warnoptions: try: warnings._setoption(wf) except warnings._OptionError as e: raise ValueError( "Invalid warning filter ({0}): {1}".format(wf, e)) return filters
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 catch_warnings_for_item(config, ihook, when, item): """ Context manager that catches warnings generated in the contained execution block. ``item`` can be None if we are not in the context of an item execution. Each warning captured triggers the ``pytest_warning_captured`` hook. """ cmdline_filters = config.getoption("pythonwarnings") or [] inifilters = config.getini("filterwarnings") with warnings.catch_warnings(record=True) as log: if not sys.warnoptions: # if user is not explicitly configuring warning filters, show deprecation warnings by default (#2908) warnings.filterwarnings("always", category=DeprecationWarning) warnings.filterwarnings("always", category=PendingDeprecationWarning) warnings.filterwarnings("error", category=pytest.RemovedInPytest4Warning) warnings.filterwarnings("error", category=pytest.PytestDeprecationWarning) # filters should have this precedence: mark, cmdline options, ini # filters should be applied in the inverse order of precedence for arg in inifilters: _setoption(warnings, arg) for arg in cmdline_filters: warnings._setoption(arg) if item is not None: for mark in item.iter_markers(name="filterwarnings"): for arg in mark.args: _setoption(warnings, arg) yield for warning_message in log: ihook.pytest_warning_captured.call_historic( kwargs=dict(warning_message=warning_message, when=when, item=item) )
def catch_warnings_for_item(config, ihook, when, item): """ Context manager that catches warnings generated in the contained execution block. ``item`` can be None if we are not in the context of an item execution. Each warning captured triggers the ``pytest_warning_captured`` hook. """ cmdline_filters = config.getoption("pythonwarnings") or [] inifilters = config.getini("filterwarnings") with warnings.catch_warnings(record=True) as log: if not sys.warnoptions: # if user is not explicitly configuring warning filters, show deprecation warnings by default (#2908) warnings.filterwarnings("always", category=DeprecationWarning) warnings.filterwarnings("always", category=PendingDeprecationWarning) warnings.filterwarnings("error", category=pytest.RemovedInPytest4Warning) # filters should have this precedence: mark, cmdline options, ini # filters should be applied in the inverse order of precedence for arg in inifilters: _setoption(warnings, arg) for arg in cmdline_filters: warnings._setoption(arg) if item is not None: for mark in item.iter_markers(name="filterwarnings"): for arg in mark.args: _setoption(warnings, arg) yield for warning_message in log: ihook.pytest_warning_captured.call_historic( kwargs=dict(warning_message=warning_message, when=when, item=item) )
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: warnings.simplefilter('once') for arg in args: warnings._setoption(arg) for arg in inifilters: _setoption(warnings, arg) yield for warning in log: msg = warnings.formatwarning(warning.message, warning.category, warning.filename, warning.lineno, warning.line) item.warn("unused", msg)
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) 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): warn_msg.args = [compat.safe_str(m) for m in warn_msg.args] unicode_warning = True msg = warnings.formatwarning(warn_msg, warning.category, warning.filename, warning.lineno, warning.line) item.warn("unused", msg) if unicode_warning: warnings.warn( "This warning %s is broken as it's message is not a str instance" "(after all this is a stdlib problem workaround)" % msg, UnicodeWarning)
def catch_warnings_for_item(config, ihook, when, item): """ Context manager that catches warnings generated in the contained execution block. ``item`` can be None if we are not in the context of an item execution. Each warning captured triggers the ``pytest_warning_captured`` hook. """ args = config.getoption("pythonwarnings") or [] inifilters = config.getini("filterwarnings") with warnings.catch_warnings(record=True) as log: filters_configured = args or inifilters or sys.warnoptions for arg in args: warnings._setoption(arg) for arg in inifilters: _setoption(warnings, arg) if item is not None: for mark in item.iter_markers(name="filterwarnings"): for arg in mark.args: _setoption(warnings, arg) filters_configured = True if not filters_configured: # if user is not explicitly configuring warning filters, show deprecation warnings by default (#2908) warnings.filterwarnings("always", category=DeprecationWarning) warnings.filterwarnings("always", category=PendingDeprecationWarning) yield for warning_message in log: ihook.pytest_warning_captured.call_historic( kwargs=dict(warning_message=warning_message, when=when, item=item) )
"this is the main file, change input file appropriately and run this script" import objectDetectUtils # keras gives too many deprecation warnings, ignoring all warnings import warnings warnings._setoption('ignore') # making instance of objectDetectUtils class objectDetectInstance = objectDetectUtils.objectDetectutils() # getting compiled model model = objectDetectInstance.getModelDefination() # providing image to model for to predict on imagePath = "images/halftrack.jpg" image = objectDetectInstance.preprocessImage(imagePath) # collecting responses response = model.predict_proba(image) # top five class according to prediction probability topFive = objectDetectInstance.getClassName(response[0]) # witting to image objectDetectInstance.writeToImage(imagePath, topFive)
def update_event(self, inp=-1): self.set_output_val(0, warnings._setoption(self.input(0)))