python 源码编译为pyd文件和反汇编

.pyd 文件是 Python 动态链接库文件,通常用于在 Windows 操作系统上。它们是用 C 或 C++ 编写的扩展模块,编译后可以被 Python 解释器直接加载和使用。.pyd 文件的功能类似于 Linux 上的 .so 文件。

这些文件通常用于提高性能或访问 Python 本身无法直接访问的系统级功能。要在 Python 中使用 .pyd 文件,只需像导入普通 Python 模块一样导入它们。

python 源码可以通过Cython 编译为 pyd文件,提高性能的同时,隐藏一些核心信息, 不能反编译, 只能通过反汇编来查看, 一定程度上, 增加了查看代码逻辑的难度。

1 python 源码生成 pyd

创建文件setup.py:

from distutils.core import setup
from Cython.Build import cythonize

setup(
    name='pydtest',
    ext_modules=cythonize("pydtest.py")
)


创建测试例子 pydtest.py:


def say_hello(name: str):
    print(f'Hello, {name}')

在terminal 窗口中执行:

PS C:\python_project\pydtest> python .\setup.py build_ext --inplace
Compiling pydtest.py because it changed.
[1/1] Cythonizing pydtest.py
C:\python\lib\site-packages\Cython\Compiler\Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: C:\python_project\pydtest\pydtest.py
  tree = Parsing.p_module(s, pxd, full_module_name)
running build_ext
building 'pydtest' extension
creating build
creating build\temp.win-amd64-cpython-310
creating build\temp.win-amd64-cpython-310\Release
"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.41.34120\bin\HostX86\x64\cl.exe" /c /nologo /O2 /W3 /GL /DNDEBUG /MD -IC:\python\include -IC:\pytho
n\Include "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.41.34120\include" "-IC:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\
Auxiliary\VS\include" "-IC:\Program Files (x86)\Windows Kits\10\include\10.0.22621.0\ucrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\um" "-IC:\Program Files 
(x86)\Windows Kits\10\\include\10.0.22621.0\\shared" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\winrt" "-IC:\Program Files (x86)\Windows Kits\10\\include\10.0.22621.0\\cppwinrt" /Tcpydtest.c /Fobuild\temp.win-amd64-cpython-310\Release\pydtest.obj
pydtest.c
creating C:\python_project\pydtest\build\lib.win-amd64-cpython-310
"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.41.34120\bin\HostX86\x64\link.exe" /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFES
TUAC:NO /LIBPATH:C:\python\libs /LIBPATH:C:\python /LIBPATH:C:\python\PCbuild\amd64 "/LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\VC\Tools\MSVC\14.41.341
20\lib\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.22621.0\ucrt\x64" "/LIBPATH:C:\Program Files (x86)\Windows Kits\10\\lib\10.0.22621.0\\um\x64" /EXPORT:PyInit_p
ydtest build\temp.win-amd64-cpython-310\Release\pydtest.obj /OUT:build\lib.win-amd64-cpython-310\pydtest.cp310-win_amd64.pyd /IMPLIB:build\temp.win-amd64-cpython-310\Release\pydtest.cp310-win_amd64.lib
  正在创建库 build\temp.win-amd64-cpython-310\Release\pydtest.cp310-win_amd64.lib 和对象 build\temp.win-amd64-cpython-310\Release\pydtest.cp310-win_amd64.exp
正在生成代码
已完成代码的生成
copying build\lib.win-amd64-cpython-310\pydtest.cp310-win_amd64.pyd ->

另起一个文件, 导入pydtest 测试一下:

import pydtest
pydtest.say_hello('world')

# output:  Hello, world

2 注意事项

在编译 pyd 的过程中遇到 下面的错误

C:\Python\python310\lib\site-packages\Cython\Compiler\Main.py:381: FutureWarning: Cython directive 'language_level' not set, using '3str' for now (Py3). This has changed from earlier releases! File: D:\worker_project\air\pydtest\pydtest.py
  tree = Parsing.p_module(s, pxd, full_module_name)
running build_ext
building 'pydtest' extension
error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/

去 https://visualstudio.microsoft.com/visual-cpp-build-tools/ 下载 build tools 并安装即可。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值