Skip to content

Commit a4a227e

Browse files
committed
Start work on fixing issues reported by 2to3
1 parent bafa6f6 commit a4a227e

File tree

6 files changed

+56
-29
lines changed

6 files changed

+56
-29
lines changed

examples/forms/example.py

+20-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22

3+
from __future__ import print_function
34
import sys
45

56
import mechanize
@@ -17,7 +18,7 @@
1718
# backwards_compat=False)
1819
# f.close()
1920
form = forms[0]
20-
print form # very useful!
21+
print(form) # very useful!
2122

2223
# A 'control' is a graphical HTML form widget: a text entry box, a
2324
# dropdown 'select' list, a checkbox, etc.
@@ -37,16 +38,16 @@
3738
# Add files to FILE controls with .add_file(). Only call this multiple
3839
# times if the server is expecting multiple files.
3940
# add a file, default value for MIME type, no filename sent to server
40-
form.add_file(open("data.dat"))
41+
form.add_file(open("data.dat", 'rb'))
4142
# add a second file, explicitly giving MIME type, and telling the server
4243
# what the filename is
43-
form.add_file(open("data.txt"), "text/plain", "data.txt")
44+
form.add_file(open("data.txt", 'rb'), "text/plain", "data.txt")
4445

4546
# All Controls may be disabled (equivalent of greyed-out in browser)...
4647
control = form.find_control("comments")
47-
print control.disabled
48+
print(control.disabled)
4849
# ...or readonly
49-
print control.readonly
50+
print(control.readonly)
5051
# readonly and disabled attributes can be assigned to
5152
control.disabled = False
5253
# convenience method, used here to make all controls writable (unless
@@ -80,11 +81,11 @@
8081

8182
# You can get the Control instances from inside the form...
8283
control = form.find_control("cheeses", type="select")
83-
print control.name, control.value, control.type
84+
print(control.name, control.value, control.type)
8485
control.value = ["mascarpone", "curd"]
8586
# ...and the Item instances from inside the Control
8687
item = control.get("curd")
87-
print item.name, item.selected, item.id, item.attrs
88+
print(item.name, item.selected, item.id, item.attrs)
8889
item.selected = False
8990

9091
# Controls may be referred to by label:
@@ -126,7 +127,7 @@ def control_has_caerphilly(control):
126127

127128
# Control.items is a list of all Item instances in the control
128129
for item in form.find_control("cheeses").items:
129-
print item.name
130+
print(item.name)
130131

131132
# To remove items from a list control, remove it from .items:
132133
cheeses = form.find_control("cheeses")
@@ -151,13 +152,13 @@ def control_has_caerphilly(control):
151152
# Which items are present, selected, and successful?
152153
# is the "parmesan" item of the "cheeses" control successful (selected
153154
# and not disabled)?
154-
print "parmesan" in form["cheeses"]
155+
print("parmesan" in form["cheeses"])
155156
# is the "parmesan" item of the "cheeses" control selected?
156-
print "parmesan" in [
157-
item.name for item in form.find_control("cheeses").items if item.selected]
157+
print("parmesan" in [
158+
item.name for item in form.find_control("cheeses").items if item.selected])
158159
# does cheeses control have a "caerphilly" item?
159-
print "caerphilly" in [
160-
item.name for item in form.find_control("cheeses").items]
160+
print("caerphilly" in [
161+
item.name for item in form.find_control("cheeses").items])
161162

162163
# Sometimes one wants to set or clear individual items in a list, rather
163164
# than setting the whole .value:
@@ -179,21 +180,21 @@ def control_has_caerphilly(control):
179180
# Items may be disabled (selecting or de-selecting a disabled item is
180181
# not allowed):
181182
control = form.find_control("cheeses")
182-
print control.get("emmenthal").disabled
183+
print(control.get("emmenthal").disabled)
183184
control.get("emmenthal").disabled = True
184185
# enable all items in control
185186
control.set_all_items_disabled(False)
186187

187188
request2 = form.click() # mechanize.Request object
188189
try:
189190
response2 = mechanize.urlopen(request2)
190-
except mechanize.HTTPError, response2:
191+
except mechanize.HTTPError as response2:
191192
pass
192193

