Skip to content

Commit

Permalink
Merge pull request zooko#1 from george-hopkins/python3-tpltnt
Browse files Browse the repository at this point in the history
Support for Python 3
  • Loading branch information
tpltnt authored Jan 12, 2018
2 parents 751d1fe + 0ab1af4 commit 5d53b1c
Show file tree
Hide file tree
Showing 25 changed files with 135 additions and 131 deletions.
10 changes: 6 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
dist: trusty
language: python

cache: pip

python:
- "2.7"
os:
- "linux"
- "3.5"
- "3.6"
- "3.7-dev"

install:
- pip install twisted setuptools_trial simplejson

script:
- python ./setup.py trial -s pyutil.test
- python ./setup.py trial -s pyutil.test.current
6 changes: 3 additions & 3 deletions pyutil/PickleSaver.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
import warnings

# from the pyutil library
import fileutil
import nummedobj
import twistedutil
import .fileutil
import .nummedobj
import .twistedutil

# from the Twisted library
from twisted.python import log
Expand Down
66 changes: 22 additions & 44 deletions pyutil/assertutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,34 @@
Tests useful in assertion checking, prints out nicely formated messages too.
"""

from humanreadable import hr
from .humanreadable import hr

def _assert(___cond=False, *___args, **___kwargs):
def _format_error(prefix, args, kwargs):
if prefix:
msgbuf=[prefix]
if args or kwargs:
msgbuf.append(": ")
else:
msgbuf=[]
if args:
msgbuf.append(", ".join(["%s %s" % tuple(map(hr, (arg, type(arg),))) for arg in args]))
if kwargs:
if args:
msgbuf.append(", ")
msgbuf.append(", ".join(["%s: %s %s" % tuple(map(hr, (k, kwargs[k], type(kwargs[k]),))) for k in sorted(kwargs.keys())]))
return "".join(msgbuf)

def _assert(___cond=False, *args, **kwargs):
if ___cond:
return True
msgbuf=[]
if ___args:
msgbuf.append("%s %s" % tuple(map(hr, (___args[0], type(___args[0]),))))
msgbuf.extend([", %s %s" % tuple(map(hr, (arg, type(arg),))) for arg in ___args[1:]])
if ___kwargs:
msgbuf.append(", %s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),)))))
else:
if ___kwargs:
msgbuf.append("%s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),)))))
msgbuf.extend([", %s: %s %s" % tuple(map(hr, (k, v, type(v),))) for k, v in ___kwargs.items()[1:]])
raise AssertionError(_format_error(None, args, kwargs))

raise AssertionError, "".join(msgbuf)

def precondition(___cond=False, *___args, **___kwargs):
def precondition(___cond=False, *args, **kwargs):
if ___cond:
return True
msgbuf=["precondition", ]
if ___args or ___kwargs:
msgbuf.append(": ")
if ___args:
msgbuf.append("%s %s" % tuple(map(hr, (___args[0], type(___args[0]),))))
msgbuf.extend([", %s %s" % tuple(map(hr, (arg, type(arg),))) for arg in ___args[1:]])
if ___kwargs:
msgbuf.append(", %s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),)))))
else:
if ___kwargs:
msgbuf.append("%s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),)))))
msgbuf.extend([", %s: %s %s" % tuple(map(hr, (k, v, type(v),))) for k, v in ___kwargs.items()[1:]])
raise AssertionError(_format_error("precondition", args, kwargs))

raise AssertionError, "".join(msgbuf)

def postcondition(___cond=False, *___args, **___kwargs):
def postcondition(___cond=False, *args, **kwargs):
if ___cond:
return True
msgbuf=["postcondition", ]
if ___args or ___kwargs:
msgbuf.append(": ")
if ___args:
msgbuf.append("%s %s" % tuple(map(hr, (___args[0], type(___args[0]),))))
msgbuf.extend([", %s %s" % tuple(map(hr, (arg, type(arg),))) for arg in ___args[1:]])
if ___kwargs:
msgbuf.append(", %s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),)))))
else:
if ___kwargs:
msgbuf.append("%s: %s %s" % ((___kwargs.items()[0][0],) + tuple(map(hr, (___kwargs.items()[0][1], type(___kwargs.items()[0][1]),)))))
msgbuf.extend([", %s: %s %s" % tuple(map(hr, (k, v, type(v),))) for k, v in ___kwargs.items()[1:]])

raise AssertionError, "".join(msgbuf)
raise AssertionError(_format_error("postcondition", args, kwargs))
2 changes: 1 addition & 1 deletion pyutil/benchutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
else:
clock = time.time

from assertutil import _assert
from .assertutil import _assert

def makeg(func):
def blah(n, func=func):
Expand Down
4 changes: 2 additions & 2 deletions pyutil/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

import operator

from assertutil import _assert, precondition
from humanreadable import hr
from .assertutil import _assert, precondition
from .humanreadable import hr

class LRUCache:
"""
Expand Down
18 changes: 9 additions & 9 deletions pyutil/fileutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
Futz with files like a pro.
"""

import errno, exceptions, os, stat, tempfile
import errno, os, stat, tempfile

try:
import bsddb
Expand Down Expand Up @@ -176,7 +176,7 @@ def __del__(self):
def shutdown(self):
remove(self.name)

def make_dirs(dirname, mode=0777):
def make_dirs(dirname, mode=0o777):
"""
An idempotent version of os.makedirs(). If the dir already exists, do
nothing and return without raising an exception. If this call creates the
Expand All @@ -187,13 +187,13 @@ def make_dirs(dirname, mode=0777):
tx = None
try:
os.makedirs(dirname, mode)
except OSError, x:
except OSError as x:
tx = x

