From 33758abd18d9a2fb728f9c116396c3233a8f423d Mon Sep 17 00:00:00 2001 From: laharah Date: Fri, 3 Jun 2016 16:47:36 -0700 Subject: [PATCH 1/2] added a dict_factory as a new row_factory for the database --- src/wormhole/server/database.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/wormhole/server/database.py b/src/wormhole/server/database.py index 163c8cd..253b536 100644 --- a/src/wormhole/server/database.py +++ b/src/wormhole/server/database.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals import os, sys import sqlite3 from pkg_resources import resource_string @@ -10,6 +11,13 @@ def get_schema(version): "db-schemas/v%d.sql" % version) return schema_bytes.decode("utf-8") + +def dict_factory(cursor, row): + d = {} + for idx, col in enumerate(cursor.description): + d[col[0]] = row[idx] + return d + def get_db(dbfile, stderr=sys.stderr): """Open or create the given db file. The parent directory must exist. Returns the db connection object, or raises DBError. @@ -20,7 +28,7 @@ def get_db(dbfile, stderr=sys.stderr): db = sqlite3.connect(dbfile) except (EnvironmentError, sqlite3.OperationalError) as e: raise DBError("Unable to create/open db file %s: %s" % (dbfile, e)) - db.row_factory = sqlite3.Row + db.row_factory = dict_factory VERSION = 2 if must_create: @@ -30,7 +38,7 @@ def get_db(dbfile, stderr=sys.stderr): db.commit() try: - version = db.execute("SELECT version FROM version").fetchone()[0] + version = db.execute("SELECT version FROM version").fetchone()["version"] except sqlite3.DatabaseError as e: # this indicates that the file is not a compatible database format. # Perhaps it was created with an old version, or it might be junk. From 6a73d50fdd2343bdbde5416f63dee40fd3a31a5c Mon Sep 17 00:00:00 2001 From: laharah Date: Fri, 3 Jun 2016 23:07:50 -0700 Subject: [PATCH 2/2] added unicode_literals import to all apropriate modules bug in twisted serverFromString prevents test.common and transit from using unicode properly should revisit if twisted gets patched --- src/wormhole/_version.py | 2 +- src/wormhole/channel_monitor.py | 2 +- src/wormhole/codes.py | 2 +- src/wormhole/errors.py | 1 + src/wormhole/ipaddrs.py | 2 +- src/wormhole/server/cmd_server.py | 2 +- src/wormhole/server/cmd_usage.py | 2 +- src/wormhole/server/endpoint_service.py | 1 + src/wormhole/server/rendezvous.py | 2 +- src/wormhole/server/rendezvous_websocket.py | 1 + src/wormhole/server/runner.py | 2 +- src/wormhole/server/server.py | 2 +- src/wormhole/server/transit_server.py | 2 +- src/wormhole/test/common.py | 2 ++ src/wormhole/test/run_trial.py | 2 +- src/wormhole/test/test_hkdf.py | 3 +-- src/wormhole/test/test_scripts.py | 2 +- src/wormhole/test/test_server.py | 10 +++++----- src/wormhole/test/test_transit.py | 2 +- src/wormhole/test/test_util.py | 1 + src/wormhole/test/test_wormhole.py | 2 +- src/wormhole/timing.py | 2 +- src/wormhole/tor_manager.py | 2 +- src/wormhole/transit.py | 1 + src/wormhole/wordlist.py | 2 +- src/wormhole/wormhole.py | 2 +- 26 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/wormhole/_version.py b/src/wormhole/_version.py index 98cb741..051704a 100644 --- a/src/wormhole/_version.py +++ b/src/wormhole/_version.py @@ -1,4 +1,4 @@ - +from __future__ import unicode_literals # This file helps to compute a version number in source trees obtained from # git-archive tarball (such as those provided by githubs download-from-tag # feature). Distribution tarballs (built by setup.py sdist) and build diff --git a/src/wormhole/channel_monitor.py b/src/wormhole/channel_monitor.py index 964b2a5..f5f4c50 100644 --- a/src/wormhole/channel_monitor.py +++ b/src/wormhole/channel_monitor.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals import sys from weakref import ref diff --git a/src/wormhole/codes.py b/src/wormhole/codes.py index b68eaf4..8f922ae 100644 --- a/src/wormhole/codes.py +++ b/src/wormhole/codes.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals import os, six from .wordlist import (byte_to_even_word, byte_to_odd_word, even_words_lowercase, odd_words_lowercase) diff --git a/src/wormhole/errors.py b/src/wormhole/errors.py index 47b9c2e..4782830 100644 --- a/src/wormhole/errors.py +++ b/src/wormhole/errors.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals import functools class ServerError(Exception): diff --git a/src/wormhole/ipaddrs.py b/src/wormhole/ipaddrs.py index b3dee1a..ced9e57 100644 --- a/src/wormhole/ipaddrs.py +++ b/src/wormhole/ipaddrs.py @@ -1,4 +1,4 @@ - +# no unicode_literals # Find all of our ip addresses. From tahoe's src/allmydata/util/iputil.py import os, re, subprocess, errno diff --git a/src/wormhole/server/cmd_server.py b/src/wormhole/server/cmd_server.py index 02e6c66..ed2afe5 100644 --- a/src/wormhole/server/cmd_server.py +++ b/src/wormhole/server/cmd_server.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals import os, time from twisted.python import usage from twisted.scripts import twistd diff --git a/src/wormhole/server/cmd_usage.py b/src/wormhole/server/cmd_usage.py index ed2b6d2..8c12fe9 100644 --- a/src/wormhole/server/cmd_usage.py +++ b/src/wormhole/server/cmd_usage.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals import os, time, json from collections import defaultdict from .database import get_db diff --git a/src/wormhole/server/endpoint_service.py b/src/wormhole/server/endpoint_service.py index b191f9b..40c9234 100644 --- a/src/wormhole/server/endpoint_service.py +++ b/src/wormhole/server/endpoint_service.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals from twisted.python import log from twisted.internet import defer from twisted.application import service diff --git a/src/wormhole/server/rendezvous.py b/src/wormhole/server/rendezvous.py index 03c50cb..ccc49c3 100644 --- a/src/wormhole/server/rendezvous.py +++ b/src/wormhole/server/rendezvous.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals import os, time, random, base64 from collections import namedtuple from twisted.python import log diff --git a/src/wormhole/server/rendezvous_websocket.py b/src/wormhole/server/rendezvous_websocket.py index 97b382a..358ccf1 100644 --- a/src/wormhole/server/rendezvous_websocket.py +++ b/src/wormhole/server/rendezvous_websocket.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals import time from twisted.internet import reactor from twisted.python import log diff --git a/src/wormhole/server/runner.py b/src/wormhole/server/runner.py index b652782..d8247d4 100644 --- a/src/wormhole/server/runner.py +++ b/src/wormhole/server/runner.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals import os, sys from .cli_args import parser diff --git a/src/wormhole/server/server.py b/src/wormhole/server/server.py index bca7565..0659a7a 100644 --- a/src/wormhole/server/server.py +++ b/src/wormhole/server/server.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals from twisted.python import log from twisted.internet import reactor, endpoints from twisted.application import service diff --git a/src/wormhole/server/transit_server.py b/src/wormhole/server/transit_server.py index 7c55b44..3241078 100644 --- a/src/wormhole/server/transit_server.py +++ b/src/wormhole/server/transit_server.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals import re, time from twisted.python import log from twisted.internet import protocol diff --git a/src/wormhole/test/common.py b/src/wormhole/test/common.py index 18008a1..5b337bf 100644 --- a/src/wormhole/test/common.py +++ b/src/wormhole/test/common.py @@ -14,6 +14,8 @@ class ServerBase: self.sp.startService() relayport = allocate_tcp_port() transitport = allocate_tcp_port() + # need to talk to twisted team about only using unicode in + # endpoints.serverFromString s = RelayServer("tcp:%d:interface=127.0.0.1" % relayport, "tcp:%s:interface=127.0.0.1" % transitport, advertise_version=__version__, diff --git a/src/wormhole/test/run_trial.py b/src/wormhole/test/run_trial.py index 3a8e743..9b58cfa 100644 --- a/src/wormhole/test/run_trial.py +++ b/src/wormhole/test/run_trial.py @@ -1,4 +1,4 @@ - +from __future__ import unicode_literals # This is a tiny helper module, to let "python -m wormhole.test.run_trial # ARGS" does the same thing as running "trial ARGS" (unfortunately # twisted/scripts/trial.py does not have a '__name__=="__main__"' clause). diff --git a/src/wormhole/test/test_hkdf.py b/src/wormhole/test/test_hkdf.py index 03d4892..7751383 100644 --- a/src/wormhole/test/test_hkdf.py +++ b/src/wormhole/test/test_hkdf.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals import unittest from binascii import unhexlify #, hexlify from hkdf import Hkdf @@ -42,4 +42,3 @@ class TestKAT(unittest.TestCase): #if __name__ == '__main__': # generate_KAT() - diff --git a/src/wormhole/test/test_scripts.py b/src/wormhole/test/test_scripts.py index aa1c26c..7e5ce01 100644 --- a/src/wormhole/test/test_scripts.py +++ b/src/wormhole/test/test_scripts.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals import os, sys, re, io, zipfile, six, stat import mock from twisted.trial import unittest diff --git a/src/wormhole/test/test_server.py b/src/wormhole/test/test_server.py index 40e5bd8..c625695 100644 --- a/src/wormhole/test/test_server.py +++ b/src/wormhole/test/test_server.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals import json, itertools from binascii import hexlify import mock @@ -375,9 +375,9 @@ class Prune(unittest.TestCase): for messages in [None, OLD, NEW]: self.one(nameplate, mailbox, has_listeners, messages) - #def test_one(self): + # def test_one(self): # # to debug specific problems found by test_lots - # self.one(None, "old", True, None) + # self.one(None, "old", False, 'new') def one(self, nameplate, mailbox, has_listeners, messages): desc = ("nameplate=%s, mailbox=%s, has_listeners=%s," @@ -427,8 +427,8 @@ class Prune(unittest.TestCase): messages = None messages_survive = False - if (nameplate is NEW or mailbox is NEW - or has_listeners or messages is NEW): + if (nameplate == NEW or mailbox == NEW + or has_listeners or messages == NEW): if nameplate is not None: nameplate_survives = True if mailbox is not None: diff --git a/src/wormhole/test/test_transit.py b/src/wormhole/test/test_transit.py index 0f13523..ee80b0a 100644 --- a/src/wormhole/test/test_transit.py +++ b/src/wormhole/test/test_transit.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals import io from binascii import hexlify, unhexlify from twisted.trial import unittest diff --git a/src/wormhole/test/test_util.py b/src/wormhole/test/test_util.py index 3c916a4..c9a6cbc 100644 --- a/src/wormhole/test/test_util.py +++ b/src/wormhole/test/test_util.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals import unicodedata from twisted.trial import unittest from .. import util diff --git a/src/wormhole/test/test_wormhole.py b/src/wormhole/test/test_wormhole.py index 795b6b6..1ce51b2 100644 --- a/src/wormhole/test/test_wormhole.py +++ b/src/wormhole/test/test_wormhole.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals import os, json, re, gc from binascii import hexlify, unhexlify import mock diff --git a/src/wormhole/timing.py b/src/wormhole/timing.py index 1699ce7..0ecf1bc 100644 --- a/src/wormhole/timing.py +++ b/src/wormhole/timing.py @@ -1,4 +1,4 @@ -from __future__ import print_function, absolute_import +from __future__ import print_function, absolute_import, unicode_literals import json, time class Event: diff --git a/src/wormhole/tor_manager.py b/src/wormhole/tor_manager.py index 97cc2e9..eadccb4 100644 --- a/src/wormhole/tor_manager.py +++ b/src/wormhole/tor_manager.py @@ -1,4 +1,4 @@ -from __future__ import print_function +from __future__ import print_function, unicode_literals import time from twisted.internet.defer import inlineCallbacks, returnValue from twisted.internet.error import ConnectError diff --git a/src/wormhole/transit.py b/src/wormhole/transit.py index a904be4..050cd00 100644 --- a/src/wormhole/transit.py +++ b/src/wormhole/transit.py @@ -1,3 +1,4 @@ +# no unicode_literals, revisit after twisted patch from __future__ import print_function, absolute_import import re, sys, time, socket from collections import namedtuple, deque diff --git a/src/wormhole/wordlist.py b/src/wormhole/wordlist.py index 049d126..3da0cff 100644 --- a/src/wormhole/wordlist.py +++ b/src/wormhole/wordlist.py @@ -1,4 +1,4 @@ - +from __future__ import unicode_literals # The PGP Word List, which maps bytes to phonetically-distinct words. There # are two lists, even and odd, and encodings should alternate between then to # detect dropped words. https://en.wikipedia.org/wiki/PGP_Words diff --git a/src/wormhole/wormhole.py b/src/wormhole/wormhole.py index 14658cb..8f71c1c 100644 --- a/src/wormhole/wormhole.py +++ b/src/wormhole/wormhole.py @@ -1,4 +1,4 @@ -from __future__ import print_function, absolute_import +from __future__ import print_function, absolute_import, unicode_literals import os, sys, re from six.moves.urllib_parse import urlparse from twisted.internet import defer, endpoints, error