diff options
author | Guido van Rossum <guido@python.org> | 2007-08-07 14:26:40 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-08-07 14:26:40 (GMT) |
commit | cd869d8d415a9278addf5f2bd3648aa2fbffe660 (patch) | |
tree | c602d76feaef709e145225722113625fc3f16962 /Lib/plat-mac | |
parent | ca8dd9182e60bc4411a5ed9b679caf188af05b6a (diff) | |
download | cpython-cd869d8d415a9278addf5f2bd3648aa2fbffe660.zip cpython-cd869d8d415a9278addf5f2bd3648aa2fbffe660.tar.gz cpython-cd869d8d415a9278addf5f2bd3648aa2fbffe660.tar.bz2 |
SF patch# 1769016 by James Brotchie.
Change plistlib to use bytes instead of strings.
Fix test_plistlib accordingly.
Diffstat (limited to 'Lib/plat-mac')
-rw-r--r-- | Lib/plat-mac/plistlib.py | 50 |
1 files changed, 25 insertions, 25 deletions
diff --git a/Lib/plat-mac/plistlib.py b/Lib/plat-mac/plistlib.py index 709e8e1..f035729 100644 --- a/Lib/plat-mac/plistlib.py +++ b/Lib/plat-mac/plistlib.py @@ -12,8 +12,8 @@ To parse a plist from a file, use the readPlist(pathOrFile) function, with a file name or a (readable) file object as the only argument. It returns the top level object (again, usually a dictionary). -To work with plist data in strings, you can use readPlistFromString() -and writePlistToString(). +To work with plist data in bytes objects, you can use readPlistFromBytes() +and writePlistToBytes(). Values can be strings, integers, floats, booleans, tuples, lists, dictionaries, Data or datetime.datetime objects. String values (including @@ -21,7 +21,7 @@ dictionary keys) may be unicode strings -- they will be written out as UTF-8. The <data> plist type is supported through the Data class. This is a -thin wrapper around a Python string. +thin wrapper around a Python bytes object. Generate Plist example: @@ -36,8 +36,8 @@ Generate Plist example: aTrueValue=True, aFalseValue=False, ), - someData = Data("<binary gunk>"), - someMoreData = Data("<lots of binary gunk>" * 10), + someData = Data(b"<binary gunk>"), + someMoreData = Data(b"<lots of binary gunk>" * 10), aDate = datetime.datetime.fromtimestamp(time.mktime(time.gmtime())), ) # unicode keys are possible, but a little awkward to use: @@ -52,7 +52,7 @@ Parse Plist example: __all__ = [ - "readPlist", "writePlist", "readPlistFromString", "writePlistToString", + "readPlist", "writePlist", "readPlistFromBytes", "writePlistToBytes", "readPlistFromResource", "writePlistToResource", "Plist", "Data", "Dict" ] @@ -60,7 +60,7 @@ __all__ = [ import binascii import datetime -from cStringIO import StringIO +from io import BytesIO import re @@ -71,7 +71,7 @@ def readPlist(pathOrFile): """ didOpen = 0 if isinstance(pathOrFile, str): - pathOrFile = open(pathOrFile) + pathOrFile = open(pathOrFile, 'rb') didOpen = 1 p = PlistParser() rootObject = p.parse(pathOrFile) @@ -86,7 +86,7 @@ def writePlist(rootObject, pathOrFile): """ didOpen = 0 if isinstance(pathOrFile, str): - pathOrFile = open(pathOrFile, "w") + pathOrFile = open(pathOrFile, 'wb') didOpen = 1 writer = PlistWriter(pathOrFile) writer.writeln("<plist version=\"1.0\">") @@ -96,16 +96,16 @@ def writePlist(rootObject, pathOrFile): pathOrFile.close() -def readPlistFromString(data): - """Read a plist data from a string. Return the root object. +def readPlistFromBytes(data): + """Read a plist data from a bytes object. Return the root object. """ - return readPlist(StringIO(data)) + return readPlist(BytesIO(data)) -def writePlistToString(rootObject): - """Return 'rootObject' as a plist-formatted string. +def writePlistToBytes(rootObject): + """Return 'rootObject' as a plist-formatted bytes object. """ - f = StringIO() + f = BytesIO() writePlist(rootObject, f) return f.getvalue() @@ -145,7 +145,6 @@ def writePlistToResource(rootObject, path, restype='plst', resid=0): class DumbXMLWriter: - def __init__(self, file, indentLevel=0, indent="\t"): self.file = file self.stack = [] @@ -172,9 +171,12 @@ class DumbXMLWriter: def writeln(self, line): if line: - self.file.write(self.indentLevel * self.indent + line + "\n") - else: - self.file.write("\n") + # plist has fixed encoding of utf-8 + if isinstance(line, str): + line = line.encode('utf-8') + self.file.write(self.indentLevel * self.indent) + self.file.write(line) + self.file.write('\n') # Contents should conform to a subset of ISO 8601 @@ -355,13 +357,15 @@ def _encodeBase64(s, maxlinelength=76): for i in range(0, len(s), maxbinsize): chunk = s[i : i + maxbinsize] pieces.append(binascii.b2a_base64(chunk)) - return "".join(pieces) + return b''.join(pieces) class Data: """Wrapper for binary data.""" def __init__(self, data): + if not isinstance(data, bytes): + raise TypeError("data must be as bytes") self.data = data def fromBase64(cls, data): @@ -426,11 +430,7 @@ class PlistParser: self.stack[-1].append(value) def getData(self): - data = "".join(self.data) - try: - data = data.encode("ascii") - except UnicodeError: - pass + data = ''.join(self.data) self.data = [] return data |