2121# input. The resulting code (xx 90 90) would appear to be interpreted as an
2222# escaped *value* of 0x90. All coders I've seen appear to ignore this nicety...
2323#
24+ import binascii
25+ import contextlib
2426import io
2527import os
2628import struct
27- import binascii
29+ import warnings
30+
31+ warnings .warn ('the binhex module is deprecated' , DeprecationWarning ,
32+ stacklevel = 2 )
33+
2834
2935__all__ = ["binhex" ,"hexbin" ,"Error" ]
3036
@@ -76,6 +82,16 @@ def write(self, *args):
7682 def close (self ):
7783 pass
7884
85+
86+ # DeprecationWarning is already emitted on "import binhex". There is no need
87+ # to repeat the warning at each call to deprecated binascii functions.
88+ @contextlib .contextmanager
89+ def _ignore_deprecation_warning ():
90+ with warnings .catch_warnings ():
91+ warnings .filterwarnings ('ignore' , '' , DeprecationWarning )
92+ yield
93+
94+
7995class _Hqxcoderengine :
8096 """Write data to the coder in 3-byte chunks"""
8197
@@ -93,7 +109,8 @@ def write(self, data):
93109 self .data = self .data [todo :]
94110 if not data :
95111 return
96- self .hqxdata = self .hqxdata + binascii .b2a_hqx (data )
112+ with _ignore_deprecation_warning ():
113+ self .hqxdata = self .hqxdata + binascii .b2a_hqx (data )
97114 self ._flush (0 )
98115
99116 def _flush (self , force ):
@@ -109,7 +126,8 @@ def _flush(self, force):
109126
110127 def close (self ):
111128 if self .data :
112- self .hqxdata = self .hqxdata + binascii .b2a_hqx (self .data )
129+ with _ignore_deprecation_warning ():
130+ self .hqxdata = self .hqxdata + binascii .b2a_hqx (self .data )
113131 self ._flush (1 )
114132 self .ofp .close ()
115133 del self .ofp
@@ -125,13 +143,15 @@ def write(self, data):
125143 self .data = self .data + data
126144 if len (self .data ) < REASONABLY_LARGE :
127145 return
128- rledata = binascii .rlecode_hqx (self .data )
146+ with _ignore_deprecation_warning ():
147+ rledata = binascii .rlecode_hqx (self .data )
129148 self .ofp .write (rledata )
130149 self .data = b''
131150
132151 def close (self ):
133152 if self .data :
134- rledata = binascii .rlecode_hqx (self .data )
153+ with _ignore_deprecation_warning ():
154+ rledata = binascii .rlecode_hqx (self .data )
135155 self .ofp .write (rledata )
136156 self .ofp .close ()
137157 del self .ofp
@@ -180,7 +200,8 @@ def _writeinfo(self, name, finfo):
180200 self ._writecrc ()
181201
182202 def _write (self , data ):
183- self .crc = binascii .crc_hqx (data , self .crc )
203+ with _ignore_deprecation_warning ():
204+ self .crc = binascii .crc_hqx (data , self .crc )
184205 self .ofp .write (data )
185206
186207 def _writecrc (self ):
@@ -276,7 +297,8 @@ def read(self, totalwtd):
276297 #
277298 while True :
278299 try :
279- decdatacur , self .eof = binascii .a2b_hqx (data )
300+ with _ignore_deprecation_warning ():
301+ decdatacur , self .eof = binascii .a2b_hqx (data )
280302 break
281303 except binascii .Incomplete :
282304 pass
@@ -312,8 +334,9 @@ def read(self, wtd):
312334 def _fill (self , wtd ):
313335 self .pre_buffer = self .pre_buffer + self .ifp .read (wtd + 4 )
314336 if self .ifp .eof :
315- self .post_buffer = self .post_buffer + \
316- binascii .rledecode_hqx (self .pre_buffer )
337+ with _ignore_deprecation_warning ():
338+ self .post_buffer = self .post_buffer + \
339+ binascii .rledecode_hqx (self .pre_buffer )
317340 self .pre_buffer = b''
318341 return
319342
@@ -340,8 +363,9 @@ def _fill(self, wtd):
340363 else :
341364 mark = mark - 1
342365
343- self .post_buffer = self .post_buffer + \
344- binascii .rledecode_hqx (self .pre_buffer [:mark ])
366+ with _ignore_deprecation_warning ():
367+ self .post_buffer = self .post_buffer + \
368+ binascii .rledecode_hqx (self .pre_buffer [:mark ])
345369 self .pre_buffer = self .pre_buffer [mark :]
346370
347371 def close (self ):
@@ -372,7 +396,8 @@ def __init__(self, ifp):
372396
373397 def _read (self , len ):
374398 data = self .ifp .read (len )
375- self .crc = binascii .crc_hqx (data , self .crc )
399+ with _ignore_deprecation_warning ():
400+ self .crc = binascii .crc_hqx (data , self .crc )
376401 return data
377402
378403 def _checkcrc (self ):
0 commit comments