Skip to content

Commit a0bb008

Browse files
committed
release 1.24, fix Python 3.7 deprecation warnings about importing ABC types from collections vs collections.abc
1 parent e4d3d77 commit a0bb008

File tree

5 files changed

+44
-19
lines changed

5 files changed

+44
-19
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ develop-eggs
1818
.installed.cfg
1919
lib
2020
lib64
21+
.idea/
2122

2223
# Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
2324
[Bb]in/

serpent.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,21 @@
6666
import types
6767
import os
6868
import gc
69-
import collections
7069
import decimal
7170
import datetime
7271
import uuid
7372
import array
7473
import math
7574
import numbers
7675
import codecs
76+
import collections
77+
if sys.version_info >= (3, 4):
78+
from collections.abc import KeysView, ValuesView, ItemsView
79+
else:
80+
from collections import KeysView, ValuesView, ItemsView
7781

78-
__version__ = "1.23"
82+
83+
__version__ = "1.24"
7984
__all__ = ["dump", "dumps", "load", "loads", "register_class", "unregister_class", "tobytes"]
8085

8186
can_use_set_literals = sys.version_info >= (3, 2) # check if we can use set literals
@@ -151,17 +156,19 @@ def _ser_DictView(obj, serializer, outputstream, indentlevel):
151156

152157
def _reset_special_classes_registry():
153158
_special_classes_registry.clear()
154-
_special_classes_registry[collections.KeysView] = _ser_DictView
155-
_special_classes_registry[collections.ValuesView] = _ser_DictView
156-
_special_classes_registry[collections.ItemsView] = _ser_DictView
159+
_special_classes_registry[KeysView] = _ser_DictView
160+
_special_classes_registry[ValuesView] = _ser_DictView
161+
_special_classes_registry[ItemsView] = _ser_DictView
157162
if sys.version_info >= (2, 7):
158163
_special_classes_registry[collections.OrderedDict] = _ser_OrderedDict
159164
if sys.version_info >= (3, 4):
160165
import enum
166+
161167
def _ser_Enum(obj, serializer, outputstream, indentlevel):
162168
serializer._serialize(obj.value, outputstream, indentlevel)
163169
_special_classes_registry[enum.Enum] = _ser_Enum
164170

171+
165172
_reset_special_classes_registry()
166173

167174

