/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
5225.2.9 by Martin Pool
Split out code style guide from HACKING
1
***********************
2
Bazaar Code Style Guide
3
***********************
4
5
Code layout
6
===========
7
8
Please write PEP-8__ compliant code.
9
10
__ http://www.python.org/peps/pep-0008.html
11
12
One often-missed requirement is that the first line of docstrings
13
should be a self-contained one-sentence summary.
14
15
We use 4 space indents for blocks, and never use tab characters.  (In vim,
16
``set expandtab``.)
17
18
Trailing white space should be avoided, but is allowed.
19
You should however not make lots of unrelated white space changes.
20
21
Unix style newlines (LF) are used.
22
23
Each file must have a newline at the end of it.
24
25
Lines should be no more than 79 characters if at all possible.
26
Lines that continue a long statement may be indented in either of
27
two ways:
28
29
within the parenthesis or other character that opens the block, e.g.::
30
31
    my_long_method(arg1,
32
                   arg2,
33
                   arg3)
34
35
or indented by four spaces::
36
37
    my_long_method(arg1,
38
        arg2,
39
        arg3)
40
41
The first is considered clearer by some people; however it can be a bit
42
harder to maintain (e.g. when the method name changes), and it does not
43
work well if the relevant parenthesis is already far to the right.  Avoid
44
this::
45
46
     self.legbone.kneebone.shinbone.toebone.shake_it(one,
47
                                                     two,
48
                                                     three)
49
50
but rather ::
51
52
     self.legbone.kneebone.shinbone.toebone.shake_it(one,
53
         two,
54
         three)
55
56
or ::
57
58
     self.legbone.kneebone.shinbone.toebone.shake_it(
59
         one, two, three)
60
61
For long lists, we like to add a trailing comma and put the closing
62
character on the following line.  This makes it easier to add new items in
63
future::
64
65
    from bzrlib.goo import (
66
        jam,
67
        jelly,
68
        marmalade,
69
        )
70
71
There should be spaces between function parameters, but not between the
72
keyword name and the value::
73
74
    call(1, 3, cheese=quark)
75
5225.2.10 by Martin Pool
More code style guidelines cleanups
76
5225.2.11 by Martin Pool
Style guide point about python versions
77
Python versions
78
===============
79
80
Bazaar supports Python from 2.4 through 2.6, and in the future we want to
81
support Python 3.0.  Avoid using language features added in 2.5 or 2.6, or
82
features deprecated in Python 3.0.  (You can check v3 compatibility using
83
the ``-3`` option of Python2.6.)
84
85
Specifically:
86
5274.3.1 by Andrew Bennetts
Expand 'Cleanup methods' section of coding style guide, and also correct some excessively indented bullets.
87
* Don't use the ``with`` statement.
88
89
* Don't ``from . import``.
90
91
* Don't use ``try/except/finally``, which is not supported in Python2.4,
92
  use separate nested ``try/except`` and ``try/finally`` blocks.
5225.2.11 by Martin Pool
Style guide point about python versions
93
94
5225.2.10 by Martin Pool
More code style guidelines cleanups
95
hasattr and getattr
96
===================
97
98
``hasattr`` should not be used because it swallows exceptions including
99
``KeyboardInterrupt``.  Instead, say something like ::
100
101
  if getattr(thing, 'name', None) is None
5225.2.9 by Martin Pool
Split out code style guide from HACKING
102
103
5418.2.1 by Martin Pool
Text about kwargs from spiv
104
kwargs
105
======
106
107
``**kwargs`` in the prototype of a function should be used sparingly.
108
109
It can be good on higher-order functions that decorate other functions,
110
such as ``addCleanup`` or ``assertRaises``.  Otherwise, be careful:
111
112
    If the parameters to a function are a bit complex and might vary over
113
    time (e.g. the commit API) then I think what's really called for is an
