Skip to content
Snippets Groups Projects
CppCallablePyz.cxx 24.6 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
// Author: Stefan Wunsch, Enric Tejedor CERN  04/2019
// Original PyROOT code by Wim Lavrijsen, LBL

/*************************************************************************
 * Copyright (C) 1995-2018, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/

#include "Python.h"

#include "CPyCppyy.h"
#include "PyROOTPythonize.h"
#include "CPPInstance.h"
#include "Utility.h"
#include "TInterpreter.h"
#include "TInterpreterValue.h"

#include <sstream>


// Parse positional arguments of the decorator
long ParsePositionalArgs(PyObject* args)
{
   if (!PyTuple_Check(args)) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to parse positional arguments: Invalid tuple.");
      return NULL;
   }

   if (PyTuple_Size(args) != 3) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to parse positional arguments: Expect exactly two positional arguments (list of input types, return type).");
      return NULL;
   }

   auto instance = PyTuple_GetItem(args, 0);
   auto inputTypes = PyTuple_GetItem(args, 1);
   auto returnType = PyTuple_GetItem(args, 2);

   // Attach arguments to instance
   PyObject_SetAttrString(instance, "input_types", inputTypes);
   PyObject_SetAttrString(instance, "return_type", returnType);

   return 1l;
}


// Attach keyword to instance
long AttachKeyword(PyObject* kwargs, PyObject* instance, const char* name)
{
   PyObject* p;
   if ((p = PyDict_GetItemString(kwargs, name))) {
      const auto status = PyObject_IsTrue(p);
      if (status == 1) {
         PyObject_SetAttrString(instance, name, Py_True);
      } else if (status == 0) {
         PyObject_SetAttrString(instance, name, Py_False);
      } else {
         return NULL;
      }
   }
   return 1l;
}


// Parse keyword arguments of the decorator
long ParseKeywordArguments(PyObject* args, PyObject* kwargs)
{
   if (!PyDict_Check(kwargs)) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to parse keyword arguments: Invalid dictionary.");
      return NULL;
   }

   // Attach optional name to instance
   auto instance = PyTuple_GetItem(args, 0);
   PyObject* p;
   if ((p = PyDict_GetItemString(kwargs, "name"))) {
      if (!CPyCppyy_PyUnicode_Check(p)) {
         PyErr_SetString(PyExc_RuntimeError,
                 "Failed to parse arguments: Given name is not a valid string.");
         return NULL;
      }
      PyObject_SetAttrString(instance, "name", p);
   }

   // Attach optional numpy_only flag to instance
   if ((AttachKeyword(kwargs, instance, "numba_only") == NULL)) {
      PyErr_SetString(PyExc_RuntimeError,
              "Failed to parse arguments: Given object for numba_only cannot be evaluated as a boolean.");
      return NULL;
   }

   // Attach optional generic_only flag to instance
   if ((AttachKeyword(kwargs, instance, "generic_only") == NULL)) {
      PyErr_SetString(PyExc_RuntimeError,
              "Failed to parse arguments: Given object for generic_only cannot be evaluated as a boolean.");
      return NULL;
   }

   // Attach optional verbose flag
   if ((AttachKeyword(kwargs, instance, "verbose") == NULL)) {
      PyErr_SetString(PyExc_RuntimeError,
              "Failed to parse arguments: Given object for verbose flag cannot be evaluated as a boolean.");
      return NULL;
   }

   return 1l;
}


// Init of class used as decorator to create generic C++ wrapper
// The init parses the arguments passed to the decorator.
PyObject* GenericCallableImpl_init(PyObject * /*self*/, PyObject *args, PyObject *kwargs)
{
   if(ParsePositionalArgs(args) == NULL) return NULL;

   if(kwargs != 0) {
      if(ParseKeywordArguments(args, kwargs) == NULL) return NULL;
   }

   Py_RETURN_NONE;
}


// Check arguments given to call operator of the decorator
long CheckCallArgs(PyObject* args)
{
   if (!PyTuple_Check(args)) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to parse arguments: Invalid tuple.");
      return NULL;
   }

   if (!(PyTuple_Size(args) == 2)) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to parse arguments: Expect exactly one argument (Python callable).");
      return NULL;
   }

   return 1l;
}


