Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

cleanup toplevel namespace

Daniel Graña9a643d689c96
diff --git a/scrapy/__init__.py b/scrapy/__init__.py
index 019179cfd..9917f70e8 100644
--- a/scrapy/__init__.py
+++ b/scrapy/__init__.py
@@ -1,40 +1,45 @@
"""
Scrapy - a screen scraping framework written in Python
"""
-from __future__ import print_function
+
+__all__ = ['__version__', 'version_info', 'optional_features', 'twisted_version']
+
+# Scrapy version
import pkgutil
-__version__ = pkgutil.get_data(__package__, 'VERSION').strip()
-if not isinstance(__version__, str):
- __version__ = __version__.decode('ascii')
+__version__ = pkgutil.get_data(__package__, 'VERSION').decode('ascii').strip()
version_info = tuple(int(v) for v in __version__.split('.')[:3])
+del pkgutil
-import sys, os, warnings
-
+# Check minimum required Python version
+import sys
if sys.version_info < (2, 7):
print("Scrapy %s requires Python 2.7" % __version__)
sys.exit(1)
+del sys
-# ignore noisy twisted deprecation warnings
+# Ignore noisy twisted deprecation warnings
+import warnings
warnings.filterwarnings('ignore', category=DeprecationWarning, module='twisted')
+del warnings
-# monkey patches to fix external library issues
-from scrapy.xlib import urlparse_monkeypatches
+# Apply monkey patches to fix issues in external libraries
+import _monkeypatches
+del _monkeypatches
# WARNING: optional_features set is deprecated and will be removed soon. Do not use.
optional_features = set()
-
# TODO: backwards compatibility, remove for Scrapy 0.20
optional_features.add('ssl')
-
try:
import boto
+ del boto
except ImportError:
pass
else:
optional_features.add('boto')
-
try:
import django
+ del django
except ImportError:
pass
else:
diff --git a/scrapy/xlib/urlparse_monkeypatches.py b/scrapy/_monkeypatches.py
similarity index 100%
rename from scrapy/xlib/urlparse_monkeypatches.py
rename to scrapy/_monkeypatches.py
diff --git a/scrapy/tests/test_toplevel.py b/scrapy/tests/test_toplevel.py
new file mode 100644
index 000000000..5d014ce6f
--- /dev/null
+++ b/scrapy/tests/test_toplevel.py
@@ -0,0 +1,16 @@
+from unittest import TestCase
+import six
+import scrapy
+
+
+class ToplevelTestCase(TestCase):
+
+ def test_version(self):
+ self.assertIs(type(scrapy.__version__), six.text_type)
+
+ def test_version_info(self):
+ self.assertIs(type(scrapy.version_info), tuple)
+
+ def test_optional_features(self):
+ self.assertIs(type(scrapy.optional_features), set)
+ self.assertIn('ssl', scrapy.optional_features)