114
    explicit object for the parameters (e.g. I think we should create a
115
    CommitParams object), rather than a bag of positional and/or keyword
116
    args.  If you have an arbitrary set of keys and values that are
117
    different with each use (e.g.  string interpolation inputs) then again
118
    I think that should not be mixed in with the regular
119
    positional/keyword args, it seems like a different category of thing.
120
121
5418.2.3 by Martin Pool
Code guideline about imitating standard objects
122
Imitating standard objects
123
==========================
124
125
Don't provide methods that imitate built-in classes (eg ``__in__``,
126
``__call__``, ``__int__``, ``__getitem__``) unless the class you're
127
implementing really does act like the builtin class, in semantics and
128
performance.
129
130
For example, old code lets you say ``file_id in inv`` but we no longer
131
consider this good style.  Instead, say more explicitly
132
``inv.has_id(file_id)``.
133
134
``__repr__``, ``__cmp__``, ``__str__`` are usually fine.
135
136
5225.2.9 by Martin Pool
Split out code style guide from HACKING
137
Module Imports
138
==============
139
140
* Imports should be done at the top-level of the file, unless there is
141
  a strong reason to have them lazily loaded when a particular
142
  function runs.  Import statements have a cost, so try to make sure
143
  they don't run inside hot functions.
144
145
* Module names should always be given fully-qualified,
146
  i.e. ``bzrlib.hashcache`` not just ``hashcache``.
147
148
149
Naming
150
======
151
152
Functions, methods or members that are relatively private are given
153
a leading underscore prefix.  Names without a leading underscore are
154
public not just across modules but to programmers using bzrlib as an
155
API.
156
157
We prefer class names to be concatenated capital words (``TestCase``)
158
and variables, methods and functions to be lowercase words joined by
159
underscores (``revision_id``, ``get_revision``).
160
161
For the purposes of naming some names are treated as single compound
162
words: "filename", "revno".
163
164
Consider naming classes as nouns and functions/methods as verbs.
165
166
Try to avoid using abbreviations in names, because there can be
167
inconsistency if other people use the full name.
168
169
170
Standard Names
171
==============
172
173
``revision_id`` not ``rev_id`` or ``revid``
174
175
Functions that transform one thing to another should be named ``x_to_y``
176
(not ``x2y`` as occurs in some old code.)
177
178
179
Destructors
180
===========
181
182
Python destructors (``__del__``) work differently to those of other
183
languages.  In particular, bear in mind that destructors may be called
184
immediately when the object apparently becomes unreferenced, or at some
185
later time, or possibly never at all.  Therefore we have restrictions on
186
what can be done inside them.
187
5274.3.1 by Andrew Bennetts
Expand 'Cleanup methods' section of coding style guide, and also correct some excessively indented bullets.
188
0. If you think you need to use a ``__del__`` method ask another
189
   developer for alternatives.  If you do need to use one, explain
190
   why in a comment.
191
192
1. Never rely on a ``__del__`` method running.  If there is code that
193
   must run, do it from a ``finally`` block instead.
194
195
2. Never ``import`` from inside a ``__del__`` method, or you may crash the
196
   interpreter!!
197
198
3. In some places we raise a warning from the destructor if the object
199
   has not been cleaned up or closed.  This is considered OK: the warning
200
   may not catch every case but it's still useful sometimes.
5225.2.9 by Martin Pool
Split out code style guide from HACKING
201
202
203
Cleanup methods
204
===============
205
5274.3.1 by Andrew Bennetts
Expand 'Cleanup methods' section of coding style guide, and also correct some excessively indented bullets.
206
Often when something has failed later code will fail too, including
207
cleanups invoked from ``finally`` blocks.  These secondary failures are
208
generally uninteresting compared to the original exception.  ``bzrlib``
209
has some facilities you can use to mitigate this.
210
211
* In ``Command`` subclasses, prefer the ``add_cleanup`` method to using
212
  ``try``/``finally`` blocks.  E.g. to acquire a lock and ensure it will
