Skip to content

Commit

Permalink
Add python 3 support - closes python-mechanize#9
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolas committed Dec 17, 2018
1 parent 6dbb1c5 commit d97e693
Show file tree
Hide file tree
Showing 29 changed files with 83 additions and 80 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ language: python
python:
# - 2.6 disabled as the TestLoader.discover() method is not available on python < 2.7
- 2.7
- 3.6
- pypy

matrix:
Expand Down
2 changes: 1 addition & 1 deletion mechanize/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import absolute_import


import logging

Expand Down
2 changes: 1 addition & 1 deletion mechanize/_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

from __future__ import absolute_import

from ._urllib2_fork import HTTPPasswordMgr
from .polyglot import is_string, iteritems

Expand Down
2 changes: 1 addition & 1 deletion mechanize/_clientcookie.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import absolute_import


import re
import time
Expand Down
2 changes: 1 addition & 1 deletion mechanize/_debug.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import absolute_import


import logging

Expand Down
58 changes: 29 additions & 29 deletions mechanize/_entities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: BSD Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>

Expand All @@ -16,7 +16,7 @@
'acute': '´',
'acy': 'а',
'aelig': 'æ',
'af': '\u2061',
'af': '\\u2061',
'afr': '𝔞',
'agrave': 'à',
'alefsym': 'ℵ',
Expand Down Expand Up @@ -55,7 +55,7 @@
'ape': '≊',
'apid': '≋',
'apos': "'",
'applyfunction': '\u2061',
'applyfunction': '\\u2061',
'approx': '≈',
'approxeq': '≊',
'aring': 'å',
Expand Down Expand Up @@ -390,11 +390,11 @@
'emptysmallsquare': '◻',
'emptyv': '∅',
'emptyverysmallsquare': '▫',
'emsp': '\u2003',
'emsp13': '\u2004',
'emsp14': '\u2005',
'emsp': '\\u2003',
'emsp13': '\\u2004',
'emsp14': '\\u2005',
'eng': 'ŋ',
'ensp': '\u2002',
'ensp': '\\u2002',
'eogon': 'ę',
'eopf': '𝕖',
'epar': '⋕',
Expand Down Expand Up @@ -534,7 +534,7 @@
'gvertneqq': '≩︀',
'gvne': '≩︀',
'hacek': 'ˇ',
'hairsp': '\u200a',
'hairsp': '\\u200a',
'half': '½',
'hamilt': 'ℋ',
'hardcy': 'ъ',
Expand Down Expand Up @@ -567,7 +567,7 @@
'hybull': '⁃',
'hyphen': '‐',
'iacute': 'í',
'ic': '\u2063',
'ic': '\\u2063',
'icirc': 'î',
'icy': 'и',
'idot': 'İ',
Expand Down Expand Up @@ -605,8 +605,8 @@
'intersection': '⋂',
'intlarhk': '⨗',
'intprod': '⨼',
'invisiblecomma': '\u2063',
'invisibletimes': '\u2062',
'invisiblecomma': '\\u2063',
'invisibletimes': '\\u2062',
'iocy': 'ё',
'iogon': 'į',
'iopf': '𝕚',
Expand All @@ -620,7 +620,7 @@
'isins': '⋴',
'isinsv': '⋳',
'isinv': '∈',
'it': '\u2062',
'it': '\\u2062',
'itilde': 'ĩ',
'iukcy': 'і',
'iuml': 'ï',
Expand Down Expand Up @@ -792,7 +792,7 @@
'lrcorner': '⌟',
'lrhar': '⇋',
'lrhard': '⥭',
'lrm': '\u200e',
'lrm': '\\u200e',
'lrtri': '⊿',
'lsaquo': '‹',
'lscr': '𝓁',
Expand Down Expand Up @@ -835,7 +835,7 @@
'mdash': '—',
'mddot': '∺',
'measuredangle': '∡',
'mediumspace': '\u205f',
'mediumspace': '\\u205f',
'mellintrf': 'ℳ',
'mfr': '𝔪',
'mho': '℧',
Expand Down Expand Up @@ -887,10 +887,10 @@
'nearr': '↗',
'nearrow': '↗',
'nedot': '≐̸',
'negativemediumspace': '\u200b',
'negativethickspace': '\u200b',
'negativethinspace': '\u200b',
'negativeverythinspace': '\u200b',
'negativemediumspace': '\\u200b',
'negativethickspace': '\\u200b',
'negativethinspace': '\\u200b',
'negativeverythinspace': '\\u200b',
'nequiv': '≢',
'nesear': '⤨',
'nesim': '≂̸',
Expand Down Expand Up @@ -934,7 +934,7 @@
'nltrie': '⋬',
'nltv': '≪̸',
'nmid': '∤',
'nobreak': '\u2060',
'nobreak': '\\u2060',
'nonbreakingspace': '\xa0',
'nopf': '𝕟',
'not': '¬',
Expand Down Expand Up @@ -1050,7 +1050,7 @@
'nu': 'ν',
'num': '#',
'numero': '№',
'numsp': '\u2007',
'numsp': '\\u2007',
'nvap': '≍⃒',
'nvdash': '⊬',
'nvge': '≥⃒',
Expand Down Expand Up @@ -1201,7 +1201,7 @@
'prurel': '⊰',
'pscr': '𝓅',
'psi': 'ψ',
'puncsp': '\u2008',
'puncsp': '\\u2008',
'qfr': '𝔮',
'qint': '⨌',
'qopf': '𝕢',
Expand Down Expand Up @@ -1307,7 +1307,7 @@
'risingdotseq': '≓',
'rlarr': '⇄',
'rlhar': '⇌',
'rlm': '\u200f',
'rlm': '\\u200f',
'rmoust': '⎱',
'rmoustache': '⎱',
'rnmid': '⫮',
Expand Down Expand Up @@ -1521,9 +1521,9 @@
'thetav': 'ϑ',
'thickapprox': '≈',
'thicksim': '∼',
'thickspace': '\u205f\u200a',
'thinsp': '\u2009',
'thinspace': '\u2009',
'thickspace': '\\u205f\\u200a',
'thinsp': '\\u2009',
'thinspace': '\\u2009',
'thkap': '≈',
'thksim': '∼',
'thorn': 'þ',
Expand Down Expand Up @@ -1659,7 +1659,7 @@
'verticalline': '|',
'verticalseparator': '❘',
'verticaltilde': '≀',
'verythinspace': '\u200a',
'verythinspace': '\\u200a',
'vfr': '𝔳',
'vltri': '⊲',
'vnsub': '⊂⃒',
Expand Down Expand Up @@ -1720,15 +1720,15 @@
'zcy': 'з',
'zdot': 'ż',
'zeetrf': 'ℨ',
'zerowidthspace': '\u200b',
'zerowidthspace': '\\u200b',
'zeta': 'ζ',
'zfr': '𝔷',
'zhcy': 'ж',
'zigrarr': '⇝',
'zopf': '𝕫',
'zscr': '𝓏',
'zwj': '\u200d',
'zwnj': '\u200c',
'zwj': '\\u200d',
'zwnj': '\\u200c',
# }}}
}

