Загрузить файлы в «venv/Lib/site-packages/greenlet»

This commit is contained in:
2026-07-02 18:28:18 +00:00
parent 4e6637daf9
commit d3bee416bb
5 changed files with 604 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
#ifndef PYGREENLET_HPP
#define PYGREENLET_HPP
#include "greenlet.h"
#include "greenlet_compiler_compat.hpp"
#include "greenlet_refs.hpp"
using greenlet::refs::OwnedGreenlet;
using greenlet::refs::BorrowedGreenlet;
using greenlet::refs::BorrowedObject;;
using greenlet::refs::OwnedObject;
using greenlet::refs::PyErrPieces;
// XXX: These doesn't really belong here, it's not a Python slot.
static OwnedObject internal_green_throw(BorrowedGreenlet self, PyErrPieces& err_pieces);
static PyGreenlet* green_new(PyTypeObject* type, PyObject* UNUSED(args), PyObject* UNUSED(kwds));
static int green_clear(PyGreenlet* self);
static int green_init(PyGreenlet* self, PyObject* args, PyObject* kwargs);
static int green_setparent(PyGreenlet* self, PyObject* nparent, void* UNUSED(context));
static int green_setrun(PyGreenlet* self, PyObject* nrun, void* UNUSED(context));
static int green_traverse(PyGreenlet* self, visitproc visit, void* arg);
static void green_dealloc(PyGreenlet* self);
static PyObject* green_getparent(PyGreenlet* self, void* UNUSED(context));
static int green_is_gc(PyObject* self);
static PyObject* green_getdead(PyGreenlet* self, void* UNUSED(context));
static PyObject* green_getrun(PyGreenlet* self, void* UNUSED(context));
static int green_setcontext(PyGreenlet* self, PyObject* nctx, void* UNUSED(context));
static PyObject* green_getframe(PyGreenlet* self, void* UNUSED(context));
static PyObject* green_repr(PyGreenlet* self);
#endif

View File

@@ -0,0 +1,150 @@
/* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
/**
Implementation of the Python slots for PyGreenletUnswitchable_Type
*/
#ifndef PY_GREENLET_UNSWITCHABLE_CPP
#define PY_GREENLET_UNSWITCHABLE_CPP
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "structmember.h" // PyMemberDef
#include "greenlet_internal.hpp"
// Code after this point can assume access to things declared in stdint.h,
// including the fixed-width types. This goes for the platform-specific switch functions
// as well.
#include "greenlet_refs.hpp"
#include "greenlet_slp_switch.hpp"
#include "greenlet_thread_support.hpp"
#include "TGreenlet.hpp"
#include "TGreenlet.cpp"
#include "TGreenletGlobals.cpp"
#include "TThreadStateDestroy.cpp"
using greenlet::LockGuard;
using greenlet::LockInitError;
using greenlet::PyErrOccurred;
using greenlet::Require;
using greenlet::g_handle_exit;
using greenlet::single_result;
using greenlet::Greenlet;
using greenlet::UserGreenlet;
using greenlet::MainGreenlet;
using greenlet::BrokenGreenlet;
using greenlet::ThreadState;
using greenlet::PythonState;
#include "PyGreenlet.hpp"
static PyGreenlet*
green_unswitchable_new(PyTypeObject* type, PyObject* UNUSED(args), PyObject* UNUSED(kwds))
{
PyGreenlet* o =
(PyGreenlet*)PyBaseObject_Type.tp_new(type, mod_globs->empty_tuple, mod_globs->empty_dict);
if (o) {
new BrokenGreenlet(o, GET_THREAD_STATE().state().borrow_current());
assert(Py_REFCNT(o) == 1);
}
return o;
}
static PyObject*
green_unswitchable_getforce(PyGreenlet* self, void* UNUSED(context))
{
BrokenGreenlet* broken = dynamic_cast<BrokenGreenlet*>(self->pimpl);
return PyBool_FromLong(broken->_force_switch_error);
}
static int
green_unswitchable_setforce(PyGreenlet* self, PyObject* nforce, void* UNUSED(context))
{
if (!nforce) {
PyErr_SetString(
PyExc_AttributeError,
"Cannot delete force_switch_error"
);
return -1;
}
BrokenGreenlet* broken = dynamic_cast<BrokenGreenlet*>(self->pimpl);
int is_true = PyObject_IsTrue(nforce);
if (is_true == -1) {
return -1;
}
broken->_force_switch_error = is_true;
return 0;
}
static PyObject*
green_unswitchable_getforceslp(PyGreenlet* self, void* UNUSED(context))
{
BrokenGreenlet* broken = dynamic_cast<BrokenGreenlet*>(self->pimpl);
return PyBool_FromLong(broken->_force_slp_switch_error);
}
static int
green_unswitchable_setforceslp(PyGreenlet* self, PyObject* nforce, void* UNUSED(context))
{
if (!nforce) {
PyErr_SetString(
PyExc_AttributeError,
"Cannot delete force_slp_switch_error"
);
return -1;
}
BrokenGreenlet* broken = dynamic_cast<BrokenGreenlet*>(self->pimpl);
int is_true = PyObject_IsTrue(nforce);
if (is_true == -1) {
return -1;
}
broken->_force_slp_switch_error = is_true;
return 0;
}
static PyGetSetDef green_unswitchable_getsets[] = {
/* name, getter, setter, doc, closure (context pointer) */
{
.name="force_switch_error",
.get=(getter)green_unswitchable_getforce,
.set=(setter)green_unswitchable_setforce,
.doc=nullptr
},
{
.name="force_slp_switch_error",
.get=(getter)green_unswitchable_getforceslp,
.set=(setter)green_unswitchable_setforceslp,
.doc=nullptr
},
{.name=nullptr}
};
PyTypeObject PyGreenletUnswitchable_Type = {
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "greenlet._greenlet.UnswitchableGreenlet",
.tp_dealloc = (destructor)green_dealloc,
.tp_flags = G_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
.tp_doc = "Undocumented internal class for testing error conditions",
.tp_traverse = (traverseproc)green_traverse,
.tp_clear = (inquiry)green_clear,
.tp_getset = green_unswitchable_getsets,
.tp_base = &PyGreenlet_Type,
.tp_init = (initproc)green_init,
.tp_alloc = PyType_GenericAlloc,
.tp_new = (newfunc)green_unswitchable_new,
.tp_free = PyObject_GC_Del,
#ifndef Py_GIL_DISABLED
// See comments in the base type
.tp_is_gc = (inquiry)green_is_gc,
#endif
};
#endif