if not os.path.isdir(dirname):
if tx:
raise tx
raise exceptions.IOError, "unknown error prevented creation of directory, or deleted the directory immediately after creation: %s" % dirname # careful not to construct an IOError with a 2-tuple, as that has a special meaning...
raise IOError("unknown error prevented creation of directory, or deleted the directory immediately after creation: %s" % dirname) # careful not to construct an IOError with a 2-tuple, as that has a special meaning...

def rmtree(dirname):
"""
Expand All @@ -214,11 +214,11 @@ def rmtree(dirname):
else:
remove(fullname)
os.rmdir(dirname)
except EnvironmentError, le:
except EnvironmentError as le:
# Ignore "No such file or directory", collect any other exception.
if (le.args[0] != 2 and le.args[0] != 3) or (le.args[0] != errno.ENOENT):
excs.append(le)
except Exception, le:
except Exception as le:
excs.append(le)

# Okay, now we've recursively removed everything, ignoring any "No
Expand All @@ -228,8 +228,8 @@ def rmtree(dirname):
if len(excs) == 1:
raise excs[0]
if len(excs) == 0:
raise OSError, "Failed to remove dir for unknown reason."
raise OSError, excs
raise OSError("Failed to remove dir for unknown reason.")
raise OSError(excs)

def rm_dir(dirname):
# Renamed to be like shutil.rmtree and unlike rmdir.
Expand All @@ -244,7 +244,7 @@ def remove_if_possible(f):
def remove_if_present(f):
try:
remove(f)
except EnvironmentError, le:
except EnvironmentError as le:
# Ignore "No such file or directory", re-raise any other exception.
if (le.args[0] != 2 and le.args[0] != 3) or (le.args[0] != errno.ENOENT):
raise
Expand Down
12 changes: 8 additions & 4 deletions pyutil/humanreadable.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@

# This file is part of pyutil; see README.rst for licensing terms.

import exceptions, os
from repr import Repr
import os

try:
from reprlib import Repr
except ImportError:
from repr import Repr

class BetterRepr(Repr):
def __init__(self):
Expand All @@ -31,7 +35,7 @@ def repr_instance_method(self, obj, level):
return '<' + obj.im_class.__name__ + '.' + obj.im_func.__name__ + '() at (builtin)'

def repr_long(self, obj, level):
s = `obj` # XXX Hope this isn't too slow...
s = repr(obj) # XXX Hope this isn't too slow...
if len(s) > self.maxlong:
i = max(0, (self.maxlong-3)/2)
j = max(0, self.maxlong-3-i)
Expand All @@ -48,7 +52,7 @@ def repr_instance(self, obj, level):
on it. If it is an instance of list call self.repr_list() on it. Else
call Repr.repr_instance().
"""
if isinstance(obj, exceptions.Exception):
if isinstance(obj, Exception):
# Don't cut down exception strings so much.
tms = self.maxstring
self.maxstring = max(512, tms * 4)
Expand Down
4 changes: 2 additions & 2 deletions pyutil/memutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import exceptions, gc, math, operator, os, sys, types

# from the pyutil library
from assertutil import precondition
import mathutil
from .assertutil import precondition
import .mathutil

class Canary:
"""
Expand Down
2 changes: 1 addition & 1 deletion pyutil/nummedobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# This file is part of pyutil; see README.rst for licensing terms.

import dictutil
import .dictutil

class NummedObj(object):
"""
Expand Down
12 changes: 5 additions & 7 deletions pyutil/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ def __init__(self):
self._fired = False
self._result = None
self._watchers = []
self.__repr__ = self._unfired_repr

def _unfired_repr(self):
return "<OneShotObserverList [%s]>" % (self._watchers, )

