summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2018-07-14 16:18:43 -0400
committerLeonard Richardson <leonardr@segfault.org>2018-07-14 16:18:43 -0400
commitbea685468cc5cb4a940a5f1bdf39f39ea947fda3 (patch)
tree373b5ed9a589975605855527ad82a6090ae807d2
parentd241e5d5a9e9c01d88b3711a82a6114f846ffdb1 (diff)
Improve the technique for finding the line number with the problematic method call.
-rw-r--r--bs4/__init__.py16
1 files changed, 13 insertions, 3 deletions
diff --git a/bs4/__init__.py b/bs4/__init__.py
index e184cce..297e4b7 100644
--- a/bs4/__init__.py
+++ b/bs4/__init__.py
@@ -29,6 +29,7 @@ __all__ = ['BeautifulSoup']
import os
import re
+import sys
import traceback
import warnings
@@ -203,9 +204,18 @@ class BeautifulSoup(Tag):
else:
markup_type = "HTML"
- caller = traceback.extract_stack()[0]
- filename = caller[0]
- line_number = caller[1]
+ # This code taken 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).
+ try:
+ caller = sys._getframe(2)
+ except ValueError:
+ globals = sys.__dict__
+ lineno = 1
+ else:
+ globals = caller.f_globals
+ line_number = caller.f_lineno
+ filename = globals.get('__file__')
values = dict(
filename=filename,
line_number=line_number,