Skip to content

Commit

Permalink
Merge pull request #19 from thisisparker/postscriptbytes
Browse files Browse the repository at this point in the history
Store postscript more consistently as bytes and recover from save errors more neatly
  • Loading branch information
alexdej authored Mar 15, 2019
2 parents fb1aa63 + b8b598d commit 8906ab8
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions puz.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def __init__(self):
"""Initializes a blank puzzle
"""
self.preamble = b''
self.postscript = ''
self.postscript = b''
self.title = ''
self.author = ''
self.copyright = ''
Expand Down Expand Up @@ -222,8 +222,9 @@ def load(self, data):
)

def save(self, filename):
puzzle_bytes = self.tobytes()
with open(filename, 'wb') as f:
f.write(self.tobytes())
f.write(puzzle_bytes)

def tobytes(self):
s = PuzzleBuffer()
Expand Down Expand Up @@ -270,7 +271,14 @@ def tobytes(self):
s.pack(EXTENSION_HEADER_FORMAT, code, len(data), data_cksum(data))
s.write(data + b'\0')

s.write(self.postscript.encode(ENCODING))
# postscript is initialized, read, and stored as bytes. In case it is
# overwritten as a string, this try/except converts it back.
try:
postscript_bytes = self.postscript.encode(ENCODING)
except AttributeError:
postscript_bytes = self.postscript

s.write(postscript_bytes)

return s.tobytes()

Expand Down

0 comments on commit 8906ab8

Please sign in to comment.