From 2b749668bad623cd5d7de19eb3044da623f8b26b Mon Sep 17 00:00:00 2001 From: Mikhail Korobov Date: Wed, 2 Jul 2014 22:48:16 +0600 Subject: [PATCH] Deprecate scrapy.python.utils.FixedSGMLParser SGML link extractor that uses it is also going to be deprecated; sgmllib module is not available in Python 3. --- scrapy/utils/python.py | 43 +++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/scrapy/utils/python.py b/scrapy/utils/python.py index 566b50f17..db88a4e55 100644 --- a/scrapy/utils/python.py +++ b/scrapy/utils/python.py @@ -10,23 +10,40 @@ import re import inspect import weakref import errno +import warnings from functools import partial, wraps -from sgmllib import SGMLParser + +import six + +from scrapy.exceptions import ScrapyDeprecationWarning -class FixedSGMLParser(SGMLParser): - """The SGMLParser that comes with Python has a bug in the convert_charref() - method. This is the same class with the bug fixed""" +if six.PY2: + from sgmllib import SGMLParser - def convert_charref(self, name): - """This method fixes a bug in Python's SGMLParser.""" - try: - n = int(name) - except ValueError: - return - if not 0 <= n <= 127 : # ASCII ends at 127, not 255 - return - return self.convert_codepoint(n) + class FixedSGMLParser(SGMLParser): + """The SGMLParser that comes with Python has a bug in the convert_charref() + method. This is the same class with the bug fixed. + + Warning: this class is deprecated and will be removed in future releases. + """ + + def __init__(self, *args, **kwargs): + warnings.warn( + "FixedSGMLParser is deprecated and will be removed in future releases.", + ScrapyDeprecationWarning + ) + SGMLParser.__init__(self, *args, **kwargs) + + def convert_charref(self, name): + """This method fixes a bug in Python's SGMLParser.""" + try: + n = int(name) + except ValueError: + return + if not 0 <= n <= 127 : # ASCII ends at 127, not 255 + return + return self.convert_codepoint(n) def flatten(x):