예제 #1
0
 def test_title(self):
     stream = TemporaryFile()
     f = formatters.TerminfoFormatter(stream, 'xterm+sl', True, 'ascii')
     f.title('TITLE')
     stream.seek(0)
     self.assertEqual(compatibility.force_bytes('\x1b]0;TITLE\x07'),
                      stream.read())
예제 #2
0
 def test_title(self):
     stream = TemporaryFile()
     f = formatters.TerminfoFormatter(stream, 'xterm+sl', True, 'ascii')
     f.title('TITLE')
     stream.seek(0)
     self.assertEqual(compatibility.force_bytes('\x1b]0;TITLE\x07'),
                      stream.read())
예제 #3
0
 def test_zero_length(self):
     path = pjoin(self.dir, "target")
     self.write_file(path, "w", "")
     m, f = self.func(path)
     self.assertIdentical(m, None)
     self.assertEqual(f.read(), compatibility.force_bytes(""))
     f.close()
예제 #4
0
 def test_zero_length(self):
     path = pjoin(self.dir, 'target')
     self.write_file(path, 'w', '')
     m, f = self.func(path)
     self.assertIdentical(m, None)
     self.assertEqual(f.read(), compatibility.force_bytes(''))
     f.close()
예제 #5
0
 def _test_stream(self, stream, formatter, inputs, output):
     stream.seek(0)
     stream.truncate()
     formatter.write(*inputs)
     stream.seek(0)
     result = stream.read()
     self.assertEqual(compatibility.force_bytes(''.join(output)), result,
         msg="given(%r), expected(%r), got(%r)" % (inputs, ''.join(output), result))
예제 #6
0
 def _test_stream(self, stream, formatter, inputs, output):
     stream.seek(0)
     stream.truncate()
     formatter.write(*inputs)
     stream.seek(0)
     result = stream.read()
     self.assertEqual(compatibility.force_bytes(''.join(output)),
                      result,
                      msg="given(%r), expected(%r), got(%r)" %
                      (inputs, ''.join(output), result))
예제 #7
0
    def test_mmap(self, data="foonani"):
        path = pjoin(self.dir, "target")
        self.write_file(path, "w", data)

        data = compatibility.force_bytes(data)
        m, f = self.func(path)
        self.assertEqual(len(m), len(data))
        self.assertEqual(m.read(len(data)), data)
        m.close()
        self.assertIdentical(f, None)
예제 #8
0
    def test_mmap(self, data='foonani'):
        path = pjoin(self.dir, 'target')
        self.write_file(path, 'w', data)

        data = compatibility.force_bytes(data)
        m, f = self.func(path)
        self.assertEqual(len(m), len(data))
        self.assertEqual(m.read(len(data)), data)
        m.close()
        self.assertIdentical(f, None)
예제 #9
0
파일: fileutils.py 프로젝트: chutz/snakeoil
def mmap_or_open_for_read(path):
    size = os.stat(path).st_size
    if size == 0:
        return (None, data_source.bytes_ro_StringIO(
            compatibility.force_bytes('')))
    fd = None
    try:
        fd = os.open(path, os.O_RDONLY)
        return (_fileutils.mmap_and_close(fd, size,
            mmap.MAP_SHARED, mmap.PROT_READ), None)
    except compatibility.IGNORED_EXCEPTIONS:
        raise
    except:
        try:
            os.close(fd)
        except EnvironmentError:
            pass
        raise
예제 #10
0
 def test_it(self):
     path = pjoin(self.dir, "target")
     data = compatibility.force_bytes("asdfasdf")
     self.write_file(path, "wb", [data])
     fd, m = None, None
     try:
         fd = os.open(path, os.O_RDONLY)
         m = _fileutils.mmap_and_close(fd, len(data), mmap.MAP_PRIVATE, mmap.PROT_READ)
         # and ensure it closed the fd...
         self.assertRaises(EnvironmentError, os.read, fd, 1)
         fd = None
         self.assertEqual(len(m), len(data))
         self.assertEqual(m.read(len(data)), data)
     finally:
         if m is not None:
             m.close()
         if fd is not None:
             os.close(fd)
예제 #11
0
def mmap_or_open_for_read(path):
    size = os.stat(path).st_size
    if size == 0:
        return (None,
                data_source.bytes_ro_StringIO(compatibility.force_bytes('')))
    fd = None
    try:
        fd = os.open(path, os.O_RDONLY)
        return (_fileutils.mmap_and_close(fd, size, mmap.MAP_SHARED,
                                          mmap.PROT_READ), None)
    except compatibility.IGNORED_EXCEPTIONS:
        raise
    except:
        try:
            os.close(fd)
        except EnvironmentError:
            pass
        raise