View File

@@ -0,0 +1,297 @@
#ifndef PY_MODULE_CPP
#define PY_MODULE_CPP
#include "greenlet_internal.hpp"
#include "TGreenletGlobals.cpp"
#include "TMainGreenlet.cpp"
#include "TThreadStateDestroy.cpp"
using greenlet::LockGuard;
using greenlet::ThreadState;
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunused-function"
# pragma clang diagnostic ignored "-Wunused-variable"
#endif
PyDoc_STRVAR(mod_getcurrent_doc,
"getcurrent() -> greenlet\n"
"\n"
"Returns the current greenlet (i.e. the one which called this "
"function).\n");
static PyObject*
mod_getcurrent(PyObject* UNUSED(module))
{
if (greenlet::IsShuttingDown()) {
PyErr_SetString(PyExc_RuntimeError, "greenlet is being finalized");
return nullptr;
}
return GET_THREAD_STATE().state().get_current().relinquish_ownership_o();
}
PyDoc_STRVAR(mod_settrace_doc,
"settrace(callback) -> object\n"
"\n"
"Sets a new tracing function and returns the previous one.\n");
static PyObject*
mod_settrace(PyObject* UNUSED(module), PyObject* args)
{
PyArgParseParam tracefunc;
if (!PyArg_ParseTuple(args, "O", &tracefunc)) {
return NULL;
}
ThreadState& state = GET_THREAD_STATE();
OwnedObject previous = state.get_tracefunc();
if (!previous) {
previous = Py_None;
}
state.set_tracefunc(tracefunc);
return previous.relinquish_ownership();
}
PyDoc_STRVAR(mod_gettrace_doc,
"gettrace() -> object\n"
"\n"
"Returns the currently set tracing function, or None.\n");
static PyObject*
mod_gettrace(PyObject* UNUSED(module))
{
OwnedObject tracefunc = GET_THREAD_STATE().state().get_tracefunc();
if (!tracefunc) {
tracefunc = Py_None;
}
return tracefunc.relinquish_ownership();
}
PyDoc_STRVAR(mod_set_thread_local_doc,
"set_thread_local(key, value) -> None\n"
"\n"
"Set a value in the current thread-local dictionary. Debugging only.\n");
static PyObject*
mod_set_thread_local(PyObject* UNUSED(module), PyObject* args)
{
PyArgParseParam key;
PyArgParseParam value;
PyObject* result = NULL;
if (PyArg_UnpackTuple(args, "set_thread_local", 2, 2, &key, &value)) {
if(PyDict_SetItem(
PyThreadState_GetDict(), // borrow
key,
value) == 0 ) {
// success
Py_INCREF(Py_None);
result = Py_None;
}
}
return result;
}
PyDoc_STRVAR(mod_get_pending_cleanup_count_doc,
"get_pending_cleanup_count() -> Integer\n"
"\n"
"Get the number of greenlet cleanup operations pending. Testing only.\n");
static PyObject*
mod_get_pending_cleanup_count(PyObject* UNUSED(module))
{
LockGuard cleanup_lock(*mod_globs->thread_states_to_destroy_lock);
return PyLong_FromSize_t(mod_globs->thread_states_to_destroy.size());
}
PyDoc_STRVAR(mod_get_total_main_greenlets_doc,
"get_total_main_greenlets() -> Integer\n"
"\n"
"Quickly return the number of main greenlets that exist. Testing only.\n");
static PyObject*
mod_get_total_main_greenlets(PyObject* UNUSED(module))
{
return PyLong_FromSize_t(G_TOTAL_MAIN_GREENLETS);
}
PyDoc_STRVAR(mod_get_clocks_used_doing_optional_cleanup_doc,
"get_clocks_used_doing_optional_cleanup() -> Integer\n"
"\n"
"Get the number of clock ticks the program has used doing optional "
"greenlet cleanup.\n"
"Beginning in greenlet 2.0, greenlet tries to find and dispose of greenlets\n"
"that leaked after a thread exited. This requires invoking Python's garbage collector,\n"
"which may have a performance cost proportional to the number of live objects.\n"
"This function returns the amount of processor time\n"
"greenlet has used to do this. In programs that run with very large amounts of live\n"
"objects, this metric can be used to decide whether the cost of doing this cleanup\n"
"is worth the memory leak being corrected. If not, you can disable the cleanup\n"
"using ``enable_optional_cleanup(False)``.\n"
"The units are arbitrary and can only be compared to themselves (similarly to ``time.clock()``);\n"
"for example, to see how it scales with your heap. You can attempt to convert them into seconds\n"
"by dividing by the value of CLOCKS_PER_SEC."
"If cleanup has been disabled, returns None."
"\n"
"This is an implementation specific, provisional API. It may be changed or removed\n"
"in the future.\n"
".. versionadded:: 2.0"
);
static PyObject*
mod_get_clocks_used_doing_optional_cleanup(PyObject* UNUSED(module))
{
std::clock_t clocks = ThreadState::clocks_used_doing_gc();
if (clocks == std::clock_t(-1)) {
Py_RETURN_NONE;
}
// This might not actually work on some implementations; clock_t
// is an opaque type.
return PyLong_FromSsize_t(clocks);
}
PyDoc_STRVAR(mod_enable_optional_cleanup_doc,
"mod_enable_optional_cleanup(bool) -> None\n"
"\n"
"Enable or disable optional cleanup operations.\n"
"See ``get_clocks_used_doing_optional_cleanup()`` for details.\n"
);
static PyObject*
mod_enable_optional_cleanup(PyObject* UNUSED(module), PyObject* flag)
{
int is_true = PyObject_IsTrue(flag);
if (is_true == -1) {
return nullptr;
}
if (is_true) {
std::clock_t clocks = ThreadState::clocks_used_doing_gc();
// If we already have a value, we don't want to lose it.
if (clocks == std::clock_t(-1)) {
ThreadState::set_clocks_used_doing_gc(0);
}
}
else {
ThreadState::set_clocks_used_doing_gc(std::clock_t(-1));
}
Py_RETURN_NONE;
}
#if !GREENLET_PY313
PyDoc_STRVAR(mod_get_tstate_trash_delete_nesting_doc,
"get_tstate_trash_delete_nesting() -> Integer\n"
"\n"
"Return the 'trash can' nesting level. Testing only.\n");
static PyObject*
mod_get_tstate_trash_delete_nesting(PyObject* UNUSED(module))
{
PyThreadState* tstate = PyThreadState_GET();
#if GREENLET_PY312
return PyLong_FromLong(tstate->trash.delete_nesting);
#else
return PyLong_FromLong(tstate->trash_delete_nesting);
#endif
}
#endif
static PyMethodDef GreenMethods[] = {
{
.ml_name="getcurrent",
.ml_meth=(PyCFunction)mod_getcurrent,
.ml_flags=METH_NOARGS,
.ml_doc=mod_getcurrent_doc
},
{
.ml_name="settrace",
.ml_meth=(PyCFunction)mod_settrace,
.ml_flags=METH_VARARGS,
.ml_doc=mod_settrace_doc
},
{
.ml_name="gettrace",
.ml_meth=(PyCFunction)mod_gettrace,
.ml_flags=METH_NOARGS,
.ml_doc=mod_gettrace_doc
},
{
.ml_name="set_thread_local",
.ml_meth=(PyCFunction)mod_set_thread_local,
.ml_flags=METH_VARARGS,
.ml_doc=mod_set_thread_local_doc
},
{
.ml_name="get_pending_cleanup_count",
.ml_meth=(PyCFunction)mod_get_pending_cleanup_count,
.ml_flags=METH_NOARGS,
.ml_doc=mod_get_pending_cleanup_count_doc
},
{
.ml_name="get_total_main_greenlets",
.ml_meth=(PyCFunction)mod_get_total_main_greenlets,
.ml_flags=METH_NOARGS,
.ml_doc=mod_get_total_main_greenlets_doc
},
{
.ml_name="get_clocks_used_doing_optional_cleanup",
.ml_meth=(PyCFunction)mod_get_clocks_used_doing_optional_cleanup,
.ml_flags=METH_NOARGS,
.ml_doc=mod_get_clocks_used_doing_optional_cleanup_doc
},
{
.ml_name="enable_optional_cleanup",
.ml_meth=(PyCFunction)mod_enable_optional_cleanup,
.ml_flags=METH_O,
.ml_doc=mod_enable_optional_cleanup_doc
},
#if !GREENLET_PY313
{
.ml_name="get_tstate_trash_delete_nesting",
.ml_meth=(PyCFunction)mod_get_tstate_trash_delete_nesting,
.ml_flags=METH_NOARGS,
.ml_doc=mod_get_tstate_trash_delete_nesting_doc
},
#endif
{.ml_name=NULL, .ml_meth=NULL} /* Sentinel */
};
static const char* const copy_on_greentype[] = {
"getcurrent",
"error",
"GreenletExit",
"settrace",
"gettrace",
NULL
};
static struct PyModuleDef greenlet_module_def = {
.m_base=PyModuleDef_HEAD_INIT,
.m_name="greenlet._greenlet",
.m_doc=NULL,
.m_size=-1,
.m_methods=GreenMethods,
};
#endif
#ifdef __clang__
# pragma clang diagnostic pop
#elif defined(__GNUC__)
# pragma GCC diagnostic pop
#endif