Expand Down
6 changes: 3 additions & 3 deletions mechanize/_equiv.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python2
#!/usr/bin/env python
# vim:fileencoding=utf-8
# License: BSD Copyright: 2017, Kovid Goyal <kovid at kovidgoyal.net>

Expand All @@ -11,7 +11,7 @@
from ._entities import html5_entities
from .polyglot import codepoint_to_chr

space_chars = frozenset(("\t", "\n", "\u000C", " ", "\r"))
space_chars = frozenset(("\t", "\n", "\\u000C", " ", "\r"))
space_chars_bytes = frozenset(item.encode("ascii") for item in space_chars)
ascii_letters_bytes = frozenset(
item.encode("ascii") for item in string.ascii_letters)
Expand Down Expand Up @@ -75,7 +75,7 @@ def __next__(self):
raise TypeError
return self[p:p + 1]

def next(self):
def __next__(self):

This comment has been minimized.

Copy link
@jellonek

jellonek Jan 5, 2019

Clearly it's a bug.

This comment has been minimized.

Copy link
@nikolas

nikolas Jan 6, 2019

Author Owner

What's a bug? Do you have a suggestion here? I don't really know about the next method.

# Py2 compat
return self.__next__()

Expand Down
6 changes: 3 additions & 3 deletions mechanize/_form.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import absolute_import


