Skip to content

Commit e20108b

Browse files
committed
initial commit of working spam module
0 parents  commit e20108b

File tree

6 files changed

+121
-0
lines changed

6 files changed

+121
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dist/spam-1.0/PKG-INFO
2+
dist/spam-1.0/Modules/build/
3+
MANIFEST
4+
.idea/

dist/spam-1.0/Modules/setup.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from distutils.core import setup, Extension
2+
3+
# static_libraries = ['igraph']
4+
# static_lib_dir = '/system/lib'
5+
# libraries = ['z', 'xml2', 'gmp']
6+
# library_dirs = ['/system/lib', '/system/lib64']
7+
#
8+
# extra_objects = ['{}/lib{}.a'.format(static_lib_dir, l) for l in static_libraries]
9+
#
10+
# ext = Extension('spam',
11+
# sources=['spammodule.c'],
12+
# libraries=libraries,
13+
# library_dirs=library_dirs,
14+
# include_dirs=[],
15+
# extra_objects=extra_objects)
16+
17+
setup(name = 'spam', version = '1.0', \
18+
ext_modules = [Extension('spam', ['spammodule.c'])])

dist/spam-1.0/Modules/spammodule.c

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#define PY_SSIZE_T_CLEAN
2+
#include <Python/Python.h>
3+
4+
#define SPAM_MODULE
5+
#include "spammodule.h"
6+
7+
// Method Table
8+
static PyMethodDef SpamMethods[] = {
9+
{"system", spam_system, METH_VARARGS,
10+
"Execute a shell command."},
11+
{NULL, NULL, 0, NULL} /* Sentinel */
12+
};
13+
14+
//// Error
15+
//PyMODINIT_FUNC
16+
//initspam(void)
17+
//{
18+
// PyObject *m;
19+
//
20+
// m = Py_InitModule("spam", SpamMethods);
21+
// if (m == NULL)
22+
// return;
23+
//
24+
// SpamError = PyErr_NewException("spam.error", NULL, NULL);
25+
// Py_INCREF(SpamError);
26+
// PyModule_AddObject(m, "error", SpamError);
27+
//}
28+
29+
// Main
30+
static PyObject *
31+
spam_system(PyObject *self, PyObject *args)
32+
{
33+
const char *command;
34+
int sts;
35+
36+
if (!PyArg_ParseTuple(args, "s", &command))
37+
return NULL;
38+
sts = system(command);
39+
// if (sts < 0) {
40+
// PyErr_SetString(SpamError, "System command failed");
41+
// return NULL;
42+
// }
43+
return Py_BuildValue("i", sts);
44+
}
45+
46+
// Module definition
47+
//static struct PyModuleDef spammodule = {
48+
// PyModuleDef_HEAD_INIT,
49+
// "spam", /* name of module */
50+
// spam_doc, /* module documentation, may be NULL */
51+
// -1, /* size of per-interpreter state of the module,
52+
// or -1 if the module keeps state in global variables. */
53+
// SpamMethods
54+
//};
55+
56+
// Initialization function
57+
//PyMODINIT_FUNC
58+
//PyInit_spam(void)
59+
//{
60+
// return PyModule_Create(&spammodule);
61+
//}
62+
63+
// Module definition and initializer
64+
PyMODINIT_FUNC
65+
initspam(void)
66+
{
67+
(void) Py_InitModule("spam", SpamMethods);
68+
}
69+
70+
// Initialization Table
71+
int
72+
main(int argc, char *argv[])
73+
{
74+
/* Pass argv[0] to the Python interpreter */
75+
Py_SetProgramName(argv[0]);
76+
77+
/* Initialize the Python interpreter. Required. */
78+
Py_Initialize();
79+
80+
/* Add a static module */
81+
initspam();
82+
83+
}

dist/spam-1.0/Modules/spammodule.h

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#ifndef SPAM_H_ /* Include guard */
2+
#define SPAM_H_
3+
4+
5+
static PyObject * system(PyObject *self, PyObject *args);
6+
7+
#endif // SPAM_H_

my_malloc.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print('hello world')

test.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# import imp
2+
#
3+
# foo = imp.load_source('spam', '/Library/Python/2.7/site-packages/')
4+
# foo.my
5+
6+
import spam
7+
8+
status = spam.system("ls -l")

0 commit comments

Comments
 (0)