Git Product home page Git Product logo

Comments (1)

jngrad avatar jngrad commented on June 16, 2024

For the curious, here is a quick dive into Cython code generation. All macros were expanded to help with readability. Most functions are documented in Python C API: Initialization, Finalization, and Threads.

Here is the code to call a cdef Cython function:

/* "espressomd/script_interface.pyx":192
 *         value = ptr.call_method(name, params)
 *         res = variant_to_python_object(value)             # <<<<<<<<<<<<<<
 *         if handle_errors_message is None:
 */
  PyObject *__pyx_v_res = NULL;
  try {
    __pyx_v_res = __pyx_f_10espressomd_16script_interface_variant_to_python_object(__pyx_v_value);
    if (unlikely(!__pyx_v_res)) { /* check for nullptr */
      __PYX_MARK_ERR_POS(0, 192) /* generate traceback with filename and line information */
      goto __pyx_L1_error; /* jump to error exit routine */
    }
  } catch(...) {
    __Pyx_CppExn2PyErr(); /* convert C++ exception to Python exception */
    __PYX_MARK_ERR_POS(0, 192) /* generate traceback with filename and line information */
    goto __pyx_L1_error; /* jump to error exit routine */
  }
  __Pyx_GOTREF(__pyx_v_res);

Here is the code to call a C++ function:

/* "espressomd/script_interface.pyx":191
 *         value = ptr.call_method(name, params)             # <<<<<<<<<<<<<<
 *         res = variant_to_python_object(value)
 *         if handle_errors_message is None:
 */
  ScriptInterface::Variant __pyx_v_value;
  try {
    __pyx_v_value = __pyx_v_ptr->call_method(__pyx_v_name, __pyx_v_params);
  } catch(...) {
    __Pyx_CppExn2PyErr(); /* convert C++ exception to Python exception */
    __PYX_MARK_ERR_POS(0, 191) /* generate traceback with filename and line information */
    goto __pyx_L1_error; /* jump to error exit routine */
  }

Inside a nogil context, a call to a C++ function becomes more sophisticated. For the GIL must be released, then the C++ function is called, and finally the GIL is re-acquired. If the C++ function throws, the GIL is re-acquired to safely handle it.

/* "espressomd/script_interface.pyx":
 *         with nogil:                                       # <<<<<<<<<<<<<<
 *             value = ptr.call_method_nogil(name, params)
 *         res = variant_to_python_object(value)
 *         if handle_errors_message is None:
 */
  #ifdef WITH_THREAD
  PyThreadState *_save = PyEval_SaveThread(); /* release the GIL */
  #endif

/* "espressomd/script_interface.pyx":191
 *         with nogil:
 *             value = ptr.call_method_nogil(name, params)   # <<<<<<<<<<<<<<
 *         res = variant_to_python_object(value)
 *         if handle_errors_message is None:
 */
  ScriptInterface::Variant __pyx_v_value;
  try {
    __pyx_v_value = __pyx_v_handle->call_method(__pyx_v_method_name_char, __pyx_v_parameters);
  } catch(...) {
    /* gracefully handle C++ exceptions */
    #ifdef WITH_THREAD
    PyGILState_STATE __pyx_gilstate_save = PyGILState_Ensure(); /* re-acquire the GIL */
    #endif
    __Pyx_CppExn2PyErr(); /* convert C++ exception to Python exception */
    #ifdef WITH_THREAD
    PyGILState_Release(__pyx_gilstate_save); /* release the GIL */
    #endif
    __PYX_MARK_ERR_POS(0, 191) /* generate traceback with filename and line information */
    goto __pyx_L6_error; /* jump to context manager error exit */
  }

/* "espressomd/script_interface.pyx":
 *         with nogil:                                       # <<<<<<<<<<<<<<
 *             value = ptr.call_method_nogil(name, params)
 *         res = variant_to_python_object(value)
 *         if handle_errors_message is None:
 */
  /* normal exit */{
    #ifdef WITH_THREAD
    PyEval_RestoreThread(_save); /* re-acquire the GIL */
    #endif
    goto __pyx_L7; /* jump to context manager normal exit */
  }
  __pyx_L6_error: { /* context manager error exit */
    #ifdef WITH_THREAD
    PyEval_RestoreThread(_save); /* re-acquire the GIL */
    #endif
    goto __pyx_L1_error; /* jump to error exit routine */
  }
  __pyx_L7:; /* context manager normal exit */

from espresso.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.