From 911a45136653b8797381a92955a792469a1b927b Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Sat, 21 Sep 2024 11:31:00 +0200 Subject: [PATCH 1/2] trim usage of six and util.long_type --- src/foolscap/appserver/cli.py | 2 +- src/foolscap/banana.py | 13 ++++++------- src/foolscap/broker.py | 4 ++-- src/foolscap/constraint.py | 5 ++--- src/foolscap/logging/dumper.py | 6 +++--- src/foolscap/logging/filter.py | 3 +-- src/foolscap/logging/gatherer.py | 2 +- src/foolscap/logging/log.py | 8 +++----- src/foolscap/logging/tail.py | 4 ++-- src/foolscap/pb.py | 2 +- src/foolscap/schema.py | 10 +++------- src/foolscap/slicers/unicode.py | 5 ++--- src/foolscap/slicers/vocab.py | 2 +- src/foolscap/test/common.py | 5 ++--- src/foolscap/test/test_banana.py | 33 ++++++++++++-------------------- src/foolscap/test/test_call.py | 3 +-- src/foolscap/test/test_schema.py | 7 +++---- src/foolscap/util.py | 6 ------ 18 files changed, 46 insertions(+), 74 deletions(-) diff --git a/src/foolscap/appserver/cli.py b/src/foolscap/appserver/cli.py index 719056d5..22966503 100644 --- a/src/foolscap/appserver/cli.py +++ b/src/foolscap/appserver/cli.py @@ -454,7 +454,7 @@ def run_flappserver(argv=None, run_by_human=True): r = dispatch(command, so) except (usage.UsageError, BadServiceArguments) as e: r = 1 - print("Error:", six.text_type(e), file=so.stderr) + print("Error:", str(e), file=so.stderr) from twisted.internet import defer if run_by_human: if isinstance(r, defer.Deferred): diff --git a/src/foolscap/banana.py b/src/foolscap/banana.py index 133b9ace..77b1a81c 100644 --- a/src/foolscap/banana.py +++ b/src/foolscap/banana.py @@ -11,7 +11,6 @@ from foolscap.slicers.allslicers import RootSlicer, RootUnslicer from foolscap.slicers.allslicers import ReplaceVocabSlicer, AddVocabSlicer -from .util import long_type from . import stringchain from . import tokens from .tokens import SIZE_LIMIT, STRING, LIST, INT, NEG, \ @@ -60,7 +59,7 @@ def bytes_to_long(s): This is (essentially) the inverse of long_to_bytes(). """ - acc = long_type(0) + acc = 0 for i in six.iterbytes(s): acc <<= 8 acc += i @@ -68,7 +67,7 @@ def bytes_to_long(s): HIGH_BIT_SET = b"\x80" -SIMPLE_TOKENS = six.integer_types + (float, six.binary_type) +SIMPLE_TOKENS = (int, float, bytes) # Banana is a big class. It is split up into three sections: sending, # receiving, and connection setup. These used to be separate classes, but @@ -417,7 +416,7 @@ def outgoingVocabTableWasReplaced(self, newTable): # this is called by the ReplaceVocabSlicer to manipulate our table. # It must certainly *not* be called by higher-level user code. for k in newTable.keys(): - assert isinstance(k, six.binary_type) + assert isinstance(k, bytes) self.outgoingVocabulary = newTable if newTable: maxIndex = max(newTable.values()) + 1 @@ -466,7 +465,7 @@ def sendOpen(self): def sendToken(self, obj): write = self.transport.write - if isinstance(obj, six.integer_types): + if isinstance(obj, int): if obj >= 2**31: s = long_to_bytes(obj) int2b128(len(s), write) @@ -486,7 +485,7 @@ def sendToken(self, obj): elif isinstance(obj, float): write(FLOAT) write(struct.pack("!d", obj)) - elif isinstance(obj, six.binary_type): + elif isinstance(obj, bytes): if obj in self.outgoingVocabulary: symbolID = self.outgoingVocabulary[obj] int2b128(symbolID, write) @@ -924,7 +923,7 @@ def handleData(self, chunk): elif typebyte == NEG: # -2**31 is too large for a positive int, so go through # LongType first - obj = int(-long_type(header)) + obj = int(-int(header)) elif typebyte == LONGINT or typebyte == LONGNEG: strlen = header if len(self.buffer) >= strlen: diff --git a/src/foolscap/broker.py b/src/foolscap/broker.py index 6b23b8d4..c6ca7760 100644 --- a/src/foolscap/broker.py +++ b/src/foolscap/broker.py @@ -441,7 +441,7 @@ def getMyReferenceByCLID(self, clid): was registered with our Factory. """ - assert isinstance(clid, six.integer_types) + assert isinstance(clid, int) if clid == 0: return self return self.myReferenceByCLID[clid].obj @@ -451,7 +451,7 @@ def getMyReferenceByCLID(self, clid): def remote_decref(self, clid, count): # invoked when the other side sends us a decref message - assert isinstance(clid, six.integer_types) + assert isinstance(clid, int) assert clid != 0 tracker = self.myReferenceByCLID.get(clid, None) if not tracker: diff --git a/src/foolscap/constraint.py b/src/foolscap/constraint.py index d484465d..fab6ac52 100644 --- a/src/foolscap/constraint.py +++ b/src/foolscap/constraint.py @@ -5,7 +5,6 @@ # This imports foolscap.tokens, but no other Foolscap modules. -import six from zope.interface import implementer, Interface from foolscap.util import ensure_tuple_str @@ -211,7 +210,7 @@ def __init__(self, maxLength=None, minLength=0): VOCAB: None} def checkObject(self, obj, inbound): - if not isinstance(obj, six.binary_type): + if not isinstance(obj, bytes): raise Violation("'%r' is not a bytestring" % (obj,)) if self.maxLength != None and len(obj) > self.maxLength: raise Violation("string too long (%d > %d)" % @@ -236,7 +235,7 @@ def __init__(self, maxBytes=-1): self.taster[LONGNEG] = maxBytes def checkObject(self, obj, inbound): - if not isinstance(obj, six.integer_types): + if not isinstance(obj, int): raise Violation("'%r' is not a number" % (obj,)) if self.maxBytes == -1: if obj >= 2**31 or obj < -2**31: diff --git a/src/foolscap/logging/dumper.py b/src/foolscap/logging/dumper.py index d7eff53b..840665d3 100644 --- a/src/foolscap/logging/dumper.py +++ b/src/foolscap/logging/dumper.py @@ -1,4 +1,4 @@ -import six, sys, errno, textwrap +import sys, errno, textwrap from twisted.python import usage from foolscap.logging import flogfile from foolscap.logging.log import format_message @@ -76,7 +76,7 @@ def print_header(self, e, options): t = h["trigger"] self.trigger = (t["incarnation"], t["num"]) if options['verbose']: - print(six.text_type(e), file=stdout) + print(str(e), file=stdout) if not options["just-numbers"] and not options["verbose"]: if "versions" in h: print(u"Application versions (embedded in logfile):", file=stdout) @@ -95,7 +95,7 @@ def print_event(self, e, options): d = e['d'] when = format_time(d['time'], options["timestamps"]) if options['just-numbers']: - print(six.text_type(when), six.text_type(d.get('num')), file=stdout) + print(str(when), str(d.get('num')), file=stdout) return eid = (d["incarnation"], d["num"]) diff --git a/src/foolscap/logging/filter.py b/src/foolscap/logging/filter.py index 69c5de77..66387d6b 100644 --- a/src/foolscap/logging/filter.py +++ b/src/foolscap/logging/filter.py @@ -1,4 +1,3 @@ -import six from twisted.python import usage import sys, os, bz2, time from foolscap.logging import log, flogfile @@ -82,7 +81,7 @@ def run(self, options): for e in flogfile.get_events(options.oldfile): if options['verbose']: if "d" in e: - print(six.text_type(e['d']['num']), file=stdout) + print(str(e['d']['num']), file=stdout) else: print(u"HEADER", file=stdout) total += 1 diff --git a/src/foolscap/logging/gatherer.py b/src/foolscap/logging/gatherer.py index 28898df0..2f307dd7 100644 --- a/src/foolscap/logging/gatherer.py +++ b/src/foolscap/logging/gatherer.py @@ -377,7 +377,7 @@ def maybe_fetch_incident(self): return self.incident_fetch_outstanding = True (name, trigger) = self.incidents_wanted.pop(0) - print("fetching incident", six.text_type(name), file=self.stdout) + print("fetching incident", str(name), file=self.stdout) d = self.publisher.callRemote("get_incident", six.ensure_binary(name)) def _clear_outstanding(res): self.incident_fetch_outstanding = False diff --git a/src/foolscap/logging/log.py b/src/foolscap/logging/log.py index d0d18548..7a8fb9ce 100644 --- a/src/foolscap/logging/log.py +++ b/src/foolscap/logging/log.py @@ -46,13 +46,13 @@ def format_message(e): args = e['args'] elif "message" in e: fmt = "%(message)s" - assert isinstance(e['message'], (six.binary_type, six.text_type)) + assert isinstance(e['message'], (bytes, str)) args = {"message": six.ensure_str(e['message'])} # i.e. just return e['message'] else: fmt = "" args = {} - assert isinstance(fmt, (six.binary_type, six.text_type)) + assert isinstance(fmt, (bytes, str)) return six.ensure_text(fmt % args) except (ValueError, TypeError): return six.ensure_text(e.get('message', "[no message]")) + " [formatting failed]" @@ -380,9 +380,7 @@ def observer(self, d): # level. log_level = d.pop("log_level") new_log_level = llmap.get(log_level, log_level) - if not isinstance(new_log_level, - six.integer_types + - (six.binary_type, six.text_type, bool)): + if not isinstance(new_log_level, (int, bytes, str, bool)): # it was something weird: just stringify it in-place new_log_level = str(new_log_level) kwargs["level"] = new_log_level # foolscap level, not twisted diff --git a/src/foolscap/logging/tail.py b/src/foolscap/logging/tail.py index 4af072ae..3330fe1b 100644 --- a/src/foolscap/logging/tail.py +++ b/src/foolscap/logging/tail.py @@ -1,4 +1,4 @@ -import six, os, sys, time +import os, sys, time from zope.interface import implementer from twisted.internet import reactor from twisted.python import usage @@ -93,7 +93,7 @@ def remote_msg(self, d): self.saver.remote_msg(d) def simple_print(self, d): - print(six.text_type(d), file=self.output) + print(str(d), file=self.output) def formatted_print(self, d): time_s = format_time(d['time'], self.options["timestamps"]) diff --git a/src/foolscap/pb.py b/src/foolscap/pb.py index 64df3a6d..236b089c 100644 --- a/src/foolscap/pb.py +++ b/src/foolscap/pb.py @@ -504,7 +504,7 @@ def listenOn(self, what, _test_options={}): @return: The Listener object that was created. This can be used to stop listening later on.""" - if isinstance(what, (six.binary_type, six.text_type)): + if isinstance(what, (bytes, str)): what = six.ensure_str(what) if what in ("0", "tcp:0"): diff --git a/src/foolscap/schema.py b/src/foolscap/schema.py index 026e025f..1e0b6893 100644 --- a/src/foolscap/schema.py +++ b/src/foolscap/schema.py @@ -54,7 +54,6 @@ """ -import six from foolscap.tokens import Violation, UnknownSchemaType, BananaError, \ tokenNames @@ -135,23 +134,20 @@ def AnyStringConstraint(*args, **kwargs): StringConstraint = ByteStringConstraint constraintMap = { - six.binary_type: ByteStringConstraint(), - six.text_type: UnicodeConstraint(), + bytes: ByteStringConstraint(), + str: UnicodeConstraint(), bool: BooleanConstraint(), + int: IntegerConstraint(maxBytes=1024), float: NumberConstraint(), None: Nothing(), } - # we don't maintain compatibility for constraints defined by types. Back in # the py2-only days, 'int' meant a 32-bit signed integer, 'long' meant # fit-in-1024-bytes. The new rule is that 'int' means fit-in-1024-bytes (and # there is no 'long' in py3, of course). To get a 32-bit signed integer # constraint, use Int(maxBytes=-1). -for t in six.integer_types: - constraintMap[t] = IntegerConstraint(maxBytes=1024) - # This module provides a function named addToConstraintTypeMap() which helps # to resolve some import cycles. diff --git a/src/foolscap/slicers/unicode.py b/src/foolscap/slicers/unicode.py index 29442519..52634e9f 100644 --- a/src/foolscap/slicers/unicode.py +++ b/src/foolscap/slicers/unicode.py @@ -1,6 +1,5 @@ # -*- test-case-name: foolscap.test.test_banana -*- -import six import re from twisted.internet.defer import Deferred from foolscap.tokens import BananaError, STRING, VOCAB, Violation @@ -9,7 +8,7 @@ class UnicodeSlicer(BaseSlicer): opentype = ("unicode",) - slices = six.text_type + slices = str def sliceBody(self, streamable, banana): yield self.obj.encode("UTF-8") @@ -73,7 +72,7 @@ def __init__(self, maxLength=None, minLength=0, regexp=None): self.regexp = re.compile(regexp) def checkObject(self, obj, inbound): - if not isinstance(obj, six.text_type): + if not isinstance(obj, str): raise Violation("not a unicode object") if self.maxLength != None and len(obj) > self.maxLength: raise Violation("string too long (%d > %d)" % diff --git a/src/foolscap/slicers/vocab.py b/src/foolscap/slicers/vocab.py index 65233085..4b60fc7e 100644 --- a/src/foolscap/slicers/vocab.py +++ b/src/foolscap/slicers/vocab.py @@ -32,7 +32,7 @@ def slice(self, streamable, banana): # flip here at the sending end. stringToIndex = self.obj for s in stringToIndex.keys(): - assert isinstance(s, six.binary_type), "%r %s" % (s, type(s)) + assert isinstance(s, bytes), "%r %s" % (s, type(s)) indexToString = dict([(stringToIndex[s],s) for s in stringToIndex]) assert len(stringToIndex) == len(indexToString) # catch duplicates indices = list(indexToString.keys()) diff --git a/src/foolscap/test/common.py b/src/foolscap/test/common.py index e6e5699d..f2f799a5 100644 --- a/src/foolscap/test/common.py +++ b/src/foolscap/test/common.py @@ -1,6 +1,5 @@ # -*- test-case-name: foolscap.test.test_pb -*- -import six import time from zope.interface import implementer, implementer_only, implementedBy, Interface from twisted.python import log @@ -16,7 +15,7 @@ NumberConstraint, ByteStringConstraint, IntegerConstraint, \ UnicodeConstraint, ChoiceOf from foolscap.referenceable import TubRef -from foolscap.util import allocate_tcp_port, long_type +from foolscap.util import allocate_tcp_port from twisted.python import failure from twisted.internet.main import CONNECTION_DONE @@ -71,7 +70,7 @@ def getHost(self): MegaSchema1 = DictOf(ByteStringConstraint(), ListOf(TupleOf(SetOf(int, maxLength=10, mutable=True), - six.binary_type, bool, int, long_type, float, None, + bytes, bool, int, float, None, UnicodeConstraint(), ByteStringConstraint(), Any(), NumberConstraint(), diff --git a/src/foolscap/test/test_banana.py b/src/foolscap/test/test_banana.py index c3fa8601..f15baf81 100644 --- a/src/foolscap/test/test_banana.py +++ b/src/foolscap/test/test_banana.py @@ -14,7 +14,6 @@ from foolscap.slicers.allslicers import RootSlicer, DictUnslicer, TupleUnslicer from foolscap.constraint import IConstraint from foolscap.banana import int2b128, long_to_bytes -from ..util import long_type import io import struct @@ -123,7 +122,7 @@ def untokenize(tokens): else: raise RuntimeError("bad token") else: - if isinstance(t, six.integer_types): + if isinstance(t, int): if t >= 2**31: s = long_to_bytes(t) int2b128(len(s), data.append) @@ -143,7 +142,7 @@ def untokenize(tokens): elif isinstance(t, float): data.append(FLOAT) data.append(struct.pack("!d", t)) - elif isinstance(t, six.string_types) or isinstance(t, six.binary_type): + elif isinstance(t, six.string_types) or isinstance(t, bytes): t = six.ensure_binary(t) int2b128(len(t), data.append) data.append(STRING) @@ -1215,14 +1214,6 @@ def testInt(self): self.check(-1, bINT(-1)) self.check(-127, bINT(-127)) - def testLong(self): - self.check(long_type(258), b"\x02\x85\x01\x02") # TODO: 0x85 for LONGINT?? - self.check(long_type(-258), b"\x02\x86\x01\x02") # TODO: 0x85 for LONGINT?? - self.check(long_type(0), b"\x85") - self.check(long_type(0), b"\x00\x85") - self.check(long_type(0), b"\x86") - self.check(long_type(0), b"\x00\x86") - def testString(self): self.check(b"", b"\x82") self.check(b"", b"\x00\x82") @@ -1513,18 +1504,18 @@ def test_int(self): def test_bigint(self): # some of these are small enough to fit in an INT d = self.looptest(int(2**31-1)) # most positive representable INT - d.addCallback(lambda res: self.looptest(long_type(2**31+0))) - d.addCallback(lambda res: self.looptest(long_type(2**31+1))) + d.addCallback(lambda res: self.looptest(2**31+0)) + d.addCallback(lambda res: self.looptest(2**31+1)) - d.addCallback(lambda res: self.looptest(long_type(-2**31-1))) + d.addCallback(lambda res: self.looptest(-2**31-1)) # the following is the most negative representable INT - d.addCallback(lambda res: self.looptest(int(-2**31+0))) - d.addCallback(lambda res: self.looptest(int(-2**31+1))) + d.addCallback(lambda res: self.looptest(-2**31+0)) + d.addCallback(lambda res: self.looptest(-2**31+1)) - d.addCallback(lambda res: self.looptest(long_type(2**100))) - d.addCallback(lambda res: self.looptest(long_type(-2**100))) - d.addCallback(lambda res: self.looptest(long_type(2**1000))) - d.addCallback(lambda res: self.looptest(long_type(-2**1000))) + d.addCallback(lambda res: self.looptest(2**100)) + d.addCallback(lambda res: self.looptest(-2**100)) + d.addCallback(lambda res: self.looptest(2**1000)) + d.addCallback(lambda res: self.looptest(-2**1000)) return d def test_decimal(self): @@ -1592,7 +1583,7 @@ def _testIdentity_1(self, z): self.assertIdentical(z[0][0], z) def testUnicode(self): - x = [six.text_type('blah')] + x = ['blah'] d = self.loop(x) d.addCallback(self._testUnicode_1, x) return d diff --git a/src/foolscap/test/test_call.py b/src/foolscap/test/test_call.py index 05936aa7..0f5c43e6 100644 --- a/src/foolscap/test/test_call.py +++ b/src/foolscap/test/test_call.py @@ -21,7 +21,6 @@ from foolscap.api import RemoteException, DeadReferenceError from foolscap.call import CopiedFailure from foolscap.logging import log as flog -from ..util import long_type class Unsendable: pass @@ -221,7 +220,7 @@ def testMegaSchema(self): # try to exercise all our constraints at once rr, target = self.setupTarget(HelperTarget()) t = (set([1, 2, 3]), - b"str", True, 12, long_type(12), 19.3, None, + b"str", True, 12, 12, 19.3, None, u"unicode", b"bytestring", "any", 14.3, diff --git a/src/foolscap/test/test_schema.py b/src/foolscap/test/test_schema.py index 636d9b16..16fc4563 100644 --- a/src/foolscap/test/test_schema.py +++ b/src/foolscap/test/test_schema.py @@ -1,4 +1,3 @@ -import six import re from twisted.trial import unittest from foolscap import schema, copyable, broker @@ -101,7 +100,7 @@ def testUnicode(self): self.violates(c4, b"I'm a bytestring") self.violates(c4, u"spelled entirely without those letters") self.conforms(c4, u"add better cases") - c5 = schema.UnicodeConstraint(regexp=re.compile("\d+\s\w+")) + c5 = schema.UnicodeConstraint(regexp=re.compile(r"\d+\s\w+")) self.violates(c5, b"I'm a bytestring") self.conforms(c5, u": 123 boo") self.violates(c5, u"more than 1 spaces") @@ -279,7 +278,7 @@ def testMakeConstraint(self): self.check(c, schema.IntegerConstraint) self.failUnlessEqual(c.maxBytes, 1024) - c = make(six.binary_type) + c = make(bytes) self.check(c, schema.ByteStringConstraint) self.failUnlessEqual(c.maxLength, None) @@ -287,7 +286,7 @@ def testMakeConstraint(self): self.check(c, schema.ByteStringConstraint) self.failUnlessEqual(c.maxLength, 2000) - c = make(six.text_type) + c = make(str) self.check(c, schema.UnicodeConstraint) self.failUnlessEqual(c.maxLength, None) diff --git a/src/foolscap/util.py b/src/foolscap/util.py index b8d84dfc..a3abedcd 100644 --- a/src/foolscap/util.py +++ b/src/foolscap/util.py @@ -201,12 +201,6 @@ def _make_socket(): s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) return s -# long_type(0) is 0L on py2, just plain 0 on py3 -if len(six.integer_types) > 1: - long_type = [t for t in six.integer_types if t is not int][0] -else: - long_type = int - def ensure_tuple_str(input_tuple): return tuple([six.ensure_str(s) for s in input_tuple]) From 2b2282b839ee57fdc4d42395b1b60899f521aeba Mon Sep 17 00:00:00 2001 From: Alexandre Detiste Date: Sat, 21 Sep 2024 11:37:58 +0200 Subject: [PATCH 2/2] sync with src/foolscap/test/common.py --- src/foolscap/test/test_call.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/foolscap/test/test_call.py b/src/foolscap/test/test_call.py index 0f5c43e6..056cde7b 100644 --- a/src/foolscap/test/test_call.py +++ b/src/foolscap/test/test_call.py @@ -220,7 +220,7 @@ def testMegaSchema(self): # try to exercise all our constraints at once rr, target = self.setupTarget(HelperTarget()) t = (set([1, 2, 3]), - b"str", True, 12, 12, 19.3, None, + b"str", True, 12, 19.3, None, u"unicode", b"bytestring", "any", 14.3,