Skip to content

Commit 17123ea

Browse files
committed
Only log warnings from commandline script.
I need to remember this is a lib first and not configure logging from within the lib. Also, from the script we are now actually displaying deprecation warnings. For some reason I don't understnad deprecation warnings are hidden by default in Python. And who remembers to run Python with the `-Wd` flag every time they upgrade a lib just to make sure there's no new deprecations? Fixes #384.
1 parent 3cfa02a commit 17123ea

File tree

4 files changed

+30
-21
lines changed

4 files changed

+30
-21
lines changed

markdown/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949

5050
__all__ = ['Markdown', 'markdown', 'markdownFromFile']
5151

52+
5253
logger = logging.getLogger('MARKDOWN')
53-
logging.captureWarnings(True)
5454

5555

5656
class Markdown(object):

markdown/__main__.py

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
55
"""
66

7-
import markdown
87
import sys
98
import optparse
109
import codecs
10+
import warnings
11+
import markdown
1112
try:
1213
import yaml
1314
except ImportError: # pragma: no cover
1415
import json as yaml
1516

1617
import logging
17-
from logging import DEBUG, INFO, CRITICAL
18+
from logging import DEBUG, WARNING, CRITICAL
1819

1920
logger = logging.getLogger('MARKDOWN')
2021

@@ -62,7 +63,7 @@ def parse_options(args=None, values=None):
6263
action="store_const", const=CRITICAL+10, dest="verbose",
6364
help="Suppress all warnings.")
6465
parser.add_option("-v", "--verbose",
65-
action="store_const", const=INFO, dest="verbose",
66+
action="store_const", const=WARNING, dest="verbose",
6667
help="Print all warnings.")
6768
parser.add_option("--noisy",
6869
action="store_const", const=DEBUG, dest="verbose",
@@ -91,14 +92,21 @@ def parse_options(args=None, values=None):
9192
e.args = (message,) + e.args[1:]
9293
raise
9394

94-
return {'input': input_file,
95-
'output': options.filename,
96-
'safe_mode': options.safe,
97-
'extensions': options.extensions,
98-
'extension_configs': extension_configs,
99-
'encoding': options.encoding,
100-
'output_format': options.output_format,
101-
'lazy_ol': options.lazy_ol}, options.verbose
95+
opts = {
96+
'input': input_file,
97+
'output': options.filename,
98+
'extensions': options.extensions,
99+
'extension_configs': extension_configs,
100+
'encoding': options.encoding,
101+
'output_format': options.output_format,
102+
'lazy_ol': options.lazy_ol
103+
}
104+
105+
if options.safe:
106+
# Avoid deprecation warning if user didn't set option
107+
opts['safe_mode'] = options.safe
108+
109+
return opts, options.verbose
102110

103111

104112
def run(): # pragma: no cover
@@ -109,14 +117,20 @@ def run(): # pragma: no cover
109117
if not options:
110118
sys.exit(2)
111119
logger.setLevel(logging_level)
112-
logger.addHandler(logging.StreamHandler())
120+
console_handler = logging.StreamHandler()
121+
logger.addHandler(console_handler)
122+
if logging_level <= WARNING:
123+
# Ensure deprecation warnings get displayed
124+
warnings.filterwarnings('default')
125+
logging.captureWarnings(True)
126+
warn_logger = logging.getLogger('py.warnings')
127+
warn_logger.addHandler(console_handler)
113128

114129
# Run
115130
markdown.markdownFromFile(**options)
116131

117132

118133
if __name__ == '__main__': # pragma: no cover
119134
# Support running module as a commandline command.
120-
# Python 2.5 & 2.6 do: `python -m markdown.__main__ [options] [args]`.
121135
# Python 2.7 & 3.x do: `python -m markdown [options] [args]`.
122136
run()

markdown/extensions/headerid.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,8 @@
2121
from ..treeprocessors import Treeprocessor
2222
from ..util import parseBoolValue
2323
from .toc import slugify, unique, stashedHTML2text
24-
import logging
2524
import warnings
2625

27-
logger = logging.getLogger('MARKDOWN')
28-
logging.captureWarnings(True)
29-
3026

3127
class HeaderIdTreeprocessor(Treeprocessor):
3228
""" Assign IDs to headers. """

tests/test_apis.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import markdown
1616
import warnings
1717
from markdown.__main__ import parse_options
18-
from logging import DEBUG, INFO, CRITICAL
18+
from logging import DEBUG, WARNING, CRITICAL
1919
import yaml
2020
import tempfile
2121

@@ -613,7 +613,6 @@ def setUp(self):
613613
'input': None,
614614
'output': None,
615615
'encoding': None,
616-
'safe_mode': False,
617616
'output_format': 'xhtml1',
618617
'lazy_ol': True,
619618
'extensions': [],
@@ -636,7 +635,7 @@ def testQuietOption(self):
636635

637636
def testVerboseOption(self):
638637
options, logging_level = parse_options(['-v'])
639-
self.assertEqual(logging_level, INFO)
638+
self.assertEqual(logging_level, WARNING)
640639

641640
def testNoisyOption(self):
642641
options, logging_level = parse_options(['--noisy'])

0 commit comments

Comments
 (0)