Skip to content

Commit

Permalink
Merge pull request #43 from b4stien/master
Browse files Browse the repository at this point in the history
Make read of in-memory PDF work with 3.x

- This closes issue #38 with code discussed there plus a regression test.
- Also add Python version 3.5 to regression tests
- Also add OSX filetypes to .gitignore
  • Loading branch information
pmaupin committed Dec 13, 2015
2 parents 7c0f427 + c8ec1ba commit 9e4aa55
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# OSX
.DS_Store
.AppleDouble
.LSOverride
Icon

# Thumbnails
._*

# Files that might appear on external disk
.Spotlight-V100
.Trashes


# Development artifacts
diffs.txt
examples/*.pdf
Expand All @@ -9,6 +23,7 @@ tests/pdfrw
tests/static_pdfs
tests/ramdisk
tests/saved_results
tests/tmp_results
wiki/


Expand Down
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ python:
- "2.7"
- "3.3"
- "3.4"
- "3.5"
- "nightly"
# command to install dependencies
before_install:
Expand Down
7 changes: 5 additions & 2 deletions pdfrw/pdfreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,13 @@ def readnode(node):

def __init__(self, fname=None, fdata=None, decompress=False,
disable_gc=True, verbose=True):

self.private.verbose = verbose

# Runs a lot faster with GC off.
disable_gc = disable_gc and gc.isenabled()
if disable_gc:
gc.disable()

try:
if fname is not None:
assert fdata is None
Expand All @@ -494,8 +495,10 @@ def __init__(self, fname=None, fdata=None, decompress=False,
except IOError:
raise PdfParseError('Could not read PDF file %s' %
fname)
fdata = convert_load(fdata)

assert fdata is not None
fdata = convert_load(fdata)

if not fdata.startswith('%PDF-'):
startloc = fdata.find('%PDF-')
if startloc >= 0:
Expand Down
4 changes: 3 additions & 1 deletion pdfrw/py23_diffs.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
except NameError:

def convert_load(s):
return s.decode('Latin-1')
if isinstance(s, bytes):
return s.decode('Latin-1')
return s

def convert_store(s):
return s.encode('Latin-1')
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',

'Programming Language :: Python :: 3.5',
'Topic :: Multimedia :: Graphics :: Graphics Conversion',
'Topic :: Software Development :: Libraries',
'Topic :: Text Processing',
Expand Down
28 changes: 28 additions & 0 deletions tests/test_pdfreader_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#! /usr/bin/env python
import static_pdfs

from pdfrw import PdfReader

try:
import unittest2 as unittest
except ImportError:
import unittest


class TestPdfReaderInit(unittest.TestCase):

def test_fname_binary_filelike(self):
with open(static_pdfs.pdffiles[0][0], 'rb') as pdf_file:
PdfReader(pdf_file)

def test_fdata_binary(self):
with open(static_pdfs.pdffiles[0][0], 'rb') as pdf_file:
pdf_bytes = pdf_file.read()
PdfReader(fdata=pdf_bytes)


def main():
unittest.main()

if __name__ == '__main__':
main()

0 comments on commit 9e4aa55

Please sign in to comment.