Mercurial > cpython-lru_cache-weakref
annotate Lib/uu.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | a48a3d75475e |
| children | 9fd1354da6db 65cf478950ff |
| rev | line source |
|---|---|
|
5772
5f7ddb7ca347
Added /usr/bin/env python and made executable.
Guido van Rossum <guido@python.org>
parents:
5274
diff
changeset
|
1 #! /usr/bin/env python |
|
5f7ddb7ca347
Added /usr/bin/env python and made executable.
Guido van Rossum <guido@python.org>
parents:
5274
diff
changeset
|
2 |
|
1872
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
3 # Copyright 1994 by Lance Ellinghouse |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 # Cathedral City, California Republic, United States of America. |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 # All Rights Reserved |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15799
diff
changeset
|
6 # Permission to use, copy, modify, and distribute this software and its |
|
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15799
diff
changeset
|
7 # documentation for any purpose and without fee is hereby granted, |
|
1872
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
8 # provided that the above copyright notice appear in all copies and that |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15799
diff
changeset
|
9 # both that copyright notice and this permission notice appear in |
|
1872
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
10 # supporting documentation, and that the name of Lance Ellinghouse |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15799
diff
changeset
|
11 # not be used in advertising or publicity pertaining to distribution |
|
1872
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
12 # of the software without specific, written prior permission. |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
13 # LANCE ELLINGHOUSE DISCLAIMS ALL WARRANTIES WITH REGARD TO |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
14 # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
15 # FITNESS, IN NO EVENT SHALL LANCE ELLINGHOUSE CENTRUM BE LIABLE |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
17 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
18 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
19 # OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
2915
4509dfe19b68
Use binascii module for inner loop
Jack Jansen <jack.jansen@cwi.nl>
parents:
1872
diff
changeset
|
20 # |
|
4509dfe19b68
Use binascii module for inner loop
Jack Jansen <jack.jansen@cwi.nl>
parents:
1872
diff
changeset
|
21 # Modified by Jack Jansen, CWI, July 1995: |
|
4509dfe19b68
Use binascii module for inner loop
Jack Jansen <jack.jansen@cwi.nl>
parents:
1872
diff
changeset
|
22 # - Use binascii module to do the actual line-by-line conversion |
|
4509dfe19b68
Use binascii module for inner loop
Jack Jansen <jack.jansen@cwi.nl>
parents:
1872
diff
changeset
|
23 # between ascii and binary. This results in a 1000-fold speedup. The C |
|
4509dfe19b68
Use binascii module for inner loop
Jack Jansen <jack.jansen@cwi.nl>
parents:
1872
diff
changeset
|
24 # version is still 5 times faster, though. |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
25 # - Arguments more compliant with python standard |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
9772
diff
changeset
|
26 |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
9772
diff
changeset
|
27 """Implementation of the UUencode and UUdecode functions. |
|
1872
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
28 |
|
11707
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
9772
diff
changeset
|
29 encode(in_file, out_file [,name, mode]) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
9772
diff
changeset
|
30 decode(in_file [, out_file, mode]) |
|
6f4146846337
The third and final doc-string sweep by Ka-Ping Yee.
Guido van Rossum <guido@python.org>
parents:
9772
diff
changeset
|
31 """ |
|
1872
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 |
|
2915
4509dfe19b68
Use binascii module for inner loop
Jack Jansen <jack.jansen@cwi.nl>
parents:
1872
diff
changeset
|
33 import binascii |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
34 import os |
|
9432
edf7553fb20e
Move 'import sys' to top of module, as noted by Tim Peters.
Guido van Rossum <guido@python.org>
parents:
7755
diff
changeset
|
35 import sys |
|
19346
a48a3d75475e
decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents:
18469
diff
changeset
|
36 from types import StringType |
|
1872
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
37 |
|
16889
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
15874
diff
changeset
|
38 __all__ = ["Error", "encode", "decode"] |
|
0feb63490093
final round of __all__ lists (I hope) - skipped urllib2 because Moshe may be
Skip Montanaro <skip@pobox.com>
parents:
15874
diff
changeset
|
39 |
|
14021
5b34b70ecf22
Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents:
11707
diff
changeset
|
40 class Error(Exception): |
|
5b34b70ecf22
Convert some old-style string exceptions to class exceptions.
Fred Drake <fdrake@acm.org>
parents:
11707
diff
changeset
|
41 pass |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
42 |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
43 def encode(in_file, out_file, name=None, mode=None): |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
44 """Uuencode file""" |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
45 # |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
46 # If in_file is a pathname open it and change defaults |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
47 # |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
48 if in_file == '-': |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
49 in_file = sys.stdin |
|
19346
a48a3d75475e
decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents:
18469
diff
changeset
|
50 elif isinstance(in_file, StringType): |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
14021
diff
changeset
|
51 if name is None: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
52 name = os.path.basename(in_file) |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
14021
diff
changeset
|
53 if mode is None: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
54 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
55 mode = os.stat(in_file)[0] |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
56 except AttributeError: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
57 pass |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
58 in_file = open(in_file, 'rb') |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
59 # |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
60 # Open out_file if it is a pathname |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
61 # |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
62 if out_file == '-': |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
63 out_file = sys.stdout |
|
19346
a48a3d75475e
decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents:
18469
diff
changeset
|
64 elif isinstance(out_file, StringType): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
65 out_file = open(out_file, 'w') |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
66 # |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
67 # Set defaults for name and mode |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
68 # |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
14021
diff
changeset
|
69 if name is None: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
70 name = '-' |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
14021
diff
changeset
|
71 if mode is None: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
72 mode = 0666 |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
73 # |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
74 # Write the data |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
75 # |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
76 out_file.write('begin %o %s\n' % ((mode&0777),name)) |
|
1872
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
77 str = in_file.read(45) |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
78 while len(str) > 0: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
79 out_file.write(binascii.b2a_uu(str)) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
80 str = in_file.read(45) |
|
1872
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
81 out_file.write(' \nend\n') |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
82 |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
83 |
|
19346
a48a3d75475e
decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents:
18469
diff
changeset
|
84 def decode(in_file, out_file=None, mode=None, quiet=0): |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
85 """Decode uuencoded file""" |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
86 # |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
87 # Open the input file, if needed. |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
88 # |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
89 if in_file == '-': |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
90 in_file = sys.stdin |
|
19346
a48a3d75475e
decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents:
18469
diff
changeset
|
91 elif isinstance(in_file, StringType): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
92 in_file = open(in_file) |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
93 # |
|
5273
1148e7ebca35
Search for a valid begin line instead of requiring the first line.
Guido van Rossum <guido@python.org>
parents:
3143
diff
changeset
|
94 # Read until a begin is encountered or we've exhausted the file |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
95 # |
|
5274
0d6e70a5ab4b
while (1) -> while 1. Sorry.
Guido van Rossum <guido@python.org>
parents:
5273
diff
changeset
|
96 while 1: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
97 hdr = in_file.readline() |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
98 if not hdr: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
99 raise Error, 'No valid begin line found in input file' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
100 if hdr[:5] != 'begin': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
101 continue |
|
15799
5a308a9f99a5
Adapted version of SF Patch #103173 by pyretic: make uu.decode work
Guido van Rossum <guido@python.org>
parents:
15543
diff
changeset
|
102 hdrfields = hdr.split(" ", 2) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
103 if len(hdrfields) == 3 and hdrfields[0] == 'begin': |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
104 try: |
|
15799
5a308a9f99a5
Adapted version of SF Patch #103173 by pyretic: make uu.decode work
Guido van Rossum <guido@python.org>
parents:
15543
diff
changeset
|
105 int(hdrfields[1], 8) |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
106 break |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
107 except ValueError: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
108 pass |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
14021
diff
changeset
|
109 if out_file is None: |
|
15799
5a308a9f99a5
Adapted version of SF Patch #103173 by pyretic: make uu.decode work
Guido van Rossum <guido@python.org>
parents:
15543
diff
changeset
|
110 out_file = hdrfields[2].rstrip() |
|
19346
a48a3d75475e
decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents:
18469
diff
changeset
|
111 if os.path.exists(out_file): |
|
a48a3d75475e
decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents:
18469
diff
changeset
|
112 raise Error, 'Cannot overwrite existing file: %s' % out_file |
|
15543
785155e2cfca
Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents:
14021
diff
changeset
|
113 if mode is None: |
|
15799
5a308a9f99a5
Adapted version of SF Patch #103173 by pyretic: make uu.decode work
Guido van Rossum <guido@python.org>
parents:
15543
diff
changeset
|
114 mode = int(hdrfields[1], 8) |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
115 # |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
116 # Open the output file |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
117 # |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
118 if out_file == '-': |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
119 out_file = sys.stdout |
|
19346
a48a3d75475e
decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents:
18469
diff
changeset
|
120 elif isinstance(out_file, StringType): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
121 fp = open(out_file, 'wb') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
122 try: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
123 os.path.chmod(out_file, mode) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
124 except AttributeError: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
125 pass |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
126 out_file = fp |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
127 # |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
128 # Main decoding loop |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
129 # |
|
9772
8813627aac46
Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents:
9432
diff
changeset
|
130 s = in_file.readline() |
|
19346
a48a3d75475e
decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents:
18469
diff
changeset
|
131 while s and s.strip() != 'end': |
|
9772
8813627aac46
Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents:
9432
diff
changeset
|
132 try: |
|
8813627aac46
Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents:
9432
diff
changeset
|
133 data = binascii.a2b_uu(s) |
|
8813627aac46
Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents:
9432
diff
changeset
|
134 except binascii.Error, v: |
|
8813627aac46
Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents:
9432
diff
changeset
|
135 # Workaround for broken uuencoders by /Fredrik Lundh |
|
8813627aac46
Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents:
9432
diff
changeset
|
136 nbytes = (((ord(s[0])-32) & 63) * 4 + 5) / 3 |
|
8813627aac46
Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents:
9432
diff
changeset
|
137 data = binascii.a2b_uu(s[:nbytes]) |
|
19346
a48a3d75475e
decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents:
18469
diff
changeset
|
138 if not quiet: |
|
a48a3d75475e
decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents:
18469
diff
changeset
|
139 sys.stderr.write("Warning: %s\n" % str(v)) |
|
9772
8813627aac46
Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents:
9432
diff
changeset
|
140 out_file.write(data) |
|
8813627aac46
Incorporate fix suggested by /Fredrik Lundh in the newsgroup to cope
Guido van Rossum <guido@python.org>
parents:
9432
diff
changeset
|
141 s = in_file.readline() |
|
18469
6c501d892003
Backport Tim's checkin 1.17:
Thomas Wouters <thomas@python.org>
parents:
16889
diff
changeset
|
142 if not s: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
143 raise Error, 'Truncated input file' |
|
1872
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
144 |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
145 def test(): |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
146 """uuencode/uudecode main program""" |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
147 import getopt |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
148 |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
149 dopt = 0 |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
150 topt = 0 |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
151 input = sys.stdin |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
152 output = sys.stdout |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
153 ok = 1 |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
154 try: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
155 optlist, args = getopt.getopt(sys.argv[1:], 'dt') |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
156 except getopt.error: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
157 ok = 0 |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
158 if not ok or len(args) > 2: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
159 print 'Usage:', sys.argv[0], '[-d] [-t] [input [output]]' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
160 print ' -d: Decode (in stead of encode)' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
161 print ' -t: data is text, encoded format unix-compatible text' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
162 sys.exit(1) |
|
15874
4c01ee0a8073
Whitespace normalization. Top level of Lib now fixed-point for reindent.py!
Tim Peters <tim.peters@gmail.com>
parents:
15799
diff
changeset
|
163 |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
164 for o, a in optlist: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
165 if o == '-d': dopt = 1 |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
166 if o == '-t': topt = 1 |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
167 |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
168 if len(args) > 0: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
169 input = args[0] |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
170 if len(args) > 1: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
171 output = args[1] |
|
3032
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
172 |
|
3b3e68075833
Changed arguments and added a lot of functionality besides
Jack Jansen <jack.jansen@cwi.nl>
parents:
2915
diff
changeset
|
173 if dopt: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
174 if topt: |
|
19346
a48a3d75475e
decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents:
18469
diff
changeset
|
175 if isinstance(output, StringType): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
176 output = open(output, 'w') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
177 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
178 print sys.argv[0], ': cannot do -t to stdout' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
179 sys.exit(1) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
180 decode(input, output) |
|
1872
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
181 else: |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
182 if topt: |
|
19346
a48a3d75475e
decode(): Raise a uu.Error if no out_file is given but the file
Barry Warsaw <barry@python.org>
parents:
18469
diff
changeset
|
183 if isinstance(input, StringType): |
|
7755
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
184 input = open(input, 'r') |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
185 else: |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
186 print sys.argv[0], ': cannot do -t from stdin' |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
187 sys.exit(1) |
|
fb4b4d069cc7
Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents:
5772
diff
changeset
|
188 encode(input, output) |
|
1872
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
189 |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
190 if __name__ == '__main__': |
|
32e6c3a671d0
mhlib.py: delay opening of sequences file so we don't overwrite it when
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
191 test() |
