Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
Compatibility.h
Go to the documentation of this file.
1#ifndef CPYRT_COMPATIBILITY_H
2#define CPYRT_COMPATIBILITY_H
3
4#include "Python.h"
5
6#include "PyStrings.h"
7#include "cppjit_interop.h"
8
9#include <string>
10
12
14#ifdef CPPJIT_USE_CLING
15 static PyObject* printValue = nullptr;
16 if (printValue)
17 return printValue;
18
19 PyObject* gbl =
20 PyDict_GetItemString(PySys_GetObject((char*)"modules"), "cppjit.gbl");
21 PyObject* cling = gbl ? PyObject_GetAttrString(gbl, (char*)"cling") : nullptr;
22 printValue =
23 cling ? PyObject_GetAttrString(cling, (char*)"printValue") : nullptr;
24 Py_XDECREF(cling);
25
26 if (printValue) {
27 Py_DECREF(printValue); // make borrowed
28 if (!PyCallable_Check(printValue))
29 printValue = nullptr;
30 }
31
32 if (!printValue)
33 PyErr_Clear();
34
35 return printValue;
36#else
37 return nullptr;
38#endif
39}
40
41// Interpreter::toString is an assert(0) stub (CppInterOp#1100); skip it.
42inline std::string ObjToString(interop::TCppScope_t /*klass*/,
43 interop::TCppObject_t /*obj*/) {
44 return "";
45}
46
47//- cppyy legacy API ----------------------------------------------------------
48// Backwards compatibility with the API that former cppyy users have in their
49// code. The macro is deliberately not a build option: it only exists so that
50// the legacy code paths are easy to find, and to remove once there is a
51// transition plan. Keep all such shims in this section.
52#define CPPYY_LEGACY_API 1
53
54// Name under which pyclass defines its explicit pythonization hook, looked up
55// in its own __dict__ only (no fall-through to base classes). Prefers the
56// __cppjit_* spelling over the old __cppyy_* one. Returns a borrowed reference,
57// or nullptr with no error set if there is no such hook.
59 // go through __dict__ instead of tp_getattr, see HasAttrDirect in
60 // Pythonize.cxx for the rationale
61 PyObject* name = nullptr;
62 PyObject* dct = PyObject_GetAttr(pyclass, PyStrings::gDict);
63 if (dct) {
64 PyObject* candidates[] = {
66#if CPPYY_LEGACY_API
68#endif
69 };
70 for (PyObject* candidate : candidates) {
71 PyObject* attr = PyObject_GetItem(dct, candidate);
72 if (attr) {
73 Py_DECREF(attr);
74 name = candidate;
75 break;
76 }
77 PyErr_Clear();
78 }
79 Py_DECREF(dct);
80 }
81 PyErr_Clear();
82 return name;
83}
84
85// Normal pythonization hook of pyclass, through regular attribute lookup (so
86// it falls through to base classes). Prefers the __cppjit_* spelling over the
87// old __cppyy_* one. Returns a new reference, or nullptr with no error set if
88// there is no such hook.
90 PyObject* func = PyObject_GetAttr(pyclass, PyStrings::gPythonize);
91#if CPPYY_LEGACY_API
92 if (!func) {
93 PyErr_Clear();
94 func = PyObject_GetAttr(pyclass, PyStrings::gCppyyPythonize);
95 }
96#endif
97 if (!func)
98 PyErr_Clear();
99 return func;
100}
101
102} // namespace cppjit::cpyrt::compat
103
104#endif // CPYRT_COMPATIBILITY_H
_object PyObject
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t attr
char name[80]
Definition TGX11.cxx:142
PyObject * GetPythonizeHook(PyObject *pyclass)
PyObject * GetClingPrintValue()
std::string ObjToString(interop::TCppScope_t, interop::TCppObject_t)
PyObject * ExplicitPythonizeHookName(PyObject *pyclass)
Cpp::ObjectRef TCppObject_t
Definition API.h:43
Cpp::DeclRef TCppScope_t
Definition API.h:41