View File

@@ -0,0 +1,45 @@
/* -*- indent-tabs-mode: nil; tab-width: 4; -*- */
/**
* Implementation of greenlet::UserGreenlet.
*
* Format with:
* clang-format -i --style=file src/greenlet/greenlet.c
*
*
* Fix missing braces with:
* clang-tidy src/greenlet/greenlet.c -fix -checks="readability-braces-around-statements"
*/
#include "TGreenlet.hpp"
namespace greenlet {
void* BrokenGreenlet::operator new(size_t UNUSED(count))
{
return allocator.allocate(1);
}
void BrokenGreenlet::operator delete(void* ptr)
{
return allocator.deallocate(static_cast<BrokenGreenlet*>(ptr),
1);
}
greenlet::PythonAllocator<greenlet::BrokenGreenlet> greenlet::BrokenGreenlet::allocator;
bool
BrokenGreenlet::force_slp_switch_error() const noexcept
{
return this->_force_slp_switch_error;
}
UserGreenlet::switchstack_result_t BrokenGreenlet::g_switchstack(void)
{
if (this->_force_switch_error) {
return switchstack_result_t(-1);
}
return UserGreenlet::g_switchstack();
}
}; //namespace greenlet

View File

@@ -0,0 +1,77 @@
/*
* Platform Selection for Stackless Python
*/
#ifdef __cplusplus
extern "C" {
#endif
#if defined(MS_WIN32) && !defined(MS_WIN64) && defined(_M_IX86) && defined(_MSC_VER)
# include "platform/switch_x86_msvc.h" /* MS Visual Studio on X86 */
#elif defined(MS_WIN64) && defined(_M_X64) && defined(_MSC_VER) || defined(__MINGW64__)
# include "platform/switch_x64_msvc.h" /* MS Visual Studio on X64 */
#elif defined(MS_WIN64) && defined(_M_ARM64)
# include "platform/switch_arm64_msvc.h" /* MS Visual Studio on ARM64 */
#elif defined(__GNUC__) && defined(__amd64__) && defined(__ILP32__)
# include "platform/switch_x32_unix.h" /* gcc on amd64 with x32 ABI */
#elif defined(__GNUC__) && defined(__amd64__)
# include "platform/switch_amd64_unix.h" /* gcc on amd64 */
#elif defined(__GNUC__) && defined(__i386__)
# include "platform/switch_x86_unix.h" /* gcc on X86 */
#elif defined(__GNUC__) && defined(__powerpc64__) && (defined(__linux__) || defined(__FreeBSD__))
# include "platform/switch_ppc64_linux.h" /* gcc on PowerPC 64-bit */
#elif defined(__GNUC__) && defined(__PPC__) && (defined(__linux__) || defined(__FreeBSD__))
# include "platform/switch_ppc_linux.h" /* gcc on PowerPC */
#elif defined(__GNUC__) && defined(__POWERPC__) && defined(__APPLE__)
# include "platform/switch_ppc_macosx.h" /* Apple MacOS X on 32-bit PowerPC */
#elif defined(__GNUC__) && defined(__powerpc64__) && defined(_AIX)
# include "platform/switch_ppc64_aix.h" /* gcc on AIX/PowerPC 64-bit */
#elif defined(__GNUC__) && defined(_ARCH_PPC) && defined(_AIX)
# include "platform/switch_ppc_aix.h" /* gcc on AIX/PowerPC */
#elif defined(__GNUC__) && defined(__powerpc__) && defined(__NetBSD__)
#include "platform/switch_ppc_unix.h" /* gcc on NetBSD/powerpc */
#elif defined(__GNUC__) && defined(sparc)
# include "platform/switch_sparc_sun_gcc.h" /* SunOS sparc with gcc */
#elif defined(__GNUC__) && defined(__sparc__)
# include "platform/switch_sparc_sun_gcc.h" /* NetBSD sparc with gcc */
#elif defined(__SUNPRO_C) && defined(sparc) && defined(sun)
# include "platform/switch_sparc_sun_gcc.h" /* SunStudio on amd64 */
#elif defined(__SUNPRO_C) && defined(__amd64__) && defined(sun)
# include "platform/switch_amd64_unix.h" /* SunStudio on amd64 */
#elif defined(__SUNPRO_C) && defined(__i386__) && defined(sun)
# include "platform/switch_x86_unix.h" /* SunStudio on x86 */
#elif defined(__GNUC__) && defined(__s390__) && defined(__linux__)
# include "platform/switch_s390_unix.h" /* Linux/S390 */
#elif defined(__GNUC__) && defined(__s390x__) && defined(__linux__)
# include "platform/switch_s390_unix.h" /* Linux/S390 zSeries (64-bit) */
#elif defined(__GNUC__) && defined(__arm__)
# ifdef __APPLE__
# include <TargetConditionals.h>
# endif
# if TARGET_OS_IPHONE
# include "platform/switch_arm32_ios.h" /* iPhone OS on arm32 */
# else
# include "platform/switch_arm32_gcc.h" /* gcc using arm32 */
# endif
#elif defined(__GNUC__) && defined(__mips__) && defined(__linux__)
# include "platform/switch_mips_unix.h" /* Linux/MIPS */
#elif defined(__GNUC__) && defined(__aarch64__)
# include "platform/switch_aarch64_gcc.h" /* Aarch64 ABI */
#elif defined(__GNUC__) && defined(__mc68000__)
# include "platform/switch_m68k_gcc.h" /* gcc on m68k */
#elif defined(__GNUC__) && defined(__csky__)
#include "platform/switch_csky_gcc.h" /* gcc on csky */
# elif defined(__GNUC__) && defined(__riscv)
# include "platform/switch_riscv_unix.h" /* gcc on RISC-V */
#elif defined(__GNUC__) && defined(__alpha__)
# include "platform/switch_alpha_unix.h" /* gcc on DEC Alpha */
#elif defined(MS_WIN32) && defined(__llvm__) && defined(__aarch64__)
# include "platform/switch_aarch64_gcc.h" /* LLVM Aarch64 ABI for Windows */
#elif defined(__GNUC__) && defined(__loongarch64) && defined(__linux__)
# include "platform/switch_loongarch64_linux.h" /* LoongArch64 */
#elif defined(__GNUC__) && defined(__sh__)
# include "platform/switch_sh_gcc.h" /* SuperH */
#endif
#ifdef __cplusplus
};
#endif