// Check instance passed from init to call operator of the decorator
long CheckInstance(PyObject* instance)
{
   if (!PyObject_HasAttrString(instance, "input_types")) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to create C++ callable: No input_types attribute found.");
      return NULL;
   }

   if (!PyObject_HasAttrString(instance, "return_type")) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to create C++ callable: No return_type attribute found.");
      return NULL;
   }

   return 1l;
}


// Check callable passed to decorator
long CheckCallable(PyObject* callable)
{
   if (!PyCallable_Check(callable)) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to create C++ callable: Given PyObject is not callable.");
      return NULL;
   }
   return 1l;
}


// Extract name of callable passed to call operator of the decorator.
// Either extract the name from the optional keyword argument or from the
// __name__ property of the callable itself.
std::string ExtractName(PyObject* instance, PyObject* pyfunc)
{
   PyObject* pyname;
   if (PyObject_HasAttrString(instance, "name")) {
      pyname = PyObject_GetAttrString(instance, "name");
   } else {
      if (!PyObject_HasAttrString(pyfunc, "__name__")) {
         PyErr_SetString(PyExc_RuntimeError, "Failed to create C++ callable: Python callable does not have attribute __name__.");
         return "";
      }
      pyname = PyObject_GetAttrString(pyfunc, "__name__");
   }
   std::string name = CPyCppyy_PyUnicode_AsString(pyname);
   Py_DECREF(pyname);
   return name;
}


