Skip to content

Commit f7f3bcf

Browse files
authored
Merge pull request #113 from a-detiste/master
trim usage of six and util.long_type
2 parents b7c46c9 + 2b2282b commit f7f3bcf

File tree

18 files changed

+46
-74
lines changed

18 files changed

+46
-74
lines changed

src/foolscap/appserver/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ def run_flappserver(argv=None, run_by_human=True):
454454
r = dispatch(command, so)
455455
except (usage.UsageError, BadServiceArguments) as e:
456456
r = 1
457-
print("Error:", six.text_type(e), file=so.stderr)
457+
print("Error:", str(e), file=so.stderr)
458458
from twisted.internet import defer
459459
if run_by_human:
460460
if isinstance(r, defer.Deferred):

src/foolscap/banana.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
from foolscap.slicers.allslicers import RootSlicer, RootUnslicer
1212
from foolscap.slicers.allslicers import ReplaceVocabSlicer, AddVocabSlicer
1313

14-
from .util import long_type
1514
from . import stringchain
1615
from . import tokens
1716
from .tokens import SIZE_LIMIT, STRING, LIST, INT, NEG, \
@@ -60,15 +59,15 @@ def bytes_to_long(s):
6059
6160
This is (essentially) the inverse of long_to_bytes().
6261
"""
63-
acc = long_type(0)
62+
acc = 0
6463
for i in six.iterbytes(s):
6564
acc <<= 8
6665
acc += i
6766
return acc
6867

6968
HIGH_BIT_SET = b"\x80"
7069

71-
SIMPLE_TOKENS = six.integer_types + (float, six.binary_type)
70+
SIMPLE_TOKENS = (int, float, bytes)
7271

7372
# Banana is a big class. It is split up into three sections: sending,
7473
# receiving, and connection setup. These used to be separate classes, but
@@ -417,7 +416,7 @@ def outgoingVocabTableWasReplaced(self, newTable):
417416
# this is called by the ReplaceVocabSlicer to manipulate our table.
418417
# It must certainly *not* be called by higher-level user code.
419418
for k in newTable.keys():
420-
assert isinstance(k, six.binary_type)
419+
assert isinstance(k, bytes)
421420
self.outgoingVocabulary = newTable
422421
if newTable:
423422
maxIndex = max(newTable.values()) + 1
@@ -466,7 +465,7 @@ def sendOpen(self):
466465

467466
def sendToken(self, obj):
468467
write = self.transport.write
469-
if isinstance(obj, six.integer_types):
468+
if isinstance(obj, int):
470469
if obj >= 2**31:
471470
s = long_to_bytes(obj)
472471
int2b128(len(s), write)
@@ -486,7 +485,7 @@ def sendToken(self, obj):
486485
elif isinstance(obj, float):
487486
write(FLOAT)
488487
write(struct.pack("!d", obj))
489-
elif isinstance(obj, six.binary_type):
488+
elif isinstance(obj, bytes):
490489
if obj in self.outgoingVocabulary:
491490
symbolID = self.outgoingVocabulary[obj]
492491
int2b128(symbolID, write)
@@ -924,7 +923,7 @@ def handleData(self, chunk):
924923
elif typebyte == NEG:
925924
# -2**31 is too large for a positive int, so go through
926925
# LongType first
927-
obj = int(-long_type(header))
926+
obj = int(-int(header))
928927
elif typebyte == LONGINT or typebyte == LONGNEG:
929928
strlen = header
930929
if len(self.buffer) >= strlen:

src/foolscap/broker.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ def getMyReferenceByCLID(self, clid):
441441
was registered with our Factory.
442442
"""
443443

444-
assert isinstance(clid, six.integer_types)
444+
assert isinstance(clid, int)
445445
if clid == 0:
446446
return self
447447
return self.myReferenceByCLID[clid].obj
@@ -451,7 +451,7 @@ def getMyReferenceByCLID(self, clid):
451451

452452
def remote_decref(self, clid, count):
453453
# invoked when the other side sends us a decref message
454-
assert isinstance(clid, six.integer_types)
454+
assert isinstance(clid, int)
455455
assert clid != 0
456456
tracker = self.myReferenceByCLID.get(clid, None)
457457
if not tracker:

src/foolscap/constraint.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
# This imports foolscap.tokens, but no other Foolscap modules.
77

8-
import six
98
from zope.interface import implementer, Interface
109