213
  always be released when the command is done::
214
215
    self.add_cleanup(branch.lock_read().unlock)
216
217
  This also avoids heavily indented code. It also makes it easier to notice
218
  mismatched lock/unlock pairs (and other kinds of resource
219
  acquire/release) because there isn't a large block of code separating
220
  them.
221
222
* Use the ``only_raises`` decorator (from ``bzrlib.decorators``) when
223
  defining methods that are typically called in ``finally`` blocks, such
224
  as ``unlock`` methods.  For example, ``@only_raises(LockNotHeld,
225
  LockBroken)``.  All errors that are unlikely to be a knock-on failure
226
  from a previous failure should be allowed.
227
228
* Consider using the ``OperationWithCleanups`` helper from
229
  ``bzrlib.cleanup`` anywhere else you have a ``finally`` block that
230
  might fail.
5225.2.9 by Martin Pool
Split out code style guide from HACKING
231
232
233
Factories
234
=========
235
236
In some places we have variables which point to callables that construct
237
new instances.  That is to say, they can be used a lot like class objects,
5225.2.10 by Martin Pool
More code style guidelines cleanups
238
but they shouldn't be *named* like classes.  Things called ``FooBar`` should
239
create an instance of ``FooBar``.  A factory method that might create a
240
``FooBar`` or might make something else should be called ``foo_factory``.
5225.2.9 by Martin Pool
Split out code style guide from HACKING
241
242
243
Registries
244
==========
245
246
Several places in Bazaar use (or will use) a registry, which is a
247
mapping from names to objects or classes.  The registry allows for
248
loading in registered code only when it's needed, and keeping
249
associated information such as a help string or description.
250
251
252
InterObject and multiple dispatch
253
=================================
254
255
The ``InterObject`` provides for two-way `multiple dispatch`__: matching
256
up for example a source and destination repository to find the right way
257
to transfer data between them.
258
259
.. __: http://en.wikipedia.org/wiki/Multiple_dispatch
260
261
There is a subclass ``InterObject`` classes for each type of object that is
262
dispatched this way, e.g. ``InterRepository``.  Calling ``.get()`` on this
263
class will return an ``InterObject`` instance providing the best match for
264
those parameters, and this instance then has methods for operations
265
between the objects.
266
267
::
268
269
  inter = InterRepository.get(source_repo, target_repo)
270
  inter.fetch(revision_id)
271
272
``InterRepository`` also acts as a registry-like object for its
273
subclasses, and they can be added through ``.register_optimizer``.  The
274
right one to run is selected by asking each class, in reverse order of
275
registration, whether it ``.is_compatible`` with the relevant objects.
276
277
Lazy Imports
278
============
279
280
To make startup time faster, we use the ``bzrlib.lazy_import`` module to
281
delay importing modules until they are actually used. ``lazy_import`` uses
282
the same syntax as regular python imports. So to import a few modules in a
283
lazy fashion do::
284
285
  from bzrlib.lazy_import import lazy_import
286
  lazy_import(globals(), """
287
  import os
288
  import subprocess
289
  import sys
290
  import time
291
292
  from bzrlib import (
293
     errors,
294
     transport,
295
     revision as _mod_revision,
296
     )
297
  import bzrlib.transport
298
  import bzrlib.xml5
299
  """)
300
301
At this point, all of these exist as a ``ImportReplacer`` object, ready to
302
be imported once a member is accessed. Also, when importing a module into
303
the local namespace, which is likely to clash with variable names, it is
304
recommended to prefix it as ``_mod_<module>``. This makes it clearer that
305
the variable is a module, and these object should be hidden anyway, since
306
they shouldn't be imported into other namespaces.
307
308
While it is possible for ``lazy_import()`` to import members of a module
309
when using the ``from module import member`` syntax, it is recommended to
310
only use that syntax to load sub modules ``from module import submodule``.
311
This is because variables and classes can frequently be used without
312
needing a sub-member for example::
313
314
  lazy_import(globals(), """
315
  from module import MyClass
316
  """)
