Staging
v0.5.1
https://github.com/python/cpython
Revision 90c3d9b99563abebbd0737c711be1c9313c1547b authored by Christian Heimes on 23 February 2008, 13:18:03 UTC, committed by Christian Heimes on 23 February 2008, 13:18:03 UTC
svn+ssh://pythondev@svn.python.org/python/trunk

........
  r60965 | eric.smith | 2008-02-22 18:43:17 +0100 (Fri, 22 Feb 2008) | 1 line

  Tests for bin() builtin.  These need to get merged into py3k, which has no tests for bin.
........
  r60968 | raymond.hettinger | 2008-02-22 20:50:06 +0100 (Fri, 22 Feb 2008) | 1 line

  Document itertools.product().
........
  r60969 | raymond.hettinger | 2008-02-23 03:20:41 +0100 (Sat, 23 Feb 2008) | 9 lines

  Improve the implementation of itertools.product()

  * Fix-up issues pointed-out by Neal Norwitz.
  * Add extensive comments.
  * The lz->result variable is now a tuple instead of a list.
  * Use fast macro getitem/setitem calls so most code is in-line.
  * Re-use the result tuple if available (modify in-place instead of copy).
........
  r60972 | raymond.hettinger | 2008-02-23 05:03:50 +0100 (Sat, 23 Feb 2008) | 1 line

  Add more comments
........
  r60973 | raymond.hettinger | 2008-02-23 11:04:15 +0100 (Sat, 23 Feb 2008) | 1 line

  Add recipe using itertools.product().
........
  r60974 | facundo.batista | 2008-02-23 13:01:13 +0100 (Sat, 23 Feb 2008) | 6 lines


  Issue 1881. Increased the stack limit from 500 to 1500. Also added
  a test for this (and because of this test you'll see in stderr a
  message that parser.c sends before raising MemoryError).
  Thanks Ralf Schmitt.
........
  r60975 | facundo.batista | 2008-02-23 13:27:17 +0100 (Sat, 23 Feb 2008) | 4 lines


  Issue 1776581. Minor corrections to smtplib, and two small tests.
  Thanks Alan McIntyre.
........
  r60976 | facundo.batista | 2008-02-23 13:46:10 +0100 (Sat, 23 Feb 2008) | 5 lines


  Issue 1781. Now ConfigParser.add_section does not let you add a
  DEFAULT section any more, because it duplicated sections with
  the rest of the machinery. Thanks Tim Lesher and Manuel Kaufmann.
........
1 parent 973a872
Raw File
Tip revision: 90c3d9b99563abebbd0737c711be1c9313c1547b authored by Christian Heimes on 23 February 2008, 13:18:03 UTC
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60569,60571-60572,60574,60576-60583,60585-60586,60589,60591,60594-60595,60597-60598,60600-60601,60606-60612,60615,60617,60619-60621,60623-60625,60627-60629,60631,60633,60635,60647,60650,60652,60654,60656,60658-60659,60664-60666,60668-60670,60672,60676,60678,60680-60683,60685-60686,60688,60690,60692-60694,60697-60700,60705-60706,60708,60711,60714,60720,60724-60730,60732,60736,60742,60744,60746,60748,60750-60751,60753,60756-60757,60759-60761,60763-60764,60766,60769-60770,60774-60784,60787-60789,60793,60796,60799-60809,60812-60813,60815-60821,60823-60826,60828-60829,60831-60834,60836,60838-60839,60846-60849,60852-60854,60856-60859,60861-60870,60874-60875,60880-60881,60886,60888-60890,60892,60894-60898,60900,60902-60906,60908,60911-60917,60919-60920,60922,60926,60929-60931,60933-60935,60937,60939-60941,60943-60954,60959-60961,60963-60969,60971-60976 via svnmerge from
Tip revision: 90c3d9b
xdrlib.py
"""Implements (a subset of) Sun XDR -- eXternal Data Representation.

See: RFC 1014

"""

import struct
from io import BytesIO

__all__ = ["Error", "Packer", "Unpacker", "ConversionError"]

# exceptions
class Error(Exception):
    """Exception class for this module. Use:

    except xdrlib.Error, var:
        # var has the Error instance for the exception

    Public ivars:
        msg -- contains the message

    """
    def __init__(self, msg):
        self.msg = msg
    def __repr__(self):
        return repr(self.msg)
    def __str__(self):
        return str(self.msg)


class ConversionError(Error):
    pass



