Skip to content

Commit b4b2588

Browse files
committed
Do not use dunder prefix for attribute names
Instance attribute names using a dunder ('__') prefix can make debugging more difficult due to how Python mangles such names at runtime. And as used in stgit, these dunder prefixed attributes do not fit the nominal use case for dunder prefixes to help with name clashes between super and sub classes: https://docs.python.org/3/reference/lexical_analysis.html#reserved-classes-of-identifiers Some instance attributes with dunder prefixes are renamed to use a single '_'. This still signals to readers that the attribute is private, but allows the variable to accessed with less trouble in a debug context. Some other instance attributes with dunder prefixes that serve to back properties are replaced along with the property with regular (non-property) attributes. Signed-off-by: Peter Grayson <[email protected]>
1 parent 6285841 commit b4b2588

File tree

19 files changed

+666
-775
lines changed

19 files changed

+666
-775
lines changed

stgit/commands/branch.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@
181181

182182

183183
def __is_current_branch(branch_name):
184-
return crt_series.get_name() == branch_name
184+
return crt_series.name == branch_name
185185

186186

187187
def __print_branch(branch_name, length):
@@ -295,7 +295,7 @@ def func(parser, options, args):
295295
elif options.clone:
296296

297297
if len(args) == 0:
298-
clone = crt_series.get_name() + time.strftime('-%C%y%m%d-%H%M%S')
298+
clone = crt_series.name + time.strftime('-%C%y%m%d-%H%M%S')
299299
elif len(args) == 1:
300300
clone = args[0]
301301
else:
@@ -309,7 +309,7 @@ def func(parser, options, args):
309309
crt_series.clone(clone)
310310
out.done()
311311