317
318
  def test(x):
319
      return isinstance(x, MyClass)
320
321
This will incorrectly fail, because ``MyClass`` is a ``ImportReplacer``
322
object, rather than the real class.
323
324
It also is incorrect to assign ``ImportReplacer`` objects to other variables.
325
Because the replacer only knows about the original name, it is unable to
326
replace other variables. The ``ImportReplacer`` class will raise an
327
``IllegalUseOfScopeReplacer`` exception if it can figure out that this
328
happened. But it requires accessing a member more than once from the new
329
variable, so some bugs are not detected right away.
330
331
332
The Null revision
333
=================
334
335
The null revision is the ancestor of all revisions.  Its revno is 0, its
336
revision-id is ``null:``, and its tree is the empty tree.  When referring
337
to the null revision, please use ``bzrlib.revision.NULL_REVISION``.  Old
338
code sometimes uses ``None`` for the null revision, but this practice is
339
being phased out.
340
341
342
Object string representations
343
=============================
344
345
Python prints objects using their ``__repr__`` method when they are
346
written to logs, exception tracebacks, or the debugger.  We want
347
objects to have useful representations to help in determining what went
348
wrong.
349
350
If you add a new class you should generally add a ``__repr__`` method
351
unless there is an adequate method in a parent class.  There should be a
352
test for the repr.
353
354
Representations should typically look like Python constructor syntax, but
355
they don't need to include every value in the object and they don't need
356
to be able to actually execute.  They're to be read by humans, not
357
machines.  Don't hardcode the classname in the format, so that we get the
358
correct value if the method is inherited by a subclass.  If you're
359
printing attributes of the object, including strings, you should normally
360
use ``%r`` syntax (to call their repr in turn).
361
362
Try to avoid the representation becoming more than one or two lines long.
363
(But balance this against including useful information, and simplicity of
364
implementation.)
365
366
Because repr methods are often called when something has already gone
367
wrong, they should be written somewhat more defensively than most code.
368
The object may be half-initialized or in some other way in an illegal
369
state.  The repr method shouldn't raise an exception, or it may hide the
370
(probably more useful) underlying exception.
371
372
Example::
373
374
    def __repr__(self):
375
        return '%s(%r)' % (self.__class__.__name__,
376
                           self._transport)
377
378
379
Exception handling
380
==================
381
382
A bare ``except`` statement will catch all exceptions, including ones that
383
really should terminate the program such as ``MemoryError`` and
384
``KeyboardInterrupt``.  They should rarely be used unless the exception is
385
later re-raised.  Even then, think about whether catching just
386
``Exception`` (which excludes system errors in Python2.5 and later) would
387
be better.
388
389
390
Test coverage
391
=============
392
393
All code should be exercised by the test suite.  See the `Bazaar Testing
5261.2.1 by Parth Malwankar
added 'Portability Tip' on explicitly closing file to code-style.
394
Guide <http://doc.bazaar.canonical.com/developers/testing.html>`_ for detailed
5225.2.9 by Martin Pool
Split out code style guide from HACKING
395
information about writing tests.
396
5225.2.10 by Martin Pool
More code style guidelines cleanups
397
398
Assertions
399
==========
400
401
Do not use the Python ``assert`` statement, either in tests or elsewhere.
402
A source test checks that it is not used.  It is ok to explicitly raise
403
AssertionError.
404
405
Rationale:
406
5274.3.1 by Andrew Bennetts
Expand 'Cleanup methods' section of coding style guide, and also correct some excessively indented bullets.
407
* It makes the behaviour vary depending on whether bzr is run with -O
408
  or not, therefore giving a chance for bugs that occur in one case or
