The Gaudi Framework  v41r0 (bdbe7744)
Loading...
Searching...
No Matches
Algorithm.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11// ============================================================================
12// Include files
13// ============================================================================
14#include <Python.h>
15
16// Python 3 compatibility
17#if PY_MAJOR_VERSION >= 3
18
19# define PyInt_Check PyLong_Check
20
21# define PyInt_AS_LONG PyLong_AS_LONG
22
23#endif
24
25// ============================================================================
26// GaudiKernel
27// ============================================================================
29// ============================================================================
30// GaudiPython
31// ============================================================================
33// ============================================================================
34
35namespace {
42 inline char* chr( const char* c ) { return const_cast<char*>( c ); }
43} // namespace
44
45// ============================================================================
47// ============================================================================
48StatusCode GaudiPython::call_python_method( PyObject* self, const char* method ) {
50 // check arguments
51 if ( !self || !method ) { return StatusCode::FAILURE; }
52
53 // call Python
54 PyObject* r = PyObject_CallMethod( self, chr( method ), nullptr );
55
56 if ( !r ) {
57 PyErr_Print();
58 return sc;
59 } // RETURN
60
61 if ( PyInt_Check( r ) ) {
62 sc = StatusCode( PyInt_AS_LONG( r ) );
63 Py_DECREF( r );
64 return sc;
65 } // RETURN
66
67 // look for the method getCode with the signature:
68 // ' int getCode() '
69 PyObject* c = PyObject_CallMethod( r, chr( "getCode" ), nullptr );
70
71 if ( !c ) {
72 PyErr_Print();
73 } else if ( PyLong_Check( c ) ) {
74 sc = StatusCode( PyLong_AsLong( c ) );
75 } else {
76 std::string msg( " call_python_method unexpected type from '" );
77 msg += method;
78 msg += "().getCode()' ";
79 PyErr_SetString( PyExc_TypeError, msg.c_str() );
80 PyErr_Print();
81 }
82 // release used objects
83 Py_XDECREF( c );
84 Py_XDECREF( r );
85 //
86 return sc;
87}
88// ============================================================================
89
90// ============================================================================
95// ============================================================================
96GaudiPython::PyAlgorithm::PyAlgorithm( PyObject* self, const std::string& name )
97 : Algorithm( name, Gaudi::svcLocator() ), m_self( self ) {
98 // The owner of the Algorithm is Python (as creator) therefore
99 // it should not be deleted by Gaudi (added an extra addRef()).
100 addRef();
101 addRef();
102}
103// ============================================================================
104
105// ============================================================================
107// ============================================================================
109// ============================================================================
111// ============================================================================
113// ============================================================================
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition Algorithm.h:98
const std::string & name() const override
The identifying name of the algorithm object.
StatusCode initialize() override
StatusCode execute() override
StatusCode finalize() override
StatusCode start() override
PyAlgorithm(PyObject *self, const std::string &name)
constructor
Definition Algorithm.cpp:96
StatusCode stop() override
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto FAILURE
Definition StatusCode.h:100
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition __init__.py:1
GAUDI_API StatusCode call_python_method(PyObject *self, const char *method)
call the python method
Definition Algorithm.cpp:48