21
21
# input. The resulting code (xx 90 90) would appear to be interpreted as an
22
22
# escaped *value* of 0x90. All coders I've seen appear to ignore this nicety...
23
23
#
24
+ import binascii
25
+ import contextlib
24
26
import io
25
27
import os
26
28
import struct
27
- import binascii
29
+ import warnings
30
+
31
+ warnings .warn ('the binhex module is deprecated' , DeprecationWarning ,
32
+ stacklevel = 2 )
33
+
28
34
29
35
__all__ = ["binhex" ,"hexbin" ,"Error" ]
30
36
@@ -76,6 +82,16 @@ def write(self, *args):
76
82
def close (self ):
77
83
pass
78
84
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
+
79
95
class _Hqxcoderengine :
80
96
"""Write data to the coder in 3-byte chunks"""
81
97
@@ -93,7 +109,8 @@ def write(self, data):
93
109
self .data = self .data [todo :]
94
110
if not data :
95
111
return
96
- self .hqxdata = self .hqxdata + binascii .b2a_hqx (data )
112
+ with _ignore_deprecation_warning ():
113
+ self .hqxdata = self .hqxdata + binascii .b2a_hqx (data )
97
114
self ._flush (0 )
98
115
99
116
def _flush (self , force ):
@@ -109,7 +126,8 @@ def _flush(self, force):
109
126
110
127
def close (self ):
111
128
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 )
113
131
self ._flush (1 )
114
132
self .ofp .close ()
115
133
del self .ofp
@@ -125,13 +143,15 @@ def write(self, data):
125
143
self .data = self .data + data
126
144
if len (self .data ) < REASONABLY_LARGE :
127
145
return
128
- rledata = binascii .rlecode_hqx (self .data )
146
+ with _ignore_deprecation_warning ():
147
+ rledata = binascii .rlecode_hqx (self .data )
129
148
self .ofp .write (rledata )
130
149
self .data = b''
131
150
132
151
def close (self ):
133
152
if self .data :
134
- rledata = binascii .rlecode_hqx (self .data )
153
+ with _ignore_deprecation_warning ():
154
+ rledata = binascii .rlecode_hqx (self .data )
135
155
self .ofp .write (rledata )
136
156
self .ofp .close ()
137
157
del self .ofp
@@ -180,7 +200,8 @@ def _writeinfo(self, name, finfo):
180
200
self ._writecrc ()
181
201
182
202
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 )
184
205
self .ofp .write (data )
185
206
186
207
def _writecrc (self ):
@@ -276,7 +297,8 @@ def read(self, totalwtd):
276
297
#
277
298
while True :
278
299
try :
279
- decdatacur , self .eof = binascii .a2b_hqx (data )
300
+ with _ignore_deprecation_warning ():
301
+ decdatacur , self .eof = binascii .a2b_hqx (data )
280
302
break
281
303
except binascii .Incomplete :
282
304
pass
@@ -312,8 +334,9 @@ def read(self, wtd):
312
334
def _fill (self , wtd ):
313
335
self .pre_buffer = self .pre_buffer + self .ifp .read (wtd + 4 )
314
336
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 )
317
340
self .pre_buffer = b''
318
341
return
319
342
@@ -340,8 +363,9 @@ def _fill(self, wtd):
340
363
else :
341
364
mark = mark - 1
342
365
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 ])
345
369
self .pre_buffer = self .pre_buffer [mark :]
346
370
347
371
def close (self ):
@@ -372,7 +396,8 @@ def __init__(self, ifp):
372
396
373
397
def _read (self , len ):
374
398
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 )
376
401
return data
377
402
378
403
def _checkcrc (self ):
0 commit comments