MCPcopy Index your code
hub / github.com/python/cpython / open

Function open

Lib/dbm/__init__.py:53–95  ·  view source on GitHub ↗

Open or create database at path given by *file*. Optional argument *flag* can be 'r' (default) for read-only access, 'w' for read-write access of an existing database, 'c' for read-write access to a new or existing database, and 'n' for read-write access to a new database. Note

(file, flag='r', mode=0o666)

Source from the content-addressed store, hash-verified

51
52
53def open(file, flag='r', mode=0o666):
54 """Open or create database at path given by *file*.
55
56 Optional argument *flag* can be 'r' (default) for read-only access, 'w'
57 for read-write access of an existing database, 'c' for read-write access
58 to a new or existing database, and 'n' for read-write access to a new
59 database.
60
61 Note: 'r' and 'w' fail if the database doesn't exist; 'c' creates it
62 only if it doesn't exist; and 'n' always creates a new database.
63 """
64 global _defaultmod
65 if _defaultmod is None:
66 for name in _names:
67 try:
68 mod = __import__(name, fromlist=['open'])
69 except ImportError:
70 continue
71 if not _defaultmod:
72 _defaultmod = mod
73 _modules[name] = mod
74 if not _defaultmod:
75 raise ImportError("no dbm clone found; tried %s" % _names)
76
77 # guess the type of an existing database, if not creating a new one
78 result = whichdb(file) if 'n' not in flag else None
79 if result is None:
80 # db doesn't exist or 'n' flag was specified to create a new db
81 if 'c' in flag or 'n' in flag:
82 # file doesn't exist and the new flag was used so use default type
83 mod = _defaultmod
84 else:
85 raise error[0]("db file doesn't exist; "
86 "use 'c' or 'n' flag to create a new db")
87 elif result == "":
88 # db type cannot be determined
89 raise error[0]("db type could not be determined")
90 elif result not in _modules:
91 raise error[0]("db type is {0}, but the module is not "
92 "available".format(result))
93 else:
94 mod = _modules[result]
95 return mod.open(file, flag, mode)
96
97
98def whichdb(filename):

Callers

nothing calls this directly

Calls 4

__import__Function · 0.85
whichdbFunction · 0.85
formatMethod · 0.45
openMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…