[Pyshapelib-commits] r2901 - trunk/pyshapelib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Dec 27 00:12:04 CET 2011


Author: bramz
Date: 2011-12-27 00:12:02 +0100 (Tue, 27 Dec 2011)
New Revision: 2901

Modified:
   trunk/pyshapelib/ChangeLog
   trunk/pyshapelib/dbflibmodule.c
   trunk/pyshapelib/pyshapelib_api.h
   trunk/pyshapelib/pyshapelib_common.h
   trunk/pyshapelib/setup.py
   trunk/pyshapelib/shapelibmodule.c
   trunk/pyshapelib/shptreemodule.c
   trunk/pyshapelib/testdbf.py
Log:
Bumped version to 1.1; 
First part of port to Python 3.x, behavior of return_unicode flag is still the same, it's False by default.


Modified: trunk/pyshapelib/ChangeLog
===================================================================
--- trunk/pyshapelib/ChangeLog	2011-09-06 07:39:28 UTC (rev 2900)
+++ trunk/pyshapelib/ChangeLog	2011-12-26 23:12:02 UTC (rev 2901)
@@ -1,3 +1,13 @@
+2011-12-17  Bram de Greve <bram.degreve at bramz.net>
+
+	Bumped version to 1.1
+	
+	First part of port to Python 3.x. 
+	Codebase should compile on both Python 2.x and 3.x.
+	Behaviour is still as before: 
+	- by default, raw encoded string values are returned (2.x: str, 3.x: bytes)
+	- return_unicode=True returns unicode strings (2.x: unicode, 3.x: str)
+
 2011-09-06  Björn Ricks <bjoern.ricks at intevation.de>
 
 	PyShapeLib 1.0 Release

Modified: trunk/pyshapelib/dbflibmodule.c
===================================================================
--- trunk/pyshapelib/dbflibmodule.c	2011-09-06 07:39:28 UTC (rev 2900)
+++ trunk/pyshapelib/dbflibmodule.c	2011-12-26 23:12:02 UTC (rev 2901)
@@ -47,7 +47,7 @@
 	self->handle = NULL;
 	PyMem_Free(self->codec);
 	self->codec = NULL;
-	self->ob_type->tp_free((PyObject*)self);
+	Py_TYPE(self)->tp_free((PyObject*)self);
 }
 
 
@@ -82,7 +82,16 @@
 			PyErr_Format(PyExc_KeyError, "code_page '%s' not found in codecs_map", code_page);
 			return -1;
 		}
-		codec = PyString_AsString(ocodec);
+		if (PyUnicode_Check(ocodec))
+		{
+			ocodec = PyUnicode_AsUTF8String(ocodec); // is this accepted?  UTF8 codec names?
+		}
+		else
+		{
+			Py_INCREF(ocodec);
+		}
+		codec = PYSHAPELIB_ASSTRING(ocodec);
+		Py_DECREF(ocodec);
 		if (!codec)
 		{
 			return -1;
@@ -173,14 +182,22 @@
 	{
 		return PyUnicode_Decode(string, strlen(string), self->codec, NULL);
 	}
+#if PYSHAPELIB_IS_PY3K
+	return PyBytes_FromString(string);
+#else
 	return PyString_FromString(string);
+#endif
 }
 
-/** encode unicode object to normal Python string object 
+/** encode unicode object to normal Python string/bytes object 
  */
 static PyObject* dbffile_encode_string(DBFFileObject* self, PyObject* string)
 {
+#if PYSHAPELIB_IS_PY3K
+	if (PyBytes_Check(string))
+#else
 	if (PyString_Check(string))
+#endif
 	{
 		Py_INCREF(string);
 		return string;
@@ -198,14 +215,14 @@
 
 static PyObject* dbffile_field_count(DBFFileObject* self)
 {
-	return PyInt_FromLong((long)DBFGetFieldCount(self->handle));
+	return PYSHAPELIB_FROMLONG((long)DBFGetFieldCount(self->handle));
 }
 
 
 
 static PyObject* dbffile_record_count(DBFFileObject* self)
 {
-	return PyInt_FromLong((long)DBFGetRecordCount(self->handle));
+	return PYSHAPELIB_FROMLONG((long)DBFGetRecordCount(self->handle));
 }
 
 
@@ -274,7 +291,7 @@
 	name = dbffile_encode_string(self, oname);
 	if (!name) return NULL;
 
-	field = DBFAddField(self->handle, PyString_AsString(name), (DBFFieldType)type, width, decimals);
+	field = DBFAddField(self->handle, PYSHAPELIB_ASSTRING(name), (DBFFieldType)type, width, decimals);
 	Py_DECREF(name);
 	
 	if (field < 0)
@@ -282,7 +299,7 @@
 		PyErr_SetString(PyExc_ValueError, "Failed to add field");
 		return NULL;
 	}
-	return PyInt_FromLong((long)field);
+	return PYSHAPELIB_FROMLONG((long)field);
 }
 
 
