Commit 26f9375b authored by Chris McDonough's avatar Chris McDonough

Add <warnfilter> section, which allows you to configure Python warning filters...

Add <warnfilter> section, which allows you to configure Python warning filters in zope.conf.  Useful for suppressing the USELESS DeprecationWarnings that emanate from TALGenerator wrt i18n.
parent da1616cb
......@@ -198,6 +198,42 @@
<!-- schema begins -->
<multisection type="warnfilter" attribute="warnfilters" name="*"
dataype="zLOG.warn_filter_handler">
<!-- from zLOG -->
<description>
A multisection which allows a user to set up a Python "warning" filter.
The following keys are valid within a warnfilter section:
action: one of the following strings:
"error" turn matching warnings into exceptions
"ignore" never print matching warnings
"always" always print matching warnings
"default" print the first occurrence of matching warnings
for each location where the warning is issued
"module" print the first occurrence of matching warnings
for each module where the warning is issued
"once" print only the first occurrence of matching
warnings, regardless of location
message: a string containing a regular expression that the
warning message must match (the match is compiled to
always be case-insensitive)
category: a Python dotted-path classname (must be a subclass of
Warning) of which the warning category must be a subclass in
order to match
module: a string containing a regular expression that the
module name must match (the match is compiled to be
case-sensitive)
lineno: an integer that the line number where the warning
occurred must match, or 0 to match all line numbers
</description>
</multisection>
<section type="environment" attribute="environment" name="*">
<description>
A section which allows a user to define arbitrary key-value pairs for
......
......@@ -58,4 +58,12 @@
<multisection type="zLOG.loghandler" attribute="handlers" name="*"/>
</sectiontype>
<sectiontype name="warnfilter" datatype=".warning_filter_handler">
<key name="action" datatype=".warn_action" default="default"/>
<key name="message" default=""/>
<key name="category" datatype=".warning_subclass" default="Warning"/>
<key name="module" default=""/>
<key name="lineno" datatype="integer" default="0"/>
</sectiontype>
</component>
......@@ -238,3 +238,50 @@ class EventLogFactory(Factory):
if handler_level < lowest:
lowest = factory.getLevel()
return lowest
def importable_name(name):
try:
components = name.split('.')
start = components[0]
g = globals()
package = __import__(start, g, g)
modulenames = [start]
for component in components[1:]:
modulenames.append(component)
try:
package = getattr(package, component)
except AttributeError:
n = '.'.join(modulenames)
package = __import__(n, g, g, component)
return package
except ImportError:
raise ValueError, (
'The object named by "%s" could not be imported' % name )
def warning_subclass(val):
ob = importable_name(val) # will fail in course
try:
if not issubclass(ob, Warning):
raise ValueError, (
'warning category "%s" must be a Warning subclass' % val)
except TypeError:
raise ValueError, (
'warning category "%s" must be a Warning subclass' % val)
return ob
def warn_action(val):
OK = ("error", "ignore", "always", "default", "module", "once")
if val not in OK:
raise ValueError, "warning action %s not one of %s" % (val, OK)
return val
def warning_filter_handler(section):
import warnings
# add the warning filter
warnings.filterwarnings(section.action, section.message, section.category,
section.module, section.lineno, 1)
return section
......@@ -680,6 +680,51 @@ instancehome $INSTANCE
# </nteventlog-handler>
# </logger>
# Directive: warnfilter
#
# Description:
# A section that allows you to define a warning filter.
# The following keys are valid within a warnfilter section:
#
# action: one of the following strings:
#
# "error" turn matching warnings into exceptions
# "ignore" never print matching warnings
# "always" always print matching warnings
# "default" print the first occurrence of matching warnings
# for each location where the warning is issued
# "module" print the first occurrence of matching warnings
# for each module where the warning is issued
# "once" print only the first occurrence of matching
# warnings, regardless of location
#
# message: a string containing a regular expression that the
# warning message must match (the match is compiled to
# always be case-insensitive)
#
# category: a Python dotted-path classname (must be a subclass of
# Warning) of which the warning category must be a subclass in
# order to match
#
# module: a string containing a regular expression that the
# module name must match (the match is compiled to be
# case-sensitive)
#
# lineno: an integer that the line number where the warning
# occurred must match, or 0 to match all line numbers
#
# All keys within a warnfilter section are optional. More than
# one warnfilter section may be specified.
#
# Default: unset
#
# Example:
#
# <warnfilter>
# action ignore
# category exceptions.DeprecationWarning
# </warnfilter>
# Directive: max-listen-sockets
#
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment