mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix for plistlib dropping Data in python 3.9
Fixes #1184 (plistlib.Data is going to be dropped in python 3.9)
This commit is contained in:
parent
4d3d69ccd1
commit
5b2baa7233
@ -363,29 +363,25 @@ class XMLConfig(dict):
|
||||
self.update(d)
|
||||
|
||||
def __getitem__(self, key):
|
||||
from polyglot.plistlib import Data
|
||||
from polyglot.plistlib import unwrap_bytes
|
||||
try:
|
||||
ans = dict.__getitem__(self, key)
|
||||
if isinstance(ans, Data):
|
||||
ans = ans.data
|
||||
return ans
|
||||
return unwrap_bytes(ans)
|
||||
except KeyError:
|
||||
return self.defaults.get(key, None)
|
||||
|
||||
def get(self, key, default=None):
|
||||
from polyglot.plistlib import Data
|
||||
from polyglot.plistlib import unwrap_bytes
|
||||
try:
|
||||
ans = dict.__getitem__(self, key)
|
||||
if isinstance(ans, Data):
|
||||
ans = ans.data
|
||||
return ans
|
||||
return unwrap_bytes(ans)
|
||||
except KeyError:
|
||||
return self.defaults.get(key, default)
|
||||
|
||||
def __setitem__(self, key, val):
|
||||
from polyglot.plistlib import Data
|
||||
from polyglot.plistlib import wrap_bytes
|
||||
if isinstance(val, bytes):
|
||||
val = Data(val)
|
||||
val = wrap_bytes(val)
|
||||
dict.__setitem__(self, key, val)
|
||||
self.commit()
|
||||
|
||||
|
@ -7,6 +7,20 @@ from __future__ import absolute_import, division, print_function, unicode_litera
|
||||
from polyglot.builtins import is_py3
|
||||
|
||||
if is_py3:
|
||||
from plistlib import loads, dumps, Data # noqa
|
||||
from plistlib import loads, dumps # noqa
|
||||
|
||||
def unwrap_bytes(x):
|
||||
return x
|
||||
|
||||
def wrap_bytes(x):
|
||||
return x
|
||||
else:
|
||||
from plistlib import readPlistFromString as loads, writePlistToString as dumps, Data # noqa
|
||||
|
||||
def unwrap_bytes(x):
|
||||
if isinstance(x, Data):
|
||||
x = x.data
|
||||
return x
|
||||
|
||||
def wrap_bytes(x):
|
||||
return Data(x)
|
||||
|
Loading…
x
Reference in New Issue
Block a user