44
44
import decimal
45
45
import codecs
46
46
47
+ from PyPDF2 .utils import ERR_STREAM_TRUNCATED_PREMATURELY
48
+
47
49
ObjectPrefix = b_ ('/<[tf(n%' )
48
50
NumberSigns = b_ ('+-' )
49
51
IndirectPattern = re .compile (b_ (r"[+-]?(\d+)\s+(\d+)\s+R[^a-zA-Z]" ))
@@ -199,17 +201,15 @@ def readFromStream(stream, pdf):
199
201
while True :
200
202
tok = stream .read (1 )
201
203
if not tok :
202
- # stream has truncated prematurely
203
- raise PdfStreamError ("Stream has ended unexpectedly" )
204
+ raise PdfStreamError (ERR_STREAM_TRUNCATED_PREMATURELY )
204
205
if tok .isspace ():
205
206
break
206
207
idnum += tok
207
208
generation = b_ ("" )
208
209
while True :
209
210
tok = stream .read (1 )
210
211
if not tok :
211
- # stream has truncated prematurely
212
- raise PdfStreamError ("Stream has ended unexpectedly" )
212
+ raise PdfStreamError (ERR_STREAM_TRUNCATED_PREMATURELY )
213
213
if tok .isspace ():
214
214
if not generation :
215
215
continue
@@ -273,10 +273,11 @@ def readFromStream(stream):
273
273
readFromStream = staticmethod (readFromStream )
274
274
275
275
276
- ##
277
- # Given a string (either a "str" or "unicode"), create a ByteStringObject or a
278
- # TextStringObject to represent the string.
279
276
def createStringObject (string ):
277
+ """
278
+ Given a string (either a "str" or "unicode"), create a ByteStringObject or a
279
+ TextStringObject to represent the string.
280
+ """
280
281
if isinstance (string , utils .string_type ):
281
282
return TextStringObject (string )
282
283
elif isinstance (string , utils .bytes_type ):
@@ -306,8 +307,7 @@ def readHexStringFromStream(stream):
306
307
while True :
307
308
tok = readNonWhitespace (stream )
308
309
if not tok :
309
- # stream has truncated prematurely
310
- raise PdfStreamError ("Stream has ended unexpectedly" )
310
+ raise PdfStreamError (ERR_STREAM_TRUNCATED_PREMATURELY )
311
311
if tok == b_ (">" ):
312
312
break
313
313
x += tok
@@ -328,8 +328,7 @@ def readStringFromStream(stream):
328
328
while True :
329
329
tok = stream .read (1 )
330
330
if not tok :
331
- # stream has truncated prematurely
332
- raise PdfStreamError ("Stream has ended unexpectedly" )
331
+ raise PdfStreamError (ERR_STREAM_TRUNCATED_PREMATURELY )
333
332
if tok == b_ ("(" ):
334
333
parens += 1
335
334
elif tok == b_ (")" ):
@@ -392,16 +391,17 @@ def readStringFromStream(stream):
392
391
return createStringObject (txt )
393
392
394
393
395
- ##
396
- # Represents a string object where the text encoding could not be determined.
397
- # This occurs quite often, as the PDF spec doesn't provide an alternate way to
398
- # represent strings -- for example, the encryption data stored in files (like
399
- # /O) is clearly not text, but is still stored in a "String" object.
400
394
class ByteStringObject (utils .bytes_type , PdfObject ):
395
+ """
396
+ Represents a string object where the text encoding could not be determined.
397
+ This occurs quite often, as the PDF spec doesn't provide an alternate way to
398
+ represent strings -- for example, the encryption data stored in files (like
399
+ /O) is clearly not text, but is still stored in a "String" object.
400
+ """
401
401
402
402
##
403
403
# For compatibility with TextStringObject.original_bytes. This method
404
- # returns self.
404
+ # self.
405
405
original_bytes = property (lambda self : self )
406
406
407
407
def writeToStream (self , stream , encryption_key ):
@@ -413,12 +413,14 @@ def writeToStream(self, stream, encryption_key):
413
413
stream .write (b_ (">" ))
414
414
415
415
416
- ##
417
- # Represents a string object that has been decoded into a real unicode string.
418
- # If read from a PDF document, this string appeared to match the
419
- # PDFDocEncoding, or contained a UTF-16BE BOM mark to cause UTF-16 decoding to
420
- # occur.
421
416
class TextStringObject (utils .string_type , PdfObject ):
417
+ """
418
+ Represents a string object that has been decoded into a real unicode string.
419
+ If read from a PDF document, this string appeared to match the
420
+ PDFDocEncoding, or contained a UTF-16BE BOM mark to cause UTF-16 decoding to
421
+ occur.
422
+ """
423
+
422
424
autodetect_pdfdocencoding = False
423
425
autodetect_utf16 = False
424
426
@@ -569,8 +571,7 @@ def readFromStream(stream, pdf):
569
571
skipOverComment (stream )
570
572
continue
571
573
if not tok :
572
- # stream has truncated prematurely
573
- raise PdfStreamError ("Stream has ended unexpectedly" )
574
+ raise PdfStreamError (ERR_STREAM_TRUNCATED_PREMATURELY )
574
575
575
576
if debug : print (("Tok:" , tok ))
576
577
if tok == b_ (">" ):
0 commit comments