././@PaxHeader0000000000000000000000000000003300000000000011451 xustar000000000000000027 mtime=1636798860.416994 pg8000-1.23.0/0000775000175000017500000000000000000000000013302 5ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/LICENSE0000664000175000017500000000263600000000000014316 0ustar00tlocketlocke00000000000000Copyright (c) 2007-2009, Mathieu Fenniak All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/MANIFEST.in0000664000175000017500000000017100000000000015037 0ustar00tlocketlocke00000000000000include README.adoc include versioneer.py include pg8000/_version.py include LICENSE graft test global-exclude *.py[cod] ././@PaxHeader0000000000000000000000000000003300000000000011451 xustar000000000000000027 mtime=1636798860.416994 pg8000-1.23.0/PKG-INFO0000664000175000017500000000336300000000000014404 0ustar00tlocketlocke00000000000000Metadata-Version: 1.2 Name: pg8000 Version: 1.23.0 Summary: PostgreSQL interface library Home-page: https://github.com/tlocke/pg8000 Author: Mathieu Fenniak Author-email: biziqe@mathieu.fenniak.net License: BSD Description: pg8000 ------ pg8000 is a Pure-Python interface to the PostgreSQL database engine. It is one of many PostgreSQL interfaces for the Python programming language. pg8000 is somewhat distinctive in that it is written entirely in Python and does not rely on any external libraries (such as a compiled python module, or PostgreSQL's libpq library). pg8000 supports the standard Python DB-API version 2.0. pg8000's name comes from the belief that it is probably about the 8000th PostgreSQL interface for Python. Keywords: postgresql dbapi Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.6 Classifier: Programming Language :: Python :: 3.7 Classifier: Programming Language :: Python :: 3.8 Classifier: Programming Language :: Python :: 3.9 Classifier: Programming Language :: Python :: 3.10 Classifier: Programming Language :: Python :: Implementation Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: Jython Classifier: Programming Language :: Python :: Implementation :: PyPy Classifier: Operating System :: OS Independent Classifier: Topic :: Database :: Front-Ends Classifier: Topic :: Software Development :: Libraries :: Python Modules Requires-Python: >=3.6 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636798770.0 pg8000-1.23.0/README.adoc0000664000175000017500000030170700000000000015077 0ustar00tlocketlocke00000000000000= pg8000 :toc: preamble pg8000 is a pure-link:http://www.python.org/[Python] http://www.postgresql.org/[PostgreSQL] driver that complies with http://www.python.org/dev/peps/pep-0249/[DB-API 2.0]. It is tested on Python versions 3.6+, on CPython and PyPy, and PostgreSQL versions 9.6+. pg8000's name comes from the belief that it is probably about the 8000th PostgreSQL interface for Python. pg8000 is distributed under the BSD 3-clause license. All bug reports, feature requests and contributions are welcome at http://github.com/tlocke/pg8000/. image::https://github.com/tlocke/pg8000/workflows/pg8000/badge.svg[Build Status] == Installation To install pg8000 using `pip` type: `pip install pg8000` == Native API Interactive Examples pg8000 comes with two APIs, the native pg8000 API and the DB-API 2.0 standard API. These are the examples for the native API, and the DB-API 2.0 examples follow in the next section. === Basic Example Import pg8000, connect to the database, create a table, add some rows and then query the table: [source,python] ---- >>> import pg8000.native >>> >>> # Connect to the database with user name postgres >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> # Create a temporary table >>> >>> con.run("CREATE TEMPORARY TABLE book (id SERIAL, title TEXT)") >>> >>> # Populate the table >>> >>> for title in ("Ender's Game", "The Magus"): ... con.run("INSERT INTO book (title) VALUES (:title)", title=title) >>> >>> # Print all the rows in the table >>> >>> for row in con.run("SELECT * FROM book"): ... print(row) [1, "Ender's Game"] [2, 'The Magus'] ---- === Transactions Here's how to run groups of SQL statements in a https://www.postgresql.org/docs/current/tutorial-transactions.html[transaction]: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("START TRANSACTION") >>> >>> # Create a temporary table >>> con.run("CREATE TEMPORARY TABLE book (id SERIAL, title TEXT)") >>> >>> for title in ("Ender's Game", "The Magus", "Phineas Finn"): ... con.run("INSERT INTO book (title) VALUES (:title)", title=title) >>> con.run("COMMIT") >>> for row in con.run("SELECT * FROM book"): ... print(row) [1, "Ender's Game"] [2, 'The Magus'] [3, 'Phineas Finn'] ---- rolling back a transaction: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> # Create a temporary table >>> con.run("CREATE TEMPORARY TABLE book (id SERIAL, title TEXT)") >>> >>> for title in ("Ender's Game", "The Magus", "Phineas Finn"): ... con.run("INSERT INTO book (title) VALUES (:title)", title=title) >>> >>> con.run("START TRANSACTION") >>> con.run("DELETE FROM book WHERE title = :title", title="Phineas Finn") >>> con.run("ROLLBACK") >>> for row in con.run("SELECT * FROM book"): ... print(row) [1, "Ender's Game"] [2, 'The Magus'] [3, 'Phineas Finn'] ---- === Query Using Fuctions Another query, using some PostgreSQL functions: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("SELECT TO_CHAR(TIMESTAMP '2021-10-10', 'YYYY BC')") [['2021 AD']] ---- === Interval Type A query that returns the PostgreSQL interval type: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> import datetime >>> >>> ts = datetime.date(1980, 4, 27) >>> con.run("SELECT timestamp '2013-12-01 16:06' - :ts", ts=ts) [[datetime.timedelta(days=12271, seconds=57960)]] ---- === Point Type A round-trip with a https://www.postgresql.org/docs/current/datatype-geometric.html[PostgreSQL point] type: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("SELECT CAST(:pt as point)", pt='(2.3,1)') [['(2.3,1)']] ---- === Client Encoding When communicating with the server, pg8000 uses the character set that the server asks it to use (the client encoding). By default the client encoding is the database's character set (chosen when the database is created), but the client encoding can be changed in a number of ways (eg. setting CLIENT_ENCODING in postgresql.conf). Another way of changing the client encoding is by using an SQL command. For example: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("SET CLIENT_ENCODING TO 'UTF8'") >>> con.run("SHOW CLIENT_ENCODING") [['UTF8']] ---- === JSON https://www.postgresql.org/docs/current/datatype-json.html[JSON] always comes back from the server de-serialized. If the JSON you want to send is a `dict` then you can just do: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> val = {'name': 'Apollo 11 Cave', 'zebra': True, 'age': 26.003} >>> con.run("SELECT CAST(:apollo as jsonb)", apollo=val) [[{'age': 26.003, 'name': 'Apollo 11 Cave', 'zebra': True}]] ---- JSON can always be sent in serialized form to the server: [source,python] ---- >>> import json >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> >>> val = ['Apollo 11 Cave', True, 26.003] >>> con.run("SELECT CAST(:apollo as jsonb)", apollo=json.dumps(val)) [[['Apollo 11 Cave', True, 26.003]]] ---- === Retrieve Column Metadata From Results Find the column metadata returned from a query: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("create temporary table quark (id serial, name text)") >>> for name in ('Up', 'Down'): ... con.run("INSERT INTO quark (name) VALUES (:name)", name=name) >>> # Now execute the query >>> >>> con.run("SELECT * FROM quark") [[1, 'Up'], [2, 'Down']] >>> >>> # and retried the metadata >>> >>> con.columns [{'table_oid': ..., 'column_attrnum': 1, 'type_oid': 23, 'type_size': 4, 'type_modifier': -1, 'format': 0, 'name': 'id'}, {'table_oid': ..., 'column_attrnum': 2, 'type_oid': 25, 'type_size': -1, 'type_modifier': -1, 'format': 0, 'name': 'name'}] >>> >>> # Show just the column names >>> >>> [c['name'] for c in con.columns] ['id', 'name'] ---- === Notices And Notifications PostgreSQL https://www.postgresql.org/docs/current/static/plpgsql-errors-and-messages.html[notices] are stored in a deque called `Connection.notices` and added using the `append()` method. Similarly there are `Connection.notifications` for https://www.postgresql.org/docs/current/static/sql-notify.html[notifications] and `Connection.parameter_statuses` for changes to the server configuration. Here's an example: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("LISTEN aliens_landed") >>> con.run("NOTIFY aliens_landed") >>> # A notification is a tuple containing (backend_pid, channel, payload) >>> >>> con.notifications[0] (..., 'aliens_landed', '') ---- === LIMIT ALL You might think that the following would work, but in fact it fails: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("SELECT 'silo 1' LIMIT :lim", lim='ALL') Traceback (most recent call last): pg8000.exceptions.DatabaseError: ... ---- Instead the https://www.postgresql.org/docs/current/sql-select.html[docs say] that you can send `null` as an alternative to `ALL`, which does work: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("SELECT 'silo 1' LIMIT :lim", lim=None) [['silo 1']] ---- === IN and NOT IN You might think that the following would work, but in fact the server doesn't like it: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("SELECT 'silo 1' WHERE 'a' IN :v", v=('a', 'b')) Traceback (most recent call last): pg8000.exceptions.DatabaseError: ... ---- instead you can write it using the https://www.postgresql.org/docs/current/functions-array.html[`unnest`] function: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run( ... "SELECT 'silo 1' WHERE 'a' IN (SELECT unnest(CAST(:v as varchar[])))", ... v=('a', 'b')) [['silo 1']] ---- and you can do the same for `NOT IN`. === Many SQL Statements Can't Be Parameterized In PostgreSQL parameters can only be used for https://www.postgresql.org/docs/current/xfunc-sql.html#XFUNC-SQL-FUNCTION-ARGUMENTS[data values, not identifiers]. Sometimes this might not work as expected, for example the following fails: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("CREATE USER juan WITH PASSWORD :password", password='quail') Traceback (most recent call last): pg8000.exceptions.DatabaseError: ... ---- It fails because the PostgreSQL server doesn't allow this statement to have any parameters. There are many SQL statements that one might think would have parameters, but don't. === COPY from and to a file The SQL https://www.postgresql.org/docs/current/sql-copy.html[COPY] statement can be used to copy from and to a file or file-like object. Here' an example using the CSV format: [source,python] ---- >>> import pg8000.native >>> from io import StringIO >>> import csv >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> # Create a CSV file in memory >>> >>> stream_in = StringIO() >>> csv_writer = csv.writer(stream_in) >>> csv_writer.writerow([1, "electron"]) 12 >>> csv_writer.writerow([2, "muon"]) 8 >>> csv_writer.writerow([3, "tau"]) 7 >>> stream_in.seek(0) 0 >>> >>> # Create a table and then copy the CSV into it >>> >>> con.run("CREATE TEMPORARY TABLE lepton (id SERIAL, name TEXT)") >>> con.run("COPY lepton FROM STDIN WITH (FORMAT CSV)", stream=stream_in) >>> >>> # COPY from a table to a stream >>> >>> stream_out = StringIO() >>> con.run("COPY lepton TO STDOUT WITH (FORMAT CSV)", stream=stream_out) >>> stream_out.seek(0) 0 >>> for row in csv.reader(stream_out): ... print(row) ['1', 'electron'] ['2', 'muon'] ['3', 'tau'] ---- === Execute Multiple SQL Statements If you want to execute a series of SQL statements (eg. an `.sql` file), you can run them as expected: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> statements = "SELECT 5; SELECT 'Erich Fromm';" >>> >>> con.run(statements) [[5], ['Erich Fromm']] ---- The only caveat is that when executing multiple statements you can't have any parameters. === Quoted Identifiers in SQL Say you had a column called `My Column`. Since it's case sensitive and contains a space, you'd have to https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERSdouble[surround it by double quotes]. But you can't do: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("select 'hello' as "My Column"") Traceback (most recent call last): SyntaxError: invalid syntax... ---- since Python uses double quotes to delimit string literals, so one solution is to use Python's https://docs.python.org/3/tutorial/introduction.html#strings[triple quotes] to delimit the string instead: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run('''select 'hello' as "My Column"''') [['hello']] ---- === Custom adapter from a Python type to a PostgreSQL type pg8000 has a mapping from Python types to PostgreSQL types for when it needs to send SQL parameters to the server. The default mapping that comes with pg8000 is designed to work well in most cases, but you might want to add or replace the default mapping. A Python `datetime.timedelta` object is sent to the server as a PostgreSQL `interval` type, which has the `oid` 1186. But let's say we wanted to create our own Python class to be sent as an `interval` type. Then we'd have to register an adapter: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> class MyInterval(str): ... pass >>> >>> def my_interval_out(my_interval): ... return my_interval # Must return a str >>> >>> con.register_out_adapter(MyInterval, my_interval_out) >>> con.run("SELECT CAST(:interval as interval)", interval=MyInterval("2 hours")) [[datetime.timedelta(seconds=7200)]] ---- Note that it still came back as a `datetime.timedelta` object because we only changed the mapping from Python to PostgreSQL. See below for an example of how to change the mapping from PostgreSQL to Python. === Custom adapter from a PostgreSQL type to a Python type pg8000 has a mapping from PostgreSQL types to Python types for when it receives SQL results from the server. The default mapping that comes with pg8000 is designed to work well in most cases, but you might want to add or replace the default mapping. If pg800 recieves PostgreSQL `interval` type, which has the `oid` 1186, it converts it into a Python `datetime.timedelta` object. But let's say we wanted to create our own Python class to be used instead of `datetime.timedelta`. Then we'd have to register an adapter: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> class MyInterval(str): ... pass >>> >>> def my_interval_in(my_interval_str): # The parameter is of type str ... return MyInterval(my_interval) >>> >>> con.register_in_adapter(1186, my_interval_in) >>> con.run("SELECT \'2 years'") [['2 years']] ---- Note that registering the 'in' adapter only afects the mapping from the PostgreSQL type to the Python type. See above for an example of how to change the mapping from PostgreSQL to Python. === Could Not Determine Data Type Of Parameter Sometimes you'll get the 'could not determine data type of parameter' error message from the server: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("SELECT :v IS NULL", v=None) Traceback (most recent call last): pg8000.exceptions.DatabaseError: {'S': 'ERROR', 'V': 'ERROR', 'C': '42P18', 'M': 'could not determine data type of parameter $1', 'F': 'postgres.c', 'L': '...', 'R': 'exec_parse_message'} ---- One way of solving it is to put a `cast` in the SQL: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("SELECT cast(:v as TIMESTAMP) IS NULL", v=None) [[True]] ---- Another way is to override the type that pg8000 sends along with each parameter: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> con.run("SELECT :v IS NULL", v=None, types={'v': pg8000.native.TIMESTAMP}) [[True]] ---- === Prepared Statements https://www.postgresql.org/docs/current/sql-prepare.html[Prepared statements] can be useful in improving performance when you have a statement that's executed repeatedly. Here's an example: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection("postgres", password="cpsnow") >>> >>> # Create the prepared statement >>> ps = con.prepare("SELECT cast(:v as varchar)") >>> >>> # Exceute the statement repeatedly >>> ps.run(v="speedy") [['speedy']] >>> ps.run(v="rapid") [['rapid']] >>> ps.run(v="swift") [['swift']] >>> >>> # Close the prepared statement, releasing resources on the server >>> ps.close() ---- === Use Environment Variables As Connection Defaults You might want to use the current user as the database username for example: [source,python] ---- >>> import pg8000.native >>> import getpass >>> >>> # Connect to the database with current user name >>> username = getpass.getuser() >>> connection = pg8000.native.Connection(username, password="cpsnow") >>> >>> connection.run("SELECT 'pilau'") [['pilau']] ---- or perhaps you may want to use some of the same https://www.postgresql.org/docs/current/libpq-envars.html[environment variables that libpq uses]: [source,python] ---- >>> import pg8000.native >>> from os import environ >>> >>> username = environ.get('PGUSER', 'postgres') >>> password = environ.get('PGPASSWORD', 'cpsnow') >>> host = environ.get('PGHOST', 'localhost') >>> port = environ.get('PGPORT', '5432') >>> database = environ.get('PGDATABASE') >>> >>> connection = pg8000.native.Connection( ... username, password=password, host=host, port=port, database=database) >>> >>> connection.run("SELECT 'Mr Cairo'") [['Mr Cairo']] ---- It might be asked, why doesn't pg8000 have this behaviour built in? The thinking follows the second aphorism of https://www.python.org/dev/peps/pep-0020/[The Zen of Python]: [quote] Explicit is better than implicit. So we've taken the approach of only being able to set connection parameters using the `pg8000.native.Connection()` constructor. === Connect To PostgreSQL Over SSL To connect to the server using SSL defaults do: [source,python] ---- import pg8000.native connection = pg8000.native.Connection( username, password="cpsnow", ssl_context=True) connection.run("SELECT 'The game is afoot!'") ---- To connect over SSL with custom settings, set the `ssl_context` parameter to an https://docs.python.org/3/library/ssl.html#ssl.SSLContext[`ssl.SSLContext`] object: [source,python] ---- import pg8000.native import ssl ssl_context = ssl.SSLContext() ssl_context.verify_mode = ssl.CERT_REQUIRED ssl_context.load_verify_locations('root.pem') connection = pg8000.native.Connection( username, password="cpsnow", ssl_context=ssl_context) ---- It may be that your PostgreSQL server is behind an SSL proxy server in which case you can set a pg8000-specific attribute `ssl.SSLContext.request_ssl = False` which tells pg8000 to connect using an SSL socket, but not to request SSL from the PostgreSQL server: [source,python] ---- import pg8000.native import ssl ssl_context = ssl.SSLContext() ssl_context.request_ssl = False connection = pg8000.native.Connection( username, password="cpsnow", ssl_context=ssl_context) ---- === Server-Side Cursors You can use the SQL commands https://www.postgresql.org/docs/current/sql-declare.html[`DECLARE`], https://www.postgresql.org/docs/current/sql-fetch.html[`FETCH`], https://www.postgresql.org/docs/current/sql-move.html[`MOVE`] and https://www.postgresql.org/docs/current/sql-close.html[`CLOSE`] to manipulate server-side cursors. For example: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection(username, password="cpsnow") >>> con.run("START TRANSACTION") >>> con.run("DECLARE c SCROLL CURSOR FOR SELECT * FROM generate_series(1, 100)") >>> con.run("FETCH FORWARD 5 FROM c") [[1], [2], [3], [4], [5]] >>> con.run("MOVE FORWARD 50 FROM c") >>> con.run("FETCH BACKWARD 10 FROM c") [[54], [53], [52], [51], [50], [49], [48], [47], [46], [45]] >>> con.run("CLOSE c") >>> con.run("ROLLBACK") ---- === BLOBs (Binary Large Objects) There's a set of https://www.postgresql.org/docs/current/lo-funcs.html[SQL functions] for manipulating BLOBs. Here's an example: [source,python] ---- >>> import pg8000.native >>> >>> con = pg8000.native.Connection(username, password="cpsnow") >>> >>> # Create a BLOB and get its oid >>> data = b'hello' >>> res = con.run("SELECT lo_from_bytea(0, :data)", data=data) >>> oid = res[0][0] >>> >>> # Create a table and store the oid of the BLOB >>> con.run("CREATE TEMPORARY TABLE image (raster oid)") >>> >>> con.run("INSERT INTO image (raster) VALUES (:oid)", oid=oid) >>> # Retrieve the data using the oid >>> con.run("SELECT lo_get(:oid)", oid=oid) [[b'hello']] >>> >>> # Add some data to the end of the BLOB >>> more_data = b' all' >>> offset = len(data) >>> con.run( ... "SELECT lo_put(:oid, :offset, :data)", ... oid=oid, offset=offset, data=more_data) [['']] >>> con.run("SELECT lo_get(:oid)", oid=oid) [[b'hello all']] >>> >>> # Download a part of the data >>> con.run("SELECT lo_get(:oid, 6, 3)", oid=oid) [[b'all']] ---- == DB-API 2 Interactive Examples These examples stick to the DB-API 2.0 standard. === Basic Example Import pg8000, connect to the database, create a table, add some rows and then query the table: [source,python] ---- >>> import pg8000.dbapi >>> >>> conn = pg8000.dbapi.connect(user="postgres", password="cpsnow") >>> cursor = conn.cursor() >>> cursor.execute("CREATE TEMPORARY TABLE book (id SERIAL, title TEXT)") >>> cursor.execute( ... "INSERT INTO book (title) VALUES (%s), (%s) RETURNING id, title", ... ("Ender's Game", "Speaker for the Dead")) >>> results = cursor.fetchall() >>> for row in results: ... id, title = row ... print("id = %s, title = %s" % (id, title)) id = 1, title = Ender's Game id = 2, title = Speaker for the Dead >>> conn.commit() ---- === Query Using Fuctions Another query, using some PostgreSQL functions: [source,python] ---- >>> import pg8000.dbapi >>> >>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow") >>> cursor = con.cursor() >>> >>> cursor.execute("SELECT TO_CHAR(TIMESTAMP '2021-10-10', 'YYYY BC')") >>> cursor.fetchone() ['2021 AD'] ---- === Interval Type A query that returns the PostgreSQL interval type: [source,python] ---- >>> import datetime >>> import pg8000.dbapi >>> >>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow") >>> cursor = con.cursor() >>> >>> cursor.execute("SELECT timestamp '2013-12-01 16:06' - %s", ... (datetime.date(1980, 4, 27),)) >>> cursor.fetchone() [datetime.timedelta(days=12271, seconds=57960)] ---- === Point Type A round-trip with a https://www.postgresql.org/docs/current/datatype-geometric.html[PostgreSQL point] type: [source,python] ---- >>> import pg8000.dbapi >>> >>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow") >>> cursor = con.cursor() >>> >>> cursor.execute("SELECT cast(%s as point)", ('(2.3,1)',)) >>> cursor.fetchone() ['(2.3,1)'] ---- === Numeric Parameter Style pg8000 supports all the DB-API parameter styles. Here's an example of using the 'numeric' parameter style: [source,python] ---- >>> import pg8000.dbapi >>> >>> pg8000.dbapi.paramstyle = "numeric" >>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow") >>> cursor = con.cursor() >>> >>> cursor.execute("SELECT array_prepend(:1, CAST(:2 AS int[]))", (500, [1, 2, 3, 4],)) >>> cursor.fetchone() [[500, 1, 2, 3, 4]] >>> pg8000.dbapi.paramstyle = "format" >>> conn.rollback() ---- === Autocommit Following the DB-API specification, autocommit is off by default. It can be turned on by using the autocommit property of the connection. [source,python] ---- >>> import pg8000.dbapi >>> >>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow") >>> con.autocommit = True >>> >>> cur = con.cursor() >>> cur.execute("vacuum") >>> conn.autocommit = False >>> cur.close() ---- === Client Encoding When communicating with the server, pg8000 uses the character set that the server asks it to use (the client encoding). By default the client encoding is the database's character set (chosen when the database is created), but the client encoding can be changed in a number of ways (eg. setting CLIENT_ENCODING in postgresql.conf). Another way of changing the client encoding is by using an SQL command. For example: [source,python] ---- >>> import pg8000.dbapi >>> >>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow") >>> cur = con.cursor() >>> cur.execute("SET CLIENT_ENCODING TO 'UTF8'") >>> cur.execute("SHOW CLIENT_ENCODING") >>> cur.fetchone() ['UTF8'] >>> cur.close() ---- === JSON JSON is sent to the server serialized, and returned de-serialized. Here's an example: [source,python] ---- >>> import json >>> import pg8000.dbapi >>> >>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow") >>> cur = con.cursor() >>> val = ['Apollo 11 Cave', True, 26.003] >>> cur.execute("SELECT cast(%s as json)", (json.dumps(val),)) >>> cur.fetchone() [['Apollo 11 Cave', True, 26.003]] >>> cur.close() ---- === Retrieve Column Names From Results Use the columns names retrieved from a query: [source,python] ---- >>> import pg8000 >>> conn = pg8000.dbapi.connect(user="postgres", password="cpsnow") >>> c = conn.cursor() >>> c.execute("create temporary table quark (id serial, name text)") >>> c.executemany("INSERT INTO quark (name) VALUES (%s)", (("Up",), ("Down",))) >>> # >>> # Now retrieve the results >>> # >>> c.execute("select * from quark") >>> rows = c.fetchall() >>> keys = [k[0] for k in c.description] >>> results = [dict(zip(keys, row)) for row in rows] >>> assert results == [{'id': 1, 'name': 'Up'}, {'id': 2, 'name': 'Down'}] ---- === Notices PostgreSQL https://www.postgresql.org/docs/current/static/plpgsql-errors-and-messages.html[notices] are stored in a deque called `Connection.notices` and added using the `append()` method. Similarly there are `Connection.notifications` for https://www.postgresql.org/docs/current/static/sql-notify.html[notifications] and `Connection.parameter_statuses` for changes to the server configuration. Here's an example: [source,python] ---- >>> import pg8000.dbapi >>> >>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow") >>> cur = con.cursor() >>> cur.execute("LISTEN aliens_landed") >>> cur.execute("NOTIFY aliens_landed") >>> con.commit() >>> con.notifications[0][1] 'aliens_landed' ---- === COPY from and to a file The SQL https://www.postgresql.org/docs/current/sql-copy.html[COPY] statement can be used to copy from and to a file or file-like object: [source,python] ---- >>> from io import StringIO >>> import pg8000.dbapi >>> >>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow") >>> cur = con.cursor() >>> # >>> # COPY from a stream to a table >>> # >>> stream_in = StringIO('1\telectron\n2\tmuon\n3\ttau\n') >>> cur = conn.cursor() >>> cur.execute("create temporary table lepton (id serial, name text)") >>> cur.execute("COPY lepton FROM stdin", stream=stream_in) >>> # >>> # Now COPY from a table to a stream >>> # >>> stream_out = StringIO() >>> cur.execute("copy lepton to stdout", stream=stream_out) >>> stream_out.getvalue() '1\telectron\n2\tmuon\n3\ttau\n' ---- === Server-Side Cursors You can use the SQL commands https://www.postgresql.org/docs/current/sql-declare.html[`DECLARE`], https://www.postgresql.org/docs/current/sql-fetch.html[`FETCH`], https://www.postgresql.org/docs/current/sql-move.html[`MOVE`] and https://www.postgresql.org/docs/current/sql-close.html[`CLOSE`] to manipulate server-side cursors. For example: [source,python] ---- >>> import pg8000.dbapi >>> >>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow") >>> cur = con.cursor() >>> cur.execute("START TRANSACTION") >>> cur.execute( ... "DECLARE c SCROLL CURSOR FOR SELECT * FROM generate_series(1, 100)") >>> cur.execute("FETCH FORWARD 5 FROM c") >>> cur.fetchall() ([1], [2], [3], [4], [5]) >>> cur.execute("MOVE FORWARD 50 FROM c") >>> cur.execute("FETCH BACKWARD 10 FROM c") >>> cur.fetchall() ([54], [53], [52], [51], [50], [49], [48], [47], [46], [45]) >>> cur.execute("CLOSE c") >>> cur.execute("ROLLBACK") ---- === BLOBs (Binary Large Objects) There's a set of https://www.postgresql.org/docs/current/lo-funcs.html[SQL functions] for manipulating BLOBs. Here's an example: [source,python] ---- >>> import pg8000.dbapi >>> >>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow") >>> cur = con.cursor() >>> >>> # Create a BLOB and get its oid >>> data = b'hello' >>> cur = conn.cursor() >>> cur.execute("SELECT lo_from_bytea(0, %s)", [data]) >>> oid = cur.fetchone()[0] >>> >>> # Create a table and store the oid of the BLOB >>> cur.execute("CREATE TEMPORARY TABLE image (raster oid)") >>> cur.execute("INSERT INTO image (raster) VALUES (%s)", [oid]) >>> >>> # Retrieve the data using the oid >>> cur.execute("SELECT lo_get(%s)", [oid]) >>> cur.fetchall() ([b'hello'],) >>> >>> # Add some data to the end of the BLOB >>> more_data = b' all' >>> offset = len(data) >>> cur.execute("SELECT lo_put(%s, %s, %s)", [oid, offset, more_data]) >>> cur.execute("SELECT lo_get(%s)", [oid]) >>> cur.fetchall() ([b'hello all'],) >>> >>> # Download a part of the data >>> cur.execute("SELECT lo_get(%s, 6, 3)", [oid]) >>> cur.fetchall() ([b'all'],) ---- == Type Mapping The following table shows the default mapping between Python types and PostgreSQL types, and vice versa. If pg8000 doesn't recognize a type that it receives from PostgreSQL, it will return it as a `str` type. This is how pg8000 handles PostgreSQL `enum` and XML types. It's possible to change the default mapping using adapters (see the examples). .Python to PostgreSQL Type Mapping |=== | Python Type | PostgreSQL Type | Notes | bool | bool | | int | int4 | | str | text | | float | float8 | | decimal.Decimal | numeric | | bytes | bytea | | datetime.datetime (without tzinfo) | timestamp without timezone | See note below. | datetime.datetime (with tzinfo) | timestamp with timezone | See note below. | datetime.date | date | See note below. | datetime.time | time without time zone | | datetime.timedelta | interval | | None | NULL | | uuid.UUID | uuid | | ipaddress.IPv4Address | inet | | ipaddress.IPv6Address | inet | | ipaddress.IPv4Network | inet | | ipaddress.IPv6Network | inet | | int | xid | | list of int | INT4[] | | list of float | FLOAT8[] | | list of bool | BOOL[] | | list of str | TEXT[] | | int | int2vector | Only from PostgreSQL to Python | JSON | json, jsonb | The Python JSON is provided as a Python serialized string. Results returned as de-serialized JSON. |=== [[_theory_of_operation]] == Theory Of Operation {empty} + [quote, Jochen Liedtke, Liedtke's minimality principle] ____ A concept is tolerated inside the microkernel only if moving it outside the kernel, i.e., permitting competing implementations, would prevent the implementation of the system's required functionality. ____ pg8000 is designed to be used with one thread per connection. Pg8000 communicates with the database using the https://www.postgresql.org/docs/current/protocol.html[PostgreSQL Frontend/Backend Protocol] (FEBE). If a query has no parameters, pg8000 uses the 'simple query protocol'. If a query does have parameters, pg8000 uses the 'extended query protocol' with unnamed prepared statements. The steps for a query with parameters are: . Query comes in. . Send a PARSE message to the server to create an unnamed prepared statement. . Send a BIND message to run against the unnamed prepared statement, resulting in an unnamed portal on the server. . Send an EXECUTE message to read all the results from the portal. It's also possible to use named prepared statements. In which case the prepared statement persists on the server, and represented in pg8000 using a PreparedStatement object. This means that the PARSE step gets executed once up front, and then only the BIND and EXECUTE steps are repeated subsequently. There are a lot of PostgreSQL data types, but few primitive data types in Python. By default, pg8000 doesn't send PostgreSQL data type information in the PARSE step, in which case PostgreSQL assumes the types implied by the SQL statement. In some cases PostgreSQL can't work out a parameter type and so an https://www.postgresql.org/docs/current/static/sql-expressions.html#SQL-SYNTAX-TYPE-CASTS[explicit cast] can be used in the SQL. In the FEBE protocol, each query parameter can be sent to the server either as binary or text according to the format code. In pg8000 the parameters are always sent as text. * PostgreSQL has +/-infinity values for dates and timestamps, but Python does not. Pg8000 handles this by returning +/-infinity strings in results, and in parameters the strings +/- infinity can be used. * PostgreSQL dates/timestamps can have values outside the range of Python datetimes. These are handled using the underlying PostgreSQL storage method. I don't know of any users of pg8000 that use this feature, so get in touch if it affects you. * Occasionally, the network connection between pg8000 and the server may go down. If pg8000 encounters a network problem it'll raise an `InterfaceError` with an error message starting with `network error` and with the original exception set as the https://docs.python.org/3/reference/simple_stmts.html#the-raise-statement[cause]. == Native API Docs === pg8000.native.Connection(user, host='localhost', database=None, port=5432, password=None, source_address=None, unix_sock=None, ssl_context=None, timeout=None, tcp_keepalive=True, application_name=None, replication=None) Creates a connection to a PostgreSQL database. user:: The username to connect to the PostgreSQL server with. If your server character encoding is not `ascii` or `utf8`, then you need to provide `user` as bytes, eg. `'my_name'.encode('EUC-JP')`. host:: The hostname of the PostgreSQL server to connect with. Providing this parameter is necessary for TCP/IP connections. One of either `host` or `unix_sock` must be provided. The default is `localhost`. database:: The name of the database instance to connect with. If `None` then the PostgreSQL server will assume the database name is the same as the username. If your server character encoding is not `ascii` or `utf8`, then you need to provide `database` as bytes, eg. `'my_db'.encode('EUC-JP')`. port:: The TCP/IP port of the PostgreSQL server instance. This parameter defaults to `5432`, the registered common port of PostgreSQL TCP/IP servers. password:: The user password to connect to the server with. This parameter is optional; if omitted and the database server requests password-based authentication, the connection will fail to open. If this parameter is provided but not requested by the server, no error will occur. + + If your server character encoding is not `ascii` or `utf8`, then you need to provide `password` as bytes, eg. `'my_password'.encode('EUC-JP')`. source_address:: The source IP address which initiates the connection to the PostgreSQL server. The default is `None` which means that the operating system will choose the source address. unix_sock:: The path to the UNIX socket to access the database through, for example, `'/tmp/.s.PGSQL.5432'`. One of either `host` or `unix_sock` must be provided. ssl_context:: This governs SSL encryption for TCP/IP sockets. It can have three values: * `None`, meaning no SSL (the default) * `True`, means use SSL with an https://docs.python.org/3/library/ssl.html#ssl.SSLContext[`ssl.SSLContext`] created using https://docs.python.org/3/library/ssl.html#ssl.create_default_context[`ssl.create_default_context()`] * An instance of https://docs.python.org/3/library/ssl.html#ssl.SSLContext[`ssl.SSLContext`] which will be used to create the SSL connection. + + If your PostgreSQL server is behind an SSL proxy, you can set the pg8000-specific attribute `ssl.SSLContext.request_ssl = False`, which tells pg8000 to use an SSL socket, but not to request SSL from the PostgreSQL server. Note that this means you can't use SCRAM authentication with channel binding. timeout:: This is the time in seconds before the connection to the server will time out. The default is `None` which means no timeout. tcp_keepalive:: If `True` then use https://en.wikipedia.org/wiki/Keepalive#TCP_keepalive[TCP keepalive]. The default is `True`. application_name:: Sets the https://www.postgresql.org/docs/current/runtime-config-logging.html#GUC-APPLICATION-NAME[application_name]. If your server character encoding is not `ascii` or `utf8`, then you need to provide values as bytes, eg. `'my_application_name'.encode('EUC-JP')`. The default is `None` which means that the server will set the application name. replication:: Used to run in https://www.postgresql.org/docs/12/protocol-replication.html[streaming replication mode]. If your server character encoding is not `ascii` or `utf8`, then you need to provide values as bytes, eg. `'database'.encode('EUC-JP')`. === pg8000.native.Error Generic exception that is the base exception of the other error exceptions. === pg8000.native.InterfaceError For errors that originate within pg8000. === pg8000.native.DatabaseError For errors that originate from the server. === pg8000.native.Connection.notifications A deque of server-side https://www.postgresql.org/docs/current/sql-notify.html[notifications] received by this database connection (via the LISTEN / NOTIFY PostgreSQL commands). Each list item is a three-element tuple containing the PostgreSQL backend PID that issued the notify, the channel and the payload. === pg8000.native.Connection.notices A deque of server-side notices received by this database connection. === pg8000.native.Connection.parameter_statuses A deque of server-side parameter statuses received by this database connection. === pg8000.native.Connection.run(sql, stream=None, types=None, **kwargs) Executes an sql statement, and returns the results as a `list`. For example: `con.run("SELECT * FROM cities where population > :pop", pop=10000)` sql:: The SQL statement to execute. Parameter placeholders appear as a `:` followed by the parameter name. stream:: For use with the PostgreSQL http://www.postgresql.org/docs/current/static/sql-copy.html[COPY] command. For a `COPY FROM` the parameter must be a readable file-like object, and for `COPY TO` it must be writable. types:: A dictionary of oids. A key corresponds to a parameter. kwargs:: The parameters of the SQL statement. === pg8000.native.Connection.row_count This read-only attribute contains the number of rows that the last `run()` method produced (for query statements like `SELECT`) or affected (for modification statements like `UPDATE`. The value is -1 if: * No `run()` method has been performed yet. * There was no rowcount associated with the last `run()`. * Using a `SELECT` query statement on a PostgreSQL server older than version 9. * Using a `COPY` query statement on PostgreSQL server version 8.1 or older. === pg8000.native.Connection.columns A list of column metadata. Each item in the list is a dictionary with the following keys: * name * table_oid * column_attrnum * type_oid * type_size * type_modifier * format === pg8000.native.Connection.close() Closes the database connection. === pg8000.native.Connection.register_out_adapter(typ, oid, out_func) Register a type adapter for types going out from pg8000 to the server. typ:: The Python class that the adapter is for. oid:: The PostgreSQL type identifier found in the https://www.postgresql.org/docs/current/catalog-pg-type.html[pg_type system calalog]. out_func:: A function that takes the Python object and returns its string representation in the format that the server requires. === pg8000.native.Connection.register_in_adapter(oid, in_func) Register a type adapter for types coming in from the server to pg8000. oid:: The PostgreSQL type identifier found in the https://www.postgresql.org/docs/current/catalog-pg-type.html[pg_type system calalog]. in_func:: A function that takes the PostgreSQL string representation and returns a corresponding Python object. === pg8000.native.Connection.prepare(sql) Returns a PreparedStatement object which represents a https://www.postgresql.org/docs/current/sql-prepare.html[prepared statement] on the server. It can subsequently be repeatedly executed as shown in the <<_prepared_statements, example>>. sql:: The SQL statement to prepare. Parameter placeholders appear as a `:` followed by the parameter name. === pg8000.native.PreparedStatement A prepared statement object is returned by the `pg8000.native.Connection.prepare()` method of a connection. It has the following methods: === pg8000.native.PreparedStatement.run(**kwargs) Executes the prepared statement, and returns the results as a `tuple`. kwargs:: The parameters of the prepared statement. === pg8000.native.PreparedStatement.close() Closes the prepared statement, releasing the prepared statement held on the server. == DB-API 2 Docs === Properties ==== pg8000.dbapi.apilevel The DBAPI level supported, currently "2.0". This property is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ==== pg8000.dbapi.threadsafety Integer constant stating the level of thread safety the DBAPI interface supports. For pg8000, the threadsafety value is 1, meaning that threads may share the module but not connections. This property is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ==== pg8000.dbapi.paramstyle String property stating the type of parameter marker formatting expected by the interface. This value defaults to "format", in which parameters are marked in this format: "WHERE name=%s". This property is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. As an extension to the DBAPI specification, this value is not constant; it can be changed to any of the following values: qmark:: Question mark style, eg. `WHERE name=?` numeric:: Numeric positional style, eg. `WHERE name=:1` named:: Named style, eg. `WHERE name=:paramname` format:: printf format codes, eg. `WHERE name=%s` pyformat:: Python format codes, eg. `WHERE name=%(paramname)s` ==== pg8000.dbapi.STRING String type oid. ==== pg8000.dbapi.BINARY ==== pg8000.dbapi.NUMBER Numeric type oid. ==== pg8000.dbapi.DATETIME Timestamp type oid ==== pg8000.dbapi.ROWID ROWID type oid === Functions ==== pg8000.dbapi.connect(user, host='localhost', database=None, port=5432, password=None, source_address=None, unix_sock=None, ssl_context=None, timeout=None, tcp_keepalive=True, application_name=None, replication=None) Creates a connection to a PostgreSQL database. This property is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. user:: The username to connect to the PostgreSQL server with. If your server character encoding is not `ascii` or `utf8`, then you need to provide `user` as bytes, eg. `'my_name'.encode('EUC-JP')`. host:: The hostname of the PostgreSQL server to connect with. Providing this parameter is necessary for TCP/IP connections. One of either `host` or `unix_sock` must be provided. The default is `localhost`. database:: The name of the database instance to connect with. If `None` then the PostgreSQL server will assume the database name is the same as the username. If your server character encoding is not `ascii` or `utf8`, then you need to provide `database` as bytes, eg. `'my_db'.encode('EUC-JP')`. port:: The TCP/IP port of the PostgreSQL server instance. This parameter defaults to `5432`, the registered common port of PostgreSQL TCP/IP servers. password:: The user password to connect to the server with. This parameter is optional; if omitted and the database server requests password-based authentication, the connection will fail to open. If this parameter is provided but not requested by the server, no error will occur. + + If your server character encoding is not `ascii` or `utf8`, then you need to provide `password` as bytes, eg. `'my_password'.encode('EUC-JP')`. source_address:: The source IP address which initiates the connection to the PostgreSQL server. The default is `None` which means that the operating system will choose the source address. unix_sock:: The path to the UNIX socket to access the database through, for example, `'/tmp/.s.PGSQL.5432'`. One of either `host` or `unix_sock` must be provided. ssl_context:: This governs SSL encryption for TCP/IP sockets. It can have three values: * `None`, meaning no SSL (the default) * `True`, means use SSL with an https://docs.python.org/3/library/ssl.html#ssl.SSLContext[`ssl.SSLContext`] created using https://docs.python.org/3/library/ssl.html#ssl.create_default_context[`ssl.create_default_context()`] * An instance of https://docs.python.org/3/library/ssl.html#ssl.SSLContext[`ssl.SSLContext`] which will be used to create the SSL connection. + + If your PostgreSQL server is behind an SSL proxy, you can set the pg8000-specific attribute `ssl.SSLContext.request_ssl = False`, which tells pg8000 to use an SSL socket, but not to request SSL from the PostgreSQL server. Note that this means you can't use SCRAM authentication with channel binding. timeout:: This is the time in seconds before the connection to the server will time out. The default is `None` which means no timeout. tcp_keepalive:: If `True` then use https://en.wikipedia.org/wiki/Keepalive#TCP_keepalive[TCP keepalive]. The default is `True`. application_name:: Sets the https://www.postgresql.org/docs/current/runtime-config-logging.html#GUC-APPLICATION-NAME[application_name]. If your server character encoding is not `ascii` or `utf8`, then you need to provide values as bytes, eg. `'my_application_name'.encode('EUC-JP')`. The default is `None` which means that the server will set the application name. replication:: Used to run in https://www.postgresql.org/docs/12/protocol-replication.html[streaming replication mode]. If your server character encoding is not `ascii` or `utf8`, then you need to provide values as bytes, eg. `'database'.encode('EUC-JP')`. ==== pg8000.dbapi.Date(year, month, day) Constuct an object holding a date value. This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. Returns: `datetime.date` ==== pg8000.dbapi.Time(hour, minute, second) Construct an object holding a time value. This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. Returns: `datetime.time` ==== pg8000.dbapi.Timestamp(year, month, day, hour, minute, second) Construct an object holding a timestamp value. This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. Returns: `datetime.datetime` ==== pg8000.dbapi.DateFromTicks(ticks) Construct an object holding a date value from the given ticks value (number of seconds since the epoch). This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. Returns: `datetime.datetime` ==== pg8000.dbapi.TimeFromTicks(ticks) Construct an objet holding a time value from the given ticks value (number of seconds since the epoch). This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. Returns: `datetime.time` ==== pg8000.dbapi.TimestampFromTicks(ticks) Construct an object holding a timestamp value from the given ticks value (number of seconds since the epoch). This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. Returns: `datetime.datetime` ==== pg8000.dbapi.Binary(value) Construct an object holding binary data. This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. Returns: `bytes`. === Generic Exceptions Pg8000 uses the standard DBAPI 2.0 exception tree as "generic" exceptions. Generally, more specific exception types are raised; these specific exception types are derived from the generic exceptions. ==== pg8000.dbapi.Warning Generic exception raised for important database warnings like data truncations. This exception is not currently used by pg8000. This exception is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ==== pg8000.dbapi.Error Generic exception that is the base exception of all other error exceptions. This exception is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ==== pg8000.dbapi.InterfaceError Generic exception raised for errors that are related to the database interface rather than the database itself. For example, if the interface attempts to use an SSL connection but the server refuses, an InterfaceError will be raised. This exception is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ==== pg8000.dbapi.DatabaseError Generic exception raised for errors that are related to the database. This exception is currently never raised by pg8000. This exception is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ==== pg8000.dbapi.DataError Generic exception raised for errors that are due to problems with the processed data. This exception is not currently raised by pg8000. This exception is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ==== pg8000.dbapi.OperationalError Generic exception raised for errors that are related to the database's operation and not necessarily under the control of the programmer. This exception is currently never raised by pg8000. This exception is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ==== pg8000.dbapi.IntegrityError Generic exception raised when the relational integrity of the database is affected. This exception is not currently raised by pg8000. This exception is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ==== pg8000.dbapi.InternalError Generic exception raised when the database encounters an internal error. This is currently only raised when unexpected state occurs in the pg8000 interface itself, and is typically the result of a interface bug. This exception is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ==== pg8000.dbapi.ProgrammingError Generic exception raised for programming errors. For example, this exception is raised if more parameter fields are in a query string than there are available parameters. This exception is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ==== pg8000.dbapi.NotSupportedError Generic exception raised in case a method or database API was used which is not supported by the database. This exception is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. === Classes ==== pg8000.dbapi.Connection A connection object is returned by the `pg8000.connect()` function. It represents a single physical connection to a PostgreSQL database. ===== pg8000.dbapi.Connection.notifications A deque of server-side https://www.postgresql.org/docs/current/sql-notify.html[notifications] received by this database connection (via the LISTEN / NOTIFY PostgreSQL commands). Each list item is a three-element tuple containing the PostgreSQL backend PID that issued the notify, the channel and the payload. This attribute is not part of the DBAPI standard; it is a pg8000 extension. ===== pg8000.dbapi.Connection.notices A deque of server-side notices received by this database connection. This attribute is not part of the DBAPI standard; it is a pg8000 extension. ===== pg8000.dbapi.Connection.parameter_statuses A deque of server-side parameter statuses received by this database connection. This attribute is not part of the DBAPI standard; it is a pg8000 extension. ===== pg8000.dbapi.Connection.autocommit Following the DB-API specification, autocommit is off by default. It can be turned on by setting this boolean pg8000-specific autocommit property to True. New in version 1.9. ===== pg8000.dbapi.Connection.close() Closes the database connection. This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ===== pg8000.dbapi.Connection.cursor() Creates a `pg8000.Cursor` object bound to this connection. This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ===== pg8000.dbapi.Connection.rollback() Rolls back the current database transaction. This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ===== pg8000.dbapi.Connection.tpc_begin(xid) Begins a TPC transaction with the given transaction ID xid. This method should be called outside of a transaction (i.e. nothing may have executed since the last `commit()` or `rollback()`. Furthermore, it is an error to call `commit()` or `rollback()` within the TPC transaction. A `ProgrammingError` is raised, if the application calls `commit()` or `rollback()` during an active TPC transaction. This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ===== pg8000.dbapi.Connection.tpc_commit(xid=None) When called with no arguments, `tpc_commit()` commits a TPC transaction previously prepared with `tpc_prepare()`. If `tpc_commit()` is called prior to `tpc_prepare()`, a single phase commit is performed. A transaction manager may choose to do this if only a single resource is participating in the global transaction. When called with a transaction ID `xid`, the database commits the given transaction. If an invalid transaction ID is provided, a ProgrammingError will be raised. This form should be called outside of a transaction, and is intended for use in recovery. On return, the TPC transaction is ended. This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ===== pg8000.dbapi.Connection.tpc_prepare() Performs the first phase of a transaction started with .tpc_begin(). A ProgrammingError is be raised if this method is called outside of a TPC transaction. After calling `tpc_prepare()`, no statements can be executed until `tpc_commit()` or `tpc_rollback()` have been called. This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ===== pg8000.dbapi.Connection.tpc_recover() Returns a list of pending transaction IDs suitable for use with `tpc_commit(xid)` or `tpc_rollback(xid)` This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ===== pg8000.dbapi.Connection.tpc_rollback(xid=None) When called with no arguments, `tpc_rollback()` rolls back a TPC transaction. It may be called before or after `tpc_prepare()`. When called with a transaction ID xid, it rolls back the given transaction. If an invalid transaction ID is provided, a `ProgrammingError` is raised. This form should be called outside of a transaction, and is intended for use in recovery. On return, the TPC transaction is ended. This function is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ===== pg8000.dbapi.Connection.xid(format_id, global_transaction_id, branch_qualifier) Create a Transaction IDs (only global_transaction_id is used in pg) format_id and branch_qualifier are not used in postgres global_transaction_id may be any string identifier supported by postgres returns a tuple (format_id, global_transaction_id, branch_qualifier) ==== pg8000.dbapi.Cursor A cursor object is returned by the `pg8000.dbapi.Connection.cursor()` method of a connection. It has the following attributes and methods: ===== pg8000.dbapi.Cursor.arraysize This read/write attribute specifies the number of rows to fetch at a time with `pg8000.dbapi.Cursor.fetchmany()`. It defaults to 1. ===== pg8000.dbapi.Cursor.connection This read-only attribute contains a reference to the connection object (an instance of `pg8000.dbapi.Connection`) on which the cursor was created. This attribute is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ===== pg8000.dbapi.Cursor.rowcount This read-only attribute contains the number of rows that the last `execute()` or `executemany()` method produced (for query statements like `SELECT`) or affected (for modification statements like `UPDATE`. The value is -1 if: * No `execute()` or `executemany()` method has been performed yet on the cursor. * There was no rowcount associated with the last `execute()`. * At least one of the statements executed as part of an `executemany()` had no row count associated with it. * Using a `SELECT` query statement on a PostgreSQL server older than version 9. * Using a `COPY` query statement on PostgreSQL server version 8.1 or older. This attribute is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ===== pg8000.dbapi.Cursor.description This read-only attribute is a sequence of 7-item sequences. Each value contains information describing one result column. The 7 items returned for each column are (name, type_code, display_size, internal_size, precision, scale, null_ok). Only the first two values are provided by the current implementation. This attribute is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ===== pg8000.dbapi.Cursor.close() Closes the cursor. This method is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ===== pg8000.dbapi.Cursor.execute(operation, args=None, stream=None) Executes a database operation. Parameters may be provided as a sequence, or as a mapping, depending upon the value of `pg8000.paramstyle`. Returns the cursor, which may be iterated over. This method is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. operation:: The SQL statement to execute. args:: If `pg8000.dbapi.paramstyle` is `qmark`, `numeric`, or `format`, this argument should be an array of parameters to bind into the statement. If `pg8000.dbapi.paramstyle` is `named`, the argument should be a `dict` mapping of parameters. If `pg8000.dbapi.paramstyle' is `pyformat`, the argument value may be either an array or a mapping. stream:: This is a pg8000 extension for use with the PostgreSQL http://www.postgresql.org/docs/current/static/sql-copy.html[COPY] command. For a `COPY FROM` the parameter must be a readable file-like object, and for `COPY TO` it must be writable. New in version 1.9.11. ===== pg8000.dbapi.Cursor.executemany(operation, param_sets) Prepare a database operation, and then execute it against all parameter sequences or mappings provided. This method is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. operation:: The SQL statement to execute. parameter_sets:: A sequence of parameters to execute the statement with. The values in the sequence should be sequences or mappings of parameters, the same as the args argument of the `pg8000.dbapi.Cursor.execute()` method. ===== pg8000.dbapi.Cursor.callproc(procname, parameters=None) Call a stored database procedure with the given name and optional parameters. This method is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. procname:: The name of the procedure to call. parameters:: A list of parameters. ===== pg8000.dbapi.Cursor.fetchall() Fetches all remaining rows of a query result. This method is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. Returns: A sequence, each entry of which is a sequence of field values making up a row. ===== pg8000.dbapi.Cursor.fetchmany(size=None) Fetches the next set of rows of a query result. This method is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. size:: The number of rows to fetch when called. If not provided, the `pg8000.dbapi.Cursor.arraysize` attribute value is used instead. Returns: A sequence, each entry of which is a sequence of field values making up a row. If no more rows are available, an empty sequence will be returned. ===== pg8000.dbapi.Cursor.fetchone() Fetch the next row of a query result set. This method is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. Returns: A row as a sequence of field values, or `None` if no more rows are available. ===== pg8000.dbapi.Cursor.setinputsizes(*sizes) Used to set the parameter types of the next query. This is useful if it's difficult for pg8000 to work out the types from the parameters themselves (eg. for parameters of type None). sizes:: Positional parameters that are either the Python type of the parameter to be sent, or the PostgreSQL oid. Common oids are available as constants such as pg8000.STRING, pg8000.INTEGER, pg8000.TIME etc. This method is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification]. ===== pg8000.dbapi.Cursor.setoutputsize(size, column=None) This method is part of the http://www.python.org/dev/peps/pep-0249/[DBAPI 2.0 specification], however, it is not implemented by pg8000. ==== pg8000.dbapi.Interval An Interval represents a measurement of time. In PostgreSQL, an interval is defined in the measure of months, days, and microseconds; as such, the pg8000 interval type represents the same information. Note that values of the `pg8000.Interval.microseconds`, `pg8000.Interval.days`, and `pg8000.Interval.months` properties are independently measured and cannot be converted to each other. A month may be 28, 29, 30, or 31 days, and a day may occasionally be lengthened slightly by a leap second. ===== pg8000.dbapi.Interval.microseconds Measure of microseconds in the interval. The microseconds value is constrained to fit into a signed 64-bit integer. Any attempt to set a value too large or too small will result in an OverflowError being raised. ===== pg8000.dbapi.Interval.days Measure of days in the interval. The days value is constrained to fit into a signed 32-bit integer. Any attempt to set a value too large or too small will result in an OverflowError being raised. ===== pg8000.dbapi.Interval.months Measure of months in the interval. The months value is constrained to fit into a signed 32-bit integer. Any attempt to set a value too large or too small will result in an OverflowError being raised. == Tests * Install http://testrun.org/tox/latest/[tox]: `pip install tox` * Enable the PostgreSQL hstore extension by running the SQL command: `create extension hstore;` * Add a line to pg_hba.conf for the various authentication options: .... host pg8000_md5 all 127.0.0.1/32 md5 host pg8000_gss all 127.0.0.1/32 gss host pg8000_password all 127.0.0.1/32 password host pg8000_scram_sha_256 all 127.0.0.1/32 scram-sha-256 host all all 127.0.0.1/32 trust .... * Set password encryption to `scram-sha-256` in `postgresql.conf`: `password_encryption = 'scram-sha-256'` * Set the password for the postgres user: `ALTER USER postgresql WITH PASSWORD 'pw';` * Run `tox` from the `pg8000` directory: `tox` This will run the tests against the Python version of the virtual environment, on the machine, and the installed PostgreSQL version listening on port 5432, or the `PGPORT` environment variable if set. Benchmarks are run as part of the test suite at `tests/test_benchmarks.py`. == Doing A Release Of pg8000 Run `tox` to make sure all tests pass, then update the release notes, then do: .... git tag -a x.y.z -m "version x.y.z" rm -r build rm -r dist python setup.py sdist bdist_wheel --python-tag py3 for f in dist/*; do gpg --detach-sign -a $f; done twine upload dist/* .... == Release Notes === Version 1.23.0, 2021-11-13 * If a query has no parameters, then the query will no longer be parsed. Although there are performance benefits for doing this, the main reason is to avoid query rewriting, which can introduce errors. === Version 1.22.1, 2021-11-10 * Fix bug in PGInterval type where `str()` failed for a millennia value. === Version 1.22.0, 2021-10-13 * Rather than specifying the oids in the `Parse` step of the Postgres protocol, pg8000 now omits them, and so Postgres will use the oids it determines from the query. This makes the pg8000 code simplier and also it should also make the nuances of type matching more straightforward. === Version 1.21.3, 2021-10-10 * Legacy prepared statement fails if the result is null. Thanks to Carlos https://github.com/carlkid1499 for reporting this. * For the currency part of the pg8000 test suite, add `C.UTF8` as a supported `LANG`. === Version 1.21.2, 2021-09-14 * The `executemany()` method fails if the `param_sets` parameter is empty. Thanks to https://github.com/GKTheOne for reporting this. === Version 1.21.1, 2021-08-25 * Require Scramp version 1.4.1 or higher so that pg8000 can cope with SCRAM with channel binding with certificates using a `sha512` hash algorithm. === Version 1.21.0, 2021-07-31 * For some SQL statements the server doesn't send back a result set (note that no result set is different from a result set with zero rows). Previously we didn't distinguish between no results and zero rows, but now we do. For `pg8000.dbapi` it means that an exception is raised if `fetchall()` is called when no results have been returned, bringing pg8000 into line with the DBAPI 2 standard. For `pg8000.native` this means that `run()` returns None if there is no result. === Version 1.20.0, 2021-07-03 * Allow text stream as 'stream' parameter in run(). Previously we only allowed a bytes stream, but now the stream can be a text stream, in which case pg8000 handles the encodings. === Version 1.19.5, 2021-05-18 * Handle invalid character encodings in error strings from the server. === Version 1.19.4, 2021-05-03 * A FLUSH message should only be send after an extended-query message, but pg8000 was sending it at other times as well. This affected AWS RDS Proxy. === Version 1.19.3, 2021-04-24 * The type (oid) of integer arrays wasn't being detected correctly. It was only going by the first element, but it should look at all the items. That's fixed now. === Version 1.19.2, 2021-04-07 * In version 1.19.1 we tried to parse the PostgreSQL `MONEY` type to return a `Decimal` but since the format of `MONEY` is locale-dependent this is too difficult and unreliable and so now we revert to returning a `str`. === Version 1.19.1, 2021-04-03 * Fix bug where setinputsizes() was only used for the first parameter set of executemany(). * Support more PostgreSQL array types. === Version 1.19.0, 2021-03-28 * Network error exceptions are now wrapped in an `InterfaceError`, with the original exception as the cause. The error message for network errors allways start with the string `network error`. * Upgraded to version 1.3.0 of Scramp, which has better error handling. === Version 1.18.0, 2021-03-06 * The `pg8000.dbapi.Cursor.callproc()` method is now implemented. * SCRAM channel binding is now supported. That means SCRAM mechanisms ending in '-PLUS' such as SCRAM-SHA-256-PLUS are now supported when connecting to the server. * A custom attribute `ssl.SSLContext.request_ssl` can be set to `False` to tell pg8000 to connect using an SSL socket, but to not request SSL from the PostgreSQL server. This is useful if you're connecting to a PostgreSQL server that's behind an SSL proxy. === Version 1.17.0, 2021-01-30 * The API is now split in two, pg8000.native and pg8000.dbapi. The legacy API still exists in this release, but will be removed in another release. The idea is that pg8000.dbapi can stick strictly to the DB-API 2 specification, while pg8000.native can focus on useability without having to worry about compatibility with the DB-API standard. * The column name in `Connection.description` used to be returned as a `bytes` but now it's returned as a `str`. * Removed extra wrapper types PGJson, PGEnum etc. These were never properly documented and the problem they solve can be solved using CAST in the SQL or by using setinputsizes. === Version 1.16.6, 2020-10-10 * The column name in `Connection.description` used to be returned as a `bytes` but now it's returned as a `str`. * Removed extra wrapper types PGJson, PGEnum etc. These were never properly documented and the problem they solve can be solved using CAST in the SQL or by using setinputsizes. === Version 1.16.5, 2020-08-07 * The TPC method `Connection.tpc_prepare()` was broken. === Version 1.16.4, 2020-08-03 * Include the `payload` in the tuples in `Connection.notifications`. * More constants (eg. `DECIMAL` and `TEXT_ARRAY`) are now available for PostgreSQL types that are used in `setinputsizes()`. === Version 1.16.3, 2020-07-26 * If an unrecognized parameter is sent to `Cursor.setinputsizes()` use the `pg8000.UNKNOWN` type (705). * When communicating with a PostgreSQL server with version < 8.2.0, `FETCH` commands don't have a row count. * Include in the source distribution all necessary test files from the `test` directory in === Version 1.16.2, 2020-07-25 * Use the https://www.postgresql.org/docs/current/protocol-flow.html#id-1.10.5.7.4[simple query] cycle for queries that don't have parameters. This should give a performance improvement and also means that multiple statements can be executed in one go (as long as they don't have parameters) whereas previously the `sqlparse` had to be used. === Version 1.16.1, 2020-07-18 * Enable the `Cursor.setinputsizes()` method. Previously this method didn't do anything. It's an optional method of the DBAPI 2.0 specification. === Version 1.16.0, 2020-07-11 * This is a backwardly incompatible release of pg8000. * All data types are now sent as text rather than binary. * Using adapters, custom types can be plugged in to pg8000. * Previously, named prepared statements were used for all statements. Now unnamed prepared statements are used by default, and named prepared statements can be used explicitly by calling the Connection.prepare() method, which returns a PreparedStatement object. === Version 1.15.3, 2020-06-14 * For TCP connections (as opposed to Unix socket connections) the https://docs.python.org/3/library/socket.html#socket.create_connection[`socket.create_connection`] function is now used. This means pg8000 now works with IPv6 as well as IPv4. * Better error messages for failed connections. A 'cause' exception is now added to the top-level pg8000 exception, and the error message contains the details of what was being connected to (host, port etc.). === Version 1.15.2, 2020-04-16 * Added a new method `run()` to the connection, which lets you run queries directly without using a `Cursor`. It always uses the `named` parameter style, and the parameters are provided using keyword arguments. There are now two sets of interactive examples, one using the pg8000 extensions, and one using just DB-API features. * Better error message if certain parameters in the `connect()` function are of the wrong type. * The constructor of the `Connection` class now has the same signature as the `connect()` function, which makes it easier to use the `Connection` class directly if you want to. === Version 1.15.1, 2020-04-04 * Up to now the only supported way to create a new connection was to use the `connect()` function. However, some people are using the `Connect` class directly and this change makes it a bit easier to do that by making the class use a contructor which has the same signature as the `connect()` function. === Version 1.15.0, 2020-04-04 * Abandon the idea of arbitrary `init_params` in the connect() function. We now go back to having a fixed number of arguments. The argument `replication` has been added as this is the only extra init param that was needed. The reason for going back to a fixed number of aguments is that you get better feedback if you accidently mis-type a parameter name. * The `max_prepared_statements` parameter has been moved from being a module property to being an argument of the connect() function. === Version 1.14.1, 2020-03-23 * Ignore any `init_params` that have a value of `None`. This seems to be more useful and the behaviour is more expected. === Version 1.14.0, 2020-03-21 * Tests are now included in the source distribution. * Any extra keyword parameters of the `connect()` function are sent as initialization parameters when the PostgreSQL session starts. See the API docs for more information. Thanks to Patrick Hayes for suggesting this. * The ssl.wrap_socket function is deprecated, so we now give the user the option of using a default `SSLContext` or to pass in a custom one. This is a backwardly incompatible change. See the API docs for more info. Thanks to Jonathan Ross Rogers for his work on this. * Oversized integers are now returned as a `Decimal` type, whereas before a `None` was returned. Thanks to Igor Kaplounenko for his work on this. * Allow setting of connection source address in the `connect()` function. See the API docs for more details. Thanks to David King for his work on this. === Version 1.13.2, 2019-06-30 * Use the https://pypi.org/project/scramp/[Scramp] library for the SCRAM implementation. * Fixed bug where SQL such as `make_interval(days := 10)` fail on the `:=` part. Thanks to https://github.com/sanepal[sanepal] for reporting this. === Version 1.13.1, 2019-02-06 * We weren't correctly uploading releases to PyPI, which led to confusion when dropping Python 2 compatibility. Thanks to https://github.com/piroux[Pierre Roux] for his https://github.com/tlocke/pg8000/issues/7[detailed explanation] of what went wrong and how to correct it. * Fixed bug where references to the `six` library were still in the code, even though we don't use `six` anymore. === Version 1.13.0, 2019-02-01 * Remove support for Python 2. * Support the scram-sha-256 authentication protocol. Reading through the https://github.com/cagdass/scrampy code was a great help in implementing this, so thanks to https://github.com/cagdass[cagdass] for his code. === Version 1.12.4, 2019-01-05 * Support the PostgreSQL cast operator `::` in SQL statements. * Added support for more advanced SSL options. See docs on `connect` function for more details. * TCP keepalives enabled by default, can be set in the `connect` function. * Fixed bug in array dimension calculation. * Can now use the `with` keyword with connection objects. === Version 1.12.3, 2018-08-22 * Make PGVarchar and PGText inherit from `str`. Simpler than inheriting from a PGType. === Version 1.12.2, 2018-06-28 * Add PGVarchar and PGText wrapper types. This allows fine control over the string type that is sent to PostgreSQL by pg8000. === Version 1.12.1, 2018-06-12 * Revert back to the Python 3 `str` type being sent as an `unknown` type, rather than the `text` type as it was in the previous release. The reason is that with the `unknown` type there's the convenience of using a plain Python string for JSON, Enum etc. There's always the option of using the `pg8000.PGJson` and `pg8000.PGEnum` wrappers if precise control over the PostgreSQL type is needed. === Version 1.12.0, 2018-06-12 Note that this version is not backward compatible with previous versions. * The Python 3 `str` type was sent as an `unknown` type, but now it's sent as the nearest PostgreSQL type `text`. * pg8000 now recognizes that inline SQL comments end with a newline. * Single `%` characters now allowed in SQL comments. * The wrappers `pg8000.PGJson`, `pg8000.PGJsonb` and `pg8000.PGTsvector` can now be used to contain Python values to be used as parameters. The wrapper `pg8000.PGEnum` can by used for Python 2, as it doesn't have a standard `enum.Enum` type. === Version 1.11.0, 2017-08-16 Note that this version is not backward compatible with previous versions. * The Python `int` type was sent as an `unknown` type, but now it's sent as the nearest matching PostgreSQL type. Thanks to Patrick Hayes. * Prepared statements are now closed on the server when pg8000 clears them from its cache. * Previously a `%` within an SQL literal had to be escaped, but this is no longer the case. * Notifications, notices and parameter statuses are now handled by simple `dequeue` buffers. See docs for more details. * Connections and cursors are no longer threadsafe. So to be clear, neither connections or cursors should be shared between threads. One thread per connection is mandatory now. This has been done for performance reasons, and to simplify the code. * Rather than reading results from the server in batches, pg8000 now always downloads them in one go. This avoids `portal closed` errors and makes things a bit quicker, but now one has to avoid downloading too many rows in a single query. * Attempts to return something informative if the returned PostgreSQL timestamp value is outside the range of the Python datetime. * Allow empty arrays as parameters, assume they're of string type. * The cursor now has a context manager, so it can be used with the `with` keyword. Thanks to Ildar Musin. * Add support for `application_name` parameter when connecting to database, issue https://github.com/mfenniak/pg8000/pull/106[#106]. Thanks to https://github.com/vadv[@vadv] for the contribution. * Fix warnings from PostgreSQL "not in a transaction", when calling ``.rollback()`` while not in a transaction, issue https://github.com/mfenniak/pg8000/issues/113[#113]. Thanks to https://github.com/jamadden[@jamadden] for the contribution. * Errors from the server are now always passed through in full. === Version 1.10.6, 2016-06-10 * Fixed a problem where we weren't handling the password connection parameter correctly. Now it's handled in the same way as the 'user' and 'database' parameters, ie. if the password is bytes, then pass it straight through to the database, if it's a string then encode it with utf8. * It used to be that if the 'user' parameter to the connection function was 'None', then pg8000 would try and look at environment variables to find a username. Now we just go by the 'user' parameter only, and give an error if it's None. === Version 1.10.5, 2016-03-04 - Include LICENCE text and sources for docs in the source distribution (the tarball). === Version 1.10.4, 2016-02-27 * Fixed bug where if a str is sent as a query parameter, and then with the same cursor an int is sent instead of a string, for the same query, then it fails. * Under Python 2, a str type is now sent 'as is', ie. as a byte string rather than trying to decode and send according to the client encoding. Under Python 2 it's recommended to send text as unicode() objects. * Dropped and added support for Python versions. Now pg8000 supports Python 2.7+ and Python 3.3+. * Dropped and added support for PostgreSQL versions. Now pg8000 supports PostgreSQL 9.1+. * pg8000 uses the 'six' library for making the same code run on both Python 2 and Python 3. We used to include it as a file in the pg8000 source code. Now we have it as a separate dependency that's installed with 'pip install'. The reason for doing this is that package maintainers for OS distributions prefer unbundled libaries. === Version 1.10.3, 2016-01-07 * Removed testing for PostgreSQL 9.0 as it's not longer supported by the PostgreSQL Global Development Group. * Fixed bug where pg8000 would fail with datetimes if PostgreSQL was compiled with the integer_datetimes option set to 'off'. The bug was in the timestamp_send_float function. === Version 1.10.2, 2015-03-17 * If there's a socket exception thrown when communicating with the database, it is now wrapped in an OperationalError exception, to conform to the DB-API spec. * Previously, pg8000 didn't recognize the EmptyQueryResponse (that the server sends back if the SQL query is an empty string) now we raise a ProgrammingError exception. * Added socket timeout option for Python 3. * If the server returns an error, we used to initialize the ProgramerException with just the first three fields of the error. Now we initialize the ProgrammerException with all the fields. * Use relative imports inside package. * User and database names given as bytes. The user and database parameters of the connect() function are now passed directly as bytes to the server. If the type of the parameter is unicode, pg8000 converts it to bytes using the uft8 encoding. * Added support for JSON and JSONB Postgres types. We take the approach of taking serialized JSON (str) as an SQL parameter, but returning results as de-serialized JSON (Python objects). See the example in the Quickstart. * Added CircleCI continuous integration. * String support in arrays now allow letters like "u", braces and whitespace. === Version 1.10.1, 2014-09-15 * Add support for the Wheel package format. * Remove option to set a connection timeout. For communicating with the server, pg8000 uses a file-like object using socket.makefile() but you can't use this if the underlying socket has a timeout. === Version 1.10.0, 2014-08-30 * Remove the old ``pg8000.dbapi`` and ``pg8000.DBAPI`` namespaces. For example, now only ``pg8000.connect()`` will work, and ``pg8000.dbapi.connect()`` won't work any more. * Parse server version string with LooseVersion. This should solve the problems that people have been having when using versions of PostgreSQL such as ``9.4beta2``. * Message if portal suspended in autocommit. Give a proper error message if the portal is suspended while in autocommit mode. The error is that the portal is closed when the transaction is closed, and so in autocommit mode the portal will be immediately closed. The bottom line is, don't use autocommit mode if there's a chance of retrieving more rows than the cache holds (currently 100). === Version 1.9.14, 2014-08-02 * Make ``executemany()`` set ``rowcount``. Previously, ``executemany()`` would always set ``rowcount`` to -1. Now we set it to a meaningful value if possible. If any of the statements have a -1 ``rowcount`` then then the ``rowcount`` for the ``executemany()`` is -1, otherwise the ``executemany()`` ``rowcount`` is the sum of the rowcounts of the individual statements. * Support for password authentication. pg8000 didn't support plain text authentication, now it does. === Version 1.9.13, 2014-07-27 * Reverted to using the string ``connection is closed`` as the message of the exception that's thrown if a connection is closed. For a few versions we were using a slightly different one with capitalization and punctuation, but we've reverted to the original because it's easier for users of the library to consume. * Previously, ``tpc_recover()`` would start a transaction if one was not already in progress. Now it won't. === Version 1.9.12, 2014-07-22 * Fixed bug in ``tpc_commit()`` where a single phase commit failed. === Version 1.9.11, 2014-07-20 * Add support for two-phase commit DBAPI extension. Thanks to Mariano Reingart's TPC code on the Google Code version: https://code.google.com/p/pg8000/source/detail?r=c8609701b348b1812c418e2c7 on which the code for this commit is based. * Deprecate ``copy_from()`` and ``copy_to()`` The methods ``copy_from()`` and ``copy_to()`` of the ``Cursor`` object are deprecated because it's simpler and more flexible to use the ``execute()`` method with a ``fileobj`` parameter. * Fixed bug in reporting unsupported authentication codes. Thanks to https://github.com/hackgnar for reporting this and providing the fix. * Have a default for the ``user`` paramater of the ``connect()`` function. If the ``user`` parameter of the ``connect()`` function isn't provided, look first for the ``PGUSER`` then the ``USER`` environment variables. Thanks to Alex Gaynor https://github.com/alex for this suggestion. * Before PostgreSQL 8.2, ``COPY`` didn't give row count. Until PostgreSQL 8.2 (which includes Amazon Redshift which forked at 8.0) the ``COPY`` command didn't return a row count, but pg8000 thought it did. That's fixed now. === Version 1.9.10, 2014-06-08 * Remember prepared statements. Now prepared statements are never closed, and pg8000 remembers which ones are on the server, and uses them when a query is repeated. This gives an increase in performance, because on subsequent queries the prepared statement doesn't need to be created each time. * For performance reasons, pg8000 never closed portals explicitly, it just let the server close them at the end of the transaction. However, this can cause memory problems for long running transactions, so now pg800 always closes a portal after it's exhausted. * Fixed bug where unicode arrays failed under Python 2. Thanks to https://github.com/jdkx for reporting this. * A FLUSH message is now sent after every message (except SYNC). This is in accordance with the protocol docs, and ensures the server sends back its responses straight away. === Version 1.9.9, 2014-05-12 * The PostgreSQL interval type is now mapped to datetime.timedelta where possible. Previously the PostgreSQL interval type was always mapped to the pg8000.Interval type. However, to support the datetime.timedelta type we now use it whenever possible. Unfortunately it's not always possible because timedelta doesn't support months. If months are needed then the fall-back is the pg8000.Interval type. This approach means we handle timedelta in a similar way to other Python PostgreSQL drivers, and it makes pg8000 compatible with popular ORMs like SQLAlchemy. * Fixed bug in executemany() where a new prepared statement should be created for each variation in the oids of the parameter sets. === Version 1.9.8, 2014-05-05 * We used to ask the server for a description of the statement, and then ask for a description of each subsequent portal. We now only ask for a description of the statement. This results in a significant performance improvement, especially for executemany() calls and when using the 'use_cache' option of the connect() function. * Fixed warning in Python 3.4 which was saying that a socket hadn't been closed. It seems that closing a socket file doesn't close the underlying socket. * Now should cope with PostgreSQL 8 versions before 8.4. This includes Amazon Redshift. * Added 'unicode' alias for 'utf-8', which is needed for Amazon Redshift. * Various other bug fixes. === Version 1.9.7, 2014-03-26 * Caching of prepared statements. There's now a 'use_cache' boolean parameter for the connect() function, which causes all prepared statements to be cached by pg8000, keyed on the SQL query string. This should speed things up significantly in most cases. * Added support for the PostgreSQL inet type. It maps to the Python types IPv*Address and IPv*Network. * Added support for PostgreSQL +/- infinity date and timestamp values. Now the Python value datetime.datetime.max maps to the PostgreSQL value 'infinity' and datetime.datetime.min maps to '-infinity', and the same for datetime.date. * Added support for the PostgreSQL types int2vector and xid, which are mostly used internally by PostgreSQL. === Version 1.9.6, 2014-02-26 * Fixed a bug where 'portal does not exist' errors were being generated. Some queries that should have been run in a transaction were run in autocommit mode and so any that suspended a portal had the portal immediately closed, because a portal can only exist within a transaction. This has been solved by determining the transaction status from the READY_FOR_QUERY message. === Version 1.9.5, 2014-02-15 * Removed warn() calls for __next__() and __iter__(). Removing the warn() in __next__() improves the performance tests by ~20%. * Increased performance of timestamp by ~20%. Should also improve timestamptz. * Moved statement_number and portal_number from module to Connection. This should reduce lock contention for cases where there's a single module and lots of connections. * Make decimal_out/in and time_in use client_encoding. These functions used to assume ascii, and I can't think of a case where that wouldn't work. Nonetheless, that theoretical bug is now fixed. * Fixed a bug in cursor.executemany(), where a non-None parameter in a sequence of parameters, is None in a subsequent sequence of parameters. === Version 1.9.4, 2014-01-18 * Fixed a bug where with Python 2, a parameter with the value Decimal('12.44'), (and probably other numbers) isn't sent correctly to PostgreSQL, and so the command fails. This has been fixed by sending decimal types as text rather than binary. I'd imagine it's slightly faster too. === Version 1.9.3, 2014-01-16 * Fixed bug where there were missing trailing zeros after the decimal point in the NUMERIC type. For example, the NUMERIC value 1.0 was returned as 1 (with no zero after the decimal point). This is fixed this by making pg8000 use the text rather than binary representation for the numeric type. This actually doubles the speed of numeric queries. === Version 1.9.2, 2013-12-17 * Fixed incompatibility with PostgreSQL 8.4. In 8.4, the CommandComplete message doesn't return a row count if the command is SELECT. We now look at the server version and don't look for a row count for a SELECT with version 8.4. === Version 1.9.1, 2013-12-15 * Fixed bug where the Python 2 'unicode' type wasn't recognized in a query parameter. === Version 1.9.0, 2013-12-01 * For Python 3, the :class:`bytes` type replaces the :class:`pg8000.Bytea` type. For backward compatibility the :class:`pg8000.Bytea` still works under Python 3, but its use is deprecated. * A single codebase for Python 2 and 3. * Everything (functions, properties, classes) is now available under the ``pg8000`` namespace. So for example: * pg8000.DBAPI.connect() -> pg8000.connect() * pg8000.DBAPI.apilevel -> pg8000.apilevel * pg8000.DBAPI.threadsafety -> pg8000.threadsafety * pg8000.DBAPI.paramstyle -> pg8000.paramstyle * pg8000.types.Bytea -> pg8000.Bytea * pg8000.types.Interval -> pg8000.Interval * pg8000.errors.Warning -> pg8000.Warning * pg8000.errors.Error -> pg8000.Error * pg8000.errors.InterfaceError -> pg8000.InterfaceError * pg8000.errors.DatabaseError -> pg8000.DatabaseError The old locations are deprecated, but still work for backward compatibility. * Lots of performance improvements. * Faster receiving of ``numeric`` types. * Query only parsed when PreparedStatement is created. * PreparedStatement re-used in executemany() * Use ``collections.deque`` rather than ``list`` for the row cache. We're adding to one end and removing from the other. This is O(n) for a list but O(1) for a deque. * Find the conversion function and do the format code check in the ROW_DESCRIPTION handler, rather than every time in the ROW_DATA handler. * Use the 'unpack_from' form of struct, when unpacking the data row, so we don't have to slice the data. * Return row as a list for better performance. At the moment result rows are turned into a tuple before being returned. Returning the rows directly as a list speeds up the performance tests about 5%. * Simplify the event loop. Now the main event loop just continues until a READY_FOR_QUERY message is received. This follows the suggestion in the Postgres protocol docs. There's not much of a difference in speed, but the code is a bit simpler, and it should make things more robust. * Re-arrange the code as a state machine to give > 30% speedup. * Using pre-compiled struct objects. Pre-compiled struct objects are a bit faster than using the struct functions directly. It also hopefully adds to the readability of the code. * Speeded up _send. Before calling the socket 'write' method, we were checking that the 'data' type implements the 'buffer' interface (bytes or bytearray), but the check isn't needed because 'write' raises an exception if data is of the wrong type. * Add facility for turning auto-commit on. This follows the suggestion of funkybob to fix the problem of not be able to execute a command such as 'create database' that must be executed outside a transaction. Now you can do conn.autocommit = True and then execute 'create database'. * Add support for the PostgreSQL ``uid`` type. Thanks to Rad Cirskis. * Add support for the PostgreSQL XML type. * Add support for the PostgreSQL ``enum`` user defined types. * Fix a socket leak, where a problem opening a connection could leave a socket open. * Fix empty array issue. https://github.com/mfenniak/pg8000/issues/10 * Fix scale on ``numeric`` types. https://github.com/mfenniak/pg8000/pull/13 * Fix numeric_send. Thanks to Christian Hofstaedtler. === Version 1.08, 2010-06-08 * Removed usage of deprecated :mod:`md5` module, replaced with :mod:`hashlib`. Thanks to Gavin Sherry for the patch. * Start transactions on execute or executemany, rather than immediately at the end of previous transaction. Thanks to Ben Moran for the patch. * Add encoding lookups where needed, to address usage of SQL_ASCII encoding. Thanks to Benjamin Schweizer for the patch. * Remove record type cache SQL query on every new pg8000 connection. * Fix and test SSL connections. * Handle out-of-band messages during authentication. === Version 1.07, 2009-01-06 * Added support for :meth:`~pg8000.dbapi.CursorWrapper.copy_to` and :meth:`~pg8000.dbapi.CursorWrapper.copy_from` methods on cursor objects, to allow the usage of the PostgreSQL COPY queries. Thanks to Bob Ippolito for the original patch. * Added the :attr:`~pg8000.dbapi.ConnectionWrapper.notifies` and :attr:`~pg8000.dbapi.ConnectionWrapper.notifies_lock` attributes to DBAPI connection objects to provide access to server-side event notifications. Thanks again to Bob Ippolito for the original patch. * Improved performance using buffered socket I/O. * Added valid range checks for :class:`~pg8000.types.Interval` attributes. * Added binary transmission of :class:`~decimal.Decimal` values. This permits full support for NUMERIC[] types, both send and receive. * New `Sphinx `_-based website and documentation. === Version 1.06, 2008-12-09 * pg8000-py3: a branch of pg8000 fully supporting Python 3.0. * New Sphinx-based documentation. * Support for PostgreSQL array types -- INT2[], INT4[], INT8[], FLOAT[], DOUBLE[], BOOL[], and TEXT[]. New support permits both sending and receiving these values. * Limited support for receiving RECORD types. If a record type is received, it will be translated into a Python dict object. * Fixed potential threading bug where the socket lock could be lost during error handling. === Version 1.05, 2008-09-03 * Proper support for timestamptz field type: * Reading a timestamptz field results in a datetime.datetime instance that has a valid tzinfo property. tzinfo is always UTC. * Sending a datetime.datetime instance with a tzinfo value will be sent as a timestamptz type, with the appropriate tz conversions done. * Map postgres < -- > python text encodings correctly. * Fix bug where underscores were not permitted in pyformat names. * Support "%s" in a pyformat strin. * Add cursor.connection DB-API extension. * Add cursor.next and cursor.__iter__ DB-API extensions. * DBAPI documentation improvements. * Don't attempt rollback in cursor.execute if a ConnectionClosedError occurs. * Add warning for accessing exceptions as attributes on the connection object, as per DB-API spec. * Fix up open connection when an unexpected connection occurs, rather than leaving the connection in an unusable state. * Use setuptools/egg package format. === Version 1.04, 2008-05-12 * DBAPI 2.0 compatibility: * rowcount returns rows affected when appropriate (eg. UPDATE, DELETE) * Fix CursorWrapper.description to return a 7 element tuple, as per spec. * Fix CursorWrapper.rowcount when using executemany. * Fix CursorWrapper.fetchmany to return an empty sequence when no more results are available. * Add access to DBAPI exceptions through connection properties. * Raise exception on closing a closed connection. * Change DBAPI.STRING to varchar type. * rowcount returns -1 when appropriate. * DBAPI implementation now passes Stuart Bishop's Python DB API 2.0 Anal Compliance Unit Test. * Make interface.Cursor class use unnamed prepared statement that binds to parameter value types. This change increases the accuracy of PG's query plans by including parameter information, hence increasing performance in some scenarios. * Raise exception when reading from a cursor without a result set. * Fix bug where a parse error may have rendered a connection unusable. === Version 1.03, 2008-05-09 * Separate pg8000.py into multiple python modules within the pg8000 package. There should be no need for a client to change how pg8000 is imported. * Fix bug in row_description property when query has not been completed. * Fix bug in fetchmany dbapi method that did not properly deal with the end of result sets. * Add close methods to DB connections. * Add callback event handlers for server notices, notifications, and runtime configuration changes. * Add boolean type output. * Add date, time, and timestamp types in/out. * Add recognition of "SQL_ASCII" client encoding, which maps to Python's "ascii" encoding. * Add types.Interval class to represent PostgreSQL's interval data type, and appropriate wire send/receive methods. * Remove unused type conversion methods. === Version 1.02, 2007-03-13 * Add complete DB-API 2.0 interface. * Add basic SSL support via ssl connect bool. * Rewrite pg8000_test.py to use Python's unittest library. * Add bytea type support. * Add support for parameter output types: NULL value, timestamp value, python long value. * Add support for input parameter type oid. === Version 1.01, 2007-03-09 * Add support for writing floats and decimal objs up to PG backend. * Add new error handling code and tests to make sure connection can recover from a database error. * Fixed bug where timestamp types were not always returned in the same binary format from the PG backend. Text format is now being used to send timestamps. * Fixed bug where large packets from the server were not being read fully, due to socket.read not always returning full read size requested. It was a lazy-coding bug. * Added locks to make most of the library thread-safe. * Added UNIX socket support. === Version 1.00, 2007-03-08 * First public release. Although fully functional, this release is mostly lacking in production testing and in type support. ././@PaxHeader0000000000000000000000000000003300000000000011451 xustar000000000000000027 mtime=1636798860.416994 pg8000-1.23.0/pg8000/0000775000175000017500000000000000000000000014220 5ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/pg8000/__init__.py0000664000175000017500000001137500000000000016340 0ustar00tlocketlocke00000000000000from pg8000.legacy import ( BIGINTEGER, BINARY, BOOLEAN, BOOLEAN_ARRAY, BYTES, Binary, CHAR, CHAR_ARRAY, Connection, Cursor, DATE, DATETIME, DECIMAL, DECIMAL_ARRAY, DataError, DatabaseError, Date, DateFromTicks, Error, FLOAT, FLOAT_ARRAY, INET, INT2VECTOR, INTEGER, INTEGER_ARRAY, INTERVAL, IntegrityError, InterfaceError, InternalError, JSON, JSONB, MACADDR, NAME, NAME_ARRAY, NULLTYPE, NUMBER, NotSupportedError, OID, OperationalError, PGInterval, ProgrammingError, ROWID, STRING, TEXT, TEXT_ARRAY, TIME, TIMEDELTA, TIMESTAMP, TIMESTAMPTZ, Time, TimeFromTicks, Timestamp, TimestampFromTicks, UNKNOWN, UUID_TYPE, VARCHAR, VARCHAR_ARRAY, Warning, XID, pginterval_in, pginterval_out, timedelta_in, ) from ._version import get_versions __version__ = get_versions()["version"] del get_versions # Copyright (c) 2007-2009, Mathieu Fenniak # Copyright (c) The Contributors # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # * The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. __author__ = "Mathieu Fenniak" def connect( user, host="localhost", database=None, port=5432, password=None, source_address=None, unix_sock=None, ssl_context=None, timeout=None, tcp_keepalive=True, application_name=None, replication=None, ): return Connection( user, host=host, database=database, port=port, password=password, source_address=source_address, unix_sock=unix_sock, ssl_context=ssl_context, timeout=timeout, tcp_keepalive=tcp_keepalive, application_name=application_name, replication=replication, ) apilevel = "2.0" """The DBAPI level supported, currently "2.0". This property is part of the `DBAPI 2.0 specification `_. """ threadsafety = 1 """Integer constant stating the level of thread safety the DBAPI interface supports. This DBAPI module supports sharing of the module only. Connections and cursors my not be shared between threads. This gives pg8000 a threadsafety value of 1. This property is part of the `DBAPI 2.0 specification `_. """ paramstyle = "format" __all__ = [ "BIGINTEGER", "BINARY", "BOOLEAN", "BOOLEAN_ARRAY", "BYTES", "Binary", "CHAR", "CHAR_ARRAY", "Connection", "Cursor", "DATE", "DATETIME", "DECIMAL", "DECIMAL_ARRAY", "DataError", "DatabaseError", "Date", "DateFromTicks", "Error", "FLOAT", "FLOAT_ARRAY", "INET", "INT2VECTOR", "INTEGER", "INTEGER_ARRAY", "INTERVAL", "IntegrityError", "InterfaceError", "InternalError", "JSON", "JSONB", "MACADDR", "NAME", "NAME_ARRAY", "NULLTYPE", "NUMBER", "NotSupportedError", "OID", "OperationalError", "PGInterval", "ProgrammingError", "ROWID", "STRING", "TEXT", "TEXT_ARRAY", "TIME", "TIMEDELTA", "TIMESTAMP", "TIMESTAMPTZ", "Time", "TimeFromTicks", "Timestamp", "TimestampFromTicks", "UNKNOWN", "UUID_TYPE", "VARCHAR", "VARCHAR_ARRAY", "Warning", "XID", "connect", "pginterval_in", "pginterval_out", "timedelta_in", ] ././@PaxHeader0000000000000000000000000000003300000000000011451 xustar000000000000000027 mtime=1636798860.416994 pg8000-1.23.0/pg8000/_version.py0000664000175000017500000000076200000000000016423 0ustar00tlocketlocke00000000000000 # This file was generated by 'versioneer.py' (0.19) from # revision-control system data, or from the parent directory name of an # unpacked source archive. Distribution tarballs contain a pre-generated copy # of this file. import json version_json = ''' { "date": "2021-11-13T10:19:53+0000", "dirty": false, "error": null, "full-revisionid": "a83e85b4a3493efe4a53fdbe142d53306c1f5622", "version": "1.23.0" } ''' # END VERSION_JSON def get_versions(): return json.loads(version_json) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636570109.0 pg8000-1.23.0/pg8000/converters.py0000664000175000017500000003702600000000000016774 0ustar00tlocketlocke00000000000000from datetime import ( date as Date, datetime as Datetime, time as Time, timedelta as Timedelta, timezone as Timezone, ) from decimal import Decimal from enum import Enum from ipaddress import ( IPv4Address, IPv4Network, IPv6Address, IPv6Network, ip_address, ip_network, ) from json import dumps, loads from uuid import UUID from pg8000.exceptions import InterfaceError ANY_ARRAY = 2277 BIGINT = 20 BIGINT_ARRAY = 1016 BOOLEAN = 16 BOOLEAN_ARRAY = 1000 BYTES = 17 BYTES_ARRAY = 1001 CHAR = 1042 CHAR_ARRAY = 1014 CIDR = 650 CIDR_ARRAY = 651 CSTRING = 2275 CSTRING_ARRAY = 1263 DATE = 1082 DATE_ARRAY = 1182 FLOAT = 701 FLOAT_ARRAY = 1022 INET = 869 INET_ARRAY = 1041 INT2VECTOR = 22 INTEGER = 23 INTEGER_ARRAY = 1007 INTERVAL = 1186 INTERVAL_ARRAY = 1187 OID = 26 JSON = 114 JSON_ARRAY = 199 JSONB = 3802 JSONB_ARRAY = 3807 MACADDR = 829 MONEY = 790 MONEY_ARRAY = 791 NAME = 19 NAME_ARRAY = 1003 NUMERIC = 1700 NUMERIC_ARRAY = 1231 NULLTYPE = -1 OID = 26 POINT = 600 REAL = 700 REAL_ARRAY = 1021 SMALLINT = 21 SMALLINT_ARRAY = 1005 SMALLINT_VECTOR = 22 STRING = 1043 TEXT = 25 TEXT_ARRAY = 1009 TIME = 1083 TIME_ARRAY = 1183 TIMESTAMP = 1114 TIMESTAMP_ARRAY = 1115 TIMESTAMPTZ = 1184 TIMESTAMPTZ_ARRAY = 1185 UNKNOWN = 705 UUID_TYPE = 2950 UUID_ARRAY = 2951 VARCHAR = 1043 VARCHAR_ARRAY = 1015 XID = 28 MIN_INT2, MAX_INT2 = -(2 ** 15), 2 ** 15 MIN_INT4, MAX_INT4 = -(2 ** 31), 2 ** 31 MIN_INT8, MAX_INT8 = -(2 ** 63), 2 ** 63 def bool_in(data): return data == "t" def bool_out(v): return "true" if v else "false" def bytes_in(data): return bytes.fromhex(data[2:]) def bytes_out(v): return "\\x" + v.hex() def cidr_out(v): return str(v) def cidr_in(data): return ip_network(data, False) if "/" in data else ip_address(data) def date_in(data): return Datetime.strptime(data, "%Y-%m-%d").date() def date_out(v): return v.isoformat() def datetime_out(v): if v.tzinfo is None: return v.isoformat() else: return v.astimezone(Timezone.utc).isoformat() def enum_out(v): return str(v.value) def float_out(v): return str(v) def inet_in(data): return ip_network(data, False) if "/" in data else ip_address(data) def inet_out(v): return str(v) def int_in(data): return int(data) def int_out(v): return str(v) def interval_in(data): t = {} curr_val = None for k in data.split(): if ":" in k: t["hours"], t["minutes"], t["seconds"] = map(float, k.split(":")) else: try: curr_val = float(k) except ValueError: t[PGInterval.UNIT_MAP[k]] = curr_val for n in ["weeks", "months", "years", "decades", "centuries", "millennia"]: if n in t: raise InterfaceError( f"Can't fit the interval {t} into a datetime.timedelta." ) return Timedelta(**t) def interval_out(v): return f"{v.days} days {v.seconds} seconds {v.microseconds} microseconds" def json_in(data): return loads(data) def json_out(v): return dumps(v) def null_out(v): return None def numeric_in(data): return Decimal(data) def numeric_out(d): return str(d) def pg_interval_in(data): return PGInterval.from_str(data) def pg_interval_out(v): return str(v) def string_in(data): return data def string_out(v): return v def time_in(data): pattern = "%H:%M:%S.%f" if "." in data else "%H:%M:%S" return Datetime.strptime(data, pattern).time() def time_out(v): return v.isoformat() def timestamp_in(data): if data in ("infinity", "-infinity"): return data pattern = "%Y-%m-%d %H:%M:%S.%f" if "." in data else "%Y-%m-%d %H:%M:%S" return Datetime.strptime(data, pattern) def timestamptz_in(data): patt = "%Y-%m-%d %H:%M:%S.%f%z" if "." in data else "%Y-%m-%d %H:%M:%S%z" return Datetime.strptime(data + "00", patt) def unknown_out(v): return str(v) def vector_in(data): return eval("[" + data.replace(" ", ",") + "]") def uuid_out(v): return str(v) def uuid_in(data): return UUID(data) class PGInterval: UNIT_MAP = { "year": "years", "years": "years", "millennia": "millennia", "millenium": "millennia", "centuries": "centuries", "century": "centuries", "decades": "decades", "decade": "decades", "years": "years", "year": "years", "months": "months", "month": "months", "mon": "months", "mons": "months", "weeks": "weeks", "week": "weeks", "days": "days", "day": "days", "hours": "hours", "hour": "hours", "minutes": "minutes", "minute": "minutes", "seconds": "seconds", "second": "seconds", "microseconds": "microseconds", "microsecond": "microseconds", } @staticmethod def from_str(interval_str): t = {} curr_val = None for k in interval_str.split(): if ":" in k: hours_str, minutes_str, seconds_str = k.split(":") hours = int(hours_str) if hours != 0: t["hours"] = hours minutes = int(minutes_str) if minutes != 0: t["minutes"] = minutes try: seconds = int(seconds_str) except ValueError: seconds = float(seconds_str) if seconds != 0: t["seconds"] = seconds else: try: curr_val = int(k) except ValueError: t[PGInterval.UNIT_MAP[k]] = curr_val return PGInterval(**t) def __init__( self, millennia=None, centuries=None, decades=None, years=None, months=None, weeks=None, days=None, hours=None, minutes=None, seconds=None, microseconds=None, ): self.millennia = millennia self.centuries = centuries self.decades = decades self.years = years self.months = months self.weeks = weeks self.days = days self.hours = hours self.minutes = minutes self.seconds = seconds self.microseconds = microseconds def __repr__(self): return f"" def __str__(self): pairs = ( ("millennia", self.millennia), ("centuries", self.centuries), ("decades", self.decades), ("years", self.years), ("months", self.months), ("weeks", self.weeks), ("days", self.days), ("hours", self.hours), ("minutes", self.minutes), ("seconds", self.seconds), ("microseconds", self.microseconds), ) return " ".join(f"{v} {n}" for n, v in pairs if v is not None) def normalize(self): months = 0 if self.months is not None: months += self.months if self.years is not None: months += self.years * 12 days = 0 if self.days is not None: days += self.days if self.weeks is not None: days += self.weeks * 7 seconds = 0 if self.hours is not None: seconds += self.hours * 60 * 60 if self.minutes is not None: seconds += self.minutes * 60 if self.seconds is not None: seconds += self.seconds if self.microseconds is not None: seconds += self.microseconds / 1000000 return PGInterval(months=months, days=days, seconds=seconds) def __eq__(self, other): if isinstance(other, PGInterval): s = self.normalize() o = other.normalize() return s.months == o.months and s.days == o.days and s.seconds == o.seconds else: return False class ArrayState(Enum): InString = 1 InEscape = 2 InValue = 3 Out = 4 def _parse_array(data, adapter): state = ArrayState.Out stack = [[]] val = [] for c in data: if state == ArrayState.InValue: if c in ("}", ","): value = "".join(val) stack[-1].append(None if value == "NULL" else adapter(value)) state = ArrayState.Out else: val.append(c) if state == ArrayState.Out: if c == "{": a = [] stack[-1].append(a) stack.append(a) elif c == "}": stack.pop() elif c == ",": pass elif c == '"': val = [] state = ArrayState.InString else: val = [c] state = ArrayState.InValue elif state == ArrayState.InString: if c == '"': stack[-1].append(adapter("".join(val))) state = ArrayState.Out elif c == "\\": state = ArrayState.InEscape else: val.append(c) elif state == ArrayState.InEscape: val.append(c) state = ArrayState.InString return stack[0][0] def _array_in(adapter): def f(data): return _parse_array(data, adapter) return f bool_array_in = _array_in(bool_in) bytes_array_in = _array_in(bytes_in) cidr_array_in = _array_in(cidr_in) date_array_in = _array_in(date_in) inet_array_in = _array_in(inet_in) int_array_in = _array_in(int) interval_array_in = _array_in(interval_in) json_array_in = _array_in(json_in) float_array_in = _array_in(float) numeric_array_in = _array_in(numeric_in) string_array_in = _array_in(string_in) time_array_in = _array_in(time_in) timestamp_array_in = _array_in(timestamp_in) timestamptz_array_in = _array_in(timestamptz_in) uuid_array_in = _array_in(uuid_in) def array_string_escape(v): cs = [] for c in v: if c == "\\": cs.append("\\") elif c == '"': cs.append("\\") cs.append(c) val = "".join(cs) if ( len(val) == 0 or val == "NULL" or any([c in val for c in ("{", "}", ",", " ", "\\")]) ): val = f'"{val}"' return val def array_out(ar): result = [] for v in ar: if isinstance(v, (list, tuple)): val = array_out(v) elif v is None: val = "NULL" elif isinstance(v, dict): val = array_string_escape(json_out(v)) elif isinstance(v, (bytes, bytearray)): val = f'"\\{bytes_out(v)}"' elif isinstance(v, str): val = array_string_escape(v) else: val = make_param(PY_TYPES, v) result.append(val) return "{" + ",".join(result) + "}" PY_PG = { Date: DATE, Decimal: NUMERIC, IPv4Address: INET, IPv6Address: INET, IPv4Network: INET, IPv6Network: INET, PGInterval: INTERVAL, Time: TIME, Timedelta: INTERVAL, UUID: UUID_TYPE, bool: BOOLEAN, bytearray: BYTES, dict: JSONB, float: FLOAT, type(None): NULLTYPE, bytes: BYTES, str: TEXT, } PY_TYPES = { Date: date_out, # date Datetime: datetime_out, Decimal: numeric_out, # numeric Enum: enum_out, # enum IPv4Address: inet_out, # inet IPv6Address: inet_out, # inet IPv4Network: inet_out, # inet IPv6Network: inet_out, # inet PGInterval: interval_out, # interval Time: time_out, # time Timedelta: interval_out, # interval UUID: uuid_out, # uuid bool: bool_out, # bool bytearray: bytes_out, # bytea dict: json_out, # jsonb float: float_out, # float8 type(None): null_out, # null bytes: bytes_out, # bytea str: string_out, # unknown int: int_out, list: array_out, tuple: array_out, } PG_TYPES = { BIGINT: int, # int8 BIGINT_ARRAY: int_array_in, # int8[] BOOLEAN: bool_in, # bool BOOLEAN_ARRAY: bool_array_in, # bool[] BYTES: bytes_in, # bytea BYTES_ARRAY: bytes_array_in, # bytea[] CHAR: string_in, # char CHAR_ARRAY: string_array_in, # char[] CIDR_ARRAY: cidr_array_in, # cidr[] CSTRING: string_in, # cstring CSTRING_ARRAY: string_array_in, # cstring[] DATE: date_in, # date DATE_ARRAY: date_array_in, # date[] FLOAT: float, # float8 FLOAT_ARRAY: float_array_in, # float8[] INET: inet_in, # inet INET_ARRAY: inet_array_in, # inet[] INTEGER: int, # int4 INTEGER_ARRAY: int_array_in, # int4[] JSON: json_in, # json JSON_ARRAY: json_array_in, # json[] JSONB: json_in, # jsonb JSONB_ARRAY: json_array_in, # jsonb[] MACADDR: string_in, # MACADDR type MONEY: string_in, # money MONEY_ARRAY: string_array_in, # money[] NAME: string_in, # name NAME_ARRAY: string_array_in, # name[] NUMERIC: numeric_in, # numeric NUMERIC_ARRAY: numeric_array_in, # numeric[] OID: int, # oid INTERVAL: interval_in, # interval INTERVAL_ARRAY: interval_array_in, # interval[] REAL: float, # float4 REAL_ARRAY: float_array_in, # float4[] SMALLINT: int, # int2 SMALLINT_ARRAY: int_array_in, # int2[] SMALLINT_VECTOR: vector_in, # int2vector TEXT: string_in, # text TEXT_ARRAY: string_array_in, # text[] TIME: time_in, # time TIME_ARRAY: time_array_in, # time[] INTERVAL: interval_in, # interval TIMESTAMP: timestamp_in, # timestamp TIMESTAMP_ARRAY: timestamp_array_in, # timestamp TIMESTAMPTZ: timestamptz_in, # timestamptz TIMESTAMPTZ_ARRAY: timestamptz_array_in, # timestamptz UNKNOWN: string_in, # unknown UUID_ARRAY: uuid_array_in, # uuid[] UUID_TYPE: uuid_in, # uuid VARCHAR: string_in, # varchar VARCHAR_ARRAY: string_array_in, # varchar[] XID: int, # xid } # PostgreSQL encodings: # https://www.postgresql.org/docs/current/multibyte.html # # Python encodings: # https://docs.python.org/3/library/codecs.html # # Commented out encodings don't require a name change between PostgreSQL and # Python. If the py side is None, then the encoding isn't supported. PG_PY_ENCODINGS = { # Not supported: "mule_internal": None, "euc_tw": None, # Name fine as-is: # "euc_jp", # "euc_jis_2004", # "euc_kr", # "gb18030", # "gbk", # "johab", # "sjis", # "shift_jis_2004", # "uhc", # "utf8", # Different name: "euc_cn": "gb2312", "iso_8859_5": "is8859_5", "iso_8859_6": "is8859_6", "iso_8859_7": "is8859_7", "iso_8859_8": "is8859_8", "koi8": "koi8_r", "latin1": "iso8859-1", "latin2": "iso8859_2", "latin3": "iso8859_3", "latin4": "iso8859_4", "latin5": "iso8859_9", "latin6": "iso8859_10", "latin7": "iso8859_13", "latin8": "iso8859_14", "latin9": "iso8859_15", "sql_ascii": "ascii", "win866": "cp886", "win874": "cp874", "win1250": "cp1250", "win1251": "cp1251", "win1252": "cp1252", "win1253": "cp1253", "win1254": "cp1254", "win1255": "cp1255", "win1256": "cp1256", "win1257": "cp1257", "win1258": "cp1258", "unicode": "utf-8", # Needed for Amazon Redshift } def make_param(py_types, value): try: func = py_types[type(value)] except KeyError: func = str for k, v in py_types.items(): try: if isinstance(value, k): func = v break except TypeError: pass return func(value) def make_params(py_types, values): return tuple([make_param(py_types, v) for v in values]) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636797761.0 pg8000-1.23.0/pg8000/core.py0000664000175000017500000006312500000000000015531 0ustar00tlocketlocke00000000000000import codecs import socket import struct from collections import defaultdict, deque from hashlib import md5 from io import TextIOBase from itertools import count from struct import Struct import scramp from pg8000.converters import ( PG_PY_ENCODINGS, PG_TYPES, PY_TYPES, make_params, string_in, ) from pg8000.exceptions import DatabaseError, InterfaceError def pack_funcs(fmt): struc = Struct(f"!{fmt}") return struc.pack, struc.unpack_from i_pack, i_unpack = pack_funcs("i") h_pack, h_unpack = pack_funcs("h") ii_pack, ii_unpack = pack_funcs("ii") ihihih_pack, ihihih_unpack = pack_funcs("ihihih") ci_pack, ci_unpack = pack_funcs("ci") bh_pack, bh_unpack = pack_funcs("bh") cccc_pack, cccc_unpack = pack_funcs("cccc") # Copyright (c) 2007-2009, Mathieu Fenniak # Copyright (c) The Contributors # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # * The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. __author__ = "Mathieu Fenniak" NULL_BYTE = b"\x00" # Message codes NOTICE_RESPONSE = b"N" AUTHENTICATION_REQUEST = b"R" PARAMETER_STATUS = b"S" BACKEND_KEY_DATA = b"K" READY_FOR_QUERY = b"Z" ROW_DESCRIPTION = b"T" ERROR_RESPONSE = b"E" DATA_ROW = b"D" COMMAND_COMPLETE = b"C" PARSE_COMPLETE = b"1" BIND_COMPLETE = b"2" CLOSE_COMPLETE = b"3" PORTAL_SUSPENDED = b"s" NO_DATA = b"n" PARAMETER_DESCRIPTION = b"t" NOTIFICATION_RESPONSE = b"A" COPY_DONE = b"c" COPY_DATA = b"d" COPY_IN_RESPONSE = b"G" COPY_OUT_RESPONSE = b"H" EMPTY_QUERY_RESPONSE = b"I" BIND = b"B" PARSE = b"P" QUERY = b"Q" EXECUTE = b"E" FLUSH = b"H" SYNC = b"S" PASSWORD = b"p" DESCRIBE = b"D" TERMINATE = b"X" CLOSE = b"C" def _create_message(code, data=b""): return code + i_pack(len(data) + 4) + data FLUSH_MSG = _create_message(FLUSH) SYNC_MSG = _create_message(SYNC) TERMINATE_MSG = _create_message(TERMINATE) COPY_DONE_MSG = _create_message(COPY_DONE) EXECUTE_MSG = _create_message(EXECUTE, NULL_BYTE + i_pack(0)) # DESCRIBE constants STATEMENT = b"S" PORTAL = b"P" # ErrorResponse codes RESPONSE_SEVERITY = "S" # always present RESPONSE_SEVERITY = "V" # always present RESPONSE_CODE = "C" # always present RESPONSE_MSG = "M" # always present RESPONSE_DETAIL = "D" RESPONSE_HINT = "H" RESPONSE_POSITION = "P" RESPONSE__POSITION = "p" RESPONSE__QUERY = "q" RESPONSE_WHERE = "W" RESPONSE_FILE = "F" RESPONSE_LINE = "L" RESPONSE_ROUTINE = "R" IDLE = b"I" IDLE_IN_TRANSACTION = b"T" IDLE_IN_FAILED_TRANSACTION = b"E" class CoreConnection: def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() def __init__( self, user, host="localhost", database=None, port=5432, password=None, source_address=None, unix_sock=None, ssl_context=None, timeout=None, tcp_keepalive=True, application_name=None, replication=None, ): self._client_encoding = "utf8" self._commands_with_count = ( b"INSERT", b"DELETE", b"UPDATE", b"MOVE", b"FETCH", b"COPY", b"SELECT", ) self.notifications = deque(maxlen=100) self.notices = deque(maxlen=100) self.parameter_statuses = deque(maxlen=100) if user is None: raise InterfaceError("The 'user' connection parameter cannot be None") init_params = { "user": user, "database": database, "application_name": application_name, "replication": replication, } for k, v in tuple(init_params.items()): if isinstance(v, str): init_params[k] = v.encode("utf8") elif v is None: del init_params[k] elif not isinstance(v, (bytes, bytearray)): raise InterfaceError(f"The parameter {k} can't be of type {type(v)}.") self.user = init_params["user"] if isinstance(password, str): self.password = password.encode("utf8") else: self.password = password self.autocommit = False self._xid = None self._statement_nums = set() self._caches = {} if unix_sock is None and host is not None: try: self._usock = socket.create_connection( (host, port), timeout, source_address ) except socket.error as e: raise InterfaceError( f"Can't create a connection to host {host} and port {port} " f"(timeout is {timeout} and source_address is {source_address})." ) from e elif unix_sock is not None: try: if not hasattr(socket, "AF_UNIX"): raise InterfaceError( "attempt to connect to unix socket on unsupported platform" ) self._usock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) self._usock.settimeout(timeout) self._usock.connect(unix_sock) except socket.error as e: if self._usock is not None: self._usock.close() raise InterfaceError("communication error") from e else: raise InterfaceError("one of host or unix_sock must be provided") if tcp_keepalive: self._usock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) self.channel_binding = None if ssl_context is not None: try: import ssl if ssl_context is True: ssl_context = ssl.create_default_context() request_ssl = getattr(ssl_context, "request_ssl", True) if request_ssl: # Int32(8) - Message length, including self. # Int32(80877103) - The SSL request code. self._usock.sendall(ii_pack(8, 80877103)) resp = self._usock.recv(1) if resp != b"S": raise InterfaceError("Server refuses SSL") self._usock = ssl_context.wrap_socket(self._usock, server_hostname=host) if request_ssl: self.channel_binding = scramp.make_channel_binding( "tls-server-end-point", self._usock ) except ImportError: raise InterfaceError( "SSL required but ssl module not available in this python " "installation." ) self._sock = self._usock.makefile(mode="rwb") def sock_flush(): try: self._sock.flush() except OSError as e: raise InterfaceError("network error on flush") from e self._flush = sock_flush def sock_read(b): try: return self._sock.read(b) except OSError as e: raise InterfaceError("network error on read") from e self._read = sock_read def sock_write(d): try: self._sock.write(d) except OSError as e: raise InterfaceError("network error on write") from e self._write = sock_write self._backend_key_data = None self.pg_types = defaultdict(lambda: string_in, PG_TYPES) self.py_types = dict(PY_TYPES) self.message_types = { NOTICE_RESPONSE: self.handle_NOTICE_RESPONSE, AUTHENTICATION_REQUEST: self.handle_AUTHENTICATION_REQUEST, PARAMETER_STATUS: self.handle_PARAMETER_STATUS, BACKEND_KEY_DATA: self.handle_BACKEND_KEY_DATA, READY_FOR_QUERY: self.handle_READY_FOR_QUERY, ROW_DESCRIPTION: self.handle_ROW_DESCRIPTION, ERROR_RESPONSE: self.handle_ERROR_RESPONSE, EMPTY_QUERY_RESPONSE: self.handle_EMPTY_QUERY_RESPONSE, DATA_ROW: self.handle_DATA_ROW, COMMAND_COMPLETE: self.handle_COMMAND_COMPLETE, PARSE_COMPLETE: self.handle_PARSE_COMPLETE, BIND_COMPLETE: self.handle_BIND_COMPLETE, CLOSE_COMPLETE: self.handle_CLOSE_COMPLETE, PORTAL_SUSPENDED: self.handle_PORTAL_SUSPENDED, NO_DATA: self.handle_NO_DATA, PARAMETER_DESCRIPTION: self.handle_PARAMETER_DESCRIPTION, NOTIFICATION_RESPONSE: self.handle_NOTIFICATION_RESPONSE, COPY_DONE: self.handle_COPY_DONE, COPY_DATA: self.handle_COPY_DATA, COPY_IN_RESPONSE: self.handle_COPY_IN_RESPONSE, COPY_OUT_RESPONSE: self.handle_COPY_OUT_RESPONSE, } # Int32 - Message length, including self. # Int32(196608) - Protocol version number. Version 3.0. # Any number of key/value pairs, terminated by a zero byte: # String - A parameter name (user, database, or options) # String - Parameter value protocol = 196608 val = bytearray(i_pack(protocol)) for k, v in init_params.items(): val.extend(k.encode("ascii") + NULL_BYTE + v + NULL_BYTE) val.append(0) self._write(i_pack(len(val) + 4)) self._write(val) self._flush() code = self.error = None while code not in (READY_FOR_QUERY, ERROR_RESPONSE): code, data_len = ci_unpack(self._read(5)) self.message_types[code](self._read(data_len - 4), None) if self.error is not None: raise self.error self.in_transaction = False def register_out_adapter(self, typ, out_func): self.py_types[typ] = out_func def register_in_adapter(self, oid, in_func): self.pg_types[oid] = in_func def handle_ERROR_RESPONSE(self, data, context): msg = dict( ( s[:1].decode("ascii"), s[1:].decode(self._client_encoding, errors="replace"), ) for s in data.split(NULL_BYTE) if s != b"" ) self.error = DatabaseError(msg) def handle_EMPTY_QUERY_RESPONSE(self, data, context): self.error = DatabaseError("query was empty") def handle_CLOSE_COMPLETE(self, data, context): pass def handle_PARSE_COMPLETE(self, data, context): # Byte1('1') - Identifier. # Int32(4) - Message length, including self. pass def handle_BIND_COMPLETE(self, data, context): pass def handle_PORTAL_SUSPENDED(self, data, context): pass def handle_PARAMETER_DESCRIPTION(self, data, context): """https://www.postgresql.org/docs/current/protocol-message-formats.html""" # count = h_unpack(data)[0] # context.parameter_oids = unpack_from("!" + "i" * count, data, 2) def handle_COPY_DONE(self, data, context): pass def handle_COPY_OUT_RESPONSE(self, data, context): """https://www.postgresql.org/docs/current/protocol-message-formats.html""" is_binary, num_cols = bh_unpack(data) # column_formats = unpack_from('!' + 'h' * num_cols, data, 3) if context.stream is None: raise InterfaceError( "An output stream is required for the COPY OUT response." ) elif isinstance(context.stream, TextIOBase): if is_binary: raise InterfaceError( "The COPY OUT stream is binary, but the stream parameter is text." ) else: decode = codecs.getdecoder(self._client_encoding) def w(data): context.stream.write(decode(data)[0]) context.stream_write = w else: context.stream_write = context.stream.write def handle_COPY_DATA(self, data, context): context.stream_write(data) def handle_COPY_IN_RESPONSE(self, data, context): """https://www.postgresql.org/docs/current/protocol-message-formats.html""" is_binary, num_cols = bh_unpack(data) # column_formats = unpack_from('!' + 'h' * num_cols, data, 3) if context.stream is None: raise InterfaceError( "An input stream is required for the COPY IN response." ) elif isinstance(context.stream, TextIOBase): if is_binary: raise InterfaceError( "The COPY IN stream is binary, but the stream parameter is text." ) else: def ri(bffr): bffr.clear() bffr.extend(context.stream.read(4096).encode(self._client_encoding)) return len(bffr) readinto = ri else: readinto = context.stream.readinto bffr = bytearray(8192) while True: bytes_read = readinto(bffr) if bytes_read == 0: break self._write(COPY_DATA) self._write(i_pack(bytes_read + 4)) self._write(bffr[:bytes_read]) self._flush() # Send CopyDone self._write(COPY_DONE_MSG) self._write(SYNC_MSG) self._flush() def handle_NOTIFICATION_RESPONSE(self, data, context): """https://www.postgresql.org/docs/current/protocol-message-formats.html""" backend_pid = i_unpack(data)[0] idx = 4 null_idx = data.find(NULL_BYTE, idx) channel = data[idx:null_idx].decode("ascii") payload = data[null_idx + 1 : -1].decode("ascii") self.notifications.append((backend_pid, channel, payload)) def close(self): """Closes the database connection. This function is part of the `DBAPI 2.0 specification `_. """ try: self._write(TERMINATE_MSG) self._flush() self._sock.close() except AttributeError: raise InterfaceError("connection is closed") except ValueError: raise InterfaceError("connection is closed") except socket.error: pass finally: self._usock.close() self._sock = None def handle_AUTHENTICATION_REQUEST(self, data, context): """https://www.postgresql.org/docs/current/protocol-message-formats.html""" auth_code = i_unpack(data)[0] if auth_code == 0: pass elif auth_code == 3: if self.password is None: raise InterfaceError( "server requesting password authentication, but no password was " "provided" ) self._send_message(PASSWORD, self.password + NULL_BYTE) self._flush() elif auth_code == 5: salt = b"".join(cccc_unpack(data, 4)) if self.password is None: raise InterfaceError( "server requesting MD5 password authentication, but no password " "was provided" ) pwd = b"md5" + md5( md5(self.password + self.user).hexdigest().encode("ascii") + salt ).hexdigest().encode("ascii") self._send_message(PASSWORD, pwd + NULL_BYTE) self._flush() elif auth_code == 10: # AuthenticationSASL mechanisms = [m.decode("ascii") for m in data[4:-2].split(NULL_BYTE)] self.auth = scramp.ScramClient( mechanisms, self.user.decode("utf8"), self.password.decode("utf8"), channel_binding=self.channel_binding, ) init = self.auth.get_client_first().encode("utf8") mech = self.auth.mechanism_name.encode("ascii") + NULL_BYTE # SASLInitialResponse self._send_message(PASSWORD, mech + i_pack(len(init)) + init) self._flush() elif auth_code == 11: # AuthenticationSASLContinue self.auth.set_server_first(data[4:].decode("utf8")) # SASLResponse msg = self.auth.get_client_final().encode("utf8") self._send_message(PASSWORD, msg) self._flush() elif auth_code == 12: # AuthenticationSASLFinal self.auth.set_server_final(data[4:].decode("utf8")) elif auth_code in (2, 4, 6, 7, 8, 9): raise InterfaceError( f"Authentication method {auth_code} not supported by pg8000." ) else: raise InterfaceError( f"Authentication method {auth_code} not recognized by pg8000." ) def handle_READY_FOR_QUERY(self, data, context): self.in_transaction = data != IDLE def handle_BACKEND_KEY_DATA(self, data, context): self._backend_key_data = data def handle_ROW_DESCRIPTION(self, data, context): count = h_unpack(data)[0] idx = 2 columns = [] input_funcs = [] for i in range(count): name = data[idx : data.find(NULL_BYTE, idx)] idx += len(name) + 1 field = dict( zip( ( "table_oid", "column_attrnum", "type_oid", "type_size", "type_modifier", "format", ), ihihih_unpack(data, idx), ) ) field["name"] = name.decode(self._client_encoding) idx += 18 columns.append(field) input_funcs.append(self.pg_types[field["type_oid"]]) context.columns = columns context.input_funcs = input_funcs if context.rows is None: context.rows = [] def send_PARSE(self, statement_name_bin, statement, oids=()): val = bytearray(statement_name_bin) val.extend(statement.encode(self._client_encoding) + NULL_BYTE) val.extend(h_pack(len(oids))) for oid in oids: val.extend(i_pack(0 if oid == -1 else oid)) self._send_message(PARSE, val) self._write(FLUSH_MSG) def send_DESCRIBE_STATEMENT(self, statement_name_bin): self._send_message(DESCRIBE, STATEMENT + statement_name_bin) self._write(FLUSH_MSG) def send_QUERY(self, sql): self._send_message(QUERY, sql.encode(self._client_encoding) + NULL_BYTE) def execute_simple(self, statement): context = Context() self.send_QUERY(statement) self._flush() self.handle_messages(context) return context def execute_unnamed(self, statement, vals=(), oids=(), stream=None): context = Context(stream=stream) self.send_PARSE(NULL_BYTE, statement, oids) self._write(SYNC_MSG) self._flush() self.handle_messages(context) self.send_DESCRIBE_STATEMENT(NULL_BYTE) self._write(SYNC_MSG) try: self._flush() except AttributeError as e: if self._sock is None: raise InterfaceError("connection is closed") else: raise e params = make_params(self.py_types, vals) self.send_BIND(NULL_BYTE, params) self.handle_messages(context) self.send_EXECUTE() self._write(SYNC_MSG) self._flush() self.handle_messages(context) return context def prepare_statement(self, statement, oids=None): for i in count(): statement_name = f"pg8000_statement_{i}" statement_name_bin = statement_name.encode("ascii") + NULL_BYTE if statement_name_bin not in self._statement_nums: self._statement_nums.add(statement_name_bin) break self.send_PARSE(statement_name_bin, statement, oids) self.send_DESCRIBE_STATEMENT(statement_name_bin) self._write(SYNC_MSG) try: self._flush() except AttributeError as e: if self._sock is None: raise InterfaceError("connection is closed") else: raise e context = Context() self.handle_messages(context) return statement_name_bin, context.columns, context.input_funcs def execute_named(self, statement_name_bin, params, columns, input_funcs): context = Context(columns=columns, input_funcs=input_funcs) self.send_BIND(statement_name_bin, params) self.send_EXECUTE() self._write(SYNC_MSG) self._flush() self.handle_messages(context) return context def _send_message(self, code, data): try: self._write(code) self._write(i_pack(len(data) + 4)) self._write(data) except ValueError as e: if str(e) == "write to closed file": raise InterfaceError("connection is closed") else: raise e except AttributeError: raise InterfaceError("connection is closed") def send_BIND(self, statement_name_bin, params): """https://www.postgresql.org/docs/current/protocol-message-formats.html""" retval = bytearray( NULL_BYTE + statement_name_bin + h_pack(0) + h_pack(len(params)) ) for value in params: if value is None: retval.extend(i_pack(-1)) else: val = value.encode(self._client_encoding) retval.extend(i_pack(len(val))) retval.extend(val) retval.extend(h_pack(0)) self._send_message(BIND, retval) self._write(FLUSH_MSG) def send_EXECUTE(self): """https://www.postgresql.org/docs/current/protocol-message-formats.html""" self._write(EXECUTE_MSG) self._write(FLUSH_MSG) def handle_NO_DATA(self, msg, context): pass def handle_COMMAND_COMPLETE(self, data, context): values = data[:-1].split(b" ") try: row_count = int(values[-1]) if context.row_count == -1: context.row_count = row_count else: context.row_count += row_count except ValueError: pass def handle_DATA_ROW(self, data, context): idx = 2 row = [] for func in context.input_funcs: vlen = i_unpack(data, idx)[0] idx += 4 if vlen == -1: v = None else: v = func(str(data[idx : idx + vlen], encoding=self._client_encoding)) idx += vlen row.append(v) context.rows.append(row) def handle_messages(self, context): code = self.error = None while code != READY_FOR_QUERY: try: code, data_len = ci_unpack(self._read(5)) except struct.error as e: raise InterfaceError("network error on read") from e self.message_types[code](self._read(data_len - 4), context) if self.error is not None: raise self.error def close_prepared_statement(self, statement_name_bin): """https://www.postgresql.org/docs/current/protocol-message-formats.html""" self._send_message(CLOSE, STATEMENT + statement_name_bin) self._write(FLUSH_MSG) self._write(SYNC_MSG) self._flush() context = Context() self.handle_messages(context) self._statement_nums.remove(statement_name_bin) def handle_NOTICE_RESPONSE(self, data, context): """https://www.postgresql.org/docs/current/protocol-message-formats.html""" self.notices.append(dict((s[0:1], s[1:]) for s in data.split(NULL_BYTE))) def handle_PARAMETER_STATUS(self, data, context): pos = data.find(NULL_BYTE) key, value = data[:pos], data[pos + 1 : -1] self.parameter_statuses.append((key, value)) if key == b"client_encoding": encoding = value.decode("ascii").lower() self._client_encoding = PG_PY_ENCODINGS.get(encoding, encoding) elif key == b"integer_datetimes": if value == b"on": pass else: pass elif key == b"server_version": pass class Context: def __init__(self, stream=None, columns=None, input_funcs=None): self.rows = None if columns is None else [] self.row_count = -1 self.columns = columns self.stream = stream self.input_funcs = [] if input_funcs is None else input_funcs ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636797795.0 pg8000-1.23.0/pg8000/dbapi.py0000664000175000017500000007304600000000000015663 0ustar00tlocketlocke00000000000000from datetime import date as Date, datetime as Datetime, time as Time from itertools import count, islice from time import localtime from warnings import warn import pg8000 from pg8000.converters import ( BIGINT, BOOLEAN, BOOLEAN_ARRAY, BYTES, CHAR, CHAR_ARRAY, DATE, FLOAT, FLOAT_ARRAY, INET, INT2VECTOR, INTEGER, INTEGER_ARRAY, INTERVAL, JSON, JSONB, MACADDR, NAME, NAME_ARRAY, NULLTYPE, NUMERIC, NUMERIC_ARRAY, OID, PGInterval, STRING, TEXT, TEXT_ARRAY, TIME, TIMESTAMP, TIMESTAMPTZ, UNKNOWN, UUID_TYPE, VARCHAR, VARCHAR_ARRAY, XID, ) from pg8000.core import Context, CoreConnection from pg8000.exceptions import DatabaseError, Error, InterfaceError from ._version import get_versions __version__ = get_versions()["version"] del get_versions # Copyright (c) 2007-2009, Mathieu Fenniak # Copyright (c) The Contributors # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # * The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. __author__ = "Mathieu Fenniak" ROWID = OID apilevel = "2.0" """The DBAPI level supported, currently "2.0". This property is part of the `DBAPI 2.0 specification `_. """ threadsafety = 1 """Integer constant stating the level of thread safety the DBAPI interface supports. This DBAPI module supports sharing of the module only. Connections and cursors my not be shared between threads. This gives pg8000 a threadsafety value of 1. This property is part of the `DBAPI 2.0 specification `_. """ paramstyle = "format" BINARY = bytes def PgDate(year, month, day): """Constuct an object holding a date value. This function is part of the `DBAPI 2.0 specification `_. :rtype: :class:`datetime.date` """ return Date(year, month, day) def PgTime(hour, minute, second): """Construct an object holding a time value. This function is part of the `DBAPI 2.0 specification `_. :rtype: :class:`datetime.time` """ return Time(hour, minute, second) def Timestamp(year, month, day, hour, minute, second): """Construct an object holding a timestamp value. This function is part of the `DBAPI 2.0 specification `_. :rtype: :class:`datetime.datetime` """ return Datetime(year, month, day, hour, minute, second) def DateFromTicks(ticks): """Construct an object holding a date value from the given ticks value (number of seconds since the epoch). This function is part of the `DBAPI 2.0 specification `_. :rtype: :class:`datetime.date` """ return Date(*localtime(ticks)[:3]) def TimeFromTicks(ticks): """Construct an objet holding a time value from the given ticks value (number of seconds since the epoch). This function is part of the `DBAPI 2.0 specification `_. :rtype: :class:`datetime.time` """ return Time(*localtime(ticks)[3:6]) def TimestampFromTicks(ticks): """Construct an object holding a timestamp value from the given ticks value (number of seconds since the epoch). This function is part of the `DBAPI 2.0 specification `_. :rtype: :class:`datetime.datetime` """ return Timestamp(*localtime(ticks)[:6]) def Binary(value): """Construct an object holding binary data. This function is part of the `DBAPI 2.0 specification `_. """ return value def connect( user, host="localhost", database=None, port=5432, password=None, source_address=None, unix_sock=None, ssl_context=None, timeout=None, tcp_keepalive=True, application_name=None, replication=None, ): return Connection( user, host=host, database=database, port=port, password=password, source_address=source_address, unix_sock=unix_sock, ssl_context=ssl_context, timeout=timeout, tcp_keepalive=tcp_keepalive, application_name=application_name, replication=replication, ) apilevel = "2.0" """The DBAPI level supported, currently "2.0". This property is part of the `DBAPI 2.0 specification `_. """ threadsafety = 1 """Integer constant stating the level of thread safety the DBAPI interface supports. This DBAPI module supports sharing of the module only. Connections and cursors my not be shared between threads. This gives pg8000 a threadsafety value of 1. This property is part of the `DBAPI 2.0 specification `_. """ paramstyle = "format" def convert_paramstyle(style, query, args): # I don't see any way to avoid scanning the query string char by char, # so we might as well take that careful approach and create a # state-based scanner. We'll use int variables for the state. OUTSIDE = 0 # outside quoted string INSIDE_SQ = 1 # inside single-quote string '...' INSIDE_QI = 2 # inside quoted identifier "..." INSIDE_ES = 3 # inside escaped single-quote string, E'...' INSIDE_PN = 4 # inside parameter name eg. :name INSIDE_CO = 5 # inside inline comment eg. -- in_quote_escape = False in_param_escape = False placeholders = [] output_query = [] param_idx = map(lambda x: "$" + str(x), count(1)) state = OUTSIDE prev_c = None for i, c in enumerate(query): if i + 1 < len(query): next_c = query[i + 1] else: next_c = None if state == OUTSIDE: if c == "'": output_query.append(c) if prev_c == "E": state = INSIDE_ES else: state = INSIDE_SQ elif c == '"': output_query.append(c) state = INSIDE_QI elif c == "-": output_query.append(c) if prev_c == "-": state = INSIDE_CO elif style == "qmark" and c == "?": output_query.append(next(param_idx)) elif ( style == "numeric" and c == ":" and next_c not in ":=" and prev_c != ":" ): # Treat : as beginning of parameter name if and only # if it's the only : around # Needed to properly process type conversions # i.e. sum(x)::float output_query.append("$") elif style == "named" and c == ":" and next_c not in ":=" and prev_c != ":": # Same logic for : as in numeric parameters state = INSIDE_PN placeholders.append("") elif style == "pyformat" and c == "%" and next_c == "(": state = INSIDE_PN placeholders.append("") elif style in ("format", "pyformat") and c == "%": style = "format" if in_param_escape: in_param_escape = False output_query.append(c) else: if next_c == "%": in_param_escape = True elif next_c == "s": state = INSIDE_PN output_query.append(next(param_idx)) else: raise InterfaceError( "Only %s and %% are supported in the query." ) else: output_query.append(c) elif state == INSIDE_SQ: if c == "'": if in_quote_escape: in_quote_escape = False else: if next_c == "'": in_quote_escape = True else: state = OUTSIDE output_query.append(c) elif state == INSIDE_QI: if c == '"': state = OUTSIDE output_query.append(c) elif state == INSIDE_ES: if c == "'" and prev_c != "\\": # check for escaped single-quote state = OUTSIDE output_query.append(c) elif state == INSIDE_PN: if style == "named": placeholders[-1] += c if next_c is None or (not next_c.isalnum() and next_c != "_"): state = OUTSIDE try: pidx = placeholders.index(placeholders[-1], 0, -1) output_query.append("$" + str(pidx + 1)) del placeholders[-1] except ValueError: output_query.append("$" + str(len(placeholders))) elif style == "pyformat": if prev_c == ")" and c == "s": state = OUTSIDE try: pidx = placeholders.index(placeholders[-1], 0, -1) output_query.append("$" + str(pidx + 1)) del placeholders[-1] except ValueError: output_query.append("$" + str(len(placeholders))) elif c in "()": pass else: placeholders[-1] += c elif style == "format": state = OUTSIDE elif state == INSIDE_CO: output_query.append(c) if c == "\n": state = OUTSIDE prev_c = c if style in ("numeric", "qmark", "format"): vals = args else: vals = tuple(args[p] for p in placeholders) return "".join(output_query), vals class Cursor: def __init__(self, connection): self._c = connection self.arraysize = 1 self._context = None self._row_iter = None self._input_oids = () @property def connection(self): warn("DB-API extension cursor.connection used", stacklevel=3) return self._c @property def rowcount(self): context = self._context if context is None: return -1 return context.row_count @property def description(self): context = self._context if context is None: return None row_desc = context.columns if row_desc is None: return None if len(row_desc) == 0: return None columns = [] for col in row_desc: columns.append((col["name"], col["type_oid"], None, None, None, None, None)) return columns ## # Executes a database operation. Parameters may be provided as a sequence # or mapping and will be bound to variables in the operation. #

# Stability: Part of the DBAPI 2.0 specification. def execute(self, operation, args=(), stream=None): """Executes a database operation. Parameters may be provided as a sequence, or as a mapping, depending upon the value of :data:`pg8000.paramstyle`. This method is part of the `DBAPI 2.0 specification `_. :param operation: The SQL statement to execute. :param args: If :data:`paramstyle` is ``qmark``, ``numeric``, or ``format``, this argument should be an array of parameters to bind into the statement. If :data:`paramstyle` is ``named``, the argument should be a dict mapping of parameters. If the :data:`paramstyle` is ``pyformat``, the argument value may be either an array or a mapping. :param stream: This is a pg8000 extension for use with the PostgreSQL `COPY `_ command. For a COPY FROM the parameter must be a readable file-like object, and for COPY TO it must be writable. .. versionadded:: 1.9.11 """ try: if not self._c.in_transaction and not self._c.autocommit: self._c.execute_simple("begin transaction") if len(args) == 0 and stream is None: self._context = self._c.execute_simple(operation) else: statement, vals = convert_paramstyle(paramstyle, operation, args) self._context = self._c.execute_unnamed( statement, vals=vals, oids=self._input_oids, stream=stream ) if self._context.rows is None: self._row_iter = None else: self._row_iter = iter(self._context.rows) self._input_oids = () except AttributeError as e: if self._c is None: raise InterfaceError("Cursor closed") elif self._c._sock is None: raise InterfaceError("connection is closed") else: raise e self.input_types = [] def executemany(self, operation, param_sets): """Prepare a database operation, and then execute it against all parameter sequences or mappings provided. This method is part of the `DBAPI 2.0 specification `_. :param operation: The SQL statement to execute :param parameter_sets: A sequence of parameters to execute the statement with. The values in the sequence should be sequences or mappings of parameters, the same as the args argument of the :meth:`execute` method. """ rowcounts = [] input_oids = self._input_oids for parameters in param_sets: self._input_oids = input_oids self.execute(operation, parameters) rowcounts.append(self._context.row_count) if len(rowcounts) == 0: self._context = Context() elif -1 in rowcounts: self._context.row_count = -1 else: self._context.row_count = sum(rowcounts) def callproc(self, procname, parameters=None): args = [] if parameters is None else parameters operation = f"CALL {procname}(" + ", ".join(["%s" for _ in args]) + ")" try: statement, vals = convert_paramstyle("format", operation, args) self._context = self._c.execute_unnamed(statement, vals=vals) if self._context.rows is None: self._row_iter = None else: self._row_iter = iter(self._context.rows) except AttributeError as e: if self._c is None: raise InterfaceError("Cursor closed") elif self._c._sock is None: raise InterfaceError("connection is closed") else: raise e def fetchone(self): """Fetch the next row of a query result set. This method is part of the `DBAPI 2.0 specification `_. :returns: A row as a sequence of field values, or ``None`` if no more rows are available. """ try: return next(self) except StopIteration: return None except TypeError: raise ProgrammingError("attempting to use unexecuted cursor") def __iter__(self): """A cursor object is iterable to retrieve the rows from a query. This is a DBAPI 2.0 extension. """ return self def __next__(self): try: return next(self._row_iter) except AttributeError: if self._context is None: raise ProgrammingError("A query hasn't been issued.") else: raise except StopIteration as e: if self._context is None: raise ProgrammingError("A query hasn't been issued.") elif len(self._context.columns) == 0: raise ProgrammingError("no result set") else: raise e def fetchmany(self, num=None): """Fetches the next set of rows of a query result. This method is part of the `DBAPI 2.0 specification `_. :param size: The number of rows to fetch when called. If not provided, the :attr:`arraysize` attribute value is used instead. :returns: A sequence, each entry of which is a sequence of field values making up a row. If no more rows are available, an empty sequence will be returned. """ try: return tuple(islice(self, self.arraysize if num is None else num)) except TypeError: raise ProgrammingError("attempting to use unexecuted cursor") def fetchall(self): """Fetches all remaining rows of a query result. This method is part of the `DBAPI 2.0 specification `_. :returns: A sequence, each entry of which is a sequence of field values making up a row. """ try: return tuple(self) except TypeError: raise ProgrammingError("attempting to use unexecuted cursor") def close(self): """Closes the cursor. This method is part of the `DBAPI 2.0 specification `_. """ self._c = None def setinputsizes(self, *sizes): """This method is part of the `DBAPI 2.0 specification""" oids = [] for size in sizes: if isinstance(size, int): oid = size else: try: oid, _ = self._c.py_types[size] except KeyError: oid = pg8000.converters.UNKNOWN oids.append(oid) self._input_oids = oids def setoutputsize(self, size, column=None): """This method is part of the `DBAPI 2.0 specification `_, however, it is not implemented by pg8000. """ pass class Connection(CoreConnection): # DBAPI Extension: supply exceptions as attributes on the connection Warning = property(lambda self: self._getError(Warning)) Error = property(lambda self: self._getError(Error)) InterfaceError = property(lambda self: self._getError(InterfaceError)) DatabaseError = property(lambda self: self._getError(DatabaseError)) OperationalError = property(lambda self: self._getError(OperationalError)) IntegrityError = property(lambda self: self._getError(IntegrityError)) InternalError = property(lambda self: self._getError(InternalError)) ProgrammingError = property(lambda self: self._getError(ProgrammingError)) NotSupportedError = property(lambda self: self._getError(NotSupportedError)) def _getError(self, error): warn(f"DB-API extension connection.{error.__name__} used", stacklevel=3) return error def cursor(self): """Creates a :class:`Cursor` object bound to this connection. This function is part of the `DBAPI 2.0 specification `_. """ return Cursor(self) def commit(self): """Commits the current database transaction. This function is part of the `DBAPI 2.0 specification `_. """ self.execute_unnamed("commit") def rollback(self): """Rolls back the current database transaction. This function is part of the `DBAPI 2.0 specification `_. """ if not self.in_transaction: return self.execute_unnamed("rollback") def xid(self, format_id, global_transaction_id, branch_qualifier): """Create a Transaction IDs (only global_transaction_id is used in pg) format_id and branch_qualifier are not used in postgres global_transaction_id may be any string identifier supported by postgres returns a tuple (format_id, global_transaction_id, branch_qualifier)""" return (format_id, global_transaction_id, branch_qualifier) def tpc_begin(self, xid): """Begins a TPC transaction with the given transaction ID xid. This method should be called outside of a transaction (i.e. nothing may have executed since the last .commit() or .rollback()). Furthermore, it is an error to call .commit() or .rollback() within the TPC transaction. A ProgrammingError is raised, if the application calls .commit() or .rollback() during an active TPC transaction. This function is part of the `DBAPI 2.0 specification `_. """ self._xid = xid if self.autocommit: self.execute_unnamed("begin transaction") def tpc_prepare(self): """Performs the first phase of a transaction started with .tpc_begin(). A ProgrammingError is be raised if this method is called outside of a TPC transaction. After calling .tpc_prepare(), no statements can be executed until .tpc_commit() or .tpc_rollback() have been called. This function is part of the `DBAPI 2.0 specification `_. """ self.execute_unnamed("PREPARE TRANSACTION '%s';" % (self._xid[1],)) def tpc_commit(self, xid=None): """When called with no arguments, .tpc_commit() commits a TPC transaction previously prepared with .tpc_prepare(). If .tpc_commit() is called prior to .tpc_prepare(), a single phase commit is performed. A transaction manager may choose to do this if only a single resource is participating in the global transaction. When called with a transaction ID xid, the database commits the given transaction. If an invalid transaction ID is provided, a ProgrammingError will be raised. This form should be called outside of a transaction, and is intended for use in recovery. On return, the TPC transaction is ended. This function is part of the `DBAPI 2.0 specification `_. """ if xid is None: xid = self._xid if xid is None: raise ProgrammingError("Cannot tpc_commit() without a TPC transaction!") try: previous_autocommit_mode = self.autocommit self.autocommit = True if xid in self.tpc_recover(): self.execute_unnamed("COMMIT PREPARED '%s';" % (xid[1],)) else: # a single-phase commit self.commit() finally: self.autocommit = previous_autocommit_mode self._xid = None def tpc_rollback(self, xid=None): """When called with no arguments, .tpc_rollback() rolls back a TPC transaction. It may be called before or after .tpc_prepare(). When called with a transaction ID xid, it rolls back the given transaction. If an invalid transaction ID is provided, a ProgrammingError is raised. This form should be called outside of a transaction, and is intended for use in recovery. On return, the TPC transaction is ended. This function is part of the `DBAPI 2.0 specification `_. """ if xid is None: xid = self._xid if xid is None: raise ProgrammingError( "Cannot tpc_rollback() without a TPC prepared transaction!" ) try: previous_autocommit_mode = self.autocommit self.autocommit = True if xid in self.tpc_recover(): # a two-phase rollback self.execute_unnamed("ROLLBACK PREPARED '%s';" % (xid[1],)) else: # a single-phase rollback self.rollback() finally: self.autocommit = previous_autocommit_mode self._xid = None def tpc_recover(self): """Returns a list of pending transaction IDs suitable for use with .tpc_commit(xid) or .tpc_rollback(xid). This function is part of the `DBAPI 2.0 specification `_. """ try: previous_autocommit_mode = self.autocommit self.autocommit = True curs = self.cursor() curs.execute("select gid FROM pg_prepared_xacts") return [self.xid(0, row[0], "") for row in curs.fetchall()] finally: self.autocommit = previous_autocommit_mode class Warning(Exception): """Generic exception raised for important database warnings like data truncations. This exception is not currently used by pg8000. This exception is part of the `DBAPI 2.0 specification `_. """ pass class DataError(DatabaseError): """Generic exception raised for errors that are due to problems with the processed data. This exception is not currently raised by pg8000. This exception is part of the `DBAPI 2.0 specification `_. """ pass class OperationalError(DatabaseError): """ Generic exception raised for errors that are related to the database's operation and not necessarily under the control of the programmer. This exception is currently never raised by pg8000. This exception is part of the `DBAPI 2.0 specification `_. """ pass class IntegrityError(DatabaseError): """ Generic exception raised when the relational integrity of the database is affected. This exception is not currently raised by pg8000. This exception is part of the `DBAPI 2.0 specification `_. """ pass class InternalError(DatabaseError): """Generic exception raised when the database encounters an internal error. This is currently only raised when unexpected state occurs in the pg8000 interface itself, and is typically the result of a interface bug. This exception is part of the `DBAPI 2.0 specification `_. """ pass class ProgrammingError(DatabaseError): """Generic exception raised for programming errors. For example, this exception is raised if more parameter fields are in a query string than there are available parameters. This exception is part of the `DBAPI 2.0 specification `_. """ pass class NotSupportedError(DatabaseError): """Generic exception raised in case a method or database API was used which is not supported by the database. This exception is part of the `DBAPI 2.0 specification `_. """ pass class ArrayContentNotSupportedError(NotSupportedError): """ Raised when attempting to transmit an array where the base type is not supported for binary data transfer by the interface. """ pass __all__ = [ "BIGINT", "BINARY", "BOOLEAN", "BOOLEAN_ARRAY", "BYTES", "Binary", "CHAR", "CHAR_ARRAY", "Connection", "Cursor", "DATE", "DataError", "DatabaseError", "Date", "DateFromTicks", "Error", "FLOAT", "FLOAT_ARRAY", "INET", "INT2VECTOR", "INTEGER", "INTEGER_ARRAY", "INTERVAL", "IntegrityError", "InterfaceError", "InternalError", "JSON", "JSONB", "MACADDR", "NAME", "NAME_ARRAY", "NULLTYPE", "NUMERIC", "NUMERIC_ARRAY", "NotSupportedError", "OID", "OperationalError", "PGInterval", "ProgrammingError", "ROWID", "STRING", "TEXT", "TEXT_ARRAY", "TIME", "TIMESTAMP", "TIMESTAMPTZ", "Time", "TimeFromTicks", "Timestamp", "TimestampFromTicks", "UNKNOWN", "UUID_TYPE", "VARCHAR", "VARCHAR_ARRAY", "Warning", "XID", "connect", ] ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/pg8000/exceptions.py0000664000175000017500000000156700000000000016764 0ustar00tlocketlocke00000000000000class Error(Exception): """Generic exception that is the base exception of all other error exceptions. This exception is part of the `DBAPI 2.0 specification `_. """ pass class InterfaceError(Error): """Generic exception raised for errors that are related to the database interface rather than the database itself. For example, if the interface attempts to use an SSL connection but the server refuses, an InterfaceError will be raised. This exception is part of the `DBAPI 2.0 specification `_. """ pass class DatabaseError(Error): """Generic exception raised for errors that are related to the database. This exception is part of the `DBAPI 2.0 specification `_. """ pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636797954.0 pg8000-1.23.0/pg8000/legacy.py0000664000175000017500000006170200000000000016044 0ustar00tlocketlocke00000000000000from datetime import date as Date, time as Time from itertools import islice from warnings import warn import pg8000 from pg8000.converters import ( BIGINT, BOOLEAN, BOOLEAN_ARRAY, BYTES, CHAR, CHAR_ARRAY, DATE, FLOAT, FLOAT_ARRAY, INET, INT2VECTOR, INTEGER, INTEGER_ARRAY, INTERVAL, JSON, JSONB, MACADDR, NAME, NAME_ARRAY, NULLTYPE, NUMERIC, NUMERIC_ARRAY, OID, PGInterval, PY_PG, STRING, TEXT, TEXT_ARRAY, TIME, TIMESTAMP, TIMESTAMPTZ, UNKNOWN, UUID_TYPE, VARCHAR, VARCHAR_ARRAY, XID, interval_in as timedelta_in, make_params, pg_interval_in as pginterval_in, pg_interval_out as pginterval_out, ) from pg8000.core import Context, CoreConnection from pg8000.dbapi import ( BINARY, Binary, DataError, DateFromTicks, IntegrityError, InternalError, NotSupportedError, OperationalError, ProgrammingError, TimeFromTicks, Timestamp, TimestampFromTicks, Warning, convert_paramstyle, ) from pg8000.exceptions import DatabaseError, Error, InterfaceError from ._version import get_versions __version__ = get_versions()["version"] del get_versions # Copyright (c) 2007-2009, Mathieu Fenniak # Copyright (c) The Contributors # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # * The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. __author__ = "Mathieu Fenniak" BIGINTEGER = BIGINT DATETIME = TIMESTAMP NUMBER = DECIMAL = NUMERIC DECIMAL_ARRAY = NUMERIC_ARRAY ROWID = OID TIMEDELTA = INTERVAL def connect( user, host="localhost", database=None, port=5432, password=None, source_address=None, unix_sock=None, ssl_context=None, timeout=None, tcp_keepalive=True, application_name=None, replication=None, ): return Connection( user, host=host, database=database, port=port, password=password, source_address=source_address, unix_sock=unix_sock, ssl_context=ssl_context, timeout=timeout, tcp_keepalive=tcp_keepalive, application_name=application_name, replication=replication, ) apilevel = "2.0" """The DBAPI level supported, currently "2.0". This property is part of the `DBAPI 2.0 specification `_. """ threadsafety = 1 """Integer constant stating the level of thread safety the DBAPI interface supports. This DBAPI module supports sharing of the module only. Connections and cursors my not be shared between threads. This gives pg8000 a threadsafety value of 1. This property is part of the `DBAPI 2.0 specification `_. """ paramstyle = "format" class Cursor: def __init__(self, connection, paramstyle=None): self._c = connection self.arraysize = 1 if paramstyle is None: self.paramstyle = pg8000.paramstyle else: self.paramstyle = paramstyle self._context = None self._row_iter = None self._input_oids = () def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.close() @property def connection(self): warn("DB-API extension cursor.connection used", stacklevel=3) return self._c @property def rowcount(self): context = self._context if context is None: return -1 return context.row_count description = property(lambda self: self._getDescription()) def _getDescription(self): context = self._context if context is None: return None row_desc = context.columns if row_desc is None: return None if len(row_desc) == 0: return None columns = [] for col in row_desc: columns.append((col["name"], col["type_oid"], None, None, None, None, None)) return columns ## # Executes a database operation. Parameters may be provided as a sequence # or mapping and will be bound to variables in the operation. #

# Stability: Part of the DBAPI 2.0 specification. def execute(self, operation, args=(), stream=None): """Executes a database operation. Parameters may be provided as a sequence, or as a mapping, depending upon the value of :data:`pg8000.paramstyle`. This method is part of the `DBAPI 2.0 specification `_. :param operation: The SQL statement to execute. :param args: If :data:`paramstyle` is ``qmark``, ``numeric``, or ``format``, this argument should be an array of parameters to bind into the statement. If :data:`paramstyle` is ``named``, the argument should be a dict mapping of parameters. If the :data:`paramstyle` is ``pyformat``, the argument value may be either an array or a mapping. :param stream: This is a pg8000 extension for use with the PostgreSQL `COPY `_ command. For a COPY FROM the parameter must be a readable file-like object, and for COPY TO it must be writable. .. versionadded:: 1.9.11 """ try: if not self._c.in_transaction and not self._c.autocommit: self._c.execute_simple("begin transaction") if len(args) == 0 and stream is None: self._context = self._c.execute_simple(operation) else: statement, vals = convert_paramstyle(self.paramstyle, operation, args) self._context = self._c.execute_unnamed( statement, vals=vals, oids=self._input_oids, stream=stream ) rows = [] if self._context.rows is None else self._context.rows self._row_iter = iter(rows) self._input_oids = () except AttributeError as e: if self._c is None: raise InterfaceError("Cursor closed") elif self._c._sock is None: raise InterfaceError("connection is closed") else: raise e except DatabaseError as e: msg = e.args[0] if isinstance(msg, dict): response_code = msg["C"] if response_code == "28000": cls = InterfaceError elif response_code == "23505": cls = IntegrityError else: cls = ProgrammingError raise cls(msg) else: raise ProgrammingError(msg) self.input_types = [] return self def executemany(self, operation, param_sets): """Prepare a database operation, and then execute it against all parameter sequences or mappings provided. This method is part of the `DBAPI 2.0 specification `_. :param operation: The SQL statement to execute :param parameter_sets: A sequence of parameters to execute the statement with. The values in the sequence should be sequences or mappings of parameters, the same as the args argument of the :meth:`execute` method. """ rowcounts = [] input_oids = self._input_oids for parameters in param_sets: self._input_oids = input_oids self.execute(operation, parameters) rowcounts.append(self._context.row_count) if len(rowcounts) == 0: self._context = Context() elif -1 in rowcounts: self._context.row_count = -1 else: self._context.row_count = sum(rowcounts) return self def fetchone(self): """Fetch the next row of a query result set. This method is part of the `DBAPI 2.0 specification `_. :returns: A row as a sequence of field values, or ``None`` if no more rows are available. """ try: return next(self) except StopIteration: return None except TypeError: raise ProgrammingError("attempting to use unexecuted cursor") except AttributeError: raise ProgrammingError("attempting to use unexecuted cursor") def fetchmany(self, num=None): """Fetches the next set of rows of a query result. This method is part of the `DBAPI 2.0 specification `_. :param size: The number of rows to fetch when called. If not provided, the :attr:`arraysize` attribute value is used instead. :returns: A sequence, each entry of which is a sequence of field values making up a row. If no more rows are available, an empty sequence will be returned. """ try: return tuple(islice(self, self.arraysize if num is None else num)) except TypeError: raise ProgrammingError("attempting to use unexecuted cursor") def fetchall(self): """Fetches all remaining rows of a query result. This method is part of the `DBAPI 2.0 specification `_. :returns: A sequence, each entry of which is a sequence of field values making up a row. """ try: return tuple(self) except TypeError: raise ProgrammingError("attempting to use unexecuted cursor") def close(self): """Closes the cursor. This method is part of the `DBAPI 2.0 specification `_. """ self._c = None def __iter__(self): """A cursor object is iterable to retrieve the rows from a query. This is a DBAPI 2.0 extension. """ return self def setinputsizes(self, *sizes): """This method is part of the `DBAPI 2.0 specification""" oids = [] for size in sizes: if isinstance(size, int): oid = size else: try: oid = PY_PG[size] except KeyError: oid = UNKNOWN oids.append(oid) self._input_oids = oids def setoutputsize(self, size, column=None): """This method is part of the `DBAPI 2.0 specification `_, however, it is not implemented by pg8000. """ pass def __next__(self): try: return next(self._row_iter) except AttributeError: if self._context is None: raise ProgrammingError("A query hasn't been issued.") else: raise except StopIteration as e: if self._context is None: raise ProgrammingError("A query hasn't been issued.") elif len(self._context.columns) == 0: raise ProgrammingError("no result set") else: raise e class Connection(CoreConnection): # DBAPI Extension: supply exceptions as attributes on the connection Warning = property(lambda self: self._getError(Warning)) Error = property(lambda self: self._getError(Error)) InterfaceError = property(lambda self: self._getError(InterfaceError)) DatabaseError = property(lambda self: self._getError(DatabaseError)) OperationalError = property(lambda self: self._getError(OperationalError)) IntegrityError = property(lambda self: self._getError(IntegrityError)) InternalError = property(lambda self: self._getError(InternalError)) ProgrammingError = property(lambda self: self._getError(ProgrammingError)) NotSupportedError = property(lambda self: self._getError(NotSupportedError)) def __init__(self, *args, **kwargs): try: super().__init__(*args, **kwargs) except DatabaseError as e: msg = e.args[0] if isinstance(msg, dict): response_code = msg["C"] if response_code == "28000": cls = InterfaceError elif response_code == "23505": cls = IntegrityError else: cls = ProgrammingError raise cls(msg) else: raise ProgrammingError(msg) self._run_cursor = Cursor(self, paramstyle="named") def _getError(self, error): warn("DB-API extension connection.%s used" % error.__name__, stacklevel=3) return error def cursor(self): """Creates a :class:`Cursor` object bound to this connection. This function is part of the `DBAPI 2.0 specification `_. """ return Cursor(self) @property def description(self): return self._run_cursor._getDescription() def commit(self): """Commits the current database transaction. This function is part of the `DBAPI 2.0 specification `_. """ self.execute_unnamed("commit") def rollback(self): """Rolls back the current database transaction. This function is part of the `DBAPI 2.0 specification `_. """ if not self.in_transaction: return self.execute_unnamed("rollback") def run(self, sql, stream=None, **params): self._run_cursor.execute(sql, params, stream=stream) if self._run_cursor._context.rows is None: return tuple() else: return tuple(self._run_cursor._context.rows) def prepare(self, operation): return PreparedStatement(self, operation) def xid(self, format_id, global_transaction_id, branch_qualifier): """Create a Transaction IDs (only global_transaction_id is used in pg) format_id and branch_qualifier are not used in postgres global_transaction_id may be any string identifier supported by postgres returns a tuple (format_id, global_transaction_id, branch_qualifier)""" return (format_id, global_transaction_id, branch_qualifier) def tpc_begin(self, xid): """Begins a TPC transaction with the given transaction ID xid. This method should be called outside of a transaction (i.e. nothing may have executed since the last .commit() or .rollback()). Furthermore, it is an error to call .commit() or .rollback() within the TPC transaction. A ProgrammingError is raised, if the application calls .commit() or .rollback() during an active TPC transaction. This function is part of the `DBAPI 2.0 specification `_. """ self._xid = xid if self.autocommit: self.execute_unnamed("begin transaction") def tpc_prepare(self): """Performs the first phase of a transaction started with .tpc_begin(). A ProgrammingError is be raised if this method is called outside of a TPC transaction. After calling .tpc_prepare(), no statements can be executed until .tpc_commit() or .tpc_rollback() have been called. This function is part of the `DBAPI 2.0 specification `_. """ q = "PREPARE TRANSACTION '%s';" % (self._xid[1],) self.execute_unnamed(q) def tpc_commit(self, xid=None): """When called with no arguments, .tpc_commit() commits a TPC transaction previously prepared with .tpc_prepare(). If .tpc_commit() is called prior to .tpc_prepare(), a single phase commit is performed. A transaction manager may choose to do this if only a single resource is participating in the global transaction. When called with a transaction ID xid, the database commits the given transaction. If an invalid transaction ID is provided, a ProgrammingError will be raised. This form should be called outside of a transaction, and is intended for use in recovery. On return, the TPC transaction is ended. This function is part of the `DBAPI 2.0 specification `_. """ if xid is None: xid = self._xid if xid is None: raise ProgrammingError("Cannot tpc_commit() without a TPC transaction!") try: previous_autocommit_mode = self.autocommit self.autocommit = True if xid in self.tpc_recover(): self.execute_unnamed("COMMIT PREPARED '%s';" % (xid[1],)) else: # a single-phase commit self.commit() finally: self.autocommit = previous_autocommit_mode self._xid = None def tpc_rollback(self, xid=None): """When called with no arguments, .tpc_rollback() rolls back a TPC transaction. It may be called before or after .tpc_prepare(). When called with a transaction ID xid, it rolls back the given transaction. If an invalid transaction ID is provided, a ProgrammingError is raised. This form should be called outside of a transaction, and is intended for use in recovery. On return, the TPC transaction is ended. This function is part of the `DBAPI 2.0 specification `_. """ if xid is None: xid = self._xid if xid is None: raise ProgrammingError( "Cannot tpc_rollback() without a TPC prepared transaction!" ) try: previous_autocommit_mode = self.autocommit self.autocommit = True if xid in self.tpc_recover(): # a two-phase rollback self.execute_unnamed("ROLLBACK PREPARED '%s';" % (xid[1],)) else: # a single-phase rollback self.rollback() finally: self.autocommit = previous_autocommit_mode self._xid = None def tpc_recover(self): """Returns a list of pending transaction IDs suitable for use with .tpc_commit(xid) or .tpc_rollback(xid). This function is part of the `DBAPI 2.0 specification `_. """ try: previous_autocommit_mode = self.autocommit self.autocommit = True curs = self.cursor() curs.execute("select gid FROM pg_prepared_xacts") return [self.xid(0, row[0], "") for row in curs] finally: self.autocommit = previous_autocommit_mode def to_statement(query): OUTSIDE = 0 # outside quoted string INSIDE_SQ = 1 # inside single-quote string '...' INSIDE_QI = 2 # inside quoted identifier "..." INSIDE_ES = 3 # inside escaped single-quote string, E'...' INSIDE_PN = 4 # inside parameter name eg. :name INSIDE_CO = 5 # inside inline comment eg. -- in_quote_escape = False placeholders = [] output_query = [] state = OUTSIDE prev_c = None for i, c in enumerate(query): if i + 1 < len(query): next_c = query[i + 1] else: next_c = None if state == OUTSIDE: if c == "'": output_query.append(c) if prev_c == "E": state = INSIDE_ES else: state = INSIDE_SQ elif c == '"': output_query.append(c) state = INSIDE_QI elif c == "-": output_query.append(c) if prev_c == "-": state = INSIDE_CO elif c == ":" and next_c not in ":=" and prev_c != ":": state = INSIDE_PN placeholders.append("") else: output_query.append(c) elif state == INSIDE_SQ: if c == "'": if in_quote_escape: in_quote_escape = False else: if next_c == "'": in_quote_escape = True else: state = OUTSIDE output_query.append(c) elif state == INSIDE_QI: if c == '"': state = OUTSIDE output_query.append(c) elif state == INSIDE_ES: if c == "'" and prev_c != "\\": # check for escaped single-quote state = OUTSIDE output_query.append(c) elif state == INSIDE_PN: placeholders[-1] += c if next_c is None or (not next_c.isalnum() and next_c != "_"): state = OUTSIDE try: pidx = placeholders.index(placeholders[-1], 0, -1) output_query.append("$" + str(pidx + 1)) del placeholders[-1] except ValueError: output_query.append("$" + str(len(placeholders))) elif state == INSIDE_CO: output_query.append(c) if c == "\n": state = OUTSIDE prev_c = c def make_vals(args): return tuple(args[p] for p in placeholders) return "".join(output_query), make_vals class PreparedStatement: def __init__(self, con, operation): self.con = con self.operation = operation statement, self.make_args = to_statement(operation) self.name_bin, self.row_desc, self.input_funcs = con.prepare_statement( statement, () ) def run(self, **vals): params = make_params(self.con.py_types, self.make_args(vals)) try: if not self.con.in_transaction and not self.con.autocommit: self.con.execute_unnamed("begin transaction") self._context = self.con.execute_named( self.name_bin, params, self.row_desc, self.input_funcs ) except AttributeError as e: if self.con is None: raise InterfaceError("Cursor closed") elif self.con._sock is None: raise InterfaceError("connection is closed") else: raise e return tuple() if self._context.rows is None else tuple(self._context.rows) def close(self): self.con.close_prepared_statement(self.name_bin) self.con = None __all__ = [ "BIGINTEGER", "BINARY", "BOOLEAN", "BOOLEAN_ARRAY", "BYTES", "Binary", "CHAR", "CHAR_ARRAY", "Connection", "Cursor", "DATE", "DATETIME", "DECIMAL", "DECIMAL_ARRAY", "DataError", "DatabaseError", "Date", "DateFromTicks", "Error", "FLOAT", "FLOAT_ARRAY", "INET", "INT2VECTOR", "INTEGER", "INTEGER_ARRAY", "INTERVAL", "IntegrityError", "InterfaceError", "InternalError", "JSON", "JSONB", "MACADDR", "NAME", "NAME_ARRAY", "NULLTYPE", "NUMBER", "NotSupportedError", "OID", "OperationalError", "PGInterval", "ProgrammingError", "ROWID", "STRING", "TEXT", "TEXT_ARRAY", "TIME", "TIMEDELTA", "TIMESTAMP", "TIMESTAMPTZ", "Time", "TimeFromTicks", "Timestamp", "TimestampFromTicks", "UNKNOWN", "UUID_TYPE", "VARCHAR", "VARCHAR_ARRAY", "Warning", "XID", "connect", "pginterval_in", "pginterval_out", "timedelta_in", ] ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636798025.0 pg8000-1.23.0/pg8000/native.py0000664000175000017500000001707000000000000016065 0ustar00tlocketlocke00000000000000from collections import defaultdict from pg8000.converters import ( BIGINT, BOOLEAN, BOOLEAN_ARRAY, BYTES, CHAR, CHAR_ARRAY, DATE, FLOAT, FLOAT_ARRAY, INET, INT2VECTOR, INTEGER, INTEGER_ARRAY, INTERVAL, JSON, JSONB, MACADDR, NAME, NAME_ARRAY, NULLTYPE, NUMERIC, NUMERIC_ARRAY, OID, PGInterval, STRING, TEXT, TEXT_ARRAY, TIME, TIMESTAMP, TIMESTAMPTZ, UNKNOWN, UUID_TYPE, VARCHAR, VARCHAR_ARRAY, XID, make_params, ) from pg8000.core import CoreConnection from pg8000.exceptions import DatabaseError, Error, InterfaceError # Copyright (c) 2007-2009, Mathieu Fenniak # Copyright (c) The Contributors # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # * Redistributions of source code must retain the above copyright notice, # this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # * The name of the author may not be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE # POSSIBILITY OF SUCH DAMAGE. OUTSIDE = 0 # outside quoted string INSIDE_SQ = 1 # inside single-quote string '...' INSIDE_QI = 2 # inside quoted identifier "..." INSIDE_ES = 3 # inside escaped single-quote string, E'...' INSIDE_PN = 4 # inside parameter name eg. :name INSIDE_CO = 5 # inside inline comment eg. -- def to_statement(query): in_quote_escape = False placeholders = [] output_query = [] state = OUTSIDE prev_c = None for i, c in enumerate(query): if i + 1 < len(query): next_c = query[i + 1] else: next_c = None if state == OUTSIDE: if c == "'": output_query.append(c) if prev_c == "E": state = INSIDE_ES else: state = INSIDE_SQ elif c == '"': output_query.append(c) state = INSIDE_QI elif c == "-": output_query.append(c) if prev_c == "-": state = INSIDE_CO elif c == ":" and next_c not in ":=" and prev_c != ":": state = INSIDE_PN placeholders.append("") else: output_query.append(c) elif state == INSIDE_SQ: if c == "'": if in_quote_escape: in_quote_escape = False elif next_c == "'": in_quote_escape = True else: state = OUTSIDE output_query.append(c) elif state == INSIDE_QI: if c == '"': state = OUTSIDE output_query.append(c) elif state == INSIDE_ES: if c == "'" and prev_c != "\\": # check for escaped single-quote state = OUTSIDE output_query.append(c) elif state == INSIDE_PN: placeholders[-1] += c if next_c is None or (not next_c.isalnum() and next_c != "_"): state = OUTSIDE try: pidx = placeholders.index(placeholders[-1], 0, -1) output_query.append(f"${pidx + 1}") del placeholders[-1] except ValueError: output_query.append(f"${len(placeholders)}") elif state == INSIDE_CO: output_query.append(c) if c == "\n": state = OUTSIDE prev_c = c for reserved in ("types", "stream"): if reserved in placeholders: raise InterfaceError( f"The name '{reserved}' can't be used as a placeholder because it's " f"used for another purpose." ) def make_vals(args): vals = [] for p in placeholders: try: vals.append(args[p]) except KeyError: raise InterfaceError( f"There's a placeholder '{p}' in the query, but no matching " f"keyword argument." ) return tuple(vals) return "".join(output_query), make_vals class Connection(CoreConnection): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._context = None @property def columns(self): context = self._context if context is None: return None return context.columns @property def row_count(self): context = self._context if context is None: return None return context.row_count def run(self, sql, stream=None, types=None, **params): if len(params) == 0 and stream is None: self._context = self.execute_simple(sql) else: statement, make_vals = to_statement(sql) oids = () if types is None else make_vals(defaultdict(lambda: None, types)) self._context = self.execute_unnamed( statement, make_vals(params), oids=oids, stream=stream ) return self._context.rows def prepare(self, sql): return PreparedStatement(self, sql) class PreparedStatement: def __init__(self, con, sql, types=None): self.con = con statement, self.make_vals = to_statement(sql) oids = () if types is None else self.make_vals(defaultdict(lambda: None, types)) self.name_bin, self.cols, self.input_funcs = con.prepare_statement( statement, oids ) @property def columns(self): return self._context.columns def run(self, stream=None, **params): params = make_params(self.con.py_types, self.make_vals(params)) self._context = self.con.execute_named( self.name_bin, params, self.cols, self.input_funcs ) return self._context.rows def close(self): self.con.close_prepared_statement(self.name_bin) __all__ = [ "BIGINT", "BOOLEAN", "BOOLEAN_ARRAY", "BYTES", "CHAR", "CHAR_ARRAY", "DATE", "DatabaseError", "Error", "FLOAT", "FLOAT_ARRAY", "INET", "INT2VECTOR", "INTEGER", "INTEGER_ARRAY", "INTERVAL", "InterfaceError", "JSON", "JSONB", "MACADDR", "NAME", "NAME_ARRAY", "NULLTYPE", "NUMERIC", "NUMERIC_ARRAY", "OID", "PGInterval", "STRING", "TEXT", "TEXT_ARRAY", "TIME", "TIMESTAMP", "TIMESTAMPTZ", "UNKNOWN", "UUID_TYPE", "VARCHAR", "VARCHAR_ARRAY", "XID", ] ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1636798860.4009943 pg8000-1.23.0/pg8000.egg-info/0000775000175000017500000000000000000000000015712 5ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636798860.0 pg8000-1.23.0/pg8000.egg-info/PKG-INFO0000664000175000017500000000336300000000000017014 0ustar00tlocketlocke00000000000000Metadata-Version: 1.2 Name: pg8000 Version: 1.23.0 Summary: PostgreSQL interface library Home-page: https://github.com/tlocke/pg8000 Author: Mathieu Fenniak Author-email: biziqe@mathieu.fenniak.net License: BSD Description: pg8000 ------ pg8000 is a Pure-Python interface to the PostgreSQL database engine. It is one of many PostgreSQL interfaces for the Python programming language. pg8000 is somewhat distinctive in that it is written entirely in Python and does not rely on any external libraries (such as a compiled python module, or PostgreSQL's libpq library). pg8000 supports the standard Python DB-API version 2.0. pg8000's name comes from the belief that it is probably about the 8000th PostgreSQL interface for Python. Keywords: postgresql dbapi Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 3 Classifier: Programming Language :: Python :: 3.6 Classifier: Programming Language :: Python :: 3.7 Classifier: Programming Language :: Python :: 3.8 Classifier: Programming Language :: Python :: 3.9 Classifier: Programming Language :: Python :: 3.10 Classifier: Programming Language :: Python :: Implementation Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: Jython Classifier: Programming Language :: Python :: Implementation :: PyPy Classifier: Operating System :: OS Independent Classifier: Topic :: Database :: Front-Ends Classifier: Topic :: Software Development :: Libraries :: Python Modules Requires-Python: >=3.6 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636798860.0 pg8000-1.23.0/pg8000.egg-info/SOURCES.txt0000664000175000017500000000421200000000000017575 0ustar00tlocketlocke00000000000000LICENSE MANIFEST.in README.adoc setup.cfg setup.py versioneer.py pg8000/__init__.py pg8000/_version.py pg8000/converters.py pg8000/core.py pg8000/dbapi.py pg8000/exceptions.py pg8000/legacy.py pg8000/native.py pg8000.egg-info/PKG-INFO pg8000.egg-info/SOURCES.txt pg8000.egg-info/dependency_links.txt pg8000.egg-info/requires.txt pg8000.egg-info/top_level.txt test/__init__.py test/dbapi/__init__.py test/dbapi/conftest.py test/dbapi/test_auth.py test/dbapi/test_benchmarks.py test/dbapi/test_connection.py test/dbapi/test_copy.py test/dbapi/test_dbapi.py test/dbapi/test_dbapi20.py test/dbapi/test_paramstyle.py test/dbapi/test_query.py test/dbapi/test_typeconversion.py test/dbapi/github-actions/__init__.py test/dbapi/github-actions/gss_dbapi.py test/dbapi/github-actions/md5_dbapi.py test/dbapi/github-actions/password_dbapi.py test/dbapi/github-actions/scram-sha-256_dbapi.py test/dbapi/github-actions/ssl_dbapi.py test/legacy/__init__.py test/legacy/conftest.py test/legacy/stress.py test/legacy/test_auth.py test/legacy/test_benchmarks.py test/legacy/test_connection.py test/legacy/test_copy.py test/legacy/test_dbapi.py test/legacy/test_dbapi20.py test/legacy/test_error_recovery.py test/legacy/test_paramstyle.py test/legacy/test_prepared_statement.py test/legacy/test_query.py test/legacy/test_typeconversion.py test/legacy/test_typeobjects.py test/legacy/github-actions/__init__.py test/legacy/github-actions/gss_legacy.py test/legacy/github-actions/md5_legacy.py test/legacy/github-actions/password_legacy.py test/legacy/github-actions/scram-sha-256_legacy.py test/legacy/github-actions/ssl_legacy.py test/native/__init__.py test/native/conftest.py test/native/test_auth.py test/native/test_benchmarks.py test/native/test_connection.py test/native/test_converters.py test/native/test_copy.py test/native/test_core.py test/native/test_prepared_statement.py test/native/test_query.py test/native/test_typeconversion.py test/native/github-actions/__init__.py test/native/github-actions/gss_native.py test/native/github-actions/md5_native.py test/native/github-actions/password_native.py test/native/github-actions/scram-sha-256_native.py test/native/github-actions/ssl_native.py././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636798860.0 pg8000-1.23.0/pg8000.egg-info/dependency_links.txt0000664000175000017500000000000100000000000021760 0ustar00tlocketlocke00000000000000 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636798860.0 pg8000-1.23.0/pg8000.egg-info/requires.txt0000664000175000017500000000001600000000000020307 0ustar00tlocketlocke00000000000000scramp>=1.4.1 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636798860.0 pg8000-1.23.0/pg8000.egg-info/top_level.txt0000664000175000017500000000000700000000000020441 0ustar00tlocketlocke00000000000000pg8000 ././@PaxHeader0000000000000000000000000000003300000000000011451 xustar000000000000000027 mtime=1636798860.416994 pg8000-1.23.0/setup.cfg0000664000175000017500000000124200000000000015122 0ustar00tlocketlocke00000000000000[versioneer] vcs = git style = pep440 versionfile_source = pg8000/_version.py versionfile_build = pg8000/_version.py tag_prefix = parentdir_prefix = pg8000- [tox:tox] [testenv] passenv = PGPORT commands = black --check . flake8 . python -m pytest -x test python setup.py check deps = pytest pytest-mock pytest-benchmark black flake8 flake8-alphabetize pytz [testenv:py38] setenv = USER = postgres commands_post = python -m doctest -o ELLIPSIS README.adoc [flake8] application-names = pg8000 ignore = E203,W503 max-line-length = 88 exclude = .git,__pycache__,build,dist,venv,.tox application-import-names = pg8000 [egg_info] tag_build = tag_date = 0 ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1635603162.0 pg8000-1.23.0/setup.py0000664000175000017500000000402100000000000015011 0ustar00tlocketlocke00000000000000#!/usr/bin/env python from setuptools import setup import versioneer long_description = """\ pg8000 ------ pg8000 is a Pure-Python interface to the PostgreSQL database engine. It is \ one of many PostgreSQL interfaces for the Python programming language. pg8000 \ is somewhat distinctive in that it is written entirely in Python and does not \ rely on any external libraries (such as a compiled python module, or \ PostgreSQL's libpq library). pg8000 supports the standard Python DB-API \ version 2.0. pg8000's name comes from the belief that it is probably about the 8000th \ PostgreSQL interface for Python.""" cmdclass = dict(versioneer.get_cmdclass()) version = versioneer.get_version() setup( name="pg8000", version=version, cmdclass=cmdclass, description="PostgreSQL interface library", long_description=long_description, author="Mathieu Fenniak", author_email="biziqe@mathieu.fenniak.net", url="https://github.com/tlocke/pg8000", license="BSD", python_requires=">=3.6", install_requires=["scramp>=1.4.1"], classifiers=[ "Development Status :: 5 - Production/Stable", "Intended Audience :: Developers", "License :: OSI Approved :: BSD License", "Programming Language :: Python", "Programming Language :: Python :: 3", "Programming Language :: Python :: 3.6", "Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.10", "Programming Language :: Python :: Implementation", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: Jython", "Programming Language :: Python :: Implementation :: PyPy", "Operating System :: OS Independent", "Topic :: Database :: Front-Ends", "Topic :: Software Development :: Libraries :: Python Modules", ], keywords="postgresql dbapi", packages=("pg8000",), ) ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1636798860.4009943 pg8000-1.23.0/test/0000775000175000017500000000000000000000000014261 5ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/__init__.py0000664000175000017500000000000000000000000016360 0ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1636798860.4049942 pg8000-1.23.0/test/dbapi/0000775000175000017500000000000000000000000015340 5ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/dbapi/__init__.py0000664000175000017500000000000000000000000017437 0ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/dbapi/conftest.py0000664000175000017500000000200400000000000017533 0ustar00tlocketlocke00000000000000import sys from os import environ import pytest import pg8000.dbapi @pytest.fixture(scope="class") def db_kwargs(): db_connect = {"user": "postgres", "password": "pw"} for kw, var, f in [ ("host", "PGHOST", str), ("password", "PGPASSWORD", str), ("port", "PGPORT", int), ]: try: db_connect[kw] = f(environ[var]) except KeyError: pass return db_connect @pytest.fixture def con(request, db_kwargs): conn = pg8000.dbapi.connect(**db_kwargs) def fin(): try: conn.rollback() except pg8000.dbapi.InterfaceError: pass try: conn.close() except pg8000.dbapi.InterfaceError: pass request.addfinalizer(fin) return conn @pytest.fixture def cursor(request, con): cursor = con.cursor() def fin(): cursor.close() request.addfinalizer(fin) return cursor @pytest.fixture def is_java(): return "java" in sys.platform.lower() ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1636798860.4049942 pg8000-1.23.0/test/dbapi/github-actions/0000775000175000017500000000000000000000000020260 5ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/dbapi/github-actions/__init__.py0000664000175000017500000000000000000000000022357 0ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/dbapi/github-actions/gss_dbapi.py0000664000175000017500000000054300000000000022567 0ustar00tlocketlocke00000000000000import pytest import pg8000.dbapi def test_gss(db_kwargs): """Called by GitHub Actions with auth method gss.""" # Should raise an exception saying gss isn't supported with pytest.raises( pg8000.dbapi.InterfaceError, match="Authentication method 7 not supported by pg8000.", ): pg8000.dbapi.connect(**db_kwargs) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/dbapi/github-actions/md5_dbapi.py0000664000175000017500000000022200000000000022452 0ustar00tlocketlocke00000000000000def test_md5(con): """Called by GitHub Actions with auth method md5. We just need to check that we can get a connection. """ pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/dbapi/github-actions/password_dbapi.py0000664000175000017500000000023400000000000023632 0ustar00tlocketlocke00000000000000def test_password(con): """Called by GitHub Actions with auth method password. We just need to check that we can get a connection. """ pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/dbapi/github-actions/scram-sha-256_dbapi.py0000664000175000017500000000024600000000000024163 0ustar00tlocketlocke00000000000000def test_scram_sha_256(con): """Called by GitHub Actions with auth method scram-sha-256. We just need to check that we can get a connection. """ pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/dbapi/github-actions/ssl_dbapi.py0000664000175000017500000000335400000000000022577 0ustar00tlocketlocke00000000000000import ssl import sys import pytest import pg8000.dbapi # Check if running in Jython if "java" in sys.platform: from javax.net.ssl import TrustManager, X509TrustManager from jarray import array from javax.net.ssl import SSLContext class TrustAllX509TrustManager(X509TrustManager): """Define a custom TrustManager which will blindly accept all certificates""" def checkClientTrusted(self, chain, auth): pass def checkServerTrusted(self, chain, auth): pass def getAcceptedIssuers(self): return None # Create a static reference to an SSLContext which will use # our custom TrustManager trust_managers = array([TrustAllX509TrustManager()], TrustManager) TRUST_ALL_CONTEXT = SSLContext.getInstance("SSL") TRUST_ALL_CONTEXT.init(None, trust_managers, None) # Keep a static reference to the JVM's default SSLContext for restoring # at a later time DEFAULT_CONTEXT = SSLContext.getDefault() @pytest.fixture def trust_all_certificates(request): """Decorator function that will make it so the context of the decorated method will run with our TrustManager that accepts all certificates""" # Only do this if running under Jython is_java = "java" in sys.platform if is_java: from javax.net.ssl import SSLContext SSLContext.setDefault(TRUST_ALL_CONTEXT) def fin(): if is_java: SSLContext.setDefault(DEFAULT_CONTEXT) request.addfinalizer(fin) @pytest.mark.usefixtures("trust_all_certificates") def test_ssl(db_kwargs): context = ssl.SSLContext() context.check_hostname = False db_kwargs["ssl_context"] = context with pg8000.dbapi.connect(**db_kwargs): pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627649365.0 pg8000-1.23.0/test/dbapi/test_auth.py0000664000175000017500000000266400000000000017722 0ustar00tlocketlocke00000000000000import pytest import pg8000.dbapi # This requires a line in pg_hba.conf that requires md5 for the database # pg8000_md5 def testMd5(db_kwargs): db_kwargs["database"] = "pg8000_md5" # Should only raise an exception saying db doesn't exist with pytest.raises(pg8000.dbapi.DatabaseError, match="3D000"): pg8000.dbapi.connect(**db_kwargs) # This requires a line in pg_hba.conf that requires gss for the database # pg8000_gss def testGss(db_kwargs): db_kwargs["database"] = "pg8000_gss" # Should raise an exception saying gss isn't supported with pytest.raises( pg8000.dbapi.InterfaceError, match="Authentication method 7 not supported by pg8000.", ): pg8000.dbapi.connect(**db_kwargs) # This requires a line in pg_hba.conf that requires 'password' for the # database pg8000_password def testPassword(db_kwargs): db_kwargs["database"] = "pg8000_password" # Should only raise an exception saying db doesn't exist with pytest.raises(pg8000.dbapi.DatabaseError, match="3D000"): pg8000.dbapi.connect(**db_kwargs) # This requires a line in pg_hba.conf that requires scram-sha-256 for the # database scram-sha-256 def test_scram_sha_256(db_kwargs): db_kwargs["database"] = "pg8000_scram_sha_256" # Should only raise an exception saying db doesn't exist with pytest.raises(pg8000.dbapi.DatabaseError, match="3D000"): pg8000.dbapi.connect(**db_kwargs) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636797493.0 pg8000-1.23.0/test/dbapi/test_benchmarks.py0000664000175000017500000000130400000000000021064 0ustar00tlocketlocke00000000000000import pytest @pytest.mark.parametrize( "txt", ( ("int2", "cast(id / 100 as int2)"), "cast(id as int4)", "cast(id * 100 as int8)", "(id % 2) = 0", "N'Static text string'", "cast(id / 100 as float4)", "cast(id / 100 as float8)", "cast(id / 100 as numeric)", "timestamp '2001-09-28'", ), ) def test_round_trips(con, benchmark, txt): def torun(): query = f"""SELECT {txt}, {txt}, {txt}, {txt}, {txt}, {txt}, {txt} FROM (SELECT generate_series(1, 10000) AS id) AS tbl""" cursor = con.cursor() cursor.execute(query) cursor.fetchall() cursor.close() benchmark(torun) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1634149967.0 pg8000-1.23.0/test/dbapi/test_connection.py0000664000175000017500000001375700000000000021125 0ustar00tlocketlocke00000000000000import datetime import warnings import pytest import pg8000 def test_unix_socket_missing(): conn_params = {"unix_sock": "/file-does-not-exist", "user": "doesn't-matter"} with pytest.raises(pg8000.dbapi.InterfaceError): pg8000.dbapi.connect(**conn_params) def test_internet_socket_connection_refused(): conn_params = {"port": 0, "user": "doesn't-matter"} with pytest.raises( pg8000.dbapi.InterfaceError, match="Can't create a connection to host localhost and port 0 " "\\(timeout is None and source_address is None\\).", ): pg8000.dbapi.connect(**conn_params) def test_database_missing(db_kwargs): db_kwargs["database"] = "missing-db" with pytest.raises(pg8000.dbapi.DatabaseError): pg8000.dbapi.connect(**db_kwargs) def test_database_name_unicode(db_kwargs): db_kwargs["database"] = "pg8000_sn\uFF6Fw" # Should only raise an exception saying db doesn't exist with pytest.raises(pg8000.dbapi.DatabaseError, match="3D000"): pg8000.dbapi.connect(**db_kwargs) def test_database_name_bytes(db_kwargs): """Should only raise an exception saying db doesn't exist""" db_kwargs["database"] = bytes("pg8000_sn\uFF6Fw", "utf8") with pytest.raises(pg8000.dbapi.DatabaseError, match="3D000"): pg8000.dbapi.connect(**db_kwargs) def test_password_bytes(con, db_kwargs): # Create user username = "boltzmann" password = "cha\uFF6Fs" cur = con.cursor() cur.execute("create user " + username + " with password '" + password + "';") con.commit() db_kwargs["user"] = username db_kwargs["password"] = password.encode("utf8") db_kwargs["database"] = "pg8000_md5" with pytest.raises(pg8000.dbapi.DatabaseError, match="3D000"): pg8000.dbapi.connect(**db_kwargs) cur.execute("drop role " + username) con.commit() def test_application_name(db_kwargs): app_name = "my test application name" db_kwargs["application_name"] = app_name with pg8000.dbapi.connect(**db_kwargs) as db: cur = db.cursor() cur.execute( "select application_name from pg_stat_activity " " where pid = pg_backend_pid()" ) application_name = cur.fetchone()[0] assert application_name == app_name def test_application_name_integer(db_kwargs): db_kwargs["application_name"] = 1 with pytest.raises( pg8000.dbapi.InterfaceError, match="The parameter application_name can't be of type " ".", ): pg8000.dbapi.connect(**db_kwargs) def test_application_name_bytearray(db_kwargs): db_kwargs["application_name"] = bytearray(b"Philby") pg8000.dbapi.connect(**db_kwargs) def test_notify(con): cursor = con.cursor() cursor.execute("select pg_backend_pid()") backend_pid = cursor.fetchall()[0][0] assert list(con.notifications) == [] cursor.execute("LISTEN test") cursor.execute("NOTIFY test") con.commit() cursor.execute("VALUES (1, 2), (3, 4), (5, 6)") assert len(con.notifications) == 1 assert con.notifications[0] == (backend_pid, "test", "") def test_notify_with_payload(con): cursor = con.cursor() cursor.execute("select pg_backend_pid()") backend_pid = cursor.fetchall()[0][0] assert list(con.notifications) == [] cursor.execute("LISTEN test") cursor.execute("NOTIFY test, 'Parnham'") con.commit() cursor.execute("VALUES (1, 2), (3, 4), (5, 6)") assert len(con.notifications) == 1 assert con.notifications[0] == (backend_pid, "test", "Parnham") def test_broken_pipe_read(con, db_kwargs): db1 = pg8000.dbapi.connect(**db_kwargs) cur1 = db1.cursor() cur2 = con.cursor() cur1.execute("select pg_backend_pid()") pid1 = cur1.fetchone()[0] cur2.execute("select pg_terminate_backend(%s)", (pid1,)) with pytest.raises(pg8000.dbapi.InterfaceError, match="network error on read"): cur1.execute("select 1") def test_broken_pipe_flush(con, db_kwargs): db1 = pg8000.dbapi.connect(**db_kwargs) cur1 = db1.cursor() cur2 = con.cursor() cur1.execute("select pg_backend_pid()") pid1 = cur1.fetchone()[0] cur2.execute("select pg_terminate_backend(%s)", (pid1,)) try: cur1.execute("select 1") except BaseException: pass # Sometimes raises and sometime doesn't try: db1.close() except pg8000.exceptions.InterfaceError as e: assert str(e) == "network error on flush" def test_broken_pipe_unpack(con): cur = con.cursor() cur.execute("select pg_backend_pid()") pid1 = cur.fetchone()[0] with pytest.raises(pg8000.dbapi.InterfaceError, match="network error"): cur.execute("select pg_terminate_backend(%s)", (pid1,)) def test_py_value_fail(con, mocker): # Ensure that if types.py_value throws an exception, the original # exception is raised (PG8000TestException), and the connection is # still usable after the error. class PG8000TestException(Exception): pass def raise_exception(val): raise PG8000TestException("oh noes!") mocker.patch.object(con, "py_types") con.py_types = {datetime.time: raise_exception} with pytest.raises(PG8000TestException): c = con.cursor() c.execute("SELECT CAST(%s AS TIME) AS f1", (datetime.time(10, 30),)) c.fetchall() # ensure that the connection is still usable for a new query c.execute("VALUES ('hw3'::text)") assert c.fetchone()[0] == "hw3" def test_no_data_error_recovery(con): for i in range(1, 4): with pytest.raises(pg8000.DatabaseError) as e: c = con.cursor() c.execute("DROP TABLE t1") assert e.value.args[0]["C"] == "42P01" con.rollback() def test_closed_connection(db_kwargs): warnings.simplefilter("ignore") my_db = pg8000.connect(**db_kwargs) cursor = my_db.cursor() my_db.close() with pytest.raises(my_db.InterfaceError, match="connection is closed"): cursor.execute("VALUES ('hw1'::text)") warnings.resetwarnings() ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/dbapi/test_copy.py0000664000175000017500000000513100000000000017723 0ustar00tlocketlocke00000000000000from io import BytesIO import pytest @pytest.fixture def db_table(request, con): cursor = con.cursor() cursor.execute( "CREATE TEMPORARY TABLE t1 (f1 int primary key, " "f2 int not null, f3 varchar(50) null) on commit drop" ) return con def test_copy_to_with_table(db_table): cursor = db_table.cursor() cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, 1)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (2, 2, 2)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (3, 3, 3)) stream = BytesIO() cursor.execute("copy t1 to stdout", stream=stream) assert stream.getvalue() == b"1\t1\t1\n2\t2\t2\n3\t3\t3\n" assert cursor.rowcount == 3 def test_copy_to_with_query(db_table): cursor = db_table.cursor() stream = BytesIO() cursor.execute( "COPY (SELECT 1 as One, 2 as Two) TO STDOUT WITH DELIMITER " "'X' CSV HEADER QUOTE AS 'Y' FORCE QUOTE Two", stream=stream, ) assert stream.getvalue() == b"oneXtwo\n1XY2Y\n" assert cursor.rowcount == 1 def test_copy_from_with_table(db_table): cursor = db_table.cursor() stream = BytesIO(b"1\t1\t1\n2\t2\t2\n3\t3\t3\n") cursor.execute("copy t1 from STDIN", stream=stream) assert cursor.rowcount == 3 cursor.execute("SELECT * FROM t1 ORDER BY f1") retval = cursor.fetchall() assert retval == ([1, 1, "1"], [2, 2, "2"], [3, 3, "3"]) def test_copy_from_with_query(db_table): cursor = db_table.cursor() stream = BytesIO(b"f1Xf2\n1XY1Y\n") cursor.execute( "COPY t1 (f1, f2) FROM STDIN WITH DELIMITER 'X' CSV HEADER " "QUOTE AS 'Y' FORCE NOT NULL f1", stream=stream, ) assert cursor.rowcount == 1 cursor.execute("SELECT * FROM t1 ORDER BY f1") retval = cursor.fetchall() assert retval == ([1, 1, None],) def test_copy_from_with_error(db_table): cursor = db_table.cursor() stream = BytesIO(b"f1Xf2\n\n1XY1Y\n") with pytest.raises(BaseException) as e: cursor.execute( "COPY t1 (f1, f2) FROM STDIN WITH DELIMITER 'X' CSV HEADER " "QUOTE AS 'Y' FORCE NOT NULL f1", stream=stream, ) arg = { "S": ("ERROR",), "C": ("22P02",), "M": ( 'invalid input syntax for type integer: ""', 'invalid input syntax for integer: ""', ), "W": ('COPY t1, line 2, column f1: ""',), "F": ("numutils.c",), "R": ("pg_atoi", "pg_strtoint32"), } earg = e.value.args[0] for k, v in arg.items(): assert earg[k] in v ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/dbapi/test_dbapi.py0000664000175000017500000001224400000000000020033 0ustar00tlocketlocke00000000000000import datetime import os import time import pytest import pg8000 @pytest.fixture def has_tzset(): # Neither Windows nor Jython 2.5.3 have a time.tzset() so skip if hasattr(time, "tzset"): os.environ["TZ"] = "UTC" time.tzset() return True return False # DBAPI compatible interface tests @pytest.fixture def db_table(con, has_tzset): c = con.cursor() c.execute( "CREATE TEMPORARY TABLE t1 " "(f1 int primary key, f2 int not null, f3 varchar(50) null) " "ON COMMIT DROP" ) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, None)) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (2, 10, None)) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (3, 100, None)) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (4, 1000, None)) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (5, 10000, None)) return con def test_parallel_queries(db_table): c1 = db_table.cursor() c2 = db_table.cursor() c1.execute("SELECT f1, f2, f3 FROM t1") while 1: row = c1.fetchone() if row is None: break f1, f2, f3 = row c2.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > %s", (f1,)) while 1: row = c2.fetchone() if row is None: break f1, f2, f3 = row def test_qmark(mocker, db_table): mocker.patch("pg8000.dbapi.paramstyle", "qmark") c1 = db_table.cursor() c1.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > ?", (3,)) while 1: row = c1.fetchone() if row is None: break f1, f2, f3 = row def test_numeric(mocker, db_table): mocker.patch("pg8000.dbapi.paramstyle", "numeric") c1 = db_table.cursor() c1.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > :1", (3,)) while 1: row = c1.fetchone() if row is None: break f1, f2, f3 = row def test_named(mocker, db_table): mocker.patch("pg8000.dbapi.paramstyle", "named") c1 = db_table.cursor() c1.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > :f1", {"f1": 3}) while 1: row = c1.fetchone() if row is None: break f1, f2, f3 = row def test_format(mocker, db_table): mocker.patch("pg8000.dbapi.paramstyle", "format") c1 = db_table.cursor() c1.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > %s", (3,)) while 1: row = c1.fetchone() if row is None: break f1, f2, f3 = row def test_pyformat(mocker, db_table): mocker.patch("pg8000.dbapi.paramstyle", "pyformat") c1 = db_table.cursor() c1.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > %(f1)s", {"f1": 3}) while 1: row = c1.fetchone() if row is None: break f1, f2, f3 = row def test_arraysize(db_table): c1 = db_table.cursor() c1.arraysize = 3 c1.execute("SELECT * FROM t1") retval = c1.fetchmany() assert len(retval) == c1.arraysize def test_date(): val = pg8000.Date(2001, 2, 3) assert val == datetime.date(2001, 2, 3) def test_time(): val = pg8000.Time(4, 5, 6) assert val == datetime.time(4, 5, 6) def test_timestamp(): val = pg8000.Timestamp(2001, 2, 3, 4, 5, 6) assert val == datetime.datetime(2001, 2, 3, 4, 5, 6) def test_date_from_ticks(has_tzset): if has_tzset: val = pg8000.DateFromTicks(1173804319) assert val == datetime.date(2007, 3, 13) def testTimeFromTicks(has_tzset): if has_tzset: val = pg8000.TimeFromTicks(1173804319) assert val == datetime.time(16, 45, 19) def test_timestamp_from_ticks(has_tzset): if has_tzset: val = pg8000.TimestampFromTicks(1173804319) assert val == datetime.datetime(2007, 3, 13, 16, 45, 19) def test_binary(): v = pg8000.Binary(b"\x00\x01\x02\x03\x02\x01\x00") assert v == b"\x00\x01\x02\x03\x02\x01\x00" assert isinstance(v, pg8000.BINARY) def test_row_count(db_table): c1 = db_table.cursor() c1.execute("SELECT * FROM t1") assert 5 == c1.rowcount c1.execute("UPDATE t1 SET f3 = %s WHERE f2 > 101", ("Hello!",)) assert 2 == c1.rowcount c1.execute("DELETE FROM t1") assert 5 == c1.rowcount def test_fetch_many(db_table): cursor = db_table.cursor() cursor.arraysize = 2 cursor.execute("SELECT * FROM t1") assert 2 == len(cursor.fetchmany()) assert 2 == len(cursor.fetchmany()) assert 1 == len(cursor.fetchmany()) assert 0 == len(cursor.fetchmany()) def test_iterator(db_table): cursor = db_table.cursor() cursor.execute("SELECT * FROM t1 ORDER BY f1") f1 = 0 for row in cursor.fetchall(): next_f1 = row[0] assert next_f1 > f1 f1 = next_f1 # Vacuum can't be run inside a transaction, so we need to turn # autocommit on. def test_vacuum(con): con.autocommit = True cursor = con.cursor() cursor.execute("vacuum") def test_prepared_statement(con): cursor = con.cursor() cursor.execute("PREPARE gen_series AS SELECT generate_series(1, 10);") cursor.execute("EXECUTE gen_series") def test_cursor_type(cursor): assert str(type(cursor)) == "" ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/dbapi/test_dbapi20.py0000664000175000017500000005256700000000000020211 0ustar00tlocketlocke00000000000000import time import warnings import pytest import pg8000 """ Python DB API 2.0 driver compliance unit test suite. This software is Public Domain and may be used without restrictions. "Now we have booze and barflies entering the discussion, plus rumours of DBAs on drugs... and I won't tell you what flashes through my mind each time I read the subject line with 'Anal Compliance' in it. All around this is turning out to be a thoroughly unwholesome unit test." -- Ian Bicking """ __rcs_id__ = "$Id: dbapi20.py,v 1.10 2003/10/09 03:14:14 zenzen Exp $" __version__ = "$Revision: 1.10 $"[11:-2] __author__ = "Stuart Bishop " # $Log: dbapi20.py,v $ # Revision 1.10 2003/10/09 03:14:14 zenzen # Add test for DB API 2.0 optional extension, where database exceptions # are exposed as attributes on the Connection object. # # Revision 1.9 2003/08/13 01:16:36 zenzen # Minor tweak from Stefan Fleiter # # Revision 1.8 2003/04/10 00:13:25 zenzen # Changes, as per suggestions by M.-A. Lemburg # - Add a table prefix, to ensure namespace collisions can always be avoided # # Revision 1.7 2003/02/26 23:33:37 zenzen # Break out DDL into helper functions, as per request by David Rushby # # Revision 1.6 2003/02/21 03:04:33 zenzen # Stuff from Henrik Ekelund: # added test_None # added test_nextset & hooks # # Revision 1.5 2003/02/17 22:08:43 zenzen # Implement suggestions and code from Henrik Eklund - test that # cursor.arraysize defaults to 1 & generic cursor.callproc test added # # Revision 1.4 2003/02/15 00:16:33 zenzen # Changes, as per suggestions and bug reports by M.-A. Lemburg, # Matthew T. Kromer, Federico Di Gregorio and Daniel Dittmar # - Class renamed # - Now a subclass of TestCase, to avoid requiring the driver stub # to use multiple inheritance # - Reversed the polarity of buggy test in test_description # - Test exception heirarchy correctly # - self.populate is now self._populate(), so if a driver stub # overrides self.ddl1 this change propogates # - VARCHAR columns now have a width, which will hopefully make the # DDL even more portible (this will be reversed if it causes more problems) # - cursor.rowcount being checked after various execute and fetchXXX methods # - Check for fetchall and fetchmany returning empty lists after results # are exhausted (already checking for empty lists if select retrieved # nothing # - Fix bugs in test_setoutputsize_basic and test_setinputsizes # """ Test a database self.driver for DB API 2.0 compatibility. This implementation tests Gadfly, but the TestCase is structured so that other self.drivers can subclass this test case to ensure compiliance with the DB-API. It is expected that this TestCase may be expanded in the future if ambiguities or edge conditions are discovered. The 'Optional Extensions' are not yet being tested. self.drivers should subclass this test, overriding setUp, tearDown, self.driver, connect_args and connect_kw_args. Class specification should be as follows: import dbapi20 class mytest(dbapi20.DatabaseAPI20Test): [...] Don't 'import DatabaseAPI20Test from dbapi20', or you will confuse the unit tester - just 'import dbapi20'. """ # The self.driver module. This should be the module where the 'connect' # method is to be found driver = pg8000 table_prefix = "dbapi20test_" # If you need to specify a prefix for tables ddl1 = "create table %sbooze (name varchar(20))" % table_prefix ddl2 = "create table %sbarflys (name varchar(20))" % table_prefix xddl1 = "drop table %sbooze" % table_prefix xddl2 = "drop table %sbarflys" % table_prefix # Name of stored procedure to convert # string->lowercase lowerfunc = "lower" # Some drivers may need to override these helpers, for example adding # a 'commit' after the execute. def executeDDL1(cursor): cursor.execute(ddl1) def executeDDL2(cursor): cursor.execute(ddl2) @pytest.fixture def db(request, con): def fin(): with con.cursor() as cur: for ddl in (xddl1, xddl2): try: cur.execute(ddl) con.commit() except driver.Error: # Assume table didn't exist. Other tests will check if # execute is busted. pass request.addfinalizer(fin) return con def test_apilevel(): # Must exist apilevel = driver.apilevel # Must equal 2.0 assert apilevel == "2.0" def test_threadsafety(): try: # Must exist threadsafety = driver.threadsafety # Must be a valid value assert threadsafety in (0, 1, 2, 3) except AttributeError: assert False, "Driver doesn't define threadsafety" def test_paramstyle(): try: # Must exist paramstyle = driver.paramstyle # Must be a valid value assert paramstyle in ("qmark", "numeric", "named", "format", "pyformat") except AttributeError: assert False, "Driver doesn't define paramstyle" def test_Exceptions(): # Make sure required exceptions exist, and are in the # defined heirarchy. assert issubclass(driver.Warning, Exception) assert issubclass(driver.Error, Exception) assert issubclass(driver.InterfaceError, driver.Error) assert issubclass(driver.DatabaseError, driver.Error) assert issubclass(driver.OperationalError, driver.Error) assert issubclass(driver.IntegrityError, driver.Error) assert issubclass(driver.InternalError, driver.Error) assert issubclass(driver.ProgrammingError, driver.Error) assert issubclass(driver.NotSupportedError, driver.Error) def test_ExceptionsAsConnectionAttributes(con): # OPTIONAL EXTENSION # Test for the optional DB API 2.0 extension, where the exceptions # are exposed as attributes on the Connection object # I figure this optional extension will be implemented by any # driver author who is using this test suite, so it is enabled # by default. warnings.simplefilter("ignore") drv = driver assert con.Warning is drv.Warning assert con.Error is drv.Error assert con.InterfaceError is drv.InterfaceError assert con.DatabaseError is drv.DatabaseError assert con.OperationalError is drv.OperationalError assert con.IntegrityError is drv.IntegrityError assert con.InternalError is drv.InternalError assert con.ProgrammingError is drv.ProgrammingError assert con.NotSupportedError is drv.NotSupportedError warnings.resetwarnings() def test_commit(con): # Commit must work, even if it doesn't do anything con.commit() def test_rollback(con): # If rollback is defined, it should either work or throw # the documented exception if hasattr(con, "rollback"): try: con.rollback() except driver.NotSupportedError: pass def test_cursor(con): con.cursor() def test_cursor_isolation(con): # Make sure cursors created from the same connection have # the documented transaction isolation level cur1 = con.cursor() cur2 = con.cursor() executeDDL1(cur1) cur1.execute("insert into %sbooze values ('Victoria Bitter')" % (table_prefix)) cur2.execute("select name from %sbooze" % table_prefix) booze = cur2.fetchall() assert len(booze) == 1 assert len(booze[0]) == 1 assert booze[0][0] == "Victoria Bitter" def test_description(con): cur = con.cursor() executeDDL1(cur) assert cur.description is None, ( "cursor.description should be none after executing a " "statement that can return no rows (such as DDL)" ) cur.execute("select name from %sbooze" % table_prefix) assert len(cur.description) == 1, "cursor.description describes too many columns" assert ( len(cur.description[0]) == 7 ), "cursor.description[x] tuples must have 7 elements" assert ( cur.description[0][0].lower() == "name" ), "cursor.description[x][0] must return column name" assert cur.description[0][1] == driver.STRING, ( "cursor.description[x][1] must return column type. Got %r" % cur.description[0][1] ) # Make sure self.description gets reset executeDDL2(cur) assert cur.description is None, ( "cursor.description not being set to None when executing " "no-result statements (eg. DDL)" ) def test_rowcount(cursor): executeDDL1(cursor) assert cursor.rowcount == -1, ( "cursor.rowcount should be -1 after executing no-result " "statements" ) cursor.execute("insert into %sbooze values ('Victoria Bitter')" % (table_prefix)) assert cursor.rowcount in (-1, 1), ( "cursor.rowcount should == number or rows inserted, or " "set to -1 after executing an insert statement" ) cursor.execute("select name from %sbooze" % table_prefix) assert cursor.rowcount in (-1, 1), ( "cursor.rowcount should == number of rows returned, or " "set to -1 after executing a select statement" ) executeDDL2(cursor) assert cursor.rowcount == -1, ( "cursor.rowcount not being reset to -1 after executing " "no-result statements" ) def test_close(con): cur = con.cursor() con.close() # cursor.execute should raise an Error if called after connection # closed with pytest.raises(driver.Error): executeDDL1(cur) # connection.commit should raise an Error if called after connection' # closed.' with pytest.raises(driver.Error): con.commit() # connection.close should raise an Error if called more than once with pytest.raises(driver.Error): con.close() def test_execute(con): cur = con.cursor() _paraminsert(cur) def _paraminsert(cur): executeDDL1(cur) cur.execute("insert into %sbooze values ('Victoria Bitter')" % (table_prefix)) assert cur.rowcount in (-1, 1) if driver.paramstyle == "qmark": cur.execute("insert into %sbooze values (?)" % table_prefix, ("Cooper's",)) elif driver.paramstyle == "numeric": cur.execute("insert into %sbooze values (:1)" % table_prefix, ("Cooper's",)) elif driver.paramstyle == "named": cur.execute( "insert into %sbooze values (:beer)" % table_prefix, {"beer": "Cooper's"} ) elif driver.paramstyle == "format": cur.execute("insert into %sbooze values (%%s)" % table_prefix, ("Cooper's",)) elif driver.paramstyle == "pyformat": cur.execute( "insert into %sbooze values (%%(beer)s)" % table_prefix, {"beer": "Cooper's"}, ) else: assert False, "Invalid paramstyle" assert cur.rowcount in (-1, 1) cur.execute("select name from %sbooze" % table_prefix) res = cur.fetchall() assert len(res) == 2, "cursor.fetchall returned too few rows" beers = [res[0][0], res[1][0]] beers.sort() assert beers[0] == "Cooper's", ( "cursor.fetchall retrieved incorrect data, or data inserted " "incorrectly" ) assert beers[1] == "Victoria Bitter", ( "cursor.fetchall retrieved incorrect data, or data inserted " "incorrectly" ) def test_executemany(cursor): executeDDL1(cursor) largs = [("Cooper's",), ("Boag's",)] margs = [{"beer": "Cooper's"}, {"beer": "Boag's"}] if driver.paramstyle == "qmark": cursor.executemany("insert into %sbooze values (?)" % table_prefix, largs) elif driver.paramstyle == "numeric": cursor.executemany("insert into %sbooze values (:1)" % table_prefix, largs) elif driver.paramstyle == "named": cursor.executemany("insert into %sbooze values (:beer)" % table_prefix, margs) elif driver.paramstyle == "format": cursor.executemany("insert into %sbooze values (%%s)" % table_prefix, largs) elif driver.paramstyle == "pyformat": cursor.executemany( "insert into %sbooze values (%%(beer)s)" % (table_prefix), margs ) else: assert False, "Unknown paramstyle" assert cursor.rowcount in (-1, 2), ( "insert using cursor.executemany set cursor.rowcount to " "incorrect value %r" % cursor.rowcount ) cursor.execute("select name from %sbooze" % table_prefix) res = cursor.fetchall() assert len(res) == 2, "cursor.fetchall retrieved incorrect number of rows" beers = [res[0][0], res[1][0]] beers.sort() assert beers[0] == "Boag's", "incorrect data retrieved" assert beers[1] == "Cooper's", "incorrect data retrieved" def test_fetchone(cursor): # cursor.fetchone should raise an Error if called before # executing a select-type query with pytest.raises(driver.Error): cursor.fetchone() # cursor.fetchone should raise an Error if called after # executing a query that cannnot return rows executeDDL1(cursor) with pytest.raises(driver.Error): cursor.fetchone() cursor.execute("select name from %sbooze" % table_prefix) assert cursor.fetchone() is None, ( "cursor.fetchone should return None if a query retrieves " "no rows" ) assert cursor.rowcount in (-1, 0) # cursor.fetchone should raise an Error if called after # executing a query that cannnot return rows cursor.execute("insert into %sbooze values ('Victoria Bitter')" % (table_prefix)) with pytest.raises(driver.Error): cursor.fetchone() cursor.execute("select name from %sbooze" % table_prefix) r = cursor.fetchone() assert len(r) == 1, "cursor.fetchone should have retrieved a single row" assert r[0] == "Victoria Bitter", "cursor.fetchone retrieved incorrect data" assert ( cursor.fetchone() is None ), "cursor.fetchone should return None if no more rows available" assert cursor.rowcount in (-1, 1) samples = [ "Carlton Cold", "Carlton Draft", "Mountain Goat", "Redback", "Victoria Bitter", "XXXX", ] def _populate(): """Return a list of sql commands to setup the DB for the fetch tests. """ populate = [ "insert into %sbooze values ('%s')" % (table_prefix, s) for s in samples ] return populate def test_fetchmany(cursor): # cursor.fetchmany should raise an Error if called without # issuing a query with pytest.raises(driver.Error): cursor.fetchmany(4) executeDDL1(cursor) for sql in _populate(): cursor.execute(sql) cursor.execute("select name from %sbooze" % table_prefix) r = cursor.fetchmany() assert len(r) == 1, ( "cursor.fetchmany retrieved incorrect number of rows, " "default of arraysize is one." ) cursor.arraysize = 10 r = cursor.fetchmany(3) # Should get 3 rows assert len(r) == 3, "cursor.fetchmany retrieved incorrect number of rows" r = cursor.fetchmany(4) # Should get 2 more assert len(r) == 2, "cursor.fetchmany retrieved incorrect number of rows" r = cursor.fetchmany(4) # Should be an empty sequence assert len(r) == 0, ( "cursor.fetchmany should return an empty sequence after " "results are exhausted" ) assert cursor.rowcount in (-1, 6) # Same as above, using cursor.arraysize cursor.arraysize = 4 cursor.execute("select name from %sbooze" % table_prefix) r = cursor.fetchmany() # Should get 4 rows assert len(r) == 4, "cursor.arraysize not being honoured by fetchmany" r = cursor.fetchmany() # Should get 2 more assert len(r) == 2 r = cursor.fetchmany() # Should be an empty sequence assert len(r) == 0 assert cursor.rowcount in (-1, 6) cursor.arraysize = 6 cursor.execute("select name from %sbooze" % table_prefix) rows = cursor.fetchmany() # Should get all rows assert cursor.rowcount in (-1, 6) assert len(rows) == 6 assert len(rows) == 6 rows = [row[0] for row in rows] rows.sort() # Make sure we get the right data back out for i in range(0, 6): assert rows[i] == samples[i], "incorrect data retrieved by cursor.fetchmany" rows = cursor.fetchmany() # Should return an empty list assert len(rows) == 0, ( "cursor.fetchmany should return an empty sequence if " "called after the whole result set has been fetched" ) assert cursor.rowcount in (-1, 6) executeDDL2(cursor) cursor.execute("select name from %sbarflys" % table_prefix) r = cursor.fetchmany() # Should get empty sequence assert len(r) == 0, ( "cursor.fetchmany should return an empty sequence if " "query retrieved no rows" ) assert cursor.rowcount in (-1, 0) def test_fetchall(cursor): # cursor.fetchall should raise an Error if called # without executing a query that may return rows (such # as a select) with pytest.raises(driver.Error): cursor.fetchall() executeDDL1(cursor) for sql in _populate(): cursor.execute(sql) # cursor.fetchall should raise an Error if called # after executing a a statement that cannot return rows with pytest.raises(driver.Error): cursor.fetchall() cursor.execute("select name from %sbooze" % table_prefix) rows = cursor.fetchall() assert cursor.rowcount in (-1, len(samples)) assert len(rows) == len(samples), "cursor.fetchall did not retrieve all rows" rows = [r[0] for r in rows] rows.sort() for i in range(0, len(samples)): assert rows[i] == samples[i], "cursor.fetchall retrieved incorrect rows" rows = cursor.fetchall() assert len(rows) == 0, ( "cursor.fetchall should return an empty list if called " "after the whole result set has been fetched" ) assert cursor.rowcount in (-1, len(samples)) executeDDL2(cursor) cursor.execute("select name from %sbarflys" % table_prefix) rows = cursor.fetchall() assert cursor.rowcount in (-1, 0) assert len(rows) == 0, ( "cursor.fetchall should return an empty list if " "a select query returns no rows" ) def test_mixedfetch(cursor): executeDDL1(cursor) for sql in _populate(): cursor.execute(sql) cursor.execute("select name from %sbooze" % table_prefix) rows1 = cursor.fetchone() rows23 = cursor.fetchmany(2) rows4 = cursor.fetchone() rows56 = cursor.fetchall() assert cursor.rowcount in (-1, 6) assert len(rows23) == 2, "fetchmany returned incorrect number of rows" assert len(rows56) == 2, "fetchall returned incorrect number of rows" rows = [rows1[0]] rows.extend([rows23[0][0], rows23[1][0]]) rows.append(rows4[0]) rows.extend([rows56[0][0], rows56[1][0]]) rows.sort() for i in range(0, len(samples)): assert rows[i] == samples[i], "incorrect data retrieved or inserted" def help_nextset_setUp(cur): """Should create a procedure called deleteme that returns two result sets, first the number of rows in booze then "name from booze" """ raise NotImplementedError("Helper not implemented") def help_nextset_tearDown(cur): "If cleaning up is needed after nextSetTest" raise NotImplementedError("Helper not implemented") def test_nextset(cursor): if not hasattr(cursor, "nextset"): return try: executeDDL1(cursor) sql = _populate() for sql in _populate(): cursor.execute(sql) help_nextset_setUp(cursor) cursor.callproc("deleteme") numberofrows = cursor.fetchone() assert numberofrows[0] == len(samples) assert cursor.nextset() names = cursor.fetchall() assert len(names) == len(samples) s = cursor.nextset() assert s is None, "No more return sets, should return None" finally: help_nextset_tearDown(cursor) def test_arraysize(cursor): # Not much here - rest of the tests for this are in test_fetchmany assert hasattr(cursor, "arraysize"), "cursor.arraysize must be defined" def test_setinputsizes(cursor): cursor.setinputsizes(25) def test_setoutputsize_basic(cursor): # Basic test is to make sure setoutputsize doesn't blow up cursor.setoutputsize(1000) cursor.setoutputsize(2000, 0) _paraminsert(cursor) # Make sure the cursor still works def test_None(cursor): executeDDL1(cursor) cursor.execute("insert into %sbooze values (NULL)" % table_prefix) cursor.execute("select name from %sbooze" % table_prefix) r = cursor.fetchall() assert len(r) == 1 assert len(r[0]) == 1 assert r[0][0] is None, "NULL value not returned as None" def test_Date(): driver.Date(2002, 12, 25) driver.DateFromTicks(time.mktime((2002, 12, 25, 0, 0, 0, 0, 0, 0))) # Can we assume this? API doesn't specify, but it seems implied # self.assertEqual(str(d1),str(d2)) def test_Time(): driver.Time(13, 45, 30) driver.TimeFromTicks(time.mktime((2001, 1, 1, 13, 45, 30, 0, 0, 0))) # Can we assume this? API doesn't specify, but it seems implied # self.assertEqual(str(t1),str(t2)) def test_Timestamp(): driver.Timestamp(2002, 12, 25, 13, 45, 30) driver.TimestampFromTicks(time.mktime((2002, 12, 25, 13, 45, 30, 0, 0, 0))) # Can we assume this? API doesn't specify, but it seems implied # self.assertEqual(str(t1),str(t2)) def test_Binary(): driver.Binary(b"Something") driver.Binary(b"") def test_STRING(): assert hasattr(driver, "STRING"), "module.STRING must be defined" def test_BINARY(): assert hasattr(driver, "BINARY"), "module.BINARY must be defined." def test_NUMBER(): assert hasattr(driver, "NUMBER"), "module.NUMBER must be defined." def test_DATETIME(): assert hasattr(driver, "DATETIME"), "module.DATETIME must be defined." def test_ROWID(): assert hasattr(driver, "ROWID"), "module.ROWID must be defined." ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636797849.0 pg8000-1.23.0/test/dbapi/test_paramstyle.py0000664000175000017500000000660600000000000021142 0ustar00tlocketlocke00000000000000import pytest from pg8000.dbapi import convert_paramstyle # "(id %% 2) = 0", @pytest.mark.parametrize( "query,statement", [ [ 'SELECT ?, ?, "field_?" FROM t ' "WHERE a='say ''what?''' AND b=? AND c=E'?\\'test\\'?'", 'SELECT $1, $2, "field_?" FROM t WHERE ' "a='say ''what?''' AND b=$3 AND c=E'?\\'test\\'?'", ], [ "SELECT ?, ?, * FROM t WHERE a=? AND b='are you ''sure?'", "SELECT $1, $2, * FROM t WHERE a=$3 AND b='are you ''sure?'", ], ], ) def test_qmark(query, statement): args = 1, 2, 3 new_query, vals = convert_paramstyle("qmark", query, args) assert (new_query, vals) == (statement, args) @pytest.mark.parametrize( "query,expected", [ [ "SELECT sum(x)::decimal(5, 2) :2, :1, * FROM t WHERE a=:3", "SELECT sum(x)::decimal(5, 2) $2, $1, * FROM t WHERE a=$3", ], ], ) def test_numeric(query, expected): args = 1, 2, 3 new_query, vals = convert_paramstyle("numeric", query, args) assert (new_query, vals) == (expected, args) @pytest.mark.parametrize( "query", [ "make_interval(days := 10)", ], ) def test_numeric_unchanged(query): args = 1, 2, 3 new_query, vals = convert_paramstyle("numeric", query, args) assert (new_query, vals) == (query, args) def test_named(): args = { "f_2": 1, "f1": 2, } new_query, vals = convert_paramstyle( "named", "SELECT sum(x)::decimal(5, 2) :f_2, :f1 FROM t WHERE a=:f_2", args ) expected = "SELECT sum(x)::decimal(5, 2) $1, $2 FROM t WHERE a=$1" assert (new_query, vals) == (expected, (1, 2)) @pytest.mark.parametrize( "query,expected", [ [ "SELECT %s, %s, \"f1_%%\", E'txt_%%' " "FROM t WHERE a=%s AND b='75%%' AND c = '%' -- Comment with %", "SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$3 AND " "b='75%%' AND c = '%' -- Comment with %", ], [ "SELECT -- Comment\n%s FROM t", "SELECT -- Comment\n$1 FROM t", ], ], ) def test_format_changed(query, expected): args = 1, 2, 3 new_query, vals = convert_paramstyle("format", query, args) assert (new_query, vals) == (expected, args) @pytest.mark.parametrize( "query", [ r"""COMMENT ON TABLE test_schema.comment_test """ r"""IS 'the test % '' " \ table comment'""", ], ) def test_format_unchanged(query): args = 1, 2, 3 new_query, vals = convert_paramstyle("format", query, args) assert (new_query, vals) == (query, args) def test_py_format(): args = {"f2": 1, "f1": 2, "f3": 3} new_query, vals = convert_paramstyle( "pyformat", "SELECT %(f2)s, %(f1)s, \"f1_%%\", E'txt_%%' " "FROM t WHERE a=%(f2)s AND b='75%%'", args, ) expected = "SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$1 AND " "b='75%%'" assert (new_query, vals) == (expected, (1, 2)) def test_pyformat_format(): """pyformat should support %s and an array, too:""" args = 1, 2, 3 new_query, vals = convert_paramstyle( "pyformat", "SELECT %s, %s, \"f1_%%\", E'txt_%%' " "FROM t WHERE a=%s AND b='75%%'", args, ) expected = "SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$3 AND " "b='75%%'" assert (new_query, vals) == (expected, args) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636748820.0 pg8000-1.23.0/test/dbapi/test_query.py0000664000175000017500000002243300000000000020122 0ustar00tlocketlocke00000000000000from datetime import datetime as Datetime, timezone as Timezone import pytest import pg8000.dbapi from pg8000.converters import INET_ARRAY, INTEGER # Tests relating to the basic operation of the database driver, driven by the # pg8000 custom interface. @pytest.fixture def db_table(request, con): con.paramstyle = "format" cursor = con.cursor() cursor.execute( "CREATE TEMPORARY TABLE t1 (f1 int primary key, " "f2 bigint not null, f3 varchar(50) null) " ) def fin(): try: cursor = con.cursor() cursor.execute("drop table t1") except pg8000.dbapi.DatabaseError: pass request.addfinalizer(fin) return con def test_database_error(cursor): with pytest.raises(pg8000.dbapi.DatabaseError): cursor.execute("INSERT INTO t99 VALUES (1, 2, 3)") def test_parallel_queries(db_table): cursor = db_table.cursor() cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, None)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (2, 10, None)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (3, 100, None)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (4, 1000, None)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (5, 10000, None)) c1 = db_table.cursor() c2 = db_table.cursor() c1.execute("SELECT f1, f2, f3 FROM t1") for row in c1.fetchall(): f1, f2, f3 = row c2.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > %s", (f1,)) for row in c2.fetchall(): f1, f2, f3 = row def test_parallel_open_portals(con): c1 = con.cursor() c2 = con.cursor() c1count, c2count = 0, 0 q = "select * from generate_series(1, %s)" params = (100,) c1.execute(q, params) c2.execute(q, params) for c2row in c2.fetchall(): c2count += 1 for c1row in c1.fetchall(): c1count += 1 assert c1count == c2count # Run a query on a table, alter the structure of the table, then run the # original query again. def test_alter(db_table): cursor = db_table.cursor() cursor.execute("select * from t1") cursor.execute("alter table t1 drop column f3") cursor.execute("select * from t1") # Run a query on a table, drop then re-create the table, then run the # original query again. def test_create(db_table): cursor = db_table.cursor() cursor.execute("select * from t1") cursor.execute("drop table t1") cursor.execute("create temporary table t1 (f1 int primary key)") cursor.execute("select * from t1") def test_insert_returning(db_table): cursor = db_table.cursor() cursor.execute("CREATE TEMPORARY TABLE t2 (id serial, data text)") # Test INSERT ... RETURNING with one row... cursor.execute("INSERT INTO t2 (data) VALUES (%s) RETURNING id", ("test1",)) row_id = cursor.fetchone()[0] cursor.execute("SELECT data FROM t2 WHERE id = %s", (row_id,)) assert "test1" == cursor.fetchone()[0] assert cursor.rowcount == 1 # Test with multiple rows... cursor.execute( "INSERT INTO t2 (data) VALUES (%s), (%s), (%s) " "RETURNING id", ("test2", "test3", "test4"), ) assert cursor.rowcount == 3 ids = tuple([x[0] for x in cursor.fetchall()]) assert len(ids) == 3 def test_row_count(db_table): cursor = db_table.cursor() expected_count = 57 cursor.executemany( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", tuple((i, i, None) for i in range(expected_count)), ) # Check rowcount after executemany assert expected_count == cursor.rowcount cursor.execute("SELECT * FROM t1") # Check row_count without doing any reading first... assert expected_count == cursor.rowcount # Check rowcount after reading some rows, make sure it still # works... for i in range(expected_count // 2): cursor.fetchone() assert expected_count == cursor.rowcount cursor = db_table.cursor() # Restart the cursor, read a few rows, and then check rowcount # again... cursor.execute("SELECT * FROM t1") for i in range(expected_count // 3): cursor.fetchone() assert expected_count == cursor.rowcount # Should be -1 for a command with no results cursor.execute("DROP TABLE t1") assert -1 == cursor.rowcount def test_row_count_update(db_table): cursor = db_table.cursor() cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, None)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (2, 10, None)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (3, 100, None)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (4, 1000, None)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (5, 10000, None)) cursor.execute("UPDATE t1 SET f3 = %s WHERE f2 > 101", ("Hello!",)) assert cursor.rowcount == 2 def test_int_oid(cursor): # https://bugs.launchpad.net/pg8000/+bug/230796 cursor.execute("SELECT typname FROM pg_type WHERE oid = %s", (100,)) def test_unicode_query(cursor): cursor.execute( "CREATE TEMPORARY TABLE \u043c\u0435\u0441\u0442\u043e " "(\u0438\u043c\u044f VARCHAR(50), " "\u0430\u0434\u0440\u0435\u0441 VARCHAR(250))" ) def test_executemany(db_table): cursor = db_table.cursor() cursor.executemany( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", ((1, 1, "Avast ye!"), (2, 1, None)), ) cursor.executemany( "select CAST(%s AS TIMESTAMP)", ((Datetime(2014, 5, 7, tzinfo=Timezone.utc),), (Datetime(2014, 5, 7),)), ) def test_executemany_setinputsizes(cursor): """Make sure that setinputsizes works for all the parameter sets""" cursor.execute( "CREATE TEMPORARY TABLE t1 (f1 int primary key, f2 inet[] not null) " ) cursor.setinputsizes(INTEGER, INET_ARRAY) cursor.executemany( "INSERT INTO t1 (f1, f2) VALUES (%s, %s)", ((1, ["1.1.1.1"]), (2, ["0.0.0.0"])) ) def test_executemany_no_param_sets(cursor): cursor.executemany("INSERT INTO t1 (f1, f2) VALUES (%s, %s)", []) assert cursor.rowcount == -1 # Check that autocommit stays off # We keep track of whether we're in a transaction or not by using the # READY_FOR_QUERY message. def test_transactions(db_table): cursor = db_table.cursor() cursor.execute("commit") cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, "Zombie")) cursor.execute("rollback") cursor.execute("select * from t1") assert cursor.rowcount == 0 def test_in(cursor): cursor.execute("SELECT typname FROM pg_type WHERE oid = any(%s)", ([16, 23],)) ret = cursor.fetchall() assert ret[0][0] == "bool" def test_no_previous_tpc(con): con.tpc_begin("Stacey") cursor = con.cursor() cursor.execute("SELECT * FROM pg_type") con.tpc_commit() # Check that tpc_recover() doesn't start a transaction def test_tpc_recover(con): con.tpc_recover() cursor = con.cursor() con.autocommit = True # If tpc_recover() has started a transaction, this will fail cursor.execute("VACUUM") def test_tpc_prepare(con): xid = "Stacey" con.tpc_begin(xid) con.tpc_prepare() con.tpc_rollback(xid) # An empty query should raise a ProgrammingError def test_empty_query(cursor): with pytest.raises(pg8000.dbapi.DatabaseError): cursor.execute("") # rolling back when not in a transaction doesn't generate a warning def test_rollback_no_transaction(con): # Remove any existing notices con.notices.clear() # First, verify that a raw rollback does produce a notice con.execute_unnamed("rollback") assert 1 == len(con.notices) # 25P01 is the code for no_active_sql_tronsaction. It has # a message and severity name, but those might be # localized/depend on the server version. assert con.notices.pop().get(b"C") == b"25P01" # Now going through the rollback method doesn't produce # any notices because it knows we're not in a transaction. con.rollback() assert 0 == len(con.notices) def test_setinputsizes(con): cursor = con.cursor() cursor.setinputsizes(20) cursor.execute("select %s", (None,)) retval = cursor.fetchall() assert retval[0][0] is None def test_unexecuted_cursor_rowcount(con): cursor = con.cursor() assert cursor.rowcount == -1 def test_unexecuted_cursor_description(con): cursor = con.cursor() assert cursor.description is None def test_callproc(cursor): cursor.execute("select current_setting('server_version')") version = cursor.fetchall()[0][0].split()[0] if not (version.startswith("9") or version.startswith("10")): cursor.execute( """ CREATE PROCEDURE echo(INOUT val text) LANGUAGE plpgsql AS $proc$ BEGIN END $proc$; """ ) cursor.callproc("echo", ["hello"]) assert cursor.fetchall() == (["hello"],) def test_null_result(db_table): cur = db_table.cursor() cur.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, "a")) with pytest.raises(pg8000.dbapi.ProgrammingError): cur.fetchall() def test_not_parsed_if_no_params(mocker, cursor): mock_convert_paramstyle = mocker.patch("pg8000.dbapi.convert_paramstyle") cursor.execute("ROLLBACK") mock_convert_paramstyle.assert_not_called() ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1634149967.0 pg8000-1.23.0/test/dbapi/test_typeconversion.py0000664000175000017500000005722000000000000022046 0ustar00tlocketlocke00000000000000import decimal import ipaddress import os import time import uuid from collections import OrderedDict from datetime import ( date as Date, datetime as Datetime, time as Time, timedelta as Timedelta, timezone as Timezone, ) from enum import Enum from json import dumps import pytest import pytz import pg8000.dbapi from pg8000.converters import ( INTERVAL, PGInterval, interval_in, pg_interval_in, pg_interval_out, ) # Type conversion tests def test_time_roundtrip(cursor): t = Time(4, 5, 6) cursor.execute("SELECT cast(%s as time) as f1", (t,)) assert cursor.fetchall()[0][0] == t def test_date_roundtrip(cursor): v = Date(2001, 2, 3) cursor.execute("SELECT cast(%s as date) as f1", (v,)) assert cursor.fetchall()[0][0] == v def test_bool_roundtrip(cursor): b = True cursor.execute("SELECT cast(%s as bool) as f1", (b,)) assert cursor.fetchall()[0][0] is b def test_null_roundtrip(cursor): cursor.execute("select current_setting('server_version')") version = cursor.fetchall()[0][0][:2] if version.startswith("9"): # Prior to PostgreSQL version 10 We can't just "SELECT %s" and set # None as the parameter, since it has no type. That would result # in a PG error, "could not determine data type of parameter %s". # So we create a temporary table, insert null values, and read them # back. cursor.execute( "CREATE TEMPORARY TABLE TestNullWrite " "(f1 int4, f2 timestamp, f3 varchar)" ) cursor.execute( "INSERT INTO TestNullWrite VALUES (%s, %s, %s)", (None, None, None) ) cursor.execute("SELECT * FROM TestNullWrite") assert cursor.fetchall()[0] == [None, None, None] with pytest.raises(pg8000.exceptions.DatabaseError): cursor.execute("SELECT %s as f1", (None,)) else: cursor.execute("SELECT %s", (None,)) assert cursor.fetchall()[0][0] is None def test_decimal_roundtrip(cursor): values = ("1.1", "-1.1", "10000", "20000", "-1000000000.123456789", "1.0", "12.44") for v in values: cursor.execute("SELECT CAST(%s AS NUMERIC)", (decimal.Decimal(v),)) retval = cursor.fetchall() assert str(retval[0][0]) == v def test_float_roundtrip(cursor): val = 1.756e-12 cursor.execute("SELECT cast(%s as double precision)", (val,)) assert cursor.fetchall()[0][0] == val def test_float_plus_infinity_roundtrip(cursor): v = float("inf") cursor.execute("SELECT cast(%s as double precision)", (v,)) assert cursor.fetchall()[0][0] == v def test_str_roundtrip(cursor): v = "hello world" cursor.execute("create temporary table test_str (f character varying(255))") cursor.execute("INSERT INTO test_str VALUES (%s)", (v,)) cursor.execute("SELECT * from test_str") assert cursor.fetchall()[0][0] == v def test_str_then_int(cursor): v1 = "hello world" cursor.execute("SELECT cast(%s as varchar) as f1", (v1,)) assert cursor.fetchall()[0][0] == v1 v2 = 1 cursor.execute("SELECT cast(%s as varchar) as f1", (v2,)) assert cursor.fetchall()[0][0] == str(v2) def test_unicode_roundtrip(cursor): v = "hello \u0173 world" cursor.execute("SELECT cast(%s as varchar) as f1", (v,)) assert cursor.fetchall()[0][0] == v def test_long_roundtrip(cursor): v = 50000000000000 cursor.execute("SELECT cast(%s as bigint)", (v,)) assert cursor.fetchall()[0][0] == v def test_int_execute_many_select(cursor): cursor.executemany("SELECT CAST(%s AS INTEGER)", ((1,), (40000,))) cursor.fetchall() def test_int_execute_many_insert(cursor): v = ([None], [4]) cursor.execute("create temporary table test_int (f integer)") cursor.executemany("INSERT INTO test_int VALUES (%s)", v) cursor.execute("SELECT * from test_int") assert cursor.fetchall() == v def test_insert_null(cursor): v = None cursor.execute("CREATE TEMPORARY TABLE test_int (f INTEGER)") cursor.execute("INSERT INTO test_int VALUES (%s)", (v,)) cursor.execute("SELECT * FROM test_int") assert cursor.fetchall()[0][0] == v def test_int_roundtrip(con, cursor): int2 = 21 int4 = 23 int8 = 20 MAP = { int2: "int2", int4: "int4", int8: "int8", } test_values = [ (0, int2), (-32767, int2), (-32768, int4), (+32767, int2), (+32768, int4), (-2147483647, int4), (-2147483648, int8), (+2147483647, int4), (+2147483648, int8), (-9223372036854775807, int8), (+9223372036854775807, int8), ] for value, typoid in test_values: cursor.execute("SELECT cast(%s as " + MAP[typoid] + ")", (value,)) assert cursor.fetchall()[0][0] == value column_name, column_typeoid = cursor.description[0][0:2] assert column_typeoid == typoid def test_bytea_roundtrip(cursor): cursor.execute( "SELECT cast(%s as bytea)", (pg8000.Binary(b"\x00\x01\x02\x03\x02\x01\x00"),) ) assert cursor.fetchall()[0][0] == b"\x00\x01\x02\x03\x02\x01\x00" def test_bytearray_round_trip(cursor): binary = b"\x00\x01\x02\x03\x02\x01\x00" cursor.execute("SELECT cast(%s as bytea)", (bytearray(binary),)) assert cursor.fetchall()[0][0] == binary def test_bytearray_subclass_round_trip(cursor): class BClass(bytearray): pass binary = b"\x00\x01\x02\x03\x02\x01\x00" cursor.execute("SELECT cast(%s as bytea)", (BClass(binary),)) assert cursor.fetchall()[0][0] == binary def test_timestamp_roundtrip(is_java, cursor): v = Datetime(2001, 2, 3, 4, 5, 6, 170000) cursor.execute("SELECT cast(%s as timestamp)", (v,)) assert cursor.fetchall()[0][0] == v # Test that time zone doesn't affect it # Jython 2.5.3 doesn't have a time.tzset() so skip if not is_java: orig_tz = os.environ.get("TZ") os.environ["TZ"] = "America/Edmonton" time.tzset() cursor.execute("SELECT cast(%s as timestamp)", (v,)) assert cursor.fetchall()[0][0] == v if orig_tz is None: del os.environ["TZ"] else: os.environ["TZ"] = orig_tz time.tzset() def test_pg_interval_repr(): v = PGInterval(microseconds=123456789, days=2, months=24) assert repr(v) == "" def test_pg_interval_in_1_year(): assert pg_interval_in("1 year") == PGInterval(years=1) def test_interval_in_2_months(): assert interval_in("2 hours") def test_pg_interval_roundtrip(con, cursor): con.register_in_adapter(INTERVAL, pg_interval_in) con.register_out_adapter(PGInterval, pg_interval_out) v = PGInterval(microseconds=123456789, days=2, months=24) cursor.execute("SELECT cast(%s as interval)", (v,)) assert cursor.fetchall()[0][0] == v def test_interval_roundtrip(cursor): v = Timedelta(seconds=30) cursor.execute("SELECT cast(%s as interval)", (v,)) assert cursor.fetchall()[0][0] == v def test_enum_str_round_trip(cursor): try: cursor.execute("create type lepton as enum ('electron', 'muon', 'tau')") v = "muon" cursor.execute("SELECT cast(%s as lepton) as f1", (v,)) retval = cursor.fetchall() assert retval[0][0] == v cursor.execute("CREATE TEMPORARY TABLE testenum (f1 lepton)") cursor.execute( "INSERT INTO testenum VALUES (cast(%s as lepton))", ("electron",) ) finally: cursor.execute("drop table testenum") cursor.execute("drop type lepton") def test_enum_custom_round_trip(con, cursor): class Lepton: # Implements PEP 435 in the minimal fashion needed __members__ = OrderedDict() def __init__(self, name, value, alias=None): self.name = name self.value = value self.__members__[name] = self setattr(self.__class__, name, self) if alias: self.__members__[alias] = self setattr(self.__class__, alias, self) def lepton_out(lepton): return lepton.value try: cursor.execute("create type lepton as enum ('1', '2', '3')") con.register_out_adapter(Lepton, lepton_out) v = Lepton("muon", "2") cursor.execute("SELECT CAST(%s AS lepton)", (v,)) assert cursor.fetchall()[0][0] == v.value finally: cursor.execute("drop type lepton") def test_enum_py_round_trip(cursor): class Lepton(Enum): electron = "1" muon = "2" tau = "3" try: cursor.execute("create type lepton as enum ('1', '2', '3')") v = Lepton.muon cursor.execute("SELECT cast(%s as lepton) as f1", (v,)) assert cursor.fetchall()[0][0] == v.value cursor.execute("CREATE TEMPORARY TABLE testenum (f1 lepton)") cursor.execute( "INSERT INTO testenum VALUES (cast(%s as lepton))", (Lepton.electron,) ) finally: cursor.execute("drop table testenum") cursor.execute("drop type lepton") def test_xml_roundtrip(cursor): v = "gatccgagtac" cursor.execute("select xmlparse(content %s) as f1", (v,)) assert cursor.fetchall()[0][0] == v def test_uuid_roundtrip(cursor): v = uuid.UUID("911460f2-1f43-fea2-3e2c-e01fd5b5069d") cursor.execute("select cast(%s as uuid)", (v,)) assert cursor.fetchall()[0][0] == v def test_inet_roundtrip_network(cursor): v = ipaddress.ip_network("192.168.0.0/28") cursor.execute("select cast(%s as inet)", (v,)) assert cursor.fetchall()[0][0] == v def test_inet_roundtrip_address(cursor): v = ipaddress.ip_address("192.168.0.1") cursor.execute("select cast(%s as inet)", (v,)) assert cursor.fetchall()[0][0] == v def test_xid_roundtrip(cursor): v = 86722 cursor.execute("select cast(cast(%s as varchar) as xid) as f1", (v,)) retval = cursor.fetchall() assert retval[0][0] == v # Should complete without an exception cursor.execute("select * from pg_locks where transactionid = %s", (97712,)) retval = cursor.fetchall() def test_int2vector_in(cursor): cursor.execute("select cast('1 2' as int2vector) as f1") assert cursor.fetchall()[0][0] == [1, 2] # Should complete without an exception cursor.execute("select indkey from pg_index") cursor.fetchall() def test_timestamp_tz_out(cursor): cursor.execute( "SELECT '2001-02-03 04:05:06.17 America/Edmonton'" "::timestamp with time zone" ) retval = cursor.fetchall() dt = retval[0][0] assert dt.tzinfo is not None, "no tzinfo returned" assert dt.astimezone(Timezone.utc) == Datetime( 2001, 2, 3, 11, 5, 6, 170000, Timezone.utc ), "retrieved value match failed" def test_timestamp_tz_roundtrip(is_java, cursor): if not is_java: mst = pytz.timezone("America/Edmonton") v1 = mst.localize(Datetime(2001, 2, 3, 4, 5, 6, 170000)) cursor.execute("SELECT cast(%s as timestamptz)", (v1,)) v2 = cursor.fetchall()[0][0] assert v2.tzinfo is not None assert v1 == v2 def test_timestamp_mismatch(is_java, cursor): if not is_java: mst = pytz.timezone("America/Edmonton") cursor.execute("SET SESSION TIME ZONE 'America/Edmonton'") try: cursor.execute( "CREATE TEMPORARY TABLE TestTz " "(f1 timestamp with time zone, " "f2 timestamp without time zone)" ) cursor.execute( "INSERT INTO TestTz (f1, f2) VALUES (%s, %s)", ( # insert timestamp into timestamptz field (v1) Datetime(2001, 2, 3, 4, 5, 6, 170000), # insert timestamptz into timestamp field (v2) mst.localize(Datetime(2001, 2, 3, 4, 5, 6, 170000)), ), ) cursor.execute("SELECT f1, f2 FROM TestTz") retval = cursor.fetchall() # when inserting a timestamp into a timestamptz field, # postgresql assumes that it is in local time. So the value # that comes out will be the server's local time interpretation # of v1. We've set the server's TZ to MST, the time should # be... f1 = retval[0][0] assert f1 == Datetime(2001, 2, 3, 11, 5, 6, 170000, Timezone.utc) # inserting the timestamptz into a timestamp field, pg8000 # converts the value into UTC, and then the PG server converts # it into local time for insertion into the field. When we # query for it, we get the same time back, like the tz was # dropped. f2 = retval[0][1] assert f2 == Datetime(2001, 2, 3, 11, 5, 6, 170000) finally: cursor.execute("SET SESSION TIME ZONE DEFAULT") def test_name_out(cursor): # select a field that is of "name" type: cursor.execute("SELECT usename FROM pg_user") cursor.fetchall() # It is sufficient that no errors were encountered. def test_oid_out(cursor): cursor.execute("SELECT oid FROM pg_type") cursor.fetchall() # It is sufficient that no errors were encountered. def test_boolean_in(cursor): cursor.execute("SELECT cast('t' as bool)") assert cursor.fetchall()[0][0] def test_numeric_out(cursor): for num in ("5000", "50.34"): cursor.execute("SELECT " + num + "::numeric") assert str(cursor.fetchall()[0][0]) == num def test_int2_out(cursor): cursor.execute("SELECT 5000::smallint") assert cursor.fetchall()[0][0] == 5000 def test_int4_out(cursor): cursor.execute("SELECT 5000::integer") assert cursor.fetchall()[0][0] == 5000 def test_int8_out(cursor): cursor.execute("SELECT 50000000000000::bigint") assert cursor.fetchall()[0][0] == 50000000000000 def test_float4_out(cursor): cursor.execute("SELECT 1.1::real") assert cursor.fetchall()[0][0] == 1.1 def test_float8_out(cursor): cursor.execute("SELECT 1.1::double precision") assert cursor.fetchall()[0][0] == 1.1000000000000001 def test_varchar_out(cursor): cursor.execute("SELECT 'hello'::varchar(20)") assert cursor.fetchall()[0][0] == "hello" def test_char_out(cursor): cursor.execute("SELECT 'hello'::char(20)") assert cursor.fetchall()[0][0] == "hello " def test_text_out(cursor): cursor.execute("SELECT 'hello'::text") assert cursor.fetchall()[0][0] == "hello" def test_interval_in(con, cursor): con.register_in_adapter(INTERVAL, pg_interval_in) cursor.execute("SELECT '1 month 16 days 12 hours 32 minutes 64 seconds'::interval") expected_value = PGInterval( microseconds=(12 * 60 * 60 * 1000 * 1000) + (32 * 60 * 1000 * 1000) + (64 * 1000 * 1000), days=16, months=1, ) assert cursor.fetchall()[0][0] == expected_value def test_interval_in_30_seconds(cursor): cursor.execute("select interval '30 seconds'") assert cursor.fetchall()[0][0] == Timedelta(seconds=30) def test_interval_in_12_days_30_seconds(cursor): cursor.execute("select interval '12 days 30 seconds'") assert cursor.fetchall()[0][0] == Timedelta(days=12, seconds=30) def test_timestamp_out(cursor): cursor.execute("SELECT '2001-02-03 04:05:06.17'::timestamp") retval = cursor.fetchall() assert retval[0][0] == Datetime(2001, 2, 3, 4, 5, 6, 170000) def test_int4_array_out(cursor): cursor.execute( "SELECT '{1,2,3,4}'::INT[] AS f1, '{{1,2,3},{4,5,6}}'::INT[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::INT[][][] AS f3" ) f1, f2, f3 = cursor.fetchone() assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_int2_array_out(cursor): cursor.execute( "SELECT '{1,2,3,4}'::INT2[] AS f1, " "'{{1,2,3},{4,5,6}}'::INT2[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::INT2[][][] AS f3" ) f1, f2, f3 = cursor.fetchone() assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_int8_array_out(cursor): cursor.execute( "SELECT '{1,2,3,4}'::INT8[] AS f1, " "'{{1,2,3},{4,5,6}}'::INT8[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::INT8[][][] AS f3" ) f1, f2, f3 = cursor.fetchone() assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_bool_array_out(cursor): cursor.execute( "SELECT '{TRUE,FALSE,FALSE,TRUE}'::BOOL[] AS f1, " "'{{TRUE,FALSE,TRUE},{FALSE,TRUE,FALSE}}'::BOOL[][] AS f2, " "'{{{TRUE,FALSE},{FALSE,TRUE}},{{NULL,TRUE},{FALSE,FALSE}}}'" "::BOOL[][][] AS f3" ) f1, f2, f3 = cursor.fetchone() assert f1 == [True, False, False, True] assert f2 == [[True, False, True], [False, True, False]] assert f3 == [[[True, False], [False, True]], [[None, True], [False, False]]] def test_float4_array_out(cursor): cursor.execute( "SELECT '{1,2,3,4}'::FLOAT4[] AS f1, " "'{{1,2,3},{4,5,6}}'::FLOAT4[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::FLOAT4[][][] AS f3" ) f1, f2, f3 = cursor.fetchone() assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_float8_array_out(cursor): cursor.execute( "SELECT '{1,2,3,4}'::FLOAT8[] AS f1, " "'{{1,2,3},{4,5,6}}'::FLOAT8[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::FLOAT8[][][] AS f3" ) f1, f2, f3 = cursor.fetchone() assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_int_array_roundtrip_small(cursor): """send small int array, should be sent as INT2[]""" cursor.execute("SELECT cast(%s as int2[])", ([1, 2, 3],)) assert cursor.fetchall()[0][0], [1, 2, 3] column_name, column_typeoid = cursor.description[0][0:2] assert column_typeoid == 1005, "type should be INT2[]" def test_int_array_roundtrip_multi(cursor): """test multi-dimensional array, should be sent as INT2[]""" cursor.execute("SELECT cast(%s as int2[])", ([[1, 2], [3, 4]],)) assert cursor.fetchall()[0][0] == [[1, 2], [3, 4]] column_name, column_typeoid = cursor.description[0][0:2] assert column_typeoid == 1005, "type should be INT2[]" def test_int4_array_roundtrip(cursor): """a larger value should kick it up to INT4[]...""" cursor.execute("SELECT cast(%s as int4[])", ([70000, 2, 3],)) assert cursor.fetchall()[0][0] == [70000, 2, 3] column_name, column_typeoid = cursor.description[0][0:2] assert column_typeoid == 1007, "type should be INT4[]" def test_int8_array_roundtrip(cursor): """a much larger value should kick it up to INT8[]...""" cursor.execute("SELECT cast(%s as int8[])", ([7000000000, 2, 3],)) assert cursor.fetchall()[0][0] == [7000000000, 2, 3], "retrieved value match failed" column_name, column_typeoid = cursor.description[0][0:2] assert column_typeoid == 1016, "type should be INT8[]" def test_int_array_with_null_roundtrip(cursor): cursor.execute("SELECT cast(%s as int[])", ([1, None, 3],)) assert cursor.fetchall()[0][0] == [1, None, 3] def test_float_array_roundtrip(cursor): cursor.execute("SELECT cast(%s as double precision[])", ([1.1, 2.2, 3.3],)) assert cursor.fetchall()[0][0] == [1.1, 2.2, 3.3] def test_bool_array_roundtrip(cursor): cursor.execute("SELECT cast(%s as bool[])", ([True, False, None],)) assert cursor.fetchall()[0][0] == [True, False, None] @pytest.mark.parametrize( "test_input,expected", [ ("SELECT '{a,b,c}'::TEXT[] AS f1", ["a", "b", "c"]), ("SELECT '{a,b,c}'::CHAR[] AS f1", ["a", "b", "c"]), ("SELECT '{a,b,c}'::VARCHAR[] AS f1", ["a", "b", "c"]), ("SELECT '{a,b,c}'::CSTRING[] AS f1", ["a", "b", "c"]), ("SELECT '{a,b,c}'::NAME[] AS f1", ["a", "b", "c"]), ("SELECT '{}'::text[];", []), ('SELECT \'{NULL,"NULL",NULL,""}\'::text[];', [None, "NULL", None, ""]), ], ) def test_string_array_out(cursor, test_input, expected): cursor.execute(test_input) assert cursor.fetchall()[0][0] == expected def test_numeric_array_out(cursor): cursor.execute("SELECT '{1.1,2.2,3.3}'::numeric[] AS f1") assert cursor.fetchone()[0] == [ decimal.Decimal("1.1"), decimal.Decimal("2.2"), decimal.Decimal("3.3"), ] def test_numeric_array_roundtrip(cursor): v = [decimal.Decimal("1.1"), None, decimal.Decimal("3.3")] cursor.execute("SELECT cast(%s as numeric[])", (v,)) assert cursor.fetchall()[0][0] == v def test_string_array_roundtrip(cursor): v = [ "Hello!", "World!", "abcdefghijklmnopqrstuvwxyz", "", "A bunch of random characters:", " ~!@#$%^&*()_+`1234567890-=[]\\{}|{;':\",./<>?\t", None, ] cursor.execute("SELECT cast(%s as varchar[])", (v,)) assert cursor.fetchall()[0][0] == v def test_array_string_escape(): v = '"' res = pg8000.converters.array_string_escape(v) assert res == '"\\""' def test_empty_array(cursor): v = [] cursor.execute("SELECT cast(%s as varchar[])", (v,)) assert cursor.fetchall()[0][0] == v def test_macaddr(cursor): cursor.execute("SELECT macaddr '08002b:010203'") assert cursor.fetchall()[0][0] == "08:00:2b:01:02:03" def test_tsvector_roundtrip(cursor): cursor.execute( "SELECT cast(%s as tsvector)", ("a fat cat sat on a mat and ate a fat rat",) ) retval = cursor.fetchall() assert retval[0][0] == "'a' 'and' 'ate' 'cat' 'fat' 'mat' 'on' 'rat' 'sat'" def test_hstore_roundtrip(cursor): val = '"a"=>"1"' cursor.execute("SELECT cast(%s as hstore)", (val,)) assert cursor.fetchall()[0][0] == val def test_json_roundtrip(cursor): val = {"name": "Apollo 11 Cave", "zebra": True, "age": 26.003} cursor.execute("SELECT cast(%s as jsonb)", (dumps(val),)) assert cursor.fetchall()[0][0] == val def test_jsonb_roundtrip(cursor): val = {"name": "Apollo 11 Cave", "zebra": True, "age": 26.003} cursor.execute("SELECT cast(%s as jsonb)", (dumps(val),)) retval = cursor.fetchall() assert retval[0][0] == val def test_json_access_object(cursor): val = {"name": "Apollo 11 Cave", "zebra": True, "age": 26.003} cursor.execute("SELECT cast(%s as json) -> %s", (dumps(val), "name")) retval = cursor.fetchall() assert retval[0][0] == "Apollo 11 Cave" def test_jsonb_access_object(cursor): val = {"name": "Apollo 11 Cave", "zebra": True, "age": 26.003} cursor.execute("SELECT cast(%s as jsonb) -> %s", (dumps(val), "name")) retval = cursor.fetchall() assert retval[0][0] == "Apollo 11 Cave" def test_json_access_array(cursor): val = [-1, -2, -3, -4, -5] cursor.execute("SELECT cast(%s as json) -> cast(%s as int)", (dumps(val), 2)) assert cursor.fetchall()[0][0] == -3 def test_jsonb_access_array(cursor): val = [-1, -2, -3, -4, -5] cursor.execute("SELECT cast(%s as jsonb) -> cast(%s as int)", (dumps(val), 2)) assert cursor.fetchall()[0][0] == -3 def test_jsonb_access_path(cursor): j = {"a": [1, 2, 3], "b": [4, 5, 6]} path = ["a", "2"] cursor.execute("SELECT cast(%s as jsonb) #>> %s", (dumps(j), path)) assert cursor.fetchall()[0][0] == str(j[path[0]][int(path[1])]) def test_infinity_timestamp_roundtrip(cursor): v = "infinity" cursor.execute("SELECT cast(%s as timestamp) as f1", (v,)) assert cursor.fetchall()[0][0] == v def test_point_roundtrip(cursor): v = "(2.3,1)" cursor.execute("SELECT cast(%s as point) as f1", (v,)) assert cursor.fetchall()[0][0] == v def test_time_in(): actual = pg8000.converters.time_in("12:57:18.000396") assert actual == Time(12, 57, 18, 396) ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1636798860.4089942 pg8000-1.23.0/test/legacy/0000775000175000017500000000000000000000000015525 5ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/__init__.py0000664000175000017500000000000000000000000017624 0ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/conftest.py0000664000175000017500000000175400000000000017733 0ustar00tlocketlocke00000000000000import sys from os import environ import pytest import pg8000 @pytest.fixture(scope="class") def db_kwargs(): db_connect = {"user": "postgres", "password": "pw"} for kw, var, f in [ ("host", "PGHOST", str), ("password", "PGPASSWORD", str), ("port", "PGPORT", int), ]: try: db_connect[kw] = f(environ[var]) except KeyError: pass return db_connect @pytest.fixture def con(request, db_kwargs): conn = pg8000.connect(**db_kwargs) def fin(): try: conn.rollback() except pg8000.InterfaceError: pass try: conn.close() except pg8000.InterfaceError: pass request.addfinalizer(fin) return conn @pytest.fixture def cursor(request, con): cursor = con.cursor() def fin(): cursor.close() request.addfinalizer(fin) return cursor @pytest.fixture def is_java(): return "java" in sys.platform.lower() ././@PaxHeader0000000000000000000000000000003400000000000011452 xustar000000000000000028 mtime=1636798860.4129941 pg8000-1.23.0/test/legacy/github-actions/0000775000175000017500000000000000000000000020445 5ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/github-actions/__init__.py0000664000175000017500000000000000000000000022544 0ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/github-actions/gss_legacy.py0000664000175000017500000000054300000000000023141 0ustar00tlocketlocke00000000000000import pytest import pg8000.dbapi def test_gss(db_kwargs): """Called by GitHub Actions with auth method gss.""" # Should raise an exception saying gss isn't supported with pytest.raises( pg8000.dbapi.InterfaceError, match="Authentication method 7 not supported by pg8000.", ): pg8000.dbapi.connect(**db_kwargs) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/github-actions/md5_legacy.py0000664000175000017500000000022200000000000023024 0ustar00tlocketlocke00000000000000def test_md5(con): """Called by GitHub Actions with auth method md5. We just need to check that we can get a connection. """ pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/github-actions/password_legacy.py0000664000175000017500000000023400000000000024204 0ustar00tlocketlocke00000000000000def test_password(con): """Called by GitHub Actions with auth method password. We just need to check that we can get a connection. """ pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/github-actions/scram-sha-256_legacy.py0000664000175000017500000000024600000000000024535 0ustar00tlocketlocke00000000000000def test_scram_sha_256(con): """Called by GitHub Actions with auth method scram-sha-256. We just need to check that we can get a connection. """ pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/github-actions/ssl_legacy.py0000664000175000017500000000335400000000000023151 0ustar00tlocketlocke00000000000000import ssl import sys import pytest import pg8000.dbapi # Check if running in Jython if "java" in sys.platform: from javax.net.ssl import TrustManager, X509TrustManager from jarray import array from javax.net.ssl import SSLContext class TrustAllX509TrustManager(X509TrustManager): """Define a custom TrustManager which will blindly accept all certificates""" def checkClientTrusted(self, chain, auth): pass def checkServerTrusted(self, chain, auth): pass def getAcceptedIssuers(self): return None # Create a static reference to an SSLContext which will use # our custom TrustManager trust_managers = array([TrustAllX509TrustManager()], TrustManager) TRUST_ALL_CONTEXT = SSLContext.getInstance("SSL") TRUST_ALL_CONTEXT.init(None, trust_managers, None) # Keep a static reference to the JVM's default SSLContext for restoring # at a later time DEFAULT_CONTEXT = SSLContext.getDefault() @pytest.fixture def trust_all_certificates(request): """Decorator function that will make it so the context of the decorated method will run with our TrustManager that accepts all certificates""" # Only do this if running under Jython is_java = "java" in sys.platform if is_java: from javax.net.ssl import SSLContext SSLContext.setDefault(TRUST_ALL_CONTEXT) def fin(): if is_java: SSLContext.setDefault(DEFAULT_CONTEXT) request.addfinalizer(fin) @pytest.mark.usefixtures("trust_all_certificates") def test_ssl(db_kwargs): context = ssl.SSLContext() context.check_hostname = False db_kwargs["ssl_context"] = context with pg8000.dbapi.connect(**db_kwargs): pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/stress.py0000664000175000017500000000204300000000000017421 0ustar00tlocketlocke00000000000000from contextlib import closing import pg8000 from pg8000.tests.connection_settings import db_connect with closing(pg8000.connect(**db_connect)) as db: for i in range(100): cursor = db.cursor() cursor.execute( """ SELECT n.nspname as "Schema", pg_catalog.format_type(t.oid, NULL) AS "Name", pg_catalog.obj_description(t.oid, 'pg_type') as "Description" FROM pg_catalog.pg_type t LEFT JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace left join pg_catalog.pg_namespace kj on n.oid = t.typnamespace WHERE (t.typrelid = 0 OR (SELECT c.relkind = 'c' FROM pg_catalog.pg_class c WHERE c.oid = t.typrelid)) AND NOT EXISTS( SELECT 1 FROM pg_catalog.pg_type el WHERE el.oid = t.typelem AND el.typarray = t.oid) AND pg_catalog.pg_type_is_visible(t.oid) ORDER BY 1, 2;""" ) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/test_auth.py0000664000175000017500000000417500000000000020106 0ustar00tlocketlocke00000000000000import ssl import sys import pytest import pg8000.legacy def testGss(db_kwargs): """This requires a line in pg_hba.conf that requires gss for the database pg8000_gss """ db_kwargs["database"] = "pg8000_gss" # Should raise an exception saying gss isn't supported with pytest.raises( pg8000.legacy.InterfaceError, match="Authentication method 7 not supported by pg8000.", ): pg8000.legacy.connect(**db_kwargs) # Check if running in Jython if "java" in sys.platform: from javax.net.ssl import TrustManager, X509TrustManager from jarray import array from javax.net.ssl import SSLContext class TrustAllX509TrustManager(X509TrustManager): """Define a custom TrustManager which will blindly accept all certificates""" def checkClientTrusted(self, chain, auth): pass def checkServerTrusted(self, chain, auth): pass def getAcceptedIssuers(self): return None # Create a static reference to an SSLContext which will use # our custom TrustManager trust_managers = array([TrustAllX509TrustManager()], TrustManager) TRUST_ALL_CONTEXT = SSLContext.getInstance("SSL") TRUST_ALL_CONTEXT.init(None, trust_managers, None) # Keep a static reference to the JVM's default SSLContext for restoring # at a later time DEFAULT_CONTEXT = SSLContext.getDefault() @pytest.fixture def trust_all_certificates(request): """Decorator function that will make it so the context of the decorated method will run with our TrustManager that accepts all certificates""" # Only do this if running under Jython is_java = "java" in sys.platform if is_java: from javax.net.ssl import SSLContext SSLContext.setDefault(TRUST_ALL_CONTEXT) def fin(): if is_java: SSLContext.setDefault(DEFAULT_CONTEXT) request.addfinalizer(fin) @pytest.mark.usefixtures("trust_all_certificates") def test_ssl(db_kwargs): context = ssl.SSLContext() context.check_hostname = False db_kwargs["ssl_context"] = context with pg8000.connect(**db_kwargs): pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/test_benchmarks.py0000664000175000017500000000133200000000000021252 0ustar00tlocketlocke00000000000000import pytest @pytest.mark.parametrize( "txt", ( ("int2", "cast(id / 100 as int2)"), "cast(id as int4)", "cast(id * 100 as int8)", "(id % 2) = 0", "N'Static text string'", "cast(id / 100 as float4)", "cast(id / 100 as float8)", "cast(id / 100 as numeric)", "timestamp '2001-09-28'", ), ) def test_round_trips(con, benchmark, txt): def torun(): query = """SELECT {0} AS column1, {0} AS column2, {0} AS column3, {0} AS column4, {0} AS column5, {0} AS column6, {0} AS column7 FROM (SELECT generate_series(1, 10000) AS id) AS tbl""".format( txt ) con.run(query) benchmark(torun) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1634149967.0 pg8000-1.23.0/test/legacy/test_connection.py0000664000175000017500000001270200000000000021277 0ustar00tlocketlocke00000000000000import pytest import pg8000 def testUnixSocketMissing(): conn_params = {"unix_sock": "/file-does-not-exist", "user": "doesn't-matter"} with pytest.raises(pg8000.InterfaceError): pg8000.connect(**conn_params) def test_internet_socket_connection_refused(): conn_params = {"port": 0, "user": "doesn't-matter"} with pytest.raises( pg8000.InterfaceError, match="Can't create a connection to host localhost and port 0 " "\\(timeout is None and source_address is None\\).", ): pg8000.connect(**conn_params) def testDatabaseMissing(db_kwargs): db_kwargs["database"] = "missing-db" with pytest.raises(pg8000.ProgrammingError): pg8000.connect(**db_kwargs) def test_notify(con): backend_pid = con.run("select pg_backend_pid()")[0][0] assert list(con.notifications) == [] con.run("LISTEN test") con.run("NOTIFY test") con.commit() con.run("VALUES (1, 2), (3, 4), (5, 6)") assert len(con.notifications) == 1 assert con.notifications[0] == (backend_pid, "test", "") def test_notify_with_payload(con): backend_pid = con.run("select pg_backend_pid()")[0][0] assert list(con.notifications) == [] con.run("LISTEN test") con.run("NOTIFY test, 'Parnham'") con.commit() con.run("VALUES (1, 2), (3, 4), (5, 6)") assert len(con.notifications) == 1 assert con.notifications[0] == (backend_pid, "test", "Parnham") # This requires a line in pg_hba.conf that requires md5 for the database # pg8000_md5 def testMd5(db_kwargs): db_kwargs["database"] = "pg8000_md5" # Should only raise an exception saying db doesn't exist with pytest.raises(pg8000.ProgrammingError, match="3D000"): pg8000.connect(**db_kwargs) # This requires a line in pg_hba.conf that requires 'password' for the # database pg8000_password def testPassword(db_kwargs): db_kwargs["database"] = "pg8000_password" # Should only raise an exception saying db doesn't exist with pytest.raises(pg8000.ProgrammingError, match="3D000"): pg8000.connect(**db_kwargs) def testUnicodeDatabaseName(db_kwargs): db_kwargs["database"] = "pg8000_sn\uFF6Fw" # Should only raise an exception saying db doesn't exist with pytest.raises(pg8000.ProgrammingError, match="3D000"): pg8000.connect(**db_kwargs) def testBytesDatabaseName(db_kwargs): """Should only raise an exception saying db doesn't exist""" db_kwargs["database"] = bytes("pg8000_sn\uFF6Fw", "utf8") with pytest.raises(pg8000.ProgrammingError, match="3D000"): pg8000.connect(**db_kwargs) def testBytesPassword(con, db_kwargs): # Create user username = "boltzmann" password = "cha\uFF6Fs" with con.cursor() as cur: cur.execute("create user " + username + " with password '" + password + "';") con.commit() db_kwargs["user"] = username db_kwargs["password"] = password.encode("utf8") db_kwargs["database"] = "pg8000_md5" with pytest.raises(pg8000.ProgrammingError, match="3D000"): pg8000.connect(**db_kwargs) cur.execute("drop role " + username) con.commit() def test_broken_pipe_read(con, db_kwargs): db1 = pg8000.legacy.connect(**db_kwargs) cur1 = db1.cursor() cur2 = con.cursor() cur1.execute("select pg_backend_pid()") pid1 = cur1.fetchone()[0] cur2.execute("select pg_terminate_backend(%s)", (pid1,)) with pytest.raises(pg8000.exceptions.InterfaceError, match="network error on read"): cur1.execute("select 1") def test_broken_pipe_flush(con, db_kwargs): db1 = pg8000.legacy.connect(**db_kwargs) cur1 = db1.cursor() cur2 = con.cursor() cur1.execute("select pg_backend_pid()") pid1 = cur1.fetchone()[0] cur2.execute("select pg_terminate_backend(%s)", (pid1,)) try: cur1.execute("select 1") except BaseException: pass # Can do an assert_raises when we're on 3.8 or above try: db1.close() except pg8000.exceptions.InterfaceError as e: assert str(e) == "network error on flush" def test_broken_pipe_unpack(con): cur = con.cursor() cur.execute("select pg_backend_pid()") pid1 = cur.fetchone()[0] with pytest.raises(pg8000.legacy.InterfaceError, match="network error"): cur.execute("select pg_terminate_backend(%s)", (pid1,)) def testApplicatioName(db_kwargs): app_name = "my test application name" db_kwargs["application_name"] = app_name with pg8000.connect(**db_kwargs) as db: cur = db.cursor() cur.execute( "select application_name from pg_stat_activity " " where pid = pg_backend_pid()" ) application_name = cur.fetchone()[0] assert application_name == app_name def test_application_name_integer(db_kwargs): db_kwargs["application_name"] = 1 with pytest.raises( pg8000.InterfaceError, match="The parameter application_name can't be of type .", ): pg8000.connect(**db_kwargs) def test_application_name_bytearray(db_kwargs): db_kwargs["application_name"] = bytearray(b"Philby") pg8000.connect(**db_kwargs) # This requires a line in pg_hba.conf that requires scram-sha-256 for the # database scram-sha-256 def test_scram_sha_256(db_kwargs): db_kwargs["database"] = "pg8000_scram_sha_256" # Should only raise an exception saying db doesn't exist with pytest.raises(pg8000.ProgrammingError, match="3D000"): pg8000.connect(**db_kwargs) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/test_copy.py0000664000175000017500000000556200000000000020120 0ustar00tlocketlocke00000000000000from io import BytesIO import pytest @pytest.fixture def db_table(request, con): with con.cursor() as cursor: cursor.execute( "CREATE TEMPORARY TABLE t1 (f1 int primary key, " "f2 int not null, f3 varchar(50) null) " "on commit drop" ) return con def test_copy_to_with_table(db_table): with db_table.cursor() as cursor: cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, 1)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (2, 2, 2)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (3, 3, 3)) stream = BytesIO() cursor.execute("copy t1 to stdout", stream=stream) assert stream.getvalue() == b"1\t1\t1\n2\t2\t2\n3\t3\t3\n" assert cursor.rowcount == 3 def test_copy_to_with_query(db_table): with db_table.cursor() as cursor: stream = BytesIO() cursor.execute( "COPY (SELECT 1 as One, 2 as Two) TO STDOUT WITH DELIMITER " "'X' CSV HEADER QUOTE AS 'Y' FORCE QUOTE Two", stream=stream, ) assert stream.getvalue() == b"oneXtwo\n1XY2Y\n" assert cursor.rowcount == 1 def test_copy_from_with_table(db_table): with db_table.cursor() as cursor: stream = BytesIO(b"1\t1\t1\n2\t2\t2\n3\t3\t3\n") cursor.execute("copy t1 from STDIN", stream=stream) assert cursor.rowcount == 3 cursor.execute("SELECT * FROM t1 ORDER BY f1") retval = cursor.fetchall() assert retval == ([1, 1, "1"], [2, 2, "2"], [3, 3, "3"]) def test_copy_from_with_query(db_table): with db_table.cursor() as cursor: stream = BytesIO(b"f1Xf2\n1XY1Y\n") cursor.execute( "COPY t1 (f1, f2) FROM STDIN WITH DELIMITER 'X' CSV HEADER " "QUOTE AS 'Y' FORCE NOT NULL f1", stream=stream, ) assert cursor.rowcount == 1 cursor.execute("SELECT * FROM t1 ORDER BY f1") retval = cursor.fetchall() assert retval == ([1, 1, None],) def test_copy_from_with_error(db_table): with db_table.cursor() as cursor: stream = BytesIO(b"f1Xf2\n\n1XY1Y\n") with pytest.raises(BaseException) as e: cursor.execute( "COPY t1 (f1, f2) FROM STDIN WITH DELIMITER 'X' CSV HEADER " "QUOTE AS 'Y' FORCE NOT NULL f1", stream=stream, ) arg = { "S": ("ERROR",), "C": ("22P02",), "M": ( 'invalid input syntax for type integer: ""', 'invalid input syntax for integer: ""', ), "W": ('COPY t1, line 2, column f1: ""',), "F": ("numutils.c",), "R": ("pg_atoi", "pg_strtoint32"), } earg = e.value.args[0] for k, v in arg.items(): assert earg[k] in v ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/test_dbapi.py0000664000175000017500000001411000000000000020212 0ustar00tlocketlocke00000000000000import datetime import os import time import pytest import pg8000 @pytest.fixture def has_tzset(): # Neither Windows nor Jython 2.5.3 have a time.tzset() so skip if hasattr(time, "tzset"): os.environ["TZ"] = "UTC" time.tzset() return True return False # DBAPI compatible interface tests @pytest.fixture def db_table(con, has_tzset): with con.cursor() as c: c.execute( "CREATE TEMPORARY TABLE t1 " "(f1 int primary key, f2 int not null, f3 varchar(50) null) " "ON COMMIT DROP" ) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, None)) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (2, 10, None)) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (3, 100, None)) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (4, 1000, None)) c.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (5, 10000, None)) return con def test_parallel_queries(db_table): with db_table.cursor() as c1, db_table.cursor() as c2: c1.execute("SELECT f1, f2, f3 FROM t1") while 1: row = c1.fetchone() if row is None: break f1, f2, f3 = row c2.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > %s", (f1,)) while 1: row = c2.fetchone() if row is None: break f1, f2, f3 = row def test_qmark(db_table): orig_paramstyle = pg8000.paramstyle try: pg8000.paramstyle = "qmark" with db_table.cursor() as c1: c1.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > ?", (3,)) while 1: row = c1.fetchone() if row is None: break f1, f2, f3 = row finally: pg8000.paramstyle = orig_paramstyle def test_numeric(db_table): orig_paramstyle = pg8000.paramstyle try: pg8000.paramstyle = "numeric" with db_table.cursor() as c1: c1.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > :1", (3,)) while 1: row = c1.fetchone() if row is None: break f1, f2, f3 = row finally: pg8000.paramstyle = orig_paramstyle def test_named(db_table): orig_paramstyle = pg8000.paramstyle try: pg8000.paramstyle = "named" with db_table.cursor() as c1: c1.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > :f1", {"f1": 3}) while 1: row = c1.fetchone() if row is None: break f1, f2, f3 = row finally: pg8000.paramstyle = orig_paramstyle def test_format(db_table): orig_paramstyle = pg8000.paramstyle try: pg8000.paramstyle = "format" with db_table.cursor() as c1: c1.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > %s", (3,)) while 1: row = c1.fetchone() if row is None: break f1, f2, f3 = row finally: pg8000.paramstyle = orig_paramstyle def test_pyformat(db_table): orig_paramstyle = pg8000.paramstyle try: pg8000.paramstyle = "pyformat" with db_table.cursor() as c1: c1.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > %(f1)s", {"f1": 3}) while 1: row = c1.fetchone() if row is None: break f1, f2, f3 = row finally: pg8000.paramstyle = orig_paramstyle def test_arraysize(db_table): with db_table.cursor() as c1: c1.arraysize = 3 c1.execute("SELECT * FROM t1") retval = c1.fetchmany() assert len(retval) == c1.arraysize def test_date(): val = pg8000.Date(2001, 2, 3) assert val == datetime.date(2001, 2, 3) def test_time(): val = pg8000.Time(4, 5, 6) assert val == datetime.time(4, 5, 6) def test_timestamp(): val = pg8000.Timestamp(2001, 2, 3, 4, 5, 6) assert val == datetime.datetime(2001, 2, 3, 4, 5, 6) def test_date_from_ticks(has_tzset): if has_tzset: val = pg8000.DateFromTicks(1173804319) assert val == datetime.date(2007, 3, 13) def testTimeFromTicks(has_tzset): if has_tzset: val = pg8000.TimeFromTicks(1173804319) assert val == datetime.time(16, 45, 19) def test_timestamp_from_ticks(has_tzset): if has_tzset: val = pg8000.TimestampFromTicks(1173804319) assert val == datetime.datetime(2007, 3, 13, 16, 45, 19) def test_binary(): v = pg8000.Binary(b"\x00\x01\x02\x03\x02\x01\x00") assert v == b"\x00\x01\x02\x03\x02\x01\x00" assert isinstance(v, pg8000.BINARY) def test_row_count(db_table): with db_table.cursor() as c1: c1.execute("SELECT * FROM t1") assert 5 == c1.rowcount c1.execute("UPDATE t1 SET f3 = %s WHERE f2 > 101", ("Hello!",)) assert 2 == c1.rowcount c1.execute("DELETE FROM t1") assert 5 == c1.rowcount def test_fetch_many(db_table): with db_table.cursor() as cursor: cursor.arraysize = 2 cursor.execute("SELECT * FROM t1") assert 2 == len(cursor.fetchmany()) assert 2 == len(cursor.fetchmany()) assert 1 == len(cursor.fetchmany()) assert 0 == len(cursor.fetchmany()) def test_iterator(db_table): with db_table.cursor() as cursor: cursor.execute("SELECT * FROM t1 ORDER BY f1") f1 = 0 for row in cursor: next_f1 = row[0] assert next_f1 > f1 f1 = next_f1 # Vacuum can't be run inside a transaction, so we need to turn # autocommit on. def test_vacuum(con): con.autocommit = True with con.cursor() as cursor: cursor.execute("vacuum") def test_prepared_statement(con): with con.cursor() as cursor: cursor.execute("PREPARE gen_series AS SELECT generate_series(1, 10);") cursor.execute("EXECUTE gen_series") def test_cursor_type(cursor): assert str(type(cursor)) == "" ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/test_dbapi20.py0000664000175000017500000005345700000000000020375 0ustar00tlocketlocke00000000000000import time import warnings import pytest import pg8000 """ Python DB API 2.0 driver compliance unit test suite. This software is Public Domain and may be used without restrictions. "Now we have booze and barflies entering the discussion, plus rumours of DBAs on drugs... and I won't tell you what flashes through my mind each time I read the subject line with 'Anal Compliance' in it. All around this is turning out to be a thoroughly unwholesome unit test." -- Ian Bicking """ __rcs_id__ = "$Id: dbapi20.py,v 1.10 2003/10/09 03:14:14 zenzen Exp $" __version__ = "$Revision: 1.10 $"[11:-2] __author__ = "Stuart Bishop " # $Log: dbapi20.py,v $ # Revision 1.10 2003/10/09 03:14:14 zenzen # Add test for DB API 2.0 optional extension, where database exceptions # are exposed as attributes on the Connection object. # # Revision 1.9 2003/08/13 01:16:36 zenzen # Minor tweak from Stefan Fleiter # # Revision 1.8 2003/04/10 00:13:25 zenzen # Changes, as per suggestions by M.-A. Lemburg # - Add a table prefix, to ensure namespace collisions can always be avoided # # Revision 1.7 2003/02/26 23:33:37 zenzen # Break out DDL into helper functions, as per request by David Rushby # # Revision 1.6 2003/02/21 03:04:33 zenzen # Stuff from Henrik Ekelund: # added test_None # added test_nextset & hooks # # Revision 1.5 2003/02/17 22:08:43 zenzen # Implement suggestions and code from Henrik Eklund - test that # cursor.arraysize defaults to 1 & generic cursor.callproc test added # # Revision 1.4 2003/02/15 00:16:33 zenzen # Changes, as per suggestions and bug reports by M.-A. Lemburg, # Matthew T. Kromer, Federico Di Gregorio and Daniel Dittmar # - Class renamed # - Now a subclass of TestCase, to avoid requiring the driver stub # to use multiple inheritance # - Reversed the polarity of buggy test in test_description # - Test exception heirarchy correctly # - self.populate is now self._populate(), so if a driver stub # overrides self.ddl1 this change propogates # - VARCHAR columns now have a width, which will hopefully make the # DDL even more portible (this will be reversed if it causes more problems) # - cursor.rowcount being checked after various execute and fetchXXX methods # - Check for fetchall and fetchmany returning empty lists after results # are exhausted (already checking for empty lists if select retrieved # nothing # - Fix bugs in test_setoutputsize_basic and test_setinputsizes # """ Test a database self.driver for DB API 2.0 compatibility. This implementation tests Gadfly, but the TestCase is structured so that other self.drivers can subclass this test case to ensure compiliance with the DB-API. It is expected that this TestCase may be expanded in the future if ambiguities or edge conditions are discovered. The 'Optional Extensions' are not yet being tested. self.drivers should subclass this test, overriding setUp, tearDown, self.driver, connect_args and connect_kw_args. Class specification should be as follows: import dbapi20 class mytest(dbapi20.DatabaseAPI20Test): [...] Don't 'import DatabaseAPI20Test from dbapi20', or you will confuse the unit tester - just 'import dbapi20'. """ # The self.driver module. This should be the module where the 'connect' # method is to be found driver = pg8000 table_prefix = "dbapi20test_" # If you need to specify a prefix for tables ddl1 = "create table %sbooze (name varchar(20))" % table_prefix ddl2 = "create table %sbarflys (name varchar(20))" % table_prefix xddl1 = "drop table %sbooze" % table_prefix xddl2 = "drop table %sbarflys" % table_prefix # Name of stored procedure to convert # string->lowercase lowerfunc = "lower" # Some drivers may need to override these helpers, for example adding # a 'commit' after the execute. def executeDDL1(cursor): cursor.execute(ddl1) def executeDDL2(cursor): cursor.execute(ddl2) @pytest.fixture def db(request, con): def fin(): with con.cursor() as cur: for ddl in (xddl1, xddl2): try: cur.execute(ddl) con.commit() except driver.Error: # Assume table didn't exist. Other tests will check if # execute is busted. pass request.addfinalizer(fin) return con def test_apilevel(): # Must exist apilevel = driver.apilevel # Must equal 2.0 assert apilevel == "2.0" def test_threadsafety(): try: # Must exist threadsafety = driver.threadsafety # Must be a valid value assert threadsafety in (0, 1, 2, 3) except AttributeError: assert False, "Driver doesn't define threadsafety" def test_paramstyle(): try: # Must exist paramstyle = driver.paramstyle # Must be a valid value assert paramstyle in ("qmark", "numeric", "named", "format", "pyformat") except AttributeError: assert False, "Driver doesn't define paramstyle" def test_Exceptions(): # Make sure required exceptions exist, and are in the # defined heirarchy. assert issubclass(driver.Warning, Exception) assert issubclass(driver.Error, Exception) assert issubclass(driver.InterfaceError, driver.Error) assert issubclass(driver.DatabaseError, driver.Error) assert issubclass(driver.OperationalError, driver.Error) assert issubclass(driver.IntegrityError, driver.Error) assert issubclass(driver.InternalError, driver.Error) assert issubclass(driver.ProgrammingError, driver.Error) assert issubclass(driver.NotSupportedError, driver.Error) def test_ExceptionsAsConnectionAttributes(con): # OPTIONAL EXTENSION # Test for the optional DB API 2.0 extension, where the exceptions # are exposed as attributes on the Connection object # I figure this optional extension will be implemented by any # driver author who is using this test suite, so it is enabled # by default. warnings.simplefilter("ignore") drv = driver assert con.Warning is drv.Warning assert con.Error is drv.Error assert con.InterfaceError is drv.InterfaceError assert con.DatabaseError is drv.DatabaseError assert con.OperationalError is drv.OperationalError assert con.IntegrityError is drv.IntegrityError assert con.InternalError is drv.InternalError assert con.ProgrammingError is drv.ProgrammingError assert con.NotSupportedError is drv.NotSupportedError warnings.resetwarnings() def test_commit(con): # Commit must work, even if it doesn't do anything con.commit() def test_rollback(con): # If rollback is defined, it should either work or throw # the documented exception if hasattr(con, "rollback"): try: con.rollback() except driver.NotSupportedError: pass def test_cursor(con): con.cursor() def test_cursor_isolation(con): # Make sure cursors created from the same connection have # the documented transaction isolation level cur1 = con.cursor() cur2 = con.cursor() executeDDL1(cur1) cur1.execute("insert into %sbooze values ('Victoria Bitter')" % (table_prefix)) cur2.execute("select name from %sbooze" % table_prefix) booze = cur2.fetchall() assert len(booze) == 1 assert len(booze[0]) == 1 assert booze[0][0] == "Victoria Bitter" def test_description(con): cur = con.cursor() executeDDL1(cur) assert cur.description is None, ( "cursor.description should be none after executing a " "statement that can return no rows (such as DDL)" ) cur.execute("select name from %sbooze" % table_prefix) assert len(cur.description) == 1, "cursor.description describes too many columns" assert ( len(cur.description[0]) == 7 ), "cursor.description[x] tuples must have 7 elements" assert ( cur.description[0][0].lower() == "name" ), "cursor.description[x][0] must return column name" assert cur.description[0][1] == driver.STRING, ( "cursor.description[x][1] must return column type. Got %r" % cur.description[0][1] ) # Make sure self.description gets reset executeDDL2(cur) assert cur.description is None, ( "cursor.description not being set to None when executing " "no-result statements (eg. DDL)" ) def test_rowcount(cursor): executeDDL1(cursor) assert cursor.rowcount == -1, ( "cursor.rowcount should be -1 after executing no-result " "statements" ) cursor.execute("insert into %sbooze values ('Victoria Bitter')" % (table_prefix)) assert cursor.rowcount in (-1, 1), ( "cursor.rowcount should == number or rows inserted, or " "set to -1 after executing an insert statement" ) cursor.execute("select name from %sbooze" % table_prefix) assert cursor.rowcount in (-1, 1), ( "cursor.rowcount should == number of rows returned, or " "set to -1 after executing a select statement" ) executeDDL2(cursor) assert cursor.rowcount == -1, ( "cursor.rowcount not being reset to -1 after executing " "no-result statements" ) lower_func = "lower" def test_callproc(cursor): if lower_func and hasattr(cursor, "callproc"): r = cursor.callproc(lower_func, ("FOO",)) assert len(r) == 1 assert r[0] == "FOO" r = cursor.fetchall() assert len(r) == 1, "callproc produced no result set" assert len(r[0]) == 1, "callproc produced invalid result set" assert r[0][0] == "foo", "callproc produced invalid results" def test_close(con): cur = con.cursor() con.close() # cursor.execute should raise an Error if called after connection # closed with pytest.raises(driver.Error): executeDDL1(cur) # connection.commit should raise an Error if called after connection' # closed.' with pytest.raises(driver.Error): con.commit() # connection.close should raise an Error if called more than once with pytest.raises(driver.Error): con.close() def test_execute(con): cur = con.cursor() _paraminsert(cur) def _paraminsert(cur): executeDDL1(cur) cur.execute("insert into %sbooze values ('Victoria Bitter')" % (table_prefix)) assert cur.rowcount in (-1, 1) if driver.paramstyle == "qmark": cur.execute("insert into %sbooze values (?)" % table_prefix, ("Cooper's",)) elif driver.paramstyle == "numeric": cur.execute("insert into %sbooze values (:1)" % table_prefix, ("Cooper's",)) elif driver.paramstyle == "named": cur.execute( "insert into %sbooze values (:beer)" % table_prefix, {"beer": "Cooper's"} ) elif driver.paramstyle == "format": cur.execute("insert into %sbooze values (%%s)" % table_prefix, ("Cooper's",)) elif driver.paramstyle == "pyformat": cur.execute( "insert into %sbooze values (%%(beer)s)" % table_prefix, {"beer": "Cooper's"}, ) else: assert False, "Invalid paramstyle" assert cur.rowcount in (-1, 1) cur.execute("select name from %sbooze" % table_prefix) res = cur.fetchall() assert len(res) == 2, "cursor.fetchall returned too few rows" beers = [res[0][0], res[1][0]] beers.sort() assert beers[0] == "Cooper's", ( "cursor.fetchall retrieved incorrect data, or data inserted " "incorrectly" ) assert beers[1] == "Victoria Bitter", ( "cursor.fetchall retrieved incorrect data, or data inserted " "incorrectly" ) def test_executemany(cursor): executeDDL1(cursor) largs = [("Cooper's",), ("Boag's",)] margs = [{"beer": "Cooper's"}, {"beer": "Boag's"}] if driver.paramstyle == "qmark": cursor.executemany("insert into %sbooze values (?)" % table_prefix, largs) elif driver.paramstyle == "numeric": cursor.executemany("insert into %sbooze values (:1)" % table_prefix, largs) elif driver.paramstyle == "named": cursor.executemany("insert into %sbooze values (:beer)" % table_prefix, margs) elif driver.paramstyle == "format": cursor.executemany("insert into %sbooze values (%%s)" % table_prefix, largs) elif driver.paramstyle == "pyformat": cursor.executemany( "insert into %sbooze values (%%(beer)s)" % (table_prefix), margs ) else: assert False, "Unknown paramstyle" assert cursor.rowcount in (-1, 2), ( "insert using cursor.executemany set cursor.rowcount to " "incorrect value %r" % cursor.rowcount ) cursor.execute("select name from %sbooze" % table_prefix) res = cursor.fetchall() assert len(res) == 2, "cursor.fetchall retrieved incorrect number of rows" beers = [res[0][0], res[1][0]] beers.sort() assert beers[0] == "Boag's", "incorrect data retrieved" assert beers[1] == "Cooper's", "incorrect data retrieved" def test_fetchone(cursor): # cursor.fetchone should raise an Error if called before # executing a select-type query with pytest.raises(driver.Error): cursor.fetchone() # cursor.fetchone should raise an Error if called after # executing a query that cannnot return rows executeDDL1(cursor) with pytest.raises(driver.Error): cursor.fetchone() cursor.execute("select name from %sbooze" % table_prefix) assert cursor.fetchone() is None, ( "cursor.fetchone should return None if a query retrieves " "no rows" ) assert cursor.rowcount in (-1, 0) # cursor.fetchone should raise an Error if called after # executing a query that cannnot return rows cursor.execute("insert into %sbooze values ('Victoria Bitter')" % (table_prefix)) with pytest.raises(driver.Error): cursor.fetchone() cursor.execute("select name from %sbooze" % table_prefix) r = cursor.fetchone() assert len(r) == 1, "cursor.fetchone should have retrieved a single row" assert r[0] == "Victoria Bitter", "cursor.fetchone retrieved incorrect data" assert ( cursor.fetchone() is None ), "cursor.fetchone should return None if no more rows available" assert cursor.rowcount in (-1, 1) samples = [ "Carlton Cold", "Carlton Draft", "Mountain Goat", "Redback", "Victoria Bitter", "XXXX", ] def _populate(): """Return a list of sql commands to setup the DB for the fetch tests. """ populate = [ "insert into %sbooze values ('%s')" % (table_prefix, s) for s in samples ] return populate def test_fetchmany(cursor): # cursor.fetchmany should raise an Error if called without # issuing a query with pytest.raises(driver.Error): cursor.fetchmany(4) executeDDL1(cursor) for sql in _populate(): cursor.execute(sql) cursor.execute("select name from %sbooze" % table_prefix) r = cursor.fetchmany() assert len(r) == 1, ( "cursor.fetchmany retrieved incorrect number of rows, " "default of arraysize is one." ) cursor.arraysize = 10 r = cursor.fetchmany(3) # Should get 3 rows assert len(r) == 3, "cursor.fetchmany retrieved incorrect number of rows" r = cursor.fetchmany(4) # Should get 2 more assert len(r) == 2, "cursor.fetchmany retrieved incorrect number of rows" r = cursor.fetchmany(4) # Should be an empty sequence assert len(r) == 0, ( "cursor.fetchmany should return an empty sequence after " "results are exhausted" ) assert cursor.rowcount in (-1, 6) # Same as above, using cursor.arraysize cursor.arraysize = 4 cursor.execute("select name from %sbooze" % table_prefix) r = cursor.fetchmany() # Should get 4 rows assert len(r) == 4, "cursor.arraysize not being honoured by fetchmany" r = cursor.fetchmany() # Should get 2 more assert len(r) == 2 r = cursor.fetchmany() # Should be an empty sequence assert len(r) == 0 assert cursor.rowcount in (-1, 6) cursor.arraysize = 6 cursor.execute("select name from %sbooze" % table_prefix) rows = cursor.fetchmany() # Should get all rows assert cursor.rowcount in (-1, 6) assert len(rows) == 6 assert len(rows) == 6 rows = [row[0] for row in rows] rows.sort() # Make sure we get the right data back out for i in range(0, 6): assert rows[i] == samples[i], "incorrect data retrieved by cursor.fetchmany" rows = cursor.fetchmany() # Should return an empty list assert len(rows) == 0, ( "cursor.fetchmany should return an empty sequence if " "called after the whole result set has been fetched" ) assert cursor.rowcount in (-1, 6) executeDDL2(cursor) cursor.execute("select name from %sbarflys" % table_prefix) r = cursor.fetchmany() # Should get empty sequence assert len(r) == 0, ( "cursor.fetchmany should return an empty sequence if " "query retrieved no rows" ) assert cursor.rowcount in (-1, 0) def test_fetchall(cursor): # cursor.fetchall should raise an Error if called # without executing a query that may return rows (such # as a select) with pytest.raises(driver.Error): cursor.fetchall() executeDDL1(cursor) for sql in _populate(): cursor.execute(sql) # cursor.fetchall should raise an Error if called # after executing a a statement that cannot return rows with pytest.raises(driver.Error): cursor.fetchall() cursor.execute("select name from %sbooze" % table_prefix) rows = cursor.fetchall() assert cursor.rowcount in (-1, len(samples)) assert len(rows) == len(samples), "cursor.fetchall did not retrieve all rows" rows = [r[0] for r in rows] rows.sort() for i in range(0, len(samples)): assert rows[i] == samples[i], "cursor.fetchall retrieved incorrect rows" rows = cursor.fetchall() assert len(rows) == 0, ( "cursor.fetchall should return an empty list if called " "after the whole result set has been fetched" ) assert cursor.rowcount in (-1, len(samples)) executeDDL2(cursor) cursor.execute("select name from %sbarflys" % table_prefix) rows = cursor.fetchall() assert cursor.rowcount in (-1, 0) assert len(rows) == 0, ( "cursor.fetchall should return an empty list if " "a select query returns no rows" ) def test_mixedfetch(cursor): executeDDL1(cursor) for sql in _populate(): cursor.execute(sql) cursor.execute("select name from %sbooze" % table_prefix) rows1 = cursor.fetchone() rows23 = cursor.fetchmany(2) rows4 = cursor.fetchone() rows56 = cursor.fetchall() assert cursor.rowcount in (-1, 6) assert len(rows23) == 2, "fetchmany returned incorrect number of rows" assert len(rows56) == 2, "fetchall returned incorrect number of rows" rows = [rows1[0]] rows.extend([rows23[0][0], rows23[1][0]]) rows.append(rows4[0]) rows.extend([rows56[0][0], rows56[1][0]]) rows.sort() for i in range(0, len(samples)): assert rows[i] == samples[i], "incorrect data retrieved or inserted" def help_nextset_setUp(cur): """Should create a procedure called deleteme that returns two result sets, first the number of rows in booze then "name from booze" """ raise NotImplementedError("Helper not implemented") def help_nextset_tearDown(cur): "If cleaning up is needed after nextSetTest" raise NotImplementedError("Helper not implemented") def test_nextset(cursor): if not hasattr(cursor, "nextset"): return try: executeDDL1(cursor) sql = _populate() for sql in _populate(): cursor.execute(sql) help_nextset_setUp(cursor) cursor.callproc("deleteme") numberofrows = cursor.fetchone() assert numberofrows[0] == len(samples) assert cursor.nextset() names = cursor.fetchall() assert len(names) == len(samples) s = cursor.nextset() assert s is None, "No more return sets, should return None" finally: help_nextset_tearDown(cursor) def test_arraysize(cursor): # Not much here - rest of the tests for this are in test_fetchmany assert hasattr(cursor, "arraysize"), "cursor.arraysize must be defined" def test_setinputsizes(cursor): cursor.setinputsizes(25) def test_setoutputsize_basic(cursor): # Basic test is to make sure setoutputsize doesn't blow up cursor.setoutputsize(1000) cursor.setoutputsize(2000, 0) _paraminsert(cursor) # Make sure the cursor still works def test_None(cursor): executeDDL1(cursor) cursor.execute("insert into %sbooze values (NULL)" % table_prefix) cursor.execute("select name from %sbooze" % table_prefix) r = cursor.fetchall() assert len(r) == 1 assert len(r[0]) == 1 assert r[0][0] is None, "NULL value not returned as None" def test_Date(): driver.Date(2002, 12, 25) driver.DateFromTicks(time.mktime((2002, 12, 25, 0, 0, 0, 0, 0, 0))) # Can we assume this? API doesn't specify, but it seems implied # self.assertEqual(str(d1),str(d2)) def test_Time(): driver.Time(13, 45, 30) driver.TimeFromTicks(time.mktime((2001, 1, 1, 13, 45, 30, 0, 0, 0))) # Can we assume this? API doesn't specify, but it seems implied # self.assertEqual(str(t1),str(t2)) def test_Timestamp(): driver.Timestamp(2002, 12, 25, 13, 45, 30) driver.TimestampFromTicks(time.mktime((2002, 12, 25, 13, 45, 30, 0, 0, 0))) # Can we assume this? API doesn't specify, but it seems implied # self.assertEqual(str(t1),str(t2)) def test_Binary(): driver.Binary(b"Something") driver.Binary(b"") def test_STRING(): assert hasattr(driver, "STRING"), "module.STRING must be defined" def test_BINARY(): assert hasattr(driver, "BINARY"), "module.BINARY must be defined." def test_NUMBER(): assert hasattr(driver, "NUMBER"), "module.NUMBER must be defined." def test_DATETIME(): assert hasattr(driver, "DATETIME"), "module.DATETIME must be defined." def test_ROWID(): assert hasattr(driver, "ROWID"), "module.ROWID must be defined." ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1634149967.0 pg8000-1.23.0/test/legacy/test_error_recovery.py0000664000175000017500000000252500000000000022211 0ustar00tlocketlocke00000000000000import datetime import warnings import pytest import pg8000 class PG8000TestException(Exception): pass def raise_exception(val): raise PG8000TestException("oh noes!") def test_py_value_fail(con, mocker): # Ensure that if types.py_value throws an exception, the original # exception is raised (PG8000TestException), and the connection is # still usable after the error. mocker.patch.object(con, "py_types") con.py_types = {datetime.time: raise_exception} with con.cursor() as c, pytest.raises(PG8000TestException): c.execute("SELECT CAST(%s AS TIME)", (datetime.time(10, 30),)) c.fetchall() # ensure that the connection is still usable for a new query c.execute("VALUES ('hw3'::text)") assert c.fetchone()[0] == "hw3" def test_no_data_error_recovery(con): for i in range(1, 4): with con.cursor() as c, pytest.raises(pg8000.ProgrammingError) as e: c.execute("DROP TABLE t1") assert e.value.args[0]["C"] == "42P01" con.rollback() def testClosedConnection(db_kwargs): warnings.simplefilter("ignore") my_db = pg8000.connect(**db_kwargs) cursor = my_db.cursor() my_db.close() with pytest.raises(my_db.InterfaceError, match="connection is closed"): cursor.execute("VALUES ('hw1'::text)") warnings.resetwarnings() ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/test_paramstyle.py0000664000175000017500000000613000000000000021317 0ustar00tlocketlocke00000000000000from pg8000.legacy import convert_paramstyle as convert # Tests of the convert_paramstyle function. def test_qmark(): args = 1, 2, 3 new_query, vals = convert( "qmark", 'SELECT ?, ?, "field_?" FROM t ' "WHERE a='say ''what?''' AND b=? AND c=E'?\\'test\\'?'", args, ) expected = ( 'SELECT $1, $2, "field_?" FROM t WHERE ' "a='say ''what?''' AND b=$3 AND c=E'?\\'test\\'?'" ) assert (new_query, vals) == (expected, args) def test_qmark_2(): args = 1, 2, 3 new_query, vals = convert( "qmark", "SELECT ?, ?, * FROM t WHERE a=? AND b='are you ''sure?'", args ) expected = "SELECT $1, $2, * FROM t WHERE a=$3 AND b='are you ''sure?'" assert (new_query, vals) == (expected, args) def test_numeric(): args = 1, 2, 3 new_query, vals = convert( "numeric", "SELECT sum(x)::decimal(5, 2) :2, :1, * FROM t WHERE a=:3", args ) expected = "SELECT sum(x)::decimal(5, 2) $2, $1, * FROM t WHERE a=$3" assert (new_query, vals) == (expected, args) def test_numeric_default_parameter(): args = 1, 2, 3 new_query, vals = convert("numeric", "make_interval(days := 10)", args) assert (new_query, vals) == ("make_interval(days := 10)", args) def test_named(): args = { "f_2": 1, "f1": 2, } new_query, vals = convert( "named", "SELECT sum(x)::decimal(5, 2) :f_2, :f1 FROM t WHERE a=:f_2", args ) expected = "SELECT sum(x)::decimal(5, 2) $1, $2 FROM t WHERE a=$1" assert (new_query, vals) == (expected, (1, 2)) def test_format(): args = 1, 2, 3 new_query, vals = convert( "format", "SELECT %s, %s, \"f1_%%\", E'txt_%%' " "FROM t WHERE a=%s AND b='75%%' AND c = '%' -- Comment with %", args, ) expected = ( "SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$3 AND " "b='75%%' AND c = '%' -- Comment with %" ) assert (new_query, vals) == (expected, args) sql = ( r"""COMMENT ON TABLE test_schema.comment_test """ r"""IS 'the test % '' " \ table comment'""" ) new_query, vals = convert("format", sql, args) assert (new_query, vals) == (sql, args) def test_format_multiline(): args = 1, 2, 3 new_query, vals = convert("format", "SELECT -- Comment\n%s FROM t", args) assert (new_query, vals) == ("SELECT -- Comment\n$1 FROM t", args) def test_py_format(): args = {"f2": 1, "f1": 2, "f3": 3} new_query, vals = convert( "pyformat", "SELECT %(f2)s, %(f1)s, \"f1_%%\", E'txt_%%' " "FROM t WHERE a=%(f2)s AND b='75%%'", args, ) expected = "SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$1 AND " "b='75%%'" assert (new_query, vals) == (expected, (1, 2)) # pyformat should support %s and an array, too: args = 1, 2, 3 new_query, vals = convert( "pyformat", "SELECT %s, %s, \"f1_%%\", E'txt_%%' " "FROM t WHERE a=%s AND b='75%%'", args, ) expected = "SELECT $1, $2, \"f1_%%\", E'txt_%%' FROM t WHERE a=$3 AND " "b='75%%'" assert (new_query, vals) == (expected, args) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1634149967.0 pg8000-1.23.0/test/legacy/test_prepared_statement.py0000664000175000017500000000036700000000000023032 0ustar00tlocketlocke00000000000000def test_prepare(con): con.prepare("SELECT CAST(:v AS INTEGER)") def test_run(con): ps = con.prepare("SELECT cast(:v as varchar)") ps.run(v="speedy") def test_run_with_no_results(con): ps = con.prepare("ROLLBACK") ps.run() ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636749156.0 pg8000-1.23.0/test/legacy/test_query.py0000664000175000017500000002420600000000000020307 0ustar00tlocketlocke00000000000000from datetime import datetime as Datetime, timezone as Timezone from warnings import filterwarnings import pytest import pg8000 from pg8000.converters import INET_ARRAY, INTEGER # Tests relating to the basic operation of the database driver, driven by the # pg8000 custom interface. @pytest.fixture def db_table(request, con): filterwarnings("ignore", "DB-API extension cursor.next()") filterwarnings("ignore", "DB-API extension cursor.__iter__()") con.paramstyle = "format" with con.cursor() as cursor: cursor.execute( "CREATE TEMPORARY TABLE t1 (f1 int primary key, " "f2 bigint not null, f3 varchar(50) null) " ) def fin(): try: with con.cursor() as cursor: cursor.execute("drop table t1") except pg8000.ProgrammingError: pass request.addfinalizer(fin) return con def test_database_error(cursor): with pytest.raises(pg8000.ProgrammingError): cursor.execute("INSERT INTO t99 VALUES (1, 2, 3)") def test_parallel_queries(db_table): with db_table.cursor() as cursor: cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, None)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (2, 10, None)) cursor.execute( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (3, 100, None) ) cursor.execute( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (4, 1000, None) ) cursor.execute( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (5, 10000, None) ) with db_table.cursor() as c1, db_table.cursor() as c2: c1.execute("SELECT f1, f2, f3 FROM t1") for row in c1: f1, f2, f3 = row c2.execute("SELECT f1, f2, f3 FROM t1 WHERE f1 > %s", (f1,)) for row in c2: f1, f2, f3 = row def test_parallel_open_portals(con): with con.cursor() as c1, con.cursor() as c2: c1count, c2count = 0, 0 q = "select * from generate_series(1, %s)" params = (100,) c1.execute(q, params) c2.execute(q, params) for c2row in c2: c2count += 1 for c1row in c1: c1count += 1 assert c1count == c2count # Run a query on a table, alter the structure of the table, then run the # original query again. def test_alter(db_table): with db_table.cursor() as cursor: cursor.execute("select * from t1") cursor.execute("alter table t1 drop column f3") cursor.execute("select * from t1") # Run a query on a table, drop then re-create the table, then run the # original query again. def test_create(db_table): with db_table.cursor() as cursor: cursor.execute("select * from t1") cursor.execute("drop table t1") cursor.execute("create temporary table t1 (f1 int primary key)") cursor.execute("select * from t1") def test_insert_returning(db_table): with db_table.cursor() as cursor: cursor.execute("CREATE TABLE t2 (id serial, data text)") # Test INSERT ... RETURNING with one row... cursor.execute("INSERT INTO t2 (data) VALUES (%s) RETURNING id", ("test1",)) row_id = cursor.fetchone()[0] cursor.execute("SELECT data FROM t2 WHERE id = %s", (row_id,)) assert "test1" == cursor.fetchone()[0] assert cursor.rowcount == 1 # Test with multiple rows... cursor.execute( "INSERT INTO t2 (data) VALUES (%s), (%s), (%s) " "RETURNING id", ("test2", "test3", "test4"), ) assert cursor.rowcount == 3 ids = tuple([x[0] for x in cursor]) assert len(ids) == 3 def test_row_count(db_table): with db_table.cursor() as cursor: expected_count = 57 cursor.executemany( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", tuple((i, i, None) for i in range(expected_count)), ) # Check rowcount after executemany assert expected_count == cursor.rowcount cursor.execute("SELECT * FROM t1") # Check row_count without doing any reading first... assert expected_count == cursor.rowcount # Check rowcount after reading some rows, make sure it still # works... for i in range(expected_count // 2): cursor.fetchone() assert expected_count == cursor.rowcount with db_table.cursor() as cursor: # Restart the cursor, read a few rows, and then check rowcount # again... cursor.execute("SELECT * FROM t1") for i in range(expected_count // 3): cursor.fetchone() assert expected_count == cursor.rowcount # Should be -1 for a command with no results cursor.execute("DROP TABLE t1") assert -1 == cursor.rowcount def test_row_count_update(db_table): with db_table.cursor() as cursor: cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, None)) cursor.execute("INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (2, 10, None)) cursor.execute( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (3, 100, None) ) cursor.execute( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (4, 1000, None) ) cursor.execute( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (5, 10000, None) ) cursor.execute("UPDATE t1 SET f3 = %s WHERE f2 > 101", ("Hello!",)) assert cursor.rowcount == 2 def test_int_oid(cursor): # https://bugs.launchpad.net/pg8000/+bug/230796 cursor.execute("SELECT typname FROM pg_type WHERE oid = %s", (100,)) def test_unicode_query(cursor): cursor.execute( "CREATE TEMPORARY TABLE \u043c\u0435\u0441\u0442\u043e " "(\u0438\u043c\u044f VARCHAR(50), " "\u0430\u0434\u0440\u0435\u0441 VARCHAR(250))" ) def test_executemany(db_table): with db_table.cursor() as cursor: cursor.executemany( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", ((1, 1, "Avast ye!"), (2, 1, None)), ) cursor.executemany( "SELECT CAST(%s AS TIMESTAMP)", ((Datetime(2014, 5, 7, tzinfo=Timezone.utc),), (Datetime(2014, 5, 7),)), ) def test_executemany_setinputsizes(cursor): """Make sure that setinputsizes works for all the parameter sets""" cursor.execute( "CREATE TEMPORARY TABLE t1 (f1 int primary key, f2 inet[] not null) " ) cursor.setinputsizes(INTEGER, INET_ARRAY) cursor.executemany( "INSERT INTO t1 (f1, f2) VALUES (%s, %s)", ((1, ["1.1.1.1"]), (2, ["0.0.0.0"])) ) def test_executemany_no_param_sets(cursor): cursor.executemany("INSERT INTO t1 (f1, f2) VALUES (%s, %s)", []) assert cursor.rowcount == -1 # Check that autocommit stays off # We keep track of whether we're in a transaction or not by using the # READY_FOR_QUERY message. def test_transactions(db_table): with db_table.cursor() as cursor: cursor.execute("commit") cursor.execute( "INSERT INTO t1 (f1, f2, f3) VALUES (%s, %s, %s)", (1, 1, "Zombie") ) cursor.execute("rollback") cursor.execute("select * from t1") assert cursor.rowcount == 0 def test_in(cursor): cursor.execute("SELECT typname FROM pg_type WHERE oid = any(%s)", ([16, 23],)) ret = cursor.fetchall() assert ret[0][0] == "bool" def test_no_previous_tpc(con): con.tpc_begin("Stacey") with con.cursor() as cursor: cursor.execute("SELECT * FROM pg_type") con.tpc_commit() # Check that tpc_recover() doesn't start a transaction def test_tpc_recover(con): con.tpc_recover() with con.cursor() as cursor: con.autocommit = True # If tpc_recover() has started a transaction, this will fail cursor.execute("VACUUM") def test_tpc_prepare(con): xid = "Stacey" con.tpc_begin(xid) con.tpc_prepare() con.tpc_rollback(xid) # An empty query should raise a ProgrammingError def test_empty_query(cursor): with pytest.raises(pg8000.ProgrammingError): cursor.execute("") # rolling back when not in a transaction doesn't generate a warning def test_rollback_no_transaction(con): # Remove any existing notices con.notices.clear() # First, verify that a raw rollback does produce a notice con.execute_unnamed("rollback") assert 1 == len(con.notices) # 25P01 is the code for no_active_sql_tronsaction. It has # a message and severity name, but those might be # localized/depend on the server version. assert con.notices.pop().get(b"C") == b"25P01" # Now going through the rollback method doesn't produce # any notices because it knows we're not in a transaction. con.rollback() assert 0 == len(con.notices) def test_context_manager_class(con): assert "__enter__" in pg8000.legacy.Cursor.__dict__ assert "__exit__" in pg8000.legacy.Cursor.__dict__ with con.cursor() as cursor: cursor.execute("select 1") def test_close_prepared_statement(con): ps = con.prepare("select 1") ps.run() res = con.run("select count(*) from pg_prepared_statements") assert res[0][0] == 1 # Should have one prepared statement ps.close() res = con.run("select count(*) from pg_prepared_statements") assert res[0][0] == 0 # Should have no prepared statements def test_setinputsizes(con): cursor = con.cursor() cursor.setinputsizes(20) cursor.execute("select %s", (None,)) retval = cursor.fetchall() assert retval[0][0] is None def test_setinputsizes_class(con): cursor = con.cursor() cursor.setinputsizes(bytes) cursor.execute("select %s", (None,)) retval = cursor.fetchall() assert retval[0][0] is None def test_unexecuted_cursor_rowcount(con): cursor = con.cursor() assert cursor.rowcount == -1 def test_unexecuted_cursor_description(con): cursor = con.cursor() assert cursor.description is None def test_not_parsed_if_no_params(mocker, cursor): mock_convert_paramstyle = mocker.patch("pg8000.legacy.convert_paramstyle") cursor.execute("ROLLBACK") mock_convert_paramstyle.assert_not_called() ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1634149967.0 pg8000-1.23.0/test/legacy/test_typeconversion.py0000664000175000017500000005631300000000000022235 0ustar00tlocketlocke00000000000000import decimal import ipaddress import os import time import uuid from collections import OrderedDict from datetime import ( date as Date, datetime as Datetime, time as Time, timedelta as Timedelta, timezone as Timezone, ) from enum import Enum from json import dumps import pytest import pytz import pg8000.converters from pg8000 import ( Binary, INTERVAL, PGInterval, ProgrammingError, pginterval_in, pginterval_out, timedelta_in, ) # Type conversion tests def test_time_roundtrip(con): retval = con.run("SELECT cast(:t as time) as f1", t=Time(4, 5, 6)) assert retval[0][0] == Time(4, 5, 6) def test_date_roundtrip(con): v = Date(2001, 2, 3) retval = con.run("SELECT cast(:d as date) as f1", d=v) assert retval[0][0] == v def test_bool_roundtrip(con): retval = con.run("SELECT cast(:b as bool) as f1", b=True) assert retval[0][0] is True def test_null_roundtrip(con): retval = con.run("select current_setting('server_version')") version = retval[0][0][:2] if version.startswith("9"): # Prior to PostgreSQL version 10 We can't just "SELECT %s" and set # None as the parameter, since it has no type. That would result # in a PG error, "could not determine data type of parameter %s". # So we create a temporary table, insert null values, and read them # back. con.run( "CREATE TEMPORARY TABLE TestNullWrite " "(f1 int4, f2 timestamp, f3 varchar)" ) con.run( "INSERT INTO TestNullWrite VALUES (:v1, :v2, :v3)", v1=None, v2=None, v3=None, ) retval = con.run("SELECT * FROM TestNullWrite") assert retval[0] == [None, None, None] with pytest.raises(ProgrammingError): con.run("SELECT :v as f1", v=None) else: retval = con.run("SELECT :v", v=None) assert retval[0][0] is None def test_decimal_roundtrip(cursor): values = ("1.1", "-1.1", "10000", "20000", "-1000000000.123456789", "1.0", "12.44") for v in values: cursor.execute("SELECT CAST(%s AS NUMERIC)", (decimal.Decimal(v),)) retval = cursor.fetchall() assert str(retval[0][0]) == v def test_float_roundtrip(con): val = 1.756e-12 retval = con.run("SELECT cast(:v as double precision)", v=val) assert retval[0][0] == val def test_float_plus_infinity_roundtrip(con): v = float("inf") retval = con.run("SELECT cast(:v as double precision)", v=v) assert retval[0][0] == v def test_str_roundtrip(cursor): v = "hello world" cursor.execute("create temporary table test_str (f character varying(255))") cursor.execute("INSERT INTO test_str VALUES (%s)", (v,)) retval = tuple(cursor.execute("SELECT * from test_str")) assert retval[0][0] == v def test_str_then_int(cursor): v1 = "hello world" retval = tuple(cursor.execute("SELECT cast(%s as varchar) as f1", (v1,))) assert retval[0][0] == v1 v2 = 1 retval = tuple(cursor.execute("SELECT cast(%s as varchar) as f1", (v2,))) assert retval[0][0] == str(v2) def test_unicode_roundtrip(cursor): v = "hello \u0173 world" retval = tuple(cursor.execute("SELECT cast(%s as varchar) as f1", (v,))) assert retval[0][0] == v def test_long_roundtrip(con): v = 50000000000000 retval = con.run("SELECT cast(:v as bigint)", v=v) assert retval[0][0] == v def test_int_execute_many_select(cursor): tuple(cursor.executemany("SELECT CAST(%s AS INTEGER)", ((1,), (40000,)))) def test_int_execute_many_insert(cursor): v = ([None], [4]) cursor.execute("create temporary table test_int (f integer)") cursor.executemany("INSERT INTO test_int VALUES (%s)", v) retval = tuple(cursor.execute("SELECT * from test_int")) assert retval == v def test_insert_null(con): v = None con.run("CREATE TEMPORARY TABLE test_int (f INTEGER)") con.run("INSERT INTO test_int VALUES (:v)", v=v) retval = con.run("SELECT * FROM test_int") assert retval[0][0] == v def test_int_roundtrip(con): int2 = 21 int4 = 23 int8 = 20 MAP = { int2: "int2", int4: "int4", int8: "int8", } test_values = [ (0, int2), (-32767, int2), (-32768, int4), (+32767, int2), (+32768, int4), (-2147483647, int4), (-2147483648, int8), (+2147483647, int4), (+2147483648, int8), (-9223372036854775807, int8), (+9223372036854775807, int8), ] for value, typoid in test_values: retval = con.run("SELECT cast(:v as " + MAP[typoid] + ")", v=value) assert retval[0][0] == value column_name, column_typeoid = con.description[0][0:2] assert column_typeoid == typoid def test_bytea_roundtrip(con): retval = con.run( "SELECT cast(:v as bytea)", v=Binary(b"\x00\x01\x02\x03\x02\x01\x00") ) assert retval[0][0] == b"\x00\x01\x02\x03\x02\x01\x00" def test_bytearray_round_trip(con): binary = b"\x00\x01\x02\x03\x02\x01\x00" retval = con.run("SELECT cast(:v as bytea)", v=bytearray(binary)) assert retval[0][0] == binary def test_bytearray_subclass_round_trip(con): class BClass(bytearray): pass binary = b"\x00\x01\x02\x03\x02\x01\x00" retval = con.run("SELECT cast(:v as bytea)", v=BClass(binary)) assert retval[0][0] == binary def test_timestamp_roundtrip(is_java, con): v = Datetime(2001, 2, 3, 4, 5, 6, 170000) retval = con.run("SELECT cast(:v as timestamp)", v=v) assert retval[0][0] == v # Test that time zone doesn't affect it # Jython 2.5.3 doesn't have a time.tzset() so skip if not is_java: orig_tz = os.environ.get("TZ") os.environ["TZ"] = "America/Edmonton" time.tzset() retval = con.run("SELECT cast(:v as timestamp)", v=v) assert retval[0][0] == v if orig_tz is None: del os.environ["TZ"] else: os.environ["TZ"] = orig_tz time.tzset() def test_interval_repr(): v = PGInterval(microseconds=123456789, days=2, months=24) assert repr(v) == "" def test_interval_in_1_year(): assert pginterval_in("1 year") == PGInterval(years=1) def test_timedelta_in_2_months(): assert timedelta_in("2 hours") def test_interval_roundtrip(con): con.register_in_adapter(INTERVAL, pginterval_in) con.register_out_adapter(PGInterval, pginterval_out) v = PGInterval(microseconds=123456789, days=2, months=24) retval = con.run("SELECT cast(:v as interval)", v=v) assert retval[0][0] == v def test_timedelta_roundtrip(con): v = Timedelta(seconds=30) retval = con.run("SELECT cast(:v as interval)", v=v) assert retval[0][0] == v def test_enum_str_round_trip(cursor): try: cursor.execute("create type lepton as enum ('electron', 'muon', 'tau')") v = "muon" cursor.execute("SELECT cast(%s as lepton) as f1", (v,)) retval = cursor.fetchall() assert retval[0][0] == v cursor.execute("CREATE TEMPORARY TABLE testenum (f1 lepton)") cursor.execute( "INSERT INTO testenum VALUES (cast(%s as lepton))", ("electron",) ) finally: cursor.execute("drop table testenum") cursor.execute("drop type lepton") def test_enum_custom_round_trip(con): class Lepton: # Implements PEP 435 in the minimal fashion needed __members__ = OrderedDict() def __init__(self, name, value, alias=None): self.name = name self.value = value self.__members__[name] = self setattr(self.__class__, name, self) if alias: self.__members__[alias] = self setattr(self.__class__, alias, self) def lepton_out(lepton): return lepton.value try: con.run("create type lepton as enum ('1', '2', '3')") con.register_out_adapter(Lepton, lepton_out) v = Lepton("muon", "2") retval = con.run("SELECT CAST(:v AS lepton)", v=v) assert retval[0][0] == v.value finally: con.run("drop type lepton") def test_enum_py_round_trip(cursor): class Lepton(Enum): electron = "1" muon = "2" tau = "3" try: cursor.execute("create type lepton as enum ('1', '2', '3')") v = Lepton.muon retval = tuple(cursor.execute("SELECT cast(%s as lepton) as f1", (v,))) assert retval[0][0] == v.value cursor.execute("CREATE TEMPORARY TABLE testenum (f1 lepton)") cursor.execute( "INSERT INTO testenum VALUES (cast(%s as lepton))", (Lepton.electron,) ) finally: cursor.execute("drop table testenum") cursor.execute("drop type lepton") def test_xml_roundtrip(cursor): v = "gatccgagtac" retval = tuple(cursor.execute("select xmlparse(content %s) as f1", (v,))) assert retval[0][0] == v def test_uuid_roundtrip(con): v = uuid.UUID("911460f2-1f43-fea2-3e2c-e01fd5b5069d") retval = con.run("select cast(:v as uuid)", v=v) assert retval[0][0] == v def test_inet_roundtrip_network(con): v = ipaddress.ip_network("192.168.0.0/28") retval = con.run("select cast(:v as inet)", v=v) assert retval[0][0] == v def test_inet_roundtrip_address(con): v = ipaddress.ip_address("192.168.0.1") retval = con.run("select cast(:v as inet)", v=v) assert retval[0][0] == v def test_xid_roundtrip(cursor): v = 86722 cursor.execute("select cast(cast(%s as varchar) as xid) as f1", (v,)) retval = cursor.fetchall() assert retval[0][0] == v # Should complete without an exception cursor.execute("select * from pg_locks where transactionid = %s", (97712,)) retval = cursor.fetchall() def test_int2vector_in(cursor): retval = tuple(cursor.execute("select cast('1 2' as int2vector) as f1")) assert retval[0][0] == [1, 2] # Should complete without an exception tuple(cursor.execute("select indkey from pg_index")) def test_timestamp_tz_out(cursor): cursor.execute( "SELECT '2001-02-03 04:05:06.17 America/Edmonton'" "::timestamp with time zone" ) retval = cursor.fetchall() dt = retval[0][0] assert dt.tzinfo is not None, "no tzinfo returned" assert dt.astimezone(Timezone.utc) == Datetime( 2001, 2, 3, 11, 5, 6, 170000, Timezone.utc ), "retrieved value match failed" def test_timestamp_tz_roundtrip(is_java, con): if not is_java: mst = pytz.timezone("America/Edmonton") v1 = mst.localize(Datetime(2001, 2, 3, 4, 5, 6, 170000)) retval = con.run("SELECT cast(:v as timestamptz)", v=v1) v2 = retval[0][0] assert v2.tzinfo is not None assert v1 == v2 def test_timestamp_mismatch(is_java, cursor): if not is_java: mst = pytz.timezone("America/Edmonton") cursor.execute("SET SESSION TIME ZONE 'America/Edmonton'") try: cursor.execute( "CREATE TEMPORARY TABLE TestTz " "(f1 timestamp with time zone, " "f2 timestamp without time zone)" ) cursor.execute( "INSERT INTO TestTz (f1, f2) VALUES (%s, %s)", ( # insert timestamp into timestamptz field (v1) Datetime(2001, 2, 3, 4, 5, 6, 170000), # insert timestamptz into timestamp field (v2) mst.localize(Datetime(2001, 2, 3, 4, 5, 6, 170000)), ), ) cursor.execute("SELECT f1, f2 FROM TestTz") retval = cursor.fetchall() # when inserting a timestamp into a timestamptz field, # postgresql assumes that it is in local time. So the value # that comes out will be the server's local time interpretation # of v1. We've set the server's TZ to MST, the time should # be... f1 = retval[0][0] assert f1 == Datetime(2001, 2, 3, 11, 5, 6, 170000, Timezone.utc) # inserting the timestamptz into a timestamp field, pg8000 # converts the value into UTC, and then the PG server converts # it into local time for insertion into the field. When we # query for it, we get the same time back, like the tz was # dropped. f2 = retval[0][1] assert f2 == Datetime(2001, 2, 3, 11, 5, 6, 170000) finally: cursor.execute("SET SESSION TIME ZONE DEFAULT") def test_name_out(cursor): # select a field that is of "name" type: tuple(cursor.execute("SELECT usename FROM pg_user")) # It is sufficient that no errors were encountered. def test_oid_out(cursor): tuple(cursor.execute("SELECT oid FROM pg_type")) # It is sufficient that no errors were encountered. def test_boolean_in(cursor): retval = tuple(cursor.execute("SELECT cast('t' as bool)")) assert retval[0][0] def test_numeric_out(cursor): for num in ("5000", "50.34"): retval = tuple(cursor.execute("SELECT " + num + "::numeric")) assert str(retval[0][0]) == num def test_int2_out(cursor): retval = tuple(cursor.execute("SELECT 5000::smallint")) assert retval[0][0] == 5000 def test_int4_out(cursor): retval = tuple(cursor.execute("SELECT 5000::integer")) assert retval[0][0] == 5000 def test_int8_out(cursor): retval = tuple(cursor.execute("SELECT 50000000000000::bigint")) assert retval[0][0] == 50000000000000 def test_float4_out(cursor): retval = tuple(cursor.execute("SELECT 1.1::real")) assert retval[0][0] == 1.1 def test_float8_out(cursor): retval = tuple(cursor.execute("SELECT 1.1::double precision")) assert retval[0][0] == 1.1000000000000001 def test_varchar_out(cursor): retval = tuple(cursor.execute("SELECT 'hello'::varchar(20)")) assert retval[0][0] == "hello" def test_char_out(cursor): retval = tuple(cursor.execute("SELECT 'hello'::char(20)")) assert retval[0][0] == "hello " def test_text_out(cursor): retval = tuple(cursor.execute("SELECT 'hello'::text")) assert retval[0][0] == "hello" def test_interval_in(con): con.register_in_adapter(INTERVAL, pginterval_in) retval = con.run( "SELECT '1 month 16 days 12 hours 32 minutes 64 seconds'::interval" ) expected_value = PGInterval( microseconds=(12 * 60 * 60 * 1000 * 1000) + (32 * 60 * 1000 * 1000) + (64 * 1000 * 1000), days=16, months=1, ) assert retval[0][0] == expected_value def test_interval_in_30_seconds(con): retval = con.run("select interval '30 seconds'") assert retval[0][0] == Timedelta(seconds=30) def test_interval_in_12_days_30_seconds(con): retval = con.run("select interval '12 days 30 seconds'") assert retval[0][0] == Timedelta(days=12, seconds=30) def test_timestamp_out(cursor): cursor.execute("SELECT '2001-02-03 04:05:06.17'::timestamp") retval = cursor.fetchall() assert retval[0][0] == Datetime(2001, 2, 3, 4, 5, 6, 170000) def test_int4_array_out(cursor): cursor.execute( "SELECT '{1,2,3,4}'::INT[] AS f1, '{{1,2,3},{4,5,6}}'::INT[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::INT[][][] AS f3" ) f1, f2, f3 = cursor.fetchone() assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_int2_array_out(cursor): cursor.execute( "SELECT '{1,2,3,4}'::INT2[] AS f1, " "'{{1,2,3},{4,5,6}}'::INT2[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::INT2[][][] AS f3" ) f1, f2, f3 = cursor.fetchone() assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_int8_array_out(cursor): cursor.execute( "SELECT '{1,2,3,4}'::INT8[] AS f1, " "'{{1,2,3},{4,5,6}}'::INT8[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::INT8[][][] AS f3" ) f1, f2, f3 = cursor.fetchone() assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_bool_array_out(cursor): cursor.execute( "SELECT '{TRUE,FALSE,FALSE,TRUE}'::BOOL[] AS f1, " "'{{TRUE,FALSE,TRUE},{FALSE,TRUE,FALSE}}'::BOOL[][] AS f2, " "'{{{TRUE,FALSE},{FALSE,TRUE}},{{NULL,TRUE},{FALSE,FALSE}}}'" "::BOOL[][][] AS f3" ) f1, f2, f3 = cursor.fetchone() assert f1 == [True, False, False, True] assert f2 == [[True, False, True], [False, True, False]] assert f3 == [[[True, False], [False, True]], [[None, True], [False, False]]] def test_float4_array_out(cursor): cursor.execute( "SELECT '{1,2,3,4}'::FLOAT4[] AS f1, " "'{{1,2,3},{4,5,6}}'::FLOAT4[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::FLOAT4[][][] AS f3" ) f1, f2, f3 = cursor.fetchone() assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_float8_array_out(cursor): cursor.execute( "SELECT '{1,2,3,4}'::FLOAT8[] AS f1, " "'{{1,2,3},{4,5,6}}'::FLOAT8[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::FLOAT8[][][] AS f3" ) f1, f2, f3 = cursor.fetchone() assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_int_array_roundtrip_small(con): """send small int array, should be sent as INT2[]""" retval = con.run("SELECT cast(:v as int2[])", v=[1, 2, 3]) assert retval[0][0], [1, 2, 3] column_name, column_typeoid = con.description[0][0:2] assert column_typeoid == 1005, "type should be INT2[]" def test_int_array_roundtrip_multi(con): """test multi-dimensional array, should be sent as INT2[]""" retval = con.run("SELECT cast(:v as int2[])", v=[[1, 2], [3, 4]]) assert retval[0][0] == [[1, 2], [3, 4]] column_name, column_typeoid = con.description[0][0:2] assert column_typeoid == 1005, "type should be INT2[]" def test_int4_array_roundtrip(con): """a larger value should kick it up to INT4[]...""" retval = con.run("SELECT cast(:v as int4[])", v=[70000, 2, 3]) assert retval[0][0] == [70000, 2, 3] column_name, column_typeoid = con.description[0][0:2] assert column_typeoid == 1007, "type should be INT4[]" def test_int8_array_roundtrip(con): """a much larger value should kick it up to INT8[]...""" retval = con.run("SELECT cast(:v as int8[])", v=[7000000000, 2, 3]) assert retval[0][0] == [7000000000, 2, 3], "retrieved value match failed" column_name, column_typeoid = con.description[0][0:2] assert column_typeoid == 1016, "type should be INT8[]" def test_int_array_with_null_roundtrip(con): retval = con.run("SELECT cast(:v as int[])", v=[1, None, 3]) assert retval[0][0] == [1, None, 3] def test_float_array_roundtrip(con): retval = con.run("SELECT cast(:v as double precision[])", v=[1.1, 2.2, 3.3]) assert retval[0][0] == [1.1, 2.2, 3.3] def test_bool_array_roundtrip(con): retval = con.run("SELECT cast(:v as bool[])", v=[True, False, None]) assert retval[0][0] == [True, False, None] @pytest.mark.parametrize( "test_input,expected", [ ("SELECT '{a,b,c}'::TEXT[] AS f1", ["a", "b", "c"]), ("SELECT '{a,b,c}'::CHAR[] AS f1", ["a", "b", "c"]), ("SELECT '{a,b,c}'::VARCHAR[] AS f1", ["a", "b", "c"]), ("SELECT '{a,b,c}'::CSTRING[] AS f1", ["a", "b", "c"]), ("SELECT '{a,b,c}'::NAME[] AS f1", ["a", "b", "c"]), ("SELECT '{}'::text[];", []), ('SELECT \'{NULL,"NULL",NULL,""}\'::text[];', [None, "NULL", None, ""]), ], ) def test_string_array_out(con, test_input, expected): result = con.run(test_input) assert result[0][0] == expected def test_numeric_array_out(cursor): cursor.execute("SELECT '{1.1,2.2,3.3}'::numeric[] AS f1") assert cursor.fetchone()[0] == [ decimal.Decimal("1.1"), decimal.Decimal("2.2"), decimal.Decimal("3.3"), ] def test_numeric_array_roundtrip(con): v = [decimal.Decimal("1.1"), None, decimal.Decimal("3.3")] retval = con.run("SELECT cast(:v as numeric[])", v=v) assert retval[0][0] == v def test_string_array_roundtrip(con): v = [ "Hello!", "World!", "abcdefghijklmnopqrstuvwxyz", "", "A bunch of random characters:", " ~!@#$%^&*()_+`1234567890-=[]\\{}|{;':\",./<>?\t", None, ] retval = con.run("SELECT cast(:v as varchar[])", v=v) assert retval[0][0] == v def test_array_string_escape(): v = '"' res = pg8000.converters.array_string_escape(v) assert res == '"\\""' def test_empty_array(con): v = [] retval = con.run("SELECT cast(:v as varchar[])", v=v) assert retval[0][0] == v def test_macaddr(cursor): retval = tuple(cursor.execute("SELECT macaddr '08002b:010203'")) assert retval[0][0] == "08:00:2b:01:02:03" def test_tsvector_roundtrip(cursor): cursor.execute( "SELECT cast(%s as tsvector)", ("a fat cat sat on a mat and ate a fat rat",) ) retval = cursor.fetchall() assert retval[0][0] == "'a' 'and' 'ate' 'cat' 'fat' 'mat' 'on' 'rat' 'sat'" def test_hstore_roundtrip(cursor): val = '"a"=>"1"' retval = tuple(cursor.execute("SELECT cast(%s as hstore)", (val,))) assert retval[0][0] == val def test_json_roundtrip(con): val = {"name": "Apollo 11 Cave", "zebra": True, "age": 26.003} retval = con.run("SELECT cast(:v as jsonb)", v=dumps(val)) assert retval[0][0] == val def test_jsonb_roundtrip(cursor): val = {"name": "Apollo 11 Cave", "zebra": True, "age": 26.003} cursor.execute("SELECT cast(%s as jsonb)", (dumps(val),)) retval = cursor.fetchall() assert retval[0][0] == val def test_json_access_object(cursor): val = {"name": "Apollo 11 Cave", "zebra": True, "age": 26.003} cursor.execute("SELECT cast(%s as json) -> %s", (dumps(val), "name")) retval = cursor.fetchall() assert retval[0][0] == "Apollo 11 Cave" def test_jsonb_access_object(cursor): val = {"name": "Apollo 11 Cave", "zebra": True, "age": 26.003} cursor.execute("SELECT cast(%s as jsonb) -> %s", (dumps(val), "name")) retval = cursor.fetchall() assert retval[0][0] == "Apollo 11 Cave" def test_json_access_array(con): val = [-1, -2, -3, -4, -5] retval = con.run( "SELECT cast(:v1 as json) -> cast(:v2 as int)", v1=dumps(val), v2=2 ) assert retval[0][0] == -3 def test_jsonb_access_array(con): val = [-1, -2, -3, -4, -5] retval = con.run( "SELECT cast(:v1 as jsonb) -> cast(:v2 as int)", v1=dumps(val), v2=2 ) assert retval[0][0] == -3 def test_jsonb_access_path(con): j = {"a": [1, 2, 3], "b": [4, 5, 6]} path = ["a", "2"] retval = con.run("SELECT cast(:v1 as jsonb) #>> :v2", v1=dumps(j), v2=path) assert retval[0][0] == str(j[path[0]][int(path[1])]) def test_infinity_timestamp_roundtrip(cursor): v = "infinity" retval = tuple(cursor.execute("SELECT cast(%s as timestamp) as f1", (v,))) assert retval[0][0] == v def test_point_roundtrip(cursor): v = "(2.3,1)" retval = tuple(cursor.execute("SELECT cast(%s as point) as f1", (v,))) assert retval[0][0] == v def test_time_in(): actual = pg8000.converters.time_in("12:57:18.000396") assert actual == Time(12, 57, 18, 396) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/legacy/test_typeobjects.py0000664000175000017500000000027000000000000021470 0ustar00tlocketlocke00000000000000from pg8000 import PGInterval def test_pginterval_constructor_days(): i = PGInterval(days=1) assert i.months is None assert i.days == 1 assert i.microseconds is None ././@PaxHeader0000000000000000000000000000003300000000000011451 xustar000000000000000027 mtime=1636798860.416994 pg8000-1.23.0/test/native/0000775000175000017500000000000000000000000015547 5ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/native/__init__.py0000664000175000017500000000000000000000000017646 0ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/native/conftest.py0000664000175000017500000000210000000000000017737 0ustar00tlocketlocke00000000000000import sys from os import environ import pytest import pg8000.native @pytest.fixture(scope="class") def db_kwargs(): db_connect = {"user": "postgres", "password": "pw"} for kw, var, f in [ ("host", "PGHOST", str), ("password", "PGPASSWORD", str), ("port", "PGPORT", int), ]: try: db_connect[kw] = f(environ[var]) except KeyError: pass return db_connect @pytest.fixture def con(request, db_kwargs): conn = pg8000.native.Connection(**db_kwargs) def fin(): try: conn.run("rollback") except pg8000.native.InterfaceError: pass try: conn.close() except pg8000.native.InterfaceError: pass request.addfinalizer(fin) return conn @pytest.fixture def pg_version(con): retval = con.run("select current_setting('server_version')") version = retval[0][0] idx = version.index(".") return int(version[:idx]) @pytest.fixture(scope="module") def is_java(): return "java" in sys.platform.lower() ././@PaxHeader0000000000000000000000000000003300000000000011451 xustar000000000000000027 mtime=1636798860.416994 pg8000-1.23.0/test/native/github-actions/0000775000175000017500000000000000000000000020467 5ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/native/github-actions/__init__.py0000664000175000017500000000000000000000000022566 0ustar00tlocketlocke00000000000000././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/native/github-actions/gss_native.py0000664000175000017500000000054300000000000023205 0ustar00tlocketlocke00000000000000import pytest import pg8000.dbapi def test_gss(db_kwargs): """Called by GitHub Actions with auth method gss.""" # Should raise an exception saying gss isn't supported with pytest.raises( pg8000.dbapi.InterfaceError, match="Authentication method 7 not supported by pg8000.", ): pg8000.dbapi.connect(**db_kwargs) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/native/github-actions/md5_native.py0000664000175000017500000000022200000000000023070 0ustar00tlocketlocke00000000000000def test_md5(con): """Called by GitHub Actions with auth method md5. We just need to check that we can get a connection. """ pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/native/github-actions/password_native.py0000664000175000017500000000023400000000000024250 0ustar00tlocketlocke00000000000000def test_password(con): """Called by GitHub Actions with auth method password. We just need to check that we can get a connection. """ pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/native/github-actions/scram-sha-256_native.py0000664000175000017500000000024600000000000024601 0ustar00tlocketlocke00000000000000def test_scram_sha_256(con): """Called by GitHub Actions with auth method scram-sha-256. We just need to check that we can get a connection. """ pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/native/github-actions/ssl_native.py0000664000175000017500000000335400000000000023215 0ustar00tlocketlocke00000000000000import ssl import sys import pytest import pg8000.dbapi # Check if running in Jython if "java" in sys.platform: from javax.net.ssl import TrustManager, X509TrustManager from jarray import array from javax.net.ssl import SSLContext class TrustAllX509TrustManager(X509TrustManager): """Define a custom TrustManager which will blindly accept all certificates""" def checkClientTrusted(self, chain, auth): pass def checkServerTrusted(self, chain, auth): pass def getAcceptedIssuers(self): return None # Create a static reference to an SSLContext which will use # our custom TrustManager trust_managers = array([TrustAllX509TrustManager()], TrustManager) TRUST_ALL_CONTEXT = SSLContext.getInstance("SSL") TRUST_ALL_CONTEXT.init(None, trust_managers, None) # Keep a static reference to the JVM's default SSLContext for restoring # at a later time DEFAULT_CONTEXT = SSLContext.getDefault() @pytest.fixture def trust_all_certificates(request): """Decorator function that will make it so the context of the decorated method will run with our TrustManager that accepts all certificates""" # Only do this if running under Jython is_java = "java" in sys.platform if is_java: from javax.net.ssl import SSLContext SSLContext.setDefault(TRUST_ALL_CONTEXT) def fin(): if is_java: SSLContext.setDefault(DEFAULT_CONTEXT) request.addfinalizer(fin) @pytest.mark.usefixtures("trust_all_certificates") def test_ssl(db_kwargs): context = ssl.SSLContext() context.check_hostname = False db_kwargs["ssl_context"] = context with pg8000.dbapi.connect(**db_kwargs): pass ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/native/test_auth.py0000664000175000017500000000575100000000000020131 0ustar00tlocketlocke00000000000000import ssl import sys import pytest import pg8000.native def test_gss(db_kwargs): """This requires a line in pg_hba.conf that requires gss for the database pg8000_gss """ db_kwargs["database"] = "pg8000_gss" # Should raise an exception saying gss isn't supported with pytest.raises( pg8000.native.InterfaceError, match="Authentication method 7 not supported by pg8000.", ): pg8000.native.Connection(**db_kwargs) # Check if running in Jython if "java" in sys.platform: from javax.net.ssl import TrustManager, X509TrustManager from jarray import array from javax.net.ssl import SSLContext class TrustAllX509TrustManager(X509TrustManager): """Define a custom TrustManager which will blindly accept all certificates""" def checkClientTrusted(self, chain, auth): pass def checkServerTrusted(self, chain, auth): pass def getAcceptedIssuers(self): return None # Create a static reference to an SSLContext which will use # our custom TrustManager trust_managers = array([TrustAllX509TrustManager()], TrustManager) TRUST_ALL_CONTEXT = SSLContext.getInstance("SSL") TRUST_ALL_CONTEXT.init(None, trust_managers, None) # Keep a static reference to the JVM's default SSLContext for restoring # at a later time DEFAULT_CONTEXT = SSLContext.getDefault() @pytest.fixture def trust_all_certificates(request): """Decorator function that will make it so the context of the decorated method will run with our TrustManager that accepts all certificates""" # Only do this if running under Jython is_java = "java" in sys.platform if is_java: from javax.net.ssl import SSLContext SSLContext.setDefault(TRUST_ALL_CONTEXT) def fin(): if is_java: SSLContext.setDefault(DEFAULT_CONTEXT) request.addfinalizer(fin) @pytest.mark.usefixtures("trust_all_certificates") def test_ssl(db_kwargs): context = ssl.SSLContext() context.check_hostname = False db_kwargs["ssl_context"] = context with pg8000.native.Connection(**db_kwargs): pass # This requires a line in pg_hba.conf that requires scram-sha-256 for the # database scram_sha_256 def test_scram_sha_256(db_kwargs): db_kwargs["database"] = "pg8000_scram_sha_256" # Should only raise an exception saying db doesn't exist with pytest.raises(pg8000.native.DatabaseError, match="3D000"): pg8000.native.Connection(**db_kwargs) # This requires a line in pg_hba.conf that requires scram-sha-256 for the # database scram_sha_256 def test_scram_sha_256_plus(db_kwargs): context = ssl.SSLContext() context.check_hostname = False db_kwargs["ssl_context"] = context db_kwargs["database"] = "pg8000_scram_sha_256" print(db_kwargs) # Should only raise an exception saying db doesn't exist with pytest.raises(pg8000.native.DatabaseError, match="3D000"): pg8000.native.Connection(**db_kwargs) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/native/test_benchmarks.py0000664000175000017500000000133200000000000021274 0ustar00tlocketlocke00000000000000import pytest @pytest.mark.parametrize( "txt", ( ("int2", "cast(id / 100 as int2)"), "cast(id as int4)", "cast(id * 100 as int8)", "(id % 2) = 0", "N'Static text string'", "cast(id / 100 as float4)", "cast(id / 100 as float8)", "cast(id / 100 as numeric)", "timestamp '2001-09-28'", ), ) def test_round_trips(con, benchmark, txt): def torun(): query = """SELECT {0} AS column1, {0} AS column2, {0} AS column3, {0} AS column4, {0} AS column5, {0} AS column6, {0} AS column7 FROM (SELECT generate_series(1, 10000) AS id) AS tbl""".format( txt ) con.run(query) benchmark(torun) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1634149967.0 pg8000-1.23.0/test/native/test_connection.py0000664000175000017500000001331000000000000021315 0ustar00tlocketlocke00000000000000from datetime import time as Time import pytest from pg8000.native import Connection, DatabaseError, InterfaceError def test_unix_socket_missing(): conn_params = {"unix_sock": "/file-does-not-exist", "user": "doesn't-matter"} with pytest.raises(InterfaceError): Connection(**conn_params) def test_internet_socket_connection_refused(): conn_params = {"port": 0, "user": "doesn't-matter"} with pytest.raises( InterfaceError, match="Can't create a connection to host localhost and port 0 " "\\(timeout is None and source_address is None\\).", ): Connection(**conn_params) def test_database_missing(db_kwargs): db_kwargs["database"] = "missing-db" with pytest.raises(DatabaseError): Connection(**db_kwargs) def test_notify(con): backend_pid = con.run("select pg_backend_pid()")[0][0] assert list(con.notifications) == [] con.run("LISTEN test") con.run("NOTIFY test") con.run("VALUES (1, 2), (3, 4), (5, 6)") assert len(con.notifications) == 1 assert con.notifications[0] == (backend_pid, "test", "") def test_notify_with_payload(con): backend_pid = con.run("select pg_backend_pid()")[0][0] assert list(con.notifications) == [] con.run("LISTEN test") con.run("NOTIFY test, 'Parnham'") con.run("VALUES (1, 2), (3, 4), (5, 6)") assert len(con.notifications) == 1 assert con.notifications[0] == (backend_pid, "test", "Parnham") # This requires a line in pg_hba.conf that requires md5 for the database # pg8000_md5 def test_md5(db_kwargs): db_kwargs["database"] = "pg8000_md5" # Should only raise an exception saying db doesn't exist with pytest.raises(DatabaseError, match="3D000"): Connection(**db_kwargs) # This requires a line in pg_hba.conf that requires 'password' for the # database pg8000_password def test_password(db_kwargs): db_kwargs["database"] = "pg8000_password" # Should only raise an exception saying db doesn't exist with pytest.raises(DatabaseError, match="3D000"): Connection(**db_kwargs) def test_unicode_databaseName(db_kwargs): db_kwargs["database"] = "pg8000_sn\uFF6Fw" # Should only raise an exception saying db doesn't exist with pytest.raises(DatabaseError, match="3D000"): Connection(**db_kwargs) def test_bytes_databaseName(db_kwargs): """Should only raise an exception saying db doesn't exist""" db_kwargs["database"] = bytes("pg8000_sn\uFF6Fw", "utf8") with pytest.raises(DatabaseError, match="3D000"): Connection(**db_kwargs) def test_bytes_password(con, db_kwargs): # Create user username = "boltzmann" password = "cha\uFF6Fs" con.run("create user " + username + " with password '" + password + "';") db_kwargs["user"] = username db_kwargs["password"] = password.encode("utf8") db_kwargs["database"] = "pg8000_md5" with pytest.raises(DatabaseError, match="3D000"): Connection(**db_kwargs) con.run("drop role " + username) def test_broken_pipe_read(con, db_kwargs): db1 = Connection(**db_kwargs) res = db1.run("select pg_backend_pid()") pid1 = res[0][0] con.run("select pg_terminate_backend(:v)", v=pid1) with pytest.raises(InterfaceError, match="network error on read"): db1.run("select 1") def test_broken_pipe_unpack(con): res = con.run("select pg_backend_pid()") pid1 = res[0][0] with pytest.raises(InterfaceError, match="network error"): con.run("select pg_terminate_backend(:v)", v=pid1) def test_broken_pipe_flush(con, db_kwargs): db1 = Connection(**db_kwargs) res = db1.run("select pg_backend_pid()") pid1 = res[0][0] con.run("select pg_terminate_backend(:v)", v=pid1) try: db1.run("select 1") except BaseException: pass # Sometimes raises and sometime doesn't try: db1.close() except InterfaceError as e: assert str(e) == "network error on flush" def test_application_name(db_kwargs): app_name = "my test application name" db_kwargs["application_name"] = app_name with Connection(**db_kwargs) as db: res = db.run( "select application_name from pg_stat_activity " " where pid = pg_backend_pid()" ) application_name = res[0][0] assert application_name == app_name def test_application_name_integer(db_kwargs): db_kwargs["application_name"] = 1 with pytest.raises( InterfaceError, match="The parameter application_name can't be of type .", ): Connection(**db_kwargs) def test_application_name_bytearray(db_kwargs): db_kwargs["application_name"] = bytearray(b"Philby") Connection(**db_kwargs) class PG8000TestException(Exception): pass def raise_exception(val): raise PG8000TestException("oh noes!") def test_py_value_fail(con, mocker): # Ensure that if types.py_value throws an exception, the original # exception is raised (PG8000TestException), and the connection is # still usable after the error. mocker.patch.object(con, "py_types") con.py_types = {Time: raise_exception} with pytest.raises(PG8000TestException): con.run("SELECT CAST(:v AS TIME)", v=Time(10, 30)) # ensure that the connection is still usable for a new query res = con.run("VALUES ('hw3'::text)") assert res[0][0] == "hw3" def test_no_data_error_recovery(con): for i in range(1, 4): with pytest.raises(DatabaseError) as e: con.run("DROP TABLE t1") assert e.value.args[0]["C"] == "42P01" con.run("ROLLBACK") def test_closed_connection(con): con.close() with pytest.raises(InterfaceError, match="connection is closed"): con.run("VALUES ('hw1'::text)") ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636570109.0 pg8000-1.23.0/test/native/test_converters.py0000664000175000017500000000605400000000000021357 0ustar00tlocketlocke00000000000000from datetime import date as Date from decimal import Decimal from ipaddress import IPv4Address, IPv4Network import pytest from pg8000.converters import ( PGInterval, PY_TYPES, array_out, array_string_escape, interval_in, make_param, null_out, numeric_in, numeric_out, pg_interval_in, string_in, string_out, ) def test_null_out(): assert null_out(None) is None @pytest.mark.parametrize( "array,out", [ [[True, False, None], "{true,false,NULL}"], # bool[] [[IPv4Address("192.168.0.1")], "{192.168.0.1}"], # inet[] [[Date(2021, 3, 1)], "{2021-03-01}"], # date[] [[b"\x00\x01\x02\x03\x02\x01\x00"], '{"\\\\x00010203020100"}'], # bytea[] [[IPv4Network("192.168.0.0/28")], "{192.168.0.0/28}"], # inet[] [[1, 2, 3], "{1,2,3}"], # int2[] [[1, None, 3], "{1,NULL,3}"], # int2[] with None [[[1, 2], [3, 4]], "{{1,2},{3,4}}"], # int2[] multidimensional [[70000, 2, 3], "{70000,2,3}"], # int4[] [[7000000000, 2, 3], "{7000000000,2,3}"], # int8[] [[0, 7000000000, 2], "{0,7000000000,2}"], # int8[] [[1.1, 2.2, 3.3], "{1.1,2.2,3.3}"], # float8[] [["Veni", "vidi", "vici"], "{Veni,vidi,vici}"], # varchar[] ], ) def test_array_out(con, array, out): assert array_out(array) == out @pytest.mark.parametrize( "value", [ "1.1", "-1.1", "10000", "20000", "-1000000000.123456789", "1.0", "12.44", ], ) def test_numeric_out(value): assert numeric_out(value) == str(value) @pytest.mark.parametrize( "value", [ "1.1", "-1.1", "10000", "20000", "-1000000000.123456789", "1.0", "12.44", ], ) def test_numeric_in(value): assert numeric_in(value) == Decimal(value) @pytest.mark.parametrize( "value", [ "hello \u0173 world", ], ) def test_string_out(value): assert string_out(value) == value @pytest.mark.parametrize( "value", [ "hello \u0173 world", ], ) def test_string_in(value): assert string_in(value) == value def test_PGInterval_init(): i = PGInterval(days=1) assert i.months is None assert i.days == 1 assert i.microseconds is None def test_PGInterval_repr(): v = PGInterval(microseconds=123456789, days=2, months=24) assert repr(v) == "" def test_PGInterval_str(): v = PGInterval(microseconds=123456789, days=2, months=24, millennia=2) assert str(v) == "2 millennia 24 months 2 days 123456789 microseconds" def test_pg_interval_in_1_year(): assert pg_interval_in("1 year") == PGInterval(years=1) def test_interval_in_2_months(): assert interval_in("2 hours") def test_array_string_escape(): v = '"' res = array_string_escape(v) assert res == '"\\""' def test_make_param(): class BClass(bytearray): pass val = BClass(b"\x00\x01\x02\x03\x02\x01\x00") assert make_param(PY_TYPES, val) == "\\x00010203020100" ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1634149967.0 pg8000-1.23.0/test/native/test_copy.py0000664000175000017500000000575600000000000020147 0ustar00tlocketlocke00000000000000from io import BytesIO, StringIO import pytest @pytest.fixture def db_table(request, con): con.run("START TRANSACTION") con.run( "CREATE TEMPORARY TABLE t1 " "(f1 int primary key, f2 int not null, f3 varchar(50) null) " "on commit drop" ) return con def test_copy_to_with_table(db_table): db_table.run("INSERT INTO t1 (f1, f2, f3) VALUES (:v1, :v1, :v2)", v1=1, v2="1") db_table.run("INSERT INTO t1 (f1, f2, f3) VALUES (:v1, :v1, :v2)", v1=2, v2="2") db_table.run("INSERT INTO t1 (f1, f2, f3) VALUES (:v1, :v1, :v2)", v1=3, v2="3") stream = BytesIO() db_table.run("copy t1 to stdout", stream=stream) assert stream.getvalue() == b"1\t1\t1\n2\t2\t2\n3\t3\t3\n" assert db_table.row_count == 3 def test_copy_to_with_query(con): stream = BytesIO() con.run( "COPY (SELECT 1 as One, 2 as Two) TO STDOUT WITH DELIMITER " "'X' CSV HEADER QUOTE AS 'Y' FORCE QUOTE Two", stream=stream, ) assert stream.getvalue() == b"oneXtwo\n1XY2Y\n" assert con.row_count == 1 def test_copy_to_with_text_stream(con): stream = StringIO() con.run( "COPY (SELECT 1 as One, 2 as Two) TO STDOUT WITH DELIMITER " "'X' CSV HEADER QUOTE AS 'Y' FORCE QUOTE Two", stream=stream, ) assert stream.getvalue() == "oneXtwo\n1XY2Y\n" assert con.row_count == 1 def test_copy_from_with_table(db_table): stream = BytesIO(b"1\t1\t1\n2\t2\t2\n3\t3\t3\n") db_table.run("copy t1 from STDIN", stream=stream) assert db_table.row_count == 3 retval = db_table.run("SELECT * FROM t1 ORDER BY f1") assert retval == [[1, 1, "1"], [2, 2, "2"], [3, 3, "3"]] def test_copy_from_with_text_stream(db_table): stream = StringIO("1\t1\t1\n2\t2\t2\n3\t3\t3\n") db_table.run("copy t1 from STDIN", stream=stream) retval = db_table.run("SELECT * FROM t1 ORDER BY f1") assert retval == [[1, 1, "1"], [2, 2, "2"], [3, 3, "3"]] def test_copy_from_with_query(db_table): stream = BytesIO(b"f1Xf2\n1XY1Y\n") db_table.run( "COPY t1 (f1, f2) FROM STDIN WITH DELIMITER 'X' CSV HEADER " "QUOTE AS 'Y' FORCE NOT NULL f1", stream=stream, ) assert db_table.row_count == 1 retval = db_table.run("SELECT * FROM t1 ORDER BY f1") assert retval == [[1, 1, None]] def test_copy_from_with_error(db_table): stream = BytesIO(b"f1Xf2\n\n1XY1Y\n") with pytest.raises(BaseException) as e: db_table.run( "COPY t1 (f1, f2) FROM STDIN WITH DELIMITER 'X' CSV HEADER " "QUOTE AS 'Y' FORCE NOT NULL f1", stream=stream, ) arg = { "S": ("ERROR",), "C": ("22P02",), "M": ( 'invalid input syntax for type integer: ""', 'invalid input syntax for integer: ""', ), "W": ('COPY t1, line 2, column f1: ""',), "F": ("numutils.c",), "R": ("pg_atoi", "pg_strtoint32"), } earg = e.value.args[0] for k, v in arg.items(): assert earg[k] in v ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/test/native/test_core.py0000664000175000017500000000232400000000000020111 0ustar00tlocketlocke00000000000000from io import BytesIO from pg8000.core import ( CoreConnection, NULL_BYTE, PASSWORD, _create_message, ) def test_handle_AUTHENTICATION_3(mocker): """Shouldn't send a FLUSH message, as FLUSH only used in extended-query""" mocker.patch.object(CoreConnection, "__init__", lambda x: None) con = CoreConnection() password = "barbour".encode("utf8") con.password = password con._flush = mocker.Mock() buf = BytesIO() con._write = buf.write CoreConnection.handle_AUTHENTICATION_REQUEST(con, b"\x00\x00\x00\x03", None) assert buf.getvalue() == _create_message(PASSWORD, password + NULL_BYTE) # assert buf.getvalue() == b"p\x00\x00\x00\x0cbarbour\x00" def test_create_message(): msg = _create_message(PASSWORD, "barbour".encode("utf8") + NULL_BYTE) assert msg == b"p\x00\x00\x00\x0cbarbour\x00" def test_handle_ERROR_RESPONSE(mocker): """Check it handles invalid encodings in the error messages""" mocker.patch.object(CoreConnection, "__init__", lambda x: None) con = CoreConnection() con._client_encoding = "utf8" CoreConnection.handle_ERROR_RESPONSE(con, b"S\xc2err" + NULL_BYTE + NULL_BYTE, None) assert str(con.error) == "{'S': '�err'}" ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1635602067.0 pg8000-1.23.0/test/native/test_prepared_statement.py0000664000175000017500000000024400000000000023046 0ustar00tlocketlocke00000000000000def test_prepare(con): con.prepare("SELECT CAST(:v AS INTEGER)") def test_run(con): ps = con.prepare("SELECT cast(:v as varchar)") ps.run(v="speedy") ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1636748296.0 pg8000-1.23.0/test/native/test_query.py0000664000175000017500000001360700000000000020334 0ustar00tlocketlocke00000000000000import pytest from pg8000.native import DatabaseError, to_statement # Tests relating to the basic operation of the database driver, driven by the # pg8000 custom interface. @pytest.fixture def db_table(request, con): con.run( "CREATE TEMPORARY TABLE t1 (f1 int primary key, " "f2 bigint not null, f3 varchar(50) null) " ) def fin(): try: con.run("drop table t1") except DatabaseError: pass request.addfinalizer(fin) return con def test_database_error(con): with pytest.raises(DatabaseError): con.run("INSERT INTO t99 VALUES (1, 2, 3)") # Run a query on a table, alter the structure of the table, then run the # original query again. def test_alter(db_table): db_table.run("select * from t1") db_table.run("alter table t1 drop column f3") db_table.run("select * from t1") # Run a query on a table, drop then re-create the table, then run the # original query again. def test_create(db_table): db_table.run("select * from t1") db_table.run("drop table t1") db_table.run("create temporary table t1 (f1 int primary key)") db_table.run("select * from t1") def test_parametrized(db_table): res = db_table.run("SELECT f1, f2, f3 FROM t1 WHERE f1 > :f1", f1=3) for row in res: f1, f2, f3 = row def test_insert_returning(db_table): db_table.run("CREATE TEMPORARY TABLE t2 (id serial, data text)") # Test INSERT ... RETURNING with one row... res = db_table.run("INSERT INTO t2 (data) VALUES (:v) RETURNING id", v="test1") row_id = res[0][0] res = db_table.run("SELECT data FROM t2 WHERE id = :v", v=row_id) assert "test1" == res[0][0] assert db_table.row_count == 1 # Test with multiple rows... res = db_table.run( "INSERT INTO t2 (data) VALUES (:v1), (:v2), (:v3) " "RETURNING id", v1="test2", v2="test3", v3="test4", ) assert db_table.row_count == 3 ids = [x[0] for x in res] assert len(ids) == 3 def test_row_count_select(db_table): expected_count = 57 for i in range(expected_count): db_table.run( "INSERT INTO t1 (f1, f2, f3) VALUES (:v1, :v2, :v3)", v1=i, v2=i, v3=None ) db_table.run("SELECT * FROM t1") # Check row_count assert expected_count == db_table.row_count # Should be -1 for a command with no results db_table.run("DROP TABLE t1") assert -1 == db_table.row_count def test_row_count_delete(db_table): db_table.run( "INSERT INTO t1 (f1, f2, f3) VALUES (:v1, :v2, :v3)", v1=1, v2=1, v3=None ) db_table.run("DELETE FROM t1") assert db_table.row_count == 1 def test_row_count_update(db_table): db_table.run( "INSERT INTO t1 (f1, f2, f3) VALUES (:v1, :v2, :v3)", v1=1, v2=1, v3=None ) db_table.run( "INSERT INTO t1 (f1, f2, f3) VALUES (:v1, :v2, :v3)", v1=2, v2=10, v3=None ) db_table.run( "INSERT INTO t1 (f1, f2, f3) VALUES (:v1, :v2, :v3)", v1=3, v2=100, v3=None ) db_table.run( "INSERT INTO t1 (f1, f2, f3) VALUES (:v1, :v2, :v3)", v1=4, v2=1000, v3=None ) db_table.run( "INSERT INTO t1 (f1, f2, f3) VALUES (:v1, :v2, :v3)", v1=5, v2=10000, v3=None ) db_table.run("UPDATE t1 SET f3 = :v1 WHERE f2 > 101", v1="Hello!") assert db_table.row_count == 2 def test_int_oid(con): # https://bugs.launchpad.net/pg8000/+bug/230796 con.run("SELECT typname FROM pg_type WHERE oid = :v", v=100) def test_unicode_query(con): con.run( "CREATE TEMPORARY TABLE \u043c\u0435\u0441\u0442\u043e " "(\u0438\u043c\u044f VARCHAR(50), " "\u0430\u0434\u0440\u0435\u0441 VARCHAR(250))" ) def test_transactions(db_table): db_table.run("start transaction") db_table.run( "INSERT INTO t1 (f1, f2, f3) VALUES (:v1, :v2, :v3)", v1=1, v2=1, v3="Zombie" ) db_table.run("rollback") db_table.run("select * from t1") assert db_table.row_count == 0 def test_in(con): ret = con.run("SELECT typname FROM pg_type WHERE oid = any(:v)", v=[16, 23]) assert ret[0][0] == "bool" # An empty query should raise a ProgrammingError def test_empty_query(con): with pytest.raises(DatabaseError): con.run("") def test_rollback_no_transaction(con): # Remove any existing notices con.notices.clear() # First, verify that a raw rollback does produce a notice con.run("rollback") assert 1 == len(con.notices) # 25P01 is the code for no_active_sql_tronsaction. It has # a message and severity name, but those might be # localized/depend on the server version. assert con.notices.pop().get(b"C") == b"25P01" def test_close_prepared_statement(con): ps = con.prepare("select 1") ps.run() res = con.run("select count(*) from pg_prepared_statements") assert res[0][0] == 1 # Should have one prepared statement ps.close() res = con.run("select count(*) from pg_prepared_statements") assert res[0][0] == 0 # Should have no prepared statements def test_no_data(con): assert con.run("START TRANSACTION") is None def test_multiple_statements(con): statements = "SELECT 5; SELECT 'Erich Fromm';" assert con.run(statements) == [[5], ["Erich Fromm"]] def test_unexecuted_connection_row_count(con): assert con.row_count is None def test_unexecuted_connection_columns(con): assert con.columns is None def test_sql_prepared_statement(con): con.run("PREPARE gen_series AS SELECT generate_series(1, 10);") con.run("EXECUTE gen_series") def test_to_statement(): new_query, _ = to_statement( "SELECT sum(x)::decimal(5, 2) :f_2, :f1 FROM t WHERE a=:f_2" ) expected = "SELECT sum(x)::decimal(5, 2) $1, $2 FROM t WHERE a=$1" assert new_query == expected def test_not_parsed_if_no_params(mocker, con): mock_to_statement = mocker.patch("pg8000.native.to_statement") con.run("ROLLBACK") mock_to_statement.assert_not_called() ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1634149967.0 pg8000-1.23.0/test/native/test_typeconversion.py0000664000175000017500000004664700000000000022270 0ustar00tlocketlocke00000000000000import os import time from collections import OrderedDict from datetime import ( date as Date, datetime as Datetime, time as Time, timedelta as Timedelta, timezone as Timezone, ) from decimal import Decimal from enum import Enum from ipaddress import IPv4Address, IPv4Network from json import dumps from uuid import UUID import pytest import pytz from pg8000.converters import ( BIGINT, BIGINT_ARRAY, BOOLEAN, CIDR_ARRAY, DATE, FLOAT_ARRAY, INET, INTEGER_ARRAY, INTERVAL, JSON, JSONB, JSONB_ARRAY, JSON_ARRAY, MONEY, MONEY_ARRAY, NUMERIC, NUMERIC_ARRAY, PGInterval, POINT, SMALLINT_ARRAY, TIME, TIMESTAMP, TIMESTAMPTZ, TIMESTAMPTZ_ARRAY, TIMESTAMP_ARRAY, UUID_ARRAY, UUID_TYPE, XID, pg_interval_in, pg_interval_out, time_in, ) def test_str_then_int(con): v1 = "hello world" retval = con.run("SELECT cast(:v1 as varchar) as f1", v1=v1) assert retval[0][0] == v1 v2 = 1 retval = con.run("SELECT cast(:v2 as varchar) as f1", v2=v2) assert retval[0][0] == str(v2) def test_insert_null(con): v = None con.run("CREATE TEMPORARY TABLE test_int (f INTEGER)") con.run("INSERT INTO test_int VALUES (:v)", v=v) retval = con.run("SELECT * FROM test_int") assert retval[0][0] == v def test_int_roundtrip(con): int2 = 21 int4 = 23 int8 = 20 MAP = { int2: "int2", int4: "int4", int8: "int8", } test_values = [ (0, int2), (-32767, int2), (-32768, int4), (+32767, int2), (+32768, int4), (-2147483647, int4), (-2147483648, int8), (+2147483647, int4), (+2147483648, int8), (-9223372036854775807, int8), (+9223372036854775807, int8), ] for value, typoid in test_values: retval = con.run("SELECT cast(:v as " + MAP[typoid] + ")", v=value) assert retval[0][0] == value column_type_oid = con.columns[0]["type_oid"] assert column_type_oid == typoid def test_bytearray_subclass_round_trip(con): class BClass(bytearray): pass binary = b"\x00\x01\x02\x03\x02\x01\x00" retval = con.run("SELECT cast(:v as bytea)", v=BClass(binary)) assert retval[0][0] == binary def test_timestamp_roundtrip(is_java, con): v = Datetime(2001, 2, 3, 4, 5, 6, 170000) retval = con.run("SELECT cast(:v as timestamp)", v=v) assert retval[0][0] == v # Test that time zone doesn't affect it # Jython 2.5.3 doesn't have a time.tzset() so skip if not is_java: orig_tz = os.environ.get("TZ") os.environ["TZ"] = "America/Edmonton" time.tzset() retval = con.run("SELECT cast(:v as timestamp)", v=v) assert retval[0][0] == v if orig_tz is None: del os.environ["TZ"] else: os.environ["TZ"] = orig_tz time.tzset() def test_interval_roundtrip(con): con.register_in_adapter(INTERVAL, pg_interval_in) con.register_out_adapter(PGInterval, pg_interval_out) v = PGInterval(microseconds=123456789, days=2, months=24) retval = con.run("SELECT cast(:v as interval)", v=v) assert retval[0][0] == v def test_enum_str_round_trip(con): try: con.run("create type lepton as enum ('electron', 'muon', 'tau')") v = "muon" retval = con.run("SELECT cast(:v as lepton) as f1", v=v) assert retval[0][0] == v con.run("CREATE TEMPORARY TABLE testenum (f1 lepton)") con.run("INSERT INTO testenum VALUES (cast(:v as lepton))", v="electron") finally: con.run("drop table testenum") con.run("drop type lepton") def test_enum_custom_round_trip(con): class Lepton: # Implements PEP 435 in the minimal fashion needed __members__ = OrderedDict() def __init__(self, name, value, alias=None): self.name = name self.value = value self.__members__[name] = self setattr(self.__class__, name, self) if alias: self.__members__[alias] = self setattr(self.__class__, alias, self) def lepton_out(lepton): return lepton.value try: con.run("create type lepton as enum ('1', '2', '3')") con.register_out_adapter(Lepton, lepton_out) v = Lepton("muon", "2") retval = con.run("SELECT CAST(:v AS lepton)", v=v) assert retval[0][0] == v.value finally: con.run("drop type lepton") def test_enum_py_round_trip(con): class Lepton(Enum): electron = "1" muon = "2" tau = "3" try: con.run("create type lepton as enum ('1', '2', '3')") v = Lepton.muon retval = con.run("SELECT cast(:v as lepton) as f1", v=v) assert retval[0][0] == v.value con.run("CREATE TEMPORARY TABLE testenum (f1 lepton)") con.run("INSERT INTO testenum VALUES (cast(:v as lepton))", v=Lepton.electron) finally: con.run("drop table testenum") con.run("drop type lepton") def test_xml_roundtrip(con): v = "gatccgagtac" retval = con.run("select xmlparse(content :v) as f1", v=v) assert retval[0][0] == v def test_int2vector_in(con): retval = con.run("select cast('1 2' as int2vector) as f1") assert retval[0][0] == [1, 2] # Should complete without an exception con.run("select indkey from pg_index") def test_timestamp_tz_out(con): retval = con.run( "SELECT '2001-02-03 04:05:06.17 America/Edmonton'" "::timestamp with time zone" ) dt = retval[0][0] assert dt.tzinfo is not None, "no tzinfo returned" assert dt.astimezone(Timezone.utc) == Datetime( 2001, 2, 3, 11, 5, 6, 170000, Timezone.utc ), "retrieved value match failed" def test_timestamp_tz_roundtrip(is_java, con): if not is_java: mst = pytz.timezone("America/Edmonton") v1 = mst.localize(Datetime(2001, 2, 3, 4, 5, 6, 170000)) retval = con.run("SELECT cast(:v as timestamptz)", v=v1) v2 = retval[0][0] assert v2.tzinfo is not None assert v1 == v2 def test_timestamp_mismatch(is_java, con): if not is_java: mst = pytz.timezone("America/Edmonton") con.run("SET SESSION TIME ZONE 'America/Edmonton'") try: con.run( "CREATE TEMPORARY TABLE TestTz (f1 timestamp with time zone, " "f2 timestamp without time zone)" ) con.run( "INSERT INTO TestTz (f1, f2) VALUES (:v1, :v2)", # insert timestamp into timestamptz field (v1) v1=Datetime(2001, 2, 3, 4, 5, 6, 170000), # insert timestamptz into timestamp field (v2) v2=mst.localize(Datetime(2001, 2, 3, 4, 5, 6, 170000)), ) retval = con.run("SELECT f1, f2 FROM TestTz") # when inserting a timestamp into a timestamptz field, # postgresql assumes that it is in local time. So the value # that comes out will be the server's local time interpretation # of v1. We've set the server's TZ to MST, the time should # be... f1 = retval[0][0] assert f1 == Datetime(2001, 2, 3, 11, 5, 6, 170000, Timezone.utc) # inserting the timestamptz into a timestamp field, pg8000 converts the # value into UTC, and then the PG server sends that time back f2 = retval[0][1] assert f2 == Datetime(2001, 2, 3, 11, 5, 6, 170000) finally: con.run("SET SESSION TIME ZONE DEFAULT") def test_name_out(con): # select a field that is of "name" type: con.run("SELECT usename FROM pg_user") # It is sufficient that no errors were encountered. def test_oid_out(con): con.run("SELECT oid FROM pg_type") # It is sufficient that no errors were encountered. def test_boolean_in(con): retval = con.run("SELECT cast('t' as bool)") assert retval[0][0] def test_numeric_out(con): for num in ("5000", "50.34"): retval = con.run("SELECT " + num + "::numeric") assert str(retval[0][0]) == num def test_int2_out(con): retval = con.run("SELECT 5000::smallint") assert retval[0][0] == 5000 def test_int4_out(con): retval = con.run("SELECT 5000::integer") assert retval[0][0] == 5000 def test_int8_out(con): retval = con.run("SELECT 50000000000000::bigint") assert retval[0][0] == 50000000000000 def test_float4_out(con): retval = con.run("SELECT 1.1::real") assert retval[0][0] == 1.1 def test_float8_out(con): retval = con.run("SELECT 1.1::double precision") assert retval[0][0] == 1.1000000000000001 def test_varchar_out(con): retval = con.run("SELECT 'hello'::varchar(20)") assert retval[0][0] == "hello" def test_char_out(con): retval = con.run("SELECT 'hello'::char(20)") assert retval[0][0] == "hello " def test_text_out(con): retval = con.run("SELECT 'hello'::text") assert retval[0][0] == "hello" def test_pg_interval_in(con): con.register_in_adapter(1186, pg_interval_in) retval = con.run( "SELECT CAST('1 month 16 days 12 hours 32 minutes 64 seconds' as INTERVAL)" ) expected_value = PGInterval( microseconds=(12 * 60 * 60 * 1000 * 1000) + (32 * 60 * 1000 * 1000) + (64 * 1000 * 1000), days=16, months=1, ) assert retval[0][0] == expected_value def test_interval_in_30_seconds(con): retval = con.run("select interval '30 seconds'") assert retval[0][0] == Timedelta(seconds=30) def test_interval_in_12_days_30_seconds(con): retval = con.run("select interval '12 days 30 seconds'") assert retval[0][0] == Timedelta(days=12, seconds=30) def test_timestamp_out(con): retval = con.run("SELECT '2001-02-03 04:05:06.17'::timestamp") assert retval[0][0] == Datetime(2001, 2, 3, 4, 5, 6, 170000) def test_int4_array_out(con): retval = con.run( "SELECT '{1,2,3,4}'::INT[] AS f1, '{{1,2,3},{4,5,6}}'::INT[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::INT[][][] AS f3" ) f1, f2, f3 = retval[0] assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_int2_array_out(con): res = con.run( "SELECT '{1,2,3,4}'::INT2[] AS f1, " "'{{1,2,3},{4,5,6}}'::INT2[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::INT2[][][] AS f3" ) f1, f2, f3 = res[0] assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_int8_array_out(con): res = con.run( "SELECT '{1,2,3,4}'::INT8[] AS f1, " "'{{1,2,3},{4,5,6}}'::INT8[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::INT8[][][] AS f3" ) f1, f2, f3 = res[0] assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_bool_array_out(con): res = con.run( "SELECT '{TRUE,FALSE,FALSE,TRUE}'::BOOL[] AS f1, " "'{{TRUE,FALSE,TRUE},{FALSE,TRUE,FALSE}}'::BOOL[][] AS f2, " "'{{{TRUE,FALSE},{FALSE,TRUE}},{{NULL,TRUE},{FALSE,FALSE}}}'" "::BOOL[][][] AS f3" ) f1, f2, f3 = res[0] assert f1 == [True, False, False, True] assert f2 == [[True, False, True], [False, True, False]] assert f3 == [[[True, False], [False, True]], [[None, True], [False, False]]] def test_float4_array_out(con): res = con.run( "SELECT '{1,2,3,4}'::FLOAT4[] AS f1, " "'{{1,2,3},{4,5,6}}'::FLOAT4[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::FLOAT4[][][] AS f3" ) f1, f2, f3 = res[0] assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] def test_float8_array_out(con): res = con.run( "SELECT '{1,2,3,4}'::FLOAT8[] AS f1, " "'{{1,2,3},{4,5,6}}'::FLOAT8[][] AS f2, " "'{{{1,2},{3,4}},{{NULL,6},{7,8}}}'::FLOAT8[][][] AS f3" ) f1, f2, f3 = res[0] assert f1 == [1, 2, 3, 4] assert f2 == [[1, 2, 3], [4, 5, 6]] assert f3 == [[[1, 2], [3, 4]], [[None, 6], [7, 8]]] CURRENCIES = { "en_GB.UTF-8": "£", "C.UTF-8": "$", "C.UTF8": "$", } LANG = os.environ["LANG"] CURRENCY = CURRENCIES[LANG] @pytest.mark.parametrize( "test_input,oid", [ [[Datetime(2001, 2, 3, 4, 5, 6)], TIMESTAMP_ARRAY], # timestamp[] [ # timestamptz[] [Datetime(2001, 2, 3, 4, 5, 6, 0, Timezone.utc)], TIMESTAMPTZ_ARRAY, ], [ {"name": "Apollo 11 Cave", "zebra": True, "age": 26.003}, # json JSON, ], [{"name": "Apollo 11 Cave", "zebra": True, "age": 26.003}, JSONB], # jsonb [[IPv4Network("192.168.0.0/28")], CIDR_ARRAY], # cidr[] [[1, 2, 3], SMALLINT_ARRAY], # int2[] [[[1, 2], [3, 4]], SMALLINT_ARRAY], # int2[] multidimensional [[1, None, 3], INTEGER_ARRAY], # int4[] with None [[7000000000, 2, 3], BIGINT_ARRAY], # int8[] [[1.1, 2.2, 3.3], FLOAT_ARRAY], # float8[] [[Decimal("1.1"), None, Decimal("3.3")], NUMERIC_ARRAY], # numeric[] [[f"{CURRENCY}1.10", None, f"{CURRENCY}3.30"], MONEY_ARRAY], # money[] [[UUID("911460f2-1f43-fea2-3e2c-e01fd5b5069d")], UUID_ARRAY], # uuid[] [ # json[] [{"name": "Apollo 11 Cave", "zebra": True, "age": 26.003}], JSON_ARRAY, ], [ # jsonb[] [{"name": "Apollo 11 Cave", "zebra": True, "age": 26.003}], JSONB_ARRAY, ], [Time(4, 5, 6), TIME], # time [Date(2001, 2, 3), DATE], # date [Datetime(2001, 2, 3, 4, 5, 6), TIMESTAMP], # timestamp [Datetime(2001, 2, 3, 4, 5, 6, 0, Timezone.utc), TIMESTAMPTZ], # timestamptz [True, BOOLEAN], # bool [None, BOOLEAN], # null [Decimal("1.1"), NUMERIC], # numeric [f"{CURRENCY}1.10", MONEY], # money [f"-{CURRENCY}1.10", MONEY], # money [50000000000000, BIGINT], # int8 [UUID("911460f2-1f43-fea2-3e2c-e01fd5b5069d"), UUID_TYPE], # uuid [IPv4Network("192.168.0.0/28"), INET], # inet [IPv4Address("192.168.0.1"), INET], # inet [86722, XID], # xid ["infinity", TIMESTAMP], # timestamp ["(2.3,1)", POINT], # point [{"name": "Apollo 11 Cave", "zebra": True, "age": 26.003}, JSON], # json [{"name": "Apollo 11 Cave", "zebra": True, "age": 26.003}, JSONB], # jsonb ], ) def test_roundtrip_oid(con, test_input, oid): retval = con.run("SELECT :v", v=test_input, types={"v": oid}) assert retval[0][0] == test_input assert oid == con.columns[0]["type_oid"] @pytest.mark.parametrize( "test_input,typ,required_version", [ [[True, False, None], "bool[]", None], [[IPv4Address("192.168.0.1")], "inet[]", None], [[Date(2021, 3, 1)], "date[]", None], [[Datetime(2001, 2, 3, 4, 5, 6)], "timestamp[]", None], [[Datetime(2001, 2, 3, 4, 5, 6, 0, Timezone.utc)], "timestamptz[]", None], [[Time(4, 5, 6)], "time[]", None], [[Timedelta(seconds=30)], "interval[]", None], [[{"name": "Apollo 11 Cave", "zebra": True, "age": 26.003}], "jsonb[]", None], [[b"\x00\x01\x02\x03\x02\x01\x00"], "bytea[]", None], [[Decimal("1.1"), None, Decimal("3.3")], "numeric[]", None], [[UUID("911460f2-1f43-fea2-3e2c-e01fd5b5069d")], "uuid[]", None], [ [ "Hello!", "World!", "abcdefghijklmnopqrstuvwxyz", "", "A bunch of random characters:", " ~!@#$%^&*()_+`1234567890-=[]\\{}|{;':\",./<>?\t", None, ], "varchar[]", None, ], [Timedelta(seconds=30), "interval", None], [Time(4, 5, 6), "time", None], [Date(2001, 2, 3), "date", None], [Datetime(2001, 2, 3, 4, 5, 6), "timestamp", None], [Datetime(2001, 2, 3, 4, 5, 6, 0, Timezone.utc), "timestamptz", None], [True, "bool", 10], [Decimal("1.1"), "numeric", None], [1.756e-12, "float8", None], [float("inf"), "float8", None], ["hello world", "unknown", 10], ["hello \u0173 world", "varchar", 10], [50000000000000, "int8", None], [b"\x00\x01\x02\x03\x02\x01\x00", "bytea", None], [bytearray(b"\x00\x01\x02\x03\x02\x01\x00"), "bytea", None], [UUID("911460f2-1f43-fea2-3e2c-e01fd5b5069d"), "uuid", None], [IPv4Network("192.168.0.0/28"), "inet", None], [IPv4Address("192.168.0.1"), "inet", None], ], ) def test_roundtrip_cast(con, pg_version, test_input, typ, required_version): if required_version is not None and pg_version < required_version: return retval = con.run(f"SELECT CAST(:v AS {typ})", v=test_input) assert retval[0][0] == test_input @pytest.mark.parametrize( "test_input,expected", [ ("SELECT CAST('{a,b,c}' AS TEXT[])", ["a", "b", "c"]), ("SELECT CAST('{a,b,c}' AS CHAR[])", ["a", "b", "c"]), ("SELECT CAST('{a,b,c}' AS VARCHAR[])", ["a", "b", "c"]), ("SELECT CAST('{a,b,c}' AS CSTRING[])", ["a", "b", "c"]), ("SELECT CAST('{a,b,c}' AS NAME[])", ["a", "b", "c"]), ("SELECT CAST('{}' AS text[])", []), ('SELECT CAST(\'{NULL,"NULL",NULL,""}\' AS text[])', [None, "NULL", None, ""]), ], ) def test_array_in(con, test_input, expected): result = con.run(test_input) assert result[0][0] == expected def test_numeric_array_out(con): res = con.run("SELECT '{1.1,2.2,3.3}'::numeric[] AS f1") assert res[0][0] == [Decimal("1.1"), Decimal("2.2"), Decimal("3.3")] def test_empty_array(con): v = [] retval = con.run("SELECT cast(:v as varchar[])", v=v) assert retval[0][0] == v def test_macaddr(con): retval = con.run("SELECT macaddr '08002b:010203'") assert retval[0][0] == "08:00:2b:01:02:03" def test_tsvector_roundtrip(con): retval = con.run( "SELECT cast(:v as tsvector)", v="a fat cat sat on a mat and ate a fat rat" ) assert retval[0][0] == "'a' 'and' 'ate' 'cat' 'fat' 'mat' 'on' 'rat' 'sat'" def test_hstore_roundtrip(con): val = '"a"=>"1"' retval = con.run("SELECT cast(:val as hstore)", val=val) assert retval[0][0] == val def test_json_access_object(con): val = {"name": "Apollo 11 Cave", "zebra": True, "age": 26.003} retval = con.run("SELECT cast(:val as json) -> :name", val=dumps(val), name="name") assert retval[0][0] == "Apollo 11 Cave" def test_jsonb_access_object(con): val = {"name": "Apollo 11 Cave", "zebra": True, "age": 26.003} retval = con.run("SELECT cast(:val as jsonb) -> :name", val=dumps(val), name="name") assert retval[0][0] == "Apollo 11 Cave" def test_json_access_array(con): val = [-1, -2, -3, -4, -5] retval = con.run( "SELECT cast(:v1 as json) -> cast(:v2 as int)", v1=dumps(val), v2=2 ) assert retval[0][0] == -3 def test_jsonb_access_array(con): val = [-1, -2, -3, -4, -5] retval = con.run( "SELECT cast(:v1 as jsonb) -> cast(:v2 as int)", v1=dumps(val), v2=2 ) assert retval[0][0] == -3 def test_jsonb_access_path(con): j = {"a": [1, 2, 3], "b": [4, 5, 6]} path = ["a", "2"] retval = con.run("SELECT cast(:v1 as jsonb) #>> :v2", v1=dumps(j), v2=path) assert retval[0][0] == str(j[path[0]][int(path[1])]) def test_time_in(): actual = time_in("12:57:18.000396") assert actual == Time(12, 57, 18, 396) ././@PaxHeader0000000000000000000000000000002600000000000011453 xustar000000000000000022 mtime=1627647036.0 pg8000-1.23.0/versioneer.py0000664000175000017500000021113600000000000016041 0ustar00tlocketlocke00000000000000# Version: 0.19 """The Versioneer - like a rocketeer, but for versions. The Versioneer ============== * like a rocketeer, but for versions! * https://github.com/python-versioneer/python-versioneer * Brian Warner * License: Public Domain * Compatible with: Python 3.6, 3.7, 3.8, 3.9 and pypy3 * [![Latest Version][pypi-image]][pypi-url] * [![Build Status][travis-image]][travis-url] This is a tool for managing a recorded version number in distutils-based python projects. The goal is to remove the tedious and error-prone "update the embedded version string" step from your release process. Making a new release should be as easy as recording a new tag in your version-control system, and maybe making new tarballs. ## Quick Install * `pip install versioneer` to somewhere in your $PATH * add a `[versioneer]` section to your setup.cfg (see [Install](INSTALL.md)) * run `versioneer install` in your source tree, commit the results * Verify version information with `python setup.py version` ## Version Identifiers Source trees come from a variety of places: * a version-control system checkout (mostly used by developers) * a nightly tarball, produced by build automation * a snapshot tarball, produced by a web-based VCS browser, like github's "tarball from tag" feature * a release tarball, produced by "setup.py sdist", distributed through PyPI Within each source tree, the version identifier (either a string or a number, this tool is format-agnostic) can come from a variety of places: * ask the VCS tool itself, e.g. "git describe" (for checkouts), which knows about recent "tags" and an absolute revision-id * the name of the directory into which the tarball was unpacked * an expanded VCS keyword ($Id$, etc) * a `_version.py` created by some earlier build step For released software, the version identifier is closely related to a VCS tag. Some projects use tag names that include more than just the version string (e.g. "myproject-1.2" instead of just "1.2"), in which case the tool needs to strip the tag prefix to extract the version identifier. For unreleased software (between tags), the version identifier should provide enough information to help developers recreate the same tree, while also giving them an idea of roughly how old the tree is (after version 1.2, before version 1.3). Many VCS systems can report a description that captures this, for example `git describe --tags --dirty --always` reports things like "0.7-1-g574ab98-dirty" to indicate that the checkout is one revision past the 0.7 tag, has a unique revision id of "574ab98", and is "dirty" (it has uncommitted changes). The version identifier is used for multiple purposes: * to allow the module to self-identify its version: `myproject.__version__` * to choose a name and prefix for a 'setup.py sdist' tarball ## Theory of Operation Versioneer works by adding a special `_version.py` file into your source tree, where your `__init__.py` can import it. This `_version.py` knows how to dynamically ask the VCS tool for version information at import time. `_version.py` also contains `$Revision$` markers, and the installation process marks `_version.py` to have this marker rewritten with a tag name during the `git archive` command. As a result, generated tarballs will contain enough information to get the proper version. To allow `setup.py` to compute a version too, a `versioneer.py` is added to the top level of your source tree, next to `setup.py` and the `setup.cfg` that configures it. This overrides several distutils/setuptools commands to compute the version when invoked, and changes `setup.py build` and `setup.py sdist` to replace `_version.py` with a small static file that contains just the generated version data. ## Installation See [INSTALL.md](./INSTALL.md) for detailed installation instructions. ## Version-String Flavors Code which uses Versioneer can learn about its version string at runtime by importing `_version` from your main `__init__.py` file and running the `get_versions()` function. From the "outside" (e.g. in `setup.py`), you can import the top-level `versioneer.py` and run `get_versions()`. Both functions return a dictionary with different flavors of version information: * `['version']`: A condensed version string, rendered using the selected style. This is the most commonly used value for the project's version string. The default "pep440" style yields strings like `0.11`, `0.11+2.g1076c97`, or `0.11+2.g1076c97.dirty`. See the "Styles" section below for alternative styles. * `['full-revisionid']`: detailed revision identifier. For Git, this is the full SHA1 commit id, e.g. "1076c978a8d3cfc70f408fe5974aa6c092c949ac". * `['date']`: Date and time of the latest `HEAD` commit. For Git, it is the commit date in ISO 8601 format. This will be None if the date is not available. * `['dirty']`: a boolean, True if the tree has uncommitted changes. Note that this is only accurate if run in a VCS checkout, otherwise it is likely to be False or None * `['error']`: if the version string could not be computed, this will be set to a string describing the problem, otherwise it will be None. It may be useful to throw an exception in setup.py if this is set, to avoid e.g. creating tarballs with a version string of "unknown". Some variants are more useful than others. Including `full-revisionid` in a bug report should allow developers to reconstruct the exact code being tested (or indicate the presence of local changes that should be shared with the developers). `version` is suitable for display in an "about" box or a CLI `--version` output: it can be easily compared against release notes and lists of bugs fixed in various releases. The installer adds the following text to your `__init__.py` to place a basic version in `YOURPROJECT.__version__`: from ._version import get_versions __version__ = get_versions()['version'] del get_versions ## Styles The setup.cfg `style=` configuration controls how the VCS information is rendered into a version string. The default style, "pep440", produces a PEP440-compliant string, equal to the un-prefixed tag name for actual releases, and containing an additional "local version" section with more detail for in-between builds. For Git, this is TAG[+DISTANCE.gHEX[.dirty]] , using information from `git describe --tags --dirty --always`. For example "0.11+2.g1076c97.dirty" indicates that the tree is like the "1076c97" commit but has uncommitted changes (".dirty"), and that this commit is two revisions ("+2") beyond the "0.11" tag. For released software (exactly equal to a known tag), the identifier will only contain the stripped tag, e.g. "0.11". Other styles are available. See [details.md](details.md) in the Versioneer source tree for descriptions. ## Debugging Versioneer tries to avoid fatal errors: if something goes wrong, it will tend to return a version of "0+unknown". To investigate the problem, run `setup.py version`, which will run the version-lookup code in a verbose mode, and will display the full contents of `get_versions()` (including the `error` string, which may help identify what went wrong). ## Known Limitations Some situations are known to cause problems for Versioneer. This details the most significant ones. More can be found on Github [issues page](https://github.com/python-versioneer/python-versioneer/issues). ### Subprojects Versioneer has limited support for source trees in which `setup.py` is not in the root directory (e.g. `setup.py` and `.git/` are *not* siblings). The are two common reasons why `setup.py` might not be in the root: * Source trees which contain multiple subprojects, such as [Buildbot](https://github.com/buildbot/buildbot), which contains both "master" and "slave" subprojects, each with their own `setup.py`, `setup.cfg`, and `tox.ini`. Projects like these produce multiple PyPI distributions (and upload multiple independently-installable tarballs). * Source trees whose main purpose is to contain a C library, but which also provide bindings to Python (and perhaps other languages) in subdirectories. Versioneer will look for `.git` in parent directories, and most operations should get the right version string. However `pip` and `setuptools` have bugs and implementation details which frequently cause `pip install .` from a subproject directory to fail to find a correct version string (so it usually defaults to `0+unknown`). `pip install --editable .` should work correctly. `setup.py install` might work too. Pip-8.1.1 is known to have this problem, but hopefully it will get fixed in some later version. [Bug #38](https://github.com/python-versioneer/python-versioneer/issues/38) is tracking this issue. The discussion in [PR #61](https://github.com/python-versioneer/python-versioneer/pull/61) describes the issue from the Versioneer side in more detail. [pip PR#3176](https://github.com/pypa/pip/pull/3176) and [pip PR#3615](https://github.com/pypa/pip/pull/3615) contain work to improve pip to let Versioneer work correctly. Versioneer-0.16 and earlier only looked for a `.git` directory next to the `setup.cfg`, so subprojects were completely unsupported with those releases. ### Editable installs with setuptools <= 18.5 `setup.py develop` and `pip install --editable .` allow you to install a project into a virtualenv once, then continue editing the source code (and test) without re-installing after every change. "Entry-point scripts" (`setup(entry_points={"console_scripts": ..})`) are a convenient way to specify executable scripts that should be installed along with the python package. These both work as expected when using modern setuptools. When using setuptools-18.5 or earlier, however, certain operations will cause `pkg_resources.DistributionNotFound` errors when running the entrypoint script, which must be resolved by re-installing the package. This happens when the install happens with one version, then the egg_info data is regenerated while a different version is checked out. Many setup.py commands cause egg_info to be rebuilt (including `sdist`, `wheel`, and installing into a different virtualenv), so this can be surprising. [Bug #83](https://github.com/python-versioneer/python-versioneer/issues/83) describes this one, but upgrading to a newer version of setuptools should probably resolve it. ## Updating Versioneer To upgrade your project to a new release of Versioneer, do the following: * install the new Versioneer (`pip install -U versioneer` or equivalent) * edit `setup.cfg`, if necessary, to include any new configuration settings indicated by the release notes. See [UPGRADING](./UPGRADING.md) for details. * re-run `versioneer install` in your source tree, to replace `SRC/_version.py` * commit any changed files ## Future Directions This tool is designed to make it easily extended to other version-control systems: all VCS-specific components are in separate directories like src/git/ . The top-level `versioneer.py` script is assembled from these components by running make-versioneer.py . In the future, make-versioneer.py will take a VCS name as an argument, and will construct a version of `versioneer.py` that is specific to the given VCS. It might also take the configuration arguments that are currently provided manually during installation by editing setup.py . Alternatively, it might go the other direction and include code from all supported VCS systems, reducing the number of intermediate scripts. ## Similar projects * [setuptools_scm](https://github.com/pypa/setuptools_scm/) - a non-vendored build-time dependency * [minver](https://github.com/jbweston/miniver) - a lightweight reimplementation of versioneer ## License To make Versioneer easier to embed, all its code is dedicated to the public domain. The `_version.py` that it creates is also in the public domain. Specifically, both are released under the Creative Commons "Public Domain Dedication" license (CC0-1.0), as described in https://creativecommons.org/publicdomain/zero/1.0/ . [pypi-image]: https://img.shields.io/pypi/v/versioneer.svg [pypi-url]: https://pypi.python.org/pypi/versioneer/ [travis-image]: https://img.shields.io/travis/com/python-versioneer/python-versioneer.svg [travis-url]: https://travis-ci.com/github/python-versioneer/python-versioneer """ import configparser import errno import json import os import re import subprocess import sys class VersioneerConfig: """Container for Versioneer configuration parameters.""" def get_root(): """Get the project root directory. We require that all commands are run from the project root, i.e. the directory that contains setup.py, setup.cfg, and versioneer.py . """ root = os.path.realpath(os.path.abspath(os.getcwd())) setup_py = os.path.join(root, "setup.py") versioneer_py = os.path.join(root, "versioneer.py") if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): # allow 'python path/to/setup.py COMMAND' root = os.path.dirname(os.path.realpath(os.path.abspath(sys.argv[0]))) setup_py = os.path.join(root, "setup.py") versioneer_py = os.path.join(root, "versioneer.py") if not (os.path.exists(setup_py) or os.path.exists(versioneer_py)): err = ( "Versioneer was unable to run the project root directory. " "Versioneer requires setup.py to be executed from " "its immediate directory (like 'python setup.py COMMAND'), " "or in a way that lets it use sys.argv[0] to find the root " "(like 'python path/to/setup.py COMMAND')." ) raise VersioneerBadRootError(err) try: # Certain runtime workflows (setup.py install/develop in a setuptools # tree) execute all dependencies in a single python process, so # "versioneer" may be imported multiple times, and python's shared # module-import table will cache the first one. So we can't use # os.path.dirname(__file__), as that will find whichever # versioneer.py was first imported, even in later projects. me = os.path.realpath(os.path.abspath(__file__)) me_dir = os.path.normcase(os.path.splitext(me)[0]) vsr_dir = os.path.normcase(os.path.splitext(versioneer_py)[0]) if me_dir != vsr_dir: print( "Warning: build in %s is using versioneer.py from %s" % (os.path.dirname(me), versioneer_py) ) except NameError: pass return root def get_config_from_root(root): """Read the project setup.cfg file to determine Versioneer config.""" # This might raise EnvironmentError (if setup.cfg is missing), or # configparser.NoSectionError (if it lacks a [versioneer] section), or # configparser.NoOptionError (if it lacks "VCS="). See the docstring at # the top of versioneer.py for instructions on writing your setup.cfg . setup_cfg = os.path.join(root, "setup.cfg") parser = configparser.ConfigParser() with open(setup_cfg, "r") as f: parser.read_file(f) VCS = parser.get("versioneer", "VCS") # mandatory def get(parser, name): if parser.has_option("versioneer", name): return parser.get("versioneer", name) return None cfg = VersioneerConfig() cfg.VCS = VCS cfg.style = get(parser, "style") or "" cfg.versionfile_source = get(parser, "versionfile_source") cfg.versionfile_build = get(parser, "versionfile_build") cfg.tag_prefix = get(parser, "tag_prefix") if cfg.tag_prefix in ("''", '""'): cfg.tag_prefix = "" cfg.parentdir_prefix = get(parser, "parentdir_prefix") cfg.verbose = get(parser, "verbose") return cfg class NotThisMethod(Exception): """Exception raised if a method is not valid for the current scenario.""" # these dictionaries contain VCS-specific tools LONG_VERSION_PY = {} HANDLERS = {} def register_vcs_handler(vcs, method): # decorator """Create decorator to mark a method as the handler of a VCS.""" def decorate(f): """Store f in HANDLERS[vcs][method].""" if vcs not in HANDLERS: HANDLERS[vcs] = {} HANDLERS[vcs][method] = f return f return decorate def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=None): """Call the given command(s).""" assert isinstance(commands, list) p = None for c in commands: try: dispcmd = str([c] + args) # remember shell=False, so use git.cmd on windows, not just git p = subprocess.Popen( [c] + args, cwd=cwd, env=env, stdout=subprocess.PIPE, stderr=(subprocess.PIPE if hide_stderr else None), ) break except EnvironmentError: e = sys.exc_info()[1] if e.errno == errno.ENOENT: continue if verbose: print("unable to run %s" % dispcmd) print(e) return None, None else: if verbose: print("unable to find command, tried %s" % (commands,)) return None, None stdout = p.communicate()[0].strip().decode() if p.returncode != 0: if verbose: print("unable to run %s (error)" % dispcmd) print("stdout was %s" % stdout) return None, p.returncode return stdout, p.returncode LONG_VERSION_PY[ "git" ] = r''' # This file helps to compute a version number in source trees obtained from # git-archive tarball (such as those provided by githubs download-from-tag # feature). Distribution tarballs (built by setup.py sdist) and build # directories (produced by setup.py build) will contain a much shorter file # that just contains the computed version number. # This file is released into the public domain. Generated by # versioneer-0.19 (https://github.com/python-versioneer/python-versioneer) """Git implementation of _version.py.""" import errno import os import re import subprocess import sys def get_keywords(): """Get the keywords needed to look up the version information.""" # these strings will be replaced by git during git-archive. # setup.py/versioneer.py will grep for the variable names, so they must # each be defined on a line of their own. _version.py will just call # get_keywords(). git_refnames = "%(DOLLAR)sFormat:%%d%(DOLLAR)s" git_full = "%(DOLLAR)sFormat:%%H%(DOLLAR)s" git_date = "%(DOLLAR)sFormat:%%ci%(DOLLAR)s" keywords = {"refnames": git_refnames, "full": git_full, "date": git_date} return keywords class VersioneerConfig: """Container for Versioneer configuration parameters.""" def get_config(): """Create, populate and return the VersioneerConfig() object.""" # these strings are filled in when 'setup.py versioneer' creates # _version.py cfg = VersioneerConfig() cfg.VCS = "git" cfg.style = "%(STYLE)s" cfg.tag_prefix = "%(TAG_PREFIX)s" cfg.parentdir_prefix = "%(PARENTDIR_PREFIX)s" cfg.versionfile_source = "%(VERSIONFILE_SOURCE)s" cfg.verbose = False return cfg class NotThisMethod(Exception): """Exception raised if a method is not valid for the current scenario.""" LONG_VERSION_PY = {} HANDLERS = {} def register_vcs_handler(vcs, method): # decorator """Create decorator to mark a method as the handler of a VCS.""" def decorate(f): """Store f in HANDLERS[vcs][method].""" if vcs not in HANDLERS: HANDLERS[vcs] = {} HANDLERS[vcs][method] = f return f return decorate def run_command(commands, args, cwd=None, verbose=False, hide_stderr=False, env=None): """Call the given command(s).""" assert isinstance(commands, list) p = None for c in commands: try: dispcmd = str([c] + args) # remember shell=False, so use git.cmd on windows, not just git p = subprocess.Popen([c] + args, cwd=cwd, env=env, stdout=subprocess.PIPE, stderr=(subprocess.PIPE if hide_stderr else None)) break except EnvironmentError: e = sys.exc_info()[1] if e.errno == errno.ENOENT: continue if verbose: print("unable to run %%s" %% dispcmd) print(e) return None, None else: if verbose: print("unable to find command, tried %%s" %% (commands,)) return None, None stdout = p.communicate()[0].strip().decode() if p.returncode != 0: if verbose: print("unable to run %%s (error)" %% dispcmd) print("stdout was %%s" %% stdout) return None, p.returncode return stdout, p.returncode def versions_from_parentdir(parentdir_prefix, root, verbose): """Try to determine the version from the parent directory name. Source tarballs conventionally unpack into a directory that includes both the project name and a version string. We will also support searching up two directory levels for an appropriately named parent directory """ rootdirs = [] for i in range(3): dirname = os.path.basename(root) if dirname.startswith(parentdir_prefix): return {"version": dirname[len(parentdir_prefix):], "full-revisionid": None, "dirty": False, "error": None, "date": None} else: rootdirs.append(root) root = os.path.dirname(root) # up a level if verbose: print("Tried directories %%s but none started with prefix %%s" %% (str(rootdirs), parentdir_prefix)) raise NotThisMethod("rootdir doesn't start with parentdir_prefix") @register_vcs_handler("git", "get_keywords") def git_get_keywords(versionfile_abs): """Extract version information from the given file.""" # the code embedded in _version.py can just fetch the value of these # keywords. When used from setup.py, we don't want to import _version.py, # so we do it with a regexp instead. This function is not used from # _version.py. keywords = {} try: f = open(versionfile_abs, "r") for line in f.readlines(): if line.strip().startswith("git_refnames ="): mo = re.search(r'=\s*"(.*)"', line) if mo: keywords["refnames"] = mo.group(1) if line.strip().startswith("git_full ="): mo = re.search(r'=\s*"(.*)"', line) if mo: keywords["full"] = mo.group(1) if line.strip().startswith("git_date ="): mo = re.search(r'=\s*"(.*)"', line) if mo: keywords["date"] = mo.group(1) f.close() except EnvironmentError: pass return keywords @register_vcs_handler("git", "keywords") def git_versions_from_keywords(keywords, tag_prefix, verbose): """Get version information from git keywords.""" if not keywords: raise NotThisMethod("no keywords at all, weird") date = keywords.get("date") if date is not None: # Use only the last line. Previous lines may contain GPG signature # information. date = date.splitlines()[-1] # git-2.2.0 added "%%cI", which expands to an ISO-8601 -compliant # datestamp. However we prefer "%%ci" (which expands to an "ISO-8601 # -like" string, which we must then edit to make compliant), because # it's been around since git-1.5.3, and it's too difficult to # discover which version we're using, or to work around using an # older one. date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) refnames = keywords["refnames"].strip() if refnames.startswith("$Format"): if verbose: print("keywords are unexpanded, not using") raise NotThisMethod("unexpanded keywords, not a git-archive tarball") refs = set([r.strip() for r in refnames.strip("()").split(",")]) # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of # just "foo-1.0". If we see a "tag: " prefix, prefer those. TAG = "tag: " tags = set([r[len(TAG):] for r in refs if r.startswith(TAG)]) if not tags: # Either we're using git < 1.8.3, or there really are no tags. We use # a heuristic: assume all version tags have a digit. The old git %%d # expansion behaves like git log --decorate=short and strips out the # refs/heads/ and refs/tags/ prefixes that would let us distinguish # between branches and tags. By ignoring refnames without digits, we # filter out many common branch names like "release" and # "stabilization", as well as "HEAD" and "master". tags = set([r for r in refs if re.search(r'\d', r)]) if verbose: print("discarding '%%s', no digits" %% ",".join(refs - tags)) if verbose: print("likely tags: %%s" %% ",".join(sorted(tags))) for ref in sorted(tags): # sorting will prefer e.g. "2.0" over "2.0rc1" if ref.startswith(tag_prefix): r = ref[len(tag_prefix):] if verbose: print("picking %%s" %% r) return {"version": r, "full-revisionid": keywords["full"].strip(), "dirty": False, "error": None, "date": date} # no suitable tags, so version is "0+unknown", but full hex is still there if verbose: print("no suitable tags, using unknown + full revision id") return {"version": "0+unknown", "full-revisionid": keywords["full"].strip(), "dirty": False, "error": "no suitable tags", "date": None} @register_vcs_handler("git", "pieces_from_vcs") def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): """Get version from 'git describe' in the root of the source tree. This only gets called if the git-archive 'subst' keywords were *not* expanded, and _version.py hasn't already been rewritten with a short version string, meaning we're inside a checked out source tree. """ GITS = ["git"] if sys.platform == "win32": GITS = ["git.cmd", "git.exe"] out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=True) if rc != 0: if verbose: print("Directory %%s not under git control" %% root) raise NotThisMethod("'git rev-parse --git-dir' returned error") # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] # if there isn't one, this yields HEX[-dirty] (no NUM) describe_out, rc = run_command(GITS, ["describe", "--tags", "--dirty", "--always", "--long", "--match", "%%s*" %% tag_prefix], cwd=root) # --long was added in git-1.5.5 if describe_out is None: raise NotThisMethod("'git describe' failed") describe_out = describe_out.strip() full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) if full_out is None: raise NotThisMethod("'git rev-parse' failed") full_out = full_out.strip() pieces = {} pieces["long"] = full_out pieces["short"] = full_out[:7] # maybe improved later pieces["error"] = None # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] # TAG might have hyphens. git_describe = describe_out # look for -dirty suffix dirty = git_describe.endswith("-dirty") pieces["dirty"] = dirty if dirty: git_describe = git_describe[:git_describe.rindex("-dirty")] # now we have TAG-NUM-gHEX or HEX if "-" in git_describe: # TAG-NUM-gHEX mo = re.search(r'^(.+)-(\d+)-g([0-9a-f]+)$', git_describe) if not mo: # unparseable. Maybe git-describe is misbehaving? pieces["error"] = ("unable to parse git-describe output: '%%s'" %% describe_out) return pieces # tag full_tag = mo.group(1) if not full_tag.startswith(tag_prefix): if verbose: fmt = "tag '%%s' doesn't start with prefix '%%s'" print(fmt %% (full_tag, tag_prefix)) pieces["error"] = ("tag '%%s' doesn't start with prefix '%%s'" %% (full_tag, tag_prefix)) return pieces pieces["closest-tag"] = full_tag[len(tag_prefix):] # distance: number of commits since tag pieces["distance"] = int(mo.group(2)) # commit: short hex revision ID pieces["short"] = mo.group(3) else: # HEX: no tags pieces["closest-tag"] = None count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], cwd=root) pieces["distance"] = int(count_out) # total number of commits # commit date: see ISO-8601 comment in git_versions_from_keywords() date = run_command(GITS, ["show", "-s", "--format=%%ci", "HEAD"], cwd=root)[0].strip() # Use only the last line. Previous lines may contain GPG signature # information. date = date.splitlines()[-1] pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) return pieces def plus_or_dot(pieces): """Return a + if we don't already have one, else return a .""" if "+" in pieces.get("closest-tag", ""): return "." return "+" def render_pep440(pieces): """Build up version string, with post-release "local version identifier". Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty Exceptions: 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"] or pieces["dirty"]: rendered += plus_or_dot(pieces) rendered += "%%d.g%%s" %% (pieces["distance"], pieces["short"]) if pieces["dirty"]: rendered += ".dirty" else: # exception #1 rendered = "0+untagged.%%d.g%%s" %% (pieces["distance"], pieces["short"]) if pieces["dirty"]: rendered += ".dirty" return rendered def render_pep440_pre(pieces): """TAG[.post0.devDISTANCE] -- No -dirty. Exceptions: 1: no tags. 0.post0.devDISTANCE """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"]: rendered += ".post0.dev%%d" %% pieces["distance"] else: # exception #1 rendered = "0.post0.dev%%d" %% pieces["distance"] return rendered def render_pep440_post(pieces): """TAG[.postDISTANCE[.dev0]+gHEX] . The ".dev0" means dirty. Note that .dev0 sorts backwards (a dirty tree will appear "older" than the corresponding clean one), but you shouldn't be releasing software with -dirty anyways. Exceptions: 1: no tags. 0.postDISTANCE[.dev0] """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"] or pieces["dirty"]: rendered += ".post%%d" %% pieces["distance"] if pieces["dirty"]: rendered += ".dev0" rendered += plus_or_dot(pieces) rendered += "g%%s" %% pieces["short"] else: # exception #1 rendered = "0.post%%d" %% pieces["distance"] if pieces["dirty"]: rendered += ".dev0" rendered += "+g%%s" %% pieces["short"] return rendered def render_pep440_old(pieces): """TAG[.postDISTANCE[.dev0]] . The ".dev0" means dirty. Exceptions: 1: no tags. 0.postDISTANCE[.dev0] """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"] or pieces["dirty"]: rendered += ".post%%d" %% pieces["distance"] if pieces["dirty"]: rendered += ".dev0" else: # exception #1 rendered = "0.post%%d" %% pieces["distance"] if pieces["dirty"]: rendered += ".dev0" return rendered def render_git_describe(pieces): """TAG[-DISTANCE-gHEX][-dirty]. Like 'git describe --tags --dirty --always'. Exceptions: 1: no tags. HEX[-dirty] (note: no 'g' prefix) """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"]: rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) else: # exception #1 rendered = pieces["short"] if pieces["dirty"]: rendered += "-dirty" return rendered def render_git_describe_long(pieces): """TAG-DISTANCE-gHEX[-dirty]. Like 'git describe --tags --dirty --always -long'. The distance/hash is unconditional. Exceptions: 1: no tags. HEX[-dirty] (note: no 'g' prefix) """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] rendered += "-%%d-g%%s" %% (pieces["distance"], pieces["short"]) else: # exception #1 rendered = pieces["short"] if pieces["dirty"]: rendered += "-dirty" return rendered def render(pieces, style): """Render the given version pieces into the requested style.""" if pieces["error"]: return {"version": "unknown", "full-revisionid": pieces.get("long"), "dirty": None, "error": pieces["error"], "date": None} if not style or style == "default": style = "pep440" # the default if style == "pep440": rendered = render_pep440(pieces) elif style == "pep440-pre": rendered = render_pep440_pre(pieces) elif style == "pep440-post": rendered = render_pep440_post(pieces) elif style == "pep440-old": rendered = render_pep440_old(pieces) elif style == "git-describe": rendered = render_git_describe(pieces) elif style == "git-describe-long": rendered = render_git_describe_long(pieces) else: raise ValueError("unknown style '%%s'" %% style) return {"version": rendered, "full-revisionid": pieces["long"], "dirty": pieces["dirty"], "error": None, "date": pieces.get("date")} def get_versions(): """Get version information or return default if unable to do so.""" # I am in _version.py, which lives at ROOT/VERSIONFILE_SOURCE. If we have # __file__, we can work backwards from there to the root. Some # py2exe/bbfreeze/non-CPython implementations don't do __file__, in which # case we can only use expanded keywords. cfg = get_config() verbose = cfg.verbose try: return git_versions_from_keywords(get_keywords(), cfg.tag_prefix, verbose) except NotThisMethod: pass try: root = os.path.realpath(__file__) # versionfile_source is the relative path from the top of the source # tree (where the .git directory might live) to this file. Invert # this to find the root from __file__. for i in cfg.versionfile_source.split('/'): root = os.path.dirname(root) except NameError: return {"version": "0+unknown", "full-revisionid": None, "dirty": None, "error": "unable to find root of source tree", "date": None} try: pieces = git_pieces_from_vcs(cfg.tag_prefix, root, verbose) return render(pieces, cfg.style) except NotThisMethod: pass try: if cfg.parentdir_prefix: return versions_from_parentdir(cfg.parentdir_prefix, root, verbose) except NotThisMethod: pass return {"version": "0+unknown", "full-revisionid": None, "dirty": None, "error": "unable to compute version", "date": None} ''' @register_vcs_handler("git", "get_keywords") def git_get_keywords(versionfile_abs): """Extract version information from the given file.""" # the code embedded in _version.py can just fetch the value of these # keywords. When used from setup.py, we don't want to import _version.py, # so we do it with a regexp instead. This function is not used from # _version.py. keywords = {} try: f = open(versionfile_abs, "r") for line in f.readlines(): if line.strip().startswith("git_refnames ="): mo = re.search(r'=\s*"(.*)"', line) if mo: keywords["refnames"] = mo.group(1) if line.strip().startswith("git_full ="): mo = re.search(r'=\s*"(.*)"', line) if mo: keywords["full"] = mo.group(1) if line.strip().startswith("git_date ="): mo = re.search(r'=\s*"(.*)"', line) if mo: keywords["date"] = mo.group(1) f.close() except EnvironmentError: pass return keywords @register_vcs_handler("git", "keywords") def git_versions_from_keywords(keywords, tag_prefix, verbose): """Get version information from git keywords.""" if not keywords: raise NotThisMethod("no keywords at all, weird") date = keywords.get("date") if date is not None: # Use only the last line. Previous lines may contain GPG signature # information. date = date.splitlines()[-1] # git-2.2.0 added "%cI", which expands to an ISO-8601 -compliant # datestamp. However we prefer "%ci" (which expands to an "ISO-8601 # -like" string, which we must then edit to make compliant), because # it's been around since git-1.5.3, and it's too difficult to # discover which version we're using, or to work around using an # older one. date = date.strip().replace(" ", "T", 1).replace(" ", "", 1) refnames = keywords["refnames"].strip() if refnames.startswith("$Format"): if verbose: print("keywords are unexpanded, not using") raise NotThisMethod("unexpanded keywords, not a git-archive tarball") refs = set([r.strip() for r in refnames.strip("()").split(",")]) # starting in git-1.8.3, tags are listed as "tag: foo-1.0" instead of # just "foo-1.0". If we see a "tag: " prefix, prefer those. TAG = "tag: " tags = set([r[len(TAG) :] for r in refs if r.startswith(TAG)]) if not tags: # Either we're using git < 1.8.3, or there really are no tags. We use # a heuristic: assume all version tags have a digit. The old git %d # expansion behaves like git log --decorate=short and strips out the # refs/heads/ and refs/tags/ prefixes that would let us distinguish # between branches and tags. By ignoring refnames without digits, we # filter out many common branch names like "release" and # "stabilization", as well as "HEAD" and "master". tags = set([r for r in refs if re.search(r"\d", r)]) if verbose: print("discarding '%s', no digits" % ",".join(refs - tags)) if verbose: print("likely tags: %s" % ",".join(sorted(tags))) for ref in sorted(tags): # sorting will prefer e.g. "2.0" over "2.0rc1" if ref.startswith(tag_prefix): r = ref[len(tag_prefix) :] if verbose: print("picking %s" % r) return { "version": r, "full-revisionid": keywords["full"].strip(), "dirty": False, "error": None, "date": date, } # no suitable tags, so version is "0+unknown", but full hex is still there if verbose: print("no suitable tags, using unknown + full revision id") return { "version": "0+unknown", "full-revisionid": keywords["full"].strip(), "dirty": False, "error": "no suitable tags", "date": None, } @register_vcs_handler("git", "pieces_from_vcs") def git_pieces_from_vcs(tag_prefix, root, verbose, run_command=run_command): """Get version from 'git describe' in the root of the source tree. This only gets called if the git-archive 'subst' keywords were *not* expanded, and _version.py hasn't already been rewritten with a short version string, meaning we're inside a checked out source tree. """ GITS = ["git"] if sys.platform == "win32": GITS = ["git.cmd", "git.exe"] out, rc = run_command(GITS, ["rev-parse", "--git-dir"], cwd=root, hide_stderr=True) if rc != 0: if verbose: print("Directory %s not under git control" % root) raise NotThisMethod("'git rev-parse --git-dir' returned error") # if there is a tag matching tag_prefix, this yields TAG-NUM-gHEX[-dirty] # if there isn't one, this yields HEX[-dirty] (no NUM) describe_out, rc = run_command( GITS, [ "describe", "--tags", "--dirty", "--always", "--long", "--match", "%s*" % tag_prefix, ], cwd=root, ) # --long was added in git-1.5.5 if describe_out is None: raise NotThisMethod("'git describe' failed") describe_out = describe_out.strip() full_out, rc = run_command(GITS, ["rev-parse", "HEAD"], cwd=root) if full_out is None: raise NotThisMethod("'git rev-parse' failed") full_out = full_out.strip() pieces = {} pieces["long"] = full_out pieces["short"] = full_out[:7] # maybe improved later pieces["error"] = None # parse describe_out. It will be like TAG-NUM-gHEX[-dirty] or HEX[-dirty] # TAG might have hyphens. git_describe = describe_out # look for -dirty suffix dirty = git_describe.endswith("-dirty") pieces["dirty"] = dirty if dirty: git_describe = git_describe[: git_describe.rindex("-dirty")] # now we have TAG-NUM-gHEX or HEX if "-" in git_describe: # TAG-NUM-gHEX mo = re.search(r"^(.+)-(\d+)-g([0-9a-f]+)$", git_describe) if not mo: # unparseable. Maybe git-describe is misbehaving? pieces["error"] = "unable to parse git-describe output: '%s'" % describe_out return pieces # tag full_tag = mo.group(1) if not full_tag.startswith(tag_prefix): if verbose: fmt = "tag '%s' doesn't start with prefix '%s'" print(fmt % (full_tag, tag_prefix)) pieces["error"] = "tag '%s' doesn't start with prefix '%s'" % ( full_tag, tag_prefix, ) return pieces pieces["closest-tag"] = full_tag[len(tag_prefix) :] # distance: number of commits since tag pieces["distance"] = int(mo.group(2)) # commit: short hex revision ID pieces["short"] = mo.group(3) else: # HEX: no tags pieces["closest-tag"] = None count_out, rc = run_command(GITS, ["rev-list", "HEAD", "--count"], cwd=root) pieces["distance"] = int(count_out) # total number of commits # commit date: see ISO-8601 comment in git_versions_from_keywords() date = run_command(GITS, ["show", "-s", "--format=%ci", "HEAD"], cwd=root)[ 0 ].strip() # Use only the last line. Previous lines may contain GPG signature # information. date = date.splitlines()[-1] pieces["date"] = date.strip().replace(" ", "T", 1).replace(" ", "", 1) return pieces def do_vcs_install(manifest_in, versionfile_source, ipy): """Git-specific installation logic for Versioneer. For Git, this means creating/changing .gitattributes to mark _version.py for export-subst keyword substitution. """ GITS = ["git"] if sys.platform == "win32": GITS = ["git.cmd", "git.exe"] files = [manifest_in, versionfile_source] if ipy: files.append(ipy) try: me = __file__ if me.endswith(".pyc") or me.endswith(".pyo"): me = os.path.splitext(me)[0] + ".py" versioneer_file = os.path.relpath(me) except NameError: versioneer_file = "versioneer.py" files.append(versioneer_file) present = False try: f = open(".gitattributes", "r") for line in f.readlines(): if line.strip().startswith(versionfile_source): if "export-subst" in line.strip().split()[1:]: present = True f.close() except EnvironmentError: pass if not present: f = open(".gitattributes", "a+") f.write("%s export-subst\n" % versionfile_source) f.close() files.append(".gitattributes") run_command(GITS, ["add", "--"] + files) def versions_from_parentdir(parentdir_prefix, root, verbose): """Try to determine the version from the parent directory name. Source tarballs conventionally unpack into a directory that includes both the project name and a version string. We will also support searching up two directory levels for an appropriately named parent directory """ rootdirs = [] for i in range(3): dirname = os.path.basename(root) if dirname.startswith(parentdir_prefix): return { "version": dirname[len(parentdir_prefix) :], "full-revisionid": None, "dirty": False, "error": None, "date": None, } else: rootdirs.append(root) root = os.path.dirname(root) # up a level if verbose: print( "Tried directories %s but none started with prefix %s" % (str(rootdirs), parentdir_prefix) ) raise NotThisMethod("rootdir doesn't start with parentdir_prefix") SHORT_VERSION_PY = """ # This file was generated by 'versioneer.py' (0.19) from # revision-control system data, or from the parent directory name of an # unpacked source archive. Distribution tarballs contain a pre-generated copy # of this file. import json version_json = ''' %s ''' # END VERSION_JSON def get_versions(): return json.loads(version_json) """ def versions_from_file(filename): """Try to determine the version from _version.py if present.""" try: with open(filename) as f: contents = f.read() except EnvironmentError: raise NotThisMethod("unable to read _version.py") mo = re.search( r"version_json = '''\n(.*)''' # END VERSION_JSON", contents, re.M | re.S ) if not mo: mo = re.search( r"version_json = '''\r\n(.*)''' # END VERSION_JSON", contents, re.M | re.S ) if not mo: raise NotThisMethod("no version_json in _version.py") return json.loads(mo.group(1)) def write_to_version_file(filename, versions): """Write the given version number to the given _version.py file.""" os.unlink(filename) contents = json.dumps(versions, sort_keys=True, indent=1, separators=(",", ": ")) with open(filename, "w") as f: f.write(SHORT_VERSION_PY % contents) print("set %s to '%s'" % (filename, versions["version"])) def plus_or_dot(pieces): """Return a + if we don't already have one, else return a .""" if "+" in pieces.get("closest-tag", ""): return "." return "+" def render_pep440(pieces): """Build up version string, with post-release "local version identifier". Our goal: TAG[+DISTANCE.gHEX[.dirty]] . Note that if you get a tagged build and then dirty it, you'll get TAG+0.gHEX.dirty Exceptions: 1: no tags. git_describe was just HEX. 0+untagged.DISTANCE.gHEX[.dirty] """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"] or pieces["dirty"]: rendered += plus_or_dot(pieces) rendered += "%d.g%s" % (pieces["distance"], pieces["short"]) if pieces["dirty"]: rendered += ".dirty" else: # exception #1 rendered = "0+untagged.%d.g%s" % (pieces["distance"], pieces["short"]) if pieces["dirty"]: rendered += ".dirty" return rendered def render_pep440_pre(pieces): """TAG[.post0.devDISTANCE] -- No -dirty. Exceptions: 1: no tags. 0.post0.devDISTANCE """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"]: rendered += ".post0.dev%d" % pieces["distance"] else: # exception #1 rendered = "0.post0.dev%d" % pieces["distance"] return rendered def render_pep440_post(pieces): """TAG[.postDISTANCE[.dev0]+gHEX] . The ".dev0" means dirty. Note that .dev0 sorts backwards (a dirty tree will appear "older" than the corresponding clean one), but you shouldn't be releasing software with -dirty anyways. Exceptions: 1: no tags. 0.postDISTANCE[.dev0] """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"] or pieces["dirty"]: rendered += ".post%d" % pieces["distance"] if pieces["dirty"]: rendered += ".dev0" rendered += plus_or_dot(pieces) rendered += "g%s" % pieces["short"] else: # exception #1 rendered = "0.post%d" % pieces["distance"] if pieces["dirty"]: rendered += ".dev0" rendered += "+g%s" % pieces["short"] return rendered def render_pep440_old(pieces): """TAG[.postDISTANCE[.dev0]] . The ".dev0" means dirty. Exceptions: 1: no tags. 0.postDISTANCE[.dev0] """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"] or pieces["dirty"]: rendered += ".post%d" % pieces["distance"] if pieces["dirty"]: rendered += ".dev0" else: # exception #1 rendered = "0.post%d" % pieces["distance"] if pieces["dirty"]: rendered += ".dev0" return rendered def render_git_describe(pieces): """TAG[-DISTANCE-gHEX][-dirty]. Like 'git describe --tags --dirty --always'. Exceptions: 1: no tags. HEX[-dirty] (note: no 'g' prefix) """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] if pieces["distance"]: rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) else: # exception #1 rendered = pieces["short"] if pieces["dirty"]: rendered += "-dirty" return rendered def render_git_describe_long(pieces): """TAG-DISTANCE-gHEX[-dirty]. Like 'git describe --tags --dirty --always -long'. The distance/hash is unconditional. Exceptions: 1: no tags. HEX[-dirty] (note: no 'g' prefix) """ if pieces["closest-tag"]: rendered = pieces["closest-tag"] rendered += "-%d-g%s" % (pieces["distance"], pieces["short"]) else: # exception #1 rendered = pieces["short"] if pieces["dirty"]: rendered += "-dirty" return rendered def render(pieces, style): """Render the given version pieces into the requested style.""" if pieces["error"]: return { "version": "unknown", "full-revisionid": pieces.get("long"), "dirty": None, "error": pieces["error"], "date": None, } if not style or style == "default": style = "pep440" # the default if style == "pep440": rendered = render_pep440(pieces) elif style == "pep440-pre": rendered = render_pep440_pre(pieces) elif style == "pep440-post": rendered = render_pep440_post(pieces) elif style == "pep440-old": rendered = render_pep440_old(pieces) elif style == "git-describe": rendered = render_git_describe(pieces) elif style == "git-describe-long": rendered = render_git_describe_long(pieces) else: raise ValueError("unknown style '%s'" % style) return { "version": rendered, "full-revisionid": pieces["long"], "dirty": pieces["dirty"], "error": None, "date": pieces.get("date"), } class VersioneerBadRootError(Exception): """The project root directory is unknown or missing key files.""" def get_versions(verbose=False): """Get the project version from whatever source is available. Returns dict with two keys: 'version' and 'full'. """ if "versioneer" in sys.modules: # see the discussion in cmdclass.py:get_cmdclass() del sys.modules["versioneer"] root = get_root() cfg = get_config_from_root(root) assert cfg.VCS is not None, "please set [versioneer]VCS= in setup.cfg" handlers = HANDLERS.get(cfg.VCS) assert handlers, "unrecognized VCS '%s'" % cfg.VCS verbose = verbose or cfg.verbose assert ( cfg.versionfile_source is not None ), "please set versioneer.versionfile_source" assert cfg.tag_prefix is not None, "please set versioneer.tag_prefix" versionfile_abs = os.path.join(root, cfg.versionfile_source) # extract version from first of: _version.py, VCS command (e.g. 'git # describe'), parentdir. This is meant to work for developers using a # source checkout, for users of a tarball created by 'setup.py sdist', # and for users of a tarball/zipball created by 'git archive' or github's # download-from-tag feature or the equivalent in other VCSes. get_keywords_f = handlers.get("get_keywords") from_keywords_f = handlers.get("keywords") if get_keywords_f and from_keywords_f: try: keywords = get_keywords_f(versionfile_abs) ver = from_keywords_f(keywords, cfg.tag_prefix, verbose) if verbose: print("got version from expanded keyword %s" % ver) return ver except NotThisMethod: pass try: ver = versions_from_file(versionfile_abs) if verbose: print("got version from file %s %s" % (versionfile_abs, ver)) return ver except NotThisMethod: pass from_vcs_f = handlers.get("pieces_from_vcs") if from_vcs_f: try: pieces = from_vcs_f(cfg.tag_prefix, root, verbose) ver = render(pieces, cfg.style) if verbose: print("got version from VCS %s" % ver) return ver except NotThisMethod: pass try: if cfg.parentdir_prefix: ver = versions_from_parentdir(cfg.parentdir_prefix, root, verbose) if verbose: print("got version from parentdir %s" % ver) return ver except NotThisMethod: pass if verbose: print("unable to compute version") return { "version": "0+unknown", "full-revisionid": None, "dirty": None, "error": "unable to compute version", "date": None, } def get_version(): """Get the short version string for this project.""" return get_versions()["version"] def get_cmdclass(cmdclass=None): """Get the custom setuptools/distutils subclasses used by Versioneer. If the package uses a different cmdclass (e.g. one from numpy), it should be provide as an argument. """ if "versioneer" in sys.modules: del sys.modules["versioneer"] # this fixes the "python setup.py develop" case (also 'install' and # 'easy_install .'), in which subdependencies of the main project are # built (using setup.py bdist_egg) in the same python process. Assume # a main project A and a dependency B, which use different versions # of Versioneer. A's setup.py imports A's Versioneer, leaving it in # sys.modules by the time B's setup.py is executed, causing B to run # with the wrong versioneer. Setuptools wraps the sub-dep builds in a # sandbox that restores sys.modules to it's pre-build state, so the # parent is protected against the child's "import versioneer". By # removing ourselves from sys.modules here, before the child build # happens, we protect the child from the parent's versioneer too. # Also see https://github.com/python-versioneer/python-versioneer/issues/52 cmds = {} if cmdclass is None else cmdclass.copy() # we add "version" to both distutils and setuptools from distutils.core import Command class cmd_version(Command): description = "report generated version string" user_options = [] boolean_options = [] def initialize_options(self): pass def finalize_options(self): pass def run(self): vers = get_versions(verbose=True) print("Version: %s" % vers["version"]) print(" full-revisionid: %s" % vers.get("full-revisionid")) print(" dirty: %s" % vers.get("dirty")) print(" date: %s" % vers.get("date")) if vers["error"]: print(" error: %s" % vers["error"]) cmds["version"] = cmd_version # we override "build_py" in both distutils and setuptools # # most invocation pathways end up running build_py: # distutils/build -> build_py # distutils/install -> distutils/build ->.. # setuptools/bdist_wheel -> distutils/install ->.. # setuptools/bdist_egg -> distutils/install_lib -> build_py # setuptools/install -> bdist_egg ->.. # setuptools/develop -> ? # pip install: # copies source tree to a tempdir before running egg_info/etc # if .git isn't copied too, 'git describe' will fail # then does setup.py bdist_wheel, or sometimes setup.py install # setup.py egg_info -> ? # we override different "build_py" commands for both environments if "build_py" in cmds: _build_py = cmds["build_py"] elif "setuptools" in sys.modules: from setuptools.command.build_py import build_py as _build_py else: from distutils.command.build_py import build_py as _build_py class cmd_build_py(_build_py): def run(self): root = get_root() cfg = get_config_from_root(root) versions = get_versions() _build_py.run(self) # now locate _version.py in the new build/ directory and replace # it with an updated value if cfg.versionfile_build: target_versionfile = os.path.join(self.build_lib, cfg.versionfile_build) print("UPDATING %s" % target_versionfile) write_to_version_file(target_versionfile, versions) cmds["build_py"] = cmd_build_py if "setuptools" in sys.modules: from setuptools.command.build_ext import build_ext as _build_ext else: from distutils.command.build_ext import build_ext as _build_ext class cmd_build_ext(_build_ext): def run(self): root = get_root() cfg = get_config_from_root(root) versions = get_versions() _build_ext.run(self) if self.inplace: # build_ext --inplace will only build extensions in # build/lib<..> dir with no _version.py to write to. # As in place builds will already have a _version.py # in the module dir, we do not need to write one. return # now locate _version.py in the new build/ directory and replace # it with an updated value target_versionfile = os.path.join(self.build_lib, cfg.versionfile_source) print("UPDATING %s" % target_versionfile) write_to_version_file(target_versionfile, versions) cmds["build_ext"] = cmd_build_ext if "cx_Freeze" in sys.modules: # cx_freeze enabled? from cx_Freeze.dist import build_exe as _build_exe # nczeczulin reports that py2exe won't like the pep440-style string # as FILEVERSION, but it can be used for PRODUCTVERSION, e.g. # setup(console=[{ # "version": versioneer.get_version().split("+", 1)[0], # FILEVERSION # "product_version": versioneer.get_version(), # ... class cmd_build_exe(_build_exe): def run(self): root = get_root() cfg = get_config_from_root(root) versions = get_versions() target_versionfile = cfg.versionfile_source print("UPDATING %s" % target_versionfile) write_to_version_file(target_versionfile, versions) _build_exe.run(self) os.unlink(target_versionfile) with open(cfg.versionfile_source, "w") as f: LONG = LONG_VERSION_PY[cfg.VCS] f.write( LONG % { "DOLLAR": "$", "STYLE": cfg.style, "TAG_PREFIX": cfg.tag_prefix, "PARENTDIR_PREFIX": cfg.parentdir_prefix, "VERSIONFILE_SOURCE": cfg.versionfile_source, } ) cmds["build_exe"] = cmd_build_exe del cmds["build_py"] if "py2exe" in sys.modules: # py2exe enabled? from py2exe.distutils_buildexe import py2exe as _py2exe class cmd_py2exe(_py2exe): def run(self): root = get_root() cfg = get_config_from_root(root) versions = get_versions() target_versionfile = cfg.versionfile_source print("UPDATING %s" % target_versionfile) write_to_version_file(target_versionfile, versions) _py2exe.run(self) os.unlink(target_versionfile) with open(cfg.versionfile_source, "w") as f: LONG = LONG_VERSION_PY[cfg.VCS] f.write( LONG % { "DOLLAR": "$", "STYLE": cfg.style, "TAG_PREFIX": cfg.tag_prefix, "PARENTDIR_PREFIX": cfg.parentdir_prefix, "VERSIONFILE_SOURCE": cfg.versionfile_source, } ) cmds["py2exe"] = cmd_py2exe # we override different "sdist" commands for both environments if "sdist" in cmds: _sdist = cmds["sdist"] elif "setuptools" in sys.modules: from setuptools.command.sdist import sdist as _sdist else: from distutils.command.sdist import sdist as _sdist class cmd_sdist(_sdist): def run(self): versions = get_versions() self._versioneer_generated_versions = versions # unless we update this, the command will keep using the old # version self.distribution.metadata.version = versions["version"] return _sdist.run(self) def make_release_tree(self, base_dir, files): root = get_root() cfg = get_config_from_root(root) _sdist.make_release_tree(self, base_dir, files) # now locate _version.py in the new base_dir directory # (remembering that it may be a hardlink) and replace it with an # updated value target_versionfile = os.path.join(base_dir, cfg.versionfile_source) print("UPDATING %s" % target_versionfile) write_to_version_file( target_versionfile, self._versioneer_generated_versions ) cmds["sdist"] = cmd_sdist return cmds CONFIG_ERROR = """ setup.cfg is missing the necessary Versioneer configuration. You need a section like: [versioneer] VCS = git style = pep440 versionfile_source = src/myproject/_version.py versionfile_build = myproject/_version.py tag_prefix = parentdir_prefix = myproject- You will also need to edit your setup.py to use the results: import versioneer setup(version=versioneer.get_version(), cmdclass=versioneer.get_cmdclass(), ...) Please read the docstring in ./versioneer.py for configuration instructions, edit setup.cfg, and re-run the installer or 'python versioneer.py setup'. """ SAMPLE_CONFIG = """ # See the docstring in versioneer.py for instructions. Note that you must # re-run 'versioneer.py setup' after changing this section, and commit the # resulting files. [versioneer] #VCS = git #style = pep440 #versionfile_source = #versionfile_build = #tag_prefix = #parentdir_prefix = """ INIT_PY_SNIPPET = """ from ._version import get_versions __version__ = get_versions()['version'] del get_versions """ def do_setup(): """Do main VCS-independent setup function for installing Versioneer.""" root = get_root() try: cfg = get_config_from_root(root) except ( EnvironmentError, configparser.NoSectionError, configparser.NoOptionError, ) as e: if isinstance(e, (EnvironmentError, configparser.NoSectionError)): print("Adding sample versioneer config to setup.cfg", file=sys.stderr) with open(os.path.join(root, "setup.cfg"), "a") as f: f.write(SAMPLE_CONFIG) print(CONFIG_ERROR, file=sys.stderr) return 1 print(" creating %s" % cfg.versionfile_source) with open(cfg.versionfile_source, "w") as f: LONG = LONG_VERSION_PY[cfg.VCS] f.write( LONG % { "DOLLAR": "$", "STYLE": cfg.style, "TAG_PREFIX": cfg.tag_prefix, "PARENTDIR_PREFIX": cfg.parentdir_prefix, "VERSIONFILE_SOURCE": cfg.versionfile_source, } ) ipy = os.path.join(os.path.dirname(cfg.versionfile_source), "__init__.py") if os.path.exists(ipy): try: with open(ipy, "r") as f: old = f.read() except EnvironmentError: old = "" if INIT_PY_SNIPPET not in old: print(" appending to %s" % ipy) with open(ipy, "a") as f: f.write(INIT_PY_SNIPPET) else: print(" %s unmodified" % ipy) else: print(" %s doesn't exist, ok" % ipy) ipy = None # Make sure both the top-level "versioneer.py" and versionfile_source # (PKG/_version.py, used by runtime code) are in MANIFEST.in, so # they'll be copied into source distributions. Pip won't be able to # install the package without this. manifest_in = os.path.join(root, "MANIFEST.in") simple_includes = set() try: with open(manifest_in, "r") as f: for line in f: if line.startswith("include "): for include in line.split()[1:]: simple_includes.add(include) except EnvironmentError: pass # That doesn't cover everything MANIFEST.in can do # (http://docs.python.org/2/distutils/sourcedist.html#commands), so # it might give some false negatives. Appending redundant 'include' # lines is safe, though. if "versioneer.py" not in simple_includes: print(" appending 'versioneer.py' to MANIFEST.in") with open(manifest_in, "a") as f: f.write("include versioneer.py\n") else: print(" 'versioneer.py' already in MANIFEST.in") if cfg.versionfile_source not in simple_includes: print( " appending versionfile_source ('%s') to MANIFEST.in" % cfg.versionfile_source ) with open(manifest_in, "a") as f: f.write("include %s\n" % cfg.versionfile_source) else: print(" versionfile_source already in MANIFEST.in") # Make VCS-specific changes. For git, this means creating/changing # .gitattributes to mark _version.py for export-subst keyword # substitution. do_vcs_install(manifest_in, cfg.versionfile_source, ipy) return 0 def scan_setup_py(): """Validate the contents of setup.py against Versioneer's expectations.""" found = set() setters = False errors = 0 with open("setup.py", "r") as f: for line in f.readlines(): if "import versioneer" in line: found.add("import") if "versioneer.get_cmdclass()" in line: found.add("cmdclass") if "versioneer.get_version()" in line: found.add("get_version") if "versioneer.VCS" in line: setters = True if "versioneer.versionfile_source" in line: setters = True if len(found) != 3: print("") print("Your setup.py appears to be missing some important items") print("(but I might be wrong). Please make sure it has something") print("roughly like the following:") print("") print(" import versioneer") print(" setup( version=versioneer.get_version(),") print(" cmdclass=versioneer.get_cmdclass(), ...)") print("") errors += 1 if setters: print("You should remove lines like 'versioneer.VCS = ' and") print("'versioneer.versionfile_source = ' . This configuration") print("now lives in setup.cfg, and should be removed from setup.py") print("") errors += 1 return errors if __name__ == "__main__": cmd = sys.argv[1] if cmd == "setup": errors = do_setup() errors += scan_setup_py() if errors: sys.exit(1)