Changeset 1110

Show
Ignore:
Timestamp:
03/06/08 19:04:49 (10 months ago)
Author:
nick
Message:

Adding in the working pyx files absolutely no optimisation, just got it working and only a couple of tests failsvn status Unbelievable

Location:
pyamf/branches/cpyamf-225/pyamf
Files:
2 copied

Legend:

Unmodified
Added
Removed
  • pyamf/branches/cpyamf-225/pyamf/amf0.pyx

    r1101 r1110  
    44# See LICENSE for details. 
    55 
    6 """ 
    7 AMF0 implementation. 
    8  
    9 C{AMF0} supports the basic data types used for the NetConnection, NetStream, 
    10 LocalConnection, SharedObjects and other classes in the Flash Player. 
    11  
    12 @see: U{AMF documentation on OSFlash (external) 
    13 <http://osflash.org/documentation/amf>} 
    14  
    15 @author: U{Arnar Birgisson<mailto:arnarbi@gmail.com>} 
    16 @author: U{Thijs Triemstra<mailto:info@collab.nl>} 
    17 @author: U{Nick Joyce<mailto:nick@boxdesign.co.uk>} 
    18  
    19 @since: 0.1.0 
    20 """ 
    21  
    226import datetime, types 
    237 
     
    3216    <http://osflash.org/documentation/amf/astypes>} 
    3317    """ 
    34     #: Represented as 9 bytes: 1 byte for C{0×00} and 8 bytes a double 
    35     #: representing the value of the number. 
    3618    NUMBER      = 0x00 
    37     #: Represented as 2 bytes: 1 byte for C{0×01} and a second, C{0×00} 
    38     #: for C{False}, C{0×01} for C{True}. 
    3919    BOOL        = 0x01 
    40     #: Represented as 3 bytes + len(String): 1 byte C{0×02}, then a UTF8 string, 
    41     #: including the top two bytes representing string length as a C{int}. 
    4220    STRING      = 0x02 
    43     #: Represented as 1 byte, C{0×03}, then pairs of UTF8 string, the key, and 
    44     #: an AMF element, ended by three bytes, C{0×00} C{0×00} C{0×09}. 
    4521    OBJECT      = 0x03 
    46     #: MovieClip does not seem to be supported by Remoting. 
    47     #: It may be used by other AMF clients such as SharedObjects. 
    4822    MOVIECLIP   = 0x04 
    49     #: 1 single byte, C{0×05} indicates null. 
    50     NULL        = 0x05 
    51     #: 1 single byte, C{0×06} indicates null. 
     23    _NULL        = 0x05 
    5224    UNDEFINED   = 0x06 
    53     #: When an ActionScript object refers to itself, such C{this.self = this}, 
    54     #: or when objects are repeated within the same scope (for example, as the 
    55     #: two parameters of the same function called), a code of C{0×07} and an 
    56     #: C{int}, the reference number, are written. 
    5725    REFERENCE   = 0x07 
    58     #: A MixedArray is indicated by code C{0×08}, then a Long representing the 
    59     #: highest numeric index in the array, or 0 if there are none or they are 
    60     #: all negative. After that follow the elements in key : value pairs. 
    6126    MIXEDARRAY  = 0x08 
    62     #: @see: L{OBJECT} 
    6327    OBJECTTERM  = 0x09 
    64     #: An array is indicated by C{0x0A}, then a Long for array length, then the 
    65     #: array elements themselves. Arrays are always sparse; values for 
    66     #: inexistant keys are set to null (C{0×06}) to maintain sparsity. 
    6728    ARRAY       = 0x0a 
    68     #: Date is represented as C{00x0B}, then a double, then an C{int}. The double 
    69     #: represents the number of milliseconds since 01/01/1970. The C{int} represents 
    70     #: the timezone offset in minutes between GMT. Note for the latter than values 
    71     #: greater than 720 (12 hours) are represented as M{2^16} - the value. Thus GMT+1 
    72     #: is 60 while GMT-5 is 65236. 
    7329    DATE        = 0x0b 
    74     #: LongString is reserved for strings larger then M{2^16} characters long. It 
    75     #: is represented as C{00x0C} then a LongUTF. 
    7630    LONGSTRING  = 0x0c 
    77     #: Trying to send values which don’t make sense, such as prototypes, functions, 
    78     #: built-in objects, etc. will be indicated by a single C{00x0D} byte. 
    7931    UNSUPPORTED = 0x0d 
    80     #: Remoting Server -> Client only. 
    81     #: @see: L{RecordSet} 
    82     #: @see: U{RecordSet structure on OSFlash (external) 
    83     #: <http://osflash.org/documentation/amf/recordset>} 
    8432    RECORDSET   = 0x0e 
    85     #: The XML element is indicated by C{00x0F} and followed by a LongUTF containing 
    86     #: the string representation of the XML object. The receiving gateway may which 
    87     #: to wrap this string inside a language-specific standard XML object, or simply 
    88     #: pass as a string. 
    8933    XML         = 0x0f 
    90     #: A typed object is indicated by C{0×10}, then a UTF string indicating class 
    91     #: name, and then the same structure as a normal C{0×03} Object. The receiving 
    92     #: gateway may use a mapping scheme, or send back as a vanilla object or 
    93     #: associative array. 
    9434    TYPEDOBJECT = 0x10 
    95     #: An AMF message sent from an AVM+ client such as the Flash Player 9 may break 
    96     #: out into L{AMF3<pyamf.amf3>} mode. In this case the next byte will be the 
    97     #: AMF3 type code and the data will be in AMF3 format until the decoded object 
    98     #: reaches it’s logical conclusion (for example, an object has no more keys). 
    9935    AMF3        = 0x11 
    10036 
    101 #: List of available ActionScript types in AMF0. 
    10237ACTIONSCRIPT_TYPES = [] 
    10338 
     
    177112 
    178113    context_class = Context 
    179     # XXX nick: Do we need to support ASTypes.MOVIECLIP here? 
    180114    type_map = { 
    181115        ASTypes.NUMBER:     'readNumber', 
     
    183117        ASTypes.STRING:     'readString', 
    184118        ASTypes.OBJECT:     'readObject', 
    185         ASTypes.NULL:       'readNull', 
     119        ASTypes._NULL:       'readNull', 
    186120        ASTypes.UNDEFINED:  'readUndefined', 
    187121        ASTypes.REFERENCE:  'readReference', 
     
    190124        ASTypes.DATE:       'readDate', 
    191125        ASTypes.LONGSTRING: 'readLongString', 
    192         # TODO: do we need a special value here? 
    193126        ASTypes.UNSUPPORTED:'readNull', 
    194127        ASTypes.XML:        'readXML', 
     
    369302                return 
    370303 
    371             for key in filter(lambda x: x in attrs, obj_attrs.keys()): 
     304            for key in obj_attrs.keys(): 
     305                if not key in attrs: 
     306                    continue 
     307 
    372308                obj.__setattr__(key, obj_attrs[key]) 
    373309        else: 
     
    460396        ((datetime.date, datetime.datetime), "writeDate"), 
    461397        ((util.ET.iselement,), "writeXML"), 
    462         ((lambda x: x is pyamf.Undefined,), "writeUndefined"), 
     398        #((lambda x: x is pyamf.Undefined,), "writeUndefined"), 
    463399        ((types.InstanceType,types.ObjectType,), "writeObject"), 
    464400    ] 
     
    540476        @param  n: Is ignored. 
    541477        """ 
    542         self.writeType(ASTypes.NULL) 
     478        self.writeType(ASTypes._NULL) 
    543479 
    544480    def writeArray(self, a): 
     
    659595        # TODO: optimise this 
    660596        # work out the highest integer index 
     597        max_index = 0 
    661598        try: 
    662599            # list comprehensions to save the day 
    663             max_index = max([y[0] for y in o.items() 
    664                 if isinstance(y[0], (int, long))]) 
     600            #max_index = max([y[0] for y in o.items() if isinstance(y[0], (int, long))]) 
    665601 
    666602            if max_index < 0: 
     
    790726    while 1: 
    791727        try: 
    792             yield decoder.readElement() 
    793         except pyamf.EOStream: 
    794             break 
     728            pass#yield decoder.readElement() 
     729        except pyamf.EOStream: 
     730            break 
    795731 
    796732def encode(element, context=None): 
     
    870806        ret = '<%s.%s object' % (self.__module__, self.__class__.__name__) 
    871807 
    872         if self.id is not None: 
    873             ret += ' id=%s' % self.id 
    874  
    875         if self.service is not None: 
    876             ret += ' service=%s' % self.service 
    877  
    878         ret += ' at 0x%x>' % id(self) 
     808        #if self.id is not None: 
     809        #    ret += ' id=s' % self.id 
     810 
     811        #if self.service is not None: 
     812        #    ret += ' service=%s' % self.service 
     813 
     814        #ret += ' at 0x%x>' % id(self) 
    879815 
    880816        return ret 
     
    899835 
    900836# check for some python2.3 problems with floats 
     837""" 
    901838try: 
    902839    float('nan') 
     
    916853 
    917854        _check_for_int = check_nan(_check_for_int) 
     855""" 
  • pyamf/branches/cpyamf-225/pyamf/amf3.pyx

    r1101 r1110  
    5252    #: The undefined type is represented by the undefined type marker. 
    5353    #: No further information is encoded for this value. 
    54     NULL       = 0x01 
     54    _NULL       = 0x01 
    5555    #: The false type is represented by the false type marker and is 
    5656    #: used to encode a Boolean value of C{false}. No further information 
     
    623623 
    624624        if hasattr(obj, '__getstate__'): 
    625             attrs = set([unicode(k) for k in obj.__getstate__()]) 
     625            attrs = obj.__getstate__() 
    626626        elif hasattr(obj, 'keys'): 
    627             attrs = set([unicode(k) for k in obj.keys()]) 
     627            attrs = obj.keys() 
    628628        elif hasattr(obj, 'iteritems'): 
    629             attrs = set([unicode(k) for k, v in obj.iteritems()]) 
     629            attrs = [] 
     630            for k, v in obj.iteritems(): 
     631                attrs.append(k) 
    630632        elif hasattr(obj, '__dict__'): 
    631             attrs = set([unicode(k) for k in obj.__dict__.keys()]) 
     633            attrs = obj.__dict__.keys() 
    632634 
    633635        static_attrs = dynamic_attrs = None 
     
    636638            if self.alias.attrs: 
    637639                static_attrs = self.alias.attrs 
    638                 [attrs.remove(x) for x in static_attrs] 
     640                for x in static_attrs: 
     641                    attrs.remove(x) 
    639642 
    640643            if self.alias.attr_func: 
     
    858861    type_map = { 
    859862        ASTypes.UNDEFINED:  'readUndefined', 
    860         ASTypes.NULL:       'readNull', 
     863        ASTypes._NULL:       'readNull', 
    861864        ASTypes.BOOL_FALSE: 'readBoolFalse', 
    862865        ASTypes.BOOL_TRUE:  'readBoolTrue', 
     
    938941 
    939942        while b & 0x80 != 0 and n < 3: 
    940             result <<= 7 
    941             result |= b & 0x7f 
     943            result = result << 7 
     944            result = result | (b & 0x7f) 
    942945            b = self.stream.read_uchar() 
    943             n += 1 
     946            n = n + 1 
    944947 
    945948        if n < 3: 
    946             result <<= 7 
    947             result |= b 
    948         else: 
    949             result <<= 8 
    950             result |= b 
     949            result = result << 7 
     950            result = result | b 
     951        else: 
     952            result = result << 8 
     953            result = result | b 
    951954 
    952955            if result & 0x10000000 != 0: 
    953                 result <<= 1 
    954                 result += 1 
     956                result = result << 1 
     957                result = result + 1 
    955958 
    956959        return result 
     960 
     961    def _readLength(self): 
     962        x = self.readInteger() 
     963 
     964        return (x >> 1, x & REFERENCE_BIT == 0) 
    957965 
    958966    def readString(self, use_references=True): 
     
    963971        @param use_references: 
    964972        """ 
    965         def readLength(): 
    966             x = self.readInteger() 
    967  
    968             return (x >> 1, x & REFERENCE_BIT == 0) 
    969  
    970         length, is_reference = readLength() 
     973        length, is_reference = self._readLength() 
    971974 
    972975        if use_references and is_reference: 
     
    10151018            return self.context.getObject(size >> 1) 
    10161019 
    1017         size >>= 1 
     1020        size = size >> 1 
    10181021 
    10191022        key = self.readString() 
     
    10531056        class_ref = ref & REFERENCE_BIT == 0 
    10541057 
    1055         ref >>= 1 
     1058        ref = ref >> 1 
    10561059 
    10571060        if class_ref: 
     
    10631066        return class_ref, class_def, ref >> 2 
    10641067 
     1068    def _readStatic(self, is_ref, class_def, obj, num_attrs): 
     1069        if not is_ref: 
     1070            for i in range(num_attrs): 
     1071                key = self.readString() 
     1072 
     1073                class_def.static_attrs.append(key) 
     1074 
     1075        for attr in class_def.static_attrs: 
     1076            setattr(obj, attr, self.readElement()) 
     1077 
     1078    def _readDynamic(self, is_ref, class_def, obj): 
     1079        attr = self.readString() 
     1080 
     1081        while attr != "": 
     1082            setattr(obj, attr, self.readElement()) 
     1083            attr = self.readString() 
     1084 
    10651085    def readObject(self): 
    10661086        """ 
    10671087        Reads an object from the stream. 
    10681088        """ 
    1069         def readStatic(is_ref, class_def, obj, num_attrs): 
    1070             if not is_ref: 
    1071                 for i in range(num_attrs): 
    1072                     key = self.readString() 
    1073  
    1074                     class_def.static_attrs.append(key) 
    1075  
    1076             for attr in class_def.static_attrs: 
    1077                 setattr(obj, attr, self.readElement()) 
    1078  
    1079         def readDynamic(is_ref, class_def, obj): 
    1080             attr = self.readString() 
    1081  
    1082             while attr != "": 
    1083                 setattr(obj, attr, self.readElement()) 
    1084                 attr = self.readString() 
    1085  
    10861089        ref = self.readInteger() 
    10871090 
     
    10891092            return self.context.getObject(ref >> 1) 
    10901093 
    1091         ref >>= 1 
     1094        ref = ref >> 1 
    10921095 
    10931096        class_ref, class_def, num_attrs = self._getClassDefinition(ref) 
     
    11081111            obj.__readamf__(DataInput(self)) 
    11091112        elif class_def.encoding == ObjectEncoding.DYNAMIC: 
    1110             readStatic(class_ref, class_def, obj_attrs, num_attrs) 
    1111             readDynamic(class_ref, class_def, obj_attrs) 
     1113            self._readStatic(class_ref, class_def, obj_attrs, num_attrs) 
     1114            self._readDynamic(class_ref, class_def, obj_attrs) 
    11121115        elif class_def.encoding == ObjectEncoding.STATIC: 
    1113             readStatic(class_ref, class_def, obj_attrs, num_attrs) 
     1116            self._readStatic(class_ref, class_def, obj_attrs, num_attrs) 
    11141117        else: 
    11151118            raise pyamf.DecodeError, "Unknown object encoding" 
     
    12101213        ((datetime.date, datetime.datetime), "writeDate"), 
    12111214        ((util.ET.iselement,), "writeXML"), 
    1212         ((lambda x: x is pyamf.Undefined,), "writeUndefined"), 
     1215        #((lambda x: x is pyamf.Undefined,), "writeUndefined"), 
    12131216        ((types.InstanceType, types.ObjectType,), "writeInstance"), 
    12141217    ] 
     
    12721275        @param  use_references: 
    12731276        """ 
    1274         self.writeType(ASTypes.NULL) 
     1277        self.writeType(ASTypes._NULL) 
    12751278 
    12761279    def writeBoolean(self, n, use_references=True): 
     
    15501553            self.writeObject(obj, use_references) 
    15511554 
     1555    def _writeStatic(self, obj, attrs, class_ref): 
     1556        if not class_ref: 
     1557            for attr in attrs: 
     1558                self._writeString(attr) 
     1559 
     1560        for attr in attrs: 
     1561            self.writeElement(obj[attr]) 
     1562 
    15521563    def writeObject(self, obj, use_references=True): 
    15531564        """ 
     
    15601571        @raise EncodeError: Unknown object encoding. 
    15611572        """ 
    1562         def writeStatic(obj, attrs, class_ref): 
    1563             if not class_ref: 
    1564                 [self._writeString(attr) for attr in attrs] 
    1565  
    1566             [self.writeElement(obj[attr]) for attr in attrs] 
    15671573 
    15681574        self.writeType(ASTypes.OBJECT) 
     
    15931599            if class_def.encoding != ObjectEncoding.EXTERNAL: 
    15941600                if class_def.alias and class_def.alias.attrs is not None: 
    1595                     ref += len(class_def.alias.attrs) << 4 
     1601                    ref = ref + (len(class_def.alias.attrs) << 4) 
    15961602 
    15971603            self._writeInteger(ref | class_def.encoding << 2 | REFERENCE_BIT << 1 | REFERENCE_BIT) 
     
    16161622 
    16171623            if static_attrs is not None: 
    1618                 writeStatic(obj_attrs, static_attrs, class_ref) 
     1624                self._writeStatic(obj_attrs, static_attrs, class_ref) 
    16191625 
    16201626            if class_def.encoding == ObjectEncoding.DYNAMIC and dynamic_attrs is not None: 
     
    16941700    while 1: 
    16951701        try: 
    1696             yield decoder.readElement() 
    1697         except pyamf.EOStream: 
    1698             break 
     1702            pass#yield decoder.readElement() 
     1703        except pyamf.EOStream: 
     1704            break 
    16991705 
    17001706def encode(element, context=None): 
     
    17251731    if n > 0x1fffff: 
    17261732        real_value = n 
    1727         n >>= 1 
    1728         bytes += chr(0x80 | ((n >> 21) & 0xff)) 
     1733        n = n >> 1 
     1734        bytes = bytes + (chr(0x80 | ((n >> 21) & 0xff))) 
    17291735 
    17301736    if n > 0x3fff: 
    1731         bytes += chr(0x80 | ((n >> 14) & 0xff)) 
     1737        bytes = bytes + (chr(0x80 | ((n >> 14) & 0xff))) 
    17321738 
    17331739    if n > 0x7f: 
    1734         bytes += chr(0x80 | ((n >> 7) & 0xff)) 
     1740        bytes = bytes + (chr(0x80 | ((n >> 7) & 0xff))) 
    17351741 
    17361742    if real_value is not None: 
     
    17381744 
    17391745    if n > 0x1fffff: 
    1740         bytes += chr(n & 0xff) 
     1746        bytes = bytes + chr(n & 0xff) 
    17411747    else: 
    1742         bytes += chr(n & 0x7f) 
     1748        bytes = bytes + chr(n & 0x7f) 
    17431749 
    17441750    return bytes