mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-02-18 10:00:31 +03:00
The Inet type knows how to adapt itself.
Implemented __conform__ as the Adaptation PEP suggests. It is not required for the type to be registered as adapter.
This commit is contained in:
parent
75a6f783c5
commit
575b2b5f77
|
@ -423,6 +423,10 @@ class Inet(object):
|
||||||
obj.prepare(self._conn)
|
obj.prepare(self._conn)
|
||||||
return obj.getquoted()+"::inet"
|
return obj.getquoted()+"::inet"
|
||||||
|
|
||||||
|
def __conform__(self, foo):
|
||||||
|
if foo is _ext.ISQLQuote:
|
||||||
|
return self
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return str(self.addr)
|
return str(self.addr)
|
||||||
|
|
||||||
|
@ -432,7 +436,6 @@ def register_inet(oid=None, conn_or_curs=None):
|
||||||
_ext.INET = _ext.new_type((oid, ), "INET",
|
_ext.INET = _ext.new_type((oid, ), "INET",
|
||||||
lambda data, cursor: data and Inet(data) or None)
|
lambda data, cursor: data and Inet(data) or None)
|
||||||
_ext.register_type(_ext.INET, conn_or_curs)
|
_ext.register_type(_ext.INET, conn_or_curs)
|
||||||
_ext.register_adapter(Inet, lambda x: x)
|
|
||||||
return _ext.INET
|
return _ext.INET
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -79,6 +79,19 @@ class TypesExtrasTests(unittest.TestCase):
|
||||||
s = self.execute("SELECT NULL::inet AS foo")
|
s = self.execute("SELECT NULL::inet AS foo")
|
||||||
self.failUnless(s is None)
|
self.failUnless(s is None)
|
||||||
|
|
||||||
|
def test_inet_conform(self):
|
||||||
|
from psycopg2.extras import Inet
|
||||||
|
i = Inet("192.168.1.0/24")
|
||||||
|
a = psycopg2.extensions.adapt(i)
|
||||||
|
a.prepare(self.conn)
|
||||||
|
self.assertEqual("E'192.168.1.0/24'::inet", a.getquoted())
|
||||||
|
|
||||||
|
# adapts ok with unicode too
|
||||||
|
i = Inet(u"192.168.1.0/24")
|
||||||
|
a = psycopg2.extensions.adapt(i)
|
||||||
|
a.prepare(self.conn)
|
||||||
|
self.assertEqual("E'192.168.1.0/24'::inet", a.getquoted())
|
||||||
|
|
||||||
def test_adapt_fail(self):
|
def test_adapt_fail(self):
|
||||||
class Foo(object): pass
|
class Foo(object): pass
|
||||||
self.assertRaises(psycopg2.ProgrammingError,
|
self.assertRaises(psycopg2.ProgrammingError,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user