예제 #12
0
    def assertOut(self, *args, **kwargs):

        stringlist = []
        objectlist = []

        args = list(args)

        prefix = kwargs.setdefault("prefix", self.prefix)
        if isinstance(prefix, tuple):
            args = list(prefix) + args
        elif isinstance(prefix, list):
            args = prefix + args
        else:
            args.insert(0, prefix)

        suffix = kwargs.setdefault("suffix", self.suffix)
        if isinstance(suffix, tuple):
            args = args + list(suffix)
        elif isinstance(suffix, list):
            args = args + suffix
        else:
            args.append(suffix)

        for arg in args:
            if isinstance(arg, unicode):
                stringlist.append(arg.encode('ascii'))
            elif isinstance(arg, bytes):
                stringlist.append(arg)
            else:
                objectlist.append(_join_bytes(stringlist))
                stringlist = []
                objectlist.append(arg)
        objectlist.append(_join_bytes(stringlist))

        # Hack because a list with an empty string in is True
        if objectlist == [force_bytes('')]:
            objectlist = []

        self.assertEqual(self.fakeout.stream, objectlist, '\n' + '\n'.join(
                difflib.unified_diff(
                    list(repr(s) for s in objectlist),
                    list(repr(s) for s in self.fakeout.stream),
                    'expected', 'actual', lineterm='')))
        self.fakeout.resetstream()
예제 #13
0
 def test_it(self):
     path = pjoin(self.dir, 'target')
     data = compatibility.force_bytes('asdfasdf')
     self.write_file(path, 'wb', [data])
     fd, m = None, None
     try:
         fd = os.open(path, os.O_RDONLY)
         m = _fileutils.mmap_and_close(fd, len(data), mmap.MAP_PRIVATE,
                                       mmap.PROT_READ)
         # and ensure it closed the fd...
         self.assertRaises(EnvironmentError, os.read, fd, 1)
         fd = None
         self.assertEqual(len(m), len(data))
         self.assertEqual(m.read(len(data)), data)
     finally:
         if m is not None:
             m.close()
         if fd is not None:
             os.close(fd)
예제 #14
0
 def __call__(self, formatter):
     if self.color is None:
         formatter._current_colors[self.mode] = None
         res = formatter._color_reset
         # slight abuse of boolean True/False and 1/0 equivalence
         other = formatter._current_colors[not self.mode]
         if other is not None:
             res = res + other
     else:
         if self.mode == 0:
             default = curses.COLOR_WHITE
         else:
             default = curses.COLOR_BLACK
         color = formatter._colors.get(self.color, default)
         # The curses module currently segfaults if handed a
         # bogus template so check explicitly.
         template = formatter._set_color[self.mode]
         if template:
             res = curses.tparm(template, color)
         else:
             res = compatibility.force_bytes('')
         formatter._current_colors[self.mode] = res
     formatter.stream.write(res)
예제 #15
0
 def assertStreamEqual(self, output, stream):
     self.assertEqual(compatibility.force_bytes(output), stream.getvalue())
예제 #16
0
            K[0:8] = L[0:8]

            L[0] = CDo(state, 0) ^ K[0]
            L[1] = CDo(state, 1) ^ K[1]
            L[2] = CDo(state, 2) ^ K[2]
            L[3] = CDo(state, 3) ^ K[3]
            L[4] = CDo(state, 4) ^ K[4]
            L[5] = CDo(state, 5) ^ K[5]
            L[6] = CDo(state, 6) ^ K[6]
            L[7] = CDo(state, 7) ^ K[7]
            for i in xrange(8):
                state[i] = L[i]
        # apply the Miyaguchi-Preneel compression function
        for i in xrange(8):
            self.hash[i] ^= state[i] ^ block[i]
        return


#
# Tests.
#

if __name__ == '__main__':
    from snakeoil.compatibility import force_bytes
    assert Whirlpool(force_bytes('The quick brown fox jumps over the lazy dog')).hexdigest() == \
        'b97de512e91e3828b40d2b0fdce9ceb3c4a71f9bea8d88e75c4fa854df36725fd2b52eb6544edcacd6f8beddfea403cb55ae31f03ad62a5ef54e42ee82c3fb35'
    assert Whirlpool(force_bytes('The quick brown fox jumps over the lazy eog')).hexdigest() == \
        'c27ba124205f72e6847f3e19834f925cc666d0974167af915bb462420ed40cc50900d85a1f923219d832357750492d5c143011a76988344c2635e69d06f2d38c'
    assert Whirlpool(force_bytes('')).hexdigest() == \
        '19fa61d75522a4669b44e39c1d2e1726c530232130d407f89afee0964997f7a73e83be698b288febcf88e3e03c4f0757ea8964e59b63d93708b138cc42a66eb3'
예제 #17
0
파일: helpers.py 프로젝트: veelai/pkgcore
    return parser


class fake_domain(object):
    pkgcore_config_type = ConfigHint(typename='domain')
    def __init__(self):
        pass

default_domain = basics.HardCodedConfigSection({
    'class': fake_domain,
    'default': True,
    })


# b''.join but works on python < 2.6
_join_bytes = force_bytes('').join

class FormatterObject(object):
    __metaclass__ = WeakInstMeta
    __inst_caching__ = True
    def __call__(self, formatter):
        formatter.stream.write(self)

class Color(FormatterObject):
    __inst_caching__ = True
    def __init__(self, mode, color):
        self.mode = mode
        self.color = color
    def __repr__(self):
        return '<Color: mode - %s; color - %s>' % (self.mode, self.color)
예제 #18
0
 def assertStreamEqual(self, output, stream):
     self.assertEqual(compatibility.force_bytes(output), stream.getvalue())