From d62f4adb3831ab0a8535f97fa304c87e0d871177 Mon Sep 17 00:00:00 2001 From: Leonard Richardson Date: Sun, 15 Jul 2018 08:38:28 -0400 Subject: Improved the 'no parser specified' warning so it doesn't show up in a REPL. --- bs4/__init__.py | 30 ++++++++++++++++++------------ 1 file 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 -- cgit v1.2.1