~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to doc/developers/code-style.txt

  • Committer: Vincent Ladeuil
  • Date: 2010-06-17 09:23:19 UTC
  • mfrom: (5301 +trunk)
  • mto: (5247.1.8 first-try)
  • mto: This revision was merged to the branch mainline in revision 5326.
  • Revision ID: v.ladeuil+lp@free.fr-20100617092319-da2bzdtf3j0voynf
Merge bzr.dev into cleanup

Show diffs side-by-side

added added

removed removed

Lines of Context:
84
84
 
85
85
Specifically:
86
86
 
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.
 
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.
93
93
 
94
94
 
95
95
hasattr and getattr
152
152
later time, or possibly never at all.  Therefore we have restrictions on
153
153
what can be done inside them.
154
154
 
155
 
 0. If you think you need to use a ``__del__`` method ask another
156
 
    developer for alternatives.  If you do need to use one, explain
157
 
    why in a comment.
158
 
 
159
 
 1. Never rely on a ``__del__`` method running.  If there is code that
160
 
    must run, do it from a ``finally`` block instead.
161
 
 
162
 
 2. Never ``import`` from inside a ``__del__`` method, or you may crash the
163
 
    interpreter!!
164
 
 
165
 
 3. In some places we raise a warning from the destructor if the object
166
 
    has not been cleaned up or closed.  This is considered OK: the warning
167
 
    may not catch every case but it's still useful sometimes.
 
155
0. If you think you need to use a ``__del__`` method ask another
 
156
   developer for alternatives.  If you do need to use one, explain
 
157
   why in a comment.
 
158
 
 
159
1. Never rely on a ``__del__`` method running.  If there is code that
 
160
   must run, do it from a ``finally`` block instead.
 
161
 
 
162
2. Never ``import`` from inside a ``__del__`` method, or you may crash the
 
163
   interpreter!!
 
164
 
 
165
3. In some places we raise a warning from the destructor if the object
 
166
   has not been cleaned up or closed.  This is considered OK: the warning
 
167
   may not catch every case but it's still useful sometimes.
168
168
 
169
169
 
170
170
Cleanup methods
171
171
===============
172
172
 
173
 
Often when something has failed later code, including cleanups invoked
174
 
from ``finally`` blocks, will fail too.  These secondary failures are
175
 
generally uninteresting compared to the original exception.  So use the
176
 
``only_raises`` decorator (from ``bzrlib.decorators``) for methods that
177
 
are typically called in ``finally`` blocks, such as ``unlock`` methods.
178
 
For example, ``@only_raises(LockNotHeld, LockBroken)``.  All errors that
179
 
are unlikely to be a knock-on failure from a previous failure should be
180
 
allowed.
 
173
Often when something has failed later code will fail too, including
 
174
cleanups invoked from ``finally`` blocks.  These secondary failures are
 
175
generally uninteresting compared to the original exception.  ``bzrlib``
 
176
has some facilities you can use to mitigate this.
 
177
 
 
178
* In ``Command`` subclasses, prefer the ``add_cleanup`` method to using
 
179
  ``try``/``finally`` blocks.  E.g. to acquire a lock and ensure it will
 
180
  always be released when the command is done::
 
181
 
 
182
    self.add_cleanup(branch.lock_read().unlock)
 
183
 
 
184
  This also avoids heavily indented code. It also makes it easier to notice
 
185
  mismatched lock/unlock pairs (and other kinds of resource
 
186
  acquire/release) because there isn't a large block of code separating
 
187
  them.
 
188
 
 
189
* Use the ``only_raises`` decorator (from ``bzrlib.decorators``) when
 
190
  defining methods that are typically called in ``finally`` blocks, such
 
191
  as ``unlock`` methods.  For example, ``@only_raises(LockNotHeld,
 
192
  LockBroken)``.  All errors that are unlikely to be a knock-on failure
 
193
  from a previous failure should be allowed.
 
194
 
 
195
* Consider using the ``OperationWithCleanups`` helper from
 
196
  ``bzrlib.cleanup`` anywhere else you have a ``finally`` block that
 
197
  might fail.
181
198
 
182
199
 
183
200
Factories
354
371
 
355
372
Rationale:
356
373
 
357
 
 * It makes the behaviour vary depending on whether bzr is run with -O
358
 
   or not, therefore giving a chance for bugs that occur in one case or
359
 
   the other, several of which have already occurred: assertions with
360
 
   side effects, code which can't continue unless the assertion passes,
361
 
   cases where we should give the user a proper message rather than an
362
 
   assertion failure.
363
 
 * It's not that much shorter than an explicit if/raise.
364
 
 * It tends to lead to fuzzy thinking about whether the check is
365
 
   actually needed or not, and whether it's an internal error or not
366
 
 * It tends to cause look-before-you-leap patterns.
367
 
 * It's unsafe if the check is needed to protect the integrity of the
368
 
   user's data.
369
 
 * It tends to give poor messages since the developer can get by with
370
 
   no explanatory text at all.
371
 
 * We can't rely on people always running with -O in normal use, so we
372
 
   can't use it for tests that are actually expensive.
373
 
 * Expensive checks that help developers are better turned on from the
374
 
   test suite or a -D flag.
375
 
 * If used instead of ``self.assert*()`` in tests it makes them falsely pass with -O.
 
374
* It makes the behaviour vary depending on whether bzr is run with -O
 
375
  or not, therefore giving a chance for bugs that occur in one case or
 
376
  the other, several of which have already occurred: assertions with
 
377
  side effects, code which can't continue unless the assertion passes,
 
378
  cases where we should give the user a proper message rather than an
 
379
  assertion failure.
 
380
* It's not that much shorter than an explicit if/raise.
 
381
* It tends to lead to fuzzy thinking about whether the check is
 
382
  actually needed or not, and whether it's an internal error or not
 
383
* It tends to cause look-before-you-leap patterns.
 
384
* It's unsafe if the check is needed to protect the integrity of the
 
385
  user's data.
 
386
* It tends to give poor messages since the developer can get by with
 
387
  no explanatory text at all.
 
388
* We can't rely on people always running with -O in normal use, so we
 
389
  can't use it for tests that are actually expensive.
 
390
* Expensive checks that help developers are better turned on from the
 
391
  test suite or a -D flag.
 
392
* If used instead of ``self.assert*()`` in tests it makes them falsely
 
393
  pass with -O.
376
394
 
377
395
emacs setup
378
396
===========
423
441
    finally:
424
442
        f.close()
425
443
 
 
444
 
 
445
Terminology
 
446
===========
 
447
 
 
448
Bazaar is a GNU project and uses standard GNU terminology, especially:
 
449
 
 
450
 * Use the word "Linux" to refer to the Linux kernel, not as a synechoche
 
451
   for the entire operating system.  (See `bug 528253
 
452
   <https://bugs.launchpad.net/bzr/+bug/528253>`_).
 
453
 
 
454
 * Don't say "open source" when you mean "free software".
 
455
 
426
456
..
427
457
   vim: ft=rst tw=74 ai