Seeing that there's a postgresql adapter written for PyPy [1], I decided to do 
a test-drive on a pet Django project. Had I not used GeoDjango, it would have 
probably worked right away. 


I didn't need to change much: Django's PostGIS database adapter uses a psycopg 
extension, psycopg2.extensions.ISQLQuote, which doesn't really provide 
anything useful. However, I'm guessing due to implementation difference, 
original psycopg calls `__conforms__` with extra parameter, while rpython 
implementation does not - that's why there's an `*args`.

The other problem was that CPython ctypes and PyPy ctypes implementations 
differ: where CPython uses `PTR._obj.value`, PyPy uses `PTR.contents.value`.


The attached patch made my project work both on CPython and PyPy.


Regards,
Gasper Zejn


[1] https://bitbucket.org/alex_gaynor/pypy-postgresql/overview
diff -r 6366a9c0a99a django/contrib/gis/db/backends/postgis/adapter.py
--- a/django/contrib/gis/db/backends/postgis/adapter.py	Mon Apr 18 20:56:22 2011 +0200
+++ b/django/contrib/gis/db/backends/postgis/adapter.py	Tue May 10 22:39:29 2011 +0200
@@ -3,7 +3,6 @@
 """
 
 from psycopg2 import Binary
-from psycopg2.extensions import ISQLQuote
 
 class PostGISAdapter(object):
     def __init__(self, geom):
@@ -13,12 +12,9 @@
         self.ewkb = str(geom.ewkb)
         self.srid = geom.srid
 
-    def __conform__(self, proto):
+    def __conform__(self, *args):
         # Does the given protocol conform to what Psycopg2 expects?
-        if proto == ISQLQuote:
-            return self
-        else:
-            raise Exception('Error implementing psycopg2 protocol. Is psycopg2 installed?')
+        return self
 
     def __eq__(self, other):
         return (self.ewkb == other.ewkb) and (self.srid == other.srid)
diff -r 6366a9c0a99a django/contrib/gis/gdal/prototypes/errcheck.py
--- a/django/contrib/gis/gdal/prototypes/errcheck.py	Mon Apr 18 20:56:22 2011 +0200
+++ b/django/contrib/gis/gdal/prototypes/errcheck.py	Tue May 10 22:39:29 2011 +0200
@@ -10,11 +10,15 @@
 # arguments passed in by reference. 
 def arg_byref(args, offset=-1):
     "Returns the pointer argument's by-refernece value."
-    return args[offset]._obj.value
+    if hasattr(args[offset], '_obj'):
+        return args[offset]._obj.value
+    return args[offset].contents.value
 
 def ptr_byref(args, offset=-1):
     "Returns the pointer argument passed in by-reference."
-    return args[offset]._obj
+    if hasattr(args[offset], '_obj'):
+        return args[offset]._obj
+    return args[offset].contents
 
 def check_bool(result, func, cargs):
     "Returns the boolean evaluation of the value."
@@ -123,5 +127,8 @@
     returns both the double and tring values.
     """
     dbl = result
-    ptr = cargs[-1]._obj
+    if hasattr(cargs[-1], '_obj'):
+        ptr = cargs[-1]._obj
+    else:
+        ptr = cargs[-1].contents
     return dbl, ptr.value
diff -r 6366a9c0a99a django/contrib/gis/geos/prototypes/errcheck.py
--- a/django/contrib/gis/geos/prototypes/errcheck.py	Mon Apr 18 20:56:22 2011 +0200
+++ b/django/contrib/gis/geos/prototypes/errcheck.py	Tue May 10 22:39:29 2011 +0200
@@ -28,7 +28,10 @@
 ### ctypes error checking routines ###
 def last_arg_byref(args):
     "Returns the last C argument's value by reference."
-    return args[-1]._obj.value
+    if hasattr(args[-1], '_obj'):
+        return args[-1]._obj.value
+    else:
+        return args[-1].contents.value
 
 def check_dbl(result, func, cargs):
     "Checks the status code and returns the double value passed in by reference."
_______________________________________________
pypy-dev mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-dev

Reply via email to