Re: Integration of Psycopg with XTA
От | Christian Ferrari |
---|---|
Тема | Re: Integration of Psycopg with XTA |
Дата | |
Msg-id | 1171000727.19526618.1537994198139@mail.yahoo.com обсуждение исходный текст |
Ответ на | Re: Integration of Psycopg with XTA (Daniele Varrazzo <daniele.varrazzo@gmail.com>) |
Ответы |
Re: Integration of Psycopg with XTA
(Federico Di Gregorio <fog@dndg.it>)
Re: Integration of Psycopg with XTA (Daniele Varrazzo <daniele.varrazzo@gmail.com>) |
Список | psycopg |
Dear Daniele,
thank you for providing the hack: it's a starting point to check integration feasibility.
Unfortunately it does not work in my environment:
Ubuntu LTS 14.04 64 bit
python 2.7.6
Here's the code I have copied from the link (with STRING.lower() patch), with some added debugging statements:
import sys
import psycopg2
def getpqconn(conn):
"""
Return the address of the libpq connection string from a psycopg connection
"""
from ctypes import string_at
from sys import getsizeof
from socket import ntohl, htonl
from binascii import hexlify
print "conn.server_version: ", conn.server_version
hver = "%08X" % ntohl(conn.server_version)
hver = hver.lower()
print "hver: ", hver
mem = hexlify(string_at(id(conn), getsizeof(conn)))
mem = mem.lower()
print "mem: ", mem
ver_off = mem.find(hver)
print "ver_off: ", ver_off
assert ver_off > 0
assert mem.find(hver, ver_off + 8) == -1, "there should be only one"
pqconn = htonl(int(hex[ver_off + 8:ver_off + 16], 16))
return pqconn
conn = psycopg2.connect("dbname=testdb")
print "conn: ", conn
pqconn = getpqconn(conn)
import psycopg2
def getpqconn(conn):
"""
Return the address of the libpq connection string from a psycopg connection
"""
from ctypes import string_at
from sys import getsizeof
from socket import ntohl, htonl
from binascii import hexlify
print "conn.server_version: ", conn.server_version
hver = "%08X" % ntohl(conn.server_version)
hver = hver.lower()
print "hver: ", hver
mem = hexlify(string_at(id(conn), getsizeof(conn)))
mem = mem.lower()
print "mem: ", mem
ver_off = mem.find(hver)
print "ver_off: ", ver_off
assert ver_off > 0
assert mem.find(hver, ver_off + 8) == -1, "there should be only one"
pqconn = htonl(int(hex[ver_off + 8:ver_off + 16], 16))
return pqconn
conn = psycopg2.connect("dbname=testdb")
print "conn: ", conn
pqconn = getpqconn(conn)
and this is the output:
conn: <connection object at 0x7f9cf76267c0; dsn: 'dbname=testdb', closed: 0>
conn.server_version: 90324
hver: d4600100
mem: 030000000000000060ef10f69c7f000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0eccf01000000000000000000000000207cd50100000000007cd501000000000000000000000000000000000000000001000000000000000000000000000000000000000000000003000000d4600100a0bad20100000000b0bed2010000000000000000000000000000000000000000986012f69c7f000000000000000000000000000000000000e08012f69c7f000010f99af19c7f000088fe9af19c7f0000000000000000000000000000000000000000000000000000010000000000000020e78f0000000000e500000000000000ffffffffffffffff
ver_off: 264
Traceback (most recent call last):
File "pqconn_hack.py", line 29, in <module>
pqconn = getpqconn(conn)
File "pqconn_hack.py", line 24, in getpqconn
pqconn = htonl(int(hex[ver_off + 8:ver_off + 16], 16))
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
I'm not a Python developer at all, but I see usage of htonl() function... if it has the same meaning of the C function it could be influenced by 32/64 architecture differences.conn.server_version: 90324
hver: d4600100
mem: 030000000000000060ef10f69c7f000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0eccf01000000000000000000000000207cd50100000000007cd501000000000000000000000000000000000000000001000000000000000000000000000000000000000000000003000000d4600100a0bad20100000000b0bed2010000000000000000000000000000000000000000986012f69c7f000000000000000000000000000000000000e08012f69c7f000010f99af19c7f000088fe9af19c7f0000000000000000000000000000000000000000000000000000010000000000000020e78f0000000000e500000000000000ffffffffffffffff
ver_off: 264
Traceback (most recent call last):
File "pqconn_hack.py", line 29, in <module>
pqconn = getpqconn(conn)
File "pqconn_hack.py", line 24, in getpqconn
pqconn = htonl(int(hex[ver_off + 8:ver_off + 16], 16))
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'
Do you have any suggestions?
What do you think about Federico's proposal to expose the pqconn pointer? Could ctypes "c_void_p" a viable option?
Thanks in advance
Ch.F.
On Tue, Sep 25, 2018 at 9:57 PM Christian Ferrari <camauz@yahoo.com> wrote:
> Stated that replacing "PGconn *" with "void *" in the API stack would not be an issue, the question is: what's the best way to retrieve something that can be transformed in a C pointer that could be passed to XTA constructor?
This is a hack I passed some time ago to an ukulele friend to get the
PGconn pointer out of the connection object:
<https://gist.github.com/dvarrazzo/b7c8f050bbd39dd2c104>. You may need
trasposing it for guitar though.
-- Daniele
> Stated that replacing "PGconn *" with "void *" in the API stack would not be an issue, the question is: what's the best way to retrieve something that can be transformed in a C pointer that could be passed to XTA constructor?
This is a hack I passed some time ago to an ukulele friend to get the
PGconn pointer out of the connection object:
<https://gist.github.com/dvarrazzo/b7c8f050bbd39dd2c104>. You may need
trasposing it for guitar though.
-- Daniele
В списке psycopg по дате отправления: