MCPcopy Index your code
hub / github.com/RustPython/RustPython / _Database

Class _Database

Lib/dbm/dumb.py:35–290  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

33error = OSError
34
35class _Database(collections.abc.MutableMapping):
36
37 # The on-disk directory and data files can remain in mutually
38 # inconsistent states for an arbitrarily long time (see comments
39 # at the end of __setitem__). This is only repaired when _commit()
40 # gets called. One place _commit() gets called is from __del__(),
41 # and if that occurs at program shutdown time, module globals may
42 # already have gotten rebound to None. Since it's crucial that
43 # _commit() finish successfully, we can't ignore shutdown races
44 # here, and _commit() must not reference any globals.
45 _os = _os # for _commit()
46 _io = _io # for _commit()
47
48 def __init__(self, filebasename, mode, flag='c'):
49 filebasename = self._os.fsencode(filebasename)
50 self._mode = mode
51 self._readonly = (flag == 'r')
52
53 # The directory file is a text file. Each line looks like
54 # "%r, (%d, %d)\n" % (key, pos, siz)
55 # where key is the string key, pos is the offset into the dat
56 # file of the associated value's first byte, and siz is the number
57 # of bytes in the associated value.
58 self._dirfile = filebasename + b'.dir'
59
60 # The data file is a binary file pointed into by the directory
61 # file, and holds the values associated with keys. Each value
62 # begins at a _BLOCKSIZE-aligned byte offset, and is a raw
63 # binary 8-bit string value.
64 self._datfile = filebasename + b'.dat'
65 self._bakfile = filebasename + b'.bak'
66
67 # The index is an in-memory dict, mirroring the directory file.
68 self._index = None # maps keys to (pos, siz) pairs
69
70 # Handle the creation
71 self._create(flag)
72 self._update(flag)
73
74 def _create(self, flag):
75 if flag == 'n':
76 for filename in (self._datfile, self._bakfile, self._dirfile):
77 try:
78 _os.remove(filename)
79 except OSError:
80 pass
81 # Mod by Jack: create data file if needed
82 try:
83 f = _io.open(self._datfile, 'r', encoding="Latin-1")
84 except OSError:
85 if flag not in ('c', 'n'):
86 raise
87 with _io.open(self._datfile, 'w', encoding="Latin-1") as f:
88 self._chmod(self._datfile)
89 else:
90 f.close()
91
92 # Read directory file into the in-memory index dict.

Callers 1

openFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected