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 40564d9007SPedro 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 46564d9007SPedro Giffuni // define PyBytes_* as the equivalent string type methods. 47564d9007SPedro Giffuni #ifndef PyBytes_Check 48564d9007SPedro Giffuni #define PyBytes_Check PyString_Check 49564d9007SPedro Giffuni #define PyBytes_AsString PyString_AsString 50564d9007SPedro Giffuni #define PyBytes_FromString PyString_FromString 51564d9007SPedro Giffuni #define PyBytes_Size PyString_Size 52564d9007SPedro Giffuni #define PyBytes_FromStringAndSize PyString_FromStringAndSize 53564d9007SPedro 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 71*1c25f252Sdamjan #define PY_DLLEXPORT SAL_DLLPUBLIC_EXPORT 72cdf0e10cSrcweir 73cdf0e10cSrcweir /** function called by the python runtime to initialize the 74cdf0e10cSrcweir pyuno module. 75cdf0e10cSrcweir 76cdf0e10cSrcweir preconditions: python has been initialized before and 77cdf0e10cSrcweir the global interpreter lock is held 78cdf0e10cSrcweir */ 79cdf0e10cSrcweir extern "C" PY_DLLEXPORT void SAL_CALL initpyuno(); 80cdf0e10cSrcweir 81cdf0e10cSrcweir 82cdf0e10cSrcweir namespace pyuno 83cdf0e10cSrcweir { 84cdf0e10cSrcweir 85cdf0e10cSrcweir /** Helper class for keeping references to python objects. 86cdf0e10cSrcweir BEWARE: Look up every python function you use to check 87cdf0e10cSrcweir wether you get an acquired or not acquired object pointer 88cdf0e10cSrcweir (python terminus for a not acquired object pointer 89cdf0e10cSrcweir is 'borrowed reference'). Use in the acquired pointer cases the 90cdf0e10cSrcweir PyRef( pointer, SAL_NO_ACQUIRE) ctor. 91cdf0e10cSrcweir 92cdf0e10cSrcweir precondition: python has been initialized before and 93cdf0e10cSrcweir the global interpreter lock is held 94cdf0e10cSrcweir 95cdf0e10cSrcweir */ 96cdf0e10cSrcweir class PyRef 97cdf0e10cSrcweir { 98cdf0e10cSrcweir PyObject *m; 99cdf0e10cSrcweir public: 100cdf0e10cSrcweir PyRef () : m(0) {} 101cdf0e10cSrcweir PyRef( PyObject * p ) : m( p ) { Py_XINCREF( m ); } 102cdf0e10cSrcweir 103cdf0e10cSrcweir PyRef( PyObject * p, __sal_NoAcquire ) : m( p ) {} 104cdf0e10cSrcweir 105cdf0e10cSrcweir PyRef( const PyRef &r ) : m( r.get() ) { Py_XINCREF( m ); } 106cdf0e10cSrcweir 107cdf0e10cSrcweir ~PyRef() { Py_XDECREF( m ); } 108cdf0e10cSrcweir 109cdf0e10cSrcweir PyObject *get() const { return m; } 110cdf0e10cSrcweir 111cdf0e10cSrcweir PyObject * getAcquired() const 112cdf0e10cSrcweir { 113cdf0e10cSrcweir Py_XINCREF( const_cast< PyObject*> (m) ); 114cdf0e10cSrcweir return m; 115cdf0e10cSrcweir } 116cdf0e10cSrcweir 117cdf0e10cSrcweir PyRef & operator = ( const PyRef & r ) 118cdf0e10cSrcweir { 119cdf0e10cSrcweir PyObject *tmp = m; 120cdf0e10cSrcweir m = r.getAcquired(); 121cdf0e10cSrcweir Py_XDECREF( tmp ); 122cdf0e10cSrcweir return *this; 123cdf0e10cSrcweir } 124cdf0e10cSrcweir 125cdf0e10cSrcweir bool operator == ( const PyRef & r ) const 126cdf0e10cSrcweir { 127cdf0e10cSrcweir return r.get() == m; 128cdf0e10cSrcweir } 129cdf0e10cSrcweir 130cdf0e10cSrcweir /** clears the reference without decreasing the reference count 13130acf5e8Spfg only seldom needed ! */ 132cdf0e10cSrcweir void scratch() 133cdf0e10cSrcweir { 134cdf0e10cSrcweir m = 0; 135cdf0e10cSrcweir } 136cdf0e10cSrcweir 137cdf0e10cSrcweir /** clears the reference decreasing the refcount of the holded object. 138cdf0e10cSrcweir */ 139cdf0e10cSrcweir void clear() 140cdf0e10cSrcweir { 141cdf0e10cSrcweir Py_XDECREF( m ); 142cdf0e10cSrcweir m = 0; 143cdf0e10cSrcweir } 144cdf0e10cSrcweir 145cdf0e10cSrcweir /** returns 1 when the reference points to a python object python object, 146cdf0e10cSrcweir otherwise 0. 147cdf0e10cSrcweir */ 148cdf0e10cSrcweir sal_Bool is() const 149cdf0e10cSrcweir { 150cdf0e10cSrcweir return m != 0; 151cdf0e10cSrcweir } 152cdf0e10cSrcweir 153cdf0e10cSrcweir struct Hash 154cdf0e10cSrcweir { 155cdf0e10cSrcweir sal_IntPtr operator () ( const PyRef &r) const { return sal_IntPtr( r.get() ); } 156cdf0e10cSrcweir }; 157cdf0e10cSrcweir }; 158cdf0e10cSrcweir 159cdf0e10cSrcweir struct stRuntimeImpl; 160cdf0e10cSrcweir typedef struct stRuntimeImpl RuntimeImpl; 161cdf0e10cSrcweir 162cdf0e10cSrcweir enum ConversionMode { ACCEPT_UNO_ANY, REJECT_UNO_ANY }; 163cdf0e10cSrcweir 164cdf0e10cSrcweir 165cdf0e10cSrcweir /** The pyuno::Runtime class keeps the internal state of the python UNO bridge 166cdf0e10cSrcweir for the currently in use python interpreter. 167cdf0e10cSrcweir 168cdf0e10cSrcweir You may keep a Runtime instance, use it from a different thread, etc. But you must 169cdf0e10cSrcweir make sure to fulfill all preconditions mentioned for the specific methods. 170cdf0e10cSrcweir */ 171cdf0e10cSrcweir 172cdf0e10cSrcweir class PY_DLLEXPORT Runtime 173cdf0e10cSrcweir { 174cdf0e10cSrcweir RuntimeImpl *impl; 175cdf0e10cSrcweir public: 176cdf0e10cSrcweir ~Runtime( ); 177cdf0e10cSrcweir 178cdf0e10cSrcweir /** 179cdf0e10cSrcweir preconditions: python has been initialized before, 180cdf0e10cSrcweir the global interpreter lock is held and pyuno 181cdf0e10cSrcweir has been initialized for the currently used interpreter. 182cdf0e10cSrcweir 183cdf0e10cSrcweir Note: This method exists for efficiency reasons to save 184cdf0e10cSrcweir lookup costs for any2PyObject and pyObject2Any 185cdf0e10cSrcweir 186cdf0e10cSrcweir @throw RuntimeException in case the runtime has not been 187cdf0e10cSrcweir initialized before 188cdf0e10cSrcweir */ 189cdf0e10cSrcweir Runtime() throw( com::sun::star::uno::RuntimeException ); 190cdf0e10cSrcweir 191cdf0e10cSrcweir Runtime( const Runtime & ); 192cdf0e10cSrcweir Runtime & operator = ( const Runtime & ); 193cdf0e10cSrcweir 194cdf0e10cSrcweir /** Initializes the python-UNO bridge. May be called only once per python interpreter. 195cdf0e10cSrcweir 196cdf0e10cSrcweir @param ctx the component context is used to instantiate bridge services needed 197cdf0e10cSrcweir for bridging such as invocation, typeconverter, invocationadapterfactory, etc. 198cdf0e10cSrcweir 199cdf0e10cSrcweir preconditions: python has been initialized before and 200cdf0e10cSrcweir the global interpreter lock is held and pyuno is not 201cdf0e10cSrcweir initialized (see isInitialized() ). 202cdf0e10cSrcweir 203cdf0e10cSrcweir @throw RuntimeException in case the thread is not attached or the runtime 204cdf0e10cSrcweir has not been initialized. 205cdf0e10cSrcweir */ 206cdf0e10cSrcweir static void SAL_CALL initialize( 207cdf0e10cSrcweir const com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext > & ctx ) 208cdf0e10cSrcweir throw ( com::sun::star::uno::RuntimeException ); 209cdf0e10cSrcweir 210cdf0e10cSrcweir 211cdf0e10cSrcweir /** Checks, whether the uno runtime is already initialized in the current python interpreter. 212cdf0e10cSrcweir */ 213cdf0e10cSrcweir static bool SAL_CALL isInitialized() throw (com::sun::star::uno::RuntimeException); 214cdf0e10cSrcweir 215cdf0e10cSrcweir 216cdf0e10cSrcweir /** disposes the UNO bridge in this interpreter. All existing stubs/proxies 217cdf0e10cSrcweir become non-functional, using these proxies/stubs leads to runtime errors. 218cdf0e10cSrcweir 219cdf0e10cSrcweir preconditions: python has been initialized before and 220cdf0e10cSrcweir the global interpreter lock is held and pyuno was 221cdf0e10cSrcweir initialized before for the currently in use interpreter. 222cdf0e10cSrcweir */ 223cdf0e10cSrcweir static void SAL_CALL finalize() throw(com::sun::star::uno::RuntimeException ); 224cdf0e10cSrcweir 225cdf0e10cSrcweir /** converts something contained in an UNO Any to a Python object 226cdf0e10cSrcweir 227cdf0e10cSrcweir preconditions: python has been initialized before, 228cdf0e10cSrcweir the global interpreter lock is held and pyuno::Runtime 229cdf0e10cSrcweir has been initialized. 230cdf0e10cSrcweir */ 231cdf0e10cSrcweir PyRef any2PyObject (const com::sun::star::uno::Any &source ) const 232cdf0e10cSrcweir throw ( com::sun::star::script::CannotConvertException, 233cdf0e10cSrcweir com::sun::star::lang::IllegalArgumentException, 234cdf0e10cSrcweir com::sun::star::uno::RuntimeException ); 235cdf0e10cSrcweir 236cdf0e10cSrcweir /** converts a Python object to a UNO any 237cdf0e10cSrcweir 238cdf0e10cSrcweir preconditions: python has been initialized before, 239cdf0e10cSrcweir the global interpreter lock is held and pyuno 240cdf0e10cSrcweir has been initialized 241cdf0e10cSrcweir */ 242cdf0e10cSrcweir com::sun::star::uno::Any pyObject2Any ( 243cdf0e10cSrcweir const PyRef & source , enum ConversionMode mode = REJECT_UNO_ANY ) const 244cdf0e10cSrcweir throw ( com::sun::star::uno::RuntimeException); 245cdf0e10cSrcweir 246cdf0e10cSrcweir /** extracts a proper uno exception from a given python exception 247cdf0e10cSrcweir */ 248cdf0e10cSrcweir com::sun::star::uno::Any extractUnoException( 249cdf0e10cSrcweir const PyRef & excType, const PyRef & excValue, const PyRef & excTraceback) const; 250cdf0e10cSrcweir 251cdf0e10cSrcweir /** Returns the internal handle. Should only be used by the module implementation 252cdf0e10cSrcweir */ 253cdf0e10cSrcweir RuntimeImpl *getImpl() const { return impl; } 254cdf0e10cSrcweir }; 255cdf0e10cSrcweir 256cdf0e10cSrcweir 257cdf0e10cSrcweir /** helper class for attaching the current thread to the python runtime. 258cdf0e10cSrcweir 259cdf0e10cSrcweir Attaching is done creating a new threadstate for the given interpreter 260cdf0e10cSrcweir and acquiring the global interpreter lock. 261cdf0e10cSrcweir 262cdf0e10cSrcweir Usage: 263cdf0e10cSrcweir 264cdf0e10cSrcweir ... don't use python here 265cdf0e10cSrcweir { 266cdf0e10cSrcweir PyThreadAttach guard( PyInterpreterState_Head() ); 267cdf0e10cSrcweir { 268cdf0e10cSrcweir ... do whatever python code you want 269cdf0e10cSrcweir { 270cdf0e10cSrcweir PyThreadDetach antiguard; 271cdf0e10cSrcweir ... don't use python here 272cdf0e10cSrcweir } 273cdf0e10cSrcweir ... do whatever python code you want 274cdf0e10cSrcweir } 275cdf0e10cSrcweir } 276cdf0e10cSrcweir ... don't use python here 277cdf0e10cSrcweir 278cdf0e10cSrcweir Note: The additional scope brackets after the PyThreadAttach are needed, 279cdf0e10cSrcweir e.g. when you would leave them away, dtors of potential pyrefs 280cdf0e10cSrcweir may be called after the thread has detached again. 281cdf0e10cSrcweir */ 282cdf0e10cSrcweir class PY_DLLEXPORT PyThreadAttach 283cdf0e10cSrcweir { 284cdf0e10cSrcweir PyThreadState *tstate; 285cdf0e10cSrcweir PyThreadAttach ( const PyThreadAttach & ); // not implemented 286cdf0e10cSrcweir PyThreadAttach & operator = ( const PyThreadAttach & ); 287cdf0e10cSrcweir public: 288cdf0e10cSrcweir 289cdf0e10cSrcweir /** Creates a new python threadstate and acquires the global interpreter lock. 290cdf0e10cSrcweir precondition: The current thread MUST NOT hold the global interpreter lock. 291cdf0e10cSrcweir postcondition: The global interpreter lock is acquired 292cdf0e10cSrcweir 293cdf0e10cSrcweir @raises com::sun::star::uno::RuntimeException 294cdf0e10cSrcweir in case no pythread state could be created 295cdf0e10cSrcweir */ 296cdf0e10cSrcweir PyThreadAttach( PyInterpreterState *interp) throw ( com::sun::star::uno::RuntimeException ); 297cdf0e10cSrcweir 298cdf0e10cSrcweir 299cdf0e10cSrcweir /** Releases the global interpreter lock and destroys the thread state. 300cdf0e10cSrcweir */ 301cdf0e10cSrcweir ~PyThreadAttach(); 302cdf0e10cSrcweir }; 303cdf0e10cSrcweir 304cdf0e10cSrcweir /** helper class for detaching the current thread from the python runtime 305cdf0e10cSrcweir to do some blocking, non-python related operation. 306cdf0e10cSrcweir 307cdf0e10cSrcweir @see PyThreadAttach 308cdf0e10cSrcweir */ 309cdf0e10cSrcweir class PY_DLLEXPORT PyThreadDetach 310cdf0e10cSrcweir { 311cdf0e10cSrcweir PyThreadState *tstate; 312cdf0e10cSrcweir PyThreadDetach ( const PyThreadDetach & ); // not implemented 313cdf0e10cSrcweir PyThreadDetach & operator = ( const PyThreadDetach & ); // not implemented 314cdf0e10cSrcweir 315cdf0e10cSrcweir public: 316cdf0e10cSrcweir /** Releases the global interpreter lock. 317cdf0e10cSrcweir 318cdf0e10cSrcweir precondition: The current thread MUST hold the global interpreter lock. 319cdf0e10cSrcweir postcondition: The current thread does not hold the global interpreter lock anymore. 320cdf0e10cSrcweir */ 321cdf0e10cSrcweir PyThreadDetach() throw ( com::sun::star::uno::RuntimeException ); 322cdf0e10cSrcweir /** Acquires the global interpreter lock again 323cdf0e10cSrcweir */ 324cdf0e10cSrcweir ~PyThreadDetach(); 325cdf0e10cSrcweir }; 326cdf0e10cSrcweir 327cdf0e10cSrcweir } 328cdf0e10cSrcweir #endif 329