Explicitly convert str and bytes where needed

This commit is contained in:
flan 2017-11-04 04:11:48 +01:00
parent 5aff76fee7
commit 024e76423d
3 changed files with 15 additions and 15 deletions

View File

@ -30,7 +30,7 @@ import string
import unicodedata import unicodedata
import zipfile import zipfile
from sqlite3 import dbapi2 as sqlite from sqlite3 import dbapi2 as sqlite
from io import StringIO from io import BytesIO
import ankisyncd import ankisyncd
@ -133,7 +133,7 @@ class SyncMediaHandler(anki.sync.MediaSyncer):
max_zip_size = 100*1024*1024 max_zip_size = 100*1024*1024
max_meta_file_size = 100000 max_meta_file_size = 100000
file_buffer = StringIO(zip_data) file_buffer = BytesIO(zip_data)
zip_file = zipfile.ZipFile(file_buffer, 'r') zip_file = zipfile.ZipFile(file_buffer, 'r')
meta_file_size = zip_file.getinfo("_meta").file_size meta_file_size = zip_file.getinfo("_meta").file_size
@ -155,11 +155,11 @@ class SyncMediaHandler(anki.sync.MediaSyncer):
according to the data in zip file zipData. according to the data in zip file zipData.
""" """
file_buffer = StringIO(zip_data) file_buffer = BytesIO(zip_data)
zip_file = zipfile.ZipFile(file_buffer, 'r') zip_file = zipfile.ZipFile(file_buffer, 'r')
# Get meta info first. # Get meta info first.
meta = json.loads(zip_file.read("_meta")) meta = json.loads(zip_file.read("_meta").decode())
# Remove media files that were removed on the client. # Remove media files that were removed on the client.
media_to_remove = [] media_to_remove = []
@ -241,7 +241,7 @@ class SyncMediaHandler(anki.sync.MediaSyncer):
flist = {} flist = {}
cnt = 0 cnt = 0
sz = 0 sz = 0
f = StringIO() f = BytesIO()
z = zipfile.ZipFile(f, "w", compression=zipfile.ZIP_DEFLATED) z = zipfile.ZipFile(f, "w", compression=zipfile.ZIP_DEFLATED)
for fname in files: for fname in files:
@ -379,7 +379,7 @@ class SyncApp:
import hashlib, time, random, string import hashlib, time, random, string
chars = string.ascii_letters + string.digits chars = string.ascii_letters + string.digits
val = ':'.join([username, str(int(time.time())), ''.join(random.choice(chars) for x in range(8))]) val = ':'.join([username, str(int(time.time())), ''.join(random.choice(chars) for x in range(8))]).encode()
return hashlib.md5(val).hexdigest() return hashlib.md5(val).hexdigest()
def create_session(self, username, user_path): def create_session(self, username, user_path):
@ -389,13 +389,13 @@ class SyncApp:
import gzip import gzip
if compression: if compression:
buf = gzip.GzipFile(mode="rb", fileobj=StringIO(data)) buf = gzip.GzipFile(mode="rb", fileobj=BytesIO(data))
data = buf.read() data = buf.read()
buf.close() buf.close()
try: try:
data = json.loads(data) data = json.loads(data.decode())
except ValueError: except (ValueError, UnicodeDecodeError):
data = {'data': data} data = {'data': data}
return data return data

View File

@ -137,7 +137,7 @@ class SqliteUserManager(SimpleUserManager):
salt = self._extract_salt(expected_value) salt = self._extract_salt(expected_value)
hashobj = hashlib.sha256() hashobj = hashlib.sha256()
hashobj.update(username + password + salt) hashobj.update((username + password + salt).encode())
actual_value = hashobj.hexdigest() + salt actual_value = hashobj.hexdigest() + salt
if actual_value == expected_value: if actual_value == expected_value:
@ -156,8 +156,8 @@ class SqliteUserManager(SimpleUserManager):
@staticmethod @staticmethod
def _create_pass_hash(username, password): def _create_pass_hash(username, password):
salt = binascii.b2a_hex(os.urandom(8)) salt = binascii.b2a_hex(os.urandom(8))
pass_hash = (hashlib.sha256(username + password + salt).hexdigest() + pass_hash = (hashlib.sha256((username + password).encode() + salt).hexdigest() +
salt) salt.decode())
return pass_hash return pass_hash
def create_auth_db(self): def create_auth_db(self):

View File

@ -1,5 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from io import StringIO from io import BytesIO
import json import json
import logging import logging
import logging.config import logging.config
@ -28,7 +28,7 @@ def create_named_file(filename, file_contents=None):
if file_contents is not None: if file_contents is not None:
open(file_path, 'w').write(file_contents) open(file_path, 'w').write(file_contents)
return file_path.decode("utf-8") return file_path
def create_zip_with_existing_files(file_paths): def create_zip_with_existing_files(file_paths):
@ -41,7 +41,7 @@ def create_zip_with_existing_files(file_paths):
:return: the data of the created zip file :return: the data of the created zip file
""" """
file_buffer = StringIO() file_buffer = BytesIO()
zip_file = zipfile.ZipFile(file_buffer, zip_file = zipfile.ZipFile(file_buffer,
'w', 'w',
compression=zipfile.ZIP_DEFLATED) compression=zipfile.ZIP_DEFLATED)