// Call method of class used as decorator to create generic C++ wrapper
// The call method creates the C++ wrapper class for the Python callable and
// passes through the actual callable.
PyObject* GenericCallableImpl_call(PyObject * /*self*/, PyObject *args)
{
   // Parse arguments
   if(CheckCallArgs(args) == NULL) return NULL;
   auto instance = PyTuple_GetItem(args, 0);
   auto pyfunc = PyTuple_GetItem(args, 1);
   if(CheckCallable(pyfunc) == NULL) return NULL;
   Py_INCREF(pyfunc);
   if(CheckInstance(instance) == NULL) return NULL;

   auto inputTypes = PyObject_GetAttrString(instance, "input_types");
   auto returnType = PyObject_GetAttrString(instance, "return_type");

   // Extract name of Python callable
   auto name = ExtractName(instance, pyfunc);
   if (name.compare("") == 0) return NULL;

   // Get C++ return type
   if (!CPyCppyy_PyUnicode_Check(returnType)) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to create C++ callable: Return type argument cannot be interpreted as string.");
      return NULL;
   }
   std::string returnTypeStr = CPyCppyy_PyUnicode_AsString(returnType);
   Py_DECREF(returnType);
   if (returnTypeStr.compare("") == 0) {
      returnTypeStr = "void";
   }

   // Put function in namespace
   std::stringstream code;
   code << "namespace CppCallable {\n";

   // Set return type
   code << returnTypeStr << " ";

   // Set name of Python callable as function name
   code << name;

   // Build function signature, type string and list of variables
   code << "(";

   auto iter = PyObject_GetIter(inputTypes);
   auto inputTypesSize = PyObject_Size(inputTypes);
   Py_DECREF(inputTypes);
   if (!iter) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to create C++ callable: Failed to iterate over input types.");
      return NULL;
   }

   // Map C++ types to type characters of Python/C API (PyObject_CallFunction)
   std::map<std::string, std::string> typemap = {
       {"float", "f"},
       {"double", "f"},
       {"int", "i"},
       {"unsigned int", "I"},
       {"long", "l"},
       {"unsigned long", "k"},
   };

   PyObject *item;
   auto idx = 0u;
   std::stringstream typestr;
   std::vector<std::string> pytypes(inputTypesSize);
   std::vector<std::string> inputTypesStr(inputTypesSize);
   std::stringstream vars;
   while ((item = PyIter_Next(iter))) {
      // Convert argument to string
      if (!CPyCppyy_PyUnicode_Check(item)) {
         Py_DECREF(iter);
         Py_DECREF(item);
         PyErr_SetString(PyExc_RuntimeError, "Failed to create C++ callable: Failed to interpret input type as string.");
         return NULL;
      }

      inputTypesStr[idx] = CPyCppyy_PyUnicode_AsString(item);
      Py_DECREF(item);

      auto pytype = typemap.find(inputTypesStr[idx]);
      if (pytype != typemap.end()) { // Types in typemap
         pytypes[idx] = pytype->second;
         vars << pytypes[idx] << "_" << idx;
         code << inputTypesStr[idx] << " " << pytypes[idx] << "_" << idx;
      } else if (inputTypesStr[idx].compare("") == 0) { // No input type
         pytypes[idx] = "";
      } else if (inputTypesStr[idx].compare("bool") == 0) { // Bool
         pytypes[idx] = "O";
         vars << "pyb_" << idx;
         code << inputTypesStr[idx] << " b_" << idx;
      } else { // C++ object
         pytypes[idx] = "O";
         vars << "pyo_" << idx;
         code << inputTypesStr[idx] << "& o_" << idx;
      }
      typestr << pytypes[idx];

      if (idx != inputTypesSize - 1 && pytypes[idx].compare("") != 0) {
         code << ", ";
         vars << ", ";
      }

      idx++;
   }
   Py_DECREF(iter);

   // Acquire lock to protect multi-threaded scenarios
   code << ") {\n"
        << "   // Acquire lock to protect multi-threaded scenarios\n"
        << "   R__WRITE_LOCKGUARD(ROOT::gCoreMutex);\n\n";

   // Get pointer to Python callable
   code << "   // Get Python callable from pointer\n"
        << "   auto pyfunc = reinterpret_cast<PyObject*>(" << pyfunc << ");\n"
        << "   if (!PyCallable_Check(pyfunc)) {\n"
        << "      throw std::runtime_error(\"Python object " << name << " is not callable.\");\n"
        << "   }\n\n";

   // Build Python proxies of the C++ objects for Python
   code << "   // Build Python proxies for C++ objects\n";
   std::stringstream cleanup; // Register clean-up code
   bool hasPyBoolIncref = false;
   for (std::size_t i = 0; i < pytypes.size(); i++) {
      if (inputTypesStr[i].compare("bool") == 0) { // Bool
         if (!hasPyBoolIncref) {
            code << "   Py_INCREF(Py_True);\n"
                 << "   Py_INCREF(Py_False);\n";
            cleanup << "   Py_DECREF(Py_True);\n"
                    << "   Py_DECREF(Py_False);\n";
            hasPyBoolIncref = true;
         }
         code << "   auto pyb_" << i << " = b_" << i << " ? Py_True : Py_False;\n";
      } else if (pytypes[i] == "O") { // C++ objects
         code << "   auto pyo_" << i << " = TPython::CPPInstance_FromVoidPtr("
              << "&o_" << i << ", \"" << inputTypesStr[i] << "\");\n";
         cleanup << "   Py_DECREF(pyo_" << i << ");\n";
      }
   }
   code << "\n";

   // Call Python callable
   auto typestr_str = typestr.str();
   auto vars_str = vars.str();
   if (vars_str.compare("") != 0) {
      vars_str = ", " + vars_str;
   }
   code << "   // Call Python callable\n"
        << "   auto pyresult = PyObject_CallFunction(pyfunc, (char*)\"" << typestr_str << "\"" << vars_str << ");\n"
        << "   if (pyresult == 0) {\n"
        << "      PyErr_Print();\n"
        << "      throw std::runtime_error(\"Failed to call Python callable " << name << ".\");\n"
        << "   }\n\n";

   // Clean-up Python proxies
   code << "   // Clean-up Python proxies\n"
        << cleanup.str()
        << "\n";

   // Convert result to C++ type
   code << "   // Convert result to C++ type\n";
   if (returnTypeStr.compare("void") == 0) {
      code << "   Py_DECREF(pyresult);\n\n"
           << "   return;\n";
   } else if (returnTypeStr.compare("bool") == 0) {
      code << "   if (pyresult == Py_True) {\n"
           << "      Py_DECREF(pyresult);\n"
           << "      return true;\n"
           << "   } else if (pyresult == Py_False) {\n"
           << "      Py_DECREF(pyresult);\n"
           << "      return false;\n"
           << "   } else {\n"
           << "      PyErr_Print();\n"
           << "      throw std::runtime_error(\"Failed to convert return value of Python callable to C++ object: Python object is not of type PyBool.\");\n"
           << "   }\n";
   } else {
      auto pytype = typemap.find(returnTypeStr);
      if (pytype != typemap.end()) {
         if (pytype->second == "f") {
            code << "   auto result = PyFloat_AsDouble(pyresult);\n";
         } else if (pytype->second == "i" || pytype->second == "l") {
            code << "   auto result = PyLong_AsLong(pyresult);\n";
         } else if (pytype->second == "I" || pytype->second == "k") {
            code << "   auto result = PyLong_AsUnsignedLong(pyresult);\n";
         }
      } else {
         code << "   if (!TPython::CPPInstance_Check(pyresult)) {\n"
              << "      throw std::runtime_error(\"Failed to convert return value of Python callable to C++ object: Python object is not created by cppyy (CPPInstance).\");\n"
              << "      \n"
              << "   }\n";
         code << "   auto result = *reinterpret_cast<" << returnTypeStr << "*>(TPython::CPPInstance_AsVoidPtr(pyresult));\n";
      }

      code << "   Py_DECREF(pyresult);\n\n"
           << "   return result;\n";
   }
   code << "}\n}";

   // Attach C++ wrapper code to callable
   auto code_str = code.str();
   auto code_cstr = code_str.c_str();
   auto pycode = CPyCppyy_PyUnicode_FromString(code_cstr);
   PyObject_SetAttrString(pyfunc, "__cpp_wrapper__", pycode);
   Py_DECREF(pycode);

   // Jit C++ wrapper
   auto err = gInterpreter->Declare("#include \"Python.h\"");
   if (!err) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to compile C++ wrapper: Failed to include Python.h.");
      return NULL;
   }

   err = gInterpreter->Declare("#include \"TPython.h\"");
   if (!err) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to compile C++ wrapper: Failed to include TPython.h.");
      return NULL;
   }

   err = gInterpreter->Declare(code_cstr);
   if (!err) {
      PyErr_SetString(PyExc_RuntimeError,
              ("Failed to compile C++ wrapper: Compilation error from following wrapper code.\n" + code.str()).c_str());
      return NULL;
   }

   // Pass through Python callable
   return pyfunc;
}


