ai_v/venv/Lib/site-packages/greenlet/tests/test_greenlet_trash.py
24024 af7c11d7f9 feat(api): 实现图像生成及后台同步功能
- 新增图像生成接口,支持试用、积分和自定义API Key模式
- 实现生成图片结果异步上传至MinIO存储,带重试机制
- 优化积分预扣除和异常退还逻辑,保障用户积分准确
- 添加获取生成历史记录接口,支持时间范围和分页
- 提供本地字典配置接口,支持模型、比例、提示模板和尺寸
- 实现图片批量上传接口,支持S3兼容对象存储

feat(admin): 增加管理员角色管理与权限分配接口

- 实现角色列表查询、角色创建、更新及删除功能
- 增加权限列表查询接口
- 实现用户角色分配接口,便于统一管理用户权限
- 增加系统字典增删查改接口,支持分类过滤和排序
- 权限控制全面覆盖管理接口,保证安全访问

feat(auth): 完善用户登录注册及权限相关接口与页面

- 实现手机号验证码发送及校验功能,保障注册安全
- 支持手机号注册、登录及退出接口,集成日志记录
- 增加修改密码功能,验证原密码后更新
- 提供动态导航菜单接口,基于权限展示不同菜单
- 实现管理界面路由及日志、角色、字典管理页面访问权限控制
- 添加系统日志查询接口,支持关键词和等级筛选

feat(app): 初始化Flask应用并配置蓝图与数据库

- 创建应用程序工厂,加载配置,初始化数据库和Redis客户端
- 注册认证、API及管理员蓝图,整合路由
- 根路由渲染主页模板
- 应用上下文中自动创建数据库表,保证运行环境准备完毕

feat(database): 提供数据库创建与迁移支持脚本

- 新增数据库创建脚本,支持自动检测是否已存在
- 添加数据库表初始化脚本,支持创建和删除所有表
- 实现RBAC权限初始化,包含基础权限和角色创建
- 新增字段手动修复脚本,添加用户API Key和积分字段
- 强制迁移脚本支持清理连接和修复表结构,初始化默认数据及角色分配

feat(config): 新增系统配置参数

- 配置数据库、Redis、Session和MinIO相关参数
- 添加AI接口地址及试用Key配置
- 集成阿里云短信服务配置及开发模式相关参数

feat(extensions): 初始化数据库、Redis和MinIO客户端

- 创建全局SQLAlchemy数据库实例和Redis客户端
- 配置基于boto3的MinIO兼容S3客户端

chore(logs): 添加示例系统日志文件

- 记录用户请求、验证码发送成功与失败的日志信息
2026-01-12 00:53:31 +08:00

188 lines
7.8 KiB
Python