def _fired_repr(self):
return "<OneShotObserverList -> %s>" % (self._result, )
def __repr__(self):
if self._fired:
return "<OneShotObserverList -> %s>" % (self._result, )
else:
return "<OneShotObserverList [%s]>" % (self._watchers, )

def _get_result(self):
return self._result
Expand All @@ -51,7 +50,6 @@ def _fire(self, result):
for w in self._watchers:
eventually(w.callback, result)
del self._watchers
self.__repr__ = self._fired_repr

def fire_if_not_fired(self, result):
if not self._fired:
Expand Down
20 changes: 10 additions & 10 deletions pyutil/odict.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

import operator

from assertutil import _assert, precondition
from humanreadable import hr
from .assertutil import _assert, precondition
from .humanreadable import hr

class OrderedDict:
"""
Expand Down Expand Up @@ -143,7 +143,7 @@ def move_to_most_recent(self, k, strictkey=False):

if not self.d.has_key(k):
if strictkey:
raise KeyError, k
raise KeyError(k)
return

node = self.d[k]
Expand Down Expand Up @@ -178,7 +178,7 @@ def __getitem__(self, key, default=None, strictkey=True):
node = self.d.get(key)
if not node:
if strictkey:
raise KeyError, key
raise KeyError(key)
return default
return node[0]

Expand Down Expand Up @@ -221,7 +221,7 @@ def __delitem__(self, key, default=None, strictkey=True):
return node[0]
elif strictkey:
assert self._assert_invariants()
raise KeyError, key
raise KeyError(key)
else:
assert self._assert_invariants()
return default
Expand Down Expand Up @@ -256,7 +256,7 @@ def update(self, otherdict):
def pop(self):
assert self._assert_invariants()
if len(self.d) < 2: # the +2 is for the sentinels
raise KeyError, 'popitem(): dictionary is empty'
raise KeyError('popitem(): dictionary is empty')
k = self.d[self.hs][2]
self.remove(k)
assert self._assert_invariants()
Expand All @@ -265,7 +265,7 @@ def pop(self):
def popitem(self):
assert self._assert_invariants()
if len(self.d) < 2: # the +2 is for the sentinels
raise KeyError, 'popitem(): dictionary is empty'
raise KeyError('popitem(): dictionary is empty')
k = self.d[self.hs][2]
val = self.remove(k)
assert self._assert_invariants()
Expand Down Expand Up @@ -475,7 +475,7 @@ def __delitem__(self, key, default=None, strictkey=True):
return val
elif strictkey:
assert self._assert_invariants()
raise KeyError, key
raise KeyError(key)
else:
assert self._assert_invariants()
return default
Expand Down Expand Up @@ -542,14 +542,14 @@ def refresh(self, key, strictkey=True):
assert self._assert_invariants()
if not dict.has_key(self, key):
if strictkey:
raise KeyError, key
raise KeyError(key)
return
self._lru.remove(key)
self._lru.append(key)

def popitem(self):
if not self._lru:
raise KeyError, 'popitem(): dictionary is empty'
raise KeyError('popitem(): dictionary is empty')
k = self._lru[-1]
obj = self.remove(k)
return (k, obj,)
5 changes: 4 additions & 1 deletion pyutil/test/current/json_tests/test_dump.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# -*- coding: utf-8-with-signature-unix; fill-column: 77 -*-
# -*- indent-tabs-mode: nil -*-
from unittest import TestCase
from cStringIO import StringIO
try:
from cStringIO import StringIO
except ImportError:
from io import StringIO

from pyutil import jsonutil as json

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# -*- coding: utf-8-with-signature-unix; fill-column: 77 -*-
# -*- indent-tabs-mode: nil -*-

import sys
from twisted.trial.unittest import SkipTest, TestCase

from pyutil.jsonutil import encoder
Expand All @@ -13,9 +15,9 @@
(u' s p a c e d ', '" s p a c e d "'),
(u'\U0001d120', '"\\ud834\\udd20"'),
(u'\u03b1\u03a9', '"\\u03b1\\u03a9"'),
('\xce\xb1\xce\xa9', '"\\u03b1\\u03a9"'),
(b'\xce\xb1\xce\xa9', '"\\u03b1\\u03a9"'),
(u'\u03b1\u03a9', '"\\u03b1\\u03a9"'),
('\xce\xb1\xce\xa9', '"\\u03b1\\u03a9"'),
(b'\xce\xb1\xce\xa9', '"\\u03b1\\u03a9"'),
(u'\u03b1\u03a9', '"\\u03b1\\u03a9"'),
(u'\u03b1\u03a9', '"\\u03b1\\u03a9"'),
(u"`1~!@#$%^&*()_+-={':[,]}|;.</>?", '"`1~!@#$%^&*()_+-={\':[,]}|;.</>?"'),
Expand Down
Loading

0 comments on commit 5d53b1c

Please sign in to comment.