// Call method of class used as decorator to create C++ wrapper using numba
// The call method creates the C++ wrapper class for the Python callable and
// passes through the actual callable.
PyObject* NumbaCallableImpl_call(PyObject * /*self*/, PyObject *args)
{
   // Parse arguments
   if(CheckCallArgs(args) == NULL) return NULL;
   auto instance = PyTuple_GetItem(args, 0);
   auto pyfunc = PyTuple_GetItem(args, 1);
   if(CheckCallable(pyfunc) == NULL) return NULL;
   Py_INCREF(pyfunc);
   if(CheckInstance(instance) == NULL) return NULL;

   auto inputTypes = PyObject_GetAttrString(instance, "input_types");
   auto returnType = PyObject_GetAttrString(instance, "return_type");

   // Extract name of Python callable
   auto name = ExtractName(instance, pyfunc);
   if (name.compare("") == 0) return NULL;

   // Get C++ return type
   if (!CPyCppyy_PyUnicode_Check(returnType)) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to create C++ callable: Return type argument cannot be interpreted as string.");
      return NULL;
   }
   std::string returnTypeStr = CPyCppyy_PyUnicode_AsString(returnType);
   Py_DECREF(returnType);
   if (returnTypeStr.compare("") == 0) {
      returnTypeStr = "void";
   }

   // Find numba types for C++ types
   std::map<std::string, std::string> typemap = {
       {"float", "float32"},
       {"double", "float64"},
       {"int", "int32"},
       {"unsigned int", "uint32"},
       {"long", "int64"},
       {"unsigned long", "uint64"},
       {"bool", "boolean"},
   };

   auto iter = PyObject_GetIter(inputTypes);
   Py_DECREF(inputTypes);
   if (!iter) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to create C++ callable: Failed to iterate over input types.");
      return NULL;
   }

   PyObject *item;
   std::vector<std::string> numbaTypes;
   std::vector<std::string> cppTypes;
   while ((item = PyIter_Next(iter))) {
      // Convert argument to string
      if (!CPyCppyy_PyUnicode_Check(item)) {
         Py_DECREF(iter);
         Py_DECREF(item);
         PyErr_SetString(PyExc_RuntimeError, "Failed to create C++ callable: Failed to interpret input type as string.");
         return NULL;
      }

      const std::string cpptype = CPyCppyy_PyUnicode_AsString(item);
      Py_DECREF(item);

      auto t = typemap.find(cpptype);
      if (t != typemap.end()) { // Types in typemap
         numbaTypes.emplace_back(t->second);
         cppTypes.emplace_back(cpptype);
      } else if (cpptype.compare("") == 0) { // No input, skip
      } else {
         Py_DECREF(iter);
         Py_DECREF(item);
         PyErr_SetString(PyExc_RuntimeError,
                 ("Failed to create C++ callable: Input type " + cpptype + " is not valid for jitting with numba.").c_str());
         return NULL;
      }
   }
   Py_DECREF(iter);

   // Import numba
   auto numba = PyImport_ImportModule("numba");
   if (!numba) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to import numba.");
      return NULL;
   }

   // Get cfunc method
   auto cfunc = PyObject_GetAttrString(numba, "cfunc");
   Py_DECREF(numba);
   if (!cfunc) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to import cfunc from numba.");
      return NULL;
   }

   // Jit Python callable
   std::stringstream numbaSignature;
   auto t = typemap.find(returnTypeStr);
   if (t != typemap.end()) {
      numbaSignature << typemap[returnTypeStr] << "(";
   } else if (returnTypeStr.compare("") == 0 || returnTypeStr.compare("void") == 0) {
      numbaSignature << "void(";
   } else {
      PyErr_SetString(PyExc_RuntimeError, "Failed to create C++ callable: Return type is not valid for jitting with numba.");
      return NULL;
   }
   for(std::size_t i = 0; i < numbaTypes.size(); i++) {
      numbaSignature << numbaTypes[i];
      if (i != numbaTypes.size() - 1) {
         numbaSignature << ", ";
      }
   }
   numbaSignature << ")";
   auto numbaSignatureStr = numbaSignature.str();
   auto args_ = Py_BuildValue("(s)", (char*)numbaSignatureStr.c_str());
   auto kwargs_ = Py_BuildValue("{s:O}", (char*)"nopython", Py_True);
   auto decorator = PyObject_Call(cfunc, args_, kwargs_);
   Py_DECREF(cfunc);
   if (!decorator) {
      PyErr_SetString(PyExc_RuntimeError,
              ("Failed to create C++ callable: Unable to create instance of numba.cfunc with signature "
              + numbaSignatureStr + ".").c_str());
      return NULL;
   }
   Py_DECREF(args_);
   Py_DECREF(kwargs_);

   auto jitted = PyObject_CallFunction(decorator, (char*)"O", pyfunc);
   Py_DECREF(decorator);
   if (!jitted) {
      PyObject *type, *value, *traceback;
      PyErr_Fetch(&type, &value, &traceback);
      auto pyerr = PyObject_Str(value);
      std::string pyerrstr = CPyCppyy_PyUnicode_AsString(pyerr);
      PyErr_SetString(PyExc_RuntimeError,
              ("Failed to create C++ callable: Unable to jit function using numba.cfunc with signature "
               + numbaSignatureStr + ":\n" + pyerrstr).c_str());
      Py_DECREF(pyerr);
      Py_DECREF(type);
      Py_DECREF(value);
      Py_DECREF(traceback);
      return NULL;
   }

   // Attach jitted function to callable
   PyObject_SetAttrString(pyfunc, "__numba_cfunc__", jitted);
   Py_DECREF(jitted);

   // Extract function pointer
   auto pyaddress = PyObject_GetAttrString(jitted, "address");
   if (!pyaddress) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to create C++ callable: Unable to extract function pointer from numba.cfunc.");
      return NULL;
   }
   auto address = PyLong_AsUnsignedLongLong(pyaddress);

   // Put wrapper function in ROOT namespace
   std::stringstream code;
   code << "namespace CppCallable {\n";

   // Set return type of wrapper functoin
   code << returnTypeStr << " ";

   // Set name of Python callable as function name
   code << name;

   // Build function signature, function pointer cast and variable list
   code << "(";
   std::stringstream vars;
   std::stringstream fPtr;
   fPtr << returnTypeStr << "(*)(";
   for(std::size_t i = 0; i < cppTypes.size(); i++) {
      code << cppTypes[i] << " x_" << i;
      vars << "x_" << i;
      fPtr << cppTypes[i];
      if (i != cppTypes.size() - 1) {
         code << ", ";
         vars << ", ";
         fPtr << ", ";
      }
   }
   code << ") {\n";
   fPtr << ")";

   // Cast int to C function pointer
   code << "   auto funcptr = reinterpret_cast<" << fPtr.str() << ">(" << address << ");\n";

   // Return result
   code << "   return funcptr(" << vars.str() << ");\n";

   // Close function and namespace
   code << "}\n}";

   // Jit C++ wrapper
   auto code_str = code.str();
   auto code_cstr = code_str.c_str();

   auto err = gInterpreter->Declare(code_cstr);
   if (!err) {
      PyErr_SetString(PyExc_RuntimeError,
              ("Failed to compile C++ wrapper: Compilation error from following wrapper code.\n" + code.str()).c_str());
      return NULL;
   }

   // Attach code function to callable
   auto pycode = CPyCppyy_PyUnicode_FromString(code_cstr);
   PyObject_SetAttrString(pyfunc, "__cpp_wrapper__", pycode);
   Py_DECREF(pycode);

   // Pass through Python callable
   return pyfunc;
}


