Skip to content

Commit

Permalink
cleaned up importer
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmadia committed Jul 9, 2012
1 parent 9bb4c36 commit 1a1a96a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 93 deletions.
52 changes: 29 additions & 23 deletions importer.c
Expand Up @@ -22,6 +22,10 @@

#undef HAVE_DYNAMIC_LOADING

FILE * mpifopen(const char *libfilename, const char * flags);
int mpifclose(FILE *stream);
int mpistat(const char *path, struct stat *stat_buf);

#ifdef __cplusplus
extern "C" {
#endif
Expand Down Expand Up @@ -303,7 +307,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
#endif

static PyObject *
mpiimporter_lock_held(PyObject *self, PyObject *noargs)
mpiimporter_lock_held(PyObject *self __attribute__((unused)), PyObject *noargs __attribute__((unused)))
{
#ifdef WITH_THREAD
return PyBool_FromLong(import_lock_thread != -1);
Expand All @@ -313,7 +317,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_acquire_lock(PyObject *self, PyObject *noargs)
mpiimporter_acquire_lock(PyObject *self __attribute__((unused)), PyObject *noargs __attribute__((unused)))
{
#ifdef WITH_THREAD
_PyImport_AcquireLock();
Expand All @@ -323,7 +327,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_release_lock(PyObject *self, PyObject *noargs)
mpiimporter_release_lock(PyObject *self __attribute__((unused)), PyObject *noargs __attribute__((unused)))
{
#ifdef WITH_THREAD
if (_PyImport_ReleaseLock() < 0) {
Expand Down Expand Up @@ -826,6 +830,8 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
return fopen(filename, "wb");
#endif
}
// non-rank 0 processes don't open a file
return NULL;
}


Expand Down Expand Up @@ -961,7 +967,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
*/
if (mtime >> 32) {
char * mtimemsg;
asprintf(mtimemsg,"modification time '%ld' overflows a 4 byte field",mtime);
asprintf(&mtimemsg,"modification time '%ld' overflows a 4 byte field",mtime);
PyErr_SetString(PyExc_OverflowError,
mtimemsg);
return NULL;
Expand Down Expand Up @@ -1497,7 +1503,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
#endif

static int
case_ok(char *buf, Py_ssize_t len, Py_ssize_t namelen, char *name)
case_ok(char *buf __attribute__((unused)), Py_ssize_t len __attribute__((unused)), Py_ssize_t namelen __attribute__((unused)), char *name __attribute__((unused)))
{
/* Pick a platform-specific implementation; the sequence of #if's here should
* match the sequence just above.
Expand Down Expand Up @@ -2002,7 +2008,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
/* The Magnum Opus of dotted-name import :-) */

static PyObject *
import_module_level(char *name, PyObject *globals, PyObject *locals,
import_module_level(char *name, PyObject *globals, PyObject *locals __attribute__((unused)),
PyObject *fromlist, int level)
{
char buf[MAXPATHLEN+1];
Expand Down Expand Up @@ -2674,7 +2680,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
*/

static PyObject *
mpiimporter_get_magic(PyObject *self, PyObject *noargs)
mpiimporter_get_magic(PyObject *self __attribute__((unused)), PyObject *noargs __attribute__((unused)))
{
char buf[4];

Expand All @@ -2687,7 +2693,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_get_suffixes(PyObject *self, PyObject *noargs)
mpiimporter_get_suffixes(PyObject *self __attribute__((unused)), PyObject *noargs __attribute__((unused)))
{
PyObject *list;
struct filedescr *fdp;
Expand Down Expand Up @@ -2747,7 +2753,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_find_module(PyObject *self, PyObject *args)
mpiimporter_find_module(PyObject *self __attribute__((unused)), PyObject *args)
{
char *name;
PyObject *path = NULL;
Expand All @@ -2757,7 +2763,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_init_builtin(PyObject *self, PyObject *args)
mpiimporter_init_builtin(PyObject *self __attribute__((unused)), PyObject *args)
{
char *name;
int ret;
Expand All @@ -2777,7 +2783,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_init_frozen(PyObject *self, PyObject *args)
mpiimporter_init_frozen(PyObject *self __attribute__((unused)), PyObject *args)
{
char *name;
int ret;
Expand All @@ -2797,7 +2803,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_get_frozen_object(PyObject *self, PyObject *args)
mpiimporter_get_frozen_object(PyObject *self __attribute__((unused)), PyObject *args)
{
char *name;

Expand All @@ -2807,7 +2813,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_is_builtin(PyObject *self, PyObject *args)
mpiimporter_is_builtin(PyObject *self __attribute__((unused)), PyObject *args)
{
char *name;
if (!PyArg_ParseTuple(args, "s:is_builtin", &name))
Expand All @@ -2816,7 +2822,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_is_frozen(PyObject *self, PyObject *args)
mpiimporter_is_frozen(PyObject *self __attribute__((unused)), PyObject *args)
{
char *name;
struct _frozen *p;
Expand Down Expand Up @@ -2847,7 +2853,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_load_compiled(PyObject *self, PyObject *args)
mpiimporter_load_compiled(PyObject *self __attribute__((unused)), PyObject *args)
{
char *name;
char *pathname;
Expand All @@ -2869,7 +2875,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
#ifdef HAVE_DYNAMIC_LOADING

static PyObject *
mpiimporter_load_dynamic(PyObject *self, PyObject *args)
mpiimporter_load_dynamic(PyObject *self __attribute__((unused)), PyObject *args)
{
char *name;
char *pathname;
Expand All @@ -2891,7 +2897,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
#endif /* HAVE_DYNAMIC_LOADING */

static PyObject *
mpiimporter_load_source(PyObject *self, PyObject *args)
mpiimporter_load_source(PyObject *self __attribute__((unused)), PyObject *args)
{
char *name;
char *pathname;
Expand All @@ -2911,7 +2917,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_load_module(PyObject *self, PyObject *args)
mpiimporter_load_module(PyObject *self __attribute__((unused)), PyObject *args)
{
char *name;
PyObject *fob;
Expand Down Expand Up @@ -2952,7 +2958,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_load_package(PyObject *self, PyObject *args)
mpiimporter_load_package(PyObject *self __attribute__((unused)), PyObject *args)
{
char *name;
char *pathname;
Expand All @@ -2962,7 +2968,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_new_module(PyObject *self, PyObject *args)
mpiimporter_new_module(PyObject *self __attribute__((unused)), PyObject *args)
{
char *name;
if (!PyArg_ParseTuple(args, "s:new_module", &name))
Expand All @@ -2971,7 +2977,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
mpiimporter_reload(PyObject *self, PyObject *v)
mpiimporter_reload(PyObject *self __attribute__((unused)), PyObject *v)
{
return PyImport_ReloadModule(v);
}
Expand Down Expand Up @@ -3073,7 +3079,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
} NullImporter;

static int
NullImporter_init(NullImporter *self, PyObject *args, PyObject *kwds)
NullImporter_init(NullImporter *self __attribute__((unused)), PyObject *args, PyObject *kwds)
{
char *path;
Py_ssize_t pathlen;
Expand Down Expand Up @@ -3109,7 +3115,7 @@ static const struct filedescr _PyImport_StandardFiletab[] = {
}

static PyObject *
NullImporter_find_module(NullImporter *self, PyObject *args)
NullImporter_find_module(NullImporter *self __attribute__((unused)), PyObject *args __attribute__((unused)))
{
Py_RETURN_NONE;
}
Expand Down
2 changes: 1 addition & 1 deletion makeinc.debian_sid
Expand Up @@ -2,7 +2,7 @@ CC = gcc
MPICC = mpicc

CFLAGS = -std=c99 -fPIC -Wall -Wextra ${CFLAGS_DEBUG} -D_LARGEFILE64_SOURCE
CFLAGS_DEBUG = -g3 -DDEBUG=1
CFLAGS_DEBUG = -g3 -DDEBUG=1 -Werror -Wno-missing-field-initializers
LIBCPATH = /usr/lib
LIBDL = -ldl
LDSO = ld-2.11.1.so
Expand Down
12 changes: 8 additions & 4 deletions mpiimport.py
Expand Up @@ -2,14 +2,17 @@
import imp
import mpiimporter

from mpi4py import MPI

class Importer:
def __init__(self, path=None):
if path is not None and not os.path.isdir(path):
raise ImportError
self.path = path

def find_module(self, fullname, path=None):
print "find_module", fullname, path
rank = MPI.COMM_WORLD.Get_rank()
# print "[%d] find_module %s %s" % (rank, fullname, path)

subname = fullname.split(".")[-1]
if subname != fullname and self.path is None:
Expand All @@ -24,7 +27,7 @@ def find_module(self, fullname, path=None):
# print ImportError
return None

print "find_module found: ", file, filename
# print "[%d] find_module found: %s %s" % (rank, file, filename)

ignore, ext = os.path.splitext(filename)

Expand All @@ -42,13 +45,14 @@ def __init__(self, file, filename, stuff):
self.stuff = stuff

def load_module(self, fullname):
print "load_module: ", fullname, self.file, self.filename, self.stuff
rank = MPI.COMM_WORLD.Get_rank()
# print "[%d] load_module: %s %s %s %s" % (rank, fullname, self.file, self.filename, self.stuff)
mod = mpiimporter.load_module(fullname, self.file, self.filename, self.stuff)
if self.file:
self.file.close()
mod.__loader__ = self # for introspection

print "load_module loaded: ", mod
# print "[%d] load_module loaded: %s" % (rank, mod)
return mod

class ImpLoader:
Expand Down

0 comments on commit 1a1a96a

Please sign in to comment.