@@ -380,7 +397,7 @@
 		}
 	}
 	
-	PyErr_Format(PyExc_IOError,	"Can't read value for row %d column %d", record, field);
+	PyErr_Format(PyExc_IOError, "Can't read value for row %d column %d", record, field);
 	return NULL;
 }
 
@@ -459,7 +476,7 @@
 		case FTString:
 			string_value = dbffile_encode_string(self, value);
 			if (!string_value) return 0;
-			if (DBFWriteStringAttribute(self->handle, record, field, PyString_AsString(string_value)))
+			if (DBFWriteStringAttribute(self->handle, record, field, PYSHAPELIB_ASSTRING(string_value)))
 			{
 				Py_DECREF(string_value);
 				return 1;
@@ -468,7 +485,7 @@
 			break;
 
 		case FTInteger:
-			int_value = PyInt_AsLong(value);
+			int_value = PYSHAPELIB_ASLONG(value);
 			if (int_value == -1 && PyErr_Occurred()) return 0;
 			if (DBFWriteIntegerAttribute(self->handle, record, field, int_value)) return 1;
 			break;
@@ -570,7 +587,7 @@
 		}
 	}
 	
-	return PyInt_FromLong((long)record);
+	return PYSHAPELIB_FROMLONG((long)record);
 }
 
 
@@ -578,7 +595,7 @@
 static PyObject* dbffile_repr(DBFFileObject* self)
 {
 	/* TODO: it would be nice to do something like "dbflib.DBFFile(filename, mode)" instead */
-	return PyString_FromFormat("<dbflib.DBFFile object at %p>", self->handle);
+	return PYSHAPELIB_FROMFORMAT("<dbflib.DBFFile object at %p>", self->handle);
 }
 
 
@@ -608,7 +625,7 @@
 	{
 		Py_RETURN_NONE;
 	}
-	return PyString_FromString(code_page);
+	return PYSHAPELIB_FROMSTRING(code_page);
 }
 
 #endif
@@ -619,7 +636,7 @@
 	{
 		Py_RETURN_NONE;
 	}
-	return PyString_FromString(self->codec);
+	return PYSHAPELIB_FROMSTRING(self->codec);
 }
 
 
