summaryrefslogtreecommitdiff
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
commit079dbd2e8024cdd3dbb8831b3deccb852e6a8861 (patch)
treed4c4326a768c24706dff42ff643334657a4cf39f
parent110907346d9566f3af8c18e8c8a623602f1622bf (diff)
downloadbeautifulsoup4-079dbd2e8024cdd3dbb8831b3deccb852e6a8861.tar.gz
Specify the file and line number when warning about a
BeautifulSoup object being instantiated without a parser being specified. [bug=1574647]
-rw-r--r--NEWS.txt4
-rw-r--r--bs4/__init__.py9
2 files changed, 12 insertions, 1 deletions
diff --git a/NEWS.txt b/NEWS.txt
index 472538a..d81e3f2 100644
--- a/NEWS.txt
+++ b/NEWS.txt
@@ -5,6 +5,10 @@
* The contents of <textarea> tags will no longer be modified when the
tree is prettified. [bug=1555829]
+* Specify the file and line number when warning about a
+ BeautifulSoup object being instantiated without a parser being
+ specified. [bug=1574647]
+
* Fixed a Python 3 ByteWarning when a URL was passed in as though it
were markup. Thanks to James Salter for a patch and
test. [bug=1533762]
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))