| 1 | # Copyright (c) 2009, samurai-x.org |
|---|
| 2 | # All rights reserved. |
|---|
| 3 | # |
|---|
| 4 | # Redistribution and use in source and binary forms, with or without |
|---|
| 5 | # modification, are permitted provided that the following conditions are met: |
|---|
| 6 | # * Redistributions of source code must retain the above copyright |
|---|
| 7 | # notice, this list of conditions and the following disclaimer. |
|---|
| 8 | # * Redistributions in binary form must reproduce the above copyright |
|---|
| 9 | # notice, this list of conditions and the following disclaimer in the |
|---|
| 10 | # documentation and/or other materials provided with the distribution. |
|---|
| 11 | # * Neither the name of the samurai-x.org nor the |
|---|
| 12 | # names of its contributors may be used to endorse or promote products |
|---|
| 13 | # derived from this software without specific prior written permission. |
|---|
| 14 | # |
|---|
| 15 | # THIS SOFTWARE IS PROVIDED BY SAMURAI-X.ORG ``AS IS'' AND ANY |
|---|
| 16 | # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 17 | # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|---|
| 18 | # DISCLAIMED. IN NO EVENT SHALL SAMURAI-X.ORG BE LIABLE FOR ANY |
|---|
| 19 | # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
|---|
| 20 | # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
|---|
| 21 | # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
|---|
| 22 | # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 23 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
|---|
| 24 | # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 25 | |
|---|
| 26 | cdef extern from "Python.h": |
|---|
| 27 | |
|---|
| 28 | object PyString_FromStringAndSize(char *s, Py_ssize_t len) |
|---|
| 29 | |
|---|
| 30 | cdef class MemoryInputStream: |
|---|
| 31 | cdef unsigned int _root, _ptr |
|---|
| 32 | cdef bool _opened |
|---|
| 33 | |
|---|
| 34 | def __init__(self, address): |
|---|
| 35 | self._root = address |
|---|
| 36 | self._ptr = address |
|---|
| 37 | self._opened = True |
|---|
| 38 | |
|---|
| 39 | def close(self): |
|---|
| 40 | self._opened = False |
|---|
| 41 | |
|---|
| 42 | property closed: |
|---|
| 43 | def __get__(self): |
|---|
| 44 | return not self._opened |
|---|
| 45 | |
|---|
| 46 | def fileno(self): |
|---|
| 47 | raise IOError() |
|---|
| 48 | |
|---|
| 49 | def isatty(self): |
|---|
| 50 | return False |
|---|
| 51 | |
|---|
| 52 | def readable(self): |
|---|
| 53 | return True |
|---|
| 54 | |
|---|
| 55 | def readline(self, limit=0): |
|---|
| 56 | raise NotImplementedError() |
|---|
| 57 | |
|---|
| 58 | def readlines(self, hint=0): |
|---|
| 59 | raise NotImplementedError() |
|---|
| 60 | |
|---|
| 61 | def seek(self, offset, whence=0): |
|---|
| 62 | if whence == 0: |
|---|
| 63 | # from the start position of the stream |
|---|
| 64 | self._ptr = self._root + offset |
|---|
| 65 | return offset |
|---|
| 66 | elif whence == 1: |
|---|
| 67 | # from the current position of the stream |
|---|
| 68 | self._ptr += offset |
|---|
| 69 | return self._ptr - self._root |
|---|
| 70 | elif whence == 2: |
|---|
| 71 | # from the end of the stream ... impossible, sorry. |
|---|
| 72 | raise NotImplementedError('The memory stream has no end!') |
|---|
| 73 | else: |
|---|
| 74 | raise NotImplementedError('Unknown whence: %d' % whence) |
|---|
| 75 | |
|---|
| 76 | property address: |
|---|
| 77 | def __get__(self): |
|---|
| 78 | return self._root |
|---|
| 79 | |
|---|
| 80 | def seekable(self): |
|---|
| 81 | return True |
|---|
| 82 | |
|---|
| 83 | def tell(self): |
|---|
| 84 | return self._ptr - self._root |
|---|
| 85 | |
|---|
| 86 | def truncate(self): |
|---|
| 87 | raise NotImplementedError() |
|---|
| 88 | |
|---|
| 89 | def writable(self): |
|---|
| 90 | return False |
|---|
| 91 | |
|---|
| 92 | def write(self, b): |
|---|
| 93 | raise NotImplementedError() |
|---|
| 94 | |
|---|
| 95 | def read(self, b=None): |
|---|
| 96 | if b is None: |
|---|
| 97 | raise NotImplementedError('The memory stream has no end!') |
|---|
| 98 | chunk = PyString_FromStringAndSize(<char*>(self._ptr), b) |
|---|
| 99 | self._ptr += b |
|---|
| 100 | return chunk |
|---|
| 101 | |
|---|
| 102 | def readall(self): |
|---|
| 103 | raise NotImplementedError('The memory stream has no end!') |
|---|
| 104 | |
|---|
| 105 | def readinto(self, b): |
|---|
| 106 | length = len(b) |
|---|
| 107 | b.extend(self.read(length)) |
|---|
| 108 | return length |
|---|