@@ -284,7 +291,8 @@ def __init__(self, indent=False, set_literals=can_use_set_literals, module_in_cl
284291
"""
285292
Initialize the serializer.
286293
indent=indent the output over multiple lines (default=false)
287-
setLiterals=use set-literals or not (set to False if you need compatibility with Python < 3.2). Serpent chooses a sensible default for you.
294+
setLiterals=use set-literals or not (set to False if you need compatibility with Python < 3.2).
295+
Serpent chooses a sensible default for you.
288296
module_in_classname = include module prefix for class names or only use the class name itself
289297
"""
290298
self.indent = indent
@@ -301,7 +309,7 @@ def serialize(self, obj):
301309
if self.set_literals:
302310
header += "python3.2\n" # set-literals require python 3.2+ to deserialize (ast.literal_eval limitation)
303311
else:
304-
header += "python2.6\n" # don't change this even though we don't support 2.6 any longer, otherwise we can't read older serpent strings
312+
header += "python2.6\n" # don't change this, otherwise we can't read older serpent strings
305313
out = [header]
306314
if os.name == "java" and type(obj) is buffer:
307315
obj = bytearray(obj)
@@ -455,8 +463,8 @@ def _check_hashable_type(self, t):
455463
return
456464
elif sys.version_info < (3, 0) and t is unicode:
457465
return
458-
raise TypeError("one of the keys in a dict or set is not of a primitive hashable type: "
459-
+ str(t) + ". Use simple types as keys or use a list or tuple as container.")
466+
raise TypeError("one of the keys in a dict or set is not of a primitive hashable type: " +
467+
str(t) + ". Use simple types as keys or use a list or tuple as container.")
460468

461469
def ser_builtins_dict(self, dict_obj, out, level):
462470
if id(dict_obj) in self.serialized_obj_ids:
@@ -544,11 +552,11 @@ def ser_decimal_Decimal(self, decimal_obj, out, level):
544552
def ser_datetime_datetime(self, datetime_obj, out, level):
545553
out.append(repr(datetime_obj.isoformat()))
546554
dispatch[datetime.datetime] = ser_datetime_datetime
547-
555+
548556
def ser_datetime_date(self, date_obj, out, level):
549557
out.append(repr(date_obj.isoformat()))
550558
dispatch[datetime.date] = ser_datetime_date
551-
559+
552560
if os.name == "java" or sys.version_info < (2, 7): # jython bug http://bugs.jython.org/issue2010
553561
def ser_datetime_timedelta(self, timedelta_obj, out, level):
554562
secs = ((timedelta_obj.days * 86400 + timedelta_obj.seconds) * 10 ** 6 + timedelta_obj.microseconds) / 10 ** 6
@@ -614,7 +622,8 @@ def ser_default_class(self, obj, out, level):
614622
value[slot] = getattr(obj, slot)
615623
value["__class__"] = self.get_class_name(obj)
616624
else:
617-
raise TypeError("don't know how to serialize class " + str(obj.__class__) + ". Give it vars() or an appropriate __getstate__")
625+
raise TypeError("don't know how to serialize class " +
626+
str(obj.__class__) + ". Give it vars() or an appropriate __getstate__")
618627
self._serialize(value, out, level)
619628
finally:
620629
self.serialized_obj_ids.discard(id(obj))

setup.cfg

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
[wheel]
22
universal = 1
33

4+
[pycodestyle]
5+
max-line-length = 140
6+
ignore = E402,E731
7+
exclude = .git,__pycache__,.tox,docs,tests,build,dist,examples

setup.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
3838
- ``ser_bytes = serpent.dumps(obj, indent=False, set_literals=True, module_in_classname=False):`` # serialize obj tree to bytes
3939
- ``obj = serpent.loads(ser_bytes)`` # deserialize bytes back into object tree
40-
- You can use ``ast.literal_eval`` yourself to deserialize, but ``serpent.deserialize`` works around a few corner cases. See source for details.
40+
- You can use ``ast.literal_eval`` yourself to deserialize, but ``serpent.deserialize``
41+
works around a few corner cases. See source for details.
4142
4243
Serpent is more sophisticated than a simple repr() + literal_eval():
4344
@@ -61,11 +62,14 @@
6162
- Why not use XML? Answer: because XML.
6263
- Why not use JSON? Answer: because JSON is quite limited in the number of datatypes it supports, and you can't use comments in a JSON file.
6364
- Why not use pickle? Answer: because pickle has security problems.
64-
- Why not use ``repr()``/``ast.literal_eval()``? See above; serpent is a superset of this and provides more convenience. Serpent provides automatic serialization mappings for types other than the builtin primitive types. ``repr()`` can't serialize these to literals that ``ast.literal_eval()`` understands.
65+
- Why not use ``repr()``/``ast.literal_eval()``? See above; serpent is a superset of this and provides more convenience.
66+
Serpent provides automatic serialization mappings for types other than the builtin primitive types.
67+
``repr()`` can't serialize these to literals that ``ast.literal_eval()`` understands.
6568
- Why not a binary format? Answer: because binary isn't readable by humans.
6669
- But I don't care about readability. Answer: doesn't matter, ``ast.literal_eval()`` wants a literal string, so that is what we produce.
6770
- But I want better performance. Answer: ok, maybe you shouldn't use serpent in this case. Find an efficient binary protocol (protobuf?)
68-
- Why only Python, Java and C#/.NET, but no bindings for insert-favorite-language-here? Answer: I don't speak that language. Maybe you could port serpent yourself?
71+
- Why only Python, Java and C#/.NET, but no bindings for insert-favorite-language-here? Answer: I don't speak that language.
72+
Maybe you could port serpent yourself?
6973
- Where is the source? It's on Github: https://github.com/irmen/Serpent
7074
- Can I use it everywhere? Sure, as long as you keep the copyright and disclaimer somewhere. See the LICENSE file.
7175

tests/test_serpent.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@
1919
import traceback
2020
import threading
2121
import time
22-
import collections
2322
import types
23+
import collections
24+
if sys.version_info >= (3, 4):
25+
from collections.abc import KeysView, ValuesView, ItemsView
26+
else:
27+
from collections import KeysView, ValuesView, ItemsView
2428

2529
if sys.version_info < (2, 7):
2630
import unittest2 as unittest
@@ -918,8 +922,11 @@ def __init__(self, name, value):
918922
def __getstate__(self):
919923
return ("bogus", "state")
920924

925+
921926
class BaseClass(object):
922927
pass
928+
929+
923930
class SubClass(BaseClass):
924931
pass
925932

@@ -978,9 +985,9 @@ def testRegisterOrderPreserving(self):
978985
serpent.register_class(BaseClass, lambda: None)
979986
serpent.register_class(SubClass, lambda: None)
980987
classes = list(serpent._special_classes_registry)
981-
self.assertEqual(collections.KeysView, classes.pop(0))
982-
self.assertEqual(collections.ValuesView, classes.pop(0))
983-
self.assertEqual(collections.ItemsView, classes.pop(0))
988+
self.assertEqual(KeysView, classes.pop(0))
989+
self.assertEqual(ValuesView, classes.pop(0))
990+
self.assertEqual(ItemsView, classes.pop(0))
984991
if sys.version_info >= (2, 7):
985992
self.assertEqual(collections.OrderedDict, classes.pop(0))
986993
if sys.version_info >= (3, 4):

0 commit comments

Comments
 (0)