summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2018-07-15 08:38:28 -0400
committerLeonard Richardson <leonardr@segfault.org>2018-07-15 08:38:28 -0400
commitd62f4adb3831ab0a8535f97fa304c87e0d871177 (patch)
tree46da8527b5aeeb1a829080499ee6f253848fab64
parentb5ffba33327acaf51a2274f9e0a5305b5fb8bdf9 (diff)
downloadbeautifulsoup4-d62f4adb3831ab0a8535f97fa304c87e0d871177.tar.gz
Improved the 'no parser specified' warning so it doesn't show up in a REPL.
-rw-r--r--bs4/__init__.py30
1 files changed, 18 insertions, 12 deletions
diff --git a/bs4/__init__.py b/bs4/__init__.py
index a081a7d..329ef53 100644
--- a/bs4/__init__.py
+++ b/bs4/__init__.py
@@ -204,29 +204,35 @@ class BeautifulSoup(Tag):
else:
markup_type = "HTML"
- # This code taken from warnings.py so that we get the same line
+ # This code adapted from warnings.py so that we get the same line
# of code as our warnings.warn() call gets, even if the answer is wrong
# (as it may be in a multithreading situation).
+ caller = None
try:
- caller = sys._getframe(2)
+ caller = sys._getframe(1)
except ValueError:
- globals = sys.__dict__
- line_number= 1
- else:
+ pass
+ if caller:
globals = caller.f_globals
line_number = caller.f_lineno
+ else:
+ globals = sys.__dict__
+ line_number= 1
filename = globals.get('__file__')
if filename:
fnl = filename.lower()
if fnl.endswith((".pyc", ".pyo")):
filename = filename[:-1]
- values = dict(
- filename=filename,
- line_number=line_number,
- parser=builder.NAME,
- markup_type=markup_type
- )
- warnings.warn(self.NO_PARSER_SPECIFIED_WARNING % values, stacklevel=2)
+ if filename:
+ # If there is no filename at all, the user is most likely in a REPL,
+ # and the warning is not necessary.
+ values = dict(
+ filename=filename,
+ line_number=line_number,
+ parser=builder.NAME,
+ markup_type=markup_type
+ )
+ warnings.warn(self.NO_PARSER_SPECIFIED_WARNING % values, stacklevel=2)
self.builder = builder
self.is_xml = builder.is_xml