| 1 |
PyPy's Configuration Handling |
| 2 |
============================= |
| 3 |
|
| 4 |
Due to more and more available configuration options it became quite annoying to |
| 5 |
hand the necessary options to where they are actually used and even more |
| 6 |
annoying to add new options. To circumvent these problems configuration |
| 7 |
management was introduced. There all the necessary options are stored in a |
| 8 |
configuration object, which is available nearly everywhere in the `RPython |
| 9 |
toolchain`_ and in the standard interpreter so that adding new options becomes |
| 10 |
trivial. Options are organized into a tree. Configuration objects can be |
| 11 |
created in different ways, there is support for creating an optparse command |
| 12 |
line parser automatically. |
| 13 |
|
| 14 |
.. _RPython toolchain: https://rpython.readthedocs.org/ |
| 15 |
|
| 16 |
|
| 17 |
Main Assumption |
| 18 |
--------------- |
| 19 |
|
| 20 |
Configuration objects are produced at the entry points and handed down to |
| 21 |
where they are actually used. This keeps configuration local but available |
| 22 |
everywhere and consistent. The configuration values are created using the |
| 23 |
command line. |
| 24 |
|
| 25 |
|
| 26 |
API Details |
| 27 |
----------- |
| 28 |
|
| 29 |
The handling of options is split into two parts: the description of which |
| 30 |
options are available, what their possible values and defaults are and how they |
| 31 |
are organized into a tree. A specific choice of options is bundled into a |
| 32 |
configuration object which has a reference to its option description (and |
| 33 |
therefore makes sure that the configuration values adhere to the option |
| 34 |
description). |
| 35 |
This splitting is remotely similar to the distinction between types and |
| 36 |
instances in the type systems of the rtyper: the types describe what sort of |
| 37 |
fields the instances have. |
| 38 |
|
| 39 |
The Options are organized in a tree. Every option has a name, as does every |
| 40 |
option group. The parts of the full name of the option are separated by dots: |
| 41 |
e.g. ``config.translation.thread``. |
| 42 |
|
| 43 |
|
| 44 |
Description of Options |
| 45 |
~~~~~~~~~~~~~~~~~~~~~~ |
| 46 |
|
| 47 |
All the constructors take a ``name`` and a ``doc`` argument as first arguments |
| 48 |
to give the option or option group a name and to document it. Most constructors |
| 49 |
take a ``default`` argument that specifies the default value of the option. If |
| 50 |
this argument is not supplied the default value is assumed to be ``None``. |
| 51 |
Most constructors |
| 52 |
also take a ``cmdline`` argument where you can specify what the command line |
| 53 |
option should look like (for example cmdline="-v --version"). If ``cmdline`` is |
| 54 |
not specified a default cmdline option is created that uses the name of the |
| 55 |
option together with its full path. If ``None`` is passed in as ``cmdline`` then |
| 56 |
no command line option is created at all. |
| 57 |
|
| 58 |
Some options types can specify requirements to specify that a particular choice |
| 59 |
for one option works only if a certain choice for another option is used. A |
| 60 |
requirement is specified using a list of pairs. The first element of the pair |
| 61 |
gives the path of the option that is required to be set and the second element |
| 62 |
gives the required value. |
| 63 |
|
| 64 |
|
| 65 |
``OptionDescription`` |
| 66 |
+++++++++++++++++++++ |
| 67 |
|
| 68 |
This class is used to group suboptions. |
| 69 |
|
| 70 |
``__init__(self, name, doc, children)`` |
| 71 |
``children`` is a list of option descriptions (including |
| 72 |
``OptionDescription`` instances for nested namespaces). |
| 73 |
|
| 74 |
|
| 75 |
``ChoiceOption`` |
| 76 |
++++++++++++++++ |
| 77 |
|
| 78 |
Represents a choice out of several objects. The option can also have the value |
| 79 |
``None``. |
| 80 |
|
| 81 |
``__init__(self, name, doc, values, default=None, requires=None, cmdline=DEFAULT)`` |
| 82 |
``values`` is a list of values the option can possibly take, |
| 83 |
``requires`` is a dictionary mapping values to lists of of two-element |
| 84 |
tuples. |
| 85 |
|
| 86 |
|
| 87 |
``BoolOption`` |
| 88 |
++++++++++++++ |
| 89 |
|
| 90 |
Represents a choice between ``True`` and ``False``. |
| 91 |
|
| 92 |
``__init__(self, name, doc, default=None, requires=None, suggests=None, cmdline=DEFAULT, negation=True)`` |
| 93 |
``default`` specifies the default value of the option. ``requires`` is |
| 94 |
a list of two-element tuples describing the requirements when the |
| 95 |
option is set to true, ``suggests`` is a list of the same structure but |
| 96 |
the options in there are only suggested, not absolutely necessary. The |
| 97 |
difference is small: if the current option is set to True, both the |
| 98 |
required and the suggested options are set. The required options cannot |
| 99 |
be changed later, though. ``negation`` specifies whether the negative |
| 100 |
commandline option should be generated. |
| 101 |
|
| 102 |
|
| 103 |
``IntOption`` |
| 104 |
+++++++++++++ |
| 105 |
|
| 106 |
Represents a choice of an integer. |
| 107 |
|
| 108 |
``__init__(self, name, doc, default=None, cmdline=DEFAULT)`` |
| 109 |
|
| 110 |
|
| 111 |
``FloatOption`` |
| 112 |
+++++++++++++++ |
| 113 |
|
| 114 |
Represents a choice of a floating point number. |
| 115 |
|
| 116 |
``__init__(self, name, doc, default=None, cmdline=DEFAULT)`` |
| 117 |
|
| 118 |
|
| 119 |
``StrOption`` |
| 120 |
+++++++++++++ |
| 121 |
|
| 122 |
Represents the choice of a string. |
| 123 |
|
| 124 |
``__init__(self, name, doc, default=None, cmdline=DEFAULT)`` |
| 125 |
|
| 126 |
|
| 127 |
Configuration Objects |
| 128 |
~~~~~~~~~~~~~~~~~~~~~ |
| 129 |
|
| 130 |
``Config`` objects hold the chosen values for the options (of the default, |
| 131 |
if no choice was made). A ``Config`` object is described by an |
| 132 |
``OptionDescription`` instance. The attributes of the ``Config`` objects are the |
| 133 |
names of the children of the ``OptionDescription``. Example:: |
| 134 |
|
| 135 |
>>> from rpython.config.config import OptionDescription, Config, BoolOption |
| 136 |
>>> descr = OptionDescription("options", "", [ |
| 137 |
... BoolOption("bool", "", default=False)]) |
| 138 |
>>> |
| 139 |
>>> config = Config(descr) |
| 140 |
>>> config.bool |
| 141 |
False |
| 142 |
>>> config.bool = True |
| 143 |
>>> config.bool |
| 144 |
True |
| 145 |
|
| 146 |
|
| 147 |
Description of the (useful) methods on ``Config``: |
| 148 |
|
| 149 |
``__init__(self, descr, **overrides)``: |
| 150 |
``descr`` is an instance of ``OptionDescription`` that describes the |
| 151 |
configuration object. ``overrides`` can be used to set different default |
| 152 |
values (see method ``override``). |
| 153 |
|
| 154 |
``override(self, overrides)``: |
| 155 |
override default values. This marks the overridden values as defaults, |
| 156 |
which makes it possible to change them (you can usually change values |
| 157 |
only once). ``overrides`` is a dictionary of path strings to values. |
| 158 |
|
| 159 |
``set(self, **kwargs)``: |
| 160 |
"do what I mean"-interface to option setting. Searches all paths |
| 161 |
starting from that config for matches of the optional arguments and sets |
| 162 |
the found option if the match is not ambiguous. |
| 163 |
|
| 164 |
|
| 165 |
Production of optparse Parsers |
| 166 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 167 |
|
| 168 |
To produce an optparse parser use the function ``to_optparse``. It will create |
| 169 |
an option parser using callbacks in such a way that the config object used for |
| 170 |
creating the parser is updated automatically. |
| 171 |
|
| 172 |
``to_optparse(config, useoptions=None, parser=None)``: |
| 173 |
Returns an optparse parser. ``config`` is the configuration object for |
| 174 |
which to create the parser. ``useoptions`` is a list of options for |
| 175 |
which to create command line options. It can contain full paths to |
| 176 |
options or also paths to an option description plus an additional ".*" |
| 177 |
to produce command line options for all sub-options of that description. |
| 178 |
If ``useoptions`` is ``None``, then all sub-options are turned into |
| 179 |
cmdline options. ``parser`` can be an existing parser object, if |
| 180 |
``None`` is passed in, then a new one is created. |
| 181 |
|
| 182 |
|
| 183 |
The usage of config objects in PyPy |
| 184 |
----------------------------------- |
| 185 |
|
| 186 |
The two large parts of PyPy, the Python interpreter and the RPython |
| 187 |
toolchain, have two separate sets of options. The translation toolchain options |
| 188 |
can be found on the ``config`` attribute of all ``TranslationContext`` |
| 189 |
instances and are described in :source:`rpython/config/translationoption.py`. The interpreter options |
| 190 |
are attached to the object space, also under the name ``config`` and are |
| 191 |
described in :source:`pypy/config/pypyoption.py`. Both set of options are |
| 192 |
documented in the :doc:`config/index` section. |
| 193 |
|