From d7fed2ace4bbbe6e24f1e7a41f9a9ff232518652 Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Sun, 6 Oct 2024 18:19:56 +0200 Subject: [PATCH 1/2] attempt to support modern Python3 versions --- README.rst | 4 ++-- Xlib/ext/xinput.py | 5 +---- Xlib/protocol/display.py | 15 ++------------- Xlib/protocol/rq.py | 12 ++++-------- examples/childwin.py | 5 +---- examples/eventthread.py | 3 --- examples/nvcontrol.py | 4 ---- examples/profilex.py | 4 ---- examples/record_demo.py | 4 ---- examples/run_examples.py | 3 --- examples/security.py | 3 --- examples/shapewin.py | 4 ---- examples/threadtest.py | 3 --- examples/xdamage.py | 4 ---- examples/xfixes-cursor-notify.py | 3 --- examples/xfixes-selection-notify.py | 3 --- examples/xfixes.py | 3 --- examples/xinerama.py | 6 +----- examples/xinput.py | 3 --- examples/xlsatoms.py | 15 +++------------ examples/xrandr.py | 4 ---- runtests.py | 3 --- setup.cfg | 12 ++++-------- test/test_struct.py | 4 ++-- tox.ini | 2 +- 25 files changed, 21 insertions(+), 110 deletions(-) diff --git a/README.rst b/README.rst index ad040fe4..0b59edd7 100644 --- a/README.rst +++ b/README.rst @@ -24,8 +24,8 @@ GPL v2. Requirements ~~~~~~~~~~~~ -The Python X Library requires Python 2.7 or newer. It has been tested to -various extents with Python 2.7 and 3.3 through 3.6. +The Python X Library requires Python 3.6 or newer. It has been tested to +various extents with Python 3.6 through 3.12. The Python X Library will only work on systems that have an X server installed, such as most Linux distros, but will not work on Windows or MacOS. diff --git a/Xlib/ext/xinput.py b/Xlib/ext/xinput.py index f9218064..d89900d2 100644 --- a/Xlib/ext/xinput.py +++ b/Xlib/ext/xinput.py @@ -28,9 +28,6 @@ import array import struct -# Python 2/3 compatibility. -from six import integer_types - from Xlib.protocol import rq from Xlib import X @@ -218,7 +215,7 @@ def pack_value(self, val): mask_seq = array.array(rq.struct_to_array_codes['L']) - if isinstance(val, integer_types): + if isinstance(val, int): # We need to build a "binary mask" that (as far as I can tell) is # encoded in native byte order from end to end. The simple case is # with a single unsigned 32-bit value, for which we construct an diff --git a/Xlib/protocol/display.py b/Xlib/protocol/display.py index 0d910dab..96c49ef4 100644 --- a/Xlib/protocol/display.py +++ b/Xlib/protocol/display.py @@ -30,7 +30,7 @@ import sys # Python 2/3 compatibility. -from six import PY3, byte2int, indexbytes +from six import byte2int, indexbytes # Xlib modules from .. import error @@ -42,9 +42,8 @@ from . import rq from . import event -if PY3: - class bytesview(object): +class bytesview: def __init__(self, data, offset=0, size=None): if size is None: @@ -65,16 +64,6 @@ def __getitem__(self, key): return bytes(self.view[key]) return self.view[key] -else: - - def bytesview(data, offset=0, size=None): - if not isinstance(data, (bytes, buffer)): - raise TypeError('unsupported type: {}'.format(type(data))) - if size is None: - size = len(data)-offset - return buffer(data, offset, size) - - class Display(object): extension_major_opcodes = {} error_classes = error.xerror_class.copy() diff --git a/Xlib/protocol/rq.py b/Xlib/protocol/rq.py index 8bc82059..d030a204 100644 --- a/Xlib/protocol/rq.py +++ b/Xlib/protocol/rq.py @@ -26,7 +26,7 @@ from array import array # Python 2/3 compatibility. -from six import PY3, binary_type, byte2int, indexbytes, iterbytes +from six import byte2int, indexbytes, iterbytes # Xlib modules from .. import X @@ -36,12 +36,8 @@ def decode_string(bs): return bs.decode('latin1') -if PY3: - def encode_array(a): - return a.tobytes() -else: - def encode_array(a): - return a.tostring() +def encode_array(a): + return a.tobytes() class BadDataError(Exception): pass @@ -676,7 +672,7 @@ def pack_value(self, value): if fmt not in (8, 16, 32): raise BadDataError('Invalid property data format {0}'.format(fmt)) - if isinstance(val, binary_type): + if isinstance(val, bytes): size = fmt // 8 vlen = len(val) if vlen % size: diff --git a/examples/childwin.py b/examples/childwin.py index 9a0cf6b3..d63f7ab2 100755 --- a/examples/childwin.py +++ b/examples/childwin.py @@ -22,9 +22,6 @@ # Suite 330, # Boston, MA 02111-1307 USA -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os @@ -34,7 +31,7 @@ from Xlib import X, display, Xutil # Application window -class Window(object): +class Window: def __init__(self, display): self.d = display diff --git a/examples/eventthread.py b/examples/eventthread.py index da8cb505..8f03139b 100755 --- a/examples/eventthread.py +++ b/examples/eventthread.py @@ -22,9 +22,6 @@ # Suite 330, # Boston, MA 02111-1307 USA -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os diff --git a/examples/nvcontrol.py b/examples/nvcontrol.py index dece439f..20187a7c 100755 --- a/examples/nvcontrol.py +++ b/examples/nvcontrol.py @@ -21,10 +21,6 @@ # Suite 330, # Boston, MA 02111-1307 USA - -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os diff --git a/examples/profilex.py b/examples/profilex.py index e8361fd0..cae69891 100755 --- a/examples/profilex.py +++ b/examples/profilex.py @@ -3,10 +3,6 @@ # Program to generate profiling data. Run with one argument, # the profile stats file to generate. - -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os diff --git a/examples/record_demo.py b/examples/record_demo.py index 25ed575d..a4dc160a 100755 --- a/examples/record_demo.py +++ b/examples/record_demo.py @@ -27,10 +27,6 @@ Not very much unlike the xmacrorec2 program in the xmacro package. ''' - -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os diff --git a/examples/run_examples.py b/examples/run_examples.py index 9c834dcd..5640462d 100644 --- a/examples/run_examples.py +++ b/examples/run_examples.py @@ -21,9 +21,6 @@ # Suite 330, # Boston, MA 02111-1307 USA -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os import subprocess diff --git a/examples/security.py b/examples/security.py index f95047ed..6bb41889 100755 --- a/examples/security.py +++ b/examples/security.py @@ -22,9 +22,6 @@ # Suite 330, # Boston, MA 02111-1307 USA -# Python 2/3 compatibility. -from __future__ import print_function - import sys, os from optparse import OptionParser diff --git a/examples/shapewin.py b/examples/shapewin.py index 015a03cf..14929fd5 100755 --- a/examples/shapewin.py +++ b/examples/shapewin.py @@ -21,10 +21,6 @@ # Suite 330, # Boston, MA 02111-1307 USA - -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os diff --git a/examples/threadtest.py b/examples/threadtest.py index 9732f575..9269adfe 100755 --- a/examples/threadtest.py +++ b/examples/threadtest.py @@ -1,8 +1,5 @@ #!/usr/bin/python -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os diff --git a/examples/xdamage.py b/examples/xdamage.py index 46cbab9d..7b154a04 100644 --- a/examples/xdamage.py +++ b/examples/xdamage.py @@ -21,10 +21,6 @@ # Suite 330, # Boston, MA 02111-1307 USA - -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os diff --git a/examples/xfixes-cursor-notify.py b/examples/xfixes-cursor-notify.py index cc443fe9..ea3b2089 100755 --- a/examples/xfixes-cursor-notify.py +++ b/examples/xfixes-cursor-notify.py @@ -23,9 +23,6 @@ # Suite 330, # Boston, MA 02111-1307 USA -# Python 2/3 compatibility. -from __future__ import print_function - import sys from Xlib.display import Display from Xlib.ext import xfixes diff --git a/examples/xfixes-selection-notify.py b/examples/xfixes-selection-notify.py index 20728fbd..b5e39a87 100755 --- a/examples/xfixes-selection-notify.py +++ b/examples/xfixes-selection-notify.py @@ -23,9 +23,6 @@ # Suite 330, # Boston, MA 02111-1307 USA -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os import time diff --git a/examples/xfixes.py b/examples/xfixes.py index 53c37146..a2790ba7 100755 --- a/examples/xfixes.py +++ b/examples/xfixes.py @@ -22,9 +22,6 @@ # Suite 330, # Boston, MA 02111-1307 USA -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os import time diff --git a/examples/xinerama.py b/examples/xinerama.py index 13419ba9..bf1b0b4c 100755 --- a/examples/xinerama.py +++ b/examples/xinerama.py @@ -21,10 +21,6 @@ # Suite 330, # Boston, MA 02111-1307 USA - -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os import pprint @@ -36,7 +32,7 @@ from Xlib.ext import xinerama # Application window (only one) -class Window(object): +class Window: def __init__(self, display): self.d = display diff --git a/examples/xinput.py b/examples/xinput.py index ccfb27a3..06573fee 100755 --- a/examples/xinput.py +++ b/examples/xinput.py @@ -22,9 +22,6 @@ # Suite 330, # Boston, MA 02111-1307 USA -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os diff --git a/examples/xlsatoms.py b/examples/xlsatoms.py index 02a26f6d..85617fb0 100755 --- a/examples/xlsatoms.py +++ b/examples/xlsatoms.py @@ -26,14 +26,11 @@ ''' -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os # Python 2/3 compatibility. -from six import PY2, MAXSIZE +from six import MAXSIZE # Change path so we find Xlib sys.path.append(os.path.join(os.path.dirname(__file__), '..')) @@ -42,12 +39,6 @@ from Xlib import display, error from optparse import OptionParser - -if PY2: - integer_type = long -else: - integer_type = int - parser = OptionParser() parser.add_option("-d","--display",dest="display",help="This option specifies the X server to which to connect",metavar="dpy",default=None) 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) @@ -90,10 +81,10 @@ def list_atoms(d,re_obj,low,high): rangeVals = options.range.split("-") if rangeVals[0] != "": - low = integer_type(rangeVals[0]) + low = int(rangeVals[0]) if rangeVals[1] != "": - high = integer_type(rangeVals[1]) + high = int(rangeVals[1]) else: high = MAXSIZE diff --git a/examples/xrandr.py b/examples/xrandr.py index 5a645340..de5642b5 100755 --- a/examples/xrandr.py +++ b/examples/xrandr.py @@ -21,10 +21,6 @@ # Suite 330, # Boston, MA 02111-1307 USA - -# Python 2/3 compatibility. -from __future__ import print_function - import sys import os import pprint diff --git a/runtests.py b/runtests.py index 4e77de87..0b6fb19d 100755 --- a/runtests.py +++ b/runtests.py @@ -1,8 +1,5 @@ #!/usr/bin/env python -# Python 2/3 compatibility. -from __future__ import print_function - import os import signal import subprocess diff --git a/setup.cfg b/setup.cfg index 29a11ad0..2fea1cff 100644 --- a/setup.cfg +++ b/setup.cfg @@ -19,21 +19,17 @@ classifiers = Intended Audience :: Developers License :: OSI Approved :: GNU Lesser General Public License v2 or later (LGPLv2+) Operating System :: OS Independent - Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 - Programming Language :: Python :: 3.3 - Programming Language :: Python :: 3.4 - Programming Language :: Python :: 3.5 - Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 + Programming Language :: Python :: 3.9 + Programming Language :: Python :: 3.10 + Programming Language :: Python :: 3.11 + Programming Language :: Python :: 3.12 Programming Language :: Python :: Implementation :: CPython Topic :: Software Development :: Libraries :: Python Modules Topic :: Software Development :: Libraries Topic :: Software Development :: User Interfaces -[bdist_wheel] -universal = 1 - # vim: list diff --git a/test/test_struct.py b/test/test_struct.py index 47da4e5d..8f7280f1 100644 --- a/test/test_struct.py +++ b/test/test_struct.py @@ -5,7 +5,7 @@ import types import re -from six import binary_type, iterbytes +from six import iterbytes from Xlib.protocol import rq from . import DummyDisplay, TestCase @@ -85,7 +85,7 @@ def _struct_test(name, fields): values_in[field_name] = field_value_in if field_value_out is not None: values_out[field_name] = field_value_out - if isinstance(field_binary, binary_type): + if isinstance(field_binary, bytes): binary += field_binary elif isinstance(field_binary, (types.FunctionType, types.LambdaType, partial)): binary += field_binary(field_value_in) diff --git a/tox.ini b/tox.ini index 360451f1..0b7395e9 100644 --- a/tox.ini +++ b/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = py27,py33,py34,py35,py36 +envlist = py36,py37,py38,py39,py310,py311,py312 skip_missing_interpreters = true [testenv] From 74b8668d4c0522a6c84a290262e6ccf218fb4b3f Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Sun, 13 Apr 2025 12:30:56 +0200 Subject: [PATCH 2/2] finish removing "six" --- Xlib/display.py | 5 +---- Xlib/protocol/display.py | 5 +++-- Xlib/protocol/rq.py | 7 ++++--- examples/xlsatoms.py | 5 +---- requirements.txt | 1 - setup.py | 1 - test/test_bytesview.py | 11 ++++------- test/test_struct.py | 4 +--- 8 files changed, 14 insertions(+), 25 deletions(-) diff --git a/Xlib/display.py b/Xlib/display.py index 87b9aa61..45139202 100644 --- a/Xlib/display.py +++ b/Xlib/display.py @@ -22,9 +22,6 @@ # Python modules import types -# Python 2/3 compatibility. -from six import create_unbound_method - # Xlib modules from . import error from . import ext @@ -283,7 +280,7 @@ def extension_add_method(self, object, name, function): if hasattr(cls, name): raise AssertionError('attempting to replace %s method: %s' % (class_name, name)) - method = create_unbound_method(function, cls) + method = function # Maybe should check extension overrides too try: diff --git a/Xlib/protocol/display.py b/Xlib/protocol/display.py index 96c49ef4..8af44010 100644 --- a/Xlib/protocol/display.py +++ b/Xlib/protocol/display.py @@ -29,8 +29,9 @@ import struct import sys -# Python 2/3 compatibility. -from six import byte2int, indexbytes +import operator +byte2int = operator.itemgetter(0) +indexbytes = operator.getitem # Xlib modules from .. import error diff --git a/Xlib/protocol/rq.py b/Xlib/protocol/rq.py index d030a204..2adc580f 100644 --- a/Xlib/protocol/rq.py +++ b/Xlib/protocol/rq.py @@ -25,8 +25,9 @@ import struct from array import array -# Python 2/3 compatibility. -from six import byte2int, indexbytes, iterbytes +import operator +byte2int = operator.itemgetter(0) +indexbytes = operator.getitem # Xlib modules from .. import X @@ -452,7 +453,7 @@ def __init__(self, name, pad = 1): def pack_value(self, val): """Convert 8-byte string into 16-byte list""" if isinstance(val, bytes): - val = list(iterbytes(val)) + val = list(iter(val)) slen = len(val) diff --git a/examples/xlsatoms.py b/examples/xlsatoms.py index 85617fb0..4ab41289 100755 --- a/examples/xlsatoms.py +++ b/examples/xlsatoms.py @@ -29,9 +29,6 @@ import sys import os -# Python 2/3 compatibility. -from six import MAXSIZE - # Change path so we find Xlib sys.path.append(os.path.join(os.path.dirname(__file__), '..')) @@ -86,7 +83,7 @@ def list_atoms(d,re_obj,low,high): if rangeVals[1] != "": high = int(rangeVals[1]) else: - high = MAXSIZE + high = sys.maxsize if options.match_re != None: re_obj = re.compile(options.match_re) diff --git a/requirements.txt b/requirements.txt index dde39334..e69de29b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +0,0 @@ -six>=1.10.0 diff --git a/setup.py b/setup.py index 71bdb085..9674de78 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,6 @@ setup( - install_requires=['six>=1.10.0'], setup_requires=['setuptools-scm'], packages=[ 'Xlib', diff --git a/test/test_bytesview.py b/test/test_bytesview.py index 565793a1..6b7af220 100644 --- a/test/test_bytesview.py +++ b/test/test_bytesview.py @@ -1,9 +1,6 @@ import unittest -# Python 2/3 compatibility. -from six import indexbytes, text_type - from Xlib.protocol.display import bytesview @@ -11,17 +8,17 @@ class BytesViewTest(unittest.TestCase): def test(self): with self.assertRaises(TypeError): - bytesview(text_type('foobar')) + bytesview('foobar') data = b'0123456789ABCDEF' view = bytesview(data) self.assertEqual(len(view), 16) self.assertEqual(view[:], data) self.assertIsInstance(view[:], bytes) self.assertEqual(view[5:-6], b'56789') - self.assertEqual(indexbytes(view, 7), ord('7')) + self.assertEqual(view[7], ord('7')) view = bytesview(view, 5) self.assertEqual(view[:], b'56789ABCDEF') - self.assertEqual(indexbytes(view, 4), ord('9')) + self.assertEqual(view[4], ord('9')) view = bytesview(view, 0, 5) self.assertEqual(view[:], b'56789') - self.assertEqual(indexbytes(view, 1), ord('6')) + self.assertEqual(view[1], ord('6')) diff --git a/test/test_struct.py b/test/test_struct.py index 8f7280f1..0047148f 100644 --- a/test/test_struct.py +++ b/test/test_struct.py @@ -5,8 +5,6 @@ import types import re -from six import iterbytes - from Xlib.protocol import rq from . import DummyDisplay, TestCase @@ -167,7 +165,7 @@ def _struct_test(name, fields): (None, lambda name: rq.LengthOf('s2', 2) , None , pack('H', 3) ), ('s1', lambda name: rq.String16(name, pad=0), (0, 1, 2), lambda s: struct.pack('>' + 'H' * len(s), *s)), # An 8-bits string is also allowed on input. - ('s2', lambda name: rq.String16(name, pad=0), b'\x03\x04\x05', lambda s: struct.pack('>' + 'H' * len(s), *iterbytes(s)), (3, 4, 5)), + ('s2', lambda name: rq.String16(name, pad=0), b'\x03\x04\x05', lambda s: struct.pack('>' + 'H' * len(s), *iter(s)), (3, 4, 5)), )) _struct_test('binary', (