312-
log.copy_log(log.default_repo(), crt_series.get_name(), clone,
312+
log.copy_log(log.default_repo(), crt_series.name, clone,
313313
'branch --clone')
314314
return
315315

@@ -324,7 +324,7 @@ def func(parser, options, args):
324324
elif options.cleanup:
325325

326326
if not args:
327-
name = crt_series.get_name()
327+
name = crt_series.name
328328
elif len(args) == 1:
329329
name = args[0]
330330
else:
@@ -356,7 +356,7 @@ def func(parser, options, args):
356356
elif options.protect:
357357

358358
if len(args) == 0:
359-
branch_name = crt_series.get_name()
359+
branch_name = crt_series.name
360360
elif len(args) == 1:
361361
branch_name = args[0]
362362
else:
@@ -390,7 +390,7 @@ def func(parser, options, args):
390390
elif options.unprotect:
391391

392392
if len(args) == 0:
393-
branch_name = crt_series.get_name()
393+
branch_name = crt_series.name
394394
elif len(args) == 1:
395395
branch_name = args[0]
396396
else:
@@ -410,7 +410,7 @@ def func(parser, options, args):
410410
elif options.description is not None:
411411

412412
if len(args) == 0:
413-
branch_name = crt_series.get_name()
413+
branch_name = crt_series.name
414414
elif len(args) == 1:
415415
branch_name = args[0]
416416
else:
@@ -445,4 +445,4 @@ def func(parser, options, args):
445445
if len(args) != 0:
446446
parser.error('incorrect number of arguments')
447447

448-
out.stdout(crt_series.get_name())
448+
out.stdout(crt_series.name)

stgit/commands/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def git_id(crt_series, rev):
7777
# TODO: remove this function once all the occurrences were converted
7878
# to git_commit()
7979
repository = StackRepository.default()
80-
return git_commit(rev, repository, crt_series.get_name()).sha1
80+
return git_commit(rev, repository, crt_series.name).sha1
8181

8282

8383
def get_public_ref(branch_name):

stgit/commands/repair.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,13 @@ def __init__(self, id):
9191
self.parents = set()
9292
self.children = set()
9393
self.patch = None
94-
self.__commit = None
94+
self._commit = None
9595

96-
def __get_commit(self):
97-
if not self.__commit:
98-
self.__commit = git.get_commit(self.id)
99-
return self.__commit
100-
commit = property(__get_commit)
96+
@property
97+
def commit(self):
98+
if not self._commit:
99+
self._commit = git.get_commit(self.id)
100+
return self._commit
101101

102102
def __str__(self):
103103
if self.patch:
@@ -147,7 +147,7 @@ def func(parser, options, args):
147147

148148
# Find commits that aren't patches, and applied patches.
149149
head = git.get_commit(git.get_head()).get_id_hash()
150-
commits, patches = read_commit_dag(crt_series.get_name())
150+
commits, patches = read_commit_dag(crt_series.name)
151151
c = commits[head]
152152
patchify = [] # commits to definitely patchify
153153
maybe_patchify = [] # commits to patchify if we find a patch below them

stgit/commands/sync.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ def func(parser, options, args):
100100
"""
101101
if options.ref_branch:
102102
remote_series = stack.Series(options.ref_branch)
103-
if options.ref_branch == crt_series.get_name():
103+
if options.ref_branch == crt_series.name:
104104
raise CmdException('Cannot synchronise with the current branch')
105105
remote_patches = remote_series.get_applied()
106106

stgit/config.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,13 @@ class GitConfigException(StgException):
5555

5656
class GitConfig(object):
5757

58-
__cache = None
58+
_cache = None
5959

6060
def load(self):
61-
"""Load the whole configuration in __cache unless it has been
62-
done already."""
63-
if self.__cache is not None:
61+
"""Load the configuration in _cache unless it has been done already."""
62+
if self._cache is not None:
6463
return
65-
self.__cache = DEFAULTS.copy()
64+
self._cache = DEFAULTS.copy()
6665
lines = Run('git', 'config', '--null', '--list'
6766
).discard_exitcode().output_lines('\0')
6867
for line in lines:
@@ -71,19 +70,19 @@ def load(self):
7170
except ValueError:
7271
key = line
7372
value = None
74-
self.__cache.setdefault(key, []).append(value)
73+
self._cache.setdefault(key, []).append(value)
7574

7675
def get(self, name):
7776
self.load()
7877
try:
79-
return self.__cache[name][-1]
78+
return self._cache[name][-1]
8079
except KeyError:
8180
return None
8281

8382
def getall(self, name):
8483
self.load()
8584
try:
86-
return self.__cache[name]
85+
return self._cache[name]
8786
except KeyError:
8887
return []
8988

@@ -105,7 +104,7 @@ def getbool(self, name):
105104
# reported as "true".
106105
self.load()
107106
try:
108-
value = self.__cache[name][-1]
107+
value = self._cache[name][-1]
109108
except KeyError:
110109
return None
111110
if value is None:
@@ -123,30 +122,30 @@ def getbool(self, name):
123122

124123
def getstartswith(self, name):
125124
self.load()
126-
return ((n, v[-1]) for (n, v) in self.__cache.items()
125+
return ((n, v[-1]) for (n, v) in self._cache.items()
127126
if n.startswith(name))
128127

129128
def rename_section(self, from_name, to_name):
130129
"""Rename a section in the config file. Silently do nothing if
131130
the section doesn't exist."""
132131
Run('git', 'config', '--rename-section', from_name, to_name
133132
).returns([0, 1, 128]).run()
134-
self.__cache.clear()
133+
self._cache.clear()
135134

136135
def remove_section(self, name):
137136
"""Remove a section in the config file. Silently do nothing if
138137
the section doesn't exist."""
139138
Run('git', 'config', '--remove-section', name
140139
).returns([0, 1, 128]).discard_stderr().discard_output()
141-
self.__cache.clear()
140+
self._cache.clear()
142141

143142
def set(self, name, value):
144143
Run('git', 'config', name, value).run()
145-
self.__cache[name] = value
144+
self._cache[name] = value
146145

147146
def unset(self, name):
148147
Run('git', 'config', '--unset', name).run()
149-
self.__cache[name] = [None]
148+
self._cache[name] = [None]
150149

151150
def sections_matching(self, regexp):
152151
"""Takes a regexp with a single group, matches it against all

stgit/git.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class Commit(object):
107107
"""
108108

109109
def __init__(self, id_hash):
110-
self.__id_hash = id_hash
110+
self._id_hash = id_hash
111111

112112
lines = GRun('cat-file', 'commit', id_hash).output_lines()
113113
for i in range(len(lines)):
@@ -116,20 +116,20 @@ def __init__(self, id_hash):
116116
break # we've seen all the header fields
117117
key, val = line.split(' ', 1)
118118
if key == 'tree':
119-
self.__tree = val
119+
self._tree = val
120120
elif key == 'author':
121-
self.__author = val
121+
self._author = val
122122
elif key == 'committer':
123-
self.__committer = val
123+
self._committer = val
124124
else:
125125
pass # ignore other headers
126-
self.__log = '\n'.join(lines[i + 1:])
126+
self._log = '\n'.join(lines[i + 1:])
127127

128128
def get_id_hash(self):
129-
return self.__id_hash
129+
return self._id_hash
130130

131131
def get_tree(self):
132-
return self.__tree
132+
return self._tree
133133

134134
def get_parent(self):
135135
parents = self.get_parents()
@@ -139,17 +139,17 @@ def get_parent(self):
139139
return None
140140

141141
def get_parents(self):
142-
return GRun('rev-list', '--parents', '--max-count=1', self.__id_hash
142+
return GRun('rev-list', '--parents', '--max-count=1', self._id_hash
143143
).output_one_line().split()[1:]
144144

145145
def get_author(self):
146-
return self.__author
146+
return self._author
147147

148148
def get_committer(self):
149-
return self.__committer
149+
return self._committer
150150

151151
def get_log(self):
152-
return self.__log
152+
return self._log
153153

154154
def __str__(self):
155155
return self.get_id_hash()

stgit/lib/git/branch.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,52 +18,48 @@ class Branch(object):
1818
"""Represents a Git branch."""
1919

2020
def __init__(self, repository, name):
21-
self.__repository = repository
22-
self.__name = name
21+
self.repository = repository
22+
self.name = name
2323
try:
2424
self.head
2525
except KeyError:
2626
raise BranchException('%s: no such branch' % name)
2727

2828
@property
29-
def name(self):
30-
return self.__name
29+
def _ref(self):
30+
return 'refs/heads/%s' % self.name
3131

3232
@property
33-
def repository(self):
34-
return self.__repository
35-
36-
def __ref(self):
37-
return 'refs/heads/%s' % self.__name
33+
def _remote_key(self):
34+
return 'branch.%s.remote' % self.name
3835

3936
@property
4037
def head(self):
41-
return self.__repository.refs.get(self.__ref())
38+
return self.repository.refs.get(self._ref)
4239

4340
def set_head(self, commit, msg):
44-
self.__repository.refs.set(self.__ref(), commit, msg)
41+
self.repository.refs.set(self._ref, commit, msg)
4542

4643
@property
4744
def parent_remote(self):
48-
remote_key = 'branch.%s.remote' % self.__name
49-
remote = config.get(remote_key)
45+
remote = config.get(self._remote_key)
5046
if remote is None:
5147
raise BranchException(
5248
'%s: no parent remote; consider configuring "%s"' % (
53-
self.__name, remote_key
49+
self.name, self._remote_key
5450
)
5551
)
5652
else:
5753
return remote
5854

5955
def set_parent_remote(self, name):
60-
config.set('branch.%s.remote' % self.__name, name)
56+
config.set('branch.%s.remote' % self.name, name)
6157

6258
def set_parent_branch(self, name):
63-
if config.get('branch.%s.remote' % self.__name):
59+
if config.get(self._remote_key):
6460
# Never set merge if remote is not set to avoid
6561
# possibly-erroneous lookups into 'origin'
66-
config.set('branch.%s.merge' % self.__name, name)
62+
config.set('branch.%s.merge' % self.name, name)
6763

6864
@classmethod
6965
def create(cls, repository, name, create_at=None):

0 commit comments

Comments
 (0)