Skip to content

trim usage of six and util.long_type #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
trim usage of six and util.long_type
  • Loading branch information
a-detiste committed Sep 21, 2024
commit 911a45136653b8797381a92955a792469a1b927b
2 changes: 1 addition & 1 deletion src/foolscap/appserver/cli.py
Original file line number Diff line number Diff line change
@@ -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):
13 changes: 6 additions & 7 deletions src/foolscap/banana.py
Original file line number Diff line number Diff line change
@@ -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,15 +59,15 @@ 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
return acc

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:
4 changes: 2 additions & 2 deletions src/foolscap/broker.py
Original file line number Diff line number Diff line change
@@ -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:
5 changes: 2 additions & 3 deletions src/foolscap/constraint.py
Original file line number Diff line number Diff line change
@@ -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:
6 changes: 3 additions & 3 deletions src/foolscap/logging/dumper.py
Original file line number Diff line number Diff line change
@@ -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"])
3 changes: 1 addition & 2 deletions src/foolscap/logging/filter.py
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/foolscap/logging/gatherer.py
Original file line number Diff line number Diff line change
@@ -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
8 changes: 3 additions & 5 deletions src/foolscap/logging/log.py
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions src/foolscap/logging/tail.py
Original file line number Diff line number Diff line change
@@ -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"])
2 changes: 1 addition & 1 deletion src/foolscap/pb.py
Original file line number Diff line number Diff line change
@@ -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"):
10 changes: 3 additions & 7 deletions src/foolscap/schema.py
Original file line number Diff line number Diff line change
@@ -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.

5 changes: 2 additions & 3 deletions src/foolscap/slicers/unicode.py
Original file line number Diff line number Diff line change
@@ -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)" %
2 changes: 1 addition & 1 deletion src/foolscap/slicers/vocab.py
Original file line number Diff line number Diff line change
@@ -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())
5 changes: 2 additions & 3 deletions src/foolscap/test/common.py
Original file line number Diff line number Diff line change
@@ -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(),
33 changes: 12 additions & 21 deletions src/foolscap/test/test_banana.py
Original file line number Diff line number Diff line change
@@ -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
3 changes: 1 addition & 2 deletions src/foolscap/test/test_call.py
Original file line number Diff line number Diff line change
@@ -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,
7 changes: 3 additions & 4 deletions src/foolscap/test/test_schema.py
Original file line number Diff line number Diff line change
@@ -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,15 +278,15 @@ 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)

c = make(schema.ByteStringConstraint(2000))
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)

6 changes: 0 additions & 6 deletions src/foolscap/util.py
Original file line number Diff line number Diff line change
@@ -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])