Skip to content

Commit 7d74887

Browse files
committed
Merge pull request #80 from lukmdo/pytest_show_py3_errors
make pytest report py3 issues
2 parents fb15acc + a5c1389 commit 7d74887

5 files changed

Lines changed: 32 additions & 20 deletions

File tree

sqlobject/compat.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Credit to six authors http://pypi.python.org/pypi/
2+
# License http://opensource.org/licenses/MIT
3+
4+
5+
def with_metaclass(meta, *bases):
6+
"""Create a base class with a metaclass."""
7+
# This requires a bit of explanation: the basic idea is to make a dummy
8+
# metaclass for one level of class instantiation that replaces itself with
9+
# the actual metaclass.
10+
11+
class metaclass(meta):
12+
def __new__(cls, name, this_bases, d):
13+
return meta(name, bases, d)
14+
return type.__new__(metaclass, 'temporary_class', (), {})

sqlobject/declarative.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import copy
3636
from . import events
37+
from sqlobject.compat import with_metaclass
3738

3839
import itertools
3940
counter = itertools.count()
@@ -90,21 +91,22 @@ def __new__(meta, class_name, bases, new_attrs):
9091
for func in early_funcs:
9192
func(cls)
9293
if '__classinit__' in new_attrs:
93-
cls.__classinit__ = staticmethod(cls.__classinit__.im_func)
94+
if hasattr(cls.__classinit__, '__func__'):
95+
cls.__classinit__ = staticmethod(cls.__classinit__.__func__)
96+
else:
97+
cls.__classinit__ = staticmethod(cls.__classinit__)
9498
cls.__classinit__(cls, new_attrs)
9599
for func in post_funcs:
96100
func(cls)
97101
return cls
98102

99103

100-
class Declarative(object):
104+
class Declarative(with_metaclass(DeclarativeMeta, object)):
101105

102106
__unpackargs__ = ()
103107

104108
__mutableattributes__ = ()
105109

106-
__metaclass__ = DeclarativeMeta
107-
108110
__restrict_attributes__ = None
109111

110112
def __classinit__(cls, new_attrs):

sqlobject/main.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
2626
USA.
2727
"""
28-
28+
import sys
2929
import threading
3030
import weakref
3131
import types
@@ -41,15 +41,16 @@
4141
from . import events
4242
from .sresults import SelectResults
4343
from .util.threadinglocal import local
44+
from sqlobject.compat import with_metaclass
4445

45-
import sys
4646
if ((sys.version_info[0] == 2) and (sys.version_info[:3] < (2, 6, 0))) or \
4747
((sys.version_info[0] == 3) and (sys.version_info[:3] < (3, 4, 0))):
4848
raise ImportError("SQLObject requires Python 2.6, 2.7 or 3.4+")
4949

5050
if sys.version_info[0] > 2:
5151
# alias for python 3 compatability
5252
long = int
53+
unicode = str
5354

5455
"""
5556
This thread-local storage is needed for RowCreatedSignals. It gathers
@@ -180,8 +181,7 @@ class CreateNewSQLObject:
180181
pass
181182

182183

183-
class sqlmeta(object):
184-
184+
class sqlmeta(with_metaclass(declarative.DeclarativeMeta, object)):
185185
"""
186186
This object is the object we use to keep track of all sorts of
187187
information. Subclasses are made for each SQLObject subclass
@@ -254,8 +254,6 @@ class sqlmeta(object):
254254
# Default encoding for UnicodeCol's
255255
dbEncoding = None
256256

257-
__metaclass__ = declarative.DeclarativeMeta
258-
259257
def __classinit__(cls, new_attrs):
260258
for attr in cls._unshared_attributes:
261259
if attr not in new_attrs:
@@ -757,9 +755,7 @@ def __get__(self, obj, type=None):
757755
# MetaSQLObject.
758756

759757

760-
class SQLObject(object):
761-
762-
__metaclass__ = declarative.DeclarativeMeta
758+
class SQLObject(with_metaclass(declarative.DeclarativeMeta, object)):
763759

764760
_connection = sqlhub
765761

@@ -903,7 +899,7 @@ def _SO_setupSqlmeta(cls, new_attrs, is_base):
903899
"(while fixing up sqlmeta %r inheritance)"
904900
% cls.sqlmeta)
905901
values = dict(cls.sqlmeta.__dict__)
906-
for key in values.keys():
902+
for key in list(values.keys()):
907903
if key.startswith('__') and key.endswith('__'):
908904
# Magic values shouldn't be passed through:
909905
del values[key]
@@ -1361,7 +1357,8 @@ def _SO_finishCreate(self, id=None):
13611357
# These are all the column values that were supposed
13621358
# to be set, but were delayed until now:
13631359
setters = self._SO_createValues.items()
1364-
setters.sort(key=lambda c: self.sqlmeta.columns[c[0]].creationOrder)
1360+
setters = sorted(
1361+
setters, key=lambda c: self.sqlmeta.columns[c[0]].creationOrder)
13651362
# Here's their database names:
13661363
names = [self.sqlmeta.columns[v[0]].dbName for v in setters]
13671364
values = [v[1] for v in setters]
@@ -1740,7 +1737,7 @@ def _reprItems(self):
17401737

17411738
@classmethod
17421739
def setConnection(cls, value):
1743-
if isinstance(value, basestring):
1740+
if isinstance(value, (str, unicode)):
17441741
value = dbconnection.connectionForURI(value)
17451742
cls._connection = value
17461743

sqlobject/manager/command.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from sqlobject.classregistry import findClass
2121
from sqlobject.declarative import DeclarativeMeta
2222
from sqlobject.util import moduleloader
23+
from sqlobject.compat import with_metaclass
2324

2425
# It's not very unsafe to use tempnam like we are doing:
2526
warnings.filterwarnings(
@@ -179,9 +180,7 @@ def standard_parser(connection=True, simulate=True,
179180
return parser
180181

181182

182-
class Command(object):
183-
184-
__metaclass__ = DeclarativeMeta
183+
class Command(with_metaclass(DeclarativeMeta, object)):
185184

186185
min_args = 0
187186
min_args_error = 'You must provide at least %(min_args)s arguments'

sqlobject/tests/test_reparent_sqlmeta.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def setClass(cls, soClass):
1919
# Well, it's pretty hard to call the superclass method
2020
# when it's a classmethod and it's not actually your
2121
# *current* superclass. Sigh
22-
real_sqlmeta.setClass.im_func(cls, soClass)
22+
real_sqlmeta.setClass.__func__(cls, soClass)
2323
cls.worked = True
2424

2525
dummy = StringCol()

0 commit comments

Comments
 (0)