Skip to content

Commit

Permalink
Merge pull request #113 from a-detiste/master
Browse files Browse the repository at this point in the history
trim usage of six and util.long_type
  • Loading branch information
meejah authored Sep 23, 2024
2 parents b7c46c9 + 2b2282b commit f7f3bcf
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/foolscap/appserver/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
13 changes: 6 additions & 7 deletions src/foolscap/banana.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions src/foolscap/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
5 changes: 2 additions & 3 deletions src/foolscap/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)" %
Expand All @@ -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:
Expand Down
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
Expand Down Expand Up @@ -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)
Expand All @@ -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"])
Expand Down
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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/foolscap/logging/gatherer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 3 additions & 5 deletions src/foolscap/logging/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]"
Expand Down Expand Up @@ -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
Expand Down
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
Expand Down Expand Up @@ -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"])
Expand Down
2 changes: 1 addition & 1 deletion src/foolscap/pb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down
10 changes: 3 additions & 7 deletions src/foolscap/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
"""

import six
from foolscap.tokens import Violation, UnknownSchemaType, BananaError, \
tokenNames

Expand Down Expand Up @@ -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.

Expand Down
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
Expand All @@ -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")

Expand Down Expand Up @@ -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)" %
Expand Down
2 changes: 1 addition & 1 deletion src/foolscap/slicers/vocab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
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
Expand All @@ -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
Expand Down Expand Up @@ -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(),
Expand Down
33 changes: 12 additions & 21 deletions src/foolscap/test/test_banana.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit f7f3bcf

Please sign in to comment.