Skip to content

Commit a24c902

Browse files
Py2/Py3 compatibility: make almost all imports relative.
Replace deprecated module "new" with the "types" one. Convert old-style classes to new-style ones (inherited from object).
1 parent 3b01790 commit a24c902

32 files changed

+93
-77
lines changed

.travis.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ language: python
33
python:
44
- "2.6"
55
- "2.7"
6+
- "3.3"
7+
- "3.4"
8+
- "3.5"
69

710
# command to install dependencies
811
install: pip install -r dev-requirements.txt

Xlib/display.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1818

1919
# Python modules
20-
import new
20+
import types
2121

2222
# Xlib modules
23-
import error
24-
import ext
25-
import X
23+
from . import error
24+
from . import ext
25+
from . import X
2626

2727
# Xlib.protocol modules
28-
import protocol.display
29-
from protocol import request, event, rq
28+
from . import protocol.display
29+
from .protocol import request, event, rq
3030

3131
# Xlib.xobjects modules
3232
import xobject.resource
@@ -78,7 +78,7 @@ def get_atom(self, atomname, only_if_exists=0):
7878
return r.atom
7979

8080

81-
class Display:
81+
class Display(object):
8282
def __init__(self, display = None):
8383
self.display = _BaseDisplay(display)
8484

@@ -126,9 +126,14 @@ def __init__(self, display = None):
126126
# Finalize extensions by creating new classes
127127
for type, dict in self.class_extension_dicts.items():
128128
origcls = self.display.resource_classes[type]
129-
self.display.resource_classes[type] = new.classobj(origcls.__name__,
130-
(origcls,),
131-
dict)
129+
if hasattr(types, 'ClassType'):
130+
self.display.resource_classes[type] = types.ClassType(origcls.__name__,
131+
(origcls,),
132+
dict)
133+
else:
134+
self.display.resource_classes[type] = types.new_class(origcls.__name__,
135+
(origcls,),
136+
dict)
132137

133138
# Problem: we have already created some objects without the
134139
# extensions: the screen roots and default colormaps.
@@ -216,7 +221,7 @@ class directly, since any X extensions dynamically added by the
216221
def __getattr__(self, attr):
217222
try:
218223
function = self.display_extension_methods[attr]
219-
return new.instancemethod(function, self, self.__class__)
224+
return types.MethodType(function, self, self.__class__)
220225
except KeyError:
221226
raise AttributeError(attr)
222227

@@ -277,7 +282,7 @@ def extension_add_method(self, object, name, function):
277282
if hasattr(cls, name):
278283
raise AssertionError('attempting to replace %s method: %s' % (type, name))
279284

280-
method = new.instancemethod(function, None, cls)
285+
method = types.MethodType(function, None, cls)
281286

282287
# Maybe should check extension overrides too
283288
try:
@@ -297,8 +302,12 @@ def extension_add_event(self, code, evt, name = None):
297302
extension_event.
298303
"""
299304

300-
newevt = new.classobj(evt.__name__, evt.__bases__,
301-
evt.__dict__.copy())
305+
if hasattr(types, 'ClassType'):
306+
newevt = types.ClassType(evt.__name__, evt.__bases__,
307+
evt.__dict__.copy())
308+
else:
309+
newevt = types.new_class(evt.__name__, evt.__bases__,
310+
evt.__dict__.copy())
302311
newevt._code = code
303312

304313
self.display.add_extension_event(code, newevt)
@@ -321,8 +330,12 @@ def extension_add_subevent(self, code, subcode, evt, name = None):
321330
extension_event.
322331
"""
323332

324-
newevt = new.classobj(evt.__name__, evt.__bases__,
325-
evt.__dict__.copy())
333+
if hasattr(types, 'ClassType'):
334+
newevt = types.ClassType(evt.__name__, evt.__bases__,
335+
evt.__dict__.copy())
336+
else:
337+
newevt = types.new_class(evt.__name__, evt.__bases__,
338+
evt.__dict__.copy())
326339
newevt._code = code
327340

