You need to change 'str' to 'bytes'. Try this:
class StrToBytes:
def __init__(self, fileobj):
self.fileobj = fileobj
def read(self, size):
return self.fileobj.read(size).encode()
def readline(self, size=-1):
return self.fileobj.readline(size).encode()
with open(fname, 'r') as f:
obj = pickle.load(StrToBytes(f))