bool GetKeyword(PyObject* obj, const char* name, bool defaultVal)
{
   bool prop = defaultVal;
   if (PyObject_HasAttrString(obj, name)) {
      auto attr = PyObject_GetAttrString(obj, name);
      prop = PyObject_IsTrue(attr);
      Py_DECREF(attr);
   }
   return prop;
}


// Call method of class used as decorator to create either generic or numba C++ wrapper.
// The call method creates the C++ wrapper class for the Python callable and
// passes through the actual callable.
PyObject* ProxyCallableImpl_call(PyObject * /*self*/, PyObject *args)
{
   // Get numba_only and generic_only optional arguments
   // The arguments are interpreted as follows (in this order):
   // 1) numba_only = true , generic_only = true or false: Just try numba implementation
   // 2) numba_only = false, generic_only = true: Just try generic implementation
   // 3) numba_only = false, generic_only = false: Try numba first, fail silently, go to generic (default setting)
   auto instance = PyTuple_GetItem(args, 0);

   auto numbaOnly = GetKeyword(instance, "numba_only", false);
   auto genericOnly = GetKeyword(instance, "generic_only", false);
   auto verbose = GetKeyword(instance, "verbose", true);

   // Case 1) Use only numba
   if (numbaOnly) {
      return NumbaCallableImpl_call(NULL, args);
   }

   // Case 2) Use only generic
   else if (genericOnly) {
      return GenericCallableImpl_call(NULL, args);
   }

   // Case 3) Try first numba and then fall back to generic
   else {
      auto pyfunc = NumbaCallableImpl_call(NULL, args);
      if (pyfunc) {
         return pyfunc;
      } else {
            PyErr_WarnEx(PyExc_RuntimeWarning,
                    "Failed to compile Python callable using numba, fall back to generic implementation. Note that the generic implementation is potentially slow and does not allow multi-threading.", 1);
         }
         return GenericCallableImpl_call(NULL, args);
      }
   }
}