328341
self.display.add_extension_event(code, newevt, subcode)

Xlib/error.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
import string
2121

2222
# Xlib modules
23-
import X
23+
from . import X
2424

2525
# Xlib.protocol modules
26-
from Xlib.protocol import rq
26+
from .protocol import rq
2727

2828

2929
class DisplayError(Exception):
@@ -129,7 +129,7 @@ class BadImplementation(XError): pass
129129
}
130130

131131

132-
class CatchError:
132+
class CatchError(object):
133133
def __init__(self, *errors):
134134
self.error_types = errors
135135
self.error = None

Xlib/protocol/display.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,16 @@
2626
import socket
2727

2828
# Xlib modules
29-
from Xlib import error
30-
from Xlib.ext import ge
29+
from .. import error
30+
from ..ext import ge
3131

32-
from Xlib.support import lock, connect
32+
from ..support import lock, connect
3333

3434
# Xlib.protocol modules
35-
import rq
36-
import event
35+
from . import rq
36+
from . import event
3737

38-
class Display:
38+
class Display(object):
3939
resource_classes = {}
4040
extension_major_opcodes = {}
4141
error_classes = error.xerror_class.copy()

Xlib/protocol/event.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818

1919

2020
# Xlib modules
21-
from Xlib import X
21+
from .. import X
2222

2323
# Xlib.protocol modules
24-
import rq
24+
from . import rq
2525

2626

2727
class AnyEvent(rq.Event):

Xlib/protocol/request.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919

2020
# Xlib modules
21-
from Xlib import X
21+
from .. import X
2222

2323
# Xlib.protocol modules
24-
import rq
25-
import structs
24+
from . import rq
25+
from . import structs
2626

2727

2828
class CreateWindow(rq.Request):

Xlib/protocol/rq.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@
2424
import string
2525
from array import array
2626
import types
27-
import new
2827

2928
# Xlib modules
30-
from Xlib import X
31-
from Xlib.support import lock
29+
from .. import X
30+
from ..support import lock
3231

3332

3433
class BadDataError(Exception): pass
@@ -52,17 +51,17 @@ class BadDataError(Exception): pass
5251
for c in 'bhil':
5352
size = array(c).itemsize
5453

55-
array_unsigned_codes[size] = string.upper(c)
54+
array_unsigned_codes[size] = c.upper()
5655
try:
5756
struct_to_array_codes[signed_codes[size]] = c
58-
struct_to_array_codes[unsigned_codes[size]] = string.upper(c)
57+
struct_to_array_codes[unsigned_codes[size]] = c.upper()
5958
except KeyError:
6059
pass
6160

6261
# print array_unsigned_codes, struct_to_array_codes
6362

6463

65-
class Field:
64+
class Field(object):
6665
"""Field objects represent the data fields of a Struct.
6766
6867
Field objects must have the following attributes:
@@ -829,7 +828,7 @@ def parse_binary_value(self, data, display, length, format):
829828
# Struct is also usable.
830829
#
831830

832-
class ScalarObj:
831+
class ScalarObj(object):
833832
def __init__(self, code):
834833
self.structcode = code
835834
self.structvalues = 1
@@ -840,7 +839,7 @@ def __init__(self, code):
840839
Card16Obj = ScalarObj('H')
841840
Card32Obj = ScalarObj('L')
842841

843-
class ResourceObj:
842+
class ResourceObj(object):
844843
structcode = 'L'
845844
structvalues = 1
846845

@@ -860,7 +859,7 @@ def parse_value(self, value, display):
860859
WindowObj = ResourceObj('window')
861860
ColormapObj = ResourceObj('colormap')
862861

863-
class StrClass:
862+
class StrClass(object):
864863
structcode = None
865864

866865
def pack_value(self, val):
@@ -873,7 +872,7 @@ def parse_binary(self, data, display):
873872
Str = StrClass()
874873

875874

876-
class Struct:
875+
class Struct(object):
877876

878877
"""Struct objects represents a binary data structure. It can
879878
contain both fields with static and dynamic sizes. However, all
@@ -1096,8 +1095,8 @@ def to_binary(self, *varargs, **keys):
10961095
# memory leak isn't that serious. Besides, Python 2.0 has
10971096
# real garbage collect.
10981097

