Skip to content

Commit 739da99

Browse files
authored
Merge pull request houtianze#258 from wangjiezhe/master
Fix "TypeError: b'xxxxxx' is not JSON serializable"
2 parents b3d38e9 + 8219217 commit 739da99

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

bypy.py

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ def format(self, obj, context, maxlevels, level):
818818
except:
819819
convert = True
820820
if convert:
821-
return ("0x{}".format(binascii.hexlify(obj)), True, False)
821+
return ("0x{}".format(obj), True, False)
822822
return pprint.PrettyPrinter.format(self, obj, context, maxlevels, level)
823823

824824
# there is room for more space optimization (like using the tree structure),
@@ -856,17 +856,11 @@ def __call__(self, *args):
856856
and info['mtime'] == getfilemtime_int(path) \
857857
and self.f.__name__ in info \
858858
and cached.usecache:
859-
# NOTE: behavior change: hexlify & unhexlify
860-
#result = info[self.f.__name__]
861-
# TODO: HACK here
862-
if self.f.__name__ == 'crc32': # it's a int (long), can't unhexlify
863-
result = info[self.f.__name__]
864-
else:
865-
result = binascii.unhexlify(info[self.f.__name__])
859+
result = info[self.f.__name__]
866860
if cached.debug:
867861
pdbg("Cache hit for file '{}',\n{}: {}\nsize: {}\nmtime: {}".format(
868862
path, self.f.__name__,
869-
result if isinstance(result, (int, long, float, complex)) else binascii.hexlify(result),
863+
result,
870864
info['size'], info['mtime']))
871865
else:
872866
result = self.f(*args)
@@ -890,20 +884,14 @@ def __store(self, info, path, value):
890884
cached.dirty = True
891885
info['size'] = getfilesize(path)
892886
info['mtime'] = getfilemtime_int(path)
893-
# NOTE: behavior change: hexlify & unhexlify
894-
#info[self.f.__name__] = value
895-
# TODO: HACK here
896-
if self.f.__name__ == 'crc32': # it's a int (long), can't hexlify
897-
info[self.f.__name__] = value
898-
else:
899-
info[self.f.__name__] = binascii.hexlify(value)
887+
info[self.f.__name__] = value
900888
if cached.debug:
901889
situation = "Storing cache"
902890
if cached.usecache:
903891
situation = "Cache miss"
904892
pdbg((situation + " for file '{}',\n{}: {}\nsize: {}\nmtime: {}").format(
905893
path, self.f.__name__,
906-
value if isinstance(value, (int, long, float, complex)) else binascii.hexlify(value),
894+
value,
907895
info['size'], info['mtime']))
908896

909897
# periodically save to prevent loss in case of system crash
@@ -1058,7 +1046,7 @@ def md5(filename, slice = OneM):
10581046
else:
10591047
break
10601048

1061-
return m.digest()
1049+
return m.hexdigest()
10621050

10631051
# slice md5 for baidu rapidupload
10641052
@cached
@@ -1068,7 +1056,7 @@ def slice_md5(filename):
10681056
buf = f.read(256 * OneK)
10691057
m.update(buf)
10701058

1071-
return m.digest()
1059+
return m.hexdigest()
10721060

10731061
@cached
10741062
def crc32(filename, slice = OneM):
@@ -1126,7 +1114,7 @@ def __str(self, prefix):
11261114
result += "{} - {}/{} - size: {} - md5: {} \n".format(
11271115
v.type, prefix, k,
11281116
v.extra['size'] if 'size' in v.extra else '',
1129-
binascii.hexlify(v.extra['md5']) if 'md5' in v.extra else '')
1117+
v.extra['md5'] if 'md5' in v.extra else '')
11301118

11311119
for k, v in self.items():
11321120
if v.type == 'D':
@@ -2139,7 +2127,7 @@ def __verify_current_file(self, j, gotlmd5):
21392127
return EHashMismatch
21402128

21412129
if 'md5' in j:
2142-
rmd5 = binascii.unhexlify(j['md5'])
2130+
rmd5 = j['md5']
21432131
#elif 'block_list' in j and len(j['block_list']) > 0:
21442132
# rmd5 = j['block_list'][0]
21452133
#else:
@@ -2161,8 +2149,8 @@ def __verify_current_file(self, j, gotlmd5):
21612149
if self.__verify:
21622150
if not gotlmd5:
21632151
self.__current_file_md5 = md5(self.__current_file)
2164-
self.pd("Local file MD5 : {}".format(binascii.hexlify(self.__current_file_md5)))
2165-
self.pd("Remote file MD5: {}".format(binascii.hexlify(rmd5)))
2152+
self.pd("Local file MD5 : {}".format(self.__current_file_md5))
2153+
self.pd("Remote file MD5: {}".format(rmd5))
21662154

