Skip to content

Instantly share code, notes, and snippets.

@saghul
Created April 23, 2012 20:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saghul/2473614 to your computer and use it in GitHub Desktop.
Save saghul/2473614 to your computer and use it in GitHub Desktop.
PyStructSequence usage example
# coding=utf8
from distutils.core import setup, Extension
setup(name = "foo",
version = "0.0.1",
author = "Saúl Ibarra Corretgé",
author_email = "saghul@gmail.com",
description = "PyStructSequence example",
ext_modules = [Extension('foo',
sources = ['test_pystructsequence.c'],
)]
)
#include "Python.h"
#include "structmember.h"
#include "structseq.h"
static PyTypeObject FooResultType = {0, 0, 0, 0, 0, 0};
static PyStructSequence_Field foo_result_fields[] = {
{"field_1", "This is field 1"},
{"field_2", "This is field 2"},
{"field_3", "This is field 3"},
{NULL}
};
static PyStructSequence_Desc foo_result_desc = {
"foo_result",
NULL,
foo_result_fields,
3
};
PyMODINIT_FUNC
initfoo(void)
{
PyObject *foo = Py_InitModule("foo", NULL);
if (FooResultType.tp_name == 0)
PyStructSequence_InitType(&FooResultType, &foo_result_desc);
Py_INCREF((PyObject *) &FooResultType);
PyModule_AddObject(foo, "foo_result", (PyObject *) &FooResultType);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment