首页 > 系统相关 >Windows下使用Visual Code编写并编译基于C的Python插件

Windows下使用Visual Code编写并编译基于C的Python插件

时间:2022-10-05 18:24:25浏览次数:98  
标签:10 插件 Code amd64 Windows win Python python fputs

环境

本地Windows 10,Visual Code,Pyhton3.10

Python的安装路径d:/develop/python/Python310

1、C代码

fputsmodule.c

#include <Python.h>
//https://realpython.com/build-python-c-extension-module/#considering-alternatives


static PyObject *StringTooShortError = NULL;

static PyObject *method_fputs(PyObject *self, PyObject *args) {
    char *str, *filename = NULL;
    int bytes_copied = -1;

    /* Parse arguments */
    if(!PyArg_ParseTuple(args, "ss", &str, &filename)) {
        return NULL;
    }

     if (strlen(str) < 10) {
        /* Passing custom exception */
        PyErr_SetString(StringTooShortError, "String length must be greater than 10");
        return NULL;
    }

    FILE *fp = fopen(filename, "w");
    bytes_copied = fputs(str, fp);
    fclose(fp);

    return PyLong_FromLong(bytes_copied);
}

static PyMethodDef FputsMethods[] = {
    {"fputs", method_fputs, METH_VARARGS, "Python interface for fputs C library function"},
    {NULL, NULL, 0, NULL}
};

static struct PyModuleDef fputsmodule = {
    PyModuleDef_HEAD_INIT,
    "fputs",
    "Python interface for the fputs C library function",
    -1,
    FputsMethods
};

PyMODINIT_FUNC PyInit_fputs(void) {
    /* Assign module value */
    PyObject *module = PyModule_Create(&fputsmodule);

    /* Add int constant by name */
    PyModule_AddIntConstant(module, "FPUTS_FLAG", 64);

     /* Define int macro */
    #define FPUTS_MACRO 256

    /* Add macro to module */
    PyModule_AddIntMacro(module, FPUTS_MACRO);


    /* Initialize new exception object */
    StringTooShortError = PyErr_NewException("fputs.StringTooShortError", NULL, NULL);

    /* Add exception object to your module */
    PyModule_AddObject(module, "StringTooShortError", StringTooShortError);

    return module;
}

 

2、编译用脚本

setup.py

from distutils.core import setup, Extension

def main():
    setup(name="fputs",
          version="1.0.0",
          include_dirs="d:/develop/python/Python310/include",
          description="Python interface for the fputs C library function",
          author="<your name>",
          author_email="[email protected]",
          ext_modules=[Extension("fputs", ["fputsmodule.c"])])

if __name__ == "__main__":
    main()

3、编译及安装

PS E:\project\python\extends1> python.exe .\setup.py build
E:\project\python\extends1\setup.py:1: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives
  from distutils.core import setup, Extension
running build
running build_ext
building 'fputs' extension
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\bin\HostX86\x64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Id:/develop/python/Python310/include -ID:\develop\python\Python310\include -ID:\develop\python\Python310\Include -IC:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\include -IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\ucrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\shared -IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\um -IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\winrt -IC:\Program Files (x86)\Windows Kits\10\include\10.0.19041.0\cppwinrt /Tcfputsmodule.c /Fobuild\temp.win-amd64-3.10\Release\fputsmodule.obj
fputsmodule.c
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\bin\HostX86\x64\link.exe /nologo /INCREMENTAL:NO /LTCG /DLL /MANIFEST:EMBED,ID=2 /MANIFESTUAC:NO /LIBPATH:D:\develop\python\Python310\libs /LIBPATH:D:\develop\python\Python310\PCbuild\amd64 /LIBPATH:C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.29.30133\lib\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\ucrt\x64 /LIBPATH:C:\Program Files (x86)\Windows Kits\10\lib\10.0.19041.0\um\x64 /EXPORT:PyInit_fputs build\temp.win-amd64-3.10\Release\fputsmodule.obj /OUT:build\lib.win-amd64-3.10\fputs.cp310-win_amd64.pyd /IMPLIB:build\temp.win-amd64-3.10\Release\fputs.cp310-win_amd64.lib
  正在创建库 build\temp.win-amd64-3.10\Release\fputs.cp310-win_amd64.lib 和对象 build\temp.win-amd64-3.10\Release\fputs.cp310-win_amd64.exp
正在生成代码
已完成代码的生成
PS E:\project\python\extends1> python.exe .\setup.py install
E:\project\python\extends1\setup.py:1: DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives
  from distutils.core import setup, Extension
running install
running build
running build_ext
running install_lib
copying build\lib.win-amd64-3.10\fputs.cp310-win_amd64.pyd -> D:\develop\python\Python310\Lib\site-packages
running install_egg_info
Removing D:\develop\python\Python310\Lib\site-packages\fputs-1.0.0-py3.10.egg-info
Writing D:\develop\python\Python310\Lib\site-packages\fputs-1.0.0-py3.10.egg-info
PS E:\project\python\extends1> D:\develop\python\Python310\python.exe .\py_fputs.py
Real Python!
PS E:\project\python\extends1>

 

4、运行

测试脚本1:
import fputs
print(fputs.__doc__)

print(fputs.__name__)

# Write to an empty file named `write.txt`
fputs.fputs("Real Python!", "write.txt")

with open("write.txt", "r") as f:
    print(f.read())

执行效果:

PS E:\project\python\extends1> D:\develop\python\Python310\python.exe .\py_fputs.py
Python interface for the fputs C library function
fputs
Real Python!
PS E:\project\python\extends1>

测试脚本2:保存字符长度小于10个字符

import fputs
print(fputs.__doc__)

print(fputs.__name__)

# Write to an empty file named `write.txt`
fputs.fputs("Real", "write.txt")

with open("write.txt", "r") as f:
    print(f.read())

执行效果

Traceback (most recent call last):
  File "E:\project\python\extends1\py_fputs.py", line 7, in <module>
    fputs.fputs("Real", "write.txt")
fputs.StringTooShortError: String length must be greater than 10

 

 

参考来源:Building a Python C Extension Module – Real Python

标签:10,插件,Code,amd64,Windows,win,Python,python,fputs
From: https://www.cnblogs.com/passedbylove/p/16756063.html

相关文章

  • Linux平台编译带PCL和PDAL插件的CloudCompare
    最近的综合课程设计需要用到CloudCompare这款软件处理点云数据,最开始我发现Debian的apt软件库就包含它,安装后却发现打不开.pcd格式的数据,于是需要从源码编译附带PCL插件的C......
  • Windows常用快捷键
    一、键盘的含义esc键:退出F1:帮助F2:重命名F3:搜索F4:地址F5:刷新F6:切换F7:调用历史命令F8:启动菜单F9:Excel计算公式F10:激活菜单栏F11:切换全屏F12:另存为,浏览网页时打......
  • MySQL数据库的各种安装方式【Windows,Linux,Docker】一次都告诉你
      MySQL数据库是作为程序员来说必备的一个组件,而安装相对来说又是非常繁琐的,所以本文就给大家整理下MySQL的各种安装操作。官网下载地址:​​https://dev.mysql.com/downlo......
  • # 如何在Windows下运行Linux程序
    如何在Windows下运行Linux程序一、搭建Linux环境1.1安装VMwareWorkstationhttps://www.aliyundrive.com/s/TvuMyFdTseh1.2下载CentOS映像文件阿里云站点:ht......
  • Windows下查看Lan口所有配置信息-查看WiFi密码
    管理员打开CMD,输入命令:netshwlanshowprofile接着输入命令:netshwlanexportprofilefolder=C:\key=clear,其中C:\可以为任意地址。然后在对应目录即可找到所有文件......
  • LeetCode 12 - 贪心
    455.分发饼干对每个孩子i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸;并且每块饼干j,都有一个尺寸s[j] 。如果s[j] >=g[i],我们可以将这个饼干j......
  • LeetCode 09 - 滑动窗口
    3.无重复字符的最长子串方法:滑动窗口使用左右两指针表示一个窗口,窗口中没有重复字符。每次向右移动right指针以扩大窗口范围,在该过程中:如果最新添加的字符在窗口中......
  • LeetCode 10 - 双指针
    11.盛最多水的容器方法:双指针用两个指针指向「容器」的左右边界,每次移动数字较小那一侧的指针,直到两指针相遇。这样移动指针的正确性是可以保证的。publicintmaxAre......
  • AtCoder Regular Contest 149
    ARC149A-RepdigitNumber符合条件的数一共只有\(9N\)个,随便怎么做都行。ACCodeARC149B-TwoLISSum这个操作相当于我们可以将\(A\)任意排列,然后对\(B\)进行......
  • LeetCode 07 - 二分查找
    注意几个点:区间的确定:一般写成闭区间[left=0,right=n-1]。循环条件的确定:因为使用闭区间,所以left==right在区间中是有意义的,所以循环条件为while(left<=right)......