build-cython-ext
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseBuild Cython Extensions
构建Cython扩展
This skill provides guidance for building Cython extensions and resolving compatibility issues, with particular focus on numpy version compatibility problems.
本技能提供构建Cython扩展及解决兼容性问题的指南,重点关注numpy版本兼容性问题。
When to Use This Skill
何时使用本技能
- Building or compiling Cython extensions (files)
.pyx - Fixing numpy compatibility issues in Cython code
- Migrating Cython projects to work with numpy 2.0+
- Resolving deprecated numpy type errors (,
np.int,np.float, etc.)np.bool - Troubleshooting Cython compilation failures
- 构建或编译Cython扩展(文件)
.pyx - 修复Cython代码中的numpy兼容性问题
- 将Cython项目迁移至numpy 2.0+版本
- 解决已弃用的numpy类型错误(、
np.int、np.float等)np.bool - 排查Cython编译失败问题
Key File Types to Examine
需要检查的关键文件类型
When working with Cython projects, always examine ALL relevant file types:
| Extension | Description | Must Check |
|---|---|---|
| Cython implementation files | Critical - Often contain numpy calls |
| Cython declaration files | Yes - May contain type declarations |
| Python files | Yes - May use deprecated types |
| Build configuration | Yes - Defines compilation settings |
| Generated C/C++ files | Only if debugging compilation |
Critical Pitfall: Never limit searches to only files when fixing numpy compatibility. The files are Cython source code and frequently contain the same deprecated numpy type references.
.py.pyx处理Cython项目时,务必检查所有相关文件类型:
| 扩展名 | 描述 | 必须检查 |
|---|---|---|
| Cython实现文件 | 关键 - 通常包含numpy调用 |
| Cython声明文件 | 是 - 可能包含类型声明 |
| Python文件 | 是 - 可能使用已弃用类型 |
| 构建配置 | 是 - 定义编译设置 |
| 生成的C/C++文件 | 仅在调试编译问题时需要 |
关键陷阱:修复numpy兼容性问题时,切勿将搜索范围仅局限于文件。文件是Cython源代码,经常包含相同的已弃用numpy类型引用。
.py.pyxApproach for Numpy 2.0+ Compatibility
numpy 2.0+兼容性处理方法
Deprecated Types to Replace
需要替换的已弃用类型
| Deprecated | Replacement |
|---|---|
| |
| |
| |
| |
| |
| |
| 已弃用类型 | 替代方案 |
|---|---|
| |
| |
| |
| |
| |
| |
Search Strategy
搜索策略
-
Search without file type restrictions to capture all occurrences:
Grep for patterns like "np\.int[^0-9_]" across all files -
Explicitly search Cython files:
Search specifically in *.pyx and *.pxd files -
Check import statements infiles - they often import numpy and use deprecated types
.pyx
-
不限制文件类型进行搜索,以捕获所有出现的情况:
Grep for patterns like "np\.int[^0-9_]" across all files -
明确搜索Cython文件:
Search specifically in *.pyx and *.pxd files -
检查文件中的导入语句——它们通常会导入numpy并使用已弃用类型
.pyx
Fix and Recompile Workflow
修复与重新编译流程
- Identify all files in the project
.pyx - Search each file for deprecated numpy types
- Apply fixes to ALL files (both and
.py).pyx - Recompile the Cython extensions after making changes to files
.pyx - Run verification tests
- 识别项目中所有文件
.pyx - 在每个文件中搜索已弃用的numpy类型
- 对所有文件(和
.py)应用修复.pyx - 修改文件后重新编译Cython扩展
.pyx - 运行验证测试
Verification Strategy
验证策略
Import Testing Is Insufficient
仅导入测试并不足够
Simply testing that a compiled module imports successfully does not verify the code works correctly. A module can import but fail when its functions are called.
仅测试已编译模块能否成功导入,并不能验证代码是否正常工作。模块可能导入成功,但调用其函数时会失败。
Recommended Verification Steps
推荐的验证步骤
- Identify all Cython modules in the project
- For each module:
- Verify import succeeds
- Call at least one core function from each module
- Pass actual data to exercise numpy operations
- Run the project's test suite if available
- Create a verification script that exercises key functionality:
python
# Example verification pattern import numpy as np from module import cython_function # Test with actual numpy arrays test_data = np.array([1, 2, 3], dtype=np.int64) result = cython_function(test_data) assert result is not None
- 识别项目中所有Cython模块
- 针对每个模块:
- 验证导入是否成功
- 调用每个模块中的至少一个核心函数
- 传入实际数据以触发numpy操作
- 如果有项目测试套件,运行它
- 创建验证脚本以测试关键功能:
python
# Example verification pattern import numpy as np from module import cython_function # Test with actual numpy arrays test_data = np.array([1, 2, 3], dtype=np.int64) result = cython_function(test_data) assert result is not None
Test Coverage Awareness
测试覆盖范围注意事项
- Repository tests may not cover all Cython code paths
- Passing tests does not guarantee all Cython functionality works
- Explicitly test functions that use numpy types
- 仓库测试可能未覆盖所有Cython代码路径
- 测试通过并不保证所有Cython功能都能正常工作
- 明确测试使用numpy类型的函数
Common Pitfalls
常见陷阱
- Narrow Search Scope: Using file type filters (e.g., ) that exclude
type: "py"files.pyx - Premature Success Declaration: Assuming success after imports work or basic tests pass
- Missing Recompilation: Forgetting to recompile after fixing files
.pyx - Incomplete Pattern Matching: Missing variations like vs
numpy.intnp.int - Ignoring Warning Signs: If compilation succeeds "surprisingly" easily, verify the compiled code actually runs correctly
- 搜索范围过窄:使用文件类型过滤器(如)排除
type: "py"文件.pyx - 过早宣告成功:假设导入成功或基础测试通过就代表任务完成
- 忘记重新编译:修复文件后忘记重新编译
.pyx - 模式匹配不完整:遗漏与
numpy.int等变体np.int - 忽略警告信号:如果编译异常顺利完成,需验证已编译代码是否真的能正常运行
Systematic Workflow
系统化流程
-
Discovery Phase
- List all ,
.pyx, and.pxdfiles.py - Identify the build system (setup.py, pyproject.toml, etc.)
- Check numpy version requirements
- List all
-
Analysis Phase
- Search ALL source files for deprecated patterns
- Document every occurrence before fixing
- Note which files need recompilation
-
Fix Phase
- Apply fixes to all identified locations
- Ensure consistency in replacement types
- Update any type annotations or docstrings
-
Build Phase
- Clean previous build artifacts
- Recompile all Cython extensions
- Watch for compilation warnings
-
Verification Phase
- Test each Cython module individually
- Run the full test suite
- Execute functions with real numpy data
- Verify no runtime AttributeError for numpy types
-
发现阶段
- 列出所有、
.pyx和.pxd文件.py - 识别构建系统(setup.py、pyproject.toml等)
- 检查numpy版本要求
- 列出所有
-
分析阶段
- 在所有源文件中搜索已弃用模式
- 修复前记录所有出现的位置
- 标记需要重新编译的文件
-
修复阶段
- 对所有已识别的位置应用修复
- 确保替换类型的一致性
- 更新任何类型注解或文档字符串
-
构建阶段
- 清理之前的构建产物
- 重新编译所有Cython扩展
- 留意编译警告
-
验证阶段
- 逐个测试每个Cython模块
- 运行完整测试套件
- 使用真实numpy数据执行函数
- 验证不存在numpy类型相关的运行时AttributeError