// Method definition for class used as decorator to create C++ wrapper
static PyMethodDef CallableImplMethods[] =
{
    {"__init__", (PyCFunction)GenericCallableImpl_init, METH_VARARGS|METH_KEYWORDS, "Parse decorator arguments"},
    {"__call__", ProxyCallableImpl_call, METH_VARARGS, "Create C++ wrapper function"},
    {NULL},
};


// Proxy to return the C++ wrapper class which can be used as decorator
PyObject *PyROOT::GetCppCallableClass(PyObject * /*self*/, PyObject * args) {
   // Parse argument to get type of callable class
   if (!PyTuple_Check(args)) {
      PyErr_SetString(PyExc_RuntimeError, "Failed to create callable class: Invalid tuple.");
      return NULL;
   }

   // Create wrapper class for decorator
   auto classDict = PyDict_New();
   auto className = CPyCppyy_PyUnicode_FromString("CppCallableImpl");
   auto classBases = PyTuple_New(0);

   // Add methods
   for (auto def = CallableImplMethods; def->ml_name != NULL; def++) {
      auto func = PyCFunction_New(def, NULL);
#if PY_VERSION_HEX < 0x03000000
      auto method = PyMethod_New(func, NULL, NULL);
#else
      auto method = PyInstanceMethod_New(func);
#endif
	  PyDict_SetItemString(classDict, def->ml_name, method);
	  Py_DECREF(func);
	  Py_DECREF(method);
   }

   auto callableClass = PyObject_CallFunctionObjArgs(
           (PyObject*)&PyType_Type, className, classBases, classDict, NULL);
   Py_DECREF(className);
   Py_DECREF(classBases);
   Py_DECREF(classDict);

   // Return implementation class
   return callableClass;
}