xref: /aoo41x/main/pyuno/inc/pyuno/pyuno.hxx (revision 564d9007)
1db6edb48SAndrew Rist /**************************************************************
2db6edb48SAndrew Rist  *
3db6edb48SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4db6edb48SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5db6edb48SAndrew Rist  * distributed with this work for additional information
6db6edb48SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7db6edb48SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8db6edb48SAndrew Rist  * "License"); you may not use this file except in compliance
9db6edb48SAndrew Rist  * with the License.  You may obtain a copy of the License at
10db6edb48SAndrew Rist  *
11db6edb48SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12db6edb48SAndrew Rist  *
13db6edb48SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14db6edb48SAndrew Rist  * software distributed under the License is distributed on an
15db6edb48SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16db6edb48SAndrew Rist  * KIND, either express or implied.  See the License for the
17db6edb48SAndrew Rist  * specific language governing permissions and limitations
18db6edb48SAndrew Rist  * under the License.
19db6edb48SAndrew Rist  *
20db6edb48SAndrew Rist  *************************************************************/
21db6edb48SAndrew Rist 
22cdf0e10cSrcweir #ifndef _PYUNO_PYUNO_HXX_
23cdf0e10cSrcweir #define _PYUNO_PYUNO_HXX_
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #ifndef Py_PYTHON_H
26cdf0e10cSrcweir #if defined _MSC_VER
27cdf0e10cSrcweir #pragma warning(push, 1)
28cdf0e10cSrcweir #endif
29cdf0e10cSrcweir #ifdef _DEBUG
30cdf0e10cSrcweir #undef _DEBUG
31cdf0e10cSrcweir #include <Python.h>
32cdf0e10cSrcweir #define _DEBUG
33cdf0e10cSrcweir #else
34cdf0e10cSrcweir #include <Python.h>
35cdf0e10cSrcweir #endif // #ifdef _DEBUG
36cdf0e10cSrcweir #if defined _MSC_VER
37cdf0e10cSrcweir #pragma warning(pop)
38cdf0e10cSrcweir #endif
39cdf0e10cSrcweir #endif // #ifdef Py_PYTHON_H
40*564d9007SPedro Giffuni 
4189e52773SPedro Giffuni // Compatibility for older system Python (2.6 and previous)
4289e52773SPedro Giffuni #ifndef PyVarObject_HEAD_INIT
4389e52773SPedro Giffuni #define PyVarObject_HEAD_INIT(type, size) \
4489e52773SPedro Giffuni 	PyObject_HEAD_INIT(type) size,
4589e52773SPedro Giffuni #endif
46*564d9007SPedro Giffuni // define PyBytes_* as the equivalent string type methods.
47*564d9007SPedro Giffuni #ifndef PyBytes_Check
48*564d9007SPedro Giffuni     #define PyBytes_Check               PyString_Check
49*564d9007SPedro Giffuni     #define PyBytes_AsString            PyString_AsString
50*564d9007SPedro Giffuni     #define PyBytes_FromString          PyString_FromString
51*564d9007SPedro Giffuni     #define PyBytes_Size                PyString_Size
52*564d9007SPedro Giffuni     #define PyBytes_FromStringAndSize   PyString_FromStringAndSize
53*564d9007SPedro Giffuni #endif
5489e52773SPedro Giffuni 
55cdf0e10cSrcweir #include <com/sun/star/uno/XComponentContext.hpp>
56cdf0e10cSrcweir #include <com/sun/star/script/CannotConvertException.hpp>
57cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp>
58cdf0e10cSrcweir 
59cdf0e10cSrcweir /**
60cdf0e10cSrcweir    External interface of the Python UNO bridge.
61cdf0e10cSrcweir 
62cdf0e10cSrcweir    This is a C++ interface, because the core UNO components
63cdf0e10cSrcweir    invocation and proxyfactory are used to implement the bridge.
64cdf0e10cSrcweir 
65cdf0e10cSrcweir    This interface is somewhat private and my change in future.
66cdf0e10cSrcweir 
67cdf0e10cSrcweir    A scripting framework implementation may use this interface
68cdf0e10cSrcweir    to do the necessary conversions.
69cdf0e10cSrcweir */
70cdf0e10cSrcweir 
71cdf0e10cSrcweir #ifdef WIN32
72cdf0e10cSrcweir #define PY_DLLEXPORT __declspec(dllexport)
73cdf0e10cSrcweir #else
74cdf0e10cSrcweir #define PY_DLLEXPORT
75cdf0e10cSrcweir #endif
76cdf0e10cSrcweir 
77cdf0e10cSrcweir /** function called by the python runtime to initialize the
78cdf0e10cSrcweir     pyuno module.
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     preconditions: python has been initialized before and
81cdf0e10cSrcweir                    the global interpreter lock is held
82cdf0e10cSrcweir */
83cdf0e10cSrcweir extern "C" PY_DLLEXPORT void SAL_CALL initpyuno();
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 
86cdf0e10cSrcweir namespace pyuno
87cdf0e10cSrcweir {
88cdf0e10cSrcweir 
89cdf0e10cSrcweir /** Helper class for keeping references to python objects.
90cdf0e10cSrcweir     BEWARE: Look up every python function you use to check
91cdf0e10cSrcweir     wether you get an acquired or not acquired object pointer
92cdf0e10cSrcweir     (python terminus for a not acquired object pointer
93cdf0e10cSrcweir     is 'borrowed reference'). Use in the acquired pointer cases the
94cdf0e10cSrcweir     PyRef( pointer, SAL_NO_ACQUIRE) ctor.
95cdf0e10cSrcweir 
96cdf0e10cSrcweir     precondition: python has been initialized before and
97cdf0e10cSrcweir     the global interpreter lock is held
98cdf0e10cSrcweir 
99cdf0e10cSrcweir */
100cdf0e10cSrcweir class PyRef
101cdf0e10cSrcweir {
102cdf0e10cSrcweir     PyObject *m;
103cdf0e10cSrcweir public:
PyRef()104cdf0e10cSrcweir     PyRef () : m(0) {}
PyRef(PyObject * p)105cdf0e10cSrcweir     PyRef( PyObject * p ) : m( p ) { Py_XINCREF( m ); }
106cdf0e10cSrcweir 
PyRef(PyObject * p,__sal_NoAcquire)107cdf0e10cSrcweir     PyRef( PyObject * p, __sal_NoAcquire ) : m( p ) {}
108cdf0e10cSrcweir 
PyRef(const PyRef & r)109cdf0e10cSrcweir     PyRef( const PyRef &r ) : m( r.get() ) { Py_XINCREF( m ); }
110cdf0e10cSrcweir 
~PyRef()111cdf0e10cSrcweir     ~PyRef() { Py_XDECREF( m ); }
112cdf0e10cSrcweir 
get() const113cdf0e10cSrcweir     PyObject *get() const { return m; }
114cdf0e10cSrcweir 
getAcquired() const115cdf0e10cSrcweir     PyObject * getAcquired() const
116cdf0e10cSrcweir     {
117cdf0e10cSrcweir         Py_XINCREF( const_cast< PyObject*> (m) );
118cdf0e10cSrcweir         return m;
119cdf0e10cSrcweir     }
120cdf0e10cSrcweir 
operator =(const PyRef & r)121cdf0e10cSrcweir     PyRef & operator = (  const PyRef & r )
122cdf0e10cSrcweir     {
123cdf0e10cSrcweir         PyObject *tmp = m;
124cdf0e10cSrcweir         m = r.getAcquired();
125cdf0e10cSrcweir         Py_XDECREF( tmp );
126cdf0e10cSrcweir         return *this;
127cdf0e10cSrcweir     }
128cdf0e10cSrcweir 
operator ==(const PyRef & r) const129cdf0e10cSrcweir     bool operator == (  const PyRef & r ) const
130cdf0e10cSrcweir     {
131cdf0e10cSrcweir         return r.get() == m;
132cdf0e10cSrcweir     }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir     /** clears the reference without decreasing the reference count
135cdf0e10cSrcweir         only seldomly needed ! */
scratch()136cdf0e10cSrcweir     void scratch()
137cdf0e10cSrcweir     {
138cdf0e10cSrcweir         m = 0;
139cdf0e10cSrcweir     }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir     /** clears the reference decreasing the refcount of the holded object.
142cdf0e10cSrcweir      */
clear()143cdf0e10cSrcweir     void clear()
144cdf0e10cSrcweir     {
145cdf0e10cSrcweir         Py_XDECREF( m );
146cdf0e10cSrcweir         m = 0;
147cdf0e10cSrcweir     }
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     /** returns 1 when the reference points to a python object python object,
150cdf0e10cSrcweir         otherwise 0.
151cdf0e10cSrcweir     */
is() const152cdf0e10cSrcweir     sal_Bool is() const
153cdf0e10cSrcweir     {
154cdf0e10cSrcweir         return m != 0;
155cdf0e10cSrcweir     }
156cdf0e10cSrcweir 
157cdf0e10cSrcweir     struct Hash
158cdf0e10cSrcweir     {
operator ()pyuno::PyRef::Hash159cdf0e10cSrcweir         sal_IntPtr operator () ( const PyRef &r) const { return sal_IntPtr( r.get() ); }
160cdf0e10cSrcweir     };
161cdf0e10cSrcweir };
162cdf0e10cSrcweir 
163cdf0e10cSrcweir struct stRuntimeImpl;
164cdf0e10cSrcweir typedef struct stRuntimeImpl RuntimeImpl;
165cdf0e10cSrcweir 
166cdf0e10cSrcweir enum ConversionMode { ACCEPT_UNO_ANY, REJECT_UNO_ANY };
167cdf0e10cSrcweir 
168cdf0e10cSrcweir 
169cdf0e10cSrcweir /** The pyuno::Runtime class keeps the internal state of the python UNO bridge
170cdf0e10cSrcweir     for the currently in use python interpreter.
171cdf0e10cSrcweir 
172cdf0e10cSrcweir     You may keep a Runtime instance, use it from a different thread, etc. But you must
173cdf0e10cSrcweir     make sure to fulfill all preconditions mentioned for the specific methods.
174cdf0e10cSrcweir */
175cdf0e10cSrcweir 
176cdf0e10cSrcweir class PY_DLLEXPORT Runtime
177cdf0e10cSrcweir {
178cdf0e10cSrcweir     RuntimeImpl *impl;
179cdf0e10cSrcweir public:
180cdf0e10cSrcweir     ~Runtime( );
181cdf0e10cSrcweir 
182cdf0e10cSrcweir     /**
183cdf0e10cSrcweir         preconditions: python has been initialized before,
184cdf0e10cSrcweir         the global interpreter lock is held and pyuno
185cdf0e10cSrcweir         has been initialized for the currently used interpreter.
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         Note: This method exists for efficiency reasons to save
188cdf0e10cSrcweir         lookup costs for any2PyObject and pyObject2Any
189cdf0e10cSrcweir 
190cdf0e10cSrcweir         @throw RuntimeException in case the runtime has not been
191cdf0e10cSrcweir                initialized before
192cdf0e10cSrcweir      */
193cdf0e10cSrcweir     Runtime() throw( com::sun::star::uno::RuntimeException );
194cdf0e10cSrcweir 
195cdf0e10cSrcweir     Runtime( const Runtime & );
196cdf0e10cSrcweir     Runtime & operator = ( const Runtime & );
197cdf0e10cSrcweir 
198cdf0e10cSrcweir     /** Initializes the python-UNO bridge. May be called only once per python interpreter.
199cdf0e10cSrcweir 
200cdf0e10cSrcweir         @param ctx the component context is used to instantiate bridge services needed
201cdf0e10cSrcweir         for bridging such as invocation, typeconverter, invocationadapterfactory, etc.
202cdf0e10cSrcweir 
203cdf0e10cSrcweir         preconditions: python has been initialized before and
204cdf0e10cSrcweir         the global interpreter lock is held and pyuno is not
205cdf0e10cSrcweir         initialized (see isInitialized() ).
206cdf0e10cSrcweir 
207cdf0e10cSrcweir         @throw RuntimeException in case the thread is not attached or the runtime
208cdf0e10cSrcweir                                 has not been initialized.
209cdf0e10cSrcweir     */
210cdf0e10cSrcweir     static void SAL_CALL initialize(
211cdf0e10cSrcweir         const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > & ctx )
212cdf0e10cSrcweir         throw ( com::sun::star::uno::RuntimeException );
213cdf0e10cSrcweir 
214cdf0e10cSrcweir 
215cdf0e10cSrcweir     /** Checks, whether the uno runtime is already initialized in the current python interpreter.
216cdf0e10cSrcweir      */
217cdf0e10cSrcweir     static bool SAL_CALL isInitialized() throw (com::sun::star::uno::RuntimeException);
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 
220cdf0e10cSrcweir     /** disposes the UNO bridge in this interpreter. All existing stubs/proxies
221cdf0e10cSrcweir         become non-functional, using these proxies/stubs leads to runtime errors.
222cdf0e10cSrcweir 
223cdf0e10cSrcweir         preconditions: python has been initialized before and
224cdf0e10cSrcweir         the global interpreter lock is held and pyuno was
225cdf0e10cSrcweir         initialized before for the currently in use interpreter.
226cdf0e10cSrcweir     */
227cdf0e10cSrcweir     static void SAL_CALL finalize() throw(com::sun::star::uno::RuntimeException );
228cdf0e10cSrcweir 
229cdf0e10cSrcweir     /** converts something contained in an UNO Any to a Python object
230cdf0e10cSrcweir 
231cdf0e10cSrcweir         preconditions: python has been initialized before,
232cdf0e10cSrcweir         the global interpreter lock is held and pyuno::Runtime
233cdf0e10cSrcweir         has been initialized.
234cdf0e10cSrcweir     */
235cdf0e10cSrcweir     PyRef any2PyObject (const com::sun::star::uno::Any &source ) const
236cdf0e10cSrcweir         throw ( com::sun::star::script::CannotConvertException,
237cdf0e10cSrcweir                 com::sun::star::lang::IllegalArgumentException,
238cdf0e10cSrcweir                 com::sun::star::uno::RuntimeException );
239cdf0e10cSrcweir 
240cdf0e10cSrcweir     /** converts a Python object to a UNO any
241cdf0e10cSrcweir 
242cdf0e10cSrcweir         preconditions: python has been initialized before,
243cdf0e10cSrcweir         the global interpreter lock is held and pyuno
244cdf0e10cSrcweir         has been initialized
245cdf0e10cSrcweir     */
246cdf0e10cSrcweir     com::sun::star::uno::Any pyObject2Any (
247cdf0e10cSrcweir         const PyRef & source , enum ConversionMode mode = REJECT_UNO_ANY ) const
248cdf0e10cSrcweir         throw ( com::sun::star::uno::RuntimeException);
249cdf0e10cSrcweir 
250cdf0e10cSrcweir     /** extracts a proper uno exception from a given python exception
251cdf0e10cSrcweir      */
252cdf0e10cSrcweir     com::sun::star::uno::Any extractUnoException(
253cdf0e10cSrcweir         const PyRef & excType, const PyRef & excValue, const PyRef & excTraceback) const;
254cdf0e10cSrcweir 
255cdf0e10cSrcweir     /** Returns the internal handle. Should only be used by the module implementation
256cdf0e10cSrcweir      */
getImpl() const257cdf0e10cSrcweir     RuntimeImpl *getImpl() const { return impl; }
258cdf0e10cSrcweir };
259cdf0e10cSrcweir 
260cdf0e10cSrcweir 
261cdf0e10cSrcweir /** helper class for attaching the current thread to the python runtime.
262cdf0e10cSrcweir 
263cdf0e10cSrcweir     Attaching is done creating a new threadstate for the given interpreter
264cdf0e10cSrcweir     and acquiring the global interpreter lock.
265cdf0e10cSrcweir 
266cdf0e10cSrcweir     Usage:
267cdf0e10cSrcweir 
268cdf0e10cSrcweir     ... don't use python here
269cdf0e10cSrcweir     {
270cdf0e10cSrcweir         PyThreadAttach guard( PyInterpreterState_Head() );
271cdf0e10cSrcweir         {
272cdf0e10cSrcweir             ... do whatever python code you want
273cdf0e10cSrcweir             {
274cdf0e10cSrcweir                PyThreadDetach antiguard;
275cdf0e10cSrcweir                ... don't use python here
276cdf0e10cSrcweir             }
277cdf0e10cSrcweir             ... do whatever python code you want
278cdf0e10cSrcweir         }
279cdf0e10cSrcweir     }
280cdf0e10cSrcweir     ... don't use python here
281cdf0e10cSrcweir 
282cdf0e10cSrcweir     Note: The additional scope brackets after the PyThreadAttach are needed,
283cdf0e10cSrcweir           e.g. when you would leave them away, dtors of potential pyrefs
284cdf0e10cSrcweir           may be called after the thread has detached again.
285cdf0e10cSrcweir  */
286cdf0e10cSrcweir class PY_DLLEXPORT PyThreadAttach
287cdf0e10cSrcweir {
288cdf0e10cSrcweir     PyThreadState *tstate;
289cdf0e10cSrcweir     PyThreadAttach ( const PyThreadAttach & ); // not implemented
290cdf0e10cSrcweir     PyThreadAttach & operator = ( const PyThreadAttach & );
291cdf0e10cSrcweir public:
292cdf0e10cSrcweir 
293cdf0e10cSrcweir     /** Creates a new python threadstate and acquires the global interpreter lock.
294cdf0e10cSrcweir         precondition: The current thread MUST NOT hold the global interpreter lock.
295cdf0e10cSrcweir         postcondition: The global interpreter lock is acquired
296cdf0e10cSrcweir 
297cdf0e10cSrcweir         @raises com::sun::star::uno::RuntimeException
298cdf0e10cSrcweir              in case no pythread state could be created
299cdf0e10cSrcweir      */
300cdf0e10cSrcweir     PyThreadAttach( PyInterpreterState *interp) throw ( com::sun::star::uno::RuntimeException );
301cdf0e10cSrcweir 
302cdf0e10cSrcweir 
303cdf0e10cSrcweir     /** Releases the global interpreter lock and destroys the thread state.
304cdf0e10cSrcweir      */
305cdf0e10cSrcweir     ~PyThreadAttach();
306cdf0e10cSrcweir };
307cdf0e10cSrcweir 
308cdf0e10cSrcweir /** helper class for detaching the current thread from the python runtime
309cdf0e10cSrcweir     to do some blocking, non-python related operation.
310cdf0e10cSrcweir 
311cdf0e10cSrcweir     @see PyThreadAttach
312cdf0e10cSrcweir */
313cdf0e10cSrcweir class PY_DLLEXPORT PyThreadDetach
314cdf0e10cSrcweir {
315cdf0e10cSrcweir     PyThreadState *tstate;
316cdf0e10cSrcweir     PyThreadDetach ( const PyThreadDetach & ); // not implemented
317cdf0e10cSrcweir     PyThreadDetach & operator = ( const PyThreadDetach & ); // not implemented
318cdf0e10cSrcweir 
319cdf0e10cSrcweir public:
320cdf0e10cSrcweir     /** Releases the global interpreter lock.
321cdf0e10cSrcweir 
322cdf0e10cSrcweir        precondition: The current thread MUST hold the global interpreter lock.
323cdf0e10cSrcweir        postcondition: The current thread does not hold the global interpreter lock anymore.
324cdf0e10cSrcweir     */
325cdf0e10cSrcweir     PyThreadDetach() throw ( com::sun::star::uno::RuntimeException );
326cdf0e10cSrcweir     /** Acquires the global interpreter lock again
327cdf0e10cSrcweir     */
328cdf0e10cSrcweir     ~PyThreadDetach();
329cdf0e10cSrcweir };
330cdf0e10cSrcweir 
331cdf0e10cSrcweir }
332cdf0e10cSrcweir #endif
333