小编Tri*_*ley的帖子

如何将我的Python C模块放入包中?

我想在一个包下收集多个Python模块,因此它们不会从全局的python包和模块集中保留太多名称.但我用C编写的模块有问题.

这是一个非常简单的例子,直接来自官方Python文档.您可以在此处的页面底部找到它:http://docs.python.org/distutils/examples.html

from distutils.core import setup
from distutils.extension import Extension
setup(name='foobar',
      version='1.0',
      ext_modules=[Extension('foopkg.foo', ['foo.c'])],
      )
Run Code Online (Sandbox Code Playgroud)

我的foo.c文件看起来像这样

#include <Python.h>

static PyObject *
foo_bar(PyObject *self, PyObject *args);

static PyMethodDef FooMethods[] = {
    {
        "bar",
        foo_bar,
        METH_VARARGS,
        ""
    },
    {NULL, NULL, 0, NULL}
};

static PyObject *
foo_bar(PyObject *self, PyObject *args)
{
    return Py_BuildValue("s", "foobar");
}

PyMODINIT_FUNC
initfoo(void)
{
    (void)Py_InitModule("foo", FooMethods);
}

int
main(int argc, char *argv[])
{
    // Pass argv[0] to the Python interpreter
    Py_SetProgramName(argv[0]);

    // Initialize the Python …
Run Code Online (Sandbox Code Playgroud)

python distutils python-module

6
推荐指数
1
解决办法
1356
查看次数

CoffeeScript范围问题

我在这做错了什么?我正在使用fat arrows =>作为我的回调,但是当代码到达cb.onEndElement并调用@returner时,我得到一个空对象异常.那么@returner为什么不存在呢?

class Parser
    constructor: () ->
        @returner = (data) ->

    searchParser: new xml.SaxParser (cb) =>
        cb.onStartElementNS (elem, attrs, prefix, url, ns) =>
            if elem is "results" then @results = []
            else if elem is "title" then @curr = "title"
            else @curr = "none"
        cb.onCdata (cdata) =>
            if @curr is "title" then @book.title = cdata
        cb.onEndElementNS (elem, prefix, url) =>
            @results.push @book if elem is "book"
        cb.onEndDocument =>
            @returner @results

    search: (str, callback) ->
        @returner = callback
        @searchParser.parseString str …
Run Code Online (Sandbox Code Playgroud)

javascript coffeescript

5
推荐指数
1
解决办法
2676
查看次数