409
  the other, several of which have already occurred: assertions with
410
  side effects, code which can't continue unless the assertion passes,
411
  cases where we should give the user a proper message rather than an
412
  assertion failure.
413
* It's not that much shorter than an explicit if/raise.
414
* It tends to lead to fuzzy thinking about whether the check is
415
  actually needed or not, and whether it's an internal error or not
416
* It tends to cause look-before-you-leap patterns.
417
* It's unsafe if the check is needed to protect the integrity of the
418
  user's data.
419
* It tends to give poor messages since the developer can get by with
420
  no explanatory text at all.
421
* We can't rely on people always running with -O in normal use, so we
422
  can't use it for tests that are actually expensive.
423
* Expensive checks that help developers are better turned on from the
424
  test suite or a -D flag.
425
* If used instead of ``self.assert*()`` in tests it makes them falsely
426
  pass with -O.
5225.2.10 by Martin Pool
More code style guidelines cleanups
427
428
emacs setup
429
===========
430
431
In emacs::
432
433
    ;(defface my-invalid-face
434
    ;  '((t (:background "Red" :underline t)))
435
    ;  "Face used to highlight invalid constructs or other uglyties"
436
    ;  )
437
438
    (defun my-python-mode-hook ()
439
     ;; setup preferred indentation style.
440
     (setq fill-column 79)
441
     (setq indent-tabs-mode nil) ; no tabs, never, I will not repeat
442
    ;  (font-lock-add-keywords 'python-mode
443
    ;                         '(("^\\s *\t" . 'my-invalid-face) ; Leading tabs
444
    ;                            ("[ \t]+$" . 'my-invalid-face)  ; Trailing spaces
445
    ;                            ("^[ \t]+$" . 'my-invalid-face)); Spaces only
446
    ;                          )
447
     )
448
449
    (add-hook 'python-mode-hook 'my-python-mode-hook)
450
451
The lines beginning with ';' are comments. They can be activated
452
if one want to have a strong notice of some tab/space usage
453
violations.
5225.2.13 by Martin Pool
More reorganization of the developer documentation
454
455
Portability Tips
456
================
457
458
The ``bzrlib.osutils`` module has many useful helper functions, including
459
some more portable variants of functions in the standard library.
460
461
In particular, don't use ``shutil.rmtree`` unless it's acceptable for it
462
to fail on Windows if some files are readonly or still open elsewhere.
463
Use ``bzrlib.osutils.rmtree`` instead.
464
5261.2.1 by Parth Malwankar
added 'Portability Tip' on explicitly closing file to code-style.
465
Using the ``open(..).read(..)`` or ``open(..).write(..)`` style chaining
466
of methods for reading or writing file content relies on garbage collection
467
to close the file which may keep the file open for an undefined period of
468
time. This may break some follow up operations like rename on Windows.
469
Use ``try/finally`` to explictly close the file. E.g.::
5225.2.13 by Martin Pool
More reorganization of the developer documentation
470
5261.2.1 by Parth Malwankar
added 'Portability Tip' on explicitly closing file to code-style.
471
    f = open('foo.txt', 'w')
472
    try:
473
        f.write(s)
474
    finally:
475
        f.close()
5225.2.13 by Martin Pool
More reorganization of the developer documentation
476
5278.1.1 by Martin Pool
Call out a couple of GNU policy points about naming (gnu/linux etc)
477
478
Terminology
479
===========
480
481
Bazaar is a GNU project and uses standard GNU terminology, especially:
482
483
 * Use the word "Linux" to refer to the Linux kernel, not as a synechoche
484
   for the entire operating system.  (See `bug 528253
485
   <https://bugs.launchpad.net/bzr/+bug/528253>`_).
486
487
 * Don't say "open source" when you mean "free software".
488
5225.2.13 by Martin Pool
More reorganization of the developer documentation
489
..
490
   vim: ft=rst tw=74 ai