class Packer:
    """Pack various data representations into a buffer."""

    def __init__(self):
        self.reset()

    def reset(self):
        self.__buf = BytesIO()

    def get_buffer(self):
        return self.__buf.getvalue()
    # backwards compatibility
    get_buf = get_buffer

    def pack_uint(self, x):
        self.__buf.write(struct.pack('>L', x))

    pack_int = pack_uint
    pack_enum = pack_int

    def pack_bool(self, x):
        if x: self.__buf.write(b'\0\0\0\1')
        else: self.__buf.write(b'\0\0\0\0')

    def pack_uhyper(self, x):
        self.pack_uint(x>>32 & 0xffffffff)
        self.pack_uint(x & 0xffffffff)

    pack_hyper = pack_uhyper

    def pack_float(self, x):
        try: self.__buf.write(struct.pack('>f', x))
        except struct.error as msg:
            raise ConversionError(msg)

    def pack_double(self, x):
        try: self.__buf.write(struct.pack('>d', x))
        except struct.error as msg:
            raise ConversionError(msg)

    def pack_fstring(self, n, s):
        if n < 0:
            raise ValueError('fstring size must be nonnegative')
        data = s[:n]
        n = ((n+3)//4)*4
        data = data + (n - len(data)) * b'\0'
        self.__buf.write(data)

    pack_fopaque = pack_fstring

    def pack_string(self, s):
        n = len(s)
        self.pack_uint(n)
        self.pack_fstring(n, s)

    pack_opaque = pack_string
    pack_bytes = pack_string

    def pack_list(self, list, pack_item):
        for item in list:
            self.pack_uint(1)
            pack_item(item)
        self.pack_uint(0)

    def pack_farray(self, n, list, pack_item):
        if len(list) != n:
            raise ValueError('wrong array size')
        for item in list:
            pack_item(item)

    def pack_array(self, list, pack_item):
        n = len(list)
        self.pack_uint(n)
        self.pack_farray(n, list, pack_item)



class Unpacker:
    """Unpacks various data representations from the given buffer."""

    def __init__(self, data):
        self.reset(data)

    def reset(self, data):
        self.__buf = data
        self.__pos = 0

    def get_position(self):
        return self.__pos

    def set_position(self, position):
        self.__pos = position

    def get_buffer(self):
        return self.__buf

    def done(self):
        if self.__pos < len(self.__buf):
            raise Error('unextracted data remains')

    def unpack_uint(self):
        i = self.__pos
        self.__pos = j = i+4
        data = self.__buf[i:j]
        if len(data) < 4:
            raise EOFError
        x = struct.unpack('>L', data)[0]
        try:
            return int(x)
        except OverflowError:
            return x

    def unpack_int(self):
        i = self.__pos
        self.__pos = j = i+4
        data = self.__buf[i:j]
        if len(data) < 4:
            raise EOFError
        return struct.unpack('>l', data)[0]

    unpack_enum = unpack_int

    def unpack_bool(self):
        return bool(self.unpack_int())

    def unpack_uhyper(self):
        hi = self.unpack_uint()
        lo = self.unpack_uint()
        return int(hi)<<32 | lo

    def unpack_hyper(self):
        x = self.unpack_uhyper()
        if x >= 0x8000000000000000:
            x = x - 0x10000000000000000
        return x

    def unpack_float(self):
        i = self.__pos
        self.__pos = j = i+4
        data = self.__buf[i:j]
        if len(data) < 4:
            raise EOFError
        return struct.unpack('>f', data)[0]

    def unpack_double(self):
        i = self.__pos
        self.__pos = j = i+8
        data = self.__buf[i:j]
        if len(data) < 8:
            raise EOFError
        return struct.unpack('>d', data)[0]

    def unpack_fstring(self, n):
        if n < 0:
            raise ValueError('fstring size must be nonnegative')
        i = self.__pos
        j = i + (n+3)//4*4
        if j > len(self.__buf):
            raise EOFError
        self.__pos = j
        return self.__buf[i:i+n]

    unpack_fopaque = unpack_fstring

    def unpack_string(self):
        n = self.unpack_uint()
        return self.unpack_fstring(n)

    unpack_opaque = unpack_string
    unpack_bytes = unpack_string

    def unpack_list(self, unpack_item):
        list = []
        while 1:
            x = self.unpack_uint()
            if x == 0: break
            if x != 1:
                raise ConversionError('0 or 1 expected, got %r' % (x,))
            item = unpack_item()
            list.append(item)
        return list

    def unpack_farray(self, n, unpack_item):
        list = []
        for i in range(n):
            list.append(unpack_item())
        return list

    def unpack_array(self, unpack_item):
        n = self.unpack_uint()
        return self.unpack_farray(n, unpack_item)
back to top