import re
from collections import defaultdict
Expand All @@ -13,7 +13,7 @@ class SkipControl(ValueError):


def normalize_line_endings(text):
return re.sub(ur"(?:(?<!\r)\n)|(?:\r(?!\n))", u"\r\n", text)
return re.sub(r"(?:(?<!\r)\n)|(?:\r(?!\n))", "\r\n", text)


def label_text(elem):
Expand Down Expand Up @@ -66,7 +66,7 @@ def parse_option(elem, parent_of, attrs_map):
def parse_textarea(elem, parent_of, *a):
ctype, name, attrs = parse_control(elem, parent_of)
ctype = 'textarea'
attrs['value'] = normalize_line_endings(elem.text or u'')
attrs['value'] = normalize_line_endings(elem.text or '')
return ctype, name, attrs


Expand Down
6 changes: 3 additions & 3 deletions mechanize/_form_controls.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python2
#!/usr/bin/env python
# vim:fileencoding=utf-8
from __future__ import absolute_import


import random
import re
Expand All @@ -12,7 +12,7 @@
from .polyglot import urlparse, urlunparse, urlencode, is_py2, iteritems

if is_py2:
from cStringIO import StringIO
from io import StringIO

This comment has been minimized.

Copy link
@jellonek

jellonek Jan 5, 2019

Seriously?

This comment has been minimized.

Copy link
@nikolas

nikolas Jan 5, 2019

Author Owner

Looks like I overlooked this work that was done by 2to3. Thx for pointing it out, fixed now.

else:
from io import StringIO # 2to3: probably broken when writing bytes

Expand Down
8 changes: 4 additions & 4 deletions mechanize/_gzip.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import absolute_import


import struct
import zlib
Expand All @@ -10,7 +10,7 @@

CRC_MASK = 0xffffffff
if is_py2:
CRC_MASK = long(CRC_MASK)
CRC_MASK = int(CRC_MASK)


def gzip_prefix():
Expand Down Expand Up @@ -141,7 +141,7 @@ def __iter__(self):
if ans:
yield ans

def next(self):
def __next__(self):
ans = self.readline()
if not ans:
raise StopIteration()
Expand Down Expand Up @@ -191,7 +191,7 @@ def http_request(self, request):
if sum('gzip' in x for x in existing) < 1:
existing.append('gzip')
request.add_header("Accept-Encoding",
', '.join(filter(None, existing)))
', '.join([_f for _f in existing if _f]))
return request

def http_response(self, request, response):
Expand Down
2 changes: 1 addition & 1 deletion mechanize/_headersutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""

from __future__ import absolute_import


import os
import re
Expand Down
8 changes: 4 additions & 4 deletions mechanize/_html.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from __future__ import absolute_import


import codecs
import copy
Expand Down Expand Up @@ -35,8 +35,8 @@ def iterlinks(root, base_url):
if val:
url = clean_url(val)
yield Link(base_url, url,
compress_whitespace(u''.join(elem_text(tag))), q,
tag.items())
compress_whitespace(''.join(elem_text(tag))), q,
list(tag.items()))
elif q == 'base':
href = tag.get('href')
if href:
Expand Down Expand Up @@ -258,7 +258,7 @@ def title(self):
if self._current_title is lazy:
self._current_title = get_title(
self.root) if self.root is not None else None
return self._current_title or u''
return self._current_title or ''

@property
def global_form(self):
Expand Down
2 changes: 1 addition & 1 deletion mechanize/_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""

from __future__ import absolute_import


import logging
import socket
Expand Down
2 changes: 1 addition & 1 deletion mechanize/_mechanize.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
included with the distribution).
"""
from __future__ import absolute_import


import copy
import os
Expand Down
2 changes: 1 addition & 1 deletion mechanize/_opener.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""

from __future__ import absolute_import


import bisect
import os
Expand Down
2 changes: 1 addition & 1 deletion mechanize/_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"""

from __future__ import absolute_import

import logging

from . import _rfc3986
Expand Down
Loading

0 comments on commit d97e693

Please sign in to comment.