Blame view

3rdparty/boost_1_81_0/libs/python/doc/internals.rst 7.58 KB
73ef4ff3   Hu Chunming   提交三方库
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  ===================================
   Boost.Python_ Internals |(logo)|__
  ===================================
  
  .. |(logo)| image:: ../../../boost.png
     :alt: Boost
     :class: boost-logo
  
  __ ../../../index.htm
  
  .. _`Boost.Python`: index.html
  
  .. _license: ../../../LICENSE_1_0.txt
  
  
  -------------------------------------------------------
  A conversation between Brett Calcott and David Abrahams
  -------------------------------------------------------
  
  :copyright: Copyright David Abrahams and Brett Calcott 2003. See
              accompanying license_ for terms of use.
  
  In both of these cases, I'm quite capable of reading code - but the
  thing I don't get from scanning the source is a sense of the
  architecture, both structurally, and temporally (er, I mean in what
  order things go on).
  
  1) What happens when you do the following::
  
       struct boring {};
       ...etc...
       class_<boring>("boring")
           ;
  
  There seems to be a fair bit going on.
  
   - Python needs a new ClassType to be registered.
   - We need to construct a new type that can hold our boring struct.
   - Inward and outward converters need to be registered for the type.
  
  Can you gesture in the general direction where these things are done?
  
    I only have time for a "off-the-top-of-my-head" answer at the moment;
    I suggest you step through the code with a debugger after reading this
    to see how it works, fill in details, and make sure I didn't forget
    anything.
  
            A new (Python) subclass of Boost.Python.Instance (see
            libs/python/src/object/class.cpp) is created by invoking
            Boost.Python.class, the metatype::
  
                  >>> boring = Boost.Python.class(
                  ...     'boring'
                  ...   , bases_tuple       # in this case, just ()
                  ...   , { 
                  ...         '__module__' : module_name
                  ...       , '__doc__' : doc_string # optional
                  ...     }
                  ... )
  
            A handle to this object is stuck in the m_class_object field
            of the registration associated with ``typeid(boring)``.  The
            registry will keep that object alive forever, even if you
            wipe out the 'boring' attribute of the extension module
            (probably not a good thing).
  
            Because you didn't specify ``class<boring, non_copyable,
            ...>``, a to-python converter for boring is registered which
            copies its argument into a value_holder held by the the
            Python boring object.
  
            Because you didn't specify ``class<boring ...>(no_init)``,
            an ``__init__`` function object is added to the class
            dictionary which default-constructs a boring in a
            value_holder (because you didn't specify some smart pointer
            or derived wrapper class as a holder) held by the Python
            boring object.
  
            ``register_class_from_python`` is used to register a
            from-python converter for ``shared_ptr<boring>``.
            ``boost::shared_ptr``\ s are special among smart pointers
            because their Deleter argument can be made to manage the
            whole Python object, not just the C++ object it contains, no
            matter how the C++ object is held.
  
            If there were any ``bases<>``, we'd also be registering the
            relationship between these base classes and boring in the
            up/down cast graph (``inheritance.[hpp/cpp]``).
  
            In earlier versions of the code, we'd be registering lvalue
            from-python converters for the class here, but now
            from-python conversion for wrapped classes is handled as a
            special case, before consulting the registry, if the source
            Python object's metaclass is the Boost.Python metaclass.
  
            Hmm, that from-python converter probably ought to be handled
            the way class converters are, with no explicit conversions
            registered.
  
  2) Can you give a brief overview of the data structures that are
     present in the registry
  
          The registry is simple: it's just a map from typeid ->
          registration (see boost/python/converter/registrations.hpp).
          ``lvalue_chain`` and ``rvalue_chain`` are simple endogenous
          linked lists.
  
          If you want to know more, just ask.
  
          If you want to know about the cast graph, ask me something specific in
          a separate message.
  
     and an overview of the process that happens as a type makes its
     way from c++ to python and back again.
  
    Big subject.  I suggest some background reading: look for relevant
    info in the LLNL progress reports and the messages they link to.
    Also, 
  
          http://mail.python.org/pipermail/c++-sig/2002-May/001023.html
  
          http://mail.python.org/pipermail/c++-sig/2002-December/003115.html
  
          http://aspn.activestate.com/ASPN/Mail/Message/1280898
  
          http://mail.python.org/pipermail/c++-sig/2002-July/001755.html
  
    from c++ to python:
  
         It depends on the type and the call policies in use or, for
         ``call<>(...)``, ``call_method<>(...)``, or ``object(...)``, if
         ``ref`` or ``ptr`` is used.  There are also two basic
         categories to to-python conversion, "return value" conversion
         (for Python->C++ calls) and "argument" conversion (for
         C++->Python calls and explicit ``object()`` conversions).  The
         behavior of these two categories differs subtly in various ways
         whose details I forget at the moment.  You can probably find
         the answers in the above references, and certainly in the code.
  
         The "default" case is by-value (copying) conversion, which uses
         to_python_value as a to-python converter.
  
             Since there can sensibly be only one way to convert any type
             to python (disregarding the idea of scoped registries for the
             moment), it makes sense that to-python conversions can be
             handled by specializing a template.  If the type is one of
             the types handled by a built-in conversion
             (builtin_converters.hpp), the corresponding template
             specialization of to_python_value gets used.
  
             Otherwise, to_python_value uses the ``m_to_python``
             function in the registration for the C++ type.
  
         Other conversions, like by-reference conversions, are only
         available for wrapped classes, and are requested explicitly by
         using ``ref(...)``, ``ptr(...)``, or by specifying different
         CallPolicies for a call, which can cause a different to-python
         converter to be used.  These conversions are never registered
         anywhere, though they do need to use the registration to find
         the Python class corresponding to the C++ type being referred
         to.  They just build a new Python instance and stick the
         appropriate Holder instance in it.
  
  
    from python to C++:
  
         Once again I think there is a distinction between "return value"
         and "argument" conversions, and I forget exactly what that is.
  
         What happens depends on whether an lvalue conversion is needed
         (see http://mail.python.org/pipermail/c++-sig/2002-May/001023.html)
         All lvalue conversions are also registered in a type's rvalue
         conversion chain, since when an rvalue will do, an lvalue is
         certainly good enough.
  
         An lvalue conversion can be done in one step (just get me the
         pointer to the object - it can be ``NULL`` if no conversion is
         possible) while an rvalue conversion requires two steps to
         support wrapped function overloading and multiple converters for
         a given C++ target type: first tell me if a conversion is
         possible, then construct the converted object as a second step.