summaryrefslogtreecommitdiff
path: root/bs4
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2016-07-16 12:36:21 -0400
committerLeonard Richardson <leonardr@segfault.org>2016-07-16 12:36:21 -0400
commit920f7750783908d6a6be9e3d1f5db45efb9003ee (patch)
treea2df2aefa051a933f4089c9ecc912293d4f8fae2 /bs4
parent682233e4c9e9fa11b5b84055e0bb272f5e941194 (diff)
Specify the file and line number when warning about a
BeautifulSoup object being instantiated without a parser being specified. [bug=1574647]
Diffstat (limited to 'bs4')
-rw-r--r--bs4/__init__.py9
1 files changed, 8 insertions, 1 deletions
diff --git a/bs4/__init__.py b/bs4/__init__.py
index 688378a..351c32c 100644
--- a/bs4/__init__.py
+++ b/bs4/__init__.py
@@ -28,6 +28,7 @@ __all__ = ['BeautifulSoup']
import os
import re
+import traceback
import warnings
from .builder import builder_registry, ParserRejectedMarkup
@@ -80,7 +81,7 @@ class BeautifulSoup(Tag):
ASCII_SPACES = '\x20\x0a\x09\x0c\x0d'
- NO_PARSER_SPECIFIED_WARNING = "No parser was explicitly specified, so I'm using the best available %(markup_type)s parser for this system (\"%(parser)s\"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.\n\nTo get rid of this warning, change this:\n\n BeautifulSoup([your markup])\n\nto this:\n\n BeautifulSoup([your markup], \"%(parser)s\")\n"
+ NO_PARSER_SPECIFIED_WARNING = "No parser was explicitly specified, so I'm using the best available %(markup_type)s parser for this system (\"%(parser)s\"). This usually isn't a problem, but if you run this code on another system, or in a different virtual environment, it may use a different parser and behave differently.\n\nThe code that caused this warning is on line %(line_number)s of the file %(filename)s. To get rid of this warning, change code that looks like this:\n\n BeautifulSoup([your markup])\n\nto this:\n\n BeautifulSoup([your markup], \"%(parser)s\")\n"
def __init__(self, markup="", features=None, builder=None,
parse_only=None, from_encoding=None, exclude_encodings=None,
@@ -164,7 +165,13 @@ class BeautifulSoup(Tag):
markup_type = "XML"
else:
markup_type = "HTML"
+
+ caller = traceback.extract_stack()[0]
+ filename = caller[0]
+ line_number = caller[1]
warnings.warn(self.NO_PARSER_SPECIFIED_WARNING % dict(
+ filename=filename,
+ line_number=line_number,
parser=builder.NAME,
markup_type=markup_type))