summaryrefslogtreecommitdiff
path: root/bs4/__init__.py
diff options
context:
space:
mode:
authorLeonard Richardson <leonardr@segfault.org>2021-02-13 10:39:15 -0500
committerLeonard Richardson <leonardr@segfault.org>2021-02-13 10:39:15 -0500
commit0986e7bb25c46c2c9c1787e474e81de157ecfedf (patch)
tree939b1ff73887f66ff92ead5df6bb0810f3463d02 /bs4/__init__.py
parent316093c577193e5e604bb90a16d520e2f5c1b60b (diff)
Improve the warning issued when a directory name (as opposed to
the name of a regular file) is passed as markup into the BeautifulSoup constructor. [bug=1913628]
Diffstat (limited to 'bs4/__init__.py')
-rw-r--r--bs4/__init__.py14
1 files changed, 13 insertions, 1 deletions
diff --git a/bs4/__init__.py b/bs4/__init__.py
index 8f78809..e33f62a 100644
--- a/bs4/__init__.py
+++ b/bs4/__init__.py
@@ -321,14 +321,26 @@ class BeautifulSoup(Tag):
else:
possible_filename = markup
is_file = False
+ is_directory = False
try:
is_file = os.path.exists(possible_filename)
+ if is_file:
+ is_directory = os.path.isdir(possible_filename)
except Exception, e:
# This is almost certainly a problem involving
# characters not valid in filenames on this
# system. Just let it go.
pass
- if is_file:
+ if is_directory:
+ warnings.warn(
+ '"%s" looks like a directory name, not markup. You may'
+ ' want to open a file found in this directory and pass'
+ ' the filehandle into Beautiful Soup.' % (
+ self._decode_markup(markup)
+ ),
+ MarkupResemblesLocatorWarning
+ )
+ elif is_file:
warnings.warn(
'"%s" looks like a filename, not markup. You should'
' probably open this file and pass the filehandle into'