From c1ff76f17bd4865a25cf4ba75d87e50f3ab4c4ab Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Tue, 20 Sep 2011 16:26:52 +0200 Subject: [PATCH] better testing (+ fixes) of sort_attributes --- wsme/tests/test_types.py | 21 +++++++++++++++++++++ wsme/types.py | 6 +++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/wsme/tests/test_types.py b/wsme/tests/test_types.py index 26d7014..3ccb1a5 100644 --- a/wsme/tests/test_types.py +++ b/wsme/tests/test_types.py @@ -2,6 +2,12 @@ import unittest from wsme import types +def gen_class(): + d = {} + exec('''class tmp(object): pass''', d) + return d['tmp'] + + class TestTypes(unittest.TestCase): def test_flat_type(self): class Flat(object): @@ -46,6 +52,21 @@ class TestTypes(unittest.TestCase): assert ForcedOrder._wsme_attributes[1][0] == 'a1' assert ForcedOrder._wsme_attributes[2][0] == 'a3' + c = gen_class() + print c + types.register_type(c) + del c._wsme_attributes + + c.a2 = int + c.a1 = int + c.a3 = int + + types.register_type(c) + + assert c._wsme_attributes[0][0] == 'a1' + assert c._wsme_attributes[1][0] == 'a2' + assert c._wsme_attributes[2][0] == 'a3' + def test_wsproperty(self): class WithWSProp(object): def __init__(self): diff --git a/wsme/types.py b/wsme/types.py index 44a38d8..9345fe8 100644 --- a/wsme/types.py +++ b/wsme/types.py @@ -50,7 +50,11 @@ def sort_attributes(class_, attributes): #. Sort by alphabetically""" + if not len(attributes): + return + attrs = dict(attributes) + if hasattr(class_, '_wsme_attr_order'): names_order = class_._wsme_attr_order else: @@ -65,7 +69,7 @@ def sort_attributes(class_, attributes): if aname in names and aname not in names_order: names_order.append(aname) assert len(names_order) == len(names) - except IOError, e: + except (TypeError, IOError): names_order = list(names) names_order.sort()