annotate Lib/whichdb.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 edd9d0b3c47e
children 062e342ccff5 bcc35a0f1a78
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 """Guess which db package to use to open a db file."""
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
16949
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16888
diff changeset
3 import os
09ba15ee1961 RISCOS changes by dschwertberger.
Guido van Rossum <guido@python.org>
parents: 16888
diff changeset
4
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 def whichdb(filename):
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 """Guess which db package to use to open a db file.
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 Return values:
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
9
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 - None if the database file can't be read;
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 - empty string if the file can be read but can't be recognized
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 - the module name (e.g. "dbm" or "gdbm") if recognized.
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
13
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 Importing the given module may still fail, and opening the
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 database using that module may still fail.
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 """
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
16888
fcfb442d7b34 move import into function to avoid having to add an __all__ list...
Skip Montanaro <skip@pobox.com>
parents: 13852
diff changeset
18 import struct
fcfb442d7b34 move import into function to avoid having to add an __all__ list...
Skip Montanaro <skip@pobox.com>
parents: 13852
diff changeset
19
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # Check for dbm first -- this has a .pag and a .dir file
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 try:
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 17055
diff changeset
22 f = open(filename + os.extsep + "pag", "rb")
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4932
diff changeset
23 f.close()
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 17055
diff changeset
24 f = open(filename + os.extsep + "dir", "rb")
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4932
diff changeset
25 f.close()
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4932
diff changeset
26 return "dbm"
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 except IOError:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4932
diff changeset
28 pass
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
13741
f65b86738d3b Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 11742
diff changeset
30 # Check for dumbdbm next -- this has a .dir and and a .dat file
13852
c480529807ab Add missing 'try:'. Patch by Rob W. W. Hooft, #101071 (closed.)
Thomas Wouters <thomas@python.org>
parents: 13741
diff changeset
31 try:
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 17055
diff changeset
32 f = open(filename + os.extsep + "dat", "rb")
13741
f65b86738d3b Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 11742
diff changeset
33 f.close()
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 17055
diff changeset
34 f = open(filename + os.extsep + "dir", "rb")
13741
f65b86738d3b Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 11742
diff changeset
35 try:
f65b86738d3b Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 11742
diff changeset
36 if f.read(1) in ["'", '"']:
f65b86738d3b Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 11742
diff changeset
37 return "dumbdbm"
f65b86738d3b Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 11742
diff changeset
38 finally:
f65b86738d3b Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 11742
diff changeset
39 f.close()
f65b86738d3b Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 11742
diff changeset
40 except IOError:
f65b86738d3b Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 11742
diff changeset
41 pass
f65b86738d3b Added support to recognize Python's internal "dumbdbm" database.
Moshe Zadka <moshez@math.huji.ac.il>
parents: 11742
diff changeset
42
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 # See if the file exists, return None if not
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4932
diff changeset
45 f = open(filename, "rb")
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 except IOError:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4932
diff changeset
47 return None
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
10933
cc6abc99d2dd Skip Montanaro:
Guido van Rossum <guido@python.org>
parents: 8172
diff changeset
49 # Read the start of the file -- the magic number
cc6abc99d2dd Skip Montanaro:
Guido van Rossum <guido@python.org>
parents: 8172
diff changeset
50 s16 = f.read(16)
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 f.close()
10933
cc6abc99d2dd Skip Montanaro:
Guido van Rossum <guido@python.org>
parents: 8172
diff changeset
52 s = s16[0:4]
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
53
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 # Return "" if not at least 4 bytes
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 if len(s) != 4:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4932
diff changeset
56 return ""
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 # Convert to 4-byte int in native byte order -- return "" if impossible
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 try:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4932
diff changeset
60 (magic,) = struct.unpack("=l", s)
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 except struct.error:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4932
diff changeset
62 return ""
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
63
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 # Check for GNU dbm
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 if magic == 0x13579ace:
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4932
diff changeset
66 return "gdbm"
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
68 # Check for BSD hash
8172
5732ec2487c5 Support byte-swapped dbhash (bsddb) files. Found by Ben Sayer.
Guido van Rossum <guido@python.org>
parents: 7755
diff changeset
69 if magic in (0x00061561, 0x61150600):
7755
fb4b4d069cc7 Mass check-in after untabifying all files that need it.
Guido van Rossum <guido@python.org>
parents: 4932
diff changeset
70 return "dbhash"
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
10933
cc6abc99d2dd Skip Montanaro:
Guido van Rossum <guido@python.org>
parents: 8172
diff changeset
72 # BSD hash v2 has a 12-byte NULL pad in front of the file type
cc6abc99d2dd Skip Montanaro:
Guido van Rossum <guido@python.org>
parents: 8172
diff changeset
73 try:
11742
840e18eec07f Untabify to pass the -tt test.
Fred Drake <fdrake@acm.org>
parents: 10933
diff changeset
74 (magic,) = struct.unpack("=l", s16[-4:])
10933
cc6abc99d2dd Skip Montanaro:
Guido van Rossum <guido@python.org>
parents: 8172
diff changeset
75 except struct.error:
cc6abc99d2dd Skip Montanaro:
Guido van Rossum <guido@python.org>
parents: 8172
diff changeset
76 return ""
cc6abc99d2dd Skip Montanaro:
Guido van Rossum <guido@python.org>
parents: 8172
diff changeset
77
cc6abc99d2dd Skip Montanaro:
Guido van Rossum <guido@python.org>
parents: 8172
diff changeset
78 # Check for BSD hash
cc6abc99d2dd Skip Montanaro:
Guido van Rossum <guido@python.org>
parents: 8172
diff changeset
79 if magic in (0x00061561, 0x61150600):
cc6abc99d2dd Skip Montanaro:
Guido van Rossum <guido@python.org>
parents: 8172
diff changeset
80 return "dbhash"
cc6abc99d2dd Skip Montanaro:
Guido van Rossum <guido@python.org>
parents: 8172
diff changeset
81
3825
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 # Unknown
c3ec69b1fe36 Function to guess which db package created a database.
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 return ""