Mercurial > cpython-lru_cache-weakref
annotate Lib/base64.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 | 3716b116fb31 |
| children | 3cc0d8fd4e2b |
| rev | line source |
|---|---|
| 5243 | 1 #! /usr/bin/env python |
| 2 | |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
3 """Conversions to/from base64 transport encoding as per RFC-1521.""" |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
4 |
|
3145
43f8db23ef15
Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents:
3104
diff
changeset
|
5 # Modified 04-Oct-95 by Jack to use binascii module |
| 2742 | 6 |
|
3145
43f8db23ef15
Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents:
3104
diff
changeset
|
7 import binascii |
| 2742 | 8 |
|
16070
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
13710
diff
changeset
|
9 __all__ = ["encode","decode","encodestring","decodestring"] |
|
b5345e99596b
added __all__ lists to a number of Python modules
Skip Montanaro <skip@pobox.com>
parents:
13710
diff
changeset
|
10 |
|
3145
43f8db23ef15
Use binascii module (resulting in a 60-fold speedup:-)
Jack Jansen <jack.jansen@cwi.nl>
parents:
3104
diff
changeset
|
11 MAXLINESIZE = 76 # Excluding the CRLF |
|
19694
3716b116fb31
The first batch of changes recommended by the fixdiv tool. These are
Guido van Rossum <guido@python.org>
parents:
18053
diff
changeset
|
12 MAXBINSIZE = (MAXLINESIZE//4)*3 |
| 2742 | 13 |
| 14 def encode(input, output): | |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
15 """Encode a file.""" |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
16 while 1: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
17 s = input.read(MAXBINSIZE) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
18 if not s: break |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
19 while len(s) < MAXBINSIZE: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
20 ns = input.read(MAXBINSIZE-len(s)) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
21 if not ns: break |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
22 s = s + ns |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
23 line = binascii.b2a_base64(s) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
24 output.write(line) |
| 2742 | 25 |
| 26 def decode(input, output): | |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
27 """Decode a file.""" |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
28 while 1: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
29 line = input.readline() |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
30 if not line: break |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
31 s = binascii.a2b_base64(line) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
32 output.write(s) |
| 2742 | 33 |
| 34 def encodestring(s): | |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
35 """Encode a string.""" |
|
18053
2b017b2c8f7c
check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
16070
diff
changeset
|
36 pieces = [] |
|
2b017b2c8f7c
check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
16070
diff
changeset
|
37 for i in range(0, len(s), MAXBINSIZE): |
|
2b017b2c8f7c
check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
16070
diff
changeset
|
38 chunk = s[i : i + MAXBINSIZE] |
|
2b017b2c8f7c
check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
16070
diff
changeset
|
39 pieces.append(binascii.b2a_base64(chunk)) |
|
2b017b2c8f7c
check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
16070
diff
changeset
|
40 return "".join(pieces) |
| 2742 | 41 |
| 42 def decodestring(s): | |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
43 """Decode a string.""" |
|
18053
2b017b2c8f7c
check in for patch #430846
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
16070
diff
changeset
|
44 return binascii.a2b_base64(s) |
| 2742 | 45 |
| 46 def test(): | |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
47 """Small test program""" |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
48 import sys, getopt |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
49 try: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
50 opts, args = getopt.getopt(sys.argv[1:], 'deut') |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
51 except getopt.error, msg: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
52 sys.stdout = sys.stderr |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
53 print msg |
|
13710
697c88cc4714
a couple of nits from Patch #100933
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
54 print """usage: %s [-d|-e|-u|-t] [file|-] |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
55 -d, -u: decode |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
56 -e: encode (default) |
|
13710
697c88cc4714
a couple of nits from Patch #100933
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
11691
diff
changeset
|
57 -t: encode and decode string 'Aladdin:open sesame'"""%sys.argv[0] |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
58 sys.exit(2) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
59 func = encode |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
60 for o, a in opts: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
61 if o == '-e': func = encode |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
62 if o == '-d': func = decode |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
63 if o == '-u': func = decode |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
64 if o == '-t': test1(); return |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
65 if args and args[0] != '-': |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
66 func(open(args[0], 'rb'), sys.stdout) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
67 else: |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
68 func(sys.stdin, sys.stdout) |
| 2742 | 69 |
| 70 def test1(): | |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
71 s0 = "Aladdin:open sesame" |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
72 s1 = encodestring(s0) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
73 s2 = decodestring(s1) |
|
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
74 print s0, `s1`, s2 |
| 2742 | 75 |
| 76 if __name__ == '__main__': | |
|
11691
e34991022571
Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents:
11642
diff
changeset
|
77 test() |
