summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Jones <eric@enthought.com>2003-03-07 15:11:46 +0000
committerEric Jones <eric@enthought.com>2003-03-07 15:11:46 +0000
commite95e42e5e2e4027ed03a32ecb066521e5997f4ba (patch)
treebea2ef15101fddfff800a7072636c234fe907706
parentc21bf7ecfd8fd90f09bc8ec9eee8cdbfd1960e43 (diff)
downloadnumpy-e95e42e5e2e4027ed03a32ecb066521e5997f4ba.tar.gz
Moved code in swigptr.c into a python string, swigptr.swigptr_code. This makes it where the code doesn't have to be read out of a file in the directory path and makes it easier on py2exe like programs that bundle code into an exe file. [see bug 37]
I think weave will still not work in bundled applications because it referes to a lot of other external files in the scxx directory and blitz directories. Still, these are not read during import, so they at least don't cause the scipy import to fail. We should look for a way to make weave work from py2exe binaries in the future.
-rw-r--r--weave/common_info.py12
-rw-r--r--weave/swigptr.py427
-rw-r--r--weave/tests/test_blitz_tools.py2
-rw-r--r--weave/tests/test_c_spec.py2
4 files changed, 434 insertions, 9 deletions
diff --git a/weave/common_info.py b/weave/common_info.py
index ce8ea9a13..fb4374914 100644
--- a/weave/common_info.py
+++ b/weave/common_info.py
@@ -118,15 +118,13 @@ class inline_info(base_info.base_info):
# swig pointer support code
#
# The support code for swig is just slirped in from the swigptr.c file
-# from the *old* swig distribution. New style swig pointers are not
-# yet supported.
+# from the *old* swig distribution. The code from swigptr.c is now a string
+# in swigptr.py to ease the process of incorporating it into py2exe
+# installations. New style swig pointers are not yet supported.
#----------------------------------------------------------------------------
-import os, common_info
-local_dir,junk = os.path.split(os.path.abspath(common_info.__file__))
-f = open(os.path.join(local_dir,'swig','swigptr.c'))
-swig_support_code = f.read()
-f.close()
+import swigptr
+swig_support_code = swigptr.swigptr_code
class swig_info(base_info.base_info):
_support_code = [swig_support_code]
diff --git a/weave/swigptr.py b/weave/swigptr.py
new file mode 100644
index 000000000..e2705c4c3
--- /dev/null
+++ b/weave/swigptr.py
@@ -0,0 +1,427 @@
+# swigptr.py
+
+swigptr_code = """
+
+/***********************************************************************
+ * $Header$
+ * swig_lib/python/python.cfg
+ *
+ * Contains variable linking and pointer type-checking code.
+ ************************************************************************/
+
+#include <string.h>
+#include <stdlib.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#include "Python.h"
+
+/* Definitions for Windows/Unix exporting */
+#if defined(_WIN32) || defined(__WIN32__)
+# if defined(_MSC_VER)
+# define SWIGEXPORT(a) __declspec(dllexport) a
+# else
+# if defined(__BORLANDC__)
+# define SWIGEXPORT(a) a _export
+# else
+# define SWIGEXPORT(a) a
+# endif
+# endif
+#else
+# define SWIGEXPORT(a) a
+#endif
+
+#ifdef SWIG_GLOBAL
+#define SWIGSTATICRUNTIME(a) SWIGEXPORT(a)
+#else
+#define SWIGSTATICRUNTIME(a) static a
+#endif
+
+typedef struct {
+ char *name;
+ PyObject *(*get_attr)(void);
+ int (*set_attr)(PyObject *);
+} swig_globalvar;
+
+typedef struct swig_varlinkobject {
+ PyObject_HEAD
+ swig_globalvar **vars;
+ int nvars;
+ int maxvars;
+} swig_varlinkobject;
+
+/* ----------------------------------------------------------------------
+ swig_varlink_repr()
+
+ Function for python repr method
+ ---------------------------------------------------------------------- */
+
+static PyObject *
+swig_varlink_repr(swig_varlinkobject *v)
+{
+ v = v;
+ return PyString_FromString("<Global variables>");
+}
+
+/* ---------------------------------------------------------------------
+ swig_varlink_print()
+
+ Print out all of the global variable names
+ --------------------------------------------------------------------- */
+
+static int
+swig_varlink_print(swig_varlinkobject *v, FILE *fp, int flags)
+{
+
+ int i = 0;
+ flags = flags;
+ fprintf(fp,"Global variables { ");
+ while (v->vars[i]) {
+ fprintf(fp,"%s", v->vars[i]->name);
+ i++;
+ if (v->vars[i]) fprintf(fp,", ");
+ }
+ fprintf(fp," }\n");
+ return 0;
+}
+
+/* --------------------------------------------------------------------
+ swig_varlink_getattr
+
+ This function gets the value of a variable and returns it as a
+ PyObject. In our case, we'll be looking at the datatype and
+ converting into a number or string
+ -------------------------------------------------------------------- */
+
+static PyObject *
+swig_varlink_getattr(swig_varlinkobject *v, char *n)
+{
+ int i = 0;
+ char temp[128];
+
+ while (v->vars[i]) {
+ if (strcmp(v->vars[i]->name,n) == 0) {
+ return (*v->vars[i]->get_attr)();
+ }
+ i++;
+ }
+ sprintf(temp,"C global variable %s not found.", n);
+ PyErr_SetString(PyExc_NameError,temp);
+ return NULL;
+}
+
+/* -------------------------------------------------------------------
+ swig_varlink_setattr()
+
+ This function sets the value of a variable.
+ ------------------------------------------------------------------- */
+
+static int
+swig_varlink_setattr(swig_varlinkobject *v, char *n, PyObject *p)
+{
+ char temp[128];
+ int i = 0;
+ while (v->vars[i]) {
+ if (strcmp(v->vars[i]->name,n) == 0) {
+ return (*v->vars[i]->set_attr)(p);
+ }
+ i++;
+ }
+ sprintf(temp,"C global variable %s not found.", n);
+ PyErr_SetString(PyExc_NameError,temp);
+ return 1;
+}
+
+statichere PyTypeObject varlinktype = {
+/* PyObject_HEAD_INIT(&PyType_Type) Note : This doesn't work on some machines */
+ PyObject_HEAD_INIT(0)
+ 0,
+ "varlink", /* Type name */
+ sizeof(swig_varlinkobject), /* Basic size */
+ 0, /* Itemsize */
+ 0, /* Deallocator */
+ (printfunc) swig_varlink_print, /* Print */
+ (getattrfunc) swig_varlink_getattr, /* get attr */
+ (setattrfunc) swig_varlink_setattr, /* Set attr */
+ 0, /* tp_compare */
+ (reprfunc) swig_varlink_repr, /* tp_repr */
+ 0, /* tp_as_number */
+ 0, /* tp_as_mapping*/
+ 0, /* tp_hash */
+};
+
+/* Create a variable linking object for use later */
+
+SWIGSTATICRUNTIME(PyObject *)
+SWIG_newvarlink(void)
+{
+ swig_varlinkobject *result = 0;
+ result = PyMem_NEW(swig_varlinkobject,1);
+ varlinktype.ob_type = &PyType_Type; /* Patch varlinktype into a PyType */
+ result->ob_type = &varlinktype;
+ /* _Py_NewReference(result); Does not seem to be necessary */
+ result->nvars = 0;
+ result->maxvars = 64;
+ result->vars = (swig_globalvar **) malloc(64*sizeof(swig_globalvar *));
+ result->vars[0] = 0;
+ result->ob_refcnt = 0;
+ Py_XINCREF((PyObject *) result);
+ return ((PyObject*) result);
+}
+
+SWIGSTATICRUNTIME(void)
+SWIG_addvarlink(PyObject *p, char *name,
+ PyObject *(*get_attr)(void), int (*set_attr)(PyObject *p))
+{
+ swig_varlinkobject *v;
+ v= (swig_varlinkobject *) p;
+
+ if (v->nvars >= v->maxvars -1) {
+ v->maxvars = 2*v->maxvars;
+ v->vars = (swig_globalvar **) realloc(v->vars,v->maxvars*sizeof(swig_globalvar *));
+ if (v->vars == NULL) {
+ fprintf(stderr,"SWIG : Fatal error in initializing Python module.\n");
+ exit(1);
+ }
+ }
+ v->vars[v->nvars] = (swig_globalvar *) malloc(sizeof(swig_globalvar));
+ v->vars[v->nvars]->name = (char *) malloc(strlen(name)+1);
+ strcpy(v->vars[v->nvars]->name,name);
+ v->vars[v->nvars]->get_attr = get_attr;
+ v->vars[v->nvars]->set_attr = set_attr;
+ v->nvars++;
+ v->vars[v->nvars] = 0;
+}
+
+/* -----------------------------------------------------------------------------
+ * Pointer type-checking
+ * ----------------------------------------------------------------------------- */
+
+/* SWIG pointer structure */
+typedef struct SwigPtrType {
+ char *name; /* Datatype name */
+ int len; /* Length (used for optimization) */
+ void *(*cast)(void *); /* Pointer casting function */
+ struct SwigPtrType *next; /* Linked list pointer */
+} SwigPtrType;
+
+/* Pointer cache structure */
+typedef struct {
+ int stat; /* Status (valid) bit */
+ SwigPtrType *tp; /* Pointer to type structure */
+ char name[256]; /* Given datatype name */
+ char mapped[256]; /* Equivalent name */
+} SwigCacheType;
+
+static int SwigPtrMax = 64; /* Max entries that can be currently held */
+static int SwigPtrN = 0; /* Current number of entries */
+static int SwigPtrSort = 0; /* Status flag indicating sort */
+static int SwigStart[256]; /* Starting positions of types */
+static SwigPtrType *SwigPtrTable = 0; /* Table containing pointer equivalences */
+
+/* Cached values */
+#define SWIG_CACHESIZE 8
+#define SWIG_CACHEMASK 0x7
+static SwigCacheType SwigCache[SWIG_CACHESIZE];
+static int SwigCacheIndex = 0;
+static int SwigLastCache = 0;
+
+/* Sort comparison function */
+static int swigsort(const void *data1, const void *data2) {
+ SwigPtrType *d1 = (SwigPtrType *) data1;
+ SwigPtrType *d2 = (SwigPtrType *) data2;
+ return strcmp(d1->name,d2->name);
+}
+
+/* Register a new datatype with the type-checker */
+SWIGSTATICRUNTIME(void)
+SWIG_RegisterMapping(char *origtype, char *newtype, void *(*cast)(void *)) {
+ int i;
+ SwigPtrType *t = 0,*t1;
+
+ /* Allocate the pointer table if necessary */
+ if (!SwigPtrTable) {
+ SwigPtrTable = (SwigPtrType *) malloc(SwigPtrMax*sizeof(SwigPtrType));
+ }
+
+ /* Grow the table */
+ if (SwigPtrN >= SwigPtrMax) {
+ SwigPtrMax = 2*SwigPtrMax;
+ SwigPtrTable = (SwigPtrType *) realloc((char *) SwigPtrTable,SwigPtrMax*sizeof(SwigPtrType));
+ }
+ for (i = 0; i < SwigPtrN; i++) {
+ if (strcmp(SwigPtrTable[i].name,origtype) == 0) {
+ t = &SwigPtrTable[i];
+ break;
+ }
+ }
+ if (!t) {
+ t = &SwigPtrTable[SwigPtrN++];
+ t->name = origtype;
+ t->len = strlen(t->name);
+ t->cast = 0;
+ t->next = 0;
+ }
+
+ /* Check for existing entries */
+ while (t->next) {
+ if ((strcmp(t->name,newtype) == 0)) {
+ if (cast) t->cast = cast;
+ return;
+ }
+ t = t->next;
+ }
+ t1 = (SwigPtrType *) malloc(sizeof(SwigPtrType));
+ t1->name = newtype;
+ t1->len = strlen(t1->name);
+ t1->cast = cast;
+ t1->next = 0;
+ t->next = t1;
+ SwigPtrSort = 0;
+}
+
+/* Make a pointer value string */
+SWIGSTATICRUNTIME(void)
+SWIG_MakePtr(char *c, const void *ptr, char *type) {
+ static char hex[17] = "0123456789abcdef";
+ unsigned long p, s;
+ char result[24], *r;
+ r = result;
+ p = (unsigned long) ptr;
+ if (p > 0) {
+ while (p > 0) {
+ s = p & 0xf;
+ *(r++) = hex[s];
+ p = p >> 4;
+ }
+ *r = '_';
+ while (r >= result)
+ *(c++) = *(r--);
+ strcpy (c, type);
+ } else {
+ strcpy (c, "NULL");
+ }
+}
+
+/* Function for getting a pointer value */
+SWIGSTATICRUNTIME(char *)
+SWIG_GetPtr(char *c, void **ptr, char *t)
+{
+ //std::cout << t << " " << c << std::endl;
+ unsigned long p;
+ char temp_type[256], *name;
+ int i, len, start, end;
+ SwigPtrType *sp,*tp;
+ SwigCacheType *cache;
+ register int d;
+ p = 0;
+ /* Pointer values must start with leading underscore */
+ if (*c != '_') {
+ *ptr = (void *) 0;
+ if (strcmp(c,"NULL") == 0) return (char *) 0;
+ else c;
+ }
+ c++;
+ /* Extract hex value from pointer */
+ while (d = *c) {
+ if ((d >= '0') && (d <= '9'))
+ p = (p << 4) + (d - '0');
+ else if ((d >= 'a') && (d <= 'f'))
+ p = (p << 4) + (d - ('a'-10));
+ else
+ break;
+ c++;
+ }
+ *ptr = (void *) p;
+ //std::cout << t << " " << c << std::endl;
+ if ((!t) || (strcmp(t,c)==0))
+ return (char *) 0;
+ else
+ {
+ // added ej -- if type check fails, its always an error.
+ return (char*) 1;
+ }
+ if (!SwigPtrSort) {
+ qsort((void *) SwigPtrTable, SwigPtrN, sizeof(SwigPtrType), swigsort);
+ for (i = 0; i < 256; i++) SwigStart[i] = SwigPtrN;
+ for (i = SwigPtrN-1; i >= 0; i--) SwigStart[(int) (SwigPtrTable[i].name[1])] = i;
+ for (i = 255; i >= 1; i--) {
+ if (SwigStart[i-1] > SwigStart[i])
+ SwigStart[i-1] = SwigStart[i];
+ }
+ SwigPtrSort = 1;
+ for (i = 0; i < SWIG_CACHESIZE; i++) SwigCache[i].stat = 0;
+ }
+ /* First check cache for matches. Uses last cache value as starting point */
+ cache = &SwigCache[SwigLastCache];
+ for (i = 0; i < SWIG_CACHESIZE; i++) {
+ if (cache->stat && (strcmp(t,cache->name) == 0) && (strcmp(c,cache->mapped) == 0)) {
+ cache->stat++;
+ if (cache->tp->cast) *ptr = (*(cache->tp->cast))(*ptr);
+ return (char *) 0;
+ }
+ SwigLastCache = (SwigLastCache+1) & SWIG_CACHEMASK;
+ if (!SwigLastCache) cache = SwigCache;
+ else cache++;
+ }
+ /* Type mismatch. Look through type-mapping table */
+ start = SwigStart[(int) t[1]];
+ end = SwigStart[(int) t[1]+1];
+ sp = &SwigPtrTable[start];
+
+ /* Try to find a match */
+ while (start <= end) {
+ if (strncmp(t,sp->name,sp->len) == 0) {
+ name = sp->name;
+ len = sp->len;
+ tp = sp->next;
+ /* Try to find entry for our given datatype */
+ while(tp) {
+ if (tp->len >= 255) {
+ return c;
+ }
+ strcpy(temp_type,tp->name);
+ strncat(temp_type,t+len,255-tp->len);
+ if (strcmp(c,temp_type) == 0) {
+ strcpy(SwigCache[SwigCacheIndex].mapped,c);
+ strcpy(SwigCache[SwigCacheIndex].name,t);
+ SwigCache[SwigCacheIndex].stat = 1;
+ SwigCache[SwigCacheIndex].tp = tp;
+ SwigCacheIndex = SwigCacheIndex & SWIG_CACHEMASK;
+ /* Get pointer value */
+ *ptr = (void *) p;
+ if (tp->cast) *ptr = (*(tp->cast))(*ptr);
+ return (char *) 0;
+ }
+ tp = tp->next;
+ }
+ }
+ sp++;
+ start++;
+ }
+ return c;
+}
+
+/* New object-based GetPointer function. This uses the Python abstract
+ * object interface to automatically dereference the 'this' attribute
+ * of shadow objects. */
+
+SWIGSTATICRUNTIME(char *)
+SWIG_GetPtrObj(PyObject *obj, void **ptr, char *type) {
+ PyObject *sobj = obj;
+ char *str;
+ if (!PyString_Check(obj)) {
+ sobj = PyObject_GetAttrString(obj,"this");
+ if (!sobj) return "";
+ }
+ str = PyString_AsString(sobj);
+ //printf("str: %s\n", str);
+ return SWIG_GetPtr(str,ptr,type);
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+""" \ No newline at end of file
diff --git a/weave/tests/test_blitz_tools.py b/weave/tests/test_blitz_tools.py
index 26a7b41eb..b95e4a979 100644
--- a/weave/tests/test_blitz_tools.py
+++ b/weave/tests/test_blitz_tools.py
@@ -153,7 +153,7 @@ class test_blitz(unittest.TestCase):
expr = "result[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1]" \
"+ b[1:-1,2:] + b[1:-1,:-2]) / 5."
self.generic_2d(expr,Float64)
- def check_5point_avg_2d_complex_float(self):
+ def _check_5point_avg_2d_complex_float(self):
""" Note: THIS TEST is KNOWN TO FAIL ON GCC 3.x. It will not adversely affect 99.99 percent of weave
result[1:-1,1:-1] = (b[1:-1,1:-1] + b[2:,1:-1] + b[:-2,1:-1]
diff --git a/weave/tests/test_c_spec.py b/weave/tests/test_c_spec.py
index 2043ac754..5a23a1bcc 100644
--- a/weave/tests/test_c_spec.py
+++ b/weave/tests/test_c_spec.py
@@ -697,7 +697,7 @@ def test_suite(level=1):
suites.append( makeSuite(test_unix_float_converter,'check_'))
suites.append( makeSuite(test_unix_complex_converter,'check_'))
# run gcc tests also on windows
- if gcc_exists() and sys.platform == 'win32':
+ if gcc_exists() and msvc_exists() and sys.platform == 'win32':
suites.append( makeSuite(test_gcc_file_converter,'check_'))
suites.append( makeSuite(test_gcc_instance_converter,'check_'))
suites.append( makeSuite(test_gcc_callable_converter,'check_'))