# -*- coding: utf-8 -*-
"""
Tests for greenlets interacting with the CPython trash can API.
The CPython trash can API is not designed to be re-entered from a
single thread. But this can happen using greenlets, if something
during the object deallocation process switches greenlets, and this second
greenlet then causes the trash can to get entered again. Here, we do this
very explicitly, but in other cases (like gevent) it could be arbitrarily more
complicated: for example, a weakref callback might try to acquire a lock that's
already held by another greenlet; that would allow a greenlet switch to occur.
See https://github.com/gevent/gevent/issues/1909
This test is fragile and relies on details of the CPython
implementation (like most of the rest of this package):
- We enter the trashcan and deferred deallocation after
``_PyTrash_UNWIND_LEVEL`` calls. This constant, defined in
CPython's object.c, is generally 50. That's basically how many objects are required to
get us into the deferred deallocation situation.
- The test fails by hitting an ``assert()`` in object.c; if the
build didn't enable assert, then we don't catch this.
- If the test fails in that way, the interpreter crashes.
"""
from __future__ import print_function, absolute_import, division
import unittest
class TestTrashCanReEnter(unittest.TestCase):
def test_it(self):
try:
# pylint:disable-next=no-name-in-module
from greenlet._greenlet import get_tstate_trash_delete_nesting # pylint:disable=unused-import
except ImportError:
import sys
# Python 3.13 has not "trash delete nesting" anymore (but "delete later")
assert sys.version_info[:2] >= (3, 13)
self.skipTest("get_tstate_trash_delete_nesting is not available.")
# Try several times to trigger it, because it isn't 100%
# reliable.
for _ in range(10):
self.check_it()
def check_it(self): # pylint:disable=too-many-statements
import greenlet
from greenlet._greenlet import get_tstate_trash_delete_nesting # pylint:disable=no-name-in-module
main = greenlet.getcurrent()
assert get_tstate_trash_delete_nesting() == 0
# We expect to be in deferred deallocation after this many
# deallocations have occurred. TODO: I wish we had a better way to do
# this --- that was before get_tstate_trash_delete_nesting; perhaps
# we can use that API to do better?
TRASH_UNWIND_LEVEL = 50
# How many objects to put in a container; it's the container that
# queues objects for deferred deallocation.
OBJECTS_PER_CONTAINER = 500
class Dealloc: # define the class here because we alter class variables each time we run.
"""
An object with a ``__del__`` method. When it starts getting deallocated
from a deferred trash can run, it switches greenlets, allocates more objects
which then also go in the trash can. If we don't save state appropriately,
nesting gets out of order and we can crash the interpreter.
"""
#: Has our deallocation actually run and switched greenlets?
#: When it does, this will be set to the current greenlet. This should
#: be happening in the main greenlet, so we check that down below.
SPAWNED = False
#: Has the background greenlet run?
BG_RAN = False
BG_GLET = None
#: How many of these things have ever been allocated.
CREATED = 0
#: How many of these things have ever been deallocated.
DESTROYED = 0
#: How many were destroyed not in the main greenlet. There should always
#: be some.
#: If the test is broken or things change in the trashcan implementation,
#: this may not be correct.
DESTROYED_BG = 0
def __init__(self, sequence_number):
"""
:param sequence_number: The ordinal of this object during
one particular creation run. This is used to detect (guess, really)
when we have entered the trash can's deferred deallocation.
"""
self.i = sequence_number
Dealloc.CREATED += 1
def __del__(self):
if self.i == TRASH_UNWIND_LEVEL and not self.SPAWNED:
Dealloc.SPAWNED = greenlet.getcurrent()
other = Dealloc.BG_GLET = greenlet.greenlet(background_greenlet)
x = other.switch()
assert x == 42
# It's important that we don't switch back to the greenlet,
# we leave it hanging there in an incomplete state. But we don't let it
# get collected, either. If we complete it now, while we're still
# in the scope of the initial trash can, things work out and we
# don't see the problem. We need this greenlet to complete
# at some point in the future, after we've exited this trash can invocation.
del other
elif self.i == 40 and greenlet.getcurrent() is not main:
Dealloc.BG_RAN = True
try:
main.switch(42)
except greenlet.GreenletExit as ex:
# We expect this; all references to us go away
# while we're still running, and we need to finish deleting
# ourself.
Dealloc.BG_RAN = type(ex)
del ex
# Record the fact that we're dead last of all. This ensures that
# we actually get returned too.
Dealloc.DESTROYED += 1
if greenlet.getcurrent() is not main:
Dealloc.DESTROYED_BG += 1
def background_greenlet():
# We direct through a second function, instead of
# directly calling ``make_some()``, so that we have complete
# control over when these objects are destroyed: we need them
# to be destroyed in the context of the background greenlet
t = make_some()
del t # Triggere deletion.
def make_some():
t = ()
i = OBJECTS_PER_CONTAINER
while i:
# Nest the tuples; it's the recursion that gets us
# into trash.
t = (Dealloc(i), t)
i -= 1
return t
some = make_some()
self.assertEqual(Dealloc.CREATED, OBJECTS_PER_CONTAINER)
self.assertEqual(Dealloc.DESTROYED, 0)
# If we're going to crash, it should be on the following line.
# We only crash if ``assert()`` is enabled, of course.
del some
# For non-debug builds of CPython, we won't crash. The best we can do is check
# the nesting level explicitly.
self.assertEqual(0, get_tstate_trash_delete_nesting())
# Discard this, raising GreenletExit into where it is waiting.
Dealloc.BG_GLET = None
# The same nesting level maintains.
self.assertEqual(0, get_tstate_trash_delete_nesting())
# We definitely cleaned some up in the background
self.assertGreater(Dealloc.DESTROYED_BG, 0)
# Make sure all the cleanups happened.
self.assertIs(Dealloc.SPAWNED, main)
self.assertTrue(Dealloc.BG_RAN)
self.assertEqual(Dealloc.BG_RAN, greenlet.GreenletExit)
self.assertEqual(Dealloc.CREATED, Dealloc.DESTROYED )
self.assertEqual(Dealloc.CREATED, OBJECTS_PER_CONTAINER * 2)
import gc
gc.collect()
if __name__ == '__main__':
unittest.main()