Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle float values in bytes escape functions #88

Merged
merged 2 commits into from
Mar 19, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix bytes escape functions to handle floats.
  • Loading branch information
amotzg committed Mar 19, 2017
commit 51dae31c2cda761645186a5de8cdde0e701ba386
5 changes: 2 additions & 3 deletions rdbtools/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import codecs
import json

from rdbtools.compat import isinteger
from rdbtools.parser import RdbCallback
from rdbtools import encodehelpers

Expand Down Expand Up @@ -240,7 +239,7 @@ def __init__(self, out, string_escape=None):
self._dbnum = 0

def dbstr(self):
return b'db=' + encodehelpers.int2bytes(self._dbnum) + b' '
return b'db=' + encodehelpers.num2bytes(self._dbnum) + b' '
def start_rdb(self):
pass

Expand Down Expand Up @@ -282,7 +281,7 @@ def start_list(self, key, expiry, info):
self._index = 0

def rpush(self, key, value) :
istr = encodehelpers.int2bytes(self._index)
istr = encodehelpers.num2bytes(self._index)
self._out.write(self.dbstr() + self.encode_key(key) + b'[' + istr + b'] -> ' + self.encode_value(value))
self.newline()
self._index = self._index + 1
Expand Down
8 changes: 4 additions & 4 deletions rdbtools/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

try:
long
def isinteger(n):
return isinstance(n, int) or isinstance(n, long)
def isnumber(n):
return isinstance(n, int) or isinstance(n, long) or isinstance(n, float)
except NameError:
def isinteger(n):
return isinstance(n, int)
def isnumber(n):
return isinstance(n, int) or isinstance(n, float)

if sys.version_info < (3,):
def str2regexp(pattern):
Expand Down
22 changes: 11 additions & 11 deletions rdbtools/encodehelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import codecs
import sys

from .compat import isinteger
from .compat import isnumber

STRING_ESCAPE_RAW = 'raw'
STRING_ESCAPE_PRINT = 'print'
Expand All @@ -14,14 +14,14 @@
if sys.version_info < (3,):
bval = ord

def int2unistr(i): return codecs.decode(str(i), 'ascii')
int2bytes = str
def num2unistr(i): return codecs.decode(str(i), 'ascii')
num2bytes = str
else:
def bval(x): return x

int2unistr = str
num2unistr = str

def int2bytes(i): return codecs.encode(str(i), 'ascii')
def num2bytes(i): return codecs.encode(str(i), 'ascii')

ASCII_ESCAPE_LOOKUP = [u'\\x00', u'\\x01', u'\\x02', u'\\x03', u'\\x04', u'\\x05', u'\\x06', u'\\x07', u'\\x08',
u'\\x09', u'\\x0A', u'\\x0B', u'\\x0C', u'\\x0D', u'\\x0E', u'\\x0F', u'\\x10', u'\\x11',
Expand Down Expand Up @@ -101,11 +101,11 @@ def bytes_to_unicode(byte_data, escape, skip_printable=False):
:param skip_printable: If True, don't escape byte_data with all 'printable ASCII' bytes. Defaults to False.
:return: New unicode string, escaped with the specified method if needed.
"""
if isinteger(byte_data):
if isnumber(byte_data):
if skip_printable:
return int2unistr(byte_data)
return num2unistr(byte_data)
else:
byte_data = int2bytes(byte_data)
byte_data = num2bytes(byte_data)
else:
assert (isinstance(byte_data, type(b'')))
if skip_printable and all(0x20 <= bval(ch) <= 0x7E for ch in byte_data):
Expand All @@ -132,11 +132,11 @@ def apply_escape_bytes(byte_data, escape, skip_printable=False):
:return: new bytes object with the escaped bytes or byte_data itself on some no-op cases.
"""

if isinteger(byte_data):
if isnumber(byte_data):
if skip_printable:
return int2bytes(byte_data)
return num2bytes(byte_data)
else:
byte_data = int2bytes(byte_data)
byte_data = num2bytes(byte_data)
else:
assert (isinstance(byte_data, type(b'')))
if skip_printable and all(0x20 <= bval(ch) <= 0x7E for ch in byte_data):
Expand Down