From ac36cad6d92096e7a4ff79dd2ab4a8eceecf4eb9 Mon Sep 17 00:00:00 2001 From: DigHuang <114602213+DigHuang@users.noreply.github.com> Date: Wed, 15 Apr 2026 23:04:37 +0800 Subject: [PATCH] fix: switch user_main invocation to use keyword arguments for better default parameter handling (#6756) * fix: switch user_main invocation to use keyword arguments for better default parameter handling * fix: test --------- Co-authored-by: archer <545436317@qq.com> --- .github/workflows/test-sandbox.yaml | 11 +++++++++++ projects/code-sandbox/src/pool/worker.py | 11 ++++------- .../code-sandbox/test/compat/legacy-python.test.ts | 10 ++++++++++ projects/code-sandbox/test/unit/security.test.ts | 4 ++-- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test-sandbox.yaml b/.github/workflows/test-sandbox.yaml index f62e7943de..37a4714e65 100644 --- a/.github/workflows/test-sandbox.yaml +++ b/.github/workflows/test-sandbox.yaml @@ -27,8 +27,19 @@ jobs: node-version: '20' cache: 'pnpm' + - uses: oven-sh/setup-bun@v2 + with: + bun-version: latest + + - uses: actions/setup-python@v5 + with: + python-version: '3.11' + - name: Install Dependencies run: pnpm install + - name: Install Python Dependencies + run: pip install -r projects/code-sandbox/requirements.txt + - name: Run Unit Tests run: pnpm --filter=code-sandbox test diff --git a/projects/code-sandbox/src/pool/worker.py b/projects/code-sandbox/src/pool/worker.py index 5ee6f39e8a..274fdead39 100644 --- a/projects/code-sandbox/src/pool/worker.py +++ b/projects/code-sandbox/src/pool/worker.py @@ -613,15 +613,12 @@ def main_loop(): else: result = user_main(variables) else: - call_args = [] + # 用 kwargs 调用:缺席参数走函数默认值,不影响后面的参数 for p in params: - if p in variables: - call_args.append(variables[p]) - elif sig.parameters[p].default is not _inspect_mod.Parameter.empty: - break - else: + if p not in variables and sig.parameters[p].default is _inspect_mod.Parameter.empty: raise TypeError(f"Missing required argument: '{p}'") - result = user_main(*call_args) + call_kwargs = {p: variables[p] for p in params if p in variables} + result = user_main(**call_kwargs) signal.alarm(0) write_line({ diff --git a/projects/code-sandbox/test/compat/legacy-python.test.ts b/projects/code-sandbox/test/compat/legacy-python.test.ts index ea9996d12c..cf2a370c1e 100644 --- a/projects/code-sandbox/test/compat/legacy-python.test.ts +++ b/projects/code-sandbox/test/compat/legacy-python.test.ts @@ -99,6 +99,16 @@ def main(name, age): expect(result.data?.codeReturn.msg).toBe('Hello, World!'); }); + it('main 前置命名参数缺失但有默认值,后置参数仍按 kwargs 注入', async () => { + const result = await pool.execute({ + code: `def main(a=None, b=None): + return {"a": a if a else "无", "b": b if b else "无"}`, + variables: { b: 'd2' } + }); + expect(result.success).toBe(true); + expect(result.data?.codeReturn).toEqual({ a: '无', b: 'd2' }); + }); + // ===== 返回值类型兼容 ===== it('返回列表(旧版常见)', async () => { diff --git a/projects/code-sandbox/test/unit/security.test.ts b/projects/code-sandbox/test/unit/security.test.ts index c0729c54c1..1616609024 100644 --- a/projects/code-sandbox/test/unit/security.test.ts +++ b/projects/code-sandbox/test/unit/security.test.ts @@ -25,8 +25,8 @@ beforeAll(async () => { }); afterAll(async () => { - await jsPool.shutdown(); - await pyPool.shutdown(); + await jsPool?.shutdown(); + await pyPool?.shutdown(); }); describe('模块拦截', () => {