193-
print response2.geturl()
194+
print(response2.geturl())
194195
# headers
195-
for name, value in response2.info().items():
196+
for name, value in list(response2.info().items()):
196197
if name != "date":
197-
print "%s: %s" % (name.title(), value)
198-
print response2.read() # body
198+
print("%s: %s" % (name.title(), value))
199+
print(response2.read()) # body
199200
response2.close()

examples/forms/simple.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22

3+
from __future__ import print_function
34
import sys
45

56
from mechanize import Browser, urljoin, urlopen

examples/hack21.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
#/usr/bin/env python
1+
#!/usr/bin/env python
22

33
# Port of Hack 21 from the O'Reilly book "Spidering Hacks" by Tara
44
# Calishain and Kevin Hemenway. Of course, there's no need to explicitly
55
# catch exceptions in Python, unlike checking error return values in Perl,
66
# but I've left those in for the sake of a direct port.
77

8+
from __future__ import print_function
89
import sys
910
import os
1011
import re
11-
from urllib2 import HTTPError
1212

1313
import mechanize
14-
assert mechanize.__version__ >= (0, 0, 6, "a")
14+
from mechanize.polyglot import HTTPError
1515

1616
mech = mechanize.Browser()
1717
# Addition 2005-01-05: Be naughty, since robots.txt asks not to
@@ -44,20 +44,20 @@
4444
# Get all the tarballs
4545
urls = [link.absolute_url for link in
4646
mech.links(url_regex=re.compile(r"\.tar\.gz$"))]
47-
print "Found", len(urls), "tarballs to download"
47+
print("Found", len(urls), "tarballs to download")
4848

4949
if "--all" not in sys.argv[1:]:
5050
urls = urls[:1]
5151

5252
for url in urls:
5353
filename = os.path.basename(url)
5454
f = open(filename, "wb")
55-
print "%s -->" % filename,
55+
print("%s -->" % filename, end=' ')
5656
r = mech.open(url)
5757
while 1:
5858
data = r.read(1024)
5959
if not data:
6060
break
6161
f.write(data)
6262
f.close()
63-
print os.stat(filename).st_size, "bytes"
63+
print(os.stat(filename).st_size, "bytes")

examples/pypi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
import sys
99
import os
10-
import urlparse
1110

1211
import mechanize
12+
from mechanize.polyglot import urlsplit
1313

1414

1515
def download_mechanize():
@@ -22,7 +22,7 @@ def download_mechanize():
2222
browser.submit()
2323
browser.follow_link(text_regex="mechanize-?(.*)")
2424
link = browser.find_link(text_regex=r"\.tar\.gz")
25-
filename = os.path.basename(urlparse.urlsplit(link.url)[2])
25+
filename = os.path.basename(urlsplit(link.url)[2])
2626
if os.path.exists(filename):
2727
sys.exit("%s already exists, not grabbing" % filename)
2828
browser.retrieve(link.url, filename)

mechanize/polyglot.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python2
2+
# vim:fileencoding=utf-8
3+
# License: GPLv3 Copyright: 2018, Kovid Goyal <kovid at kovidgoyal.net>
4+
5+
from __future__ import (absolute_import, division, print_function,
6+
unicode_literals)
7+
8+
import sys
9+
10+
is_py2 = sys.version_info.major < 3
11+
12+
if is_py2:
13+
from urllib2 import HTTPError
14+
from urlparse import urlsplit
15+
else:
16+
from urllib.error import HTTPError
17+
from urllib.parse import urlsplit
18+
if False:
19+
HTTPError, urlsplit

publish.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,18 @@ def upload_release():
5858
os.path.join(os.environ['PENV'], 'pypi'), *files)
5959

6060

61+
try:
62+
myinput = raw_input
63+
except NameError:
64+
myinput = input
65+
66+
6167
def main():
62-
if raw_input('Publish version {} [y/n]? '.format(red(VERSION))) != 'y':
68+
if myinput('Publish version {} [y/n]? '.format(red(VERSION))) != 'y':
6369
raise SystemExit(1)
6470
build_release()
6571
sign_release()
66-
if raw_input(red('Upload') + ' release [y/n]? ') != 'y':
72+
if myinput(red('Upload') + ' release [y/n]? ') != 'y':
6773
raise SystemExit(1)
6874
tag_release()
6975
upload_release()

0 commit comments

Comments
 (0)