From 9858cb5e3cf3fa0bb1f8f3f85c16e7751e9d4627 Mon Sep 17 00:00:00 2001 From: Yohan Date: Tue, 4 Apr 2017 19:08:31 +0900 Subject: [PATCH] Work Encode and Decode with Message in AMF3 --- .gitignore | 1 + pyamf/__init__.py | 24 +- pyamf/adapters/_django_db_models_base.py | 8 +- pyamf/adapters/_google_appengine_ext_db.py | 4 +- pyamf/adapters/_google_appengine_ext_ndb.py | 8 +- pyamf/adapters/gae_base.py | 2 +- pyamf/adapters/models.py | 4 +- pyamf/adapters/tests/google/test_blobstore.py | 2 +- pyamf/adapters/tests/google/test_ndb.py | 4 +- pyamf/adapters/tests/test_array.py | 8 +- pyamf/alias.py | 18 +- pyamf/amf0.py | 50 +-- pyamf/amf3.py | 74 ++--- pyamf/codec.py | 8 +- pyamf/python.py | 12 +- pyamf/remoting/__init__.py | 22 +- pyamf/remoting/amf0.py | 2 +- pyamf/remoting/client/__init__.py | 25 +- pyamf/remoting/gateway/__init__.py | 27 +- pyamf/remoting/gateway/wsgi.py | 2 +- pyamf/sol.py | 4 +- pyamf/tests/gateway/test_django.py | 2 +- pyamf/tests/gateway/test_google.py | 2 +- pyamf/tests/gateway/test_wsgi.py | 9 +- pyamf/tests/modules/test_sets.py | 21 +- pyamf/tests/remoting/test_client.py | 12 +- pyamf/tests/test_adapters_util.py | 6 - pyamf/tests/test_basic.py | 2 +- pyamf/tests/test_imports.py | 4 +- pyamf/tests/test_remoting.py | 146 ++++----- pyamf/tests/test_util.py | 290 +++++++++--------- pyamf/tests/test_xml.py | 2 +- pyamf/util/__init__.py | 7 +- pyamf/util/pure.py | 64 ++-- setup.py | 14 +- setupinfo.py | 1 + 36 files changed, 434 insertions(+), 457 deletions(-) diff --git a/.gitignore b/.gitignore index cc85b8e9..60f01460 100644 --- a/.gitignore +++ b/.gitignore @@ -62,6 +62,7 @@ dkms.conf .idea/**/workspace.xml .idea/**/tasks.xml .idea/dictionaries +.idea/ # Sensitive or high-churn files: .idea/**/dataSources/ diff --git a/pyamf/__init__.py b/pyamf/__init__.py index b71ae91e..4f9f2ae6 100644 --- a/pyamf/__init__.py +++ b/pyamf/__init__.py @@ -11,7 +11,6 @@ @status: Production/Stable """ -import types import inspect from pyamf import util, _version @@ -377,6 +376,9 @@ def load_class(alias): @return: Class registered to the alias. @rtype: C{classobj} """ + if isinstance(alias, bytes): + alias = alias.decode() + # Try the CLASS_CACHE first try: return CLASS_CACHE[alias] @@ -399,10 +401,10 @@ def load_class(alias): raise TypeError("Expecting class object or ClassAlias from loader") - mod_class = alias.split('.') + mod_class = alias.split(b'.') if mod_class: - module = '.'.join(mod_class[:-1]) + module = b'.'.join(mod_class[:-1]) klass = mod_class[-1] try: @@ -571,15 +573,15 @@ def flex_loader(alias): @raise UnknownClassAlias: Trying to load an unknown Flex compatibility class. """ - if not alias.startswith('flex.'): + if not alias.startswith(b'flex.'): return try: - if alias.startswith('flex.messaging.messages'): + if alias.startswith(b'flex.messaging.messages'): import pyamf.flex.messaging - elif alias.startswith('flex.messaging.io'): + elif alias.startswith(b'flex.messaging.io'): import pyamf.flex - elif alias.startswith('flex.data.messages'): + elif alias.startswith(b'flex.data.messages'): import pyamf.flex.data # noqa return CLASS_CACHE[alias] @@ -611,7 +613,7 @@ def _check_type(type_): if type_ in TYPE_MAP: raise KeyError('Type %r already exists' % (type_,)) - if isinstance(type_, types.TupleType): + if isinstance(type_, tuple): for x in type_: _check_type(x) else: @@ -630,7 +632,7 @@ def get_type(type_): if isinstance(type_, list): type_ = tuple(type_) - for k, v in TYPE_MAP.iteritems(): + for k, v in TYPE_MAP.items(): if k == type_: return v @@ -757,7 +759,7 @@ def register_alias_type(klass, *args): - At least one type must be supplied """ def check_type_registered(arg): - for k, v in ALIAS_TYPES.iteritems(): + for k, v in list(ALIAS_TYPES.items()): for kl in v: if arg is kl: raise RuntimeError('%r is already registered under %r' % ( @@ -785,7 +787,7 @@ def check_type_registered(arg): ALIAS_TYPES[klass] = args - for k, v in CLASS_CACHE.copy().iteritems(): + for k, v in list(CLASS_CACHE.copy().items()): new_alias = util.get_class_alias(v.klass) if new_alias is klass: diff --git a/pyamf/adapters/_django_db_models_base.py b/pyamf/adapters/_django_db_models_base.py index 4102e1f0..b497cebd 100644 --- a/pyamf/adapters/_django_db_models_base.py +++ b/pyamf/adapters/_django_db_models_base.py @@ -185,7 +185,7 @@ def getEncodableAttributes(self, obj, **kwargs): if not attrs: attrs = {} - for name, prop in self.fields.iteritems(): + for name, prop in self.fields.items(): if name not in attrs.keys(): continue @@ -197,7 +197,7 @@ def getEncodableAttributes(self, obj, **kwargs): if key.startswith('_'): del attrs[key] - for name, relation in self.relations.iteritems(): + for name, relation in self.relations.items(): if '_%s_cache' % name in obj.__dict__: attrs[name] = getattr(obj, name) @@ -230,7 +230,7 @@ def getDecodableAttributes(self, obj, attrs, **kwargs): # primary key of django object must always be set first for # relationships with other model objects to work properly - # and dict.iteritems() does not guarantee order + # and dict.items() does not guarantee order # # django also forces the use only one attribute as primary key, so # our obj._meta.pk.attname check is sufficient) @@ -249,7 +249,7 @@ def getDecodableAttributes(self, obj, attrs, **kwargs): pass if not getattr(obj, pk_attr): - for name, relation in self.relations.iteritems(): + for name, relation in self.relations.items(): if isinstance(relation, models.ManyToManyField): try: if len(attrs[name]) == 0: diff --git a/pyamf/adapters/_google_appengine_ext_db.py b/pyamf/adapters/_google_appengine_ext_db.py index efaf166f..4e75ace9 100644 --- a/pyamf/adapters/_google_appengine_ext_db.py +++ b/pyamf/adapters/_google_appengine_ext_db.py @@ -74,7 +74,7 @@ def getCustomProperties(self): self.properties = {} reverse_props = [] - for name, prop in self.klass.properties().iteritems(): + for name, prop in self.klass.properties().items(): self.properties[name] = prop if isinstance(prop, db.ReferenceProperty): @@ -86,7 +86,7 @@ def getCustomProperties(self): # check if the property is a defined as a collection_name. These types # of properties are read-only and the datastore freaks out if you # attempt to meddle with it. We delete the attribute entirely .. - for name, value in self.klass.__dict__.iteritems(): + for name, value in self.klass.__dict__.items(): if isinstance(value, db._ReverseReferenceProperty): reverse_props.append(name) diff --git a/pyamf/adapters/_google_appengine_ext_ndb.py b/pyamf/adapters/_google_appengine_ext_ndb.py index cd70f966..5a1c889c 100644 --- a/pyamf/adapters/_google_appengine_ext_ndb.py +++ b/pyamf/adapters/_google_appengine_ext_ndb.py @@ -72,7 +72,7 @@ def getCustomProperties(self): # list of property names that are computed computed_props = {} - for name, prop in self.klass._properties.iteritems(): + for name, prop in self.klass._properties.items(): props[name] = prop if prop._repeated: @@ -88,7 +88,7 @@ def getCustomProperties(self): # check if the property is a defined as a computed property. These # types of properties are read-only - for name, value in self.klass.__dict__.iteritems(): + for name, value in self.klass.__dict__.items(): if isinstance(value, ndb.ComputedProperty): read_only_props.append(name) @@ -110,7 +110,7 @@ def getDecodableAttributes(self, obj, attrs, codec=None): ) if self.repeated_properties: - for name, prop in self.repeated_properties.iteritems(): + for name, prop in self.repeated_properties.items(): try: value = attrs[name] except KeyError: @@ -183,7 +183,7 @@ def getEncodableAttributes(self, obj, codec=None): ) if isinstance(obj, ndb.Expando): - for name, prop in obj._properties.iteritems(): + for name, prop in obj._properties.items(): if name in self.model_properties: continue diff --git a/pyamf/adapters/gae_base.py b/pyamf/adapters/gae_base.py index c5fb671d..120a4008 100644 --- a/pyamf/adapters/gae_base.py +++ b/pyamf/adapters/gae_base.py @@ -139,7 +139,7 @@ def transform(self, stub=None): return - for stub, (klass, key) in self.stubs.iteritems(): + for stub, (klass, key) in self.stubs.items(): self.transformStub(stub, klass, key) diff --git a/pyamf/adapters/models.py b/pyamf/adapters/models.py index df154be7..1ca419a0 100644 --- a/pyamf/adapters/models.py +++ b/pyamf/adapters/models.py @@ -80,7 +80,7 @@ def decode_model_property(obj, prop, value): if handler: return handler(obj, prop, value) - for model_prop, handler in _property_decoders.iteritems(): + for model_prop, handler in _property_decoders.items(): if isinstance(prop, model_prop): _property_decoders[prop.__class__] = handler @@ -97,7 +97,7 @@ def encode_model_property(obj, prop, value): if handler: return handler(obj, prop, value) - for model_prop, handler in _property_encoders.iteritems(): + for model_prop, handler in _property_encoders.items(): if isinstance(prop, model_prop): _property_encoders[prop.__class__] = handler diff --git a/pyamf/adapters/tests/google/test_blobstore.py b/pyamf/adapters/tests/google/test_blobstore.py index e9cff515..06ba9690 100644 --- a/pyamf/adapters/tests/google/test_blobstore.py +++ b/pyamf/adapters/tests/google/test_blobstore.py @@ -35,7 +35,7 @@ class BlobStoreTestCase(google.BaseTestCase): 'content_type': 'text/plain', 'size': 1234567, 'filename': 'not-telling.ogg', - 'creation': datetime.datetime(2010, 07, 11, 14, 15, 01) + 'creation': datetime.datetime(2010, 7, 11, 14, 15, 1) } def setUp(self): diff --git a/pyamf/adapters/tests/google/test_ndb.py b/pyamf/adapters/tests/google/test_ndb.py index 73809cb6..b6923353 100644 --- a/pyamf/adapters/tests/google/test_ndb.py +++ b/pyamf/adapters/tests/google/test_ndb.py @@ -176,8 +176,8 @@ def test_amf0(self): b'\x00\x00\x00\x00\x00\x00\x00\nbirth_date\x0bB9\x15\xda$\x00\x00' b'\x00\x00\x00\x00\x06height\x00?\xfc=p\xa3\xd7\n=\x00\x0cmeasurem' b'ents\n\x00\x00\x00\x03\x00?\xf0\x00\x00\x00\x00\x00\x00\x00@\x00' - '\x00\x00\x00\x00\x00\x00\x00@\x08\x00\x00\x00\x00\x00\x00\x00\x04' - 'name\x02\x00\nHeidi Klum\x00\x00\t' + b'\x00\x00\x00\x00\x00\x00\x00@\x08\x00\x00\x00\x00\x00\x00\x00\x04' + b'name\x02\x00\nHeidi Klum\x00\x00\t' ) decoder = pyamf.decode(data, encoding=pyamf.AMF0) diff --git a/pyamf/adapters/tests/test_array.py b/pyamf/adapters/tests/test_array.py index 85f96983..b1ac9021 100644 --- a/pyamf/adapters/tests/test_array.py +++ b/pyamf/adapters/tests/test_array.py @@ -27,11 +27,11 @@ def setUp(self): self.orig = ['f', 'o', 'o'] - self.obj = array.array('c') + self.obj = array.array('b') - self.obj.append('f') - self.obj.append('o') - self.obj.append('o') + self.obj.append(ord('f')) + self.obj.append(ord('o')) + self.obj.append(ord('o')) def encdec(self, encoding): return pyamf.decode( diff --git a/pyamf/alias.py b/pyamf/alias.py index 1ef3af34..a1875c90 100644 --- a/pyamf/alias.py +++ b/pyamf/alias.py @@ -123,7 +123,7 @@ def compile(self): self.decodable_properties.update(self.klass.__slots__) self.encodable_properties.update(self.klass.__slots__) - for k, v in self.klass.__dict__.iteritems(): + for k, v in list(self.klass.__dict__.items()): if not isinstance(v, property): continue @@ -318,7 +318,7 @@ def __repr__(self): ) def __eq__(self, other): - if isinstance(other, basestring): + if isinstance(other, str): return self.alias == other elif isinstance(other, self.__class__): return self.klass == other.klass @@ -439,14 +439,14 @@ def getEncodableAttributes(self, obj, codec=None): if self.proxy_attrs is not None and attrs and codec: context = codec.context - for k, v in attrs.copy().iteritems(): + for k, v in attrs.copy().items(): if k in self.proxy_attrs: attrs[k] = context.getProxyForObject(v) if self.synonym_attrs: missing = object() - for k, v in self.synonym_attrs.iteritems(): + for k, v in self.synonym_attrs.items(): value = attrs.pop(k, missing) if value is missing: @@ -474,7 +474,7 @@ def getDecodableAttributes(self, obj, attrs, codec=None): changed = False - props = set(attrs.keys()) + props = set(key.decode() if isinstance(key, bytes) else key for key in attrs.keys()) if self.static_attrs: missing_attrs = self.static_attrs_set.difference(props) @@ -528,7 +528,7 @@ def getDecodableAttributes(self, obj, attrs, codec=None): if self.synonym_attrs: missing = object() - for k, v in self.synonym_attrs.iteritems(): + for k, v in self.synonym_attrs.items(): value = attrs.pop(v, missing) if value is missing: @@ -552,6 +552,12 @@ def applyAttributes(self, obj, attrs, codec=None): if not self._compiled: self.compile() + old_attrs = attrs + attrs = dict() + for key, value in old_attrs.items(): + key = key.decode() if isinstance(key, bytes) else key + attrs[key] = value + if not self.shortcut_decode: attrs = self.getDecodableAttributes(obj, attrs, codec=codec) else: diff --git a/pyamf/amf0.py b/pyamf/amf0.py index d1019b98..89b37b28 100644 --- a/pyamf/amf0.py +++ b/pyamf/amf0.py @@ -24,70 +24,70 @@ #: Represented as 9 bytes: 1 byte for C{0x00} and 8 bytes a double #: representing the value of the number. -TYPE_NUMBER = '\x00' +TYPE_NUMBER = b'\x00' #: Represented as 2 bytes: 1 byte for C{0x01} and a second, C{0x00} #: for C{False}, C{0x01} for C{True}. -TYPE_BOOL = '\x01' +TYPE_BOOL = b'\x01' #: Represented as 3 bytes + len(String): 1 byte C{0x02}, then a UTF8 string, #: including the top two bytes representing string length as a C{int}. -TYPE_STRING = '\x02' +TYPE_STRING = b'\x02' #: Represented as 1 byte, C{0x03}, then pairs of UTF8 string, the key, and #: an AMF element, ended by three bytes, C{0x00} C{0x00} C{0x09}. -TYPE_OBJECT = '\x03' +TYPE_OBJECT = b'\x03' #: MovieClip does not seem to be supported by Remoting. #: It may be used by other AMF clients such as SharedObjects. -TYPE_MOVIECLIP = '\x04' +TYPE_MOVIECLIP = b'\x04' #: 1 single byte, C{0x05} indicates null. -TYPE_NULL = '\x05' +TYPE_NULL = b'\x05' #: 1 single byte, C{0x06} indicates null. -TYPE_UNDEFINED = '\x06' +TYPE_UNDEFINED = b'\x06' #: When an ActionScript object refers to itself, such C{this.self = this}, #: or when objects are repeated within the same scope (for example, as the #: two parameters of the same function called), a code of C{0x07} and an #: C{int}, the reference number, are written. -TYPE_REFERENCE = '\x07' +TYPE_REFERENCE = b'\x07' #: A MixedArray is indicated by code C{0x08}, then a Long representing the #: highest numeric index in the array, or 0 if there are none or they are #: all negative. After that follow the elements in key : value pairs. -TYPE_MIXEDARRAY = '\x08' +TYPE_MIXEDARRAY = b'\x08' #: @see: L{TYPE_OBJECT} -TYPE_OBJECTTERM = '\x09' +TYPE_OBJECTTERM = b'\x09' #: An array is indicated by C{0x0A}, then a Long for array length, then the #: array elements themselves. Arrays are always sparse; values for #: inexistant keys are set to null (C{0x06}) to maintain sparsity. -TYPE_ARRAY = '\x0A' +TYPE_ARRAY = b'\x0A' #: Date is represented as C{0x0B}, then a double, then an C{int}. The double #: represents the number of milliseconds since 01/01/1970. The C{int} #: represents the timezone offset in minutes between GMT. Note for the latter #: than values greater than 720 (12 hours) are represented as M{2^16} - the #: value. Thus GMT+1 is 60 while GMT-5 is 65236. -TYPE_DATE = '\x0B' +TYPE_DATE = b'\x0B' #: LongString is reserved for strings larger then M{2^16} characters long. It #: is represented as C{0x0C} then a LongUTF. -TYPE_LONGSTRING = '\x0C' +TYPE_LONGSTRING = b'\x0C' #: Trying to send values which don't make sense, such as prototypes, functions, #: built-in objects, etc. will be indicated by a single C{00x0D} byte. -TYPE_UNSUPPORTED = '\x0D' +TYPE_UNSUPPORTED = b'\x0D' #: Remoting Server -> Client only. #: @see: L{RecordSet} #: @see: U{RecordSet structure on OSFlash #: } -TYPE_RECORDSET = '\x0E' +TYPE_RECORDSET = b'\x0E' #: The XML element is indicated by C{0x0F} and followed by a LongUTF containing #: the string representation of the XML object. The receiving gateway may which #: to wrap this string inside a language-specific standard XML object, or #: simply pass as a string. -TYPE_XML = '\x0F' +TYPE_XML = b'\x0F' #: A typed object is indicated by C{0x10}, then a UTF string indicating class #: name, and then the same structure as a normal C{0x03} Object. The receiving #: gateway may use a mapping scheme, or send back as a vanilla object or #: associative array. -TYPE_TYPEDOBJECT = '\x10' +TYPE_TYPEDOBJECT = b'\x10' #: An AMF message sent from an AVM+ client such as the Flash Player 9 may break #: out into L{AMF3} mode. In this case the next byte will be the #: AMF3 type code and the data will be in AMF3 format until the decoded object #: reaches it's logical conclusion (for example, an object has no more keys). -TYPE_AMF3 = '\x11' +TYPE_AMF3 = b'\x11' class Context(codec.Context): @@ -243,7 +243,7 @@ def readMixedArray(self): attrs = self.readObjectAttributes(obj) - for key, value in attrs.iteritems(): + for key, value in attrs.items(): try: key = int(key) except ValueError: @@ -261,7 +261,7 @@ def readList(self): self.context.addObject(obj) l = self.stream.read_ulong() - for i in xrange(l): + for i in range(l): obj.append(self.readElement()) return obj @@ -485,7 +485,7 @@ def serialiseString(self, s): """ Similar to L{writeString} but does not encode a type byte. """ - if type(s) is unicode: + if type(s) is str: s = self.context.getBytesForString(s) l = len(s) @@ -545,7 +545,7 @@ def _writeDict(self, o): @param o: The C{dict} data to be encoded to the AMF0 data stream. """ - for key, val in o.iteritems(): + for key, val in o.items(): if type(key) in python.int_types: key = str(key) @@ -570,7 +570,7 @@ def writeMixedArray(self, o): # list comprehensions to save the day max_index = max([ y[0] for y in o.items() - if isinstance(y[0], (int, long)) + if isinstance(y[0], int) ]) if max_index < 0: @@ -584,7 +584,7 @@ def writeMixedArray(self, o): self._writeEndObject() def _writeEndObject(self): - self.stream.write('\x00\x00' + TYPE_OBJECTTERM) + self.stream.write(b'\x00\x00' + TYPE_OBJECTTERM) def writeObject(self, o): """ @@ -659,7 +659,7 @@ def writeXML(self, e): data = xml.tostring(e) - if isinstance(data, unicode): + if isinstance(data, str): data = data.encode('utf-8') self.stream.write_ulong(len(data)) diff --git a/pyamf/amf3.py b/pyamf/amf3.py index d2413f9d..5ba60855 100644 --- a/pyamf/amf3.py +++ b/pyamf/amf3.py @@ -44,37 +44,37 @@ #: The undefined type is represented by the undefined type marker. No further #: information is encoded for this value. -TYPE_UNDEFINED = '\x00' +TYPE_UNDEFINED = b'\x00' #: The null type is represented by the null type marker. No further #: information is encoded for this value. -TYPE_NULL = '\x01' +TYPE_NULL = b'\x01' #: The false type is represented by the false type marker and is used to #: encode a Boolean value of C{false}. No further information is encoded for #: this value. -TYPE_BOOL_FALSE = '\x02' +TYPE_BOOL_FALSE = b'\x02' #: The true type is represented by the true type marker and is used to encode #: a Boolean value of C{true}. No further information is encoded for this #: value. -TYPE_BOOL_TRUE = '\x03' +TYPE_BOOL_TRUE = b'\x03' #: In AMF 3 integers are serialized using a variable length signed 29-bit #: integer. #: @see: U{Parsing Integers on OSFlash (external) #: } -TYPE_INTEGER = '\x04' +TYPE_INTEGER = b'\x04' #: This type is used to encode an ActionScript Number or an ActionScript #: C{int} of value greater than or equal to 2^28 or an ActionScript uint of #: value greater than or equal to 2^29. The encoded value is is always an 8 #: byte IEEE-754 double precision floating point value in network byte order #: (sign bit in low memory). The AMF 3 number type is encoded in the same #: manner as the AMF 0 L{Number} type. -TYPE_NUMBER = '\x05' +TYPE_NUMBER = b'\x05' #: ActionScript String values are represented using a single string type in #: AMF 3 - the concept of string and long string types from AMF 0 is not used. #: Strings can be sent as a reference to a previously occurring String by #: using an index to the implicit string reference table. Strings are encoding #: using UTF-8 - however the header may either describe a string literal or a #: string reference. -TYPE_STRING = '\x06' +TYPE_STRING = b'\x06' #: ActionScript 3.0 introduced a new XML type however the legacy C{XMLDocument} #: type from ActionScript 1.0 and 2.0.is retained in the language as #: C{flash.xml.XMLDocument}. Similar to AMF 0, the structure of an @@ -86,29 +86,29 @@ #: @see: U{OSFlash documentation (external) #: } -TYPE_XML = '\x07' +TYPE_XML = b'\x07' #: In AMF 3 an ActionScript Date is serialized as the number of #: milliseconds elapsed since the epoch of midnight, 1st Jan 1970 in the #: UTC time zone. Local time zone information is not sent. -TYPE_DATE = '\x08' +TYPE_DATE = b'\x08' #: ActionScript Arrays are described based on the nature of their indices, #: i.e. their type and how they are positioned in the Array. -TYPE_ARRAY = '\x09' +TYPE_ARRAY = b'\x09' #: A single AMF 3 type handles ActionScript Objects and custom user classes. -TYPE_OBJECT = '\x0A' +TYPE_OBJECT = b'\x0A' #: ActionScript 3.0 introduces a new top-level XML class that supports #: U{E4X} syntax. #: For serialization purposes the XML type needs to be flattened into a #: string representation. As with other strings in AMF, the content is #: encoded using UTF-8. -TYPE_XMLSTRING = '\x0B' +TYPE_XMLSTRING = b'\x0B' #: ActionScript 3.0 introduces the L{ByteArray} type to hold an Array #: of bytes. AMF 3 serializes this type using a variable length encoding #: 29-bit integer for the byte-length prefix followed by the raw bytes #: of the L{ByteArray}. #: @see: U{Parsing ByteArrays on OSFlash (external) #: } -TYPE_BYTEARRAY = '\x0C' +TYPE_BYTEARRAY = b'\x0C' #: Reference bit. REFERENCE_BIT = 0x01 @@ -249,7 +249,7 @@ def writeMultiByte(self, value, charset): @see: U{Supported character sets on Adobe Help (external) } """ - if type(value) is unicode: + if type(value) is str: value = value.encode(charset) self.stream.write(value) @@ -318,10 +318,10 @@ def writeUTFBytes(self, value): """ val = None - if isinstance(value, unicode): + if isinstance(value, str): val = value else: - val = unicode(value, 'utf8') + val = str(value, 'utf8') self.stream.write_utf8_string(val) @@ -417,7 +417,7 @@ def readMultiByte(self, length, charset): # FIXME nick: how to work out the code point byte size (on the fly)? bytes = self.stream.read(length) - return unicode(bytes, charset) + return str(bytes, charset) def readObject(self): """ @@ -532,11 +532,11 @@ def writeObject(self, obj): return super(ByteArray, self).writeObject(obj) - def __cmp__(self, other): + def __eq__(self, other): if isinstance(other, ByteArray): - return cmp(self.getvalue(), other.getvalue()) + return self.getvalue() == other.getvalue() - return cmp(self.getvalue(), other) + return self.getvalue() == other def __str__(self): buf = self.getvalue() @@ -664,7 +664,7 @@ def addString(self, s): @raise TypeError: The parameter C{s} is not of C{basestring} type. """ - if not isinstance(s, basestring): + if not isinstance(s, bytes): raise TypeError if len(s) == 0: @@ -938,7 +938,7 @@ def readArray(self): result = [] self.context.addObject(result) - for i in xrange(size): + for i in range(size): result.append(self.readElement()) return result @@ -950,7 +950,7 @@ def readArray(self): result[key] = self.readElement() key = self.readBytes() - for i in xrange(size): + for i in range(size): el = self.readElement() result[i] = el @@ -989,7 +989,7 @@ def _getClassDefinition(self, ref): class_def.static_properties = [] if class_def.attr_len > 0: - for i in xrange(class_def.attr_len): + for i in range(class_def.attr_len): key = self.readBytes() class_def.static_properties.append(key) @@ -1244,7 +1244,7 @@ def serialiseString(self, s): @type s: C{str} @param s: The string data to be encoded to the AMF3 data stream. """ - if type(s) is unicode: + if type(s) is str: s = self.context.getBytesForString(s) self.serialiseBytes(s) @@ -1328,7 +1328,7 @@ def writeList(self, n, is_proxy=False): self.context.addObject(n) self._writeInteger((len(n) << 1) | REFERENCE_BIT) - self.stream.write('\x01') + self.stream.write(b'\x01') [self.writeElement(x) for x in n] @@ -1469,7 +1469,7 @@ def writeObject(self, obj, is_proxy=False): definition.reference << 2 | REFERENCE_BIT) if alias.anonymous: - self.stream.write('\x01') + self.stream.write(b'\x01') else: self.serialiseString(alias.alias) @@ -1499,14 +1499,14 @@ def writeObject(self, obj, is_proxy=False): if definition.encoding == ObjectEncoding.DYNAMIC: if attrs: - for attr, value in attrs.iteritems(): + for attr, value in attrs.items(): if type(attr) in python.int_types: attr = str(attr) self.serialiseString(attr) self.writeElement(value) - self.stream.write('\x01') + self.stream.write(b'\x01') def writeByteArray(self, n): """ @@ -1574,31 +1574,31 @@ def encode_int(n): if n < 0: n += 0x20000000 - bytes = '' + data = b'' real_value = None if n > 0x1fffff: real_value = n n >>= 1 - bytes += chr(0x80 | ((n >> 21) & 0xff)) + data += bytes([0x80 | ((n >> 21) & 0xff)]) if n > 0x3fff: - bytes += chr(0x80 | ((n >> 14) & 0xff)) + data += bytes([0x80 | ((n >> 14) & 0xff)]) if n > 0x7f: - bytes += chr(0x80 | ((n >> 7) & 0xff)) + data += bytes([0x80 | ((n >> 7) & 0xff)]) if real_value is not None: n = real_value if n > 0x1fffff: - bytes += chr(n & 0xff) + data += bytes([n & 0xff]) else: - bytes += chr(n & 0x7f) + data += bytes([n & 0x7f]) - ENCODED_INT_CACHE[n] = bytes + ENCODED_INT_CACHE[n] = data - return bytes + return data def decode_int(stream, signed=False): diff --git a/pyamf/codec.py b/pyamf/codec.py index d7b87f2b..a0a7371b 100644 --- a/pyamf/codec.py +++ b/pyamf/codec.py @@ -290,7 +290,7 @@ class _Codec(object): def __init__(self, stream=None, context=None, strict=False, timezone_offset=None, forbid_dtd=True, forbid_entities=True): - if isinstance(stream, basestring) or stream is None: + if isinstance(stream, str) or stream is None: stream = util.BufferedByteStream(stream) self.stream = stream @@ -524,7 +524,7 @@ def getTypeFunc(self, data): return self.writeNumber elif t in python.int_types: return self.writeNumber - elif t in (list, tuple): + elif t in (list, tuple, frozenset): return self.writeList elif t is types.GeneratorType: # flake8: noqa return self.writeGenerator @@ -536,7 +536,7 @@ def getTypeFunc(self, data): return self.writeXML # check for any overridden types - for type_, func in pyamf.TYPE_MAP.iteritems(): + for type_, func in list(pyamf.TYPE_MAP.items()): try: if isinstance(data, type_): return _CustomTypeFunc(self, func) @@ -544,7 +544,7 @@ def getTypeFunc(self, data): if python.callable(type_) and type_(data): return _CustomTypeFunc(self, func) - if isinstance(data, (list, tuple)): + if isinstance(data, (list, tuple, frozenset)): return self.writeSequence # now try some types that won't encode diff --git a/pyamf/python.py b/pyamf/python.py index 198e5266..e4c0b170 100644 --- a/pyamf/python.py +++ b/pyamf/python.py @@ -19,17 +19,7 @@ ) class_types = [type] int_types = [int] -str_types = [str] - -try: - int_types.append(long) -except NameError: - pass - -try: - str_types.append(unicode) -except NameError: - pass +str_types = [str, bytes] try: class_types.append(types.ClassType) diff --git a/pyamf/remoting/__init__.py b/pyamf/remoting/__init__.py index f683b588..766f2a3e 100644 --- a/pyamf/remoting/__init__.py +++ b/pyamf/remoting/__init__.py @@ -353,7 +353,7 @@ def raiseException(self): Raises an exception based on the fault object. There is no traceback available. """ - raise get_exception_from_fault(self), self.description, None + raise (get_exception_from_fault(self), self.description, None) class ErrorFault(BaseFault): @@ -460,7 +460,7 @@ def _read_args(): stream.read(1) x = stream.read_ulong() - return [decoder.readElement() for i in xrange(x)] + return [decoder.readElement() for i in range(x)] target = stream.read_utf8_string(stream.read_ushort()) response = stream.read_utf8_string(stream.read_ushort()) @@ -468,7 +468,7 @@ def _read_args(): status = STATUS_OK is_request = True - for code, s in STATUS_CODES.iteritems(): + for code, s in STATUS_CODES.items(): if not target.endswith(s): continue @@ -517,7 +517,7 @@ def _encode_body(message): return - stream.write('\x0a') + stream.write(b'\x0a') stream.write_ulong(len(message.body)) for x in message.body: encoder.writeElement(x) @@ -528,7 +528,7 @@ def _encode_body(message): target = None if isinstance(message, Request): - target = unicode(message.target) + target = str(message.target) else: target = u"%s%s" % (name, _get_status(message.status)) @@ -593,8 +593,8 @@ def get_fault(data): e = {} - for x, y in data.iteritems(): - if isinstance(x, unicode): + for x, y in data.items(): + if isinstance(x, str): e[str(x)] = y else: e[x] = y @@ -650,7 +650,7 @@ def decode(stream, strict=False, logger=None, timezone_offset=None, decoder.use_amf3 = msg.amfVersion == pyamf.AMF3 header_count = stream.read_ushort() - for i in xrange(header_count): + for i in range(header_count): name, required, data = _read_header(stream, decoder, strict) msg.headers[name] = data @@ -659,7 +659,7 @@ def decode(stream, strict=False, logger=None, timezone_offset=None, body_count = stream.read_short() - for i in xrange(body_count): + for i in range(body_count): context.clear() target, payload = _read_body(stream, decoder, strict, logger) @@ -706,7 +706,7 @@ def encode(msg, strict=False, logger=None, timezone_offset=None, **kwargs): stream.write_ushort(msg.amfVersion) stream.write_ushort(len(msg.headers)) - for name, header in msg.headers.iteritems(): + for name, header in msg.headers.items(): _write_header( name, header, @@ -718,7 +718,7 @@ def encode(msg, strict=False, logger=None, timezone_offset=None, **kwargs): stream.write_short(len(msg)) - for name, message in msg.iteritems(): + for name, message in msg.items(): encoder.context.clear() _write_body(name, message, stream, encoder, strict) diff --git a/pyamf/remoting/amf0.py b/pyamf/remoting/amf0.py index 8ccf49da..b90e610d 100644 --- a/pyamf/remoting/amf0.py +++ b/pyamf/remoting/amf0.py @@ -186,6 +186,6 @@ def build_fault(cls, e, tb, include_traceback=False): return remoting.ErrorFault( code=code, - description=unicode(e), + description=str(e), details=details ) diff --git a/pyamf/remoting/client/__init__.py b/pyamf/remoting/client/__init__.py index 283b955d..91c04ab1 100644 --- a/pyamf/remoting/client/__init__.py +++ b/pyamf/remoting/client/__init__.py @@ -7,8 +7,9 @@ @since: 0.1 """ -import urllib2 -import urlparse +from urllib.error import URLError +from urllib.parse import urlparse +from urllib.request import Request, urlopen import pyamf from pyamf import remoting @@ -19,9 +20,9 @@ GzipFile = False try: - from cStringIO import StringIO + from cStringIO import StringIO as BytesIO except ImportError: - from StringIO import StringIO + from io import BytesIO #: Default user agent is `PyAMF/x.x(.x)`. @@ -224,7 +225,7 @@ def __init__(self, url, amf_version=pyamf.AMF0, **kwargs): self.referer = kwargs.pop('referer', None) self.strict = kwargs.pop('strict', False) self.logger = kwargs.pop('logger', None) - self.opener = kwargs.pop('opener', urllib2.urlopen) + self.opener = kwargs.pop('opener', urlopen) if kwargs: raise TypeError('Unexpected keyword arguments %r' % (kwargs,)) @@ -232,7 +233,7 @@ def __init__(self, url, amf_version=pyamf.AMF0, **kwargs): self._setUrl(url) def _setUrl(self, url): - self.url = urlparse.urlparse(url) + self.url = urlparse(url) self._root_url = url if not self.url[0] in ('http', 'https'): @@ -285,7 +286,7 @@ def getService(self, name, auto_execute=True): @rtype: L{ServiceProxy} @raise TypeError: Unexpected type for string C{name}. """ - if not isinstance(name, basestring): + if not isinstance(name, str): raise TypeError('string type required') return ServiceProxy(self, name, auto_execute) @@ -398,7 +399,7 @@ def execute_single(self, request): strict=self.strict ) - http_request = urllib2.Request( + http_request = Request( self._root_url, body.getvalue(), self._get_execute_headers() ) @@ -425,7 +426,7 @@ def execute(self): strict=self.strict ) - http_request = urllib2.Request( + http_request = Request( self._root_url, body.getvalue(), self._get_execute_headers() ) @@ -446,7 +447,7 @@ def _getResponse(self, http_request): try: fbh = self.opener(http_request) - except urllib2.URLError, e: + except URLError as e: if self.logger: self.logger.exception('Failed request for %s', self._root_url) @@ -484,7 +485,7 @@ def _getResponse(self, http_request): 'Decompression of Content-Encoding: %s not available.' % ( content_encoding,)) - compressedstream = StringIO(bytes) + compressedstream = BytesIO(bytes) gzipper = GzipFile(fileobj=compressedstream) bytes = gzipper.read() gzipper.close() @@ -509,7 +510,7 @@ def _getResponse(self, http_request): if remoting.REQUEST_PERSISTENT_HEADER in response.headers: data = response.headers[remoting.REQUEST_PERSISTENT_HEADER] - for k, v in data.iteritems(): + for k, v in data.items(): self.headers[k] = v return response diff --git a/pyamf/remoting/gateway/__init__.py b/pyamf/remoting/gateway/__init__.py index b5e32aaf..2f8c2743 100644 --- a/pyamf/remoting/gateway/__init__.py +++ b/pyamf/remoting/gateway/__init__.py @@ -70,11 +70,11 @@ def __init__(self, service, description=None, authenticator=None, self.expose_request = expose_request self.preprocessor = preprocessor - def __cmp__(self, other): + def __eq__(self, other): if isinstance(other, ServiceWrapper): - return cmp(self.__dict__, other.__dict__) + return self.__dict__ == other.__dict__ - return cmp(self.service, other) + return self.service == other def _get_service_func(self, method, params): """ @@ -85,7 +85,7 @@ def _get_service_func(self, method, params): """ service = None - if isinstance(self.service, (type, types.ClassType)): + if isinstance(self.service, type): service = self.service() else: service = self.service @@ -238,7 +238,7 @@ class ServiceCollection(dict): I hold a collection of services, mapping names to objects. """ def __contains__(self, value): - if isinstance(value, basestring): + if isinstance(value, str): return value in self.keys() return value in self.values() @@ -276,7 +276,7 @@ def __init__(self, services=None, **kwargs): if services is None: services = {} - if not hasattr(services, 'iteritems'): + if not hasattr(services, 'items'): raise TypeError("dict type required for services") self.services = ServiceCollection() @@ -292,7 +292,7 @@ def __init__(self, services=None, **kwargs): if kwargs: raise TypeError('Unknown kwargs: %r' % (kwargs,)) - for name, service in services.iteritems(): + for name, service in services.items(): self.addService(service, name) def addService(self, service, name=None, description=None, @@ -308,16 +308,15 @@ def addService(self, service, name=None, description=None, @raise TypeError: C{service} cannot be a scalar value. @raise TypeError: C{service} must be C{callable} or a module. """ - if isinstance(service, (int, long, float, basestring)): + if isinstance(service, (int, float, str)): raise TypeError("Service cannot be a scalar value") allowed_types = ( types.ModuleType, types.FunctionType, - types.DictType, + dict, types.MethodType, - types.InstanceType, - types.ObjectType, + object, ) if not python.callable(service) and \ @@ -326,10 +325,10 @@ def addService(self, service, name=None, description=None, if name is None: # TODO: include the module in the name - if isinstance(service, (type, types.ClassType)): + if isinstance(service, (type, type)): name = service.__name__ elif isinstance(service, types.FunctionType): - name = service.func_name + name = service.__name__ elif isinstance(service, types.ModuleType): name = service.__name__ else: @@ -364,7 +363,7 @@ def removeService(self, service): @type service: C{callable} or a class instance @raise NameError: Service not found. """ - for name, wrapper in self.services.iteritems(): + for name, wrapper in self.services.items(): if service in (name, wrapper.service): del self.services[name] return diff --git a/pyamf/remoting/gateway/wsgi.py b/pyamf/remoting/gateway/wsgi.py index d5d7d6fa..75fb3d32 100644 --- a/pyamf/remoting/gateway/wsgi.py +++ b/pyamf/remoting/gateway/wsgi.py @@ -106,7 +106,7 @@ def __call__(self, environ, start_response): return [response] except (KeyboardInterrupt, SystemExit): raise - except: + except Exception as e: if self.logger: self.logger.exception('Unexpected error decoding AMF request') diff --git a/pyamf/sol.py b/pyamf/sol.py index 6d0c7888..6ee51d2e 100644 --- a/pyamf/sol.py +++ b/pyamf/sol.py @@ -118,7 +118,7 @@ def encode(name, values, strict=True, encoding=pyamf.AMF0): stream.write(PADDING_BYTE * 3) stream.write_uchar(encoding) - for n, v in values.iteritems(): + for n, v in values.items(): encoder.serialiseString(n) encoder.writeElement(v) @@ -153,7 +153,7 @@ def load(name_or_file): name, values = decode(f.read()) s = SOL(name) - for n, v in values.iteritems(): + for n, v in values.items(): s[n] = v if opened is True: diff --git a/pyamf/tests/gateway/test_django.py b/pyamf/tests/gateway/test_django.py index 04f18cbf..6698edf5 100644 --- a/pyamf/tests/gateway/test_django.py +++ b/pyamf/tests/gateway/test_django.py @@ -16,7 +16,7 @@ try: from cStringIO import StringIO except ImportError: - from StringIO import StringIO + from io import BytesIO as StringIO try: import django as _django diff --git a/pyamf/tests/gateway/test_google.py b/pyamf/tests/gateway/test_google.py index d63cbb2b..aba82287 100644 --- a/pyamf/tests/gateway/test_google.py +++ b/pyamf/tests/gateway/test_google.py @@ -11,7 +11,7 @@ import unittest -from StringIO import StringIO +from io import BytesIO as StringIO try: from google.appengine.ext import webapp diff --git a/pyamf/tests/gateway/test_wsgi.py b/pyamf/tests/gateway/test_wsgi.py index 2f160889..49d3e14c 100644 --- a/pyamf/tests/gateway/test_wsgi.py +++ b/pyamf/tests/gateway/test_wsgi.py @@ -36,8 +36,8 @@ def sr(status, headers): self.executed = True return r - - return self.gw(kwargs, sr) + response = self.gw(kwargs, sr) + return response def makeRequest(self, service, body, raw=False): if not raw: @@ -62,7 +62,7 @@ def bad_response(status, headers): def test_bad_request(self): request = util.BufferedByteStream() - request.write('Bad request') + request.write(b'Bad request') request.seek(0, 0) def start_response(status, headers): @@ -183,7 +183,8 @@ def echo(d): self.gw.addService(echo) self.gw.timezone_offset = -18000 - response = self.doRequest(self.makeRequest('echo', now), None) + request = self.makeRequest('echo', now) + response = self.doRequest(request, None) envelope = remoting.decode(''.join(response)) message = envelope['/1'] diff --git a/pyamf/tests/modules/test_sets.py b/pyamf/tests/modules/test_sets.py index 5ac36240..8781c9ec 100644 --- a/pyamf/tests/modules/test_sets.py +++ b/pyamf/tests/modules/test_sets.py @@ -6,7 +6,6 @@ """ import unittest -import sets import pyamf from pyamf.tests.util import check_buffer @@ -14,27 +13,27 @@ class ImmutableSetTestCase(unittest.TestCase): def test_amf0_encode(self): - x = sets.ImmutableSet(['1', '2', '3']) + x = frozenset(['1', '2', '3']) self.assertTrue(check_buffer( pyamf.encode(x, encoding=pyamf.AMF0).getvalue(), ( - '\n\x00\x00\x00\x03', ( - '\x02\x00\x011', - '\x02\x00\x013', - '\x02\x00\x012' + b'\n\x00\x00\x00\x03', ( + b'\x02\x00\x011', + b'\x02\x00\x013', + b'\x02\x00\x012' ) ) )) def test_amf3_encode(self): - x = sets.ImmutableSet(['1', '2', '3']) + x = frozenset(['1', '2', '3']) self.assertTrue(check_buffer( pyamf.encode(x, encoding=pyamf.AMF3).getvalue(), ( - '\t\x07\x01', ( - '\x06\x031', - '\x06\x033', - '\x06\x032' + b'\t\x07\x01', ( + b'\x06\x031', + b'\x06\x033', + b'\x06\x032' ) ) )) diff --git a/pyamf/tests/remoting/test_client.py b/pyamf/tests/remoting/test_client.py index d8fd5350..dddbd1d9 100644 --- a/pyamf/tests/remoting/test_client.py +++ b/pyamf/tests/remoting/test_client.py @@ -10,7 +10,7 @@ """ import unittest -import urllib2 +import urllib.request as urllib2 import pyamf from pyamf import remoting, util @@ -265,10 +265,10 @@ class BaseServiceTestCase(unittest.TestCase): """ canned_response = ( - '\x00\x00\x00\x00\x00\x01\x00\x0b/1/onResult\x00' - '\x04null\x00\x00\x00\x00\n\x00\x00\x00\x03\x00?\xf0\x00\x00' - '\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00@\x08\x00' - '\x00\x00\x00\x00\x00' + b'\x00\x00\x00\x00\x00\x01\x00\x0b/1/onResult\x00' + b'\x04null\x00\x00\x00\x00\n\x00\x00\x00\x03\x00?\xf0\x00\x00' + b'\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00@\x08\x00' + b'\x00\x00\x00\x00\x00' ) headers = { @@ -658,6 +658,6 @@ def test_good_response(self): def test_bad_response(self): self.headers['Content-Length'] = len('foobar') - self.setResponse(200, 'foobar', self.headers) + self.setResponse(200, b'foobar', self.headers) self.assertRaises(IOError, self.gw._getResponse, None) diff --git a/pyamf/tests/test_adapters_util.py b/pyamf/tests/test_adapters_util.py index 5e4b30f0..db347baa 100644 --- a/pyamf/tests/test_adapters_util.py +++ b/pyamf/tests/test_adapters_util.py @@ -11,12 +11,6 @@ from pyamf.adapters import util -# check for set function in python 2.3 -import __builtin__ - -if not hasattr(__builtin__, 'set'): - from sets import Set as set - class Iterable(object): """ diff --git a/pyamf/tests/test_basic.py b/pyamf/tests/test_basic.py index 2735dbc9..16e8dd63 100644 --- a/pyamf/tests/test_basic.py +++ b/pyamf/tests/test_basic.py @@ -66,7 +66,7 @@ def test_iter(self): x = [] - for k, v in bag.iteritems(): + for k, v in bag.items(): x.append((k, v)) self.assertEqual(x, [('spam', 'eggs')]) diff --git a/pyamf/tests/test_imports.py b/pyamf/tests/test_imports.py index daa5a6e8..917a3e73 100644 --- a/pyamf/tests/test_imports.py +++ b/pyamf/tests/test_imports.py @@ -43,7 +43,7 @@ def tearDown(self): def _clearModules(self, *args): for mod in args: - for k, v in sys.modules.copy().iteritems(): + for k, v in sys.modules.copy().items(): if k.startswith(mod) or k == 'pyamf.tests.%s' % (mod,): del sys.modules[k] @@ -95,7 +95,7 @@ def h(mod): try: import spam # noqa - except Exception, e: + except Exception as e: pass else: self.fail('expected exception') diff --git a/pyamf/tests/test_remoting.py b/pyamf/tests/test_remoting.py index d77978ab..a0b3301c 100644 --- a/pyamf/tests/test_remoting.py +++ b/pyamf/tests/test_remoting.py @@ -24,14 +24,14 @@ def test_client_version(self): """ Tests the AMF client version. """ - for x in ('\x00', '\x01', '\x03'): + for x in (b'\x00', b'\x01', b'\x03'): try: - remoting.decode('\x00' + x) + remoting.decode(b'\x00' + x) except IOError: pass def test_null_msg(self): - msg = remoting.decode('\x00\x00\x00\x00\x00\x00') + msg = remoting.decode(b'\x00\x00\x00\x00\x00\x00') self.assertEqual(msg.amfVersion, 0) self.assertEqual(msg.headers, {}) @@ -46,8 +46,8 @@ def test_simple_header(self): Test header decoder. """ msg = remoting.decode( - '\x00\x00\x00\x01\x00\x04name\x00\x00\x00\x00' - '\x05\x0a\x00\x00\x00\x00\x00\x00' + b'\x00\x00\x00\x01\x00\x04name\x00\x00\x00\x00' + b'\x05\x0a\x00\x00\x00\x00\x00\x00' ) self.assertEqual(msg.amfVersion, 0) @@ -63,31 +63,31 @@ def test_simple_header(self): def test_required_header(self): msg = remoting.decode( - '\x00\x00\x00\x01\x00\x04name\x01\x00\x00\x00' - '\x05\x0a\x00\x00\x00\x00\x00\x00' + b'\x00\x00\x00\x01\x00\x04name\x01\x00\x00\x00' + b'\x05\x0a\x00\x00\x00\x00\x00\x00' ) self.assertTrue(msg.headers.is_required('name')) def test_invalid_header_data_length(self): remoting.decode( - '\x00\x00\x00\x01\x00\x04name\x00\x00\x00\x00\x06\x0a' - '\x00\x00\x00\x00\x00\x00' + b'\x00\x00\x00\x01\x00\x04name\x00\x00\x00\x00\x06\x0a' + b'\x00\x00\x00\x00\x00\x00' ) self.failUnlessRaises( pyamf.DecodeError, remoting.decode, - '\x00\x00\x00\x01\x00\x04name\x00\x00\x00\x00\x06\x0a\x00\x00\x00' - '\x00\x00\x00', + b'\x00\x00\x00\x01\x00\x04name\x00\x00\x00\x00\x06\x0a\x00\x00\x00' + b'\x00\x00\x00', strict=True ) def test_multiple_headers(self): msg = remoting.decode( - '\x00\x00\x00\x02\x00\x04name\x00\x00\x00\x00' - '\x05\x0a\x00\x00\x00\x00\x00\x04spam\x01\x00\x00\x00\x01\x05\x00' - '\x00' + b'\x00\x00\x00\x02\x00\x04name\x00\x00\x00\x00' + b'\x05\x0a\x00\x00\x00\x00\x00\x04spam\x01\x00\x00\x00\x01\x05\x00' + b'\x00' ) self.assertEqual(msg.amfVersion, 0) @@ -112,9 +112,9 @@ def test_simple_body(self): ) msg = remoting.decode( - '\x00\x00\x00\x00\x00\x01\x00\x09test.test\x00' - '\x02/1\x00\x00\x00\x14\x0a\x00\x00\x00\x01\x08\x00\x00\x00\x00' - '\x00\x01\x61\x02\x00\x01\x61\x00\x00\x09' + b'\x00\x00\x00\x00\x00\x01\x00\x09test.test\x00' + b'\x02/1\x00\x00\x00\x14\x0a\x00\x00\x00\x01\x08\x00\x00\x00\x00' + b'\x00\x01\x61\x02\x00\x01\x61\x00\x00\x09' ) self.assertEqual(msg.amfVersion, 0) @@ -136,26 +136,26 @@ def test_simple_body(self): def test_invalid_body_data_length(self): remoting.decode( - '\x00\x00\x00\x00\x00\x01\x00\x09test.test\x00\x02/1' - '\x00\x00\x00\x13\x0a\x00\x00\x00\x01\x08\x00\x00\x00\x00\x00\x01' - '\x61\x02\x00\x01\x61\x00\x00\x09' + b'\x00\x00\x00\x00\x00\x01\x00\x09test.test\x00\x02/1' + b'\x00\x00\x00\x13\x0a\x00\x00\x00\x01\x08\x00\x00\x00\x00\x00\x01' + b'\x61\x02\x00\x01\x61\x00\x00\x09' ) self.failUnlessRaises( pyamf.DecodeError, remoting.decode, - '\x00\x00\x00\x00\x00\x01\x00\x09test.test\x00\x02/1\x00\x00\x00' - '\x13\x0a\x00\x00\x00\x01\x08\x00\x00\x00\x00\x00\x01\x61\x02\x00' - '\x01\x61\x00\x00\x09', + b'\x00\x00\x00\x00\x00\x01\x00\x09test.test\x00\x02/1\x00\x00\x00' + b'\x13\x0a\x00\x00\x00\x01\x08\x00\x00\x00\x00\x00\x01\x61\x02\x00' + b'\x01\x61\x00\x00\x09', strict=True ) def test_message_order(self): request = util.BufferedByteStream() request.write( - '\x00\x00\x00\x00\x00\x02\x00\x08get_spam\x00\x02/2\x00' - '\x00\x00\x00\x0a\x00\x00\x00\x00\x00\x04echo\x00\x02/1\x00\x00' - '\x00\x00\x0a\x00\x00\x00\x01\x02\x00\x0bhello world' + b'\x00\x00\x00\x00\x00\x02\x00\x08get_spam\x00\x02/2\x00' + b'\x00\x00\x00\x0a\x00\x00\x00\x00\x00\x04echo\x00\x02/1\x00\x00' + b'\x00\x00\x0a\x00\x00\x00\x01\x02\x00\x0bhello world' ) request.seek(0, 0) @@ -169,14 +169,14 @@ def test_message_order(self): def test_multiple_request_header_references(self): msg = remoting.decode( - '\x00\x00\x00\x01\x00\x0b\x43\x72\x65\x64\x65\x6e\x74\x69\x61\x6c' - '\x73\x00\x00\x00\x00\x2c\x11\x0a\x0b\x01\x0d\x75\x73\x65\x72\x69' - '\x64\x06\x1f\x67\x65\x6e\x6f\x70\x72\x6f\x5c\x40\x67\x65\x72\x61' - '\x72\x64\x11\x70\x61\x73\x73\x77\x6f\x72\x64\x06\x09\x67\x67\x67' - '\x67\x01\x00\x01\x00\x0b\x63\x72\x65\x61\x74\x65\x47\x72\x6f\x75' - '\x70\x00\x02\x2f\x31\x00\x00\x00\x1c\x0a\x00\x00\x00\x01\x11\x0a' - '\x0b\x01\x09\x73\x74\x72\x41\x06\x09\x74\x65\x73\x74\x09\x73\x74' - '\x72\x42\x06\x02\x01' + b'\x00\x00\x00\x01\x00\x0b\x43\x72\x65\x64\x65\x6e\x74\x69\x61\x6c' + b'\x73\x00\x00\x00\x00\x2c\x11\x0a\x0b\x01\x0d\x75\x73\x65\x72\x69' + b'\x64\x06\x1f\x67\x65\x6e\x6f\x70\x72\x6f\x5c\x40\x67\x65\x72\x61' + b'\x72\x64\x11\x70\x61\x73\x73\x77\x6f\x72\x64\x06\x09\x67\x67\x67' + b'\x67\x01\x00\x01\x00\x0b\x63\x72\x65\x61\x74\x65\x47\x72\x6f\x75' + b'\x70\x00\x02\x2f\x31\x00\x00\x00\x1c\x0a\x00\x00\x00\x01\x11\x0a' + b'\x0b\x01\x09\x73\x74\x72\x41\x06\x09\x74\x65\x73\x74\x09\x73\x74' + b'\x72\x42\x06\x02\x01' ) self.assertEqual(msg.amfVersion, 0) @@ -202,8 +202,8 @@ def test_timezone(self): td = datetime.timedelta(hours=-5) msg = remoting.decode( - '\x00\x00\x00\x00\x00\x01\x00\x0b/1/onResult\x00\x04null\x00\x00' - '\x00\x00\n\x00\x00\x00\x01\x0bBr>\xcc\n~\x00\x00\x00\x00', + b'\x00\x00\x00\x00\x00\x01\x00\x0b/1/onResult\x00\x04null\x00\x00' + b'\x00\x00\n\x00\x00\x00\x01\x0bBr>\xcc\n~\x00\x00\x00\x00', timezone_offset=td) self.assertEqual( @@ -220,12 +220,12 @@ def test_basic(self): """ """ msg = remoting.Envelope(pyamf.AMF0) - self.assertEqual(remoting.encode(msg).getvalue(), '\x00' * 6) + self.assertEqual(remoting.encode(msg).getvalue(), b'\x00' * 6) msg = remoting.Envelope(pyamf.AMF3) self.assertEqual( remoting.encode(msg).getvalue(), - '\x00\x03' + '\x00' * 4 + b'\x00\x03' + b'\x00' * 4 ) def test_header(self): @@ -237,8 +237,8 @@ def test_header(self): msg.headers['spam'] = (False, 'eggs') self.assertEqual( remoting.encode(msg).getvalue(), - '\x00\x00\x00\x01\x00\x04spam\x00\x00\x00\x00\x00\n\x00\x00\x00' - '\x02\x01\x00\x02\x00\x04eggs\x00\x00' + b'\x00\x00\x00\x01\x00\x04spam\x00\x00\x00\x00\x00\n\x00\x00\x00' + b'\x02\x01\x00\x02\x00\x04eggs\x00\x00' ) msg = remoting.Envelope(pyamf.AMF0) @@ -246,9 +246,9 @@ def test_header(self): msg.headers['spam'] = (True, ['a', 'b', 'c']) self.assertEqual( remoting.encode(msg).getvalue(), - '\x00\x00\x00\x01\x00\x04spam\x00\x00\x00\x00\x00\n\x00\x00\x00' - '\x02\x01\x01\n\x00\x00\x00\x03\x02\x00\x01a\x02\x00\x01b\x02\x00' - '\x01c\x00\x00' + b'\x00\x00\x00\x01\x00\x04spam\x00\x00\x00\x00\x00\n\x00\x00\x00' + b'\x02\x01\x01\n\x00\x00\x00\x03\x02\x00\x01a\x02\x00\x01b\x02\x00' + b'\x01c\x00\x00' ) def test_request(self): @@ -271,8 +271,8 @@ def test_request(self): self.assertEqual( remoting.encode(msg).getvalue(), - '\x00\x00\x00\x00\x00\x01\x00\ttest.test\x00\x02/1\x00\x00\x00' - '\x00\n\x00\x00\x00\x01\x02\x00\x05hello' + b'\x00\x00\x00\x00\x00\x01\x00\ttest.test\x00\x02/1\x00\x00\x00' + b'\x00\n\x00\x00\x00\x01\x02\x00\x05hello' ) def test_response(self): @@ -295,10 +295,10 @@ def test_response(self): self.assertEqual( remoting.encode(msg).getvalue(), - '\x00\x00\x00\x00' - '\x00\x01\x00\x0b/1/onResult\x00\x04null\x00\x00\x00\x00\n\x00\x00' - '\x00\x03\x00?\xf0\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00' - '\x00\x00\x00\x00@\x08\x00\x00\x00\x00\x00\x00' + b'\x00\x00\x00\x00' + b'\x00\x01\x00\x0b/1/onResult\x00\x04null\x00\x00\x00\x00\n\x00\x00' + b'\x00\x03\x00?\xf0\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00' + b'\x00\x00\x00\x00@\x08\x00\x00\x00\x00\x00\x00' ) def test_message_order(self): @@ -343,9 +343,9 @@ def test_timezone(self): self.assertEqual( stream, - '\x00\x00\x00\x00\x00\x01\x00\x0b/1/onResult' - '\x00\x04null\x00\x00\x00\x00\n\x00\x00\x00\x01\x0bBr>\xdd5\x06' - '\x00\x00\x00\x00' + b'\x00\x00\x00\x00\x00\x01\x00\x0b/1/onResult' + b'\x00\x04null\x00\x00\x00\x00\n\x00\x00\x00\x01\x0bBr>\xdd5\x06' + b'\x00\x00\x00\x00' ) @@ -357,8 +357,8 @@ def test_request(self): self.assertEqual( remoting.encode(msg, strict=True).getvalue(), - '\x00\x00\x00\x00\x00\x01\x00\ttest.test\x00\x02/1\x00\x00\x00' - '\r\n\x00\x00\x00\x01\x02\x00\x05hello' + b'\x00\x00\x00\x00\x00\x01\x00\ttest.test\x00\x02/1\x00\x00\x00' + b'\r\n\x00\x00\x00\x01\x02\x00\x05hello' ) def test_response(self): @@ -368,8 +368,8 @@ def test_response(self): self.assertEqual( remoting.encode(msg, strict=True).getvalue(), - '\x00\x00\x00\x00\x00\x01\x00\x0b/1/onResult\x00\x04null\x00\x00' - '\x00\x0c\n\x00\x00\x00\x01\x02\x00\x04spam' + b'\x00\x00\x00\x00\x00\x01\x00\x0b/1/onResult\x00\x04null\x00\x00' + b'\x00\x0c\n\x00\x00\x00\x01\x02\x00\x04spam' ) @@ -399,11 +399,11 @@ def test_body_references(self): s = remoting.encode(msg).getvalue() self.assertEqual( s, - '\x00\x00\x00\x00\x00\x02\x00\x03foo\x00\x02/1' - '\x00\x00\x00\x00\n\x00\x00\x00\x01\n\x00\x00\x00\x03\x02\x00\x01' - 'a\x02\x00\x01b\x02\x00\x01c\x00\x03bar\x00\x02/2\x00\x00\x00\x00' - '\n\x00\x00\x00\x01\n\x00\x00\x00\x03\x02\x00\x01a\x02\x00\x01b' - '\x02\x00\x01c' + b'\x00\x00\x00\x00\x00\x02\x00\x03foo\x00\x02/1' + b'\x00\x00\x00\x00\n\x00\x00\x00\x01\n\x00\x00\x00\x03\x02\x00\x01' + b'a\x02\x00\x01b\x02\x00\x01c\x00\x03bar\x00\x02/2\x00\x00\x00\x00' + b'\n\x00\x00\x00\x01\n\x00\x00\x00\x03\x02\x00\x01a\x02\x00\x01b' + b'\x02\x00\x01c' ) @@ -420,38 +420,38 @@ def test_encode_bytearray(self): self.assertEqual( remoting.encode(msg).getvalue(), - '\x00\x00\x00\x00\x00\x01\x00\x0b/1/onResult\x00\x04null' - '\x00\x00\x00\x00\n\x00\x00\x00\x01\x11\x0c\x1112345678' + b'\x00\x00\x00\x00\x00\x01\x00\x0b/1/onResult\x00\x04null' + b'\x00\x00\x00\x00\n\x00\x00\x00\x01\x11\x0c\x1112345678' ) class ReprTestCase(unittest.TestCase): def test_response(self): - r = remoting.Response(u'€±') + r = remoting.Response('€±') self.assertEqual( repr(r), - "u'\\u20ac\\xb1'" + "'€±'" ) def test_request(self): - r = remoting.Request(u'€±', [u'å∫ç']) + r = remoting.Request('€±', ['å∫ç']) self.assertEqual( repr(r), - "[u'\\xe5\\u222b\\xe7']" + "['å∫ç']" ) def test_base_fault(self): r = remoting.BaseFault( - code=u'å', - type=u'å', - description=u'å', - details=u'å' + code='å', + type='å', + description='å', + details='å' ) self.assertEqual( repr(r), - "BaseFault level=None code=u'\\xe5' type=u'\\xe5' description=" - "u'\\xe5'\nTraceback:\nu'\\xe5'" + "BaseFault level=None code='å' type='å' description=" + "'å'\nTraceback:\n'å'" ) diff --git a/pyamf/tests/test_util.py b/pyamf/tests/test_util.py index a13220a0..31e950f8 100644 --- a/pyamf/tests/test_util.py +++ b/pyamf/tests/test_util.py @@ -12,7 +12,7 @@ import unittest from datetime import datetime -from StringIO import StringIO +from io import StringIO import pyamf from pyamf import util @@ -64,31 +64,31 @@ def test_create(self): sp = util.BufferedByteStream() self.assertEqual(sp.tell(), 0) - self.assertEqual(sp.getvalue(), '') + self.assertEqual(sp.getvalue(), b'') self.assertEqual(len(sp), 0) - self.assertEqual(sp.getvalue(), '') + self.assertEqual(sp.getvalue(), b'') sp = util.BufferedByteStream(None) self.assertEqual(sp.tell(), 0) - self.assertEqual(sp.getvalue(), '') + self.assertEqual(sp.getvalue(), b'') self.assertEqual(len(sp), 0) sp = util.BufferedByteStream('') self.assertEqual(sp.tell(), 0) - self.assertEqual(sp.getvalue(), '') + self.assertEqual(sp.getvalue(), b'') self.assertEqual(len(sp), 0) sp = util.BufferedByteStream('spam') self.assertEqual(sp.tell(), 0) - self.assertEqual(sp.getvalue(), 'spam') + self.assertEqual(sp.getvalue(), b'spam') self.assertEqual(len(sp), 4) sp = util.BufferedByteStream(StringIO('this is a test')) self.assertEqual(sp.tell(), 0) - self.assertEqual(sp.getvalue(), 'this is a test') + self.assertEqual(sp.getvalue(), b'this is a test') self.assertEqual(len(sp), 14) self.assertRaises(TypeError, util.BufferedByteStream, self) @@ -97,62 +97,62 @@ def test_getvalue(self): sp = util.BufferedByteStream() sp.write('asdfasdf') - self.assertEqual(sp.getvalue(), 'asdfasdf') + self.assertEqual(sp.getvalue(), b'asdfasdf') sp.write('spam') - self.assertEqual(sp.getvalue(), 'asdfasdfspam') + self.assertEqual(sp.getvalue(), b'asdfasdfspam') def test_read(self): sp = util.BufferedByteStream('this is a test') self.assertEqual(len(sp), 14) - self.assertEqual(sp.read(1), 't') - self.assertEqual(sp.getvalue(), 'this is a test') + self.assertEqual(sp.read(1), b't') + self.assertEqual(sp.getvalue(), b'this is a test') self.assertEqual(len(sp), 14) - self.assertEqual(sp.read(10), 'his is a t') - self.assertEqual(sp.read(), 'est') + self.assertEqual(sp.read(10), b'his is a t') + self.assertEqual(sp.read(), b'est') def test_seek(self): sp = util.BufferedByteStream('abcdefghijklmnopqrstuvwxyz') - self.assertEqual(sp.getvalue(), 'abcdefghijklmnopqrstuvwxyz') + self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz') self.assertEqual(sp.tell(), 0) # Relative to the beginning of the stream sp.seek(0, 0) self.assertEqual(sp.tell(), 0) - self.assertEqual(sp.getvalue(), 'abcdefghijklmnopqrstuvwxyz') - self.assertEqual(sp.read(1), 'a') + self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz') + self.assertEqual(sp.read(1), b'a') self.assertEqual(len(sp), 26) sp.seek(10, 0) self.assertEqual(sp.tell(), 10) - self.assertEqual(sp.getvalue(), 'abcdefghijklmnopqrstuvwxyz') - self.assertEqual(sp.read(1), 'k') + self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz') + self.assertEqual(sp.read(1), b'k') self.assertEqual(len(sp), 26) sp.seek(-5, 1) self.assertEqual(sp.tell(), 6) - self.assertEqual(sp.getvalue(), 'abcdefghijklmnopqrstuvwxyz') - self.assertEqual(sp.read(1), 'g') + self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz') + self.assertEqual(sp.read(1), b'g') self.assertEqual(len(sp), 26) sp.seek(-3, 2) self.assertEqual(sp.tell(), 23) - self.assertEqual(sp.getvalue(), 'abcdefghijklmnopqrstuvwxyz') - self.assertEqual(sp.read(1), 'x') + self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz') + self.assertEqual(sp.read(1), b'x') self.assertEqual(len(sp), 26) def test_tell(self): sp = util.BufferedByteStream('abcdefghijklmnopqrstuvwxyz') - self.assertEqual(sp.getvalue(), 'abcdefghijklmnopqrstuvwxyz') + self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz') self.assertEqual(len(sp), 26) self.assertEqual(sp.tell(), 0) sp.read(1) self.assertEqual(sp.tell(), 1) - self.assertEqual(sp.getvalue(), 'abcdefghijklmnopqrstuvwxyz') + self.assertEqual(sp.getvalue(), b'abcdefghijklmnopqrstuvwxyz') self.assertEqual(len(sp), 26) sp.read(5) @@ -161,50 +161,50 @@ def test_tell(self): def test_truncate(self): sp = util.BufferedByteStream('abcdef') - self.assertEqual(sp.getvalue(), 'abcdef') + self.assertEqual(sp.getvalue(), b'abcdef') self.assertEqual(len(sp), 6) sp.truncate() - self.assertEqual(sp.getvalue(), '') + self.assertEqual(sp.getvalue(), b'') self.assertEqual(len(sp), 0) sp = util.BufferedByteStream('hello') - self.assertEqual(sp.getvalue(), 'hello') + self.assertEqual(sp.getvalue(), b'hello') self.assertEqual(len(sp), 5) sp.truncate(3) - self.assertEqual(sp.getvalue(), 'hel') + self.assertEqual(sp.getvalue(), b'hel') self.assertEqual(len(sp), 3) def test_write(self): sp = util.BufferedByteStream() - self.assertEqual(sp.getvalue(), '') + self.assertEqual(sp.getvalue(), b'') self.assertEqual(len(sp), 0) self.assertEqual(sp.tell(), 0) sp.write('hello') - self.assertEqual(sp.getvalue(), 'hello') + self.assertEqual(sp.getvalue(), b'hello') self.assertEqual(len(sp), 5) self.assertEqual(sp.tell(), 5) - sp = util.BufferedByteStream('xyz') + sp = util.BufferedByteStream(b'xyz') - self.assertEqual(sp.getvalue(), 'xyz') + self.assertEqual(sp.getvalue(), b'xyz') self.assertEqual(len(sp), 3) self.assertEqual(sp.tell(), 0) sp.write('abc') - self.assertEqual(sp.getvalue(), 'abc') + self.assertEqual(sp.getvalue(), b'abc') self.assertEqual(len(sp), 3) self.assertEqual(sp.tell(), 3) def test_len(self): sp = util.BufferedByteStream() - self.assertEqual(sp.getvalue(), '') + self.assertEqual(sp.getvalue(), b'') self.assertEqual(len(sp), 0) self.assertEqual(sp.tell(), 0) @@ -224,17 +224,17 @@ def test_len(self): def test_consume(self): sp = util.BufferedByteStream() - self.assertEqual(sp.getvalue(), '') + self.assertEqual(sp.getvalue(), b'') self.assertEqual(sp.tell(), 0) sp.consume() - self.assertEqual(sp.getvalue(), '') + self.assertEqual(sp.getvalue(), b'') self.assertEqual(sp.tell(), 0) sp = util.BufferedByteStream('foobar') - self.assertEqual(sp.getvalue(), 'foobar') + self.assertEqual(sp.getvalue(), b'foobar') self.assertEqual(sp.tell(), 0) sp.seek(3) @@ -242,7 +242,7 @@ def test_consume(self): self.assertEqual(sp.tell(), 3) sp.consume() - self.assertEqual(sp.getvalue(), 'bar') + self.assertEqual(sp.getvalue(), b'bar') self.assertEqual(sp.tell(), 0) # from ticket 451 - http://pyamf.org/ticket/451 @@ -257,7 +257,7 @@ def test_consume(self): sp = util.BufferedByteStream('abcdef') sp.seek(6) sp.consume() - self.assertEqual(sp.getvalue(), '') + self.assertEqual(sp.getvalue(), b'') class DataTypeMixInTestCase(unittest.TestCase): @@ -286,7 +286,7 @@ def _read_endian(self, data, func, args, expected): self.assertEqual(result, expected) def test_read_uchar(self): - x = util.BufferedByteStream('\x00\xff') + x = util.BufferedByteStream(b'\x00\xff') self.assertEqual(x.read_uchar(), 0) self.assertEqual(x.read_uchar(), 255) @@ -295,16 +295,16 @@ def test_write_uchar(self): x = util.BufferedByteStream() x.write_uchar(0) - self.assertEqual(x.getvalue(), '\x00') + self.assertEqual(x.getvalue(), b'\x00') x.write_uchar(255) - self.assertEqual(x.getvalue(), '\x00\xff') + self.assertEqual(x.getvalue(), b'\x00\xff') self.assertRaises(OverflowError, x.write_uchar, 256) self.assertRaises(OverflowError, x.write_uchar, -1) self.assertRaises(TypeError, x.write_uchar, 'f') def test_read_char(self): - x = util.BufferedByteStream('\x00\x7f\xff\x80') + x = util.BufferedByteStream(b'\x00\x7f\xff\x80') self.assertEqual(x.read_char(), 0) self.assertEqual(x.read_char(), 127) @@ -318,7 +318,7 @@ def test_write_char(self): x.write_char(-128) x.write_char(127) - self.assertEqual(x.getvalue(), '\x00\x80\x7f') + self.assertEqual(x.getvalue(), b'\x00\x80\x7f') self.assertRaises(OverflowError, x.write_char, 128) self.assertRaises(OverflowError, x.write_char, -129) @@ -327,13 +327,13 @@ def test_write_char(self): def test_write_ushort(self): x = util.BufferedByteStream() - self._write_endian(x, x.write_ushort, (0,), ('\x00\x00', '\x00\x00')) - self._write_endian(x, x.write_ushort, (12345,), ('09', '90')) + self._write_endian(x, x.write_ushort, (0,), (b'\x00\x00', b'\x00\x00')) + self._write_endian(x, x.write_ushort, (12345,), (b'09', b'90')) self._write_endian( x, x.write_ushort, (65535,), - ('\xff\xff', '\xff\xff') + (b'\xff\xff', b'\xff\xff') ) self.assertRaises(OverflowError, x.write_ushort, 65536) @@ -341,9 +341,9 @@ def test_write_ushort(self): self.assertRaises(TypeError, x.write_ushort, 'aa') def test_read_ushort(self): - self._read_endian(['\x00\x00', '\x00\x00'], 'read_ushort', (), 0) + self._read_endian([b'\x00\x00', b'\x00\x00'], 'read_ushort', (), 0) self._read_endian(['09', '90'], 'read_ushort', (), 12345) - self._read_endian(['\xff\xff', '\xff\xff'], 'read_ushort', (), 65535) + self._read_endian([b'\xff\xff', b'\xff\xff'], 'read_ushort', (), 65535) def test_write_short(self): x = util.BufferedByteStream() @@ -352,12 +352,12 @@ def test_write_short(self): x, x.write_short, (-5673,), - ('\xe9\xd7', '\xd7\xe9') + (b'\xe9\xd7', b'\xd7\xe9') ) self._write_endian( x, x.write_short, (32767,), - ('\x7f\xff', '\xff\x7f') + (b'\x7f\xff', b'\xff\x7f') ) self.assertRaises(OverflowError, x.write_ushort, 65537) @@ -365,8 +365,8 @@ def test_write_short(self): self.assertRaises(TypeError, x.write_short, '\x00\x00') def test_read_short(self): - self._read_endian(['\xe9\xd7', '\xd7\xe9'], 'read_short', (), -5673) - self._read_endian(['\x7f\xff', '\xff\x7f'], 'read_short', (), 32767) + self._read_endian([b'\xe9\xd7', b'\xd7\xe9'], 'read_short', (), -5673) + self._read_endian([b'\x7f\xff', b'\xff\x7f'], 'read_short', (), 32767) def test_write_ulong(self): x = util.BufferedByteStream() @@ -375,43 +375,43 @@ def test_write_ulong(self): x, x.write_ulong, (0,), - ('\x00\x00\x00\x00', '\x00\x00\x00\x00') + (b'\x00\x00\x00\x00', b'\x00\x00\x00\x00') ) self._write_endian( x, x.write_ulong, (16810049,), - ('\x01\x00\x80A', 'A\x80\x00\x01') + (b'\x01\x00\x80A', b'A\x80\x00\x01') ) self._write_endian( x, x.write_ulong, - (4294967295L,), - ('\xff\xff\xff\xff', '\xff\xff\xff\xff') + (4294967295,), + (b'\xff\xff\xff\xff', b'\xff\xff\xff\xff') ) - self.assertRaises(OverflowError, x.write_ulong, 4294967296L) + self.assertRaises(OverflowError, x.write_ulong, 4294967296) self.assertRaises(OverflowError, x.write_ulong, -1) self.assertRaises(TypeError, x.write_ulong, '\x00\x00\x00\x00') def test_read_ulong(self): self._read_endian( - ['\x00\x00\x00\x00', '\x00\x00\x00\x00'], + [b'\x00\x00\x00\x00', b'\x00\x00\x00\x00'], 'read_ulong', (), 0 ) self._read_endian( - ['\x01\x00\x80A', 'A\x80\x00\x01'], + [b'\x01\x00\x80A', b'A\x80\x00\x01'], 'read_ulong', (), 16810049 ) self._read_endian( - ['\xff\xff\xff\xff', '\xff\xff\xff\xff'], + [b'\xff\xff\xff\xff', b'\xff\xff\xff\xff'], 'read_ulong', (), - 4294967295L + 4294967295 ) def test_write_long(self): @@ -421,25 +421,25 @@ def test_write_long(self): x, x.write_long, (0,), - ('\x00\x00\x00\x00', '\x00\x00\x00\x00') + (b'\x00\x00\x00\x00', b'\x00\x00\x00\x00') ) self._write_endian( x, x.write_long, (16810049,), - ('\x01\x00\x80A', 'A\x80\x00\x01') + (b'\x01\x00\x80A', b'A\x80\x00\x01') ) self._write_endian( x, x.write_long, - (2147483647L,), - ('\x7f\xff\xff\xff', '\xff\xff\xff\x7f') + (2147483647,), + (b'\x7f\xff\xff\xff', b'\xff\xff\xff\x7f') ) self._write_endian( x, x.write_long, (-2147483648,), - ('\x80\x00\x00\x00', '\x00\x00\x00\x80') + (b'\x80\x00\x00\x00', b'\x00\x00\x00\x80') ) self.assertRaises(OverflowError, x.write_long, 2147483648) @@ -448,28 +448,28 @@ def test_write_long(self): def test_read_long(self): self._read_endian( - ['\xff\xff\xcf\xc7', '\xc7\xcf\xff\xff'], + [b'\xff\xff\xcf\xc7', b'\xc7\xcf\xff\xff'], 'read_long', (), -12345 ) self._read_endian( - ['\x00\x00\x00\x00', '\x00\x00\x00\x00'], + [b'\x00\x00\x00\x00', b'\x00\x00\x00\x00'], 'read_long', (), 0 ) self._read_endian( - ['\x01\x00\x80A', 'A\x80\x00\x01'], + [b'\x01\x00\x80A', b'A\x80\x00\x01'], 'read_long', (), 16810049 ) self._read_endian( - ['\x7f\xff\xff\xff', '\xff\xff\xff\x7f'], + [b'\x7f\xff\xff\xff', b'\xff\xff\xff\x7f'], 'read_long', (), - 2147483647L + 2147483647 ) def test_write_u24bit(self): @@ -479,19 +479,19 @@ def test_write_u24bit(self): x, x.write_24bit_uint, (0,), - ('\x00\x00\x00', '\x00\x00\x00') + (b'\x00\x00\x00', b'\x00\x00\x00') ) self._write_endian( x, x.write_24bit_uint, (4292609,), - ('A\x80\x01', '\x01\x80A') + (b'A\x80\x01', b'\x01\x80A') ) self._write_endian( x, x.write_24bit_uint, (16777215,), - ('\xff\xff\xff', '\xff\xff\xff') + (b'\xff\xff\xff', b'\xff\xff\xff') ) self.assertRaises(OverflowError, x.write_24bit_uint, 16777216) @@ -500,38 +500,38 @@ def test_write_u24bit(self): def test_read_u24bit(self): self._read_endian( - ['\x00\x00\x00', '\x00\x00\x00'], 'read_24bit_uint', (), 0 + [b'\x00\x00\x00', b'\x00\x00\x00'], 'read_24bit_uint', (), 0 ) self._read_endian( - ['\x00\x00\x80', '\x80\x00\x00'], 'read_24bit_uint', (), 128 + [b'\x00\x00\x80', b'\x80\x00\x00'], 'read_24bit_uint', (), 128 ) self._read_endian( - ['\x80\x00\x00', '\x00\x00\x80'], 'read_24bit_uint', (), 8388608 + [b'\x80\x00\x00', b'\x00\x00\x80'], 'read_24bit_uint', (), 8388608 ) self._read_endian( - ['\xff\xff\x7f', '\x7f\xff\xff'], 'read_24bit_uint', (), 16777087 + [b'\xff\xff\x7f', b'\x7f\xff\xff'], 'read_24bit_uint', (), 16777087 ) self._read_endian( - ['\x7f\xff\xff', '\xff\xff\x7f'], 'read_24bit_uint', (), 8388607 + [b'\x7f\xff\xff', b'\xff\xff\x7f'], 'read_24bit_uint', (), 8388607 ) def test_write_24bit(self): x = util.BufferedByteStream() self._write_endian( - x, x.write_24bit_int, (0,), ('\x00\x00\x00', '\x00\x00\x00') + x, x.write_24bit_int, (0,), (b'\x00\x00\x00', b'\x00\x00\x00') ) self._write_endian( - x, x.write_24bit_int, (128,), ('\x00\x00\x80', '\x80\x00\x00') + x, x.write_24bit_int, (128,), (b'\x00\x00\x80', b'\x80\x00\x00') ) self._write_endian( - x, x.write_24bit_int, (8388607,), ('\x7f\xff\xff', '\xff\xff\x7f') + x, x.write_24bit_int, (8388607,), (b'\x7f\xff\xff', b'\xff\xff\x7f') ) self._write_endian( - x, x.write_24bit_int, (-1,), ('\xff\xff\xff', '\xff\xff\xff') + x, x.write_24bit_int, (-1,), (b'\xff\xff\xff', b'\xff\xff\xff') ) self._write_endian( - x, x.write_24bit_int, (-8388608,), ('\x80\x00\x00', '\x00\x00\x80') + x, x.write_24bit_int, (-8388608,), (b'\x80\x00\x00', b'\x00\x00\x80') ) self.assertRaises(OverflowError, x.write_24bit_int, 8388608) @@ -540,32 +540,32 @@ def test_write_24bit(self): def test_read_24bit(self): self._read_endian( - ['\x00\x00\x00', '\x00\x00\x00'], 'read_24bit_int', (), 0 + [b'\x00\x00\x00', b'\x00\x00\x00'], 'read_24bit_int', (), 0 ) self._read_endian( - ['\x00\x00\x80', '\x80\x00\x00'], 'read_24bit_int', (), 128 + [b'\x00\x00\x80', b'\x80\x00\x00'], 'read_24bit_int', (), 128 ) self._read_endian( - ['\x80\x00\x00', '\x00\x00\x80'], 'read_24bit_int', (), -8388608 + [b'\x80\x00\x00', b'\x00\x00\x80'], 'read_24bit_int', (), -8388608 ) self._read_endian( - ['\xff\xff\x7f', '\x7f\xff\xff'], 'read_24bit_int', (), -129 + [b'\xff\xff\x7f', b'\x7f\xff\xff'], 'read_24bit_int', (), -129 ) self._read_endian( - ['\x7f\xff\xff', '\xff\xff\x7f'], 'read_24bit_int', (), 8388607 + [b'\x7f\xff\xff', b'\xff\xff\x7f'], 'read_24bit_int', (), 8388607 ) def test_write_float(self): x = util.BufferedByteStream() self._write_endian( - x, x.write_float, (0.2,), ('>L\xcc\xcd', '\xcd\xccL>') + x, x.write_float, (0.2,), (b'>L\xcc\xcd', b'\xcd\xccL>') ) self.assertRaises(TypeError, x.write_float, 'foo') def test_read_float(self): self._read_endian( - ['?\x00\x00\x00', '\x00\x00\x00?'], 'read_float', (), 0.5 + [b'?\x00\x00\x00', b'\x00\x00\x00?'], 'read_float', (), 0.5 ) def test_write_double(self): @@ -575,13 +575,13 @@ def test_write_double(self): x, x.write_double, (0.2,), - ('?\xc9\x99\x99\x99\x99\x99\x9a', '\x9a\x99\x99\x99\x99\x99\xc9?') + (b'?\xc9\x99\x99\x99\x99\x99\x9a', b'\x9a\x99\x99\x99\x99\x99\xc9?') ) self.assertRaises(TypeError, x.write_double, 'foo') def test_read_double(self): self._read_endian( - ['?\xc9\x99\x99\x99\x99\x99\x9a', '\x9a\x99\x99\x99\x99\x99\xc9?'], + [b'?\xc9\x99\x99\x99\x99\x99\x9a', b'\x9a\x99\x99\x99\x99\x99\xc9?'], 'read_double', (), 0.2 @@ -594,7 +594,7 @@ def test_write_utf8_string(self): x, x.write_utf8_string, (u'ᚠᛇᚻ',), - ['\xe1\x9a\xa0\xe1\x9b\x87\xe1\x9a\xbb'] * 2 + [b'\xe1\x9a\xa0\xe1\x9b\x87\xe1\x9a\xbb'] * 2 ) self.assertRaises(TypeError, x.write_utf8_string, 1) self.assertRaises(TypeError, x.write_utf8_string, 1.0) @@ -603,32 +603,32 @@ def test_write_utf8_string(self): def test_read_utf8_string(self): self._read_endian( - ['\xe1\x9a\xa0\xe1\x9b\x87\xe1\x9a\xbb'] * 2, + [b'\xe1\x9a\xa0\xe1\x9b\x87\xe1\x9a\xbb'] * 2, 'read_utf8_string', (9,), u'ᚠᛇᚻ' ) def test_nan(self): - x = util.BufferedByteStream('\xff\xf8\x00\x00\x00\x00\x00\x00') + x = util.BufferedByteStream(b'\xff\xf8\x00\x00\x00\x00\x00\x00') self.assertTrue(isNaN(x.read_double())) - x = util.BufferedByteStream('\xff\xf0\x00\x00\x00\x00\x00\x00') + x = util.BufferedByteStream(b'\xff\xf0\x00\x00\x00\x00\x00\x00') self.assertTrue(isNegInf(x.read_double())) - x = util.BufferedByteStream('\x7f\xf0\x00\x00\x00\x00\x00\x00') + x = util.BufferedByteStream(b'\x7f\xf0\x00\x00\x00\x00\x00\x00') self.assertTrue(isPosInf(x.read_double())) # now test little endian - x = util.BufferedByteStream('\x00\x00\x00\x00\x00\x00\xf8\xff') + x = util.BufferedByteStream(b'\x00\x00\x00\x00\x00\x00\xf8\xff') x.endian = '<' self.assertTrue(isNaN(x.read_double())) - x = util.BufferedByteStream('\x00\x00\x00\x00\x00\x00\xf0\xff') + x = util.BufferedByteStream(b'\x00\x00\x00\x00\x00\x00\xf0\xff') x.endian = '<' self.assertTrue(isNegInf(x.read_double())) - x = util.BufferedByteStream('\x00\x00\x00\x00\x00\x00\xf0\x7f') + x = util.BufferedByteStream(b'\x00\x00\x00\x00\x00\x00\xf0\x7f') x.endian = '<' self.assertTrue(isPosInf(x.read_double())) @@ -636,18 +636,18 @@ def test_write_infinites(self): x = util.BufferedByteStream() self._write_endian(x, x.write_double, (NaN,), ( - '\xff\xf8\x00\x00\x00\x00\x00\x00', - '\x00\x00\x00\x00\x00\x00\xf8\xff' + b'\xff\xf8\x00\x00\x00\x00\x00\x00', + b'\x00\x00\x00\x00\x00\x00\xf8\xff' )) self._write_endian(x, x.write_double, (PosInf,), ( - '\x7f\xf0\x00\x00\x00\x00\x00\x00', - '\x00\x00\x00\x00\x00\x00\xf0\x7f' + b'\x7f\xf0\x00\x00\x00\x00\x00\x00', + b'\x00\x00\x00\x00\x00\x00\xf0\x7f' )) self._write_endian(x, x.write_double, (NegInf,), ( - '\xff\xf0\x00\x00\x00\x00\x00\x00', - '\x00\x00\x00\x00\x00\x00\xf0\xff' + b'\xff\xf0\x00\x00\x00\x00\x00\x00', + b'\x00\x00\x00\x00\x00\x00\xf0\xff' )) @@ -659,12 +659,12 @@ class BufferedByteStreamTestCase(unittest.TestCase): def test_create(self): x = util.BufferedByteStream() - self.assertEqual(x.getvalue(), '') + self.assertEqual(x.getvalue(), b'') self.assertEqual(x.tell(), 0) x = util.BufferedByteStream('abc') - self.assertEqual(x.getvalue(), 'abc') + self.assertEqual(x.getvalue(), b'abc') self.assertEqual(x.tell(), 0) def test_read(self): @@ -679,7 +679,7 @@ def test_read(self): x.write('hello') x.seek(0) self.assertRaises(IOError, x.read, 10) - self.assertEqual(x.read(), 'hello') + self.assertEqual(x.read(), b'hello') def test_read_negative(self): """ @@ -696,12 +696,12 @@ def test_peek(self): self.assertEqual(x.tell(), 0) - self.assertEqual(x.peek(), 'a') - self.assertEqual(x.peek(5), 'abcde') - self.assertEqual(x.peek(-1), 'abcdefghijklmnopqrstuvwxyz') + self.assertEqual(x.peek(), b'a') + self.assertEqual(x.peek(5), b'abcde') + self.assertEqual(x.peek(-1), b'abcdefghijklmnopqrstuvwxyz') x.seek(10) - self.assertEqual(x.peek(50), 'klmnopqrstuvwxyz') + self.assertEqual(x.peek(50), b'klmnopqrstuvwxyz') def test_eof(self): x = util.BufferedByteStream() @@ -730,12 +730,12 @@ def test_add(self): c = a + b self.assertTrue(isinstance(c, util.BufferedByteStream)) - self.assertEqual(c.getvalue(), 'ab') + self.assertEqual(c.getvalue(), b'ab') self.assertEqual(c.tell(), 0) def test_add_pos(self): - a = util.BufferedByteStream('abc') - b = util.BufferedByteStream('def') + a = util.BufferedByteStream(b'abc') + b = util.BufferedByteStream(b'def') a.seek(1) b.seek(0, 2) @@ -752,7 +752,7 @@ def test_append_types(self): self.assertRaises(TypeError, a.append, 234234) self.assertRaises(TypeError, a.append, 234.0) - self.assertRaises(TypeError, a.append, 234234L) + self.assertRaises(TypeError, a.append, 234234) self.assertRaises(TypeError, a.append, []) self.assertRaises(TypeError, a.append, {}) self.assertRaises(TypeError, a.append, lambda _: None) @@ -766,13 +766,13 @@ def test_append_string(self): # test empty a = util.BufferedByteStream() - self.assertEqual(a.getvalue(), '') + self.assertEqual(a.getvalue(), b'') self.assertEqual(a.tell(), 0) self.assertEqual(len(a), 0) a.append('foo') - self.assertEqual(a.getvalue(), 'foo') + self.assertEqual(a.getvalue(), b'foo') self.assertEqual(a.tell(), 0) # <-- pointer hasn't moved self.assertEqual(len(a), 3) @@ -780,13 +780,13 @@ def test_append_string(self): a = util.BufferedByteStream('bar') - self.assertEqual(a.getvalue(), 'bar') + self.assertEqual(a.getvalue(), b'bar') self.assertEqual(a.tell(), 0) self.assertEqual(len(a), 3) a.append('gak') - self.assertEqual(a.getvalue(), 'bargak') + self.assertEqual(a.getvalue(), b'bargak') self.assertEqual(a.tell(), 0) # <-- pointer hasn't moved self.assertEqual(len(a), 6) @@ -795,13 +795,13 @@ def test_append_string(self): a = util.BufferedByteStream('bar') a.seek(2) - self.assertEqual(a.getvalue(), 'bar') + self.assertEqual(a.getvalue(), b'bar') self.assertEqual(a.tell(), 2) self.assertEqual(len(a), 3) a.append('gak') - self.assertEqual(a.getvalue(), 'bargak') + self.assertEqual(a.getvalue(), b'bargak') self.assertEqual(a.tell(), 2) # <-- pointer hasn't moved self.assertEqual(len(a), 6) @@ -810,32 +810,32 @@ def test_append_string(self): a = util.BufferedByteStream('bar') a.seek(0, 2) - self.assertEqual(a.getvalue(), 'bar') + self.assertEqual(a.getvalue(), b'bar') self.assertEqual(a.tell(), 3) self.assertEqual(len(a), 3) a.append('gak') - self.assertEqual(a.getvalue(), 'bargak') + self.assertEqual(a.getvalue(), b'bargak') self.assertEqual(a.tell(), 3) # <-- pointer hasn't moved self.assertEqual(len(a), 6) class Foo(object): def getvalue(self): - return 'foo' + return b'foo' def __str__(self): raise AttributeError() a = util.BufferedByteStream() - self.assertEqual(a.getvalue(), '') + self.assertEqual(a.getvalue(), b'') self.assertEqual(a.tell(), 0) self.assertEqual(len(a), 0) a.append(Foo()) - self.assertEqual(a.getvalue(), 'foo') + self.assertEqual(a.getvalue(), b'foo') self.assertEqual(a.tell(), 0) self.assertEqual(len(a), 3) @@ -846,13 +846,13 @@ def test_append_unicode(self): # test empty a = util.BufferedByteStream() - self.assertEqual(a.getvalue(), '') + self.assertEqual(a.getvalue(), b'') self.assertEqual(a.tell(), 0) self.assertEqual(len(a), 0) - a.append(u'foo') + a.append('foo') - self.assertEqual(a.getvalue(), 'foo') + self.assertEqual(a.getvalue(), b'foo') self.assertEqual(a.tell(), 0) # <-- pointer hasn't moved self.assertEqual(len(a), 3) @@ -860,13 +860,13 @@ def test_append_unicode(self): a = util.BufferedByteStream('bar') - self.assertEqual(a.getvalue(), 'bar') + self.assertEqual(a.getvalue(), b'bar') self.assertEqual(a.tell(), 0) self.assertEqual(len(a), 3) - a.append(u'gak') + a.append('gak') - self.assertEqual(a.getvalue(), 'bargak') + self.assertEqual(a.getvalue(), b'bargak') self.assertEqual(a.tell(), 0) # <-- pointer hasn't moved self.assertEqual(len(a), 6) @@ -875,13 +875,13 @@ def test_append_unicode(self): a = util.BufferedByteStream('bar') a.seek(2) - self.assertEqual(a.getvalue(), 'bar') + self.assertEqual(a.getvalue(), b'bar') self.assertEqual(a.tell(), 2) self.assertEqual(len(a), 3) - a.append(u'gak') + a.append('gak') - self.assertEqual(a.getvalue(), 'bargak') + self.assertEqual(a.getvalue(), b'bargak') self.assertEqual(a.tell(), 2) # <-- pointer hasn't moved self.assertEqual(len(a), 6) @@ -890,13 +890,13 @@ def test_append_unicode(self): a = util.BufferedByteStream('bar') a.seek(0, 2) - self.assertEqual(a.getvalue(), 'bar') + self.assertEqual(a.getvalue(), b'bar') self.assertEqual(a.tell(), 3) self.assertEqual(len(a), 3) - a.append(u'gak') + a.append('gak') - self.assertEqual(a.getvalue(), 'bargak') + self.assertEqual(a.getvalue(), b'bargak') self.assertEqual(a.tell(), 3) # <-- pointer hasn't moved self.assertEqual(len(a), 6) @@ -909,13 +909,13 @@ def __str__(self): a = util.BufferedByteStream() - self.assertEqual(a.getvalue(), '') + self.assertEqual(a.getvalue(), b'') self.assertEqual(a.tell(), 0) self.assertEqual(len(a), 0) a.append(Foo()) - self.assertEqual(a.getvalue(), 'foo') + self.assertEqual(a.getvalue(), b'foo') self.assertEqual(a.tell(), 0) self.assertEqual(len(a), 3) @@ -1040,7 +1040,7 @@ class A: class B(object): pass - for t in ['', u'', 1, 1.0, 1L, [], {}, object, object(), A(), B()]: + for t in ['', u'', 1, 1.0, 1, [], {}, object, object(), A(), B()]: self.assertRaises(TypeError, util.get_class_meta, t) def test_no_meta(self): diff --git a/pyamf/tests/test_xml.py b/pyamf/tests/test_xml.py index cfc143d4..c9845754 100644 --- a/pyamf/tests/test_xml.py +++ b/pyamf/tests/test_xml.py @@ -145,7 +145,7 @@ def test_dtd(self): writer to write the test once and ensure that it works for all the supported xml modules. """ -for name, value in globals().copy().iteritems(): +for name, value in list(globals().copy().items()): try: is_subclass = issubclass(value, _BaseTestCase) except TypeError: diff --git a/pyamf/util/__init__.py b/pyamf/util/__init__.py index 035e4aa3..a346f704 100644 --- a/pyamf/util/__init__.py +++ b/pyamf/util/__init__.py @@ -99,14 +99,14 @@ def set_attrs(obj, attrs): if hasattr(obj, '__setitem__'): o = type(obj).__setitem__ - [o(obj, k, v) for k, v in attrs.iteritems()] + [o(obj, k, v) for k, v in attrs.items()] def get_class_alias(klass): """ Tries to find a suitable L{pyamf.ClassAlias} subclass for C{klass}. """ - for k, v in pyamf.ALIAS_TYPES.iteritems(): + for k, v in list(pyamf.ALIAS_TYPES.items()): for kl in v: try: if issubclass(klass, kl): @@ -198,6 +198,9 @@ def get_module(mod_name): """ Load and return a module based on C{mod_name}. """ + if isinstance(mod_name, bytes): + mod_name = mod_name.decode() + if mod_name is '': raise ImportError('Unable to import empty module') diff --git a/pyamf/util/pure.py b/pyamf/util/pure.py index 07161a52..5da058a9 100644 --- a/pyamf/util/pure.py +++ b/pyamf/util/pure.py @@ -14,9 +14,9 @@ import struct try: - from cStringIO import StringIO + from cStringIO import StringIO as BytesIO except ImportError: - from StringIO import StringIO + from io import BytesIO from pyamf import python @@ -24,10 +24,9 @@ SYSTEM_ENDIAN = None -class StringIOProxy(object): +class BytesIOProxy(object): """ - I am a C{StringIO} type object containing byte data from the AMF stream. - + I am a C{BytesIO} type object containing byte data from the AMF stream. @see: U{ByteArray on OSFlash } @see: U{Parsing ByteArrays on OSFlash @@ -36,24 +35,26 @@ class StringIOProxy(object): def __init__(self, buf=None): """ - @raise TypeError: Unable to coerce C{buf} to C{StringIO}. + @raise TypeError: Unable to coerce C{buf} to C{BytesIO}. """ - self._buffer = StringIO() + self._buffer = BytesIO() - if isinstance(buf, python.str_types): + if isinstance(buf, bytes): self._buffer.write(buf) + elif isinstance(buf, str): + self._buffer.write(buf.encode('utf-8')) elif hasattr(buf, 'getvalue'): self._buffer.write(buf.getvalue()) elif ( - hasattr(buf, 'read') and - hasattr(buf, 'seek') and - hasattr(buf, 'tell')): + hasattr(buf, 'read') and + hasattr(buf, 'seek') and + hasattr(buf, 'tell')): old_pos = buf.tell() buf.seek(0) self._buffer.write(buf.read()) buf.seek(old_pos) elif buf is not None: - raise TypeError("Unable to coerce buf->StringIO got %r" % (buf,)) + raise TypeError("Unable to coerce buf->BytesIO got %r" % (buf,)) self._get_len() self._len_changed = False @@ -80,7 +81,6 @@ def seek(self, pos, mode=0): """ Sets the file-pointer offset, measured from the beginning of this stream, at which the next write operation will occur. - @param pos: @type pos: C{int} @param mode: @@ -97,12 +97,11 @@ def tell(self): def truncate(self, size=0): """ Truncates the stream to the specified length. - @param size: The length of the stream, in bytes. @type size: C{int} """ if size == 0: - self._buffer = StringIO() + self._buffer = BytesIO() self._len_changed = True return @@ -110,7 +109,7 @@ def truncate(self, size=0): cur_pos = self.tell() self.seek(0) buf = self.read(size) - self._buffer = StringIO() + self._buffer = BytesIO() self._buffer.write(buf) self.seek(cur_pos) @@ -119,7 +118,6 @@ def truncate(self, size=0): def write(self, s, size=None): """ Writes the content of the specified C{s} into this buffer. - @param s: Raw bytes """ self._buffer.write(s) @@ -153,7 +151,6 @@ def consume(self): """ Chops the tail off the stream starting at 0 and ending at C{tell()}. The stream pointer is set to 0 at the end of this function. - @since: 0.4 """ try: @@ -172,7 +169,6 @@ class DataTypeMixIn(object): """ Provides methods for reading and writing basic data types for file-like objects. - @ivar endian: Byte ordering used to represent the data. Default byte order is L{ENDIAN_NETWORK}. @type endian: C{str} @@ -227,7 +223,6 @@ def read_uchar(self): def write_uchar(self, c): """ Writes an C{unsigned char} to the stream. - @param c: Unsigned char @type c: C{int} @raise TypeError: Unexpected type for int C{c}. @@ -250,7 +245,6 @@ def read_char(self): def write_char(self, c): """ Write a C{char} to the stream. - @param c: char @type c: C{int} @raise TypeError: Unexpected type for int C{c}. @@ -273,7 +267,6 @@ def read_ushort(self): def write_ushort(self, s): """ Writes a 2 byte unsigned integer to the stream. - @param s: 2 byte unsigned integer @type s: C{int} @raise TypeError: Unexpected type for int C{s}. @@ -296,7 +289,6 @@ def read_short(self): def write_short(self, s): """ Writes a 2 byte integer to the stream. - @param s: 2 byte integer @type s: C{int} @raise TypeError: Unexpected type for int C{s}. @@ -319,7 +311,6 @@ def read_ulong(self): def write_ulong(self, l): """ Writes a 4 byte unsigned integer to the stream. - @param l: 4 byte unsigned integer @type l: C{int} @raise TypeError: Unexpected type for int C{l}. @@ -342,7 +333,6 @@ def read_long(self): def write_long(self, l): """ Writes a 4 byte integer to the stream. - @param l: 4 byte integer @type l: C{int} @raise TypeError: Unexpected type for int C{l}. @@ -359,7 +349,6 @@ def write_long(self, l): def read_24bit_uint(self): """ Reads a 24 bit unsigned integer from the stream. - @since: 0.4 """ order = None @@ -379,7 +368,6 @@ def read_24bit_uint(self): def write_24bit_uint(self, n): """ Writes a 24 bit unsigned integer to the stream. - @since: 0.4 @param n: 24 bit unsigned integer @type n: C{int} @@ -405,7 +393,6 @@ def write_24bit_uint(self, n): def read_24bit_int(self): """ Reads a 24 bit integer from the stream. - @since: 0.4 """ n = self.read_24bit_uint() @@ -419,7 +406,6 @@ def read_24bit_int(self): def write_24bit_int(self, n): """ Writes a 24 bit integer to the stream. - @since: 0.4 @param n: 24 bit integer @type n: C{int} @@ -454,7 +440,6 @@ def read_double(self): def write_double(self, d): """ Writes an 8 byte float to the stream. - @param d: 8 byte float @type d: C{float} @raise TypeError: Unexpected type for float C{d}. @@ -473,7 +458,6 @@ def read_float(self): def write_float(self, f): """ Writes a 4 byte float to the stream. - @param f: 4 byte float @type f: C{float} @raise TypeError: Unexpected type for float C{f}. @@ -486,20 +470,18 @@ def write_float(self, f): def read_utf8_string(self, length): """ Reads a UTF-8 string from the stream. - @rtype: C{unicode} """ s = struct.unpack("%s%ds" % ( self.endian, length), - self.read(length) - ) + self.read(length) + ) return s[0].decode('utf-8') def write_utf8_string(self, u): """ Writes a unicode object to the stream in UTF-8. - @param u: unicode object @raise TypeError: Unexpected type for str C{u}. """ @@ -508,13 +490,13 @@ def write_utf8_string(self, u): bytes = u - if isinstance(bytes, unicode): + if isinstance(bytes, str): bytes = u.encode("utf8") self.write(struct.pack("%s%ds" % (self.endian, len(bytes)), bytes)) -class BufferedByteStream(StringIOProxy, DataTypeMixIn): +class BufferedByteStream(BytesIOProxy, DataTypeMixIn): """ An extension of C{StringIO}. @@ -529,7 +511,7 @@ def __init__(self, buf=None, min_buf_size=None): @type buf: C{str} or C{StringIO} instance @param min_buf_size: Ignored in the pure Python version. """ - StringIOProxy.__init__(self, buf=buf) + BytesIOProxy.__init__(self, buf=buf) def read(self, length=-1): """ @@ -547,7 +529,7 @@ def read(self, length=-1): 'remain' % (length, len(self) - self.tell()) ) - return StringIOProxy.read(self, length) + return BytesIOProxy.read(self, length) def peek(self, size=1): """ @@ -566,7 +548,7 @@ def peek(self, size=1): if size < -1: raise ValueError("Cannot peek backwards") - bytes = '' + bytes = b'' pos = self.tell() while not self.at_eof() and len(bytes) != size: @@ -642,7 +624,7 @@ def is_float_broken(): @return: Boolean indicating whether floats are broken on this platform. """ return str(python.NaN) != str( - struct.unpack("!d", '\xff\xf8\x00\x00\x00\x00\x00\x00')[0]) + struct.unpack("!d", b'\xff\xf8\x00\x00\x00\x00\x00\x00')[0]) # init the module from here .. diff --git a/setup.py b/setup.py index 6dd0a1db..fddcb9da 100644 --- a/setup.py +++ b/setup.py @@ -13,9 +13,9 @@ name = "PyAMF" description = "AMF support for Python" long_description = setupinfo.read('README.md') -url = "http://pyamf.org" -author = "The PyAMF Project" -author_email = "users@pyamf.org" +url = "https://github.com/StdCarrot/Py3AMF" +author = "The Py3AMF Project" +author_email = "yhbu@stdc.so" license = "MIT License" classifiers = """ @@ -30,16 +30,14 @@ Programming Language :: C Programming Language :: Python Programming Language :: Cython -Programming Language :: Python :: 2.4 -Programming Language :: Python :: 2.5 -Programming Language :: Python :: 2.6 -Programming Language :: Python :: 2.7 +Programming Language :: Python :: 3.5 +Programming Language :: Python :: 3.6 Topic :: Internet :: WWW/HTTP :: WSGI :: Application Topic :: Software Development :: Libraries :: Python Modules """ keywords = """ -amf amf0 amf3 flex flash remoting rpc http flashplayer air bytearray +python3 amf amf0 amf3 flex flash remoting rpc http flashplayer air bytearray objectproxy arraycollection recordset actionscript decoder encoder gateway remoteobject twisted pylons django sharedobject lso sol """ diff --git a/setupinfo.py b/setupinfo.py index 9e84a56a..732c415d 100644 --- a/setupinfo.py +++ b/setupinfo.py @@ -140,6 +140,7 @@ def get_extras_require(): 'sqlalchemy': ['SQLAlchemy>=0.4'], 'elixir': ['Elixir>=0.7.1'], 'lxml': ['lxml>=2.2'], + 'six': ['six>=1.10.0'] }