1099-
exec code
1100-
self.to_binary = new.instancemethod(to_binary, self, self.__class__)
1098+
exec(code)
1099+
self.to_binary = types.MethodType(to_binary, self, self.__class__)
11011100

11021101
# Finally call it manually
11031102
return apply(self.to_binary, varargs, keys)
@@ -1181,8 +1180,8 @@ def parse_value(self, val, display, rawdict = 0):
11811180

11821181
# Finally, compile function as for to_binary.
11831182

1184-
exec code
1185-
self.parse_value = new.instancemethod(parse_value, self, self.__class__)
1183+
exec(code)
1184+
self.parse_value = types.MethodType(parse_value, self, self.__class__)
11861185

11871186
# Call it manually
11881187
return self.parse_value(val, display, rawdict)
@@ -1285,8 +1284,8 @@ def parse_binary(self, data, display, rawdict = 0):
12851284

12861285
# Finally, compile function as for to_binary.
12871286

1288-
exec code
1289-
self.parse_binary = new.instancemethod(parse_binary, self, self.__class__)
1287+
exec(code)
1288+
self.parse_binary = types.MethodType(parse_binary, self, self.__class__)
12901289

12911290
# Call it manually
12921291
return self.parse_binary(data, display, rawdict)
@@ -1412,7 +1411,7 @@ def __cmp__(self, other):
14121411
return cmp(self._data, other)
14131412

14141413

1415-
class Request:
1414+
class Request(object):
14161415
def __init__(self, display, onerror = None, *args, **keys):
14171416
self._errorhandler = onerror
14181417
self._binary = apply(self._request.to_binary, args, keys)

Xlib/protocol/structs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1818

1919
# Xlib modules
20-
from Xlib import X
20+
from .. import X
2121

2222
# Xlib.protocol modules
23-
import rq
23+
from . import rq
2424

2525
def WindowValues(arg):
2626
return rq.ValueList( arg, 4, 0,

Xlib/rdb.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import sys
2929

3030
# Xlib modules
31-
from support import lock
31+
from .support import lock
3232

3333
# Set up a few regexpes for parsing string representation of resources
3434

@@ -49,7 +49,7 @@ class OptionError(Exception):
4949
pass
5050

5151

52-
class ResourceDB:
52+
class ResourceDB(object):
5353
def __init__(self, file = None, string = None, resources = None):
5454
self.db = {}
5555
self.lock = lock.allocate_lock()
@@ -189,14 +189,15 @@ def insert(self, resource, value):
189189

190190
self.lock.release()
191191

192-
def __getitem__(self, (name, cls)):
192+
def __getitem__(self, keys_tuple):
193193
"""db[name, class]
194194
195195
Return the value matching the resource identified by NAME and
196196
CLASS. If no match is found, KeyError is raised.
197197
"""
198198

199199
# Split name and class into their parts
200+
name, cls = keys_tuple
200201

201202
namep = string.split(name, '.')
202203
clsp = string.split(cls, '.')
@@ -376,7 +377,7 @@ def getopt(self, name, argv, opts):
376377
return argv
377378

378379

379-
class _Match:
380+
class _Match(object):
380381
def __init__(self, path, dbs):
381382
self.path = path
382383

@@ -551,7 +552,7 @@ def output_escape(value):
551552
# Option type definitions
552553
#
553554

554-
class Option:
555+
class Option(object):
555556
def __init__(self):
556557
pass
557558

Xlib/support/lock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# along with this program; if not, write to the Free Software
1717
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1818

19-
class _DummyLock:
19+
class _DummyLock(object):
2020
def __init__(self):
2121

2222
# This might be nerdy, but by assigning methods like this

0 commit comments

Comments
 (0)