@@ -786,7 +803,7 @@
 {
 	char code_page[64];
 	char constant[64];
-	PyObject* ocodec = PyString_FromString(codec);
+	PyObject* ocodec = PYSHAPELIB_FROMSTRING(codec);
 	sprintf(code_page, "LDID/%i", ldid);
 	PyDict_SetItemString(default_codecs_map, code_page, ocodec);
 	Py_XDECREF(ocodec);
@@ -797,8 +814,8 @@
 void add_cpg(PyObject* module, char* code_page, const char* codec, const char* name)
 {
 	char constant[64];
-	PyObject* ocodec = PyString_FromString(codec);
-	PyDict_SetItemString(default_codecs_map, code_page, PyString_FromString(codec));
+	PyObject* ocodec = PYSHAPELIB_FROMSTRING(codec);
+	PyDict_SetItemString(default_codecs_map, code_page, PYSHAPELIB_FROMSTRING(codec));
 	Py_XDECREF(ocodec);
 	sprintf(constant, "CPG_%s", name);
 	PyModule_AddStringConstant(module, constant, code_page);
@@ -806,10 +823,32 @@
 
 #endif
 
+
+#if PYSHAPELIB_IS_PY3K
+
+static struct PyModuleDef dbflib_moduledef = {
+		PyModuleDef_HEAD_INIT,
+		"dbflib",
+		NULL,
+		0,
+		dbflib_methods,
+		NULL,
+		NULL,
+		NULL,
+		NULL
+};
+
+PyMODINIT_FUNC PyInit_dbflib(void)
+#else
 PyMODINIT_FUNC initdbflib(void)
+#endif
 {
+#if PYSHAPELIB_IS_PY3K
+	PyObject *module = PyModule_Create(&dbflib_moduledef);
+#else
 	PyObject* module = Py_InitModule("dbflib", dbflib_methods);
-	if (!module) return;
+#endif
+	if (!module) PYSHAPELIB_INITRETURN(NULL);
 	
 	PYSHAPELIB_ADD_TYPE(DBFFileType, "DBFFile");
 	
@@ -929,4 +968,5 @@
 
 #endif
 
+	PYSHAPELIB_INITRETURN(module);
 }

Modified: trunk/pyshapelib/pyshapelib_api.h
===================================================================
--- trunk/pyshapelib/pyshapelib_api.h	2011-09-06 07:39:28 UTC (rev 2900)
+++ trunk/pyshapelib/pyshapelib_api.h	2011-12-26 23:12:02 UTC (rev 2901)
@@ -4,6 +4,17 @@
 #ifndef PYSHAPELIB_API_H
 #define PYSHAPELIB_API_H
 
+#if PY_VERSION_HEX < 0x03010000 // < 3.1
+#	define PYSHAPELIB_CAPSULE_TYPE PyCObject_Type
+#	define PYSHAPELIB_FROMVOID(value, name, destructor) PyCObject_FromVoidPtr(value, destructor)
+#	define PYSHAPELIB_ASVOID(object, name) PyCObject_AsVoidPtr(object)
+#else
+#	define PYSHAPELIB_CAPSULE_TYPE PyCapsule_Type
+#	define PYSHAPELIB_FROMVOID(value, name, destructor) PyCapsule_New(value, name, destructor)
+#	define PYSHAPELIB_ASVOID(object, name) PyCapsule_GetPointer(object, name)
+#endif
+
+
 typedef struct {
     /* Shapefile functions */
     SHPObject * (*SHPReadObject)(SHPHandle hSHP, int iShape);
@@ -31,7 +42,7 @@
 	    PyObject * cobj = PyObject_CallObject(c_api_func, NULL);	   \
 	    if (cobj)							   \
 	    {								   \
-		(apivariable) = (PyShapeLibAPI*)PyCObject_AsVoidPtr(cobj); \
+		(apivariable) = (PyShapeLibAPI*)PYSHAPELIB_ASVOID(cobj, NULL); \
 	    }								   \
 	}								   \
     }									   \

Modified: trunk/pyshapelib/pyshapelib_common.h
===================================================================
--- trunk/pyshapelib/pyshapelib_common.h	2011-09-06 07:39:28 UTC (rev 2900)
+++ trunk/pyshapelib/pyshapelib_common.h	2011-12-26 23:12:02 UTC (rev 2901)
@@ -15,6 +15,49 @@
 #include "pyshapelib_api.h"
 
 
+
+#ifndef Py_RETURN_NONE
+#	define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
+#endif
+#ifndef Py_RETURN_TRUE
+#	define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
+#endif
+#ifndef Py_RETURN_FALSE
+#	define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
+#endif
+#ifndef Py_TYPE
+#	define Py_TYPE(ob) (((PyObject*)(ob))->ob_type)
+#endif
+
+
+/* switch to distinguish between 2.x and 3.x Python API where incompatible.
+ */
+#if PY_MAJOR_VERSION >= 3
+#	define PYSHAPELIB_IS_PY3K 1
+#endif
+
+#if PYSHAPELIB_IS_PY3K
+#	define PYSHAPELIB_ASLONG PyLong_AsLong
+#	define PYSHAPELIB_FROMLONG PyLong_FromLong
+#	define PYSHAPELIB_ASSTRING PyBytes_AsString
+#	define PYSHAPELIB_FROMSTRING PyUnicode_FromString
+#	define PYSHAPELIB_FORMAT PyUnicode_Format
+#	define PYSHAPELIB_FROMFORMAT PyUnicode_FromFormat
+#	define PYSHAPELIB_HEAD_INIT(type, size) PyVarObject_HEAD_INIT(type, size)
+#	define PYSHAPELIB_INITRETURN(value) return value
+#else
+#	define PYSHAPELIB_ASLONG PyInt_AsLong
+#	define PYSHAPELIB_FROMLONG PyInt_FromLong
+#	define PYSHAPELIB_ASSTRING PyString_AsString
+#	define PYSHAPELIB_FROMSTRING PyString_FromString
+#	define PYSHAPELIB_FORMAT PyString_Format
+#	define PYSHAPELIB_FROMFORMAT PyString_FromFormat
+#	define PYSHAPELIB_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
+#	define PYSHAPELIB_INITRETURN(value) return
+#endif
+
+
+
 /* helper to export constants (macros) to Python.
  * The constant in Python will have the same name as in C
  */
@@ -28,8 +71,7 @@
  */
 #define PYSHAPELIB_DEFINE_TYPE(object, prefix, name, doc) \
 { \
-		PyObject_HEAD_INIT(NULL) \
-		0,									/*ob_size*/ \
+		PYSHAPELIB_HEAD_INIT(NULL, 0) \
 		name,								/*tp_name*/ \
 		sizeof(object),						/*tp_basicsize*/ \
 		0,									/*tp_itemsize*/ \
@@ -74,7 +116,7 @@
  * Does a bit of the tedious bookkeeping for us
  */
 #define PYSHAPELIB_ADD_TYPE(type, name) \
-	type.ob_type = &PyType_Type; \
+	Py_TYPE(&type) = &PyType_Type; \
 	if (PyType_Ready(&type) >= 0) \
 	{ \
 		Py_INCREF(&type); \
@@ -83,9 +125,26 @@
 
 #define PYSHAPELIB_NO_DATA 0
 
+
 /* helpers to setup the shapelib API hooks correctly
  */
-#if PY_VERSION_HEX >=0x02040000
+#if PY_VERSION_HEX >=0x03010000
+	double pyshapelib_atof(const char *nptr)
+	{
+		double result;
+		while (*nptr && isspace(*nptr))
+		{
+			++nptr;
+		}
+		result = PyOS_string_to_double(nptr, NULL, NULL);
+		if (result == -1 && PyErr_Occurred())
+		{
+			return 0;
+		}
+		return result;
+	}
+#	define PYSHAPELIB_ATOF pyshapelib_atof
+#elif PY_VERSION_HEX >=0x02040000
 #	define PYSHAPELIB_ATOF PyOS_ascii_atof
 #else
 #	define PYSHAPELIB_ATOF atof
@@ -105,17 +164,5 @@
 		(pHooks)->Atof = PYSHAPELIB_ATOF
 #endif
 
-
-
-#ifndef Py_RETURN_NONE
-#	define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
 #endif
-#ifndef Py_RETURN_TRUE
-#	define Py_RETURN_TRUE return Py_INCREF(Py_True), Py_True
-#endif
-#ifndef Py_RETURN_FALSE
-#	define Py_RETURN_FALSE return Py_INCREF(Py_False), Py_False
-#endif
 
-#endif
-

Modified: trunk/pyshapelib/setup.py
===================================================================
--- trunk/pyshapelib/setup.py	2011-09-06 07:39:28 UTC (rev 2900)
+++ trunk/pyshapelib/setup.py	2011-12-26 23:12:02 UTC (rev 2901)
@@ -41,8 +41,10 @@
 	''' 
 	Return a filelist of additional files implementing the SA hooks.
 	'''
-	candidates = [shp_dir + "/safileio.c"]
-	return filter(os.path.exists, candidates)
+	path = shp_dir + "/safileio.c"
+	if not os.path.exists(path):
+		return []
+	return [path]
 
 sahooks_files = find_sahooks_files()
 
@@ -109,7 +111,7 @@
 
 
 setup(name = "pyshapelib",
-	version = "1.0",
+	version = "1.1",
 	description = "Python bindings for shapelib",
 	author = "Bernhard Herzog, Bram de Greve",
 	author_email = "bh at intevation.de, bram.degreve at bramz.net",

Modified: trunk/pyshapelib/shapelibmodule.c
===================================================================
--- trunk/pyshapelib/shapelibmodule.c	2011-09-06 07:39:28 UTC (rev 2900)
+++ trunk/pyshapelib/shapelibmodule.c	2011-12-26 23:12:02 UTC (rev 2901)
@@ -73,7 +73,7 @@
 {
 	SHPDestroyObject(self->shpObject);
 	self->shpObject = NULL;
-	self->ob_type->tp_free((PyObject*)self);
+	Py_TYPE(self)->tp_free((PyObject*)self);
 }
 
 static int unpack_vertex(PyObject* vertex, int vertex_type, 
@@ -189,7 +189,7 @@
 		for (i = 0; i < num_parts; i++)
 		{
 			PyObject* otype = PySequence_ITEM(part_type_list, i);
-			part_types[i] = PyInt_AsLong(otype);
+			part_types[i] = PYSHAPELIB_ASLONG(otype);
 			Py_DECREF(otype);
 			if (part_types[i] < 0)
 			{
@@ -441,7 +441,7 @@
 	for (i = 0; i < object->nParts; ++i)
 	{
 		/* PyTuple_SetItem steals a reference */
-		PyObject* part_type = PyInt_FromLong((long)object->panPartType[i]);
+		PyObject* part_type = PYSHAPELIB_FROMLONG((long)object->panPartType[i]);
 		if (!part_type || PyTuple_SetItem(result, i, part_type) < 0) goto fail;
 	}	
 	return result;
@@ -455,14 +455,14 @@
 
 static PyObject* shpobject_type(SHPObjectObject* self, void* closure)
 {
-	return PyInt_FromLong(self->shpObject->nSHPType);
+	return PYSHAPELIB_FROMLONG(self->shpObject->nSHPType);
 }
 
 
 
 static PyObject* shpobject_id(SHPObjectObject* self, void* closure)
 {
-	return PyInt_FromLong(self->shpObject->nShapeId);
+	return PYSHAPELIB_FROMLONG(self->shpObject->nShapeId);
 }
 
 
@@ -502,7 +502,7 @@
 	PyObject* args = NULL;
 	PyObject* result = NULL;
 	
-	format = PyString_FromString("shapelib.SHPObject(%i, %i, %s, %s)");
+	format = PyUnicode_FromString("shapelib.SHPObject(%i, %i, %s, %s)");
 	if (!format) return NULL;
 
 	args = getstate(self);
@@ -512,7 +512,7 @@
 		return NULL;
 	}
 	
-	result = PyString_Format(format, args);
+	result = PYSHAPELIB_FORMAT(format, args);
 	Py_DECREF(args);
 	Py_DECREF(format);
 	return result;
@@ -523,7 +523,7 @@
 static PyObject* shpobject_reduce(SHPObjectObject* self)
 {
 	return Py_BuildValue("ON",
-		self->ob_type,
+		Py_TYPE(self),
 		getstate(self));
 }
 
@@ -581,7 +581,7 @@
 static void shapefile_dealloc(ShapeFileObject* self)
 {
 	SHPClose(self->handle);
-	self->ob_type->tp_free((PyObject*)self);
+	Py_TYPE(self)->tp_free((PyObject*)self);
 }
 
 /* constructor
@@ -674,18 +674,18 @@
 		PyErr_SetString(PyExc_RuntimeError, "failed to write object");
 		return NULL;
 	}
-	return PyInt_FromLong((long)result);
+	return PYSHAPELIB_FROMLONG((long)result);
 }
 
 static PyObject* shapefile_cobject(ShapeFileObject* self)
 {
-	return PyCObject_FromVoidPtr(self->handle, NULL);
+	return PYSHAPELIB_FROMVOID(self->handle, NULL, NULL);
 }
 
 static PyObject* shapefile_repr(ShapeFileObject* self)
 {
 	/* TODO: it would be nice to do something like "shapelib.ShapeFile(filename, mode)" instead */
-	return PyString_FromFormat("<shapelib.ShapeFile object at %p>", self->handle);
+	return PYSHAPELIB_FROMFORMAT("<shapelib.ShapeFile object at %p>", self->handle);
 }
 
 static struct PyMethodDef shapefile_methods[] = 
@@ -768,21 +768,21 @@
 
 static PyObject* shapelib_c_api(PyObject* module) 
 {
-	return PyCObject_FromVoidPtr(&shapelib_the_api, NULL);
+	return PYSHAPELIB_FROMVOID(&shapelib_the_api, NULL, NULL);
 }
 
 static PyObject* shapelib_type_name(PyObject* module, PyObject* args)
 {
 	int type;
 	if (!PyArg_ParseTuple(args, "i:type_name", &type)) return NULL;
-	return PyString_FromString(SHPTypeName(type));
+	return PYSHAPELIB_FROMSTRING(SHPTypeName(type));
 }
 
 static PyObject* shapelib_part_type_name(PyObject* module, PyObject* args)
 {
 	int type;
 	if (!PyArg_ParseTuple(args, "i:part_type_name", &type)) return NULL;
-	return PyString_FromString(SHPPartTypeName(type));
+	return PYSHAPELIB_FROMSTRING(SHPPartTypeName(type));
 }
 
 static struct PyMethodDef shapelib_methods[] = 
@@ -805,10 +805,31 @@
 	{NULL}
 };
 
+#if PYSHAPELIB_IS_PY3K
+
+static struct PyModuleDef shapelib_moduledef = {
+		PyModuleDef_HEAD_INIT,
+		"shapelib",
+		NULL,
+		0,
+		shapelib_methods,
+		NULL,
+		NULL,
+		NULL,
+		NULL
+};
+
+PyMODINIT_FUNC PyInit_shapelib(void)
+#else
 PyMODINIT_FUNC initshapelib(void)
+#endif
 {
+#if PYSHAPELIB_IS_PY3K
+	PyObject *module = PyModule_Create(&shapelib_moduledef);
+#else
 	PyObject* module = Py_InitModule("shapelib", shapelib_methods);
-	if (!module) return;
+#endif
+	if (!module) PYSHAPELIB_INITRETURN(NULL);
 	
 	PYSHAPELIB_ADD_TYPE(SHPObjectType, "SHPObject");
 	PYSHAPELIB_ADD_TYPE(ShapeFileType, "ShapeFile");
@@ -835,5 +856,7 @@
 	PYSHAPELIB_ADD_CONSTANT(SHPP_INNERRING);
 	PYSHAPELIB_ADD_CONSTANT(SHPP_FIRSTRING);
 	PYSHAPELIB_ADD_CONSTANT(SHPP_RING);
+	
+	PYSHAPELIB_INITRETURN(module);
 }
 

Modified: trunk/pyshapelib/shptreemodule.c
===================================================================
--- trunk/pyshapelib/shptreemodule.c	2011-09-06 07:39:28 UTC (rev 2900)
+++ trunk/pyshapelib/shptreemodule.c	2011-12-26 23:12:02 UTC (rev 2901)
@@ -8,16 +8,14 @@
 
 /* Python wrapper for the shapelib SHPTree */
 
-#include <Python.h>
+#include "pyshapelib_common.h"
 #include <shapefil.h>
 
-#include "pyshapelib_api.h"
-
 PyShapeLibAPI * api;
 
 typedef struct {
-    PyObject_HEAD
-    SHPTree * tree; 
+	PyObject_HEAD
+	SHPTree * tree; 
 } SHPTreeObject;
 
 extern PyTypeObject SHPTreeType;
@@ -28,148 +26,180 @@
 static PyObject *
 SHPTreeObject_FromSHPTree(SHPTree* tree)
 {
-    SHPTreeObject * self = PyObject_NEW(SHPTreeObject, &SHPTreeType);
-    if (!self)
-	return NULL;
+	SHPTreeObject * self = PyObject_NEW(SHPTreeObject, &SHPTreeType);
+	if (!self)
+		return NULL;
 
-    self->tree = tree;
+	self->tree = tree;
 
-    return (PyObject *)self;
+	return (PyObject *)self;
 }
 
 /* Deallocate the SHPTree wrapper. */
 static void
 shptree_dealloc(SHPTreeObject * self)
 {
-    api->SHPDestroyTree(self->tree);
-    PyObject_Del(self);
+	api->SHPDestroyTree(self->tree);
+	PyObject_Del(self);
 }
 
 /* Return the repr of the wrapper */
 static PyObject *
 shptree_repr(SHPTreeObject * self)
 {
-    char buf[1000];
-    sprintf(buf, "<SHPTree at %p>", self);
-    return PyString_FromString(buf);
+	return PYSHAPELIB_FROMFORMAT("<shptree.SHPTree object at %p>", self->tree);
 }
 
 static PyObject *
 shptree_find_shapes(SHPTreeObject * self, PyObject * args)
 {
-    double min[4] = {0, 0, 0, 0};
-    double max[4] = {0, 0, 0, 0};
-    int count, idx;
-    int * ids;
-    PyObject * list = NULL, *temp = NULL;
+	double min[4] = {0, 0, 0, 0};
+	double max[4] = {0, 0, 0, 0};
+	int count, idx;
+	int * ids;
+	PyObject * list = NULL, *temp = NULL;
 
-    if (!PyArg_ParseTuple(args, "(dd)(dd)", min + 0, min + 1,
+	if (!PyArg_ParseTuple(args, "(dd)(dd)", min + 0, min + 1,
 			  max + 0, max + 1))
 	return NULL;
 
-    ids = api->SHPTreeFindLikelyShapes(self->tree, min, max, &count);
+	ids = api->SHPTreeFindLikelyShapes(self->tree, min, max, &count);
 
-    list = PyList_New(count);
-    if (!list)
+	list = PyList_New(count);
+	if (!list)
 	goto fail;
 
-    /* Turn the returned array of indices into a python list of ints. */
-    for (idx = 0; idx < count; idx++)
-    {
-	temp = PyInt_FromLong(ids[idx]);
+	/* Turn the returned array of indices into a python list of ints. */
+	for (idx = 0; idx < count; idx++)
+	{
+	temp = PYSHAPELIB_FROMLONG(ids[idx]);
 	if (!temp)
-	    goto fail;
+		goto fail;
 
 	if (PyList_SetItem(list, idx, temp) == -1)
 	{
-	    /* temp's refcount has already be decreased. Set temp to
-	     * NULL so that the fail code doesn't do it again
-	     */
-	    temp = NULL;
-	    goto fail;
+		/* temp's refcount has already be decreased. Set temp to
+		 * NULL so that the fail code doesn't do it again
+		 */
+		temp = NULL;
+		goto fail;
 	}
-    }
+	}
 
-    free(ids);
-    return list;
-    
+	free(ids);
+	return list;
+	
  fail:
-    free(ids);
-    Py_XDECREF(list);
-    Py_XDECREF(temp);
-    return NULL;
+	free(ids);
+	Py_XDECREF(list);
+	Py_XDECREF(temp);
+	return NULL;
 }
 
 
 static struct PyMethodDef shptree_methods[] = {
-    {"find_shapes",	(PyCFunction)shptree_find_shapes,	METH_VARARGS},
-    {NULL,	NULL}
+	{"find_shapes",	(PyCFunction)shptree_find_shapes,	METH_VARARGS},
+	{NULL,	NULL}
 };
 
-static PyObject *
-shptree_getattr(PyObject * self, char * name)
+
+PyTypeObject SHPTreeType =
 {
-    return Py_FindMethod(shptree_methods, self, name);
-}
-
-
-PyTypeObject SHPTreeType = {
-	PyObject_HEAD_INIT(NULL)
-	0,
-	"SHPTree",
-	sizeof(SHPTreeObject),
-	0,
-	(destructor)shptree_dealloc,	/*tp_dealloc*/
-	(printfunc)NULL,		/*tp_print*/
-	shptree_getattr,		/*tp_getattr*/
-	0,				/*tp_setattr*/
-	0,				/*tp_compare*/
-	(reprfunc)shptree_repr,		/*tp_repr*/
-	0,				/*tp_as_number*/
-	0,				/*tp_as_sequence*/
-	0,				/*tp_as_mapping*/
-	0,				/*tp_hash*/
-        0,				/*tp_call*/
-        0,				/*tp_str*/
-	0,				/*tp_getattro*/
-	0,				/*tp_setattro*/
-	0,				/*tp_as_buffer*/
+	PYSHAPELIB_HEAD_INIT(NULL, 0)
+	"SHPTree",							/*tp_name*/
+	sizeof(SHPTreeObject),				/*tp_basicsize*/
+	0,									/*tp_itemsize*/
+	(destructor) shptree_dealloc,		/*tp_dealloc*/
+	0,									/*tp_print*/
+	0,									/*tp_getattr*/
+	0,									/*tp_setattr*/
+	0,									/*tp_compare*/
+	(reprfunc) shptree_repr,			/*tp_repr*/
+	0,									/*tp_as_number*/
+	0,									/*tp_as_sequence*/
+	0,									/*tp_as_mapping*/
+	0,									/*tp_hash */
+	0,									/*tp_call*/
+	0,									/*tp_str*/
+	0,									/*tp_getattro*/
+	0,									/*tp_setattro*/
+	0,									/*tp_as_buffer*/
+	Py_TPFLAGS_DEFAULT,					/*tp_flags*/
+	0,									/* tp_doc */
+	0,									/* tp_traverse */
+	0,									/* tp_clear */
+	0,									/* tp_richcompare */
+	0,									/* tp_weaklistoffset */
+	0,									/* tp_iter */
+	0,									/* tp_iternext */
+	shptree_methods,					/* tp_methods */
+	0,									/* tp_members */
+	0,									/* tp_getset */
+	0,									/* tp_base */
+	0,									/* tp_dict */
+	0,									/* tp_descr_get */
+	0,									/* tp_descr_set */
+	0,									/* tp_dictoffset */
+	0,									/* tp_init */
+	0,									/* tp_alloc */
+	0,									/* tp_new */
 };
 
 
-
 static PyObject *
 shptree_from_shapefile(PyObject * self, PyObject * args)
 {
-    SHPTree * tree;
-    SHPHandle handle;
-    PyObject * cobject;
-    int dimension, max_depth;
+	SHPTree * tree;
+	SHPHandle handle;
+	PyObject * cobject;
+	int dimension, max_depth;
 
-    if (!PyArg_ParseTuple(args, "O!ii", &PyCObject_Type, &cobject,
+	if (!PyArg_ParseTuple(args, "O!ii", &PYSHAPELIB_CAPSULE_TYPE, &cobject,
 			  &dimension, &max_depth))
 	return NULL;
 
-    handle = PyCObject_AsVoidPtr(cobject);
+	handle = PYSHAPELIB_ASVOID(cobject, NULL);
 
-    tree = api->SHPCreateTree(handle, dimension, max_depth, NULL, NULL);
+	tree = api->SHPCreateTree(handle, dimension, max_depth, NULL, NULL);
 
-    /* apparently SHPCreateTree doesn't do any error checking, so we
-     * have to assume that tree is valid at this point. */
-    return SHPTreeObject_FromSHPTree(tree);
+	/* apparently SHPCreateTree doesn't do any error checking, so we
+	 * have to assume that tree is valid at this point. */
+	return SHPTreeObject_FromSHPTree(tree);
 }
 
 
 static PyMethodDef module_functions[] = {
-    {"SHPTree",		shptree_from_shapefile,		METH_VARARGS},
-    { NULL, NULL }
+	{"SHPTree",		shptree_from_shapefile,		METH_VARARGS},
+	{ NULL, NULL }
 };
 
 
+#if PYSHAPELIB_IS_PY3K
+
+static struct PyModuleDef shptree_moduledef = {
+		PyModuleDef_HEAD_INIT,
+		"shptree",
+		NULL,
+		0,
+		shptree_methods,
+		NULL,
+		NULL,
+		NULL,
+		NULL
+};
+
+PyMODINIT_FUNC PyInit_shptree(void)
+#else
 PyMODINIT_FUNC initshptree(void)
+#endif
 {
-    SHPTreeType.ob_type = &PyType_Type;
+#if PYSHAPELIB_IS_PY3K
+	PyObject *module = PyModule_Create(&shptree_moduledef);
+#else
+	PyObject* module = Py_InitModule("shptree", shptree_methods);
+#endif
+	if (!module) PYSHAPELIB_INITRETURN(NULL);
 
-    Py_InitModule("shptree", module_functions);
-    PYSHAPELIB_IMPORT_API(api);
+	PYSHAPELIB_ADD_TYPE(SHPTreeType, "SHPTree");
+	PYSHAPELIB_IMPORT_API(api);
 }

Modified: trunk/pyshapelib/testdbf.py
===================================================================
--- trunk/pyshapelib/testdbf.py	2011-09-06 07:39:28 UTC (rev 2900)
+++ trunk/pyshapelib/testdbf.py	2011-12-26 23:12:02 UTC (rev 2901)
@@ -34,7 +34,7 @@
 		self.records = [
 			('Weatherwax', 1, 3.1415926535, True),
 			('Ogg', 2, -1000.1234, False),
-			(u'x\u03C0\u03C1\u03C2', 10, 0, 1),
+			('x\u03C0\u03C1\u03C2', 10, 0, 1),
 			]
 
 	def test_add_field(self):
@@ -84,10 +84,10 @@
 				self.__assertEqual(dbf.read_attribute(i, k), self.records[i][k])
 
 		# try to read complete records (they are returned as dictionaries)
-		keys = zip(*self.fields)[0]
+		keys = [f[0] for f in self.fields]
 		for i in range(dbf.record_count()):
 			rec = dbf.read_record(i)
-			self.assert_(isinstance(rec, dict))
+			self.assertTrue(isinstance(rec, dict))
 			for k, key in enumerate(keys):
 				self.__assertEqual(rec[key], self.records[i][k])
 		



More information about the Pyshapelib-commits mailing list