/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
enum NotNull
{ /** definition of a no acquire enum for ctors
*/
NOT_NULL
};
/** Helper class for keeping references to python objects. BEWARE: Look up every python function you use to check whether you get an acquired or not acquired object pointer (python terminus for a not acquired object pointer is 'borrowed reference'). Use in the acquired pointer cases the PyRef( pointer, SAL_NO_ACQUIRE) ctor.
precondition: python has been initialized before and the global interpreter lock is held
*/ class PyRef
{
PyObject *m; public:
PyRef () : m(nullptr) {}
PyRef( PyObject * p ) : m( p ) { Py_XINCREF( m ); }
/** The pyuno::Runtime class keeps the internal state of the python UNO bridge for the currently in use python interpreter.
You may keep a Runtime instance, use it from a different thread, etc. But you must make sure to fulfill all preconditions mentioned for the specific methods.
*/
class LO_DLLPUBLIC_PYUNO Runtime
{
RuntimeImpl *impl;
/** Safely unpacks a Python iterator into a sequence, then stores it in an Any. Used internally by pyObject2Any
*/ bool pyIterUnpack( PyObject *const, css::uno::Any & ) const; public:
~Runtime( );
/** preconditions: python has been initialized before, the global interpreter lock is held and pyuno has been initialized for the currently used interpreter.
Note: This method exists for efficiency reasons to save lookup costs for any2PyObject and pyObject2Any
@throw RuntimeException in case the runtime has not been initialized before
*/
Runtime();
/** Initializes the python-UNO bridge. May be called only once per python interpreter.
@param ctx the component context is used to instantiate bridge services needed for bridging such as invocation, typeconverter, invocationadapterfactory, etc.
preconditions: python has been initialized before and the global interpreter lock is held and pyuno is not initialized (see isInitialized() ).
@throw RuntimeException in case the thread is not attached or the runtime has not been initialized.
*/ staticvoid initialize( const css::uno::Reference< css::uno::XComponentContext > & ctx );
/** Checks, whether the uno runtime is already initialized in the current python interpreter.
/** extracts a proper uno exception from a given python exception
*/
css::uno::Any extractUnoException( const PyRef & excType, const PyRef & excValue, const PyRef & excTraceback) const;
/** Returns the internal handle. Should only be used by the module implementation
*/
RuntimeImpl *getImpl() const { return impl; }
};
/** helper class for attaching the current thread to the python runtime.
Attaching is done creating a new threadstate for the given interpreter and acquiring the global interpreter lock.
Usage:
... don't use python here { PyThreadAttach guard( PyInterpreterState_Head() ); { ... do whatever python code you want { PyThreadDetach antiguard; ... don't use python here } ... do whatever python code you want } } ... don't use python here
Note: The additional scope brackets after the PyThreadAttach are needed, e.g. when you would leave them away, dtors of potential pyrefs may be called after the thread has detached again.
*/ class LO_DLLPUBLIC_PYUNO PyThreadAttach
{
PyThreadState *tstate; bool m_isNewState;
PyThreadAttach ( const PyThreadAttach & ) = delete;
PyThreadAttach & operator = ( const PyThreadAttach & ) = delete; public:
/** Creates a new python threadstate and acquires the global interpreter lock. precondition: The current thread MUST NOT hold the global interpreter lock. postcondition: The global interpreter lock is acquired
@throws css::uno::RuntimeException in case no pythread state could be created
*/
PyThreadAttach( PyInterpreterState *interp);
/** Releases the global interpreter lock and destroys the thread state.
*/
~PyThreadAttach();
};
/** helper class for detaching the current thread from the python runtime to do some blocking, non-python related operation.
precondition: The current thread MUST hold the global interpreter lock. postcondition: The current thread does not hold the global interpreter lock anymore.
@throws css::uno::RuntimeException
*/
PyThreadDetach(); /** Acquires the global interpreter lock again
*/
~PyThreadDetach();
};
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.