查看: 15|回复: 12

哪位朋友帮忙用 mac 测试下我的 Python 脚本

[复制链接]

1

主题

6

回帖

15

积分

新手上路

积分
15
发表于 2026-7-10 16:51:48 | 显示全部楼层 |阅读模式
把输出贴给我

如果有异常,如果能附带帮忙 fix 更好

import signal
import subprocess
from typing import OrderedDict

APP_NAME = "Hello"

class CalledProcessError(subprocess.CalledProcessError):
    """
    `subprocess.CalledProcessError` does not print `stderr` and `output`.
    """

    def __str__(self):
        if self.returncode and self.returncode < 0:
            try:
                return "Command '%s' died with %r." % (
                    self.cmd,
                    signal.Signals(-self.returncode),
                )
            except ValueError:
                return "Command '%s' died with unknown signal %d." % (
                    self.cmd,
                    -self.returncode,
                )
        else:
            return (
                "Command '%s' returned non-zero exit status %d. stderr: '%s'. output: '%s'"
                % (self.cmd, self.returncode, self.stderr.strip(), self.output.strip())
            )

class DecryptError(Exception): ...

class DarwinCryptex:
    scheme = "security"

    def encrypt(self, attrs: OrderedDict[str, str], plaintext: str) -> str:
        account = "-".join([f"{k}:{v}" for k, v in attrs.items()])
        cmd = [
            "security",
            "add-generic-password",
            "-a",
            account,
            "-s",
            APP_NAME,
            "-w",
            plaintext,
            "-U",
        ]
        r = subprocess.run(
            cmd,
            capture_output=True,
            encoding="utf-8",
            text=True,
            timeout=5,
        )
        if r.returncode != 0:
            raise CalledProcessError(
                r.returncode, cmd, output=r.stdout, stderr=r.stderr
            )
        return "******"

    def decrypt(self, attrs: OrderedDict[str, str], ciphertext: str) -> str:
        account = "-".join([f"{k}:{v}" for k, v in attrs.items()])
        cmd = [
            "security",
            "find-generic-password",
            "-a",
            account,
            "-s",
            APP_NAME,
            "-w",
        ]
        r = subprocess.run(
            cmd,
            capture_output=True,
            encoding="utf-8",
            text=True,
            timeout=5,
        )
        if r.returncode == 0:
            return r.stdout.strip()
        elif r.returncode == 44:
            raise DecryptError()
        else:
            raise CalledProcessError(
                r.returncode, cmd, output=r.stdout, stderr=r.stderr
            )

    def clean(self, attrs: OrderedDict[str, str]):
        account = "-".join([f"{k}:{v}" for k, v in attrs.items()])
        cmd = ["security", "delete-generic-password", "-a", account, "-s", APP_NAME]
        r = subprocess.run(
            cmd, capture_output=True, encoding="utf-8", text=True, timeout=5
        )
        if r.returncode not in (0, 44):
            raise CalledProcessError(
                r.returncode, cmd, output=r.stdout, stderr=r.stderr
            )

cryptex = DarwinCryptex()
attrs: OrderedDict[str, str] = OrderedDict(
    [
        ("app", "test"),
        ("section", "hello"),
        ("key", "你好"),
    ]
)
plaintext = "Hello world! 你好,世界!"

try:
    ciphertext = cryptex.encrypt(attrs, plaintext)
    decrypted = cryptex.decrypt(attrs, ciphertext)
    assert decrypted == plaintext

    mismatch_attrs = attrs.copy()
    mismatch_attrs["needless"] = "有毒"
    try:
        cryptex.decrypt(mismatch_attrs, ciphertext)
    except DecryptError:
        pass
    else:
        raise AssertionError("Expected DecryptError")
finally:
    cryptex.clean(attrs)
回复

使用道具 举报

0

主题

4

回帖

8

积分

新手上路

积分
8
发表于 2026-7-10 17:05:51 | 显示全部楼层
assert decrypted == plaintext

不成立报错了

且 encrypt 是不是没写完全呀
回复

使用道具 举报

1

主题

6

回帖

15

积分

新手上路

积分
15
 楼主| 发表于 2026-7-10 17:14:17 | 显示全部楼层
@busln
感觉是写完了
把两个变量都打印瞧瞧?
回复

使用道具 举报

0

主题

4

回帖

8

积分

新手上路

积分
8
发表于 2026-7-10 17:26:10 | 显示全部楼层
程序有问题, 没写进去
回复

使用道具 举报

1

主题

6

回帖

15

积分

新手上路

积分
15
 楼主| 发表于 2026-7-10 17:37:01 | 显示全部楼层
@busln 我用 claude review 了下,没大问题啊
回复

使用道具 举报

0

主题

1

回帖

2

积分

新手上路

积分
2
发表于 2026-7-10 17:42:24 | 显示全部楼层
assert 失败,打印两个变量如下
decrypted: 48656c6c6f20776f726c642120e4bda0e5a5bdefbc8ce4b896e7958cefbc81
plaintext: Hello world! 你好,世界!
应该是遇到非 ascii 字符给他转成 hex 了
回复

使用道具 举报

1

主题

6

回帖

15

积分

新手上路

积分
15
 楼主| 发表于 2026-7-10 17:48:42 | 显示全部楼层
@zhhanging
"Hello world! 你好,世界!".encode('utf-8').hex()
刚好是
'48656c6c6f20776f726c642120e4bda0e5a5bdefbc8ce4b896e7958cefbc81'
回复

使用道具 举报

0

主题

4

回帖

8

积分

新手上路

积分
8
发表于 2026-7-10 17:52:05 | 显示全部楼层
finally 给删了,漏看了
@gosky
回复

使用道具 举报

0

主题

4

回帖

8

积分

新手上路

积分
8
发表于 2026-7-10 17:53:57 | 显示全部楼层
bytes.fromhex(hex).decode("utf-8")
这样就能转换回来了
回复

使用道具 举报

1

主题

6

回帖

15

积分

新手上路

积分
15
 楼主| 发表于 2026-7-10 17:56:44 | 显示全部楼层
@busln
把下面的中文字符,全换成英文字母,会怎么样?

attrs: OrderedDict[str, str] = OrderedDict(
    [
        ("app", "test"),
        ("section", "hello"),
        ("key", "你好"),
    ]
)
plaintext = "Hello world! 你好,世界!"
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Powered by Discuz! X5.0 © 2001-2026 Discuz! Team.

在本版发帖
返回顶部