1110
from foolscap.util import ensure_tuple_str
@@ -211,7 +210,7 @@ def __init__(self, maxLength=None, minLength=0):
211210
VOCAB: None}
212211

213212
def checkObject(self, obj, inbound):
214-
if not isinstance(obj, six.binary_type):
213+
if not isinstance(obj, bytes):
215214
raise Violation("'%r' is not a bytestring" % (obj,))
216215
if self.maxLength != None and len(obj) > self.maxLength:
217216
raise Violation("string too long (%d > %d)" %
@@ -236,7 +235,7 @@ def __init__(self, maxBytes=-1):
236235
self.taster[LONGNEG] = maxBytes
237236

238237
def checkObject(self, obj, inbound):
239-
if not isinstance(obj, six.integer_types):
238+
if not isinstance(obj, int):
240239
raise Violation("'%r' is not a number" % (obj,))
241240
if self.maxBytes == -1:
242241
if obj >= 2**31 or obj < -2**31:

src/foolscap/logging/dumper.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import six, sys, errno, textwrap
1+
import sys, errno, textwrap
22
from twisted.python import usage
33
from foolscap.logging import flogfile
44
from foolscap.logging.log import format_message
@@ -76,7 +76,7 @@ def print_header(self, e, options):
7676
t = h["trigger"]
7777
self.trigger = (t["incarnation"], t["num"])
7878
if options['verbose']:
79-
print(six.text_type(e), file=stdout)
79+
print(str(e), file=stdout)
8080
if not options["just-numbers"] and not options["verbose"]:
8181
if "versions" in h:
8282
print(u"Application versions (embedded in logfile):", file=stdout)
@@ -95,7 +95,7 @@ def print_event(self, e, options):
9595
d = e['d']
9696
when = format_time(d['time'], options["timestamps"])
9797
if options['just-numbers']:
98-
print(six.text_type(when), six.text_type(d.get('num')), file=stdout)
98+
print(str(when), str(d.get('num')), file=stdout)
9999
return
100100

101101
eid = (d["incarnation"], d["num"])

src/foolscap/logging/filter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import six
21
from twisted.python import usage
32
import sys, os, bz2, time
43
from foolscap.logging import log, flogfile
@@ -82,7 +81,7 @@ def run(self, options):
8281
for e in flogfile.get_events(options.oldfile):
8382
if options['verbose']:
8483
if "d" in e:
85-
print(six.text_type(e['d']['num']), file=stdout)
84+
print(str(e['d']['num']), file=stdout)
8685
else:
8786
print(u"HEADER", file=stdout)
8887
total += 1

src/foolscap/logging/gatherer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def maybe_fetch_incident(self):
377377
return
378378
self.incident_fetch_outstanding = True
379379
(name, trigger) = self.incidents_wanted.pop(0)
380-
print("fetching incident", six.text_type(name), file=self.stdout)
380+
print("fetching incident", str(name), file=self.stdout)
381381
d = self.publisher.callRemote("get_incident", six.ensure_binary(name))
382382
def _clear_outstanding(res):
383383
self.incident_fetch_outstanding = False

src/foolscap/logging/log.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ def format_message(e):
4646
args = e['args']
4747
elif "message" in e:
4848
fmt = "%(message)s"
49-
assert isinstance(e['message'], (six.binary_type, six.text_type))
49+
assert isinstance(e['message'], (bytes, str))
5050
args = {"message": six.ensure_str(e['message'])}
5151
# i.e. just return e['message']
5252
else:
5353
fmt = ""
5454
args = {}
55-
assert isinstance(fmt, (six.binary_type, six.text_type))
55+
assert isinstance(fmt, (bytes, str))
5656
return six.ensure_text(fmt % args)
5757
except (ValueError, TypeError):
5858
return six.ensure_text(e.get('message', "[no message]")) + " [formatting failed]"
@@ -380,9 +380,7 @@ def observer(self, d):
380380
# level.
381381
log_level = d.pop("log_level")
382382
new_log_level = llmap.get(log_level, log_level)
383-
if not isinstance(new_log_level,
384-
six.integer_types +
385-
(six.binary_type, six.text_type, bool)):
383+
if not isinstance(new_log_level, (int, bytes, str, bool)):
386384
# it was something weird: just stringify it in-place
387385
new_log_level = str(new_log_level)
388386
kwargs["level"] = new_log_level # foolscap level, not twisted

src/foolscap/logging/tail.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import six, os, sys, time
1+
import os, sys, time
22
from zope.interface import implementer
33
from twisted.internet import reactor
44
from twisted.python import usage
@@ -93,7 +93,7 @@ def remote_msg(self, d):
9393
self.saver.remote_msg(d)
9494

9595
def simple_print(self, d):
96-
print(six.text_type(d), file=self.output)
96+
print(str(d), file=self.output)
9797

9898
def formatted_print(self, d):
9999
time_s = format_time(d['time'], self.options["timestamps"])

src/foolscap/pb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def listenOn(self, what, _test_options={}):
504504
@return: The Listener object that was created. This can be used to
505505
stop listening later on."""
506506

507-
if isinstance(what, (six.binary_type, six.text_type)):
507+
if isinstance(what, (bytes, str)):
508508
what = six.ensure_str(what)
509509

510510
if what in ("0", "tcp:0"):

src/foolscap/schema.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
5555
"""
5656

57-
import six
5857
from foolscap.tokens import Violation, UnknownSchemaType, BananaError, \
5958
tokenNames
6059

@@ -135,23 +134,20 @@ def AnyStringConstraint(*args, **kwargs):
135134
StringConstraint = ByteStringConstraint
136135

137136
constraintMap = {
138-
six.binary_type: ByteStringConstraint(),
139-
six.text_type: UnicodeConstraint(),
137+
bytes: ByteStringConstraint(),
138+
str: UnicodeConstraint(),
140139
bool: BooleanConstraint(),
140+
int: IntegerConstraint(maxBytes=1024),
141141
float: NumberConstraint(),
142142
None: Nothing(),
143143
}
144144

145-
146145
# we don't maintain compatibility for constraints defined by types. Back in
147146
# the py2-only days, 'int' meant a 32-bit signed integer, 'long' meant
148147
# fit-in-1024-bytes. The new rule is that 'int' means fit-in-1024-bytes (and
149148
# there is no 'long' in py3, of course). To get a 32-bit signed integer
150149
# constraint, use Int(maxBytes=-1).
151150

152-
for t in six.integer_types:
153-
constraintMap[t] = IntegerConstraint(maxBytes=1024)
154-
155151
# This module provides a function named addToConstraintTypeMap() which helps
156152
# to resolve some import cycles.
157153

src/foolscap/slicers/unicode.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- test-case-name: foolscap.test.test_banana -*-
22

3-
import six
43
import re
54
from twisted.internet.defer import Deferred
65
from foolscap.tokens import BananaError, STRING, VOCAB, Violation
@@ -9,7 +8,7 @@
98

109
class UnicodeSlicer(BaseSlicer):
1110
opentype = ("unicode",)
12-
slices = six.text_type
11+
slices = str
1312
def sliceBody(self, streamable, banana):
1413
yield self.obj.encode("UTF-8")
1514

@@ -73,7 +72,7 @@ def __init__(self, maxLength=None, minLength=0, regexp=None):
7372
self.regexp = re.compile(regexp)
7473

7574
def checkObject(self, obj, inbound):
76-
if not isinstance(obj, six.text_type):
75+
if not isinstance(obj, str):
7776
raise Violation("not a unicode object")
7877
if self.maxLength != None and len(obj) > self.maxLength:
7978
raise Violation("string too long (%d > %d)" %

src/foolscap/slicers/vocab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def slice(self, streamable, banana):
3232
# flip here at the sending end.
3333
stringToIndex = self.obj
3434
for s in stringToIndex.keys():
35-
assert isinstance(s, six.binary_type), "%r %s" % (s, type(s))
35+
assert isinstance(s, bytes), "%r %s" % (s, type(s))
3636
indexToString = dict([(stringToIndex[s],s) for s in stringToIndex])
3737
assert len(stringToIndex) == len(indexToString) # catch duplicates
3838
indices = list(indexToString.keys())

src/foolscap/test/common.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- test-case-name: foolscap.test.test_pb -*-
22

3-
import six
43
import time
54
from zope.interface import implementer, implementer_only, implementedBy, Interface
65
from twisted.python import log
@@ -16,7 +15,7 @@
1615
NumberConstraint, ByteStringConstraint, IntegerConstraint, \
1716
UnicodeConstraint, ChoiceOf
1817
from foolscap.referenceable import TubRef
19-
from foolscap.util import allocate_tcp_port, long_type
18+
from foolscap.util import allocate_tcp_port
2019

2120
from twisted.python import failure
2221
from twisted.internet.main import CONNECTION_DONE
@@ -71,7 +70,7 @@ def getHost(self):
7170

7271
MegaSchema1 = DictOf(ByteStringConstraint(),
7372
ListOf(TupleOf(SetOf(int, maxLength=10, mutable=True),
74-
six.binary_type, bool, int, long_type, float, None,
73+
bytes, bool, int, float, None,
7574
UnicodeConstraint(),
7675
ByteStringConstraint(),
7776
Any(), NumberConstraint(),

src/foolscap/test/test_banana.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from foolscap.slicers.allslicers import RootSlicer, DictUnslicer, TupleUnslicer
1515
from foolscap.constraint import IConstraint
1616
from foolscap.banana import int2b128, long_to_bytes
17-
from ..util import long_type
1817

1918
import io
2019
import struct
@@ -123,7 +122,7 @@ def untokenize(tokens):
123122
else:
124123
raise RuntimeError("bad token")
125124
else:
126-
if isinstance(t, six.integer_types):
125+
if isinstance(t, int):
127126
if t >= 2**31:
128127
s = long_to_bytes(t)
129128
int2b128(len(s), data.append)
@@ -143,7 +142,7 @@ def untokenize(tokens):
143142
elif isinstance(t, float):
144143
data.append(FLOAT)
145144
data.append(struct.pack("!d", t))
146-
elif isinstance(t, six.string_types) or isinstance(t, six.binary_type):
145+
elif isinstance(t, six.string_types) or isinstance(t, bytes):
147146
t = six.ensure_binary(t)
148147
int2b128(len(t), data.append)
149148
data.append(STRING)
@@ -1215,14 +1214,6 @@ def testInt(self):
12151214
self.check(-1, bINT(-1))
12161215
self.check(-127, bINT(-127))
12171216

1218-
def testLong(self):
1219-
self.check(long_type(258), b"\x02\x85\x01\x02") # TODO: 0x85 for LONGINT??
1220-
self.check(long_type(-258), b"\x02\x86\x01\x02") # TODO: 0x85 for LONGINT??
1221-
self.check(long_type(0), b"\x85")
1222-
self.check(long_type(0), b"\x00\x85")
1223-
self.check(long_type(0), b"\x86")
1224-
self.check(long_type(0), b"\x00\x86")
1225-
12261217
def testString(self):
12271218
self.check(b"", b"\x82")
12281219
self.check(b"", b"\x00\x82")
@@ -1513,18 +1504,18 @@ def test_int(self):
15131504
def test_bigint(self):
15141505
# some of these are small enough to fit in an INT
15151506
d = self.looptest(int(2**31-1)) # most positive representable INT
1516-
d.addCallback(lambda res: self.looptest(long_type(2**31+0)))
1517-
d.addCallback(lambda res: self.looptest(long_type(2**31+1)))
1507+
d.addCallback(lambda res: self.looptest(2**31+0))
1508+
d.addCallback(lambda res: self.looptest(2**31+1))
15181509

1519-
d.addCallback(lambda res: self.looptest(long_type(-2**31-1)))
1510+
d.addCallback(lambda res: self.looptest(-2**31-1))
15201511
# the following is the most negative representable INT
1521-
d.addCallback(lambda res: self.looptest(int(-2**31+0)))
1522-
d.addCallback(lambda res: self.looptest(int(-2**31+1)))
1512+
d.addCallback(lambda res: self.looptest(-2**31+0))
1513+
d.addCallback(lambda res: self.looptest(-2**31+1))
15231514

1524-
d.addCallback(lambda res: self.looptest(long_type(2**100)))
1525-
d.addCallback(lambda res: self.looptest(long_type(-2**100)))
1526-
d.addCallback(lambda res: self.looptest(long_type(2**1000)))
1527-
d.addCallback(lambda res: self.looptest(long_type(-2**1000)))
1515+
d.addCallback(lambda res: self.looptest(2**100))
1516+
d.addCallback(lambda res: self.looptest(-2**100))
1517+
d.addCallback(lambda res: self.looptest(2**1000))
1518+
d.addCallback(lambda res: self.looptest(-2**1000))
15281519
return d
15291520

15301521
def test_decimal(self):
@@ -1592,7 +1583,7 @@ def _testIdentity_1(self, z):
15921583
self.assertIdentical(z[0][0], z)
15931584

15941585
def testUnicode(self):
1595-
x = [six.text_type('blah')]
1586+
x = ['blah']
15961587
d = self.loop(x)
15971588
d.addCallback(self._testUnicode_1, x)
15981589
return d

0 commit comments

Comments
 (0)