Skip to content

Commit 7a2dfb2

Browse files
committed
examples: Python 3 support
1 parent c805f23 commit 7a2dfb2

13 files changed

Lines changed: 102 additions & 64 deletions

examples/childwin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
# along with this program; if not, write to the Free Software
2020
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2121

22+
# Python 2/3 compatibility.
23+
from __future__ import print_function
2224

2325
import sys
2426
import os
@@ -127,7 +129,7 @@ def loop(self):
127129
# Button released, add or subtract
128130
elif e.type == X.ButtonRelease:
129131
if e.detail == 1:
130-
print "Moving child window."
132+
print("Moving child window.")
131133
self.childWindow.configure(
132134
x=e.event_x - self.childWidth // 2,
133135
y=e.event_y - self.childHeight // 2

examples/eventthread.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
# along with this program; if not, write to the Free Software
2020
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2121

22+
# Python 2/3 compatibility.
23+
from __future__ import print_function
24+
2225
import sys
2326
import os
2427

@@ -44,7 +47,7 @@ def __init__(self, display):
4447
def run(self):
4548
while True:
4649
event = self.display.next_event()
47-
print 'event: %r' % event
50+
print('event: %r' % event)
4851

4952

5053
def main(argv):
@@ -60,7 +63,7 @@ def main(argv):
6063
# call in the thread.
6164
atom = display.intern_atom('_XROOTPMAP_ID', True)
6265
response = screen.root.get_property(atom, Xatom.PIXMAP, 0, 1)
63-
print 'get_property response: %r' % response
66+
print('get_property response: %r' % response)
6467

6568
display.close()
6669

examples/profilex.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
# the profile stats file to generate.
55

66

7+
# Python 2/3 compatibility.
8+
from __future__ import print_function
9+
710
import sys
811
import os
912

@@ -19,9 +22,9 @@ def dostuff():
1922
r = d.screen().root
2023
cm = d.screen().default_colormap
2124

22-
for i in xrange(0, 1000):
25+
for i in range(0, 1000):
2326
if i % 50 == 0:
24-
print 'Iteration', i
27+
print('Iteration', i)
2528

2629
r.delete_property(Xatom.WM_NORMAL_HINTS)
2730
r.delete_property(Xatom.WM_NORMAL_HINTS)
@@ -40,4 +43,4 @@ def main(filename):
4043
if len(sys.argv) == 2:
4144
main(sys.argv[1])
4245
else:
43-
print sys.argv[0], "<filename to write profile output to>"
46+
print(sys.argv[0], "<filename to write profile output to>")

examples/record_demo.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
# Not very much unlike the xmacrorec2 program in the xmacro package.
2323

2424

25+
# Python 2/3 compatibility.
26+
from __future__ import print_function
27+
2528
import sys
2629
import os
2730

@@ -45,7 +48,7 @@ def record_callback(reply):
4548
if reply.category != record.FromServer:
4649
return
4750
if reply.client_swapped:
48-
print "* received swapped protocol data, cowardly ignored"
51+
print("* received swapped protocol data, cowardly ignored")
4952
return
5053
if not len(reply.data) or ord(reply.data[0]) < 2:
5154
# not an event
@@ -60,28 +63,28 @@ def record_callback(reply):
6063

6164
keysym = local_dpy.keycode_to_keysym(event.detail, 0)
6265
if not keysym:
63-
print "KeyCode%s" % pr, event.detail
66+
print("KeyCode%s" % pr, event.detail)
6467
else:
65-
print "KeyStr%s" % pr, lookup_keysym(keysym)
68+
print("KeyStr%s" % pr, lookup_keysym(keysym))
6669

6770
if event.type == X.KeyPress and keysym == XK.XK_Escape:
6871
local_dpy.record_disable_context(ctx)
6972
local_dpy.flush()
7073
return
7174
elif event.type == X.ButtonPress:
72-
print "ButtonPress", event.detail
75+
print("ButtonPress", event.detail)
7376
elif event.type == X.ButtonRelease:
74-
print "ButtonRelease", event.detail
77+
print("ButtonRelease", event.detail)
7578
elif event.type == X.MotionNotify:
76-
print "MotionNotify", event.root_x, event.root_y
79+
print("MotionNotify", event.root_x, event.root_y)
7780

7881

7982
# Check if the extension is present
8083
if not record_dpy.has_extension("RECORD"):
81-
print "RECORD extension not found"
84+
print("RECORD extension not found")
8285
sys.exit(1)
8386
r = record_dpy.record_get_version(0, 0)
84-
print "RECORD extension version %d.%d" % (r.major_version, r.minor_version)
87+
print("RECORD extension version %d.%d" % (r.major_version, r.minor_version))
8588

8689
# Create a recording context; we only want key and mouse events
8790
ctx = record_dpy.record_create_context(

examples/run_examples.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
# Suite 330,
2525
# Boston, MA 02111-1307 USA
2626

27+
# Python 2/3 compatibility.
28+
from __future__ import print_function
29+
2730
import sys
2831
import os
2932
import subprocess

examples/security.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
# along with this program; if not, write to the Free Software
2020
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2121

22+
# Python 2/3 compatibility.
23+
from __future__ import print_function
24+
2225
import sys, os
2326
from optparse import OptionParser
2427

@@ -51,14 +54,14 @@ def main(argv):
5154

5255
if not display.has_extension('SECURITY'):
5356
if display.query_extension('SECURITY') is None:
54-
print >>sys.stderr, 'SECURITY extension not supported'
57+
print('SECURITY extension not supported', file=sys.stderr)
5558
return 1
5659

5760
security_version = display.security_query_version()
58-
print >>sys.stderr, 'Found SECURITY version %s.%s' % (
61+
print('SECURITY version %s.%s' % (
5962
security_version.major_version,
6063
security_version.minor_version,
61-
)
64+
), file=sys.stderr)
6265

6366
if opts.generate:
6467
kwargs = {}
@@ -67,7 +70,7 @@ def main(argv):
6770
elif opts.untrusted:
6871
kwargs['trust_level'] = security.SecurityClientUntrusted
6972
reply = display.security_generate_authorization(opts.proto, **kwargs)
70-
print reply.authid
73+
print(reply.authid)
7174

7275
elif opts.revoke:
7376
for arg in args:

examples/shapewin.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2020

2121

22+
# Python 2/3 compatibility.
23+
from __future__ import print_function
24+
2225
import sys
2326
import os
2427

@@ -39,9 +42,9 @@ def __init__(self, display):
3942
% sys.argv[1])
4043
sys.exit(1)
4144

42-
# print version
45+
# print(version)
4346
r = self.d.shape_query_version()
44-
print 'SHAPE version %d.%d' % (r.major_version, r.minor_version)
47+
print('SHAPE version %d.%d' % (r.major_version, r.minor_version))
4548

4649

4750
# Find which screen to open the window on
@@ -163,7 +166,7 @@ def loop(self):
163166

164167
# Shape has changed
165168
elif e.type == self.d.extension_event.ShapeNotify:
166-
print 'Shape change'
169+
print('Shape change')
167170

168171
# Somebody wants to tell us something
169172
elif e.type == X.ClientMessage:

examples/threadtest.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/python
22

3+
# Python 2/3 compatibility.
4+
from __future__ import print_function
5+
36
import sys
47
import os
58

@@ -17,7 +20,7 @@ def redraw(win, gc):
1720
def blink(display, win, gc, cols):
1821
while 1:
1922
time.sleep(2)
20-
print 'Changing color', cols[0]
23+
print('Changing color', cols[0])
2124
gc.change(foreground = cols[0])
2225
cols = (cols[1], cols[0])
2326
redraw(win, gc)

examples/xfixes.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
# along with this program; if not, write to the Free Software
2020
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2121

22+
# Python 2/3 compatibility.
23+
from __future__ import print_function
24+
2225
import sys
2326
import os
2427
import time
@@ -34,24 +37,24 @@ def main(argv):
3437

3538
if not display.has_extension('XFIXES'):
3639
if display.query_extension('XFIXES') is None:
37-
print >>sys.stderr, 'XFIXES extension not supported'
40+
print('XFIXES extension not supported', file=sys.stderr)
3841
return 1
3942

4043
xfixes_version = display.xfixes_query_version()
41-
print >>sys.stderr, 'Found XFIXES version %s.%s' % (
44+
print('Found XFIXES version %s.%s' % (
4245
xfixes_version.major_version,
4346
xfixes_version.minor_version,
44-
)
47+
), file=sys.stderr)
4548

4649
screen = display.screen()
4750

48-
print >>sys.stderr, 'Hiding cursor ...'
51+
print('Hiding cursor ...', file=sys.stderr)
4952
screen.root.xfixes_hide_cursor()
5053
display.sync()
5154

5255
time.sleep(5)
5356

54-
print >>sys.stderr, 'Showing cursor ...'
57+
print('Showing cursor ...', file=sys.stderr)
5558
screen.root.xfixes_hide_cursor()
5659
display.sync()
5760

examples/xinerama.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2020

2121

22+
# Python 2/3 compatibility.
23+
from __future__ import print_function
24+
2225
import sys
2326
import os
2427
import pprint
@@ -38,14 +41,14 @@ def __init__(self, display):
3841
if not self.d.has_extension('XINERAMA'):
3942
sys.stderr.write('%s: server does not have the XINERAMA extension\n'
4043
% sys.argv[0])
41-
print self.d.query_extension('XINERAMA')
44+
print(self.d.query_extension('XINERAMA'))
4245
sys.stderr.write("\n".join(self.d.list_extensions()))
4346
if self.d.query_extension('XINERAMA') is None:
4447
sys.exit(1)
4548

4649
# print version
4750
r = self.d.xinerama_query_version()
48-
print 'XINERAMA version %d.%d' % (r.major_version, r.minor_version)
51+
print('XINERAMA version %d.%d' % (r.major_version, r.minor_version))
4952

5053

5154
# Grab the current screen
@@ -95,23 +98,23 @@ def __init__(self, display):
9598

9699
self.pp = pprint.PrettyPrinter(indent=4)
97100

98-
print "Xinerama active:", bool(self.d.xinerama_is_active())
101+
print("Xinerama active:", bool(self.d.xinerama_is_active()))
99102

100-
print "Screen info:"
103+
print("Screen info:")
101104
self.pp.pprint(self.d.xinerama_query_screens()._data)
102105

103106
# FIXME: This doesn't work!
104-
#print "Xinerama info:"
107+
#print("Xinerama info:")
105108
#self.pp.pprint(self.d.xinerama_get_info(self.d.screen().root_visual)._data)
106109

107-
print "Xinerama state:"
110+
print("Xinerama state:")
108111
self.pp.pprint(self.window.xinerama_get_state()._data)
109112

110-
print "Screen count:"
113+
print("Screen count:")
111114
self.pp.pprint(self.window.xinerama_get_screen_count()._data)
112115

113116
for screennum in range(self.window.xinerama_get_screen_count().screen_count):
114-
print "Screen %d size:" % (screennum, )
117+
print("Screen %d size:" % (screennum, ))
115118
self.pp.pprint(self.window.xinerama_get_screen_size(screennum)._data)
116119

117120
def parseModes(self, mode_names, modes):

0 commit comments

Comments
 (0)