Skip to content
Open
Changes from all commits
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
18 changes: 15 additions & 3 deletions unrar/rarfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,17 @@ def b(x):
return x
else:
def b(x):
if x is not None:
# encode using DOS OEM standard
return x.encode('cp437')
if isinstance(x, str):
try:
if platform.system() == 'Windows':
return x.encode('cp' + str(ctypes.windll.kernel32.GetACP()))
else:
return x.encode('utf-8')
except UnicodeEncodeError:
return x.encode('cp437')
else:
# assume None or bytes
return x


class BadRarFile(Exception):
Expand Down Expand Up @@ -128,6 +136,10 @@ def __init__(self, filename, mode='r', pwd=None):
handle = self._open(archive)

# assert(archive.OpenResult == constants.SUCCESS)
# On windows, unRAR use system codepage to convert to wide char
# On linux, assume utf-8 and convert to wide char
# Users also can pass bytes object directly if require
# TODO: handle password in callback
self.pwd = pwd
if self.pwd is not None:
unrarlib.RARSetPassword(handle, b(self.pwd))
Expand Down