Skip to content

Commit 7360dbc

Browse files
committed
examples: fix xlsatoms
- fix Python 3 support - fix default display - enable it in run_examples.py
1 parent f65a85a commit 7360dbc

2 files changed

Lines changed: 38 additions & 30 deletions

File tree

examples/run_examples.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,9 @@ def test_xfixes(self):
7373
""" Run xfixes.py -- demonstrate the XFIXES extension """
7474
self.assertEqual(run_example(examples_folder + "xfixes.py"), 0)
7575

76-
# TODO
77-
# def test_xlsatoms(self):
78-
# """ Run xlsatoms.py -- show list atoms on X server """
79-
# self.assertEqual(run_example(examples_folder + "xlsatoms.py"), 0)
76+
def test_xlsatoms(self):
77+
""" Run xlsatoms.py -- show list atoms on X server """
78+
self.assertEqual(run_example(examples_folder + "xlsatoms.py"), 0)
8079

8180

8281
if __name__ == '__main__':

examples/xlsatoms.py

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,24 @@
3232
import sys
3333
import os
3434

35+
# Python 2/3 compatibility.
36+
from six import PY2, MAXSIZE
37+
3538
# Change path so we find Xlib
3639
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
3740

3841
import re
3942
from Xlib import display, error
4043
from optparse import OptionParser
4144

45+
46+
if PY2:
47+
integer_type = long
48+
else:
49+
integer_type = int
50+
4251
parser = OptionParser()
43-
parser.add_option("-d","--display",dest="display",help="This option specifies the X server to which to connect",metavar="dpy",default=":0.0")
52+
parser.add_option("-d","--display",dest="display",help="This option specifies the X server to which to connect",metavar="dpy",default=None)
4453
parser.add_option("-n","--name",dest="name",help="This option specifies the name of an atom to list. If the atom does not exist, a message will be printed on the standard error.",metavar="string",default=None)
4554
parser.add_option("-m","--match",dest="match_re",help="This option specifies a regular expression to match against name of an atom to list. If the atom does not exist, a message will be printed on the standard error.",metavar="reg-exp",default=None)
4655
parser.add_option("-f","--format",dest="format",help="This option specifies a printf-style string used to list each atom <value,name> pair, printed in that order (value is an unsigned long and name is a char *). Xlsatoms will supply a newline at the end of each line. The default is %ld\\t%s.",metavar="string",default="%ld\t%s")
@@ -54,43 +63,43 @@
5463
d = display.Display(options.display)
5564

5665
def print_atom(print_format,atom,value):
57-
print(print_format%(atom,value))
66+
print(print_format%(atom,value))
5867

5968
def list_atoms(d,re_obj,low,high):
60-
while(low <= high):
61-
try:
62-
val = d.get_atom_name(low)
63-
if (re_obj == None) :
64-
print_atom(options.format,low,val)
65-
elif re_obj.match(val) != None:
66-
print_atom(options.format,low,val)
67-
low += 1
68-
except:
69-
sys.exit(0)
69+
while(low <= high):
70+
try:
71+
val = d.get_atom_name(low)
72+
if (re_obj == None) :
73+
print_atom(options.format,low,val)
74+
elif re_obj.match(val) != None:
75+
print_atom(options.format,low,val)
76+
low += 1
77+
except:
78+
sys.exit(0)
7079

7180
if options.name != None:
72-
try:
73-
atom = d.intern_atom(options.name)
74-
val = d.get_atom_name(atom)
75-
print_atom(options.format,atom,val)
76-
except:
77-
sys.stderr.write('xlsatoms: no atom named "%s" on server "%s"'%(options.name,options.display))
78-
sys.stderr.write("\n")
79-
sys.exit(1)
80-
sys.exit(0)
81+
try:
82+
atom = d.intern_atom(options.name)
83+
val = d.get_atom_name(atom)
84+
print_atom(options.format,atom,val)
85+
except:
86+
sys.stderr.write('xlsatoms: no atom named "%s" on server "%s"'%(options.name,options.display))
87+
sys.stderr.write("\n")
88+
sys.exit(1)
89+
sys.exit(0)
8190

8291
rangeVals = options.range.split("-")
8392
if rangeVals[0] != "":
84-
low = long(rangeVals[0])
93+
low = integer_type(rangeVals[0])
8594

8695
if rangeVals[1] != "":
87-
high = long(rangeVals[1])
96+
high = integer_type(rangeVals[1])
8897
else:
89-
high = sys.maxint
98+
high = MAXSIZE
9099

91100
if options.match_re != None:
92-
re_obj = re.compile(options.match_re)
101+
re_obj = re.compile(options.match_re)
93102
else:
94-
re_obj = None
103+
re_obj = None
95104

96105
list_atoms(d,re_obj,low,high)

0 commit comments

Comments
 (0)