21672155
if self.__current_file_md5 == rmd5:
21682156
self.pd("Local file and remote file hashes match")
@@ -2340,7 +2328,7 @@ def __upload_slice_act(self, r, args):
23402328
# otherwise, it makes the uploading slower at the end
23412329
rsmd5 = j['md5']
23422330
self.pd("Uploaded MD5 slice: " + rsmd5)
2343-
if self.__current_slice_md5 == binascii.unhexlify(rsmd5):
2331+
if self.__current_slice_md5 == rsmd5:
23442332
self.__slice_md5s.append(rsmd5)
23452333
self.pv("'{}' >>==> '{}' OK.".format(self.__current_file, args))
23462334
return ENoError
@@ -2402,7 +2390,7 @@ def __upload_file_slices(self, localpath, remotepath, ondup = 'overwrite'):
24022390
for md in md5s:
24032391
cslice = f.read(slice)
24042392
cm = hashlib.md5(cslice)
2405-
if (binascii.hexlify(cm.digest()) == md):
2393+
if (cm.hexdigest() == md):
24062394
self.pd("{} verified".format(md))
24072395
# TODO: a more rigorous check would be also verifying
24082396
# slices exist at Baidu Yun as well (rapidupload test?)
@@ -2424,9 +2412,9 @@ def __upload_file_slices(self, localpath, remotepath, ondup = 'overwrite'):
24242412
self.__current_slice = f.read(slice)
24252413
m = hashlib.md5()
24262414
m.update(self.__current_slice)
2427-
self.__current_slice_md5 = m.digest()
2415+
self.__current_slice_md5 = m.hexdigest()
24282416
self.pd("Uploading MD5 slice: {}, #{} / {}".format(
2429-
binascii.hexlify(self.__current_slice_md5),
2417+
self.__current_slice_md5,
24302418
i + 1, pieces))
24312419
j = 0
24322420
while True:
@@ -2497,8 +2485,8 @@ def __get_hashes_for_rapidupload(self, lpath, setlocalfile = False):
24972485
def __rapidupload_file(self, lpath, rpath, ondup = 'overwrite', setlocalfile = False):
24982486
self.__get_hashes_for_rapidupload(lpath, setlocalfile)
24992487

2500-
md5str = binascii.hexlify(self.__current_file_md5)
2501-
slicemd5str = binascii.hexlify(self.__current_file_slice_md5)
2488+
md5str = self.__current_file_md5
2489+
slicemd5str = self.__current_file_slice_md5
25022490
crcstr = hex(self.__current_file_crc32)
25032491
return self.__rapidupload_file_post(rpath, self.__current_file_size, md5str, slicemd5str, crcstr, ondup)
25042492

@@ -3409,11 +3397,11 @@ def __proceed_remote_gather(self, remotepath, dirjs, filejs, args = None):
34093397
dlen = len(remotepath) + 1
34103398
for d in dirjs:
34113399
self.__remote_dir_contents.get(remotepath[rootlen:]).add(
3412-
d['path'][dlen:], PathDictTree('D', size = d['size'], md5 = binascii.unhexlify(d['md5'])))
3400+
d['path'][dlen:], PathDictTree('D', size = d['size'], md5 = d['md5']))
34133401

34143402
for f in filejs:
34153403
self.__remote_dir_contents.get(remotepath[rootlen:]).add(
3416-
f['path'][dlen:], PathDictTree('F', size = f['size'], md5 = binascii.unhexlify(f['md5'])))
3404+
f['path'][dlen:], PathDictTree('F', size = f['size'], md5 = f['md5']))
34173405

34183406
return ENoError
34193407

@@ -3837,8 +3825,8 @@ def cdl_cancel(self, task_id):
38373825
return self.__cdl_cancel(task_id)
38383826

38393827
def __get_accept_cmd(self, rpath):
3840-
md5str = binascii.hexlify(self.__current_file_md5)
3841-
slicemd5str = binascii.hexlify(self.__current_file_slice_md5)
3828+
md5str = self.__current_file_md5
3829+
slicemd5str = self.__current_file_slice_md5
38423830
crcstr = hex(self.__current_file_crc32)
38433831
remotepath = rpath[AppPcsPathLen:]
38443832
if len(remotepath) == 0:

0 commit comments

Comments
 (0)