Python';s';设置工具&x27;不';不包括自动生成的SWIG Python文件 背景

Python';s';设置工具&x27;不';不包括自动生成的SWIG Python文件 背景,python,c,swig,setuptools,python-c-api,Python,C,Swig,Setuptools,Python C Api,由于Python应用程序中的性能原因,我最终将尝试用该语言编写两个函数,以将整数转换为VLQ二进制字符串表示形式或从VLQ二进制字符串表示形式转换为整数。由于我不想处理实现细节,而且这个应用程序的目标是标准解释器,所以我现在决定使用包装生成器,并尝试使用Python包“setuptools”,以便自动编译和绑定一个示例函数(只需将一个数增加1) 问题 我已经尝试了很长时间来正确地编写我的'setup.py'文件,这样它既可以A)编译相关的C源代码,也可以B)包含自动生成的Python包装器组件。

由于Python应用程序中的性能原因,我最终将尝试用该语言编写两个函数,以将整数转换为VLQ二进制字符串表示形式或从VLQ二进制字符串表示形式转换为整数。由于我不想处理实现细节,而且这个应用程序的目标是标准解释器,所以我现在决定使用包装生成器,并尝试使用Python包“setuptools”,以便自动编译和绑定一个示例函数(只需将一个数增加1)

问题 我已经尝试了很长时间来正确地编写我的'setup.py'文件,这样它既可以A)编译相关的C源代码,也可以B)包含自动生成的Python包装器组件。我能够完成“A”,但不能完成“B”(我一直在检查生成的车轮文件以检查这一点)

尝试的解决方案 我曾尝试阅读“setuptools”文档并检查其源代码(包括包“distutils”的源代码),以及读取其他用户对堆栈溢出的输入,并尝试检查GitHub中的各种示例包。如果我现在能解决这个问题,我会的

可接受的解决方案 这对我来说基本上只是一个巨大的头痛。因此,从最具体的解决方案到最不具体的解决方案,可以解决我的问题:

  • 要么修复'setup.py'文件,要么修复项目配置/布局的其他方面,使其包含相关的Python源代码(我更希望能够在没有'MANIFEST.in'或二进制数据文件包含hack的情况下完成此操作)
  • 找到一个仍然可以使用CPython和“pip”的替代Python打包工具
  • 编译C代码示例,并以其他方式创建相关的包,该包仍然与CPython和“pip”兼容(例如,使用GNU“make”,我对此非常熟悉)
附加的 我不相信我现在在源代码发行版中包含了头文件'example.h',因此我希望您能帮助我修复它,以及修复您可能发现的任何其他错误

配置和代码 相关文件 //README.md 文件./setup.py 文件./swig_example/example.c 文件。/swig_示例/example.h 文件./swig_example/example.swig 文件./swig_示例/init.py (空文件)

文件。/swig\u示例/example\u wrap.c(自动生成) (为简洁起见省略)

文件./swig_example/example.py(自动生成) (为简洁起见省略)

# Greetings programs!!!

Don't panic!

I am primarily using this example package to test my ability to create compiled C code extensions for the Python interpreter via the SWIG mechanism. I will also be using this package to test that I can, ultimately, access the contents of a package of this sort from within 'python' itself on my computer.

# License

Anything contained within this package is licensed under [WTFPL][4] where applicable (though, obviously, you may not use my username and email without my permission). If there are any files contained within this project that I, Mr. Minty Fresh, do not have copyright control over, then said files' copyright statuses belong to each respective owner(s) (this may or may not include some files automatically generated by SWIG).
#!/usr/bin/env python3

import os
import setuptools
from setuptools import Extension, Command, find_packages
from setuptools.command.build_ext import build_ext
from distutils.command.clean import clean


class build_swig(build_ext):
    """Use SWIG on some files within the 'swig_example/' directory."""
    def run(self):
        print("building SWIG interface")
        os.system("swig -python ./swig_example/example.swig")
        super().run()

class clean_swig(clean):
    """Cleanup all files generated by SWIG in the 'swig_example/' directory."""
    def run(self):
        print("removing temporary SWIG files")
        os.system("rm -f ./swig_example/example_wrap.c")
        os.system("rm -f ./swig_example/example.py")
        print("removing dist-ish files")
        os.system("rm -rf ./dist/")
        os.system("rm -rf ./*.egg-info/")
        super().run()

class noop(Command):
    """Do absolutely nothing (successfully)!"""
    user_options = []
    def initialize_options(self): pass
    def finalize_options(self): pass
    def run(self): pass
noop.description = noop.__doc__


with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name = "example-pkg-mrmintyfresh",
    version = "0.0.1",
    author = "mrmintyfresh",
    author_email = "mrmintyfresh@example.com",
    description = "Sample SWIG usage package.",
    long_description = long_description,
    url = "https://www.example.com/",
    ext_modules = [
        Extension("_example", ["./swig_example/example_wrap.c", "./swig_example/example.c"]),
    ],
    cmdclass = {
        "build_ext": build_swig,
        "clean": clean_swig,
        "noop": noop,
    },
    packages = find_packages("./swig_example/"),
    python_requires = ">=3.0",
)
#include "example.h"

int increment(int x){
    return ++x;
}
#ifndef EXAMPLE_H
#define EXAMPLE_H

    extern int increment(int x);

#endif /* EXAMPLE_H */
%module example

%{
#include "example.h"
%}

%include "example.h"