From 1ccb61fe18ddc1fbb498ee90f182c44066aefb36 Mon Sep 17 00:00:00 2001 From: Dmitry Shachnev Date: Thu, 12 Oct 2017 13:08:24 +0300 Subject: [PATCH 01/96] Make dbapi_extension.py compatible with Sphinx 1.6 In Sphinx commit 1a821b89e9952fc2, the deprecated make_admonition() function was removed. This commit updates the code to use the modern API instead. --- doc/src/tools/lib/dbapi_extension.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/doc/src/tools/lib/dbapi_extension.py b/doc/src/tools/lib/dbapi_extension.py index cb3fec47..7ab656fa 100755 --- a/doc/src/tools/lib/dbapi_extension.py +++ b/doc/src/tools/lib/dbapi_extension.py @@ -12,7 +12,7 @@ from docutils import nodes from sphinx.locale import _ -from sphinx.util.compat import Directive, make_admonition +from docutils.parsers.rst import Directive class extension_node(nodes.Admonition, nodes.Element): pass @@ -29,12 +29,11 @@ class Extension(Directive): option_spec = {} def run(self): - nodes = make_admonition(extension_node, - self.name, [_('DB API extension')], self.options, - self.content, self.lineno, self.content_offset, - self.block_text, self.state, self.state_machine) - nodes[0]['classes'].append('dbapi-extension') - return nodes + node = extension_node('\n'.join(self.content)) + node += nodes.title(_('DB API extension'), _('DB API extension')) + self.state.nested_parse(self.content, self.content_offset, node) + node['classes'].append('dbapi-extension') + return [node] def visit_extension_node(self, node): From 4d68f6e4140f72957beed4c7534a56e8c13adac0 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 18 Oct 2017 21:56:48 +0100 Subject: [PATCH 02/96] Sphinx version do build docs updated to 1.6 --- doc/requirements.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/requirements.txt b/doc/requirements.txt index b5323e79..b5ce608e 100644 --- a/doc/requirements.txt +++ b/doc/requirements.txt @@ -1,3 +1,3 @@ # Packages only needed to build the docs -Pygments>=1.5 -Sphinx>=1.2,<=1.3 +Pygments>=2.2,<2.3 +Sphinx>=1.6,<=1.7 From e7587caee3dffffe001df7a749d5bc1f6abf9eff Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 18 Oct 2017 21:57:17 +0100 Subject: [PATCH 03/96] Dropped doc building warning doctests have sure completely rotten however. --- doc/src/connection.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/src/connection.rst b/doc/src/connection.rst index 436d83f7..454f80d7 100644 --- a/doc/src/connection.rst +++ b/doc/src/connection.rst @@ -551,7 +551,7 @@ The ``connection`` class the session. .. doctest:: - :options: NORMALIZE_WHITESPACE + :options: +NORMALIZE_WHITESPACE >>> cur.execute("CREATE TABLE foo (id serial PRIMARY KEY);") >>> pprint(conn.notices) From 6ee8b4c9aad60a43f288b8e4673b22b296f5d8ac Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 18 Oct 2017 22:25:42 +0100 Subject: [PATCH 04/96] Bumping version to test libpq 10 wheel package --- NEWS | 6 ++++++ setup.py | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index d23ba7ce..e4816f06 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,12 @@ Current release --------------- +What's new in psycopg 2.7.3.2 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Wheel package compiled against PostgreSQL 10.0 libpq and OpenSSL 1.0.2l + + What's new in psycopg 2.7.3.1 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/setup.py b/setup.py index f4587682..962a4a42 100644 --- a/setup.py +++ b/setup.py @@ -64,7 +64,7 @@ except ImportError: # Take a look at http://www.python.org/dev/peps/pep-0440/ # for a consistent versioning pattern. -PSYCOPG_VERSION = '2.7.3.1' +PSYCOPG_VERSION = '2.7.3.2.dev0' # note: if you are changing the list of supported Python version please fix From f1461d2d7ed1beff28c37d24b02d4e35c81b2d83 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 19 Oct 2017 02:16:27 +0100 Subject: [PATCH 05/96] Parse PG 10 error codes from final version --- doc/src/errorcodes.rst | 2 +- scripts/make_errorcodes.py | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/doc/src/errorcodes.rst b/doc/src/errorcodes.rst index ed40b1f1..f5f2fa8a 100644 --- a/doc/src/errorcodes.rst +++ b/doc/src/errorcodes.rst @@ -50,7 +50,7 @@ An example of the available constants defined in the module: '42P01' Constants representing all the error values defined by PostgreSQL versions -between 8.1 and 10 beta 1 are included in the module. +between 8.1 and 10 are included in the module. .. autofunction:: lookup(code) diff --git a/scripts/make_errorcodes.py b/scripts/make_errorcodes.py index 73aeed9b..1769ba5a 100755 --- a/scripts/make_errorcodes.py +++ b/scripts/make_errorcodes.py @@ -32,10 +32,10 @@ def main(): filename = sys.argv[1] file_start = read_base_file(filename) - # If you add a version to the list fix the docs (errorcodes.rst, err.rst) + # If you add a version to the list fix the docs (in errorcodes.rst) classes, errors = fetch_errors( ['8.1', '8.2', '8.3', '8.4', '9.0', '9.1', '9.2', '9.3', '9.4', '9.5', - '9.6', '10 b1']) + '9.6', '10']) f = open(filename, "w") for line in file_start: @@ -146,13 +146,18 @@ def fetch_errors(versions): if tver < (9, 1): c1, e1 = parse_errors_sgml(errors_sgml_url % version) else: - # TODO: move to 10 stable when released. - if version == '10 b1': - tag = 'REL_10_BETA1' - else: - tag = 'REL%s_STABLE' % version.replace('.', '_') + tag = '%s%s_STABLE' % ( + (tver[0] >= 10 and 'REL_' or 'REL'), + version.replace('.', '_')) c1, e1 = parse_errors_txt(errors_txt_url % tag) classes.update(c1) + + # TODO: this error was added in PG 10 beta 1 but dropped in the + # final release. It doesn't harm leaving it in the file. Check if it + # will be added back in PG 11. + # https://github.com/postgres/postgres/commit/28e0727076 + errors['55']['55P04'] = 'UNSAFE_NEW_ENUM_VALUE_USAGE' + for c, cerrs in e1.iteritems(): errors[c].update(cerrs) From 0d90c173fb94cc88f7523cd3c8113da43191e2b3 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 18 Oct 2017 23:02:08 +0100 Subject: [PATCH 06/96] Test against PG 10.0 on Travis --- scripts/travis_prepare.sh | 5 +++-- scripts/travis_test.sh | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/scripts/travis_prepare.sh b/scripts/travis_prepare.sh index 3743eac3..73cbb841 100755 --- a/scripts/travis_prepare.sh +++ b/scripts/travis_prepare.sh @@ -105,11 +105,11 @@ cd / # Postgres versions supported by Travis CI if [[ -z "$DONT_TEST_PRESENT" ]]; then + create 10 create 9.6 create 9.5 create 9.4 create 9.3 - create 9.2 fi # Unsupported postgres versions that we still support @@ -123,9 +123,10 @@ if [[ -n "$TEST_PAST" ]]; then create 8.4 create 9.0 create 9.1 + create 9.2 fi # Postgres built from master if [[ -n "$TEST_FUTURE" ]]; then - create 10 10beta1 + create 11 11-master fi diff --git a/scripts/travis_test.sh b/scripts/travis_test.sh index 4a4630b0..033482b8 100755 --- a/scripts/travis_test.sh +++ b/scripts/travis_test.sh @@ -46,11 +46,11 @@ run_test () { # Postgres versions supported by Travis CI if [[ -z "$DONT_TEST_PRESENT" ]]; then + run_test 10 run_test 9.6 run_test 9.5 run_test 9.4 run_test 9.3 - run_test 9.2 fi # Unsupported postgres versions that we still support @@ -64,9 +64,10 @@ if [[ -n "$TEST_PAST" ]]; then run_test 8.4 run_test 9.0 run_test 9.1 + run_test 9.2 fi # Postgres built from master if [[ -n "$TEST_FUTURE" ]]; then - run_test 10 + run_test 11 fi From 552e49a11dafaff4e50cdc816023f4d929e52187 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 18 Oct 2017 22:37:14 +0100 Subject: [PATCH 07/96] Test building PG 10 using the solution provided by Jason Erickson https://github.com/psycopg/psycopg2-wheels/pull/3/ --- .appveyor.yml | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 26109cef..b41e7ef5 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -73,6 +73,9 @@ matrix: fast_finish: false services: + # Note: if you change this service also change the path to match in: + # - the postgresql.conf customisation in 'init' + # - the path in 'build_script' - postgresql96 cache: @@ -187,30 +190,33 @@ install: - IF NOT EXIST %PGTOP%\lib MKDIR %PGTOP%\lib # Download PostgreSQL source - CD C:\Others - - IF NOT EXIST postgres-REL9_6_3.zip ( - curl -fsSL -o postgres-REL9_6_3.zip https://github.com/postgres/postgres/archive/REL9_6_3.zip + - IF NOT EXIST postgres-REL_10_0.zip ( + curl -fsSL -o postgres-REL_10_0.zip https://github.com/postgres/postgres/archive/REL_10_0.zip ) # Setup build config file (config.pl) - # Build libpgport first - # Build libpq + # Hack the Mkvcbuild.pm file so we build the lib version of libpq + # Build libpgport, libpgcommon, libpq + # Copy over includes + # Copy over built libraries # NOTE: Cannot set and use the same variable inside an IF - - SET PGBUILD=%BUILD_DIR%\postgres-REL9_6_3 + - SET PGBUILD=%BUILD_DIR%\postgres-REL_10_0 - IF NOT EXIST %PGTOP%\lib\libpq.lib ( CD %BUILD_DIR% && - 7z x C:\Others\postgres-REL9_6_3.zip && - CD postgres-REL9_6_3\src\tools\msvc && + 7z x C:\Others\postgres-REL_10_0.zip && + CD postgres-REL_10_0\src\tools\msvc && ECHO $config-^>{ldap} = 0; > config.pl && ECHO $config-^>{openssl} = "%OPENSSLTOP:\=\\%"; >> config.pl && ECHO.>> config.pl && ECHO 1;>> config.pl && + perl -pi.bak -e "s/'libpq', 'dll'/'libpq', 'lib'/g" Mkvcbuild.pm && build libpgport && + build libpgcommon && + build libpq && XCOPY /E ..\..\include %PGTOP%\include && COPY %PGBUILD%\Release\libpgport\libpgport.lib %PGTOP%\lib && - CD ..\..\interfaces\libpq && - nmake -f win32.mak USE_OPENSSL=1 ENABLE_THREAD_SAFETY=1 SSL_INC=%OPENSSLTOP%\include SSL_LIB_PATH=%OPENSSLTOP%\lib config .\Release\libpq.lib && - COPY *.h %PGTOP%\include && - COPY Release\libpq.lib %PGTOP%\lib && + COPY %PGBUILD%\Release\libpgcommon\libpgcommon.lib %PGTOP%\lib && + COPY %PGBUILD%\Release\libpq\libpq.lib %PGTOP%\lib && CD %BASE_DIR% && RMDIR /S /Q %PGBUILD% ) @@ -223,7 +229,7 @@ build_script: # Add PostgreSQL binaries to the path - PATH=C:\Program Files\PostgreSQL\9.6\bin\;%PATH% - CD C:\Project - - "%PYTHON%\\python.exe setup.py build_ext --have-ssl -l libpgcommon -L %OPENSSLTOP%\\lib;%PGTOP%\\lib -I %OPENSSLTOP%\\include;%PGTOP%\\include" + - "%PYTHON%\\python.exe setup.py build_ext --have-ssl -l libpgcommon -l libpgport -L %OPENSSLTOP%\\lib;%PGTOP%\\lib -I %OPENSSLTOP%\\include;%PGTOP%\\include" - "%PYTHON%\\python.exe setup.py build" - "%PYTHON%\\python.exe setup.py install" From f3f394ced900a031d93e4ff1339c5dc5ba522459 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 24 Oct 2017 11:03:14 +0100 Subject: [PATCH 08/96] Import recipe to build PG 10 libpq on win from wheel repos Also build pg_config so the libpq version is correct at compile time too. --- .appveyor.yml | 30 ++++++++++++++++++++++++------ scripts/appveyor.cache_rebuild | 9 ++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index b41e7ef5..4f9a40c5 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -73,13 +73,13 @@ matrix: fast_finish: false services: - # Note: if you change this service also change the path to match in: - # - the postgresql.conf customisation in 'init' - # - the path in 'build_script' + # Note: if you change this service also change the paths to match + # (see where Program Files\Postgres\9.6 is used) - postgresql96 cache: # Rebuild cache if following file changes + # (See the file to zap the cache manually) - C:\Others -> scripts\appveyor.cache_rebuild # Script called before repo cloning @@ -116,6 +116,11 @@ init: - IF "%PYTHON_ARCH%"=="32" (CALL "C:\\Program Files (x86)\\Microsoft Visual Studio %VS_VER%\\VC\\vcvarsall.bat" x86) - IF "%PYTHON_ARCH%"=="64" (CALL "C:\\Program Files (x86)\\Microsoft Visual Studio %VS_VER%\\VC\\vcvarsall.bat" amd64) + # The program rc.exe on 64bit with some versions look in the wrong path + # location when building postgresql. This cheats by copying the x64 bit + # files to that location. + - IF "%PYTHON_ARCH%"=="64" (COPY /Y "C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0\\Bin\\x64\\rc*" "C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v7.0A\\Bin") + # Change PostgreSQL config before service starts to allow > 1 prepared # transactions for test cases - ECHO max_prepared_transactions = 10 >> "C:\\Program Files\\PostgreSQL\\9.6\\data\\postgresql.conf" @@ -188,6 +193,8 @@ install: - SET PGTOP=%BASE_DIR%\postgresql - IF NOT EXIST %PGTOP%\include MKDIR %PGTOP%\include - IF NOT EXIST %PGTOP%\lib MKDIR %PGTOP%\lib + - IF NOT EXIST %PGTOP%\bin MKDIR %PGTOP%\bin + # Download PostgreSQL source - CD C:\Others - IF NOT EXIST postgres-REL_10_0.zip ( @@ -197,8 +204,10 @@ install: # Setup build config file (config.pl) # Hack the Mkvcbuild.pm file so we build the lib version of libpq # Build libpgport, libpgcommon, libpq - # Copy over includes + # Install includes # Copy over built libraries + # Prepare local include directory for building from + # Build pg_config in place # NOTE: Cannot set and use the same variable inside an IF - SET PGBUILD=%BUILD_DIR%\postgres-REL_10_0 - IF NOT EXIST %PGTOP%\lib\libpq.lib ( @@ -213,10 +222,16 @@ install: build libpgport && build libpgcommon && build libpq && - XCOPY /E ..\..\include %PGTOP%\include && + ECHO "" > %PGBUILD%\src\backend\parser\gram.h && + perl -pi.bak -e "s/qw\(Install\)/qw\(Install CopyIncludeFiles\)/g" Install.pm && + perl -MInstall=CopyIncludeFiles -e"chdir('../../..'); CopyIncludeFiles('%PGTOP%')" && COPY %PGBUILD%\Release\libpgport\libpgport.lib %PGTOP%\lib && COPY %PGBUILD%\Release\libpgcommon\libpgcommon.lib %PGTOP%\lib && COPY %PGBUILD%\Release\libpq\libpq.lib %PGTOP%\lib && + XCOPY /Y /S %PGBUILD%\src\include\port\win32\* %PGBUILD%\src\include && + XCOPY /Y /S %PGBUILD%\src\include\port\win32_msvc\* %PGBUILD%\src\include && + CD %PGBUILD%\src\bin\pg_config && + cl pg_config.c /MT /nologo /I%PGBUILD%\src\include /link /LIBPATH:%PGTOP%\lib libpgcommon.lib libpgport.lib advapi32.lib /NODEFAULTLIB:libcmt.lib /OUT:%PGTOP%\bin\pg_config.exe && CD %BASE_DIR% && RMDIR /S /Q %PGBUILD% ) @@ -229,7 +244,7 @@ build_script: # Add PostgreSQL binaries to the path - PATH=C:\Program Files\PostgreSQL\9.6\bin\;%PATH% - CD C:\Project - - "%PYTHON%\\python.exe setup.py build_ext --have-ssl -l libpgcommon -l libpgport -L %OPENSSLTOP%\\lib;%PGTOP%\\lib -I %OPENSSLTOP%\\include;%PGTOP%\\include" + - "%PYTHON%\\python.exe setup.py build_ext --have-ssl --pg-config %PGTOP%\\bin\\pg_config.exe -l libpgcommon -l libpgport -L %OPENSSLTOP%\\lib -I %OPENSSLTOP%\\include" - "%PYTHON%\\python.exe setup.py build" - "%PYTHON%\\python.exe setup.py install" @@ -241,5 +256,8 @@ before_test: - psql -d %PSYCOPG2_TESTDB% -c "CREATE EXTENSION HSTORE;" test_script: + # Print psycopg and libpq versions - "%PYTHON%\\python.exe -c \"import psycopg2; print(psycopg2.__version__)\"" + - "%PYTHON%\\python.exe -c \"import psycopg2; print(psycopg2.__libpq_version__)\"" + - "%PYTHON%\\python.exe -c \"import psycopg2; print(psycopg2.extensions.libpq_version())\"" - "%PYTHON%\\python.exe -c \"from psycopg2 import tests; tests.unittest.main(defaultTest='tests.test_suite')\" --verbose" diff --git a/scripts/appveyor.cache_rebuild b/scripts/appveyor.cache_rebuild index c9db5073..8ecd97fb 100644 --- a/scripts/appveyor.cache_rebuild +++ b/scripts/appveyor.cache_rebuild @@ -12,4 +12,11 @@ OpenSSL Version: 1.0.2l PostgreSQL - Version: 9.6.3 + Version: 10.0 + + +NOTE: to zap the cache manually you can also use: + + curl -X DELETE -H "Authorization: Bearer $APPVEYOR_TOKEN" -H "Content-Type: application/json" https://ci.appveyor.com/api/projects/psycopg/psycopg2/buildcache + +with the token from https://ci.appveyor.com/api-token From 980145eaee13f5014e929816058b83db78cb0833 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 24 Oct 2017 11:39:42 +0100 Subject: [PATCH 09/96] Preparing release 2.7.3.2 Close #601, close #602. --- NEWS | 1 + setup.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index e4816f06..799477ec 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,7 @@ What's new in psycopg 2.7.3.2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Wheel package compiled against PostgreSQL 10.0 libpq and OpenSSL 1.0.2l + (:tickets:`#601, #602`) What's new in psycopg 2.7.3.1 diff --git a/setup.py b/setup.py index 962a4a42..86f32d2b 100644 --- a/setup.py +++ b/setup.py @@ -64,7 +64,7 @@ except ImportError: # Take a look at http://www.python.org/dev/peps/pep-0440/ # for a consistent versioning pattern. -PSYCOPG_VERSION = '2.7.3.2.dev0' +PSYCOPG_VERSION = '2.7.3.2' # note: if you are changing the list of supported Python version please fix From f54783ae6e6de44dafcecefdc8963c58a2ed98b9 Mon Sep 17 00:00:00 2001 From: My Karlsson Date: Thu, 26 Oct 2017 20:11:51 +0200 Subject: [PATCH 10/96] Emulate timeradd and timersub on Solaris Solaris does not have timeradd and timersub. Add solaris_support.c which provides emulated versions of them on Solaris. --- psycopg/pqpath.c | 4 +++ psycopg/solaris_support.c | 54 +++++++++++++++++++++++++++++++++++++++ psycopg/solaris_support.h | 37 +++++++++++++++++++++++++++ setup.py | 2 +- 4 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 psycopg/solaris_support.c create mode 100644 psycopg/solaris_support.h diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index 7e8c8e8b..a4235d1d 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -52,6 +52,10 @@ #include "win32_support.h" #endif +#if defined(__sun) && defined(__SVR4) +#include "solaris_support.h" +#endif + extern HIDDEN PyObject *psyco_DescriptionType; extern HIDDEN const char *srv_isolevels[]; extern HIDDEN const char *srv_readonly[]; diff --git a/psycopg/solaris_support.c b/psycopg/solaris_support.c new file mode 100644 index 00000000..cf82e2ed --- /dev/null +++ b/psycopg/solaris_support.c @@ -0,0 +1,54 @@ +/* solaris_support.c - emulate functions missing on Solaris + * + * Copyright (C) 2017 My Karlsson + * + * This file is part of psycopg. + * + * psycopg2 is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * In addition, as a special exception, the copyright holders give + * permission to link this program with the OpenSSL library (or with + * modified versions of OpenSSL that use the same license as OpenSSL), + * and distribute linked combinations including the two. + * + * You must obey the GNU Lesser General Public License in all respects for + * all of the code used other than OpenSSL. + * + * psycopg2 is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + */ + +#define PSYCOPG_MODULE +#include "psycopg/psycopg.h" +#include "psycopg/solaris_support.h" + +#if defined(__sun) && defined(__SVR4) +/* timeradd is missing on Solaris */ +void +timeradd(struct timeval *a, struct timeval *b, struct timeval *c) +{ + c->tv_sec = a->tv_sec + b->tv_sec; + c->tv_usec = a->tv_usec + b->tv_usec; + if (c->tv_usec >= 1000000) { + c->tv_usec -= 1000000; + c->tv_sec += 1; + } +} + +/* timersub is missing on Solaris */ +void +timersub(struct timeval *a, struct timeval *b, struct timeval *c) +{ + c->tv_sec = a->tv_sec - b->tv_sec; + c->tv_usec = a->tv_usec - b->tv_usec; + if (c->tv_usec < 0) { + c->tv_usec += 1000000; + c->tv_sec -= 1; + } +} +#endif /* defined(__sun) && defined(__SVR4) */ diff --git a/psycopg/solaris_support.h b/psycopg/solaris_support.h new file mode 100644 index 00000000..33c2f2b8 --- /dev/null +++ b/psycopg/solaris_support.h @@ -0,0 +1,37 @@ +/* solaris_support.h - definitions for solaris_support.c + * + * Copyright (C) 2017 My Karlsson + * + * This file is part of psycopg. + * + * psycopg2 is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published + * by the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * In addition, as a special exception, the copyright holders give + * permission to link this program with the OpenSSL library (or with + * modified versions of OpenSSL that use the same license as OpenSSL), + * and distribute linked combinations including the two. + * + * You must obey the GNU Lesser General Public License in all respects for + * all of the code used other than OpenSSL. + * + * psycopg2 is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public + * License for more details. + */ +#ifndef PSYCOPG_SOLARIS_SUPPORT_H +#define PSYCOPG_SOLARIS_SUPPORT_H + +#include "psycopg/config.h" + +#if defined(__sun) && defined(__SVR4) +#include + +extern HIDDEN void timeradd(struct timeval *a, struct timeval *b, struct timeval *c); +extern HIDDEN void timersub(struct timeval *a, struct timeval *b, struct timeval *c); +#endif + +#endif /* !defined(PSYCOPG_SOLARIS_SUPPORT_H) */ diff --git a/setup.py b/setup.py index 67af9db0..f2f63c57 100644 --- a/setup.py +++ b/setup.py @@ -480,7 +480,7 @@ data_files = [] sources = [ 'psycopgmodule.c', 'green.c', 'pqpath.c', 'utils.c', 'bytes_format.c', - 'libpq_support.c', 'win32_support.c', + 'libpq_support.c', 'win32_support.c', 'solaris_support.c', 'connection_int.c', 'connection_type.c', 'cursor_int.c', 'cursor_type.c', From dfee199351429a4170c7d190fbecacb35777de2e Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 27 Oct 2017 12:27:05 +0100 Subject: [PATCH 11/96] Dropped suggestion for --no-binary :all: to skip wheels :all: applies to the entire file. --no-binary psycopg2 is the solution. See issue #543 --- doc/src/install.rst | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/doc/src/install.rst b/doc/src/install.rst index 9119753e..086bc628 100644 --- a/doc/src/install.rst +++ b/doc/src/install.rst @@ -33,6 +33,8 @@ The current `!psycopg2` implementation supports: .. index:: single: Install; from PyPI + single: Install; wheel + single: Wheel Binary install from PyPI ------------------------ @@ -67,21 +69,36 @@ using something like ``pip install -U pip``) incompatible with other extension modules binding with ``libssl`` as well, for instance the Python `ssl` module: the result will likely be a segfault. If you need using both `!psycopg2` and other libraries using - ``libssl`` please :ref:`install psycopg from source - `. + ``libssl`` please :ref:`disable the use of wheel packages for Psycopg + `. -If you prefer to use the system libraries available on your client you can use -the :command:`pip` ``--no-binary`` option: + + +.. index:: + single: Install; disable wheel + single: Wheel; disable + +.. _disable-wheel: + +Disabling wheel packages +^^^^^^^^^^^^^^^^^^^^^^^^ + +If you want to disable the use of wheel binary packages and use the system +system libraries available on your client you can use the :command:`pip` +|--no-binary option|__: .. code-block:: console $ pip install --no-binary psycopg2 +.. |--no-binary option| replace:: ``--no-binary`` option +.. __: https://pip.pypa.io/en/stable/reference/pip_install/#install-no-binary + which can be specified in your :file:`requirements.txt` files too, e.g. use: .. code-block:: none - psycopg2>=2.7,<2.8 --no-binary :all: + psycopg2>=2.7,<2.8 --no-binary psycopg2 to use the last bugfix release of the `!psycopg2` 2.7 package, specifying to always compile it from source. Of course in this case you will have to meet From b804c092ac2897cb139810c025ff4f998c7c66d4 Mon Sep 17 00:00:00 2001 From: Kevin Campbell Date: Fri, 3 Nov 2017 17:30:06 +0800 Subject: [PATCH 12/96] Patch for issue #609 for MinTimeLoggingConnection On Python3 MinTimeLoggingConnection raises an exception as it tries to mix strings and bytes --- lib/extras.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/extras.py b/lib/extras.py index 798b3d2b..7ee52c75 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -455,6 +455,8 @@ class MinTimeLoggingConnection(LoggingConnection): def filter(self, msg, curs): t = (_time.time() - curs.timestamp) * 1000 if t > self._mintime: + if _sys.version_info[0] >= 3 and isinstance(msg, bytes): + msg = msg.decode(_ext.encodings[self.encoding], 'replace') return msg + _os.linesep + " (execution time: %d ms)" % t def cursor(self, *args, **kwargs): From 4f1505857bc1de75f83cf113c736881e0033b9ec Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 6 Nov 2017 16:58:50 +0000 Subject: [PATCH 13/96] Parameters passing docs improved Every point has an example and all the example show wrong/correct. Nice rhythm. Among the improvements, added point saying explicitly "thou shall not quote placeholders". Quoted placeholders will just fail except in the most contrived cases (a statement raising an exception with all the strings except with the attack ones...), and an example in the following section explicitly notes "no quotes", but apparenty someone still thinks this is not documented enough? (see issue #611) so let's just write it plain and clear into the list of commandments. --- doc/src/usage.rst | 53 +++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/doc/src/usage.rst b/doc/src/usage.rst index 660f2904..429d6b92 100644 --- a/doc/src/usage.rst +++ b/doc/src/usage.rst @@ -92,51 +92,68 @@ is converted into the SQL command:: Named arguments are supported too using :samp:`%({name})s` placeholders. Using named arguments the values can be passed to the query in any order and -many placeholders can use the same values:: +several placeholders can use the same value:: >>> cur.execute( ... """INSERT INTO some_table (an_int, a_date, another_date, a_string) ... VALUES (%(int)s, %(date)s, %(date)s, %(str)s);""", ... {'int': 10, 'str': "O'Reilly", 'date': datetime.date(2005, 11, 18)}) +Using characters ``%``, ``(``, ``)`` in the argument names is not supported. + When parameters are used, in order to include a literal ``%`` in the query you -can use the ``%%`` string. Using characters ``%``, ``(``, ``)`` in the -argument names is not supported. +can use the ``%%`` string:: + + >>> cur.execute("SELECT (%s % 2) = 0 AS even", (10,)) # WRONG + >>> cur.execute("SELECT (%s %% 2) = 0 AS even", (10,)) # correct While the mechanism resembles regular Python strings manipulation, there are a few subtle differences you should care about when passing parameters to a -query: +query. -- The Python string operator ``%`` is not used: the `~cursor.execute()` +- The Python string operator ``%`` *must not be used*: the `~cursor.execute()` method accepts a tuple or dictionary of values as second parameter. - |sql-warn|__. + |sql-warn|__: .. |sql-warn| replace:: **Never** use ``%`` or ``+`` to merge values into queries .. __: sql-injection_ -- The variables placeholder must *always be a* ``%s``, even if a different - placeholder (such as a ``%d`` for integers or ``%f`` for floats) may look - more appropriate:: - - >>> cur.execute("INSERT INTO numbers VALUES (%d)", (42,)) # WRONG - >>> cur.execute("INSERT INTO numbers VALUES (%s)", (42,)) # correct + >>> cur.execute("INSERT INTO numbers VALUES (%s, %s)" % (10, 20)) # WRONG + >>> cur.execute("INSERT INTO numbers VALUES (%s, %s)", (10, 20)) # correct - For positional variables binding, *the second argument must always be a - sequence*, even if it contains a single variable. And remember that Python - requires a comma to create a single element tuple:: + sequence*, even if it contains a single variable (remember that Python + requires a comma to create a single element tuple):: >>> cur.execute("INSERT INTO foo VALUES (%s)", "bar") # WRONG >>> cur.execute("INSERT INTO foo VALUES (%s)", ("bar")) # WRONG >>> cur.execute("INSERT INTO foo VALUES (%s)", ("bar",)) # correct >>> cur.execute("INSERT INTO foo VALUES (%s)", ["bar"]) # correct -- Only query values should be bound via this method: it shouldn't be used to - merge table or field names to the query. If you need to generate dynamically - an SQL query (for instance choosing dynamically a table name) you can use - the facilities provided by the `psycopg2.sql` module. +- The placeholder *must not be quoted*. Psycopg will add quotes where needed:: + >>> cur.execute("INSERT INTO numbers VALUES ('%s')", (10,)) # WRONG + >>> cur.execute("INSERT INTO numbers VALUES (%s)", (10,)) # correct + +- The variables placeholder *must always be a* ``%s``, even if a different + placeholder (such as a ``%d`` for integers or ``%f`` for floats) may look + more appropriate:: + + >>> cur.execute("INSERT INTO numbers VALUES (%d)", (10,)) # WRONG + >>> cur.execute("INSERT INTO numbers VALUES (%s)", (10,)) # correct + +- Only query values should be bound via this method: it shouldn't be used to + merge table or field names to the query (Psycopg will try quoting the table + name as a string value, generating invalid SQL). If you need to generate + dynamically SQL queries (for instance choosing dynamically a table name) + you can use the facilities provided by the `psycopg2.sql` module:: + + >>> cur.execute("INSERT INTO %s VALUES (%s)", ('numbers', 10)) # WRONG + >>> cur.execute( # correct + ... SQL("INSERT INTO {} VALUES (%s)").format(Identifier('numbers')), + ... (10,)) .. index:: Security, SQL injection From 9614e7241bf8a4c713895b01018027dd35ee8c60 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 6 Nov 2017 17:57:39 +0000 Subject: [PATCH 14/96] Further docs cleanup Recent Sphinx versions seem overly aggressive in autodetecting python, or I just didn't notice the errors, so be explicit in what language to use with code examples. --- doc/src/advanced.rst | 4 +++- doc/src/conf.py | 4 ++++ doc/src/extras.rst | 4 ++-- doc/src/faq.rst | 4 +++- doc/src/usage.rst | 44 ++++++++++++++++++++++++++++---------------- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/doc/src/advanced.rst b/doc/src/advanced.rst index 5b5fb354..b2b8afe3 100644 --- a/doc/src/advanced.rst +++ b/doc/src/advanced.rst @@ -295,7 +295,9 @@ something to read:: print "Got NOTIFY:", notify.pid, notify.channel, notify.payload Running the script and executing a command such as :sql:`NOTIFY test, 'hello'` -in a separate :program:`psql` shell, the output may look similar to:: +in a separate :program:`psql` shell, the output may look similar to: + +.. code-block:: none Waiting for notifications on channel 'test' Timeout diff --git a/doc/src/conf.py b/doc/src/conf.py index 9e73308b..6afb2869 100644 --- a/doc/src/conf.py +++ b/doc/src/conf.py @@ -101,6 +101,10 @@ default_role = 'obj' # output. They are ignored by default. #show_authors = False +# Using 'python' instead of the default gives warnings if parsing an example +# fails, instead of defaulting to none +highlight_language = 'python' + # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' diff --git a/doc/src/extras.rst b/doc/src/extras.rst index 36118e7e..c844873a 100644 --- a/doc/src/extras.rst +++ b/doc/src/extras.rst @@ -403,7 +403,7 @@ The individual messages in the replication stream are represented by class LogicalStreamConsumer(object): - ... + # ... def __call__(self, msg): self.process_message(msg.payload) @@ -501,7 +501,7 @@ The individual messages in the replication stream are represented by from datetime import datetime def consume(msg): - ... + # ... keepalive_interval = 10.0 while True: diff --git a/doc/src/faq.rst b/doc/src/faq.rst index fb7b33dd..0ef4c708 100644 --- a/doc/src/faq.rst +++ b/doc/src/faq.rst @@ -306,7 +306,9 @@ I can't compile `!psycopg2`: the compiler says *error: libpq-fe.h: No such file API support (*i.e.* the libpq used at compile time was at least 9.3) but at runtime an older libpq dynamic library is found. - You can use:: + You can use: + + .. code-block:: shell $ ldd /path/to/packages/psycopg2/_psycopg.so | grep libpq diff --git a/doc/src/usage.rst b/doc/src/usage.rst index 429d6b92..79823b11 100644 --- a/doc/src/usage.rst +++ b/doc/src/usage.rst @@ -48,7 +48,7 @@ The main entry points of Psycopg are: - The class `connection` encapsulates a database session. It allows to: - - create new `cursor`\s using the `~connection.cursor()` method to + - create new `cursor` instances using the `~connection.cursor()` method to execute database commands and queries, - terminate transactions using the methods `~connection.commit()` or @@ -73,30 +73,40 @@ The main entry points of Psycopg are: Passing parameters to SQL queries --------------------------------- -Psycopg casts Python variables to SQL literals by type. Many standard Python types -are already `adapted to the correct SQL representation`__. +Psycopg converts Python variables to SQL values using their types: the Python +type determines the function used to convert the object into a string +representation suitable for PostgreSQL. Many standard Python types are +already `adapted to the correct SQL representation`__. .. __: python-types-adaptation_ -Example: the Python function call:: +Passing parameters to an SQL statement happens in functions such as +`cursor.execute()` by using ``%s`` placeholders in the SQL statement, and +passing a sequence of values as the second argument of the function. For +example the Python function call:: - >>> cur.execute( - ... """INSERT INTO some_table (an_int, a_date, a_string) - ... VALUES (%s, %s, %s);""", + >>> cur.execute(""" + ... INSERT INTO some_table (an_int, a_date, a_string) + ... VALUES (%s, %s, %s); + ... """, ... (10, datetime.date(2005, 11, 18), "O'Reilly")) -is converted into the SQL command:: +is converted into a SQL command similar to: + +.. code-block:: sql INSERT INTO some_table (an_int, a_date, a_string) - VALUES (10, '2005-11-18', 'O''Reilly'); + VALUES (10, '2005-11-18', 'O''Reilly'); -Named arguments are supported too using :samp:`%({name})s` placeholders. -Using named arguments the values can be passed to the query in any order and -several placeholders can use the same value:: +Named arguments are supported too using :samp:`%({name})s` placeholders in the +query and specifying the values into a mapping. Using named arguments allows +to specify the values in any order and to repeat the same value in several +places in the query:: - >>> cur.execute( - ... """INSERT INTO some_table (an_int, a_date, another_date, a_string) - ... VALUES (%(int)s, %(date)s, %(date)s, %(str)s);""", + >>> cur.execute(""" + ... INSERT INTO some_table (an_int, a_date, another_date, a_string) + ... VALUES (%(int)s, %(date)s, %(date)s, %(str)s); + ... """, ... {'int': 10, 'str': "O'Reilly", 'date': datetime.date(2005, 11, 18)}) Using characters ``%``, ``(``, ``)`` in the argument names is not supported. @@ -809,7 +819,9 @@ lifetime extends well after `~connection.commit()`, calling It is also possible to use a named cursor to consume a cursor created in some other way than using the |DECLARE| executed by `~cursor.execute()`. For example, you may have a PL/pgSQL function - returning a cursor:: + returning a cursor: + + .. code-block:: postgres CREATE FUNCTION reffunc(refcursor) RETURNS refcursor AS $$ BEGIN From d88d8f961947fb672e0cecd023eaabedc119fa5f Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 6 Nov 2017 18:38:22 +0000 Subject: [PATCH 15/96] Added PostgreSQL 10 in the list of supported servers --- doc/src/install.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/src/install.rst b/doc/src/install.rst index 086bc628..2993c7cb 100644 --- a/doc/src/install.rst +++ b/doc/src/install.rst @@ -19,7 +19,7 @@ The current `!psycopg2` implementation supports: - Python 2 versions from 2.6 to 2.7 - Python 3 versions from 3.2 to 3.6 -- PostgreSQL server versions from 7.4 to 9.6 +- PostgreSQL server versions from 7.4 to 10 - PostgreSQL client library version from 9.1 .. _PostgreSQL: http://www.postgresql.org/ From 5983b96c5579be2403fec0611adf276192c5a75e Mon Sep 17 00:00:00 2001 From: Federico Di Gregorio Date: Thu, 16 Nov 2017 10:07:27 +0100 Subject: [PATCH 16/96] Don't cast point arrays to float arrays (fixes: #613) --- psycopg/typecast_builtins.c | 2 +- tests/test_types_extras.py | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/psycopg/typecast_builtins.c b/psycopg/typecast_builtins.c index 3ed76892..1c4a8ca0 100644 --- a/psycopg/typecast_builtins.c +++ b/psycopg/typecast_builtins.c @@ -15,7 +15,7 @@ static long int typecast_BINARY_types[] = {17, 0}; static long int typecast_ROWID_types[] = {26, 0}; static long int typecast_LONGINTEGERARRAY_types[] = {1016, 0}; static long int typecast_INTEGERARRAY_types[] = {1005, 1006, 1007, 0}; -static long int typecast_FLOATARRAY_types[] = {1017, 1021, 1022, 0}; +static long int typecast_FLOATARRAY_types[] = {1021, 1022, 0}; static long int typecast_DECIMALARRAY_types[] = {1231, 0}; static long int typecast_UNICODEARRAY_types[] = {1002, 1003, 1009, 1014, 1015, 0}; static long int typecast_STRINGARRAY_types[] = {1002, 1003, 1009, 1014, 1015, 0}; diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index b64d27e8..3d0d5ed5 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -116,6 +116,11 @@ class TypesExtrasTests(ConnectingTestCase): except psycopg2.ProgrammingError, err: self.failUnless(str(err) == "can't adapt type 'Foo'") + def test_point_array(self): + # make sure a point array is never casted to a float array, + # see https://github.com/psycopg/psycopg2/issues/613 + s = self.execute("""SELECT '{"(1,2)","(3,4)"}' AS foo""") + self.failUnless(s == """{"(1,2)","(3,4)"}""") def skip_if_no_hstore(f): @wraps(f) From 7a2dd85caa7553d163f6579eb513a370ef069a0e Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 16 Nov 2017 16:07:34 +0000 Subject: [PATCH 17/96] NEWS updated after last bugfix. --- NEWS | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 799477ec..64852635 100644 --- a/NEWS +++ b/NEWS @@ -1,18 +1,24 @@ Current release --------------- +What's new in psycopg 2.7.4 +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Fixed parsing of array of points as floats (:ticket:`#613`). + + What's new in psycopg 2.7.3.2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Wheel package compiled against PostgreSQL 10.0 libpq and OpenSSL 1.0.2l - (:tickets:`#601, #602`) + (:tickets:`#601, #602`). What's new in psycopg 2.7.3.1 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Dropped libresolv from wheel package to avoid incompatibility with - glibc 2.26 (wheels ticket #2) + glibc 2.26 (wheels ticket #2). What's new in psycopg 2.7.3 From 390e43fcb191d1b52ca747e2b71f938f9df52c95 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Mon, 20 Nov 2017 20:00:35 -0800 Subject: [PATCH 18/96] Use modern except syntax throughout project The syntax "except Exception, exc:" is deprecated. All Python versions supported by psycopg2 support the newer, modern syntax. Forward compatible with future Python versions. --- lib/extras.py | 2 +- tests/__init__.py | 2 +- tests/test_async.py | 2 +- tests/test_async_keyword.py | 2 +- tests/test_cancel.py | 4 ++-- tests/test_connection.py | 4 ++-- tests/test_copy.py | 2 +- tests/test_errcodes.py | 2 +- tests/test_module.py | 22 +++++++++++----------- tests/test_transaction.py | 8 ++++---- tests/test_types_basic.py | 2 +- tests/test_types_extras.py | 2 +- tests/test_with.py | 2 +- tests/testutils.py | 6 +++--- 14 files changed, 31 insertions(+), 31 deletions(-) diff --git a/lib/extras.py b/lib/extras.py index 798b3d2b..8abb14fc 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -363,7 +363,7 @@ class NamedTupleCursor(_cursor): try: from collections import namedtuple - except ImportError, _exc: + except ImportError as _exc: def _make_nt(self): raise self._exc else: diff --git a/tests/__init__.py b/tests/__init__.py index 85a4ec90..6cb6c7ba 100755 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -65,7 +65,7 @@ def test_suite(): import psycopg2 try: cnn = psycopg2.connect(dsn) - except Exception, e: + except Exception as e: print "Failed connection to test db:", e.__class__.__name__, e print "Please set env vars 'PSYCOPG2_TESTDB*' to valid values." sys.exit(1) diff --git a/tests/test_async.py b/tests/test_async.py index 0a386d3e..4eb5e6a2 100755 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -444,7 +444,7 @@ class AsyncTests(ConnectingTestCase): try: cnn = psycopg2.connect('dbname=thisdatabasedoesntexist', async_=True) self.wait(cnn) - except psycopg2.Error, e: + except psycopg2.Error as e: self.assertNotEqual(str(e), "asynchronous connection failed", "connection error reason lost") else: diff --git a/tests/test_async_keyword.py b/tests/test_async_keyword.py index ff41e1b6..ddf43266 100755 --- a/tests/test_async_keyword.py +++ b/tests/test_async_keyword.py @@ -81,7 +81,7 @@ class AsyncTests(ConnectingTestCase): try: cnn = psycopg2.connect('dbname=thisdatabasedoesntexist', async=True) self.wait(cnn) - except psycopg2.Error, e: + except psycopg2.Error as e: self.assertNotEqual(str(e), "asynchronous connection failed", "connection error reason lost") else: diff --git a/tests/test_cancel.py b/tests/test_cancel.py index 9b3261c3..529c4ba4 100755 --- a/tests/test_cancel.py +++ b/tests/test_cancel.py @@ -63,7 +63,7 @@ class CancelTests(ConnectingTestCase): conn.rollback() cur.execute("select 1") self.assertEqual(cur.fetchall(), [(1, )]) - except Exception, e: + except Exception as e: errors.append(e) raise @@ -71,7 +71,7 @@ class CancelTests(ConnectingTestCase): cur = conn.cursor() try: conn.cancel() - except Exception, e: + except Exception as e: errors.append(e) raise del cur diff --git a/tests/test_connection.py b/tests/test_connection.py index 7818c8cd..eff10650 100755 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -358,7 +358,7 @@ class ParseDsnTestCase(ConnectingTestCase): try: # unterminated quote after dbname: ext.parse_dsn("dbname='test 2 user=tester password=secret") - except ProgrammingError, e: + except ProgrammingError as e: raised = True self.assertTrue(str(e).find('secret') < 0, "DSN was not exposed in error message") @@ -376,7 +376,7 @@ class ParseDsnTestCase(ConnectingTestCase): try: # extra '=' after port value ext.parse_dsn(dsn='postgresql://tester:secret@/test?port=1111=x') - except psycopg2.ProgrammingError, e: + except psycopg2.ProgrammingError as e: raised = True self.assertTrue(str(e).find('secret') < 0, "URI was not exposed in error message") diff --git a/tests/test_copy.py b/tests/test_copy.py index 3aa509b5..96623211 100755 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -364,7 +364,7 @@ conn.close() # curs.copy_from, BrokenRead(), "tcopy") try: curs.copy_from(BrokenRead(), "tcopy") - except Exception, e: + except Exception as e: self.assert_('ZeroDivisionError' in str(e)) def test_copy_to_propagate_error(self): diff --git a/tests/test_errcodes.py b/tests/test_errcodes.py index 4848a764..d651b227 100755 --- a/tests/test_errcodes.py +++ b/tests/test_errcodes.py @@ -48,7 +48,7 @@ class ErrocodeTests(ConnectingTestCase): def f(pg_code='40001'): try: errorcodes.lookup(pg_code) - except Exception, e: + except Exception as e: errs.append(e) for __ in xrange(MAX_CYCLES): diff --git a/tests/test_module.py b/tests/test_module.py index 2af3c612..d2b13673 100755 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -152,7 +152,7 @@ class ExceptionsTestCase(ConnectingTestCase): cur = self.conn.cursor() try: cur.execute("select * from nonexist") - except psycopg2.Error, exc: + except psycopg2.Error as exc: e = exc self.assertEqual(e.pgcode, '42P01') @@ -163,7 +163,7 @@ class ExceptionsTestCase(ConnectingTestCase): cur = self.conn.cursor() try: cur.execute("select * from nonexist") - except psycopg2.Error, exc: + except psycopg2.Error as exc: e = exc diag = e.diag @@ -182,7 +182,7 @@ class ExceptionsTestCase(ConnectingTestCase): cur = self.conn.cursor() try: cur.execute("select * from nonexist") - except psycopg2.Error, exc: + except psycopg2.Error as exc: e = exc self.assertEqual(e.diag.sqlstate, '42P01') @@ -196,7 +196,7 @@ class ExceptionsTestCase(ConnectingTestCase): cur = self.conn.cursor() try: cur.execute("select * from nonexist") - except psycopg2.Error, exc: + except psycopg2.Error as exc: return cur, exc cur, e = tmp() @@ -221,7 +221,7 @@ class ExceptionsTestCase(ConnectingTestCase): cur = self.conn.cursor() try: cur.copy_to(f, 'nonexist') - except psycopg2.Error, exc: + except psycopg2.Error as exc: diag = exc.diag self.assertEqual(diag.sqlstate, '42P01') @@ -230,14 +230,14 @@ class ExceptionsTestCase(ConnectingTestCase): cur = self.conn.cursor() try: cur.execute("l'acqua e' poca e 'a papera nun galleggia") - except Exception, exc: + except Exception as exc: diag1 = exc.diag self.conn.rollback() try: cur.execute("select level from water where ducks > 1") - except psycopg2.Error, exc: + except psycopg2.Error as exc: diag2 = exc.diag self.assertEqual(diag1.sqlstate, '42601') @@ -254,7 +254,7 @@ class ExceptionsTestCase(ConnectingTestCase): cur.execute("insert into test_deferred values (1,2)") try: self.conn.commit() - except psycopg2.Error, exc: + except psycopg2.Error as exc: e = exc self.assertEqual(e.diag.sqlstate, '23503') @@ -267,7 +267,7 @@ class ExceptionsTestCase(ConnectingTestCase): )""") try: cur.execute("insert into test_exc values(2)") - except psycopg2.Error, exc: + except psycopg2.Error as exc: e = exc self.assertEqual(e.pgcode, '23514') self.assertEqual(e.diag.schema_name[:7], "pg_temp") @@ -282,7 +282,7 @@ class ExceptionsTestCase(ConnectingTestCase): cur = self.conn.cursor() try: cur.execute("select * from nonexist") - except psycopg2.Error, exc: + except psycopg2.Error as exc: e = exc e1 = pickle.loads(pickle.dumps(e)) @@ -297,7 +297,7 @@ class ExceptionsTestCase(ConnectingTestCase): import pickle try: psycopg2.connect('dbname=nosuchdatabasemate') - except psycopg2.Error, exc: + except psycopg2.Error as exc: e = exc e1 = pickle.loads(pickle.dumps(e)) diff --git a/tests/test_transaction.py b/tests/test_transaction.py index 36947dee..dd487c05 100755 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -145,7 +145,7 @@ class DeadlockSerializationTests(ConnectingTestCase): step1.set() step2.wait() curs.execute("LOCK table2 IN ACCESS EXCLUSIVE MODE") - except psycopg2.DatabaseError, exc: + except psycopg2.DatabaseError as exc: self.thread1_error = exc step1.set() conn.close() @@ -158,7 +158,7 @@ class DeadlockSerializationTests(ConnectingTestCase): curs.execute("LOCK table2 IN ACCESS EXCLUSIVE MODE") step2.set() curs.execute("LOCK table1 IN ACCESS EXCLUSIVE MODE") - except psycopg2.DatabaseError, exc: + except psycopg2.DatabaseError as exc: self.thread2_error = exc step2.set() conn.close() @@ -195,7 +195,7 @@ class DeadlockSerializationTests(ConnectingTestCase): step2.wait() curs.execute("UPDATE table1 SET name='task1' WHERE id = 1") conn.commit() - except psycopg2.DatabaseError, exc: + except psycopg2.DatabaseError as exc: self.thread1_error = exc step1.set() conn.close() @@ -207,7 +207,7 @@ class DeadlockSerializationTests(ConnectingTestCase): step1.wait() curs.execute("UPDATE table1 SET name='task2' WHERE id = 1") conn.commit() - except psycopg2.DatabaseError, exc: + except psycopg2.DatabaseError as exc: self.thread2_error = exc step2.set() conn.close() diff --git a/tests/test_types_basic.py b/tests/test_types_basic.py index b4a5f2c6..94868583 100755 --- a/tests/test_types_basic.py +++ b/tests/test_types_basic.py @@ -443,7 +443,7 @@ class ByteaParserTest(unittest.TestCase): def setUp(self): try: self._cast = self._import_cast() - except Exception, e: + except Exception as e: self._cast = None self._exc = e diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index 3d0d5ed5..e1db2c2c 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -113,7 +113,7 @@ class TypesExtrasTests(ConnectingTestCase): psycopg2.extensions.adapt, Foo(), ext.ISQLQuote, None) try: psycopg2.extensions.adapt(Foo(), ext.ISQLQuote, None) - except psycopg2.ProgrammingError, err: + except psycopg2.ProgrammingError as err: self.failUnless(str(err) == "can't adapt type 'Foo'") def test_point_array(self): diff --git a/tests/test_with.py b/tests/test_with.py index 9d91b51e..e5c0bc23 100755 --- a/tests/test_with.py +++ b/tests/test_with.py @@ -212,7 +212,7 @@ class WithCursorTestCase(WithTestCase): with conn.cursor('named') as cur: cur.execute("select 1/0") cur.fetchone() - except psycopg2.DataError, e: + except psycopg2.DataError as e: self.assertEqual(e.pgcode, '22012') else: self.fail("where is my exception?") diff --git a/tests/testutils.py b/tests/testutils.py index 7489a986..5c192e30 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -116,7 +116,7 @@ class ConnectingTestCase(unittest.TestCase): def connect(self, **kwargs): try: self._conns - except AttributeError, e: + except AttributeError as e: raise AttributeError( "%s (did you forget to call ConnectingTestCase.setUp()?)" % e) @@ -149,7 +149,7 @@ class ConnectingTestCase(unittest.TestCase): conn = self.connect(**kwargs) if conn.async_ == 1: self.wait(conn) - except psycopg2.OperationalError, e: + except psycopg2.OperationalError as e: # If pgcode is not set it is a genuine connection error # Otherwise we tried to run some bad operation in the connection # (e.g. bug #482) and we'd rather know that. @@ -388,7 +388,7 @@ def skip_if_no_superuser(f): from psycopg2 import ProgrammingError try: return f(self) - except ProgrammingError, e: + except ProgrammingError as e: import psycopg2.errorcodes if e.pgcode == psycopg2.errorcodes.INSUFFICIENT_PRIVILEGE: self.skipTest("skipped because not superuser") From 5ddac80cec29de8ab08f124fbace86188b43fdce Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 26 Nov 2017 10:42:30 -0800 Subject: [PATCH 19/96] Include license file in the generated wheel package The wheel package format supports including the license file. This is done using the [metadata] section in the setup.cfg file. For additional information on this feature, see: https://wheel.readthedocs.io/en/stable/index.html#including-the-license-in-the-generated-wheel-file --- setup.cfg | 3 +++ 1 file changed, 3 insertions(+) diff --git a/setup.cfg b/setup.cfg index 0d41934f..a5efdcfa 100644 --- a/setup.cfg +++ b/setup.cfg @@ -27,3 +27,6 @@ static_libpq=0 # Add here eventual extra libraries required to link the module. libraries= + +[metadata] +license_file = LICENSE From 05c28cce78aa223e632635af813cbbaed4c82169 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 26 Nov 2017 13:41:22 -0800 Subject: [PATCH 20/96] Remove workarounds for namedtuple on Python <= 2.5 namedtuple is available on all Python versions supported by psycopg2. It was first introduced in Python 2.6. Can remove all workarounds and special documentation. --- doc/src/extras.rst | 14 ----------- lib/extras.py | 21 ++++------------- tests/test_cursor.py | 3 +-- tests/test_extras_dictcursor.py | 41 --------------------------------- tests/test_types_extras.py | 22 ++++-------------- tests/testutils.py | 13 ----------- 6 files changed, 11 insertions(+), 103 deletions(-) diff --git a/doc/src/extras.rst b/doc/src/extras.rst index c844873a..cb8335be 100644 --- a/doc/src/extras.rst +++ b/doc/src/extras.rst @@ -99,20 +99,6 @@ Real dictionary cursor .. versionadded:: 2.3 -These objects require :py:func:`collections.namedtuple` to be found, so it is -available out-of-the-box only from Python 2.6. Anyway, the namedtuple -implementation is compatible with previous Python versions, so all you -have to do is to `download it`__ and make it available where we -expect it to be... :: - - from somewhere import namedtuple - import collections - collections.namedtuple = namedtuple - from psycopg.extras import NamedTupleConnection - # ... - -.. __: http://code.activestate.com/recipes/500261-named-tuples/ - .. autoclass:: NamedTupleCursor .. autoclass:: NamedTupleConnection diff --git a/lib/extras.py b/lib/extras.py index 8abb14fc..bdd61f7a 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -29,6 +29,7 @@ import os as _os import sys as _sys import time as _time import re as _re +from collections import namedtuple try: import logging as _logging @@ -361,14 +362,8 @@ class NamedTupleCursor(_cursor): except StopIteration: return - try: - from collections import namedtuple - except ImportError as _exc: - def _make_nt(self): - raise self._exc - else: - def _make_nt(self, namedtuple=namedtuple): - return namedtuple("Record", [d[0] for d in self.description or ()]) + def _make_nt(self): + return namedtuple("Record", [d[0] for d in self.description or ()]) class LoggingConnection(_connection): @@ -1055,14 +1050,8 @@ class CompositeCaster(object): return rv def _create_type(self, name, attnames): - try: - from collections import namedtuple - except ImportError: - self.type = tuple - self._ctor = self.type - else: - self.type = namedtuple(name, attnames) - self._ctor = self.type._make + self.type = namedtuple(name, attnames) + self._ctor = self.type._make @classmethod def _from_db(self, name, conn_or_curs): diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 10b8d71e..dbc72e19 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -27,7 +27,7 @@ import pickle import psycopg2 import psycopg2.extensions from testutils import (unittest, ConnectingTestCase, skip_before_postgres, - skip_if_no_namedtuple, skip_if_no_getrefcount, slow, skip_if_no_superuser, + skip_if_no_getrefcount, slow, skip_if_no_superuser, skip_if_windows) import psycopg2.extras @@ -377,7 +377,6 @@ class CursorTests(ConnectingTestCase): for i, rec in enumerate(curs): self.assertEqual(i + 1, curs.rownumber) - @skip_if_no_namedtuple def test_namedtuple_description(self): curs = self.conn.cursor() curs.execute("""select diff --git a/tests/test_extras_dictcursor.py b/tests/test_extras_dictcursor.py index 20393c66..2c867d38 100755 --- a/tests/test_extras_dictcursor.py +++ b/tests/test_extras_dictcursor.py @@ -19,7 +19,6 @@ from datetime import timedelta import psycopg2 import psycopg2.extras from testutils import unittest, ConnectingTestCase, skip_before_postgres -from testutils import skip_if_no_namedtuple class ExtrasDictCursorTests(ConnectingTestCase): @@ -232,11 +231,6 @@ class NamedTupleCursorTest(ConnectingTestCase): ConnectingTestCase.setUp(self) from psycopg2.extras import NamedTupleConnection - try: - from collections import namedtuple # noqa - except ImportError: - return - self.conn = self.connect(connection_factory=NamedTupleConnection) curs = self.conn.cursor() curs.execute("CREATE TEMPORARY TABLE nttest (i int, s text)") @@ -245,13 +239,11 @@ class NamedTupleCursorTest(ConnectingTestCase): curs.execute("INSERT INTO nttest VALUES (3, 'baz')") self.conn.commit() - @skip_if_no_namedtuple def test_cursor_args(self): cur = self.conn.cursor('foo', cursor_factory=psycopg2.extras.DictCursor) self.assertEqual(cur.name, 'foo') self.assert_(isinstance(cur, psycopg2.extras.DictCursor)) - @skip_if_no_namedtuple def test_fetchone(self): curs = self.conn.cursor() curs.execute("select * from nttest order by 1") @@ -263,7 +255,6 @@ class NamedTupleCursorTest(ConnectingTestCase): self.assertEqual(curs.rownumber, 1) self.assertEqual(curs.rowcount, 3) - @skip_if_no_namedtuple def test_fetchmany_noarg(self): curs = self.conn.cursor() curs.arraysize = 2 @@ -277,7 +268,6 @@ class NamedTupleCursorTest(ConnectingTestCase): self.assertEqual(curs.rownumber, 2) self.assertEqual(curs.rowcount, 3) - @skip_if_no_namedtuple def test_fetchmany(self): curs = self.conn.cursor() curs.execute("select * from nttest order by 1") @@ -290,7 +280,6 @@ class NamedTupleCursorTest(ConnectingTestCase): self.assertEqual(curs.rownumber, 2) self.assertEqual(curs.rowcount, 3) - @skip_if_no_namedtuple def test_fetchall(self): curs = self.conn.cursor() curs.execute("select * from nttest order by 1") @@ -305,7 +294,6 @@ class NamedTupleCursorTest(ConnectingTestCase): self.assertEqual(curs.rownumber, 3) self.assertEqual(curs.rowcount, 3) - @skip_if_no_namedtuple def test_executemany(self): curs = self.conn.cursor() curs.executemany("delete from nttest where i = %s", @@ -316,7 +304,6 @@ class NamedTupleCursorTest(ConnectingTestCase): self.assertEqual(res[0].i, 3) self.assertEqual(res[0].s, 'baz') - @skip_if_no_namedtuple def test_iter(self): curs = self.conn.cursor() curs.execute("select * from nttest order by 1") @@ -342,26 +329,6 @@ class NamedTupleCursorTest(ConnectingTestCase): self.assertEqual(curs.rownumber, 3) self.assertEqual(curs.rowcount, 3) - def test_error_message(self): - try: - from collections import namedtuple # noqa - except ImportError: - # an import error somewhere - from psycopg2.extras import NamedTupleConnection - try: - self.conn = self.connect( - connection_factory=NamedTupleConnection) - curs = self.conn.cursor() - curs.execute("select 1") - curs.fetchone() - except ImportError: - pass - else: - self.fail("expecting ImportError") - else: - return self.skipTest("namedtuple available") - - @skip_if_no_namedtuple def test_record_updated(self): curs = self.conn.cursor() curs.execute("select 1 as foo;") @@ -373,7 +340,6 @@ class NamedTupleCursorTest(ConnectingTestCase): self.assertEqual(r.bar, 2) self.assertRaises(AttributeError, getattr, r, 'foo') - @skip_if_no_namedtuple def test_no_result_no_surprise(self): curs = self.conn.cursor() curs.execute("update nttest set s = s") @@ -382,7 +348,6 @@ class NamedTupleCursorTest(ConnectingTestCase): curs.execute("update nttest set s = s") self.assertRaises(psycopg2.ProgrammingError, curs.fetchall) - @skip_if_no_namedtuple def test_minimal_generation(self): # Instrument the class to verify it gets called the minimum number of times. from psycopg2.extras import NamedTupleCursor @@ -416,7 +381,6 @@ class NamedTupleCursorTest(ConnectingTestCase): finally: NamedTupleCursor._make_nt = f_orig - @skip_if_no_namedtuple @skip_before_postgres(8, 0) def test_named(self): curs = self.conn.cursor('tmp') @@ -427,28 +391,24 @@ class NamedTupleCursorTest(ConnectingTestCase): recs.extend(curs.fetchall()) self.assertEqual(range(10), [t.i for t in recs]) - @skip_if_no_namedtuple def test_named_fetchone(self): curs = self.conn.cursor('tmp') curs.execute("""select 42 as i""") t = curs.fetchone() self.assertEqual(t.i, 42) - @skip_if_no_namedtuple def test_named_fetchmany(self): curs = self.conn.cursor('tmp') curs.execute("""select 42 as i""") recs = curs.fetchmany(10) self.assertEqual(recs[0].i, 42) - @skip_if_no_namedtuple def test_named_fetchall(self): curs = self.conn.cursor('tmp') curs.execute("""select 42 as i""") recs = curs.fetchall() self.assertEqual(recs[0].i, 42) - @skip_if_no_namedtuple @skip_before_postgres(8, 2) def test_not_greedy(self): curs = self.conn.cursor('tmp') @@ -463,7 +423,6 @@ class NamedTupleCursorTest(ConnectingTestCase): self.assert_(recs[1].ts - recs[0].ts < timedelta(seconds=0.005)) self.assert_(recs[2].ts - recs[1].ts > timedelta(seconds=0.0099)) - @skip_if_no_namedtuple @skip_before_postgres(8, 0) def test_named_rownumber(self): curs = self.conn.cursor('tmp') diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index e1db2c2c..a5aa7d2b 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -541,16 +541,10 @@ class AdaptTypeTestCase(ConnectingTestCase): self.assertEqual(v[0], 10) self.assertEqual(v[1], "hello") self.assertEqual(v[2], date(2011, 1, 2)) - - try: - from collections import namedtuple # noqa - except ImportError: - pass - else: - self.assert_(t.type is not tuple) - self.assertEqual(v.anint, 10) - self.assertEqual(v.astring, "hello") - self.assertEqual(v.adate, date(2011, 1, 2)) + self.assert_(t.type is not tuple) + self.assertEqual(v.anint, 10) + self.assertEqual(v.astring, "hello") + self.assertEqual(v.adate, date(2011, 1, 2)) @skip_if_no_composite def test_empty_string(self): @@ -591,13 +585,7 @@ class AdaptTypeTestCase(ConnectingTestCase): v = curs.fetchone()[0] self.assertEqual(r, v) - - try: - from collections import namedtuple # noqa - except ImportError: - pass - else: - self.assertEqual(v.anotherpair.apair.astring, "hello") + self.assertEqual(v.anotherpair.apair.astring, "hello") @skip_if_no_composite def test_register_on_cursor(self): diff --git a/tests/testutils.py b/tests/testutils.py index 5c192e30..4e1ee37f 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -248,19 +248,6 @@ def skip_if_tpc_disabled(f): return skip_if_tpc_disabled_ -def skip_if_no_namedtuple(f): - @wraps(f) - def skip_if_no_namedtuple_(self): - try: - from collections import namedtuple # noqa - except ImportError: - return self.skipTest("collections.namedtuple not available") - else: - return f(self) - - return skip_if_no_namedtuple_ - - def skip_if_no_iobase(f): """Skip a test if io.TextIOBase is not available.""" @wraps(f) From f7d5d25651ccd875f7fae479da8847c0b3c0c3ea Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 26 Nov 2017 14:32:35 -0800 Subject: [PATCH 21/96] Remove io.TextIOBase workaround for Python <= 2.5 io.TextIOBase is available on all Python versions supported by psycopg2. Can remove all workarounds. --- tests/test_copy.py | 5 +---- tests/testutils.py | 14 -------------- 2 files changed, 1 insertion(+), 18 deletions(-) diff --git a/tests/test_copy.py b/tests/test_copy.py index 96623211..c5e79138 100755 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -25,7 +25,7 @@ import sys import string from testutils import (unittest, ConnectingTestCase, decorate_all_tests, - skip_if_no_iobase, skip_before_postgres, slow) + skip_before_postgres, slow) from cStringIO import StringIO from itertools import cycle, izip from subprocess import Popen, PIPE @@ -131,7 +131,6 @@ class CopyTests(ConnectingTestCase): finally: curs.close() - @skip_if_no_iobase def test_copy_text(self): self.conn.set_client_encoding('latin1') self._create_temp_table() # the above call closed the xn @@ -154,7 +153,6 @@ class CopyTests(ConnectingTestCase): f.seek(0) self.assertEqual(f.readline().rstrip(), about) - @skip_if_no_iobase def test_copy_bytes(self): self.conn.set_client_encoding('latin1') self._create_temp_table() # the above call closed the xn @@ -176,7 +174,6 @@ class CopyTests(ConnectingTestCase): f.seek(0) self.assertEqual(f.readline().rstrip(), about) - @skip_if_no_iobase def test_copy_expert_textiobase(self): self.conn.set_client_encoding('latin1') self._create_temp_table() # the above call closed the xn diff --git a/tests/testutils.py b/tests/testutils.py index 5c192e30..bead6967 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -261,20 +261,6 @@ def skip_if_no_namedtuple(f): return skip_if_no_namedtuple_ -def skip_if_no_iobase(f): - """Skip a test if io.TextIOBase is not available.""" - @wraps(f) - def skip_if_no_iobase_(self): - try: - from io import TextIOBase # noqa - except ImportError: - return self.skipTest("io.TextIOBase not found.") - else: - return f(self) - - return skip_if_no_iobase_ - - def skip_before_postgres(*ver): """Skip a test on PostgreSQL before a certain version.""" ver = ver + (0,) * (3 - len(ver)) From 582ec189cc41491b804f9fb391e99b5720d56d27 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 26 Nov 2017 14:46:53 -0800 Subject: [PATCH 22/96] Remove uuid workaround for older Pythons uuid is available on all Python versions supported by psycopg2. --- tests/testutils.py | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/testutils.py b/tests/testutils.py index 5c192e30..033aba85 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -203,11 +203,6 @@ def skip_if_no_uuid(f): """Decorator to skip a test if uuid is not supported by Py/PG.""" @wraps(f) def skip_if_no_uuid_(self): - try: - import uuid # noqa - except ImportError: - return self.skipTest("uuid not available in this Python version") - try: cur = self.conn.cursor() cur.execute("select typname from pg_type where typname = 'uuid'") From dfc9932f27b003c8cd428f29c1910bf17902bd81 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 26 Nov 2017 16:43:18 -0800 Subject: [PATCH 23/96] Remove use of skip_before_python for unsupported Python versions psycopg2 does not support Python < 2.6, remove all test guards for these versions. --- tests/test_module.py | 4 +--- tests/test_types_basic.py | 1 - 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/tests/test_module.py b/tests/test_module.py index d2b13673..4a4941c1 100755 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -26,7 +26,7 @@ import os import sys from subprocess import Popen -from testutils import (unittest, skip_before_python, skip_before_postgres, +from testutils import (unittest, skip_before_postgres, ConnectingTestCase, skip_copy_if_green, script_to_py3, slow) import psycopg2 @@ -276,7 +276,6 @@ class ExceptionsTestCase(ConnectingTestCase): self.assertEqual(e.diag.constraint_name, "chk_eq1") self.assertEqual(e.diag.datatype_name, None) - @skip_before_python(2, 5) def test_pickle(self): import pickle cur = self.conn.cursor() @@ -291,7 +290,6 @@ class ExceptionsTestCase(ConnectingTestCase): self.assertEqual(e.pgcode, e1.pgcode) self.assert_(e1.cursor is None) - @skip_before_python(2, 5) def test_pickle_connection_error(self): # segfaults on psycopg 2.5.1 - see ticket #170 import pickle diff --git a/tests/test_types_basic.py b/tests/test_types_basic.py index 94868583..8d624c1a 100755 --- a/tests/test_types_basic.py +++ b/tests/test_types_basic.py @@ -272,7 +272,6 @@ class TypesBasicTests(ConnectingTestCase): o2 = self.execute("select %s;", (o1,)) self.assertEqual(memoryview, type(o2[0])) - @testutils.skip_before_python(2, 6) def testAdaptBytearray(self): o1 = bytearray(range(256)) o2 = self.execute("select %s;", (o1,)) From d58844e5483483240f97537e9a77b4e79cea2ab3 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 26 Nov 2017 17:11:27 -0800 Subject: [PATCH 24/96] Clean up JSON workarounds for unsupported Python versions All Python versions supported by psycopg2 have the json module. It was added in Python 2.6. Can remove checks for availability, slightly simplifying the code. --- lib/_json.py | 32 ++++---------------------------- 1 file changed, 4 insertions(+), 28 deletions(-) diff --git a/lib/_json.py b/lib/_json.py index 92a9def7..cd2e7e1b 100644 --- a/lib/_json.py +++ b/lib/_json.py @@ -27,22 +27,13 @@ extensions importing register_json from extras. # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License for more details. +import json import sys from psycopg2._psycopg import ISQLQuote, QuotedString from psycopg2._psycopg import new_type, new_array_type, register_type -# import the best json implementation available -if sys.version_info[:2] >= (2, 6): - import json -else: - try: - import simplejson as json - except ImportError: - json = None - - # oids from PostgreSQL 9.2 JSON_OID = 114 JSONARRAY_OID = 199 @@ -67,13 +58,7 @@ class Json(object): def __init__(self, adapted, dumps=None): self.adapted = adapted self._conn = None - - if dumps is not None: - self._dumps = dumps - elif json is not None: - self._dumps = json.dumps - else: - self._dumps = None + self._dumps = dumps or json.dumps def __conform__(self, proto): if proto is ISQLQuote: @@ -86,13 +71,7 @@ class Json(object): provided in the constructor. You can override this method to create a customized JSON wrapper. """ - dumps = self._dumps - if dumps is not None: - return dumps(obj) - else: - raise ImportError( - "json module not available: " - "you should provide a dumps function") + return self._dumps(obj) def prepare(self, conn): self._conn = conn @@ -181,10 +160,7 @@ def register_default_jsonb(conn_or_curs=None, globally=False, loads=None): def _create_json_typecasters(oid, array_oid, loads=None, name='JSON'): """Create typecasters for json data type.""" if loads is None: - if json is None: - raise ImportError("no json module available") - else: - loads = json.loads + loads = json.loads def typecast_json(s, cur): if s is None: From 048f1bb95a8ad3863cdd02cd25a2786c48c8d91f Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 26 Nov 2017 17:55:24 -0800 Subject: [PATCH 25/96] Remove workaround for decimal module The decimal module is available on all Python versions supported by psycopg2. It has been available since Python 2.4. No need to catch an ImportError. https://docs.python.org/2/library/decimal.html --- lib/__init__.py | 12 ++++-------- tests/test_cursor.py | 14 +++----------- 2 files changed, 7 insertions(+), 19 deletions(-) diff --git a/lib/__init__.py b/lib/__init__.py index f4d4fc79..cbec98fd 100644 --- a/lib/__init__.py +++ b/lib/__init__.py @@ -72,14 +72,10 @@ _ext.register_adapter(type(None), _ext.NoneAdapter) # Register the Decimal adapter here instead of in the C layer. # This way a new class is registered for each sub-interpreter. # See ticket #52 -try: - from decimal import Decimal -except ImportError: - pass -else: - from psycopg2._psycopg import Decimal as Adapter - _ext.register_adapter(Decimal, Adapter) - del Decimal, Adapter +from decimal import Decimal +from psycopg2._psycopg import Decimal as Adapter +_ext.register_adapter(Decimal, Adapter) +del Decimal, Adapter def connect(dsn=None, connection_factory=None, cursor_factory=None, **kwargs): diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 10b8d71e..7faaca63 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -98,11 +98,7 @@ class CursorTests(ConnectingTestCase): cur.mogrify(u"SELECT %s;", (snowman,))) def test_mogrify_decimal_explodes(self): - # issue #7: explodes on windows with python 2.5 and psycopg 2.2.2 - try: - from decimal import Decimal - except: - return + from decimal import Decimal conn = self.conn cur = conn.cursor() @@ -138,12 +134,8 @@ class CursorTests(ConnectingTestCase): self.assertEqual(42, curs.cast(20, '42')) self.assertAlmostEqual(3.14, curs.cast(700, '3.14')) - try: - from decimal import Decimal - except ImportError: - self.assertAlmostEqual(123.45, curs.cast(1700, '123.45')) - else: - self.assertEqual(Decimal('123.45'), curs.cast(1700, '123.45')) + from decimal import Decimal + self.assertEqual(Decimal('123.45'), curs.cast(1700, '123.45')) from datetime import date self.assertEqual(date(2011, 1, 2), curs.cast(1082, '2011-01-02')) From 296abf735ee236da7dcd78866156cdb0c1d3137e Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 26 Nov 2017 10:51:48 -0800 Subject: [PATCH 26/96] Add some missing trove classifiers for general Python support --- setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setup.py b/setup.py index 67af9db0..d1218a58 100644 --- a/setup.py +++ b/setup.py @@ -75,6 +75,7 @@ Intended Audience :: Developers License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL) License :: OSI Approved :: Zope Public License Programming Language :: Python +Programming Language :: Python :: 2 Programming Language :: Python :: 2.6 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 @@ -83,6 +84,7 @@ Programming Language :: Python :: 3.3 Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 +Programming Language :: Python :: Implementation :: CPython Programming Language :: C Programming Language :: SQL Topic :: Database From dda55dbf368f34c19833bc508c9faf6bbbcf0e87 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 28 Nov 2017 03:00:33 +0000 Subject: [PATCH 27/96] Moving to next major version on master Opened maintenance branch `maint_2_7` --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index d1218a58..ac597600 100644 --- a/setup.py +++ b/setup.py @@ -64,7 +64,7 @@ except ImportError: # Take a look at http://www.python.org/dev/peps/pep-0440/ # for a consistent versioning pattern. -PSYCOPG_VERSION = '2.7.4.dev0' +PSYCOPG_VERSION = '2.8.dev0' # note: if you are changing the list of supported Python version please fix From a1831ef4982b05ac847c30a89a402a46f838a18c Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 28 Nov 2017 03:13:25 +0000 Subject: [PATCH 28/96] Mention solaris support in NEWS Close #532. --- NEWS | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NEWS b/NEWS index 799477ec..227734b3 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,12 @@ Current release --------------- +What's new in psycopg 2.7.4 +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Fixed Solaris 10 support (:ticket:`#532`). + + What's new in psycopg 2.7.3.2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ From b4f066bd43545a96ec98c94a4460d2b5372379a7 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 28 Nov 2017 03:22:53 +0000 Subject: [PATCH 29/96] Mention MinTimeLoggingCursor fix in NEWS file --- NEWS | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/NEWS b/NEWS index 799477ec..0c3ebe5f 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,12 @@ Current release --------------- +What's new in psycopg 2.7.4 +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Fixed `MinTimeLoggingCursor` on Python 3 (:ticket:`#609`). + + What's new in psycopg 2.7.3.2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ From 4c95668c720d2314d89f7a94e782611e1dfb768f Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Tue, 28 Nov 2017 06:03:46 -0800 Subject: [PATCH 30/96] Remove "from __future__ import with_statement" All versions of Python supported by psycopg2 have builtin support for the with statement. The import is unnecessary noise. --- tests/test_types_extras.py | 1 - tests/test_with.py | 3 --- 2 files changed, 4 deletions(-) diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index a5aa7d2b..6d842d80 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -13,7 +13,6 @@ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License for more details. -from __future__ import with_statement import re import sys diff --git a/tests/test_with.py b/tests/test_with.py index e5c0bc23..53dfa464 100755 --- a/tests/test_with.py +++ b/tests/test_with.py @@ -22,9 +22,6 @@ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License for more details. - -from __future__ import with_statement - import psycopg2 import psycopg2.extensions as ext From c1d3948be90742560df6784e11706773c062c562 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Tue, 28 Nov 2017 06:38:44 -0800 Subject: [PATCH 31/96] Update documentation to reflect JSON import behavior The docs don't need to describe what will happen on Python versions before 2.6 as they are unsupported by psycopg2. Should have been included in commit d58844e5483483240f97537e9a77b4e79cea2ab3, but was missed. --- doc/src/extras.rst | 8 ++------ lib/_json.py | 6 ++---- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/doc/src/extras.rst b/doc/src/extras.rst index cb8335be..1342f13c 100644 --- a/doc/src/extras.rst +++ b/doc/src/extras.rst @@ -539,17 +539,13 @@ fields to JSON) you can use the `register_json()` function. .. __: http://people.planetpostgresql.org/andrew/index.php?/archives/255-JSON-for-PG-9.2-...-and-now-for-9.1!.html -The Python library used by default to convert Python objects to JSON and to -parse data from the database depends on the language version: with Python 2.6 -and following the :py:mod:`json` module from the standard library is used; -with previous versions the `simplejson`_ module is used if available. Note -that the last `!simplejson` version supporting Python 2.4 is the 2.0.9. +The Python :py:mod:`json` module is used by default to convert Python objects +to JSON and to parse data from the database. .. _JSON: http://www.json.org/ .. |pgjson| replace:: :sql:`json` .. |jsonb| replace:: :sql:`jsonb` .. _pgjson: http://www.postgresql.org/docs/current/static/datatype-json.html -.. _simplejson: http://pypi.python.org/pypi/simplejson/ In order to pass a Python object to the database as query argument you can use the `Json` adapter:: diff --git a/lib/_json.py b/lib/_json.py index cd2e7e1b..30910eba 100644 --- a/lib/_json.py +++ b/lib/_json.py @@ -49,10 +49,8 @@ class Json(object): :sql:`json` data type. `!Json` can be used to wrap any object supported by the provided *dumps* - function. If none is provided, the standard :py:func:`json.dumps()` is - used (`!simplejson` for Python < 2.6; - `~psycopg2.extensions.ISQLQuote.getquoted()` will raise `!ImportError` if - the module is not available). + function. If none is provided, the standard :py:func:`json.dumps()` is + used. """ def __init__(self, adapted, dumps=None): From ffcc65d4f04190551de5c71c6f106cb92f9235ab Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 28 Nov 2017 10:11:42 +0200 Subject: [PATCH 32/96] Drop support for EOL Python 2.6 --- .gitignore | 1 + .travis.yml | 1 - doc/src/install.rst | 2 +- psycopg/python.h | 12 ++---------- setup.py | 20 ++++---------------- tests/test_connection.py | 3 +-- tests/test_sql.py | 2 -- tests/test_types_basic.py | 1 - tox.ini | 2 +- 9 files changed, 10 insertions(+), 34 deletions(-) diff --git a/.gitignore b/.gitignore index 409bb3a7..d78118b8 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ doc/html/* doc/psycopg2.txt scripts/pypi_docs_upload.py env +.idea .tox /rel /wheels diff --git a/.travis.yml b/.travis.yml index 4d558f17..6152a4bc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,6 @@ language: python python: - 2.7 - 3.6 - - 2.6 - 3.5 - 3.4 - 3.3 diff --git a/doc/src/install.rst b/doc/src/install.rst index 2993c7cb..c2c245a9 100644 --- a/doc/src/install.rst +++ b/doc/src/install.rst @@ -17,7 +17,7 @@ The current `!psycopg2` implementation supports: .. NOTE: keep consistent with setup.py and the /features/ page. -- Python 2 versions from 2.6 to 2.7 +- Python version 2.7 - Python 3 versions from 3.2 to 3.6 - PostgreSQL server versions from 7.4 to 10 - PostgreSQL client library version from 9.1 diff --git a/psycopg/python.h b/psycopg/python.h index cfb8dad3..b16c4b9d 100644 --- a/psycopg/python.h +++ b/psycopg/python.h @@ -31,8 +31,8 @@ #include #endif -#if PY_VERSION_HEX < 0x02060000 -# error "psycopg requires Python >= 2.6" +#if PY_VERSION_HEX < 0x02070000 +# error "psycopg requires Python >= 2.7" #endif /* hash() return size changed around version 3.2a4 on 64bit platforms. Before @@ -44,14 +44,6 @@ typedef long Py_hash_t; typedef unsigned long Py_uhash_t; #endif -/* Macros defined in Python 2.6 */ -#ifndef Py_REFCNT -#define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt) -#define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) -#define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size) -#define PyVarObject_HEAD_INIT(x,n) PyObject_HEAD_INIT(x) n, -#endif - /* FORMAT_CODE_PY_SSIZE_T is for Py_ssize_t: */ #define FORMAT_CODE_PY_SSIZE_T "%" PY_FORMAT_SIZE_T "d" diff --git a/setup.py b/setup.py index 6ffae80a..f010ef5b 100644 --- a/setup.py +++ b/setup.py @@ -76,7 +76,6 @@ License :: OSI Approved :: GNU Library or Lesser General Public License (LGPL) License :: OSI Approved :: Zope Public License Programming Language :: Python Programming Language :: Python :: 2 -Programming Language :: Python :: 2.6 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 Programming Language :: Python :: 3.2 @@ -297,7 +296,7 @@ class psycopg_build_ext(build_ext): # For Python versions that use MSVC compiler 2008, re-insert the # manifest into the resulting .pyd file. - if self.compiler_is_msvc() and sysVer in ((2, 6), (2, 7), (3, 0), (3, 1), (3, 2)): + if self.compiler_is_msvc() and sysVer in ((2, 7), (3, 0), (3, 1), (3, 2)): platform = get_platform() # Default to the x86 manifest manifest = '_psycopg.vc9.x86.manifest' @@ -319,7 +318,6 @@ class psycopg_build_ext(build_ext): def finalize_win32(self): """Finalize build system configuration on win32 platform.""" - sysVer = sys.version_info[:2] # Add compiler-specific arguments: extra_compiler_args = [] @@ -335,17 +333,6 @@ class psycopg_build_ext(build_ext): # API code. extra_compiler_args.append('-fno-strict-aliasing') - # Force correct C runtime library linkage: - if sysVer <= (2, 3): - # Yes: 'msvcr60', rather than 'msvcrt', is the correct value - # on the line below: - self.libraries.append('msvcr60') - elif sysVer in ((2, 4), (2, 5)): - self.libraries.append('msvcr71') - # Beyond Python 2.5, we take our chances on the default C runtime - # library, because we don't know what compiler those future - # versions of Python will use. - for extension in ext: # ext is a global list of Extension objects extension.extra_compile_args.extend(extra_compiler_args) # End of add-compiler-specific arguments section. @@ -414,7 +401,7 @@ class psycopg_build_ext(build_ext): # *at least* PostgreSQL 7.4 is available (this is the only # 7.x series supported by psycopg 2) pgversion = pg_config_helper.query("version").split()[1] - except: + except Exception: pgversion = "7.4.0" verre = re.compile( @@ -619,7 +606,7 @@ try: f = open("README.rst") readme = f.read() f.close() -except: +except Exception: print("failed to read readme: ignoring...") readme = __doc__ @@ -633,6 +620,7 @@ setup(name="psycopg2", download_url=download_url, license="LGPL with exceptions or ZPL", platforms=["any"], + python_requires='>=2.7,!=3.0.*,!=3.1.*', description=readme.split("\n")[0], long_description="\n".join(readme.split("\n")[2:]).lstrip(), classifiers=[x for x in classifiers.split("\n") if x], diff --git a/tests/test_connection.py b/tests/test_connection.py index eff10650..f13df18d 100755 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -352,8 +352,7 @@ class ParseDsnTestCase(ConnectingTestCase): "DSN with quoting parsed") # Can't really use assertRaisesRegexp() here since we need to - # make sure that secret is *not* exposed in the error messgage - # (and it also requires python >= 2.7). + # make sure that secret is *not* exposed in the error message. raised = False try: # unterminated quote after dbname: diff --git a/tests/test_sql.py b/tests/test_sql.py index e35bf32d..2e12ba63 100755 --- a/tests/test_sql.py +++ b/tests/test_sql.py @@ -32,7 +32,6 @@ from psycopg2 import sql class SqlFormatTests(ConnectingTestCase): - @skip_before_python(2, 7) def test_pos(self): s = sql.SQL("select {} from {}").format( sql.Identifier('field'), sql.Identifier('table')) @@ -91,7 +90,6 @@ class SqlFormatTests(ConnectingTestCase): def test_compose_badnargs(self): self.assertRaises(IndexError, sql.SQL("select {0};").format) - @skip_before_python(2, 7) def test_compose_badnargs_auto(self): self.assertRaises(IndexError, sql.SQL("select {};").format) self.assertRaises(ValueError, sql.SQL("select {} {1};").format, 10, 20) diff --git a/tests/test_types_basic.py b/tests/test_types_basic.py index 8d624c1a..b0af6daa 100755 --- a/tests/test_types_basic.py +++ b/tests/test_types_basic.py @@ -295,7 +295,6 @@ class TypesBasicTests(ConnectingTestCase): else: self.assertEqual(memoryview, type(o2)) - @testutils.skip_before_python(2, 7) def testAdaptMemoryview(self): o1 = memoryview(bytearray(range(256))) o2 = self.execute("select %s;", (o1,)) diff --git a/tox.ini b/tox.ini index 4a1129d5..17612e25 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ # and then run "tox" from this directory. [tox] -envlist = py26, py27 +envlist = py27 [testenv] commands = make check From 53c1c5dcc1da6aa36ee9f53c5276739053b85897 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 28 Nov 2017 10:22:02 +0200 Subject: [PATCH 33/96] Remove redundant hasattr checks --- setup.py | 3 +-- tests/testutils.py | 33 ++------------------------------- 2 files changed, 3 insertions(+), 33 deletions(-) diff --git a/setup.py b/setup.py index f010ef5b..efcf81d8 100644 --- a/setup.py +++ b/setup.py @@ -214,8 +214,7 @@ or with the pg_config option in 'setup.cfg'. # Support unicode paths, if this version of Python provides the # necessary infrastructure: - if sys.version_info[0] < 3 \ - and hasattr(sys, 'getfilesystemencoding'): + if sys.version_info[0] < 3: pg_config_path = pg_config_path.encode( sys.getfilesystemencoding()) diff --git a/tests/testutils.py b/tests/testutils.py index ba31e3b0..03401891 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -32,37 +32,8 @@ from functools import wraps from testconfig import dsn, repl_dsn -if hasattr(unittest, 'skipIf'): - skip = unittest.skip - skipIf = unittest.skipIf - -else: - import warnings - - def skipIf(cond, msg): - def skipIf_(f): - @wraps(f) - def skipIf__(self): - if cond: - with warnings.catch_warnings(): - warnings.simplefilter('always', UserWarning) - warnings.warn(msg) - return - else: - return f(self) - return skipIf__ - return skipIf_ - - def skip(msg): - return skipIf(True, msg) - - def skipTest(self, msg): - with warnings.catch_warnings(): - warnings.simplefilter('always', UserWarning) - warnings.warn(msg) - return - - unittest.TestCase.skipTest = skipTest +skip = unittest.skip +skipIf = unittest.skipIf # Silence warnings caused by the stubbornness of the Python unittest # maintainers From 2f3c233f389fc895f65cca8168b2dd6da016a8bb Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 28 Nov 2017 10:25:09 +0200 Subject: [PATCH 34/96] Simplify Boolean --- sandbox/pbool.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sandbox/pbool.py b/sandbox/pbool.py index 35ca760c..817e3ea0 100644 --- a/sandbox/pbool.py +++ b/sandbox/pbool.py @@ -6,7 +6,7 @@ class B(object): print "ga called", attr return object.__getattribute__(self, attr) def _sqlquote(self): - if self._o == True: + if self._o: return 'It is True' else: return 'It is False' From b69457ccdf072b20797689fe8b03a318769a96fb Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 28 Nov 2017 10:30:15 +0200 Subject: [PATCH 35/96] Update to Exception as e, print() --- doc/src/errorcodes.rst | 2 +- examples/copy_from.py | 44 +++++++++++++++++++++--------------------- examples/cursor.py | 10 +++++----- examples/threads.py | 18 ++++++++--------- 4 files changed, 37 insertions(+), 37 deletions(-) diff --git a/doc/src/errorcodes.rst b/doc/src/errorcodes.rst index f5f2fa8a..ab49afb6 100644 --- a/doc/src/errorcodes.rst +++ b/doc/src/errorcodes.rst @@ -59,7 +59,7 @@ between 8.1 and 10 are included in the module. >>> try: ... cur.execute("SELECT ouch FROM aargh;") - ... except Exception, e: + ... except Exception as e: ... pass ... >>> errorcodes.lookup(e.pgcode[:2]) diff --git a/examples/copy_from.py b/examples/copy_from.py index fed6bf15..e258a077 100644 --- a/examples/copy_from.py +++ b/examples/copy_from.py @@ -27,9 +27,9 @@ import psycopg2 if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) -print "Encoding for this connection is", conn.encoding +print("Encoding for this connection is", conn.encoding) curs = conn.cursor() try: @@ -51,16 +51,16 @@ io.close() io = open('copy_from.txt', 'r') curs.copy_from(io, 'test_copy') -print "1) Copy %d records from file object " % len(data) + \ - "using defaults (sep: \\t and null = \\N)" +print("1) Copy %d records from file object " % len(data) + + "using defaults (sep: \\t and null = \\N)") io.close() curs.execute("SELECT * FROM test_copy") rows = curs.fetchall() -print " Select returned %d rows" % len(rows) +print(" Select returned %d rows" % len(rows)) for r in rows: - print " %s %s\t%s" % (r[0], r[1], r[2]) + print(" %s %s\t%s" % (r[0], r[1], r[2])) curs.execute("delete from test_copy") conn.commit() @@ -75,15 +75,15 @@ io.close() io = open('copy_from.txt', 'r') curs.copy_from(io, 'test_copy', ':') -print "2) Copy %d records from file object using sep = :" % len(data) +print("2) Copy %d records from file object using sep = :" % len(data)) io.close() curs.execute("SELECT * FROM test_copy") rows = curs.fetchall() -print " Select returned %d rows" % len(rows) +print(" Select returned %d rows" % len(rows)) for r in rows: - print " %s %s\t%s" % (r[0], r[1], r[2]) + print(" %s %s\t%s" % (r[0], r[1], r[2])) curs.execute("delete from test_copy") conn.commit() @@ -98,15 +98,15 @@ io.close() io = open('copy_from.txt', 'r') curs.copy_from(io, 'test_copy', null='NULL') -print "3) Copy %d records from file object using null = NULL" % len(data) +print("3) Copy %d records from file object using null = NULL" % len(data)) io.close() curs.execute("SELECT * FROM test_copy") rows = curs.fetchall() -print " Select using cursor returned %d rows" % len(rows) +print(" Select using cursor returned %d rows" % len(rows)) for r in rows: - print " %s %s\t%s" % (r[0], r[1], r[2]) + print(" %s %s\t%s" % (r[0], r[1], r[2])) curs.execute("delete from test_copy") conn.commit() @@ -119,16 +119,16 @@ io.close() io = open('copy_from.txt', 'r') curs.copy_from(io, 'test_copy', ':', 'NULL') -print "4) Copy %d records from file object " % len(data) + \ - "using sep = : and null = NULL" +print("4) Copy %d records from file object " % len(data) + + "using sep = : and null = NULL") io.close() curs.execute("SELECT * FROM test_copy") rows = curs.fetchall() -print " Select using cursor returned %d rows" % len(rows) +print(" Select using cursor returned %d rows" % len(rows)) for r in rows: - print " %s %s\t%s" % (r[0], r[1], r[2]) + print(" %s %s\t%s" % (r[0], r[1], r[2])) curs.execute("delete from test_copy") conn.commit() @@ -141,20 +141,20 @@ data.write('\n'.join(['Tom\tJenkins\t37', data.seek(0) curs.copy_from(data, 'test_copy') -print "5) Copy 3 records from StringIO object using defaults" +print("5) Copy 3 records from StringIO object using defaults") curs.execute("SELECT * FROM test_copy") rows = curs.fetchall() -print " Select using cursor returned %d rows" % len(rows) +print(" Select using cursor returned %d rows" % len(rows)) for r in rows: - print " %s %s\t%s" % (r[0], r[1], r[2]) + print(" %s %s\t%s" % (r[0], r[1], r[2])) curs.execute("delete from test_copy") conn.commit() # simple error test -print "6) About to raise an error" +print("6) About to raise an error") data = StringIO.StringIO() data.write('\n'.join(['Tom\tJenkins\t37', 'Madonna\t\N\t45', @@ -163,9 +163,9 @@ data.seek(0) try: curs.copy_from(data, 'test_copy') -except StandardError, err: +except StandardError as err: conn.rollback() - print " Caught error (as expected):\n", err + print(" Caught error (as expected):\n", err) conn.rollback() diff --git a/examples/cursor.py b/examples/cursor.py index 2d56fd73..465967c3 100644 --- a/examples/cursor.py +++ b/examples/cursor.py @@ -25,9 +25,9 @@ import psycopg2.extensions if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) -print "Encoding for this connection is", conn.encoding +print("Encoding for this connection is", conn.encoding) class NoDataError(psycopg2.ProgrammingError): @@ -52,12 +52,12 @@ class Cursor(psycopg2.extensions.cursor): curs = conn.cursor(cursor_factory=Cursor) curs.execute("SELECT 1 AS foo") -print "Result of fetchone():", curs.fetchone() +print("Result of fetchone():", curs.fetchone()) # now let's raise the exception try: curs.fetchone() -except NoDataError, err: - print "Exception caught:", err +except NoDataError as err: + print("Exception caught:", err) conn.rollback() diff --git a/examples/threads.py b/examples/threads.py index d73730ce..d24b0a5d 100644 --- a/examples/threads.py +++ b/examples/threads.py @@ -84,7 +84,7 @@ def insert_func(conn_or_pool, rows): try: c.execute("INSERT INTO test_threads VALUES (%s, %s, %s)", (str(i), i, float(i))) - except psycopg2.ProgrammingError, err: + except psycopg2.ProgrammingError as err: print name, ": an error occurred; skipping this insert" print err conn.commit() @@ -112,10 +112,10 @@ def select_func(conn_or_pool, z): if MODE == 1: conn_or_pool.putconn(conn) s = name + ": number of rows fetched: " + str(len(l)) - print s - except psycopg2.ProgrammingError, err: - print name, ": an error occurred; skipping this select" - print err + print(s) + except psycopg2.ProgrammingError as err: + print(name, ": an error occurred; skipping this select") + print(err) ## create the connection pool or the connections if MODE == 0: @@ -129,14 +129,14 @@ else: ## create the threads threads = [] -print "Creating INSERT threads:" +print("Creating INSERT threads:") for name in INSERT_THREADS: t = threading.Thread(None, insert_func, 'Thread-'+name, (conn_insert, ROWS)) t.setDaemon(0) threads.append(t) -print "Creating SELECT threads:" +print("Creating SELECT threads:") for name in SELECT_THREADS: t = threading.Thread(None, select_func, 'Thread-'+name, (conn_select, SELECT_DIV)) @@ -150,12 +150,12 @@ for t in threads: # and wait for them to finish for t in threads: t.join() - print t.getName(), "exited OK" + print(t.getName(), "exited OK") conn.commit() curs.execute("SELECT count(name) FROM test_threads") -print "Inserted", curs.fetchone()[0], "rows." +print("Inserted", curs.fetchone()[0], "rows.") curs.execute("DROP TABLE test_threads") conn.commit() From 955526b200f998ce00d0dadfd8fa8049ac1df284 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 28 Nov 2017 10:31:03 +0200 Subject: [PATCH 36/96] Replace comparison with None with equality operator --- tests/dbapi20.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/dbapi20.py b/tests/dbapi20.py index f707c090..ff98ddc6 100644 --- a/tests/dbapi20.py +++ b/tests/dbapi20.py @@ -762,7 +762,7 @@ class DatabaseAPI20Test(unittest.TestCase): names=cur.fetchall() assert len(names) == len(self.samples) s=cur.nextset() - assert s == None,'No more return sets, should return None' + assert s is None, 'No more return sets, should return None' finally: self.help_nextset_tearDown(cur) From 7282ef0d14f54812aefb47806b949ee7209812b9 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 28 Nov 2017 10:32:04 +0200 Subject: [PATCH 37/96] Rewrite list creation as list literal --- tests/test_types_extras.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index 6d842d80..f50b1b5e 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -365,9 +365,7 @@ class HstoreTestCase(ConnectingTestCase): from psycopg2.extras import register_hstore register_hstore(self.conn) - ds = [] - ds.append({}) - ds.append({'a': 'b', 'c': None}) + ds = [{}, {'a': 'b', 'c': None}] ab = map(chr, range(32, 128)) ds.append(dict(zip(ab, ab))) From 08b479bc105af9ee50ce3d67d7661bd85fc6437f Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 28 Nov 2017 15:40:25 +0200 Subject: [PATCH 38/96] __slots__ should be a tuple --- lib/extras.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/extras.py b/lib/extras.py index 8b430535..67e50b29 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -264,7 +264,7 @@ class RealDictCursor(DictCursorBase): class RealDictRow(dict): """A `!dict` subclass representing a data record.""" - __slots__ = ('_column_mapping') + __slots__ = ('_column_mapping',) def __init__(self, cursor): dict.__init__(self) From 283de270987dbd17a3cf2285da60722d4b5cc001 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 28 Nov 2017 10:32:55 +0200 Subject: [PATCH 39/96] Remove redundant parentheses --- lib/_json.py | 2 +- lib/extras.py | 2 +- lib/tz.py | 2 +- scripts/refcounter.py | 4 ++-- tests/test_lobject.py | 4 ++-- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/_json.py b/lib/_json.py index 30910eba..71c2b76c 100644 --- a/lib/_json.py +++ b/lib/_json.py @@ -194,7 +194,7 @@ def _get_json_oids(conn_or_curs, name='json'): r = curs.fetchone() # revert the status of the connection as before the command - if (conn_status != STATUS_IN_TRANSACTION and not conn.autocommit): + if conn_status != STATUS_IN_TRANSACTION and not conn.autocommit: conn.rollback() if not r: diff --git a/lib/extras.py b/lib/extras.py index 67e50b29..3308f78f 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -280,7 +280,7 @@ class RealDictRow(dict): return dict.__setitem__(self, name, value) def __getstate__(self): - return (self.copy(), self._column_mapping[:]) + return self.copy(), self._column_mapping[:] def __setstate__(self, data): self.update(data[0]) diff --git a/lib/tz.py b/lib/tz.py index 92a16041..d593175c 100644 --- a/lib/tz.py +++ b/lib/tz.py @@ -75,7 +75,7 @@ class FixedOffsetTimezone(datetime.tzinfo): def __getinitargs__(self): offset_mins = self._offset.seconds // 60 + self._offset.days * 24 * 60 - return (offset_mins, self._name) + return offset_mins, self._name def utcoffset(self, dt): return self._offset diff --git a/scripts/refcounter.py b/scripts/refcounter.py index 9e900cf7..305dd1bc 100755 --- a/scripts/refcounter.py +++ b/scripts/refcounter.py @@ -43,7 +43,7 @@ def main(): dump(i, opt) f1 = open('debug-%02d.txt' % (opt.nruns - 1)).readlines() - f2 = open('debug-%02d.txt' % (opt.nruns)).readlines() + f2 = open('debug-%02d.txt' % opt.nruns).readlines() for line in difflib.unified_diff(f1, f2, "run %d" % (opt.nruns - 1), "run %d" % opt.nruns): sys.stdout.write(line) @@ -52,7 +52,7 @@ def main(): if opt.objs: f1 = open('objs-%02d.txt' % (opt.nruns - 1)).readlines() - f2 = open('objs-%02d.txt' % (opt.nruns)).readlines() + f2 = open('objs-%02d.txt' % opt.nruns).readlines() for line in difflib.unified_diff(f1, f2, "run %d" % (opt.nruns - 1), "run %d" % opt.nruns): sys.stdout.write(line) diff --git a/tests/test_lobject.py b/tests/test_lobject.py index cf8a556d..8eafabc9 100755 --- a/tests/test_lobject.py +++ b/tests/test_lobject.py @@ -463,9 +463,9 @@ def _has_lo64(conn): % conn.server_version) if 'lo64' not in psycopg2.__version__: - return (False, "this psycopg build doesn't support the lo64 API") + return False, "this psycopg build doesn't support the lo64 API" - return (True, "this server and build support the lo64 API") + return True, "this server and build support the lo64 API" def skip_if_no_lo64(f): From ea76504cd1f8c32c0eb15d337d5e6b44af1cb301 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 28 Nov 2017 10:33:31 +0200 Subject: [PATCH 40/96] Remove trailing semicolons --- sandbox/async.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sandbox/async.py b/sandbox/async.py index 17e96d01..3766fbb9 100644 --- a/sandbox/async.py +++ b/sandbox/async.py @@ -22,15 +22,15 @@ def sleep(curs): # DECLARE zz INSENSITIVE SCROLL CURSOR WITH HOLD FOR # SELECT now(); # FOR READ ONLY;""", async = 1) -curs.execute("SELECT now() AS foo", async=1); +curs.execute("SELECT now() AS foo", async=1) sleep(curs) print curs.fetchall() #curs.execute(""" # FETCH FORWARD 1 FROM zz;""", async = 1) -curs.execute("SELECT now() AS bar", async=1); +curs.execute("SELECT now() AS bar", async=1) print curs.fetchall() -curs.execute("SELECT now() AS bar"); +curs.execute("SELECT now() AS bar") sleep(curs) From c2d082e896e7bcb81231603404e7d4789e56cf00 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 28 Nov 2017 11:13:38 +0200 Subject: [PATCH 41/96] Drop support for EOL Python 3.0-3.3 --- .appveyor.yml | 11 +---------- .travis.yml | 2 -- doc/src/install.rst | 2 +- setup.py | 6 ++---- 4 files changed, 4 insertions(+), 17 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 4f9a40c5..46f54dfe 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -15,7 +15,7 @@ environment: # http://www.appveyor.com/docs/installed-software#python # Py 2.7 = VS Ver. 9.0 (VS 2008) - # Py 3.3, 3.4 = VS Ver. 10.0 (VS 2010) + # Py 3.4 = VS Ver. 10.0 (VS 2010) # Py 3.5, 3.6 = VS Ver. 14.0 (VS 2015) - PYTHON: C:\Python27-x64 @@ -51,15 +51,6 @@ environment: PYTHON_ARCH: 32 VS_VER: 10.0 - - PYTHON: C:\Python33-x64 - DISTUTILS_USE_SDK: '1' - PYTHON_ARCH: 64 - VS_VER: 10.0 - - - PYTHON: C:\Python33 - PYTHON_ARCH: 32 - VS_VER: 10.0 - PSYCOPG2_TESTDB: psycopg2_test PSYCOPG2_TESTDB_USER: postgres PSYCOPG2_TESTDB_PASSWORD: Password12! diff --git a/.travis.yml b/.travis.yml index 6152a4bc..e6182a75 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,8 +9,6 @@ python: - 3.6 - 3.5 - 3.4 - - 3.3 - - 3.2 install: - python setup.py install diff --git a/doc/src/install.rst b/doc/src/install.rst index c2c245a9..8b6651da 100644 --- a/doc/src/install.rst +++ b/doc/src/install.rst @@ -18,7 +18,7 @@ The current `!psycopg2` implementation supports: NOTE: keep consistent with setup.py and the /features/ page. - Python version 2.7 -- Python 3 versions from 3.2 to 3.6 +- Python 3 versions from 3.4 to 3.6 - PostgreSQL server versions from 7.4 to 10 - PostgreSQL client library version from 9.1 diff --git a/setup.py b/setup.py index efcf81d8..264078a9 100644 --- a/setup.py +++ b/setup.py @@ -78,8 +78,6 @@ Programming Language :: Python Programming Language :: Python :: 2 Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 -Programming Language :: Python :: 3.2 -Programming Language :: Python :: 3.3 Programming Language :: Python :: 3.4 Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 @@ -295,7 +293,7 @@ class psycopg_build_ext(build_ext): # For Python versions that use MSVC compiler 2008, re-insert the # manifest into the resulting .pyd file. - if self.compiler_is_msvc() and sysVer in ((2, 7), (3, 0), (3, 1), (3, 2)): + if self.compiler_is_msvc() and sysVer == (2, 7): platform = get_platform() # Default to the x86 manifest manifest = '_psycopg.vc9.x86.manifest' @@ -619,7 +617,7 @@ setup(name="psycopg2", download_url=download_url, license="LGPL with exceptions or ZPL", platforms=["any"], - python_requires='>=2.7,!=3.0.*,!=3.1.*', + python_requires='>=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*', description=readme.split("\n")[0], long_description="\n".join(readme.split("\n")[2:]).lstrip(), classifiers=[x for x in classifiers.split("\n") if x], From 60b1517c5590ec0addc60d50174d0e96566582e0 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 28 Nov 2017 13:59:54 +0200 Subject: [PATCH 42/96] Add news and update version check --- NEWS | 8 ++++++++ psycopg/python.h | 6 ++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 65f4936a..7d99ed32 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,14 @@ Current release --------------- +What's new in psycopg 2.8 +------------------------- + +Other changes: + +- Dropped support for Python 2.6, 3.2, 3.3. + + What's new in psycopg 2.7.4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/psycopg/python.h b/psycopg/python.h index b16c4b9d..fc8c2fed 100644 --- a/psycopg/python.h +++ b/psycopg/python.h @@ -31,8 +31,10 @@ #include #endif -#if PY_VERSION_HEX < 0x02070000 -# error "psycopg requires Python >= 2.7" +#if ((PY_VERSION_HEX < 0x02070000) \ + || ((PY_VERSION_HEX >= 0x03000000) \ + && (PY_VERSION_HEX < 0x03040000)) ) +# error "psycopg requires Python 2.7 or 3.4+" #endif /* hash() return size changed around version 3.2a4 on 64bit platforms. Before From a0229cff8252c07eddf22626a65e3f2967e2b037 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 28 Nov 2017 15:38:27 +0000 Subject: [PATCH 43/96] Documentation tweaked to omit Python 2.6 distinctions --- doc/src/usage.rst | 14 ++++++-------- lib/sql.py | 12 ++++++------ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/doc/src/usage.rst b/doc/src/usage.rst index 79823b11..5dcab8fe 100644 --- a/doc/src/usage.rst +++ b/doc/src/usage.rst @@ -457,14 +457,12 @@ the connection or globally: see the function Binary adaptation ^^^^^^^^^^^^^^^^^ -Python types representing binary objects are converted into -PostgreSQL binary string syntax, suitable for :sql:`bytea` fields. Such -types are `buffer` (only available in Python 2), `memoryview` (available -from Python 2.7), `bytearray` (available from Python 2.6) and `bytes` -(only from Python 3: the name is available from Python 2.6 but it's only an -alias for the type `!str`). Any object implementing the `Revised Buffer -Protocol`__ should be usable as binary type where the protocol is supported -(i.e. from Python 2.6). Received data is returned as `!buffer` (in Python 2) +Python types representing binary objects are converted into PostgreSQL binary +string syntax, suitable for :sql:`bytea` fields. Such types are `buffer` +(only available in Python 2), `memoryview`, `bytearray`, and `bytes` (only in +Python 3: the name is available in Python 2 but it's only an alias for the +type `!str`). Any object implementing the `Revised Buffer Protocol`__ should +be usable as binary type. Received data is returned as `!buffer` (in Python 2) or `!memoryview` (in Python 3). .. __: http://www.python.org/dev/peps/pep-3118/ diff --git a/lib/sql.py b/lib/sql.py index d89e1176..5dfe7c8b 100644 --- a/lib/sql.py +++ b/lib/sql.py @@ -203,12 +203,12 @@ class SQL(Composable): :rtype: `Composed` The method is similar to the Python `str.format()` method: the string - template supports auto-numbered (``{}``, only available from Python - 2.7), numbered (``{0}``, ``{1}``...), and named placeholders - (``{name}``), with positional arguments replacing the numbered - placeholders and keywords replacing the named ones. However placeholder - modifiers (``{0!r}``, ``{0:<10}``) are not supported. Only - `!Composable` objects can be passed to the template. + template supports auto-numbered (``{}``), numbered (``{0}``, + ``{1}``...), and named placeholders (``{name}``), with positional + arguments replacing the numbered placeholders and keywords replacing + the named ones. However placeholder modifiers (``{0!r}``, ``{0:<10}``) + are not supported. Only `!Composable` objects can be passed to the + template. Example:: From c3ee9cac41a7703b3f75049403621edd0702c330 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 28 Nov 2017 15:38:56 +0000 Subject: [PATCH 44/96] Dropped unused test functions --- tests/testutils.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/testutils.py b/tests/testutils.py index 03401891..bd08d6c7 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -32,9 +32,6 @@ from functools import wraps from testconfig import dsn, repl_dsn -skip = unittest.skip -skipIf = unittest.skipIf - # Silence warnings caused by the stubbornness of the Python unittest # maintainers # http://bugs.python.org/issue9424 From f939f39580c46e1601e2f7c491e2afa9fdabbfbe Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 28 Nov 2017 15:47:35 +0000 Subject: [PATCH 45/96] Use dict comprehensions --- lib/_range.py | 7 ++----- lib/extensions.py | 2 +- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/lib/_range.py b/lib/_range.py index ee9c329e..c1facc0f 100644 --- a/lib/_range.py +++ b/lib/_range.py @@ -181,11 +181,8 @@ class Range(object): return self.__gt__(other) def __getstate__(self): - return dict( - (slot, getattr(self, slot)) - for slot in self.__slots__ - if hasattr(self, slot) - ) + return {slot: getattr(self, slot) + for slot in self.__slots__ if hasattr(self, slot)} def __setstate__(self, state): for slot, value in state.items(): diff --git a/lib/extensions.py b/lib/extensions.py index 91b81331..d15f76c9 100644 --- a/lib/extensions.py +++ b/lib/extensions.py @@ -163,7 +163,7 @@ def make_dsn(dsn=None, **kwargs): kwargs['dbname'] = kwargs.pop('database') # Drop the None arguments - kwargs = dict((k, v) for (k, v) in kwargs.iteritems() if v is not None) + kwargs = {k: v for (k, v) in kwargs.iteritems() if v is not None} if dsn is not None: tmp = parse_dsn(dsn) From 87da2f898def201484772089e904d0053bddc873 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 28 Nov 2017 03:33:23 +0000 Subject: [PATCH 46/96] Fixed __libpq_version__ for Postgres >= 10.1 The version should be considered as 10.0.1; the number was generated as 10.1.0 instead. Version number bumped to test building new wheels packages. Fix #632. --- NEWS | 4 +++- setup.py | 8 +++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/NEWS b/NEWS index 7d99ed32..c3e225a4 100644 --- a/NEWS +++ b/NEWS @@ -13,8 +13,10 @@ What's new in psycopg 2.7.4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Fixed Solaris 10 support (:ticket:`#532`). -- Fixed `MinTimeLoggingCursor` on Python 3 (:ticket:`#609`). +- Fixed `!MinTimeLoggingCursor` on Python 3 (:ticket:`#609`). - Fixed parsing of array of points as floats (:ticket:`#613`). +- Fixed `~psycopg2.__libpq_version__` building with libpq >= 10.1 + (:ticket:`632`). What's new in psycopg 2.7.3.2 diff --git a/setup.py b/setup.py index 264078a9..e6144af9 100644 --- a/setup.py +++ b/setup.py @@ -194,8 +194,7 @@ or with the pg_config option in 'setup.cfg'. return None pg_first_inst_key = winreg.OpenKey(reg, - 'SOFTWARE\\PostgreSQL\\Installations\\' - + first_sub_key_name) + 'SOFTWARE\\PostgreSQL\\Installations\\' + first_sub_key_name) try: pg_inst_base_dir = winreg.QueryValueEx( pg_first_inst_key, 'Base Directory')[0] @@ -406,11 +405,14 @@ class psycopg_build_ext(build_ext): m = verre.match(pgversion) if m: pgmajor, pgminor, pgpatch = m.group(1, 2, 3) + # Postgres >= 10 doesn't have pgminor anymore. + pgmajor = int(pgmajor) + if pgmajor >= 10: + pgminor, pgpatch = None, pgminor if pgminor is None or not pgminor.isdigit(): pgminor = 0 if pgpatch is None or not pgpatch.isdigit(): pgpatch = 0 - pgmajor = int(pgmajor) pgminor = int(pgminor) pgpatch = int(pgpatch) else: From 727b952a32ba1715826d0116e14b3c263a4f2920 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 28 Nov 2017 03:40:47 +0000 Subject: [PATCH 47/96] Build and test packages with libpq 10.1 and OpenSSL 1.0.2m --- .appveyor.yml | 20 ++++++++++---------- NEWS | 1 + scripts/appveyor.cache_rebuild | 4 ++-- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 46f54dfe..44dbdbd6 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -153,8 +153,8 @@ install: } # Download OpenSSL source - CD C:\Others - - IF NOT EXIST OpenSSL_1_0_2l.zip ( - curl -fsSL -o OpenSSL_1_0_2l.zip https://github.com/openssl/openssl/archive/OpenSSL_1_0_2l.zip + - IF NOT EXIST OpenSSL_1_0_2m.zip ( + curl -fsSL -o OpenSSL_1_0_2m.zip https://github.com/openssl/openssl/archive/OpenSSL_1_0_2m.zip ) # To use OpenSSL >= 1.1.0, both libpq and psycopg build environments have @@ -166,15 +166,15 @@ install: # - nmake build_libs install_dev - IF NOT EXIST %OPENSSLTOP%\lib\ssleay32.lib ( CD %BUILD_DIR% && - 7z x C:\Others\OpenSSL_1_0_2l.zip && - CD openssl-OpenSSL_1_0_2l && + 7z x C:\Others\OpenSSL_1_0_2m.zip && + CD openssl-OpenSSL_1_0_2m && perl Configure %TARGET% no-asm no-shared no-zlib --prefix=%OPENSSLTOP% --openssldir=%OPENSSLTOP% && CALL ms\%DO% && nmake -f ms\nt.mak init headers lib && COPY inc32\openssl\*.h %OPENSSLTOP%\include\openssl && COPY out32\*.lib %OPENSSLTOP%\lib && CD %BASE_DIR% && - RMDIR /S /Q %BUILD_DIR%\openssl-OpenSSL_1_0_2l + RMDIR /S /Q %BUILD_DIR%\openssl-OpenSSL_1_0_2m ) # Setup directories for building PostgreSQL librarires @@ -188,8 +188,8 @@ install: # Download PostgreSQL source - CD C:\Others - - IF NOT EXIST postgres-REL_10_0.zip ( - curl -fsSL -o postgres-REL_10_0.zip https://github.com/postgres/postgres/archive/REL_10_0.zip + - IF NOT EXIST postgres-REL_10_1.zip ( + curl -fsSL -o postgres-REL_10_1.zip https://github.com/postgres/postgres/archive/REL_10_1.zip ) # Setup build config file (config.pl) @@ -200,11 +200,11 @@ install: # Prepare local include directory for building from # Build pg_config in place # NOTE: Cannot set and use the same variable inside an IF - - SET PGBUILD=%BUILD_DIR%\postgres-REL_10_0 + - SET PGBUILD=%BUILD_DIR%\postgres-REL_10_1 - IF NOT EXIST %PGTOP%\lib\libpq.lib ( CD %BUILD_DIR% && - 7z x C:\Others\postgres-REL_10_0.zip && - CD postgres-REL_10_0\src\tools\msvc && + 7z x C:\Others\postgres-REL_10_1.zip && + CD postgres-REL_10_1\src\tools\msvc && ECHO $config-^>{ldap} = 0; > config.pl && ECHO $config-^>{openssl} = "%OPENSSLTOP:\=\\%"; >> config.pl && ECHO.>> config.pl && diff --git a/NEWS b/NEWS index c3e225a4..4253b961 100644 --- a/NEWS +++ b/NEWS @@ -17,6 +17,7 @@ What's new in psycopg 2.7.4 - Fixed parsing of array of points as floats (:ticket:`#613`). - Fixed `~psycopg2.__libpq_version__` building with libpq >= 10.1 (:ticket:`632`). +- Wheel packages compiled against PostgreSQL 10.1 libpq and OpenSSL 1.0.2m. What's new in psycopg 2.7.3.2 diff --git a/scripts/appveyor.cache_rebuild b/scripts/appveyor.cache_rebuild index 8ecd97fb..da1b2be5 100644 --- a/scripts/appveyor.cache_rebuild +++ b/scripts/appveyor.cache_rebuild @@ -9,10 +9,10 @@ To invalidate the cache, update this file and check it into git. Currently used modules built in the cache: OpenSSL - Version: 1.0.2l + Version: 1.0.2m PostgreSQL - Version: 10.0 + Version: 10.1 NOTE: to zap the cache manually you can also use: From 65ed5478d126d83a11f69a9e13a141185f4c0f38 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 29 Nov 2017 15:28:10 +0000 Subject: [PATCH 48/96] Collect rowcount in executemany even when discarding results Closes #633. --- NEWS | 4 +++- psycopg/pqpath.c | 3 ++- tests/test_cursor.py | 14 ++++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 4253b961..99df5d9b 100644 --- a/NEWS +++ b/NEWS @@ -13,10 +13,12 @@ What's new in psycopg 2.7.4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Fixed Solaris 10 support (:ticket:`#532`). -- Fixed `!MinTimeLoggingCursor` on Python 3 (:ticket:`#609`). +- Fixed `~psycopg2.extras.MinTimeLoggingCursor` on Python 3 (:ticket:`#609`). - Fixed parsing of array of points as floats (:ticket:`#613`). - Fixed `~psycopg2.__libpq_version__` building with libpq >= 10.1 (:ticket:`632`). +- Fixed `~cursor.rowcount` after `~cursor.executemany()` with :sql:`RETURNING` statements + (:ticket:`633`). - Wheel packages compiled against PostgreSQL 10.1 libpq and OpenSSL 1.0.2m. diff --git a/psycopg/pqpath.c b/psycopg/pqpath.c index a4235d1d..204a6b00 100644 --- a/psycopg/pqpath.c +++ b/psycopg/pqpath.c @@ -1952,8 +1952,9 @@ pq_fetch(cursorObject *curs, int no_result) } else { Dprintf("pq_fetch: got tuples, discarding them"); + /* TODO: is there any case in which PQntuples == PQcmdTuples? */ + _read_rowcount(curs); CLEARPGRES(curs->pgres); - curs->rowcount = -1; ex = 0; } break; diff --git a/tests/test_cursor.py b/tests/test_cursor.py index e97e817b..c5ce70fa 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -583,6 +583,20 @@ class CursorTests(ConnectingTestCase): self.assertEqual(victim_conn.closed, 2) + @skip_before_postgres(8, 2) + def test_rowcount_on_executemany_returning(self): + cur = self.conn.cursor() + cur.execute("create table execmany(id serial primary key, data int)") + cur.executemany( + "insert into execmany (data) values (%s)", + [(i,) for i in range(4)]) + self.assertEqual(cur.rowcount, 4) + + cur.executemany( + "insert into execmany (data) values (%s) returning data", + [(i,) for i in range(5)]) + self.assertEqual(cur.rowcount, 5) + def test_suite(): return unittest.TestLoader().loadTestsFromName(__name__) From a51160317c680662c52e4a1a6b4d11beae37f205 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 1 Dec 2017 16:24:59 +0000 Subject: [PATCH 49/96] Fixed NEWS file entries - 2.6.3 has not been released (yet). Fixes for bug #420, bug #462 were relased in 2.7. - Added missing report for bug #489 fixed in 2.7. --- NEWS | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/NEWS b/NEWS index 99df5d9b..54094e90 100644 --- a/NEWS +++ b/NEWS @@ -128,9 +128,13 @@ New features: Bug fixes: +- Throw an exception trying to pass ``NULL`` chars as parameters + (:ticket:`#420`). - Fixed error caused by missing decoding `~psycopg2.extras.LoggingConnection` (:ticket:`#483`). - Fixed integer overflow in :sql:`interval` seconds (:ticket:`#512`). +- Make `~psycopg2.extras.Range` objects picklable (:ticket:`#462`). +- Fixed version parsing and building with PostgreSQL 10 (:ticket:`#489`). Other changes: @@ -144,14 +148,6 @@ Other changes: (:ticket:`#506`) -What's new in psycopg 2.6.3 -^^^^^^^^^^^^^^^^^^^^^^^^^^^ - -- Throw an exception trying to pass ``NULL`` chars as parameters - (:ticket:`#420`). -- Make `~psycopg2.extras.Range` objects picklable (:ticket:`#462`). - - What's new in psycopg 2.6.2 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ From 6a1546adde1fee3db494cf7aec54ceed4acbc420 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 1 Dec 2017 18:21:50 -0800 Subject: [PATCH 50/96] Add *.egg-info directories to .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index d78118b8..88f3c787 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ MANIFEST *.pidb *.pyc *.sw[po] +*.egg-info/ dist/* build/* doc/src/_build/* From fcc083dc123946039fdfc88c55bbd4902dadd52a Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 1 Dec 2017 17:59:53 -0800 Subject: [PATCH 51/96] Always import the system unittest There is no need to import testutils.unittest instead of simply unittest. They are simple aliases. Use system unittest to be more regular, consistent as well as idiomatic with the wider Python community. --- tests/__init__.py | 2 +- tests/test_async.py | 3 ++- tests/test_async_keyword.py | 3 ++- tests/test_cancel.py | 3 ++- tests/test_copy.py | 3 ++- tests/test_cursor.py | 3 ++- tests/test_dates.py | 3 ++- tests/test_errcodes.py | 3 ++- tests/test_extras_dictcursor.py | 3 ++- tests/test_fast_executemany.py | 2 +- tests/test_ipaddress.py | 2 +- tests/test_lobject.py | 3 ++- tests/test_module.py | 3 ++- tests/test_notify.py | 2 +- tests/test_psycopg2_dbapi20.py | 3 ++- tests/test_quote.py | 3 ++- tests/test_replication.py | 3 ++- tests/test_sql.py | 3 ++- tests/test_transaction.py | 3 ++- tests/test_types_basic.py | 3 ++- tests/test_types_extras.py | 3 ++- tests/test_with.py | 3 ++- 22 files changed, 40 insertions(+), 22 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 6cb6c7ba..fa7e3939 100755 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -29,7 +29,7 @@ warnings.simplefilter('error') # noqa import sys from testconfig import dsn -from testutils import unittest +import unittest import test_async import test_bugX000 diff --git a/tests/test_async.py b/tests/test_async.py index 4eb5e6a2..2c4bfa3b 100755 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -23,7 +23,8 @@ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License for more details. -from testutils import unittest, skip_before_postgres, slow +import unittest +from testutils import skip_before_postgres, slow import psycopg2 from psycopg2 import extensions as ext diff --git a/tests/test_async_keyword.py b/tests/test_async_keyword.py index ddf43266..e08c2750 100755 --- a/tests/test_async_keyword.py +++ b/tests/test_async_keyword.py @@ -29,7 +29,8 @@ import psycopg2 from psycopg2 import extras from testconfig import dsn -from testutils import ConnectingTestCase, unittest, skip_before_postgres, slow +import unittest +from testutils import ConnectingTestCase, skip_before_postgres, slow from test_replication import ReplicationTestCase, skip_repl_if_green from psycopg2.extras import LogicalReplicationConnection, StopReplication diff --git a/tests/test_cancel.py b/tests/test_cancel.py index 529c4ba4..7888bbff 100755 --- a/tests/test_cancel.py +++ b/tests/test_cancel.py @@ -31,7 +31,8 @@ import psycopg2.extensions from psycopg2 import extras from testconfig import dsn -from testutils import unittest, ConnectingTestCase, skip_before_postgres, slow +import unittest +from testutils import ConnectingTestCase, skip_before_postgres, slow class CancelTests(ConnectingTestCase): diff --git a/tests/test_copy.py b/tests/test_copy.py index c5e79138..d52b8026 100755 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -24,7 +24,8 @@ import sys import string -from testutils import (unittest, ConnectingTestCase, decorate_all_tests, +import unittest +from testutils import (ConnectingTestCase, decorate_all_tests, skip_before_postgres, slow) from cStringIO import StringIO from itertools import cycle, izip diff --git a/tests/test_cursor.py b/tests/test_cursor.py index c5ce70fa..1676d0ff 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -26,7 +26,8 @@ import time import pickle import psycopg2 import psycopg2.extensions -from testutils import (unittest, ConnectingTestCase, skip_before_postgres, +import unittest +from testutils import (ConnectingTestCase, skip_before_postgres, skip_if_no_getrefcount, slow, skip_if_no_superuser, skip_if_windows) diff --git a/tests/test_dates.py b/tests/test_dates.py index 9ce74d88..ccfdc20a 100755 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -25,7 +25,8 @@ import math import psycopg2 from psycopg2.tz import FixedOffsetTimezone, ZERO -from testutils import unittest, ConnectingTestCase, skip_before_postgres +import unittest +from testutils import ConnectingTestCase, skip_before_postgres def total_seconds(d): diff --git a/tests/test_errcodes.py b/tests/test_errcodes.py index d651b227..0fdf8e59 100755 --- a/tests/test_errcodes.py +++ b/tests/test_errcodes.py @@ -22,7 +22,8 @@ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License for more details. -from testutils import unittest, ConnectingTestCase, slow +import unittest +from testutils import ConnectingTestCase, slow try: reload diff --git a/tests/test_extras_dictcursor.py b/tests/test_extras_dictcursor.py index 2c867d38..06d8af60 100755 --- a/tests/test_extras_dictcursor.py +++ b/tests/test_extras_dictcursor.py @@ -18,7 +18,8 @@ import time from datetime import timedelta import psycopg2 import psycopg2.extras -from testutils import unittest, ConnectingTestCase, skip_before_postgres +import unittest +from testutils import ConnectingTestCase, skip_before_postgres class ExtrasDictCursorTests(ConnectingTestCase): diff --git a/tests/test_fast_executemany.py b/tests/test_fast_executemany.py index ad7a5b12..32b34545 100755 --- a/tests/test_fast_executemany.py +++ b/tests/test_fast_executemany.py @@ -17,7 +17,7 @@ from datetime import date import testutils -from testutils import unittest +import unittest import psycopg2 import psycopg2.extras diff --git a/tests/test_ipaddress.py b/tests/test_ipaddress.py index bfeaae44..ea193bf8 100755 --- a/tests/test_ipaddress.py +++ b/tests/test_ipaddress.py @@ -20,7 +20,7 @@ import sys from functools import wraps import testutils -from testutils import unittest +import unittest import psycopg2 import psycopg2.extras diff --git a/tests/test_lobject.py b/tests/test_lobject.py index 8eafabc9..7e91fe7a 100755 --- a/tests/test_lobject.py +++ b/tests/test_lobject.py @@ -29,7 +29,8 @@ from functools import wraps import psycopg2 import psycopg2.extensions -from testutils import (unittest, decorate_all_tests, skip_if_tpc_disabled, +import unittest +from testutils import (decorate_all_tests, skip_if_tpc_disabled, ConnectingTestCase, skip_if_green, slow) diff --git a/tests/test_module.py b/tests/test_module.py index 4a4941c1..13391d1a 100755 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -26,7 +26,8 @@ import os import sys from subprocess import Popen -from testutils import (unittest, skip_before_postgres, +import unittest +from testutils import (skip_before_postgres, ConnectingTestCase, skip_copy_if_green, script_to_py3, slow) import psycopg2 diff --git a/tests/test_notify.py b/tests/test_notify.py index 0e74e1d5..fabc3b65 100755 --- a/tests/test_notify.py +++ b/tests/test_notify.py @@ -22,7 +22,7 @@ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License for more details. -from testutils import unittest +import unittest import psycopg2 from psycopg2 import extensions diff --git a/tests/test_psycopg2_dbapi20.py b/tests/test_psycopg2_dbapi20.py index c780d506..3bcedc41 100755 --- a/tests/test_psycopg2_dbapi20.py +++ b/tests/test_psycopg2_dbapi20.py @@ -25,7 +25,8 @@ import dbapi20 import dbapi20_tpc from testutils import skip_if_tpc_disabled -from testutils import unittest, decorate_all_tests +import unittest +from testutils import decorate_all_tests import psycopg2 from testconfig import dsn diff --git a/tests/test_quote.py b/tests/test_quote.py index 1ced845c..0f60dd9f 100755 --- a/tests/test_quote.py +++ b/tests/test_quote.py @@ -24,7 +24,8 @@ import sys import testutils -from testutils import unittest, ConnectingTestCase +import unittest +from testutils import ConnectingTestCase import psycopg2 import psycopg2.extensions diff --git a/tests/test_replication.py b/tests/test_replication.py index d03c6566..444dd111 100755 --- a/tests/test_replication.py +++ b/tests/test_replication.py @@ -27,7 +27,8 @@ from psycopg2.extras import ( PhysicalReplicationConnection, LogicalReplicationConnection, StopReplication) import testconfig -from testutils import unittest, ConnectingTestCase +import unittest +from testutils import ConnectingTestCase from testutils import skip_before_postgres, skip_if_green skip_repl_if_green = skip_if_green("replication not supported in green mode") diff --git a/tests/test_sql.py b/tests/test_sql.py index 2e12ba63..bf2f96fb 100755 --- a/tests/test_sql.py +++ b/tests/test_sql.py @@ -24,7 +24,8 @@ import datetime as dt from cStringIO import StringIO -from testutils import (unittest, ConnectingTestCase, +import unittest +from testutils import (ConnectingTestCase, skip_before_postgres, skip_before_python, skip_copy_if_green) import psycopg2 diff --git a/tests/test_transaction.py b/tests/test_transaction.py index dd487c05..26704d8f 100755 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -23,7 +23,8 @@ # License for more details. import threading -from testutils import unittest, ConnectingTestCase, skip_before_postgres, slow +import unittest +from testutils import ConnectingTestCase, skip_before_postgres, slow import psycopg2 from psycopg2.extensions import ( diff --git a/tests/test_types_basic.py b/tests/test_types_basic.py index b0af6daa..cd1a4f6b 100755 --- a/tests/test_types_basic.py +++ b/tests/test_types_basic.py @@ -27,7 +27,8 @@ import decimal import sys from functools import wraps import testutils -from testutils import unittest, ConnectingTestCase, decorate_all_tests +import unittest +from testutils import ConnectingTestCase, decorate_all_tests import psycopg2 diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index f50b1b5e..8526814d 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -22,7 +22,8 @@ from datetime import date, datetime from functools import wraps from pickle import dumps, loads -from testutils import (unittest, skip_if_no_uuid, skip_before_postgres, +import unittest +from testutils import (skip_if_no_uuid, skip_before_postgres, ConnectingTestCase, decorate_all_tests, py3_raises_typeerror, slow) import psycopg2 diff --git a/tests/test_with.py b/tests/test_with.py index 53dfa464..83612dec 100755 --- a/tests/test_with.py +++ b/tests/test_with.py @@ -25,7 +25,8 @@ import psycopg2 import psycopg2.extensions as ext -from testutils import unittest, ConnectingTestCase +import unittest +from testutils import ConnectingTestCase class WithTestCase(ConnectingTestCase): From 56ec575351ee2a562665bdf9162cbc5d19fbe387 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 1 Dec 2017 18:57:43 -0800 Subject: [PATCH 52/96] Drop leading 0's from numeric literals Not compatible with Python3. Makes the code more forward compatible with modern Pythons. In Python2, it was an alternative syntax for octal. $ python3 >>> 01 File "", line 1 01 ^ SyntaxError: invalid token --- tests/test_types_extras.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index f50b1b5e..870819cf 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -1619,7 +1619,7 @@ class RangeCasterTestCase(ConnectingTestCase): from psycopg2.tz import FixedOffsetTimezone cur = self.conn.cursor() - d1 = date(2012, 01, 01) + d1 = date(2012, 1, 1) d2 = date(2012, 12, 31) r = DateRange(d1, d2) cur.execute("select %s", (r,)) From f5703dc3e55008b03aad4db8d8f3c5fa12dbae86 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 1 Dec 2017 19:53:30 -0800 Subject: [PATCH 53/96] Use builtin function next() throughout project Available since Python 2.6. Use of .next() is deprecated and not supported in Python 3. Forward compatible with modern Python. https://docs.python.org/2/library/functions.html#next --- doc/src/tools/stitch_text.py | 5 +---- lib/extras.py | 12 ++++++------ lib/sql.py | 2 +- tests/test_cursor.py | 4 ++-- tests/test_extras_dictcursor.py | 8 ++++---- tests/test_sql.py | 6 +++--- 6 files changed, 17 insertions(+), 20 deletions(-) diff --git a/doc/src/tools/stitch_text.py b/doc/src/tools/stitch_text.py index c9ed99aa..9b5d9172 100755 --- a/doc/src/tools/stitch_text.py +++ b/doc/src/tools/stitch_text.py @@ -19,10 +19,7 @@ def main(): def iter_file_base(fn): f = open(fn) - if sys.version_info[0] >= 3: - have_line = iter(f).__next__ - else: - have_line = iter(f).next + have_line = iter(f).__next__ while not have_line().startswith('.. toctree'): pass diff --git a/lib/extras.py b/lib/extras.py index 3308f78f..a6c025e1 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -110,16 +110,16 @@ class DictCursorBase(_cursor): try: if self._prefetch: res = super(DictCursorBase, self).__iter__() - first = res.next() + first = next(res) if self._query_executed: self._build_index() if not self._prefetch: res = super(DictCursorBase, self).__iter__() - first = res.next() + first = next(res) yield first while 1: - yield res.next() + yield next(res) except StopIteration: return @@ -349,7 +349,7 @@ class NamedTupleCursor(_cursor): def __iter__(self): try: it = super(NamedTupleCursor, self).__iter__() - t = it.next() + t = next(it) nt = self.Record if nt is None: @@ -358,7 +358,7 @@ class NamedTupleCursor(_cursor): yield nt._make(t) while 1: - yield nt._make(it.next()) + yield nt._make(next(it)) except StopIteration: return @@ -1144,7 +1144,7 @@ def _paginate(seq, page_size): while 1: try: for i in xrange(page_size): - page.append(it.next()) + page.append(next(it)) yield page page = [] except StopIteration: diff --git a/lib/sql.py b/lib/sql.py index 5dfe7c8b..849b25fb 100644 --- a/lib/sql.py +++ b/lib/sql.py @@ -276,7 +276,7 @@ class SQL(Composable): rv = [] it = iter(seq) try: - rv.append(it.next()) + rv.append(next(it)) except StopIteration: pass else: diff --git a/tests/test_cursor.py b/tests/test_cursor.py index c5ce70fa..6772bfe5 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -334,9 +334,9 @@ class CursorTests(ConnectingTestCase): # timestamp will not be influenced by the pause in Python world. curs.execute("""select clock_timestamp() from generate_series(1,2)""") i = iter(curs) - t1 = (i.next())[0] # the brackets work around a 2to3 bug + t1 = next(i)[0] time.sleep(0.2) - t2 = (i.next())[0] + t2 = next(i)[0] self.assert_((t2 - t1).microseconds * 1e-6 < 0.1, "named cursor records fetched in 2 roundtrips (delta: %s)" % (t2 - t1)) diff --git a/tests/test_extras_dictcursor.py b/tests/test_extras_dictcursor.py index 2c867d38..e95c923a 100755 --- a/tests/test_extras_dictcursor.py +++ b/tests/test_extras_dictcursor.py @@ -310,22 +310,22 @@ class NamedTupleCursorTest(ConnectingTestCase): i = iter(curs) self.assertEqual(curs.rownumber, 0) - t = i.next() + t = next(i) self.assertEqual(t.i, 1) self.assertEqual(t.s, 'foo') self.assertEqual(curs.rownumber, 1) self.assertEqual(curs.rowcount, 3) - t = i.next() + t = next(i) self.assertEqual(t.i, 2) self.assertEqual(t.s, 'bar') self.assertEqual(curs.rownumber, 2) self.assertEqual(curs.rowcount, 3) - t = i.next() + t = next(i) self.assertEqual(t.i, 3) self.assertEqual(t.s, 'baz') - self.assertRaises(StopIteration, i.next) + self.assertRaises(StopIteration, next, i) self.assertEqual(curs.rownumber, 3) self.assertEqual(curs.rowcount, 3) diff --git a/tests/test_sql.py b/tests/test_sql.py index 2e12ba63..08f43b45 100755 --- a/tests/test_sql.py +++ b/tests/test_sql.py @@ -344,11 +344,11 @@ class ComposedTest(ConnectingTestCase): def test_iter(self): obj = sql.Composed([sql.SQL("foo"), sql.SQL('bar')]) it = iter(obj) - i = it.next() + i = next(it) self.assertEqual(i, sql.SQL('foo')) - i = it.next() + i = next(it) self.assertEqual(i, sql.SQL('bar')) - self.assertRaises(StopIteration, it.next) + self.assertRaises(StopIteration, next, it) class PlaceholderTest(ConnectingTestCase): From d0c7126aa88e8347d1add7e7e0f61974717e58d3 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 1 Dec 2017 21:06:10 -0800 Subject: [PATCH 54/96] Remove test decorators for json module The json module is available in all Python versions supported by psycopg2. No need to check for its presence when executing tests. Should have been included with d58844e5483483240f97537e9a77b4e79cea2ab3 but was missed. --- tests/test_types_extras.py | 56 -------------------------------------- 1 file changed, 56 deletions(-) diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index f50b1b5e..992a73d3 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -812,30 +812,6 @@ class AdaptTypeTestCase(ConnectingTestCase): return oid -def skip_if_json_module(f): - """Skip a test if a Python json module *is* available""" - @wraps(f) - def skip_if_json_module_(self): - if psycopg2.extras.json is not None: - return self.skipTest("json module is available") - - return f(self) - - return skip_if_json_module_ - - -def skip_if_no_json_module(f): - """Skip a test if no Python json module is available""" - @wraps(f) - def skip_if_no_json_module_(self): - if psycopg2.extras.json is None: - return self.skipTest("json module not available") - - return f(self) - - return skip_if_no_json_module_ - - def skip_if_no_json_type(f): """Skip a test if PostgreSQL json type is not available""" @wraps(f) @@ -851,23 +827,6 @@ def skip_if_no_json_type(f): class JsonTestCase(ConnectingTestCase): - @skip_if_json_module - def test_module_not_available(self): - from psycopg2.extras import Json - self.assertRaises(ImportError, Json(None).getquoted) - - @skip_if_json_module - def test_customizable_with_module_not_available(self): - from psycopg2.extras import Json - - class MyJson(Json): - def dumps(self, obj): - assert obj is None - return "hi" - - self.assertEqual(MyJson(None).getquoted(), "'hi'") - - @skip_if_no_json_module def test_adapt(self): from psycopg2.extras import json, Json @@ -879,7 +838,6 @@ class JsonTestCase(ConnectingTestCase): self.assertQuotedEqual(curs.mogrify("%s", (Json(obj),)), psycopg2.extensions.QuotedString(json.dumps(obj)).getquoted()) - @skip_if_no_json_module def test_adapt_dumps(self): from psycopg2.extras import json, Json @@ -897,7 +855,6 @@ class JsonTestCase(ConnectingTestCase): self.assertQuotedEqual(curs.mogrify("%s", (Json(obj, dumps=dumps),)), b"'123.45'") - @skip_if_no_json_module def test_adapt_subclass(self): from psycopg2.extras import json, Json @@ -915,7 +872,6 @@ class JsonTestCase(ConnectingTestCase): obj = Decimal('123.45') self.assertQuotedEqual(curs.mogrify("%s", (MyJson(obj),)), b"'123.45'") - @skip_if_no_json_module def test_register_on_dict(self): from psycopg2.extras import Json psycopg2.extensions.register_adapter(dict, Json) @@ -937,7 +893,6 @@ class JsonTestCase(ConnectingTestCase): self.assertRaises(psycopg2.ProgrammingError, psycopg2.extras.register_json, self.conn) - @skip_if_no_json_module @skip_before_postgres(9, 2) def test_default_cast(self): curs = self.conn.cursor() @@ -948,7 +903,6 @@ class JsonTestCase(ConnectingTestCase): curs.execute("""select array['{"a": 100.0, "b": null}']::json[]""") self.assertEqual(curs.fetchone()[0], [{'a': 100.0, 'b': None}]) - @skip_if_no_json_module @skip_if_no_json_type def test_register_on_connection(self): psycopg2.extras.register_json(self.conn) @@ -956,7 +910,6 @@ class JsonTestCase(ConnectingTestCase): curs.execute("""select '{"a": 100.0, "b": null}'::json""") self.assertEqual(curs.fetchone()[0], {'a': 100.0, 'b': None}) - @skip_if_no_json_module @skip_if_no_json_type def test_register_on_cursor(self): curs = self.conn.cursor() @@ -964,7 +917,6 @@ class JsonTestCase(ConnectingTestCase): curs.execute("""select '{"a": 100.0, "b": null}'::json""") self.assertEqual(curs.fetchone()[0], {'a': 100.0, 'b': None}) - @skip_if_no_json_module @skip_if_no_json_type def test_register_globally(self): old = psycopg2.extensions.string_types.get(114) @@ -982,7 +934,6 @@ class JsonTestCase(ConnectingTestCase): if olda: psycopg2.extensions.register_type(olda) - @skip_if_no_json_module @skip_if_no_json_type def test_loads(self): json = psycopg2.extras.json @@ -996,7 +947,6 @@ class JsonTestCase(ConnectingTestCase): self.assert_(isinstance(data['a'], Decimal)) self.assertEqual(data['a'], Decimal('100.0')) - @skip_if_no_json_module @skip_if_no_json_type def test_no_conn_curs(self): from psycopg2._json import _get_json_oids @@ -1023,7 +973,6 @@ class JsonTestCase(ConnectingTestCase): if olda: psycopg2.extensions.register_type(olda) - @skip_if_no_json_module @skip_before_postgres(9, 2) def test_register_default(self): curs = self.conn.cursor() @@ -1042,7 +991,6 @@ class JsonTestCase(ConnectingTestCase): self.assert_(isinstance(data[0]['a'], Decimal)) self.assertEqual(data[0]['a'], Decimal('100.0')) - @skip_if_no_json_module @skip_if_no_json_type def test_null(self): psycopg2.extras.register_json(self.conn) @@ -1052,7 +1000,6 @@ class JsonTestCase(ConnectingTestCase): curs.execute("""select NULL::json[]""") self.assertEqual(curs.fetchone()[0], None) - @skip_if_no_json_module def test_no_array_oid(self): curs = self.conn.cursor() t1, t2 = psycopg2.extras.register_json(curs, oid=25) @@ -1064,7 +1011,6 @@ class JsonTestCase(ConnectingTestCase): self.assertEqual(data['a'], 100) self.assertEqual(data['b'], None) - @skip_if_no_json_module def test_str(self): snowman = u"\u2603" obj = {'a': [1, 2, snowman]} @@ -1075,7 +1021,6 @@ class JsonTestCase(ConnectingTestCase): self.assert_(s.startswith("'")) self.assert_(s.endswith("'")) - @skip_if_no_json_module @skip_before_postgres(8, 2) def test_scs(self): cnn_on = self.connect(options="-c standard_conforming_strings=on") @@ -1188,7 +1133,6 @@ class JsonbTestCase(ConnectingTestCase): curs.execute("""select NULL::jsonb[]""") self.assertEqual(curs.fetchone()[0], None) -decorate_all_tests(JsonbTestCase, skip_if_no_json_module) decorate_all_tests(JsonbTestCase, skip_if_no_jsonb_type) From e335d6d2234a7bb1d83741f851dba5dbbabd6e3c Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 1 Dec 2017 21:37:49 -0800 Subject: [PATCH 55/96] Trim trailing whitespace from all files throughout project Many editors automatically trim whitespace on save. By trimming all files in one go, makes future diffs cleaner without extraneous whitespace changes. --- AUTHORS | 2 +- LICENSE | 4 +- doc/COPYING.LESSER | 4 +- doc/SUCCESS | 22 +- doc/pep-0249.txt | 294 +++++++++++++-------------- doc/release.rst | 2 +- doc/src/advanced.rst | 2 +- doc/src/connection.rst | 12 +- doc/src/cursor.rst | 44 ++-- doc/src/faq.rst | 1 - doc/src/index.rst | 1 - doc/src/pool.rst | 3 +- doc/src/tools/lib/dbapi_extension.py | 1 - doc/src/tools/lib/sql_role.py | 5 +- doc/src/tools/lib/ticket_role.py | 1 - doc/src/tools/stitch_text.py | 3 +- doc/src/tz.rst | 3 +- doc/src/usage.rst | 1 - examples/binary.py | 4 +- examples/copy_from.py | 5 +- examples/copy_to.py | 2 +- examples/cursor.py | 2 +- examples/dialtone.py | 68 +++---- examples/dict.py | 2 +- examples/encoding.py | 2 +- examples/myfirstrecipe.py | 12 +- examples/threads.py | 10 +- examples/typecast.py | 2 - examples/tz.py | 4 +- examples/usercast.py | 6 +- psycopg/bytes_format.c | 1 - psycopg/notify_type.c | 2 - psycopg/typecast_binary.c | 1 - psycopg/typecast_builtins.c | 1 - psycopg/typecast_mxdatetime.c | 1 - psycopg2.sln | 5 +- sandbox/array.py | 1 - sandbox/async.py | 3 +- sandbox/gtk.py | 1 - sandbox/iter.py | 3 +- sandbox/leak.test.py | 3 +- sandbox/misc_dbapi_test.py | 6 +- sandbox/named.py | 2 +- sandbox/pbool.py | 1 - sandbox/test.py | 3 +- sandbox/test814.py | 1 - sandbox/test_copy2.py | 1 - sandbox/textfloat.py | 1 - sandbox/ticket58.py | 2 - sandbox/valgrind-python.supp | 10 +- scripts/maketypes.sh | 4 +- tests/dbapi20.py | 33 ++- 52 files changed, 286 insertions(+), 324 deletions(-) diff --git a/AUTHORS b/AUTHORS index 32017591..60677ddb 100644 --- a/AUTHORS +++ b/AUTHORS @@ -6,7 +6,7 @@ For the win32 port: Jason Erickson Additional Help: - + Peter Fein contributed a logging connection/cursor class that even if it was not used directly heavily influenced the implementation currently in psycopg2.extras. diff --git a/LICENSE b/LICENSE index dd7c124d..360a44f6 100644 --- a/LICENSE +++ b/LICENSE @@ -47,8 +47,8 @@ psycopg/microprotocol*.{h,c}: claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. - + 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. - + 3. This notice may not be removed or altered from any source distribution. diff --git a/doc/COPYING.LESSER b/doc/COPYING.LESSER index fc8a5de7..cca7fc27 100644 --- a/doc/COPYING.LESSER +++ b/doc/COPYING.LESSER @@ -10,7 +10,7 @@ the terms and conditions of version 3 of the GNU General Public License, supplemented by the additional permissions listed below. - 0. Additional Definitions. + 0. Additional Definitions. As used herein, "this License" refers to version 3 of the GNU Lesser General Public License, and the "GNU GPL" refers to version 3 of the GNU @@ -111,7 +111,7 @@ the following: a copy of the Library already present on the user's computer system, and (b) will operate properly with a modified version of the Library that is interface-compatible with the Linked - Version. + Version. e) Provide Installation Information, but only if you would otherwise be required to provide such information under section 6 of the diff --git a/doc/SUCCESS b/doc/SUCCESS index 7788b13f..de459918 100644 --- a/doc/SUCCESS +++ b/doc/SUCCESS @@ -1,10 +1,10 @@ From: Jack Moffitt To: Psycopg Mailing List Subject: Re: [Psycopg] preparing for 1.0 -Date: 22 Oct 2001 11:16:21 -0600 +Date: 22 Oct 2001 11:16:21 -0600 -www.vorbis.com is serving from 5-10k pages per day with psycopg serving -data for most of that. +www.vorbis.com is serving from 5-10k pages per day with psycopg serving +data for most of that. I plan to use it for several of our other sites, so that number will increase. @@ -19,7 +19,7 @@ jack. From: Yury Don To: Psycopg Mailing List Subject: Re: [Psycopg] preparing for 1.0 -Date: 23 Oct 2001 09:53:11 +0600 +Date: 23 Oct 2001 09:53:11 +0600 We use psycopg and psycopg zope adapter since fisrt public release (it seems version 0.4). Now it works on 3 our sites and in intranet @@ -32,7 +32,7 @@ to solve the problem, even thouth my knowledge of c were poor. BTW, segfault with dictfetchall on particular data set (see [Psycopg] dictfetchXXX() problems) disappeared in 0.99.8pre2. --- +-- Best regards, Yury Don @@ -42,7 +42,7 @@ To: Federico Di Gregorio Cc: Psycopg Mailing List Subject: Re: [Psycopg] preparing for 1.0 Date: 23 Oct 2001 08:25:52 -0400 - + The US Govt Department of Labor's Office of Disability Employment Policy's DisabilityDirect website is run on zope and zpsycopg. @@ -50,7 +50,7 @@ Policy's DisabilityDirect website is run on zope and zpsycopg. From: Scott Leerssen To: Federico Di Gregorio Subject: Re: [Psycopg] preparing for 1.0 -Date: 23 Oct 2001 09:56:10 -0400 +Date: 23 Oct 2001 09:56:10 -0400 Racemi's load management software infrastructure uses psycopg to handle complex server allocation decisions, plus storage and access of @@ -66,10 +66,10 @@ From: Andre Schubert To: Federico Di Gregorio Cc: Psycopg Mailing List Subject: Re: [Psycopg] preparing for 1.0 -Date: 23 Oct 2001 11:46:07 +0200 +Date: 23 Oct 2001 11:46:07 +0200 i have changed the psycopg version to 0.99.8pre2 on all devel-machines -and all segfaults are gone. after my holiday i wil change to 0.99.8pre2 +and all segfaults are gone. after my holiday i wil change to 0.99.8pre2 or 1.0 on our production-server. this server contains several web-sites which are all connected to postgres over ZPsycopgDA. @@ -81,7 +81,7 @@ From: Fred Wilson Horch To: Subject: [Psycopg] Success story for psycopg Date: 23 Oct 2001 10:59:17 -0400 - + Due to various quirks of PyGreSQL and PoPy, EcoAccess has been looking for a reliable, fast and relatively bug-free Python-PostgreSQL interface for our project. @@ -98,7 +98,7 @@ reports and feature requests, and we're looking forward to using psycopg as the Python interface for additional database-backed web applications. Keep up the good work! --- +-- Fred Wilson Horch mailto:fhorch@ecoaccess.org Executive Director, EcoAccess http://ecoaccess.org/ diff --git a/doc/pep-0249.txt b/doc/pep-0249.txt index e0e36002..e74fd0db 100644 --- a/doc/pep-0249.txt +++ b/doc/pep-0249.txt @@ -9,15 +9,15 @@ Replaces: 248 Release-Date: 07 Apr 1999 Introduction - + This API has been defined to encourage similarity between the Python modules that are used to access databases. By doing this, we hope to achieve a consistency leading to more easily understood modules, code that is generally more portable across databases, and a broader reach of database connectivity from Python. - + The interface specification consists of several sections: - + * Module Interface * Connection Objects * Cursor Objects @@ -25,7 +25,7 @@ Introduction * Type Objects and Constructors * Implementation Hints * Major Changes from 1.0 to 2.0 - + Comments and questions about this specification may be directed to the SIG for Database Interfacing with Python (db-sig@python.org). @@ -41,7 +41,7 @@ Introduction basis for new interfaces. Module Interface - + Access to the database is made available through connection objects. The module must provide the following constructor for these: @@ -51,17 +51,17 @@ Module Interface Constructor for creating a connection to the database. Returns a Connection Object. It takes a number of parameters which are database dependent. [1] - + These module globals must be defined: apilevel String constant stating the supported DB API level. Currently only the strings '1.0' and '2.0' are allowed. - + If not given, a DB-API 1.0 level interface should be assumed. - + threadsafety Integer constant stating the level of thread safety the @@ -81,33 +81,33 @@ Module Interface or other external sources that are beyond your control. paramstyle - + String constant stating the type of parameter marker formatting expected by the interface. Possible values are [2]: - 'qmark' Question mark style, + 'qmark' Question mark style, e.g. '...WHERE name=?' - 'numeric' Numeric, positional style, + 'numeric' Numeric, positional style, e.g. '...WHERE name=:1' - 'named' Named style, + 'named' Named style, e.g. '...WHERE name=:name' - 'format' ANSI C printf format codes, + 'format' ANSI C printf format codes, e.g. '...WHERE name=%s' - 'pyformat' Python extended format codes, + 'pyformat' Python extended format codes, e.g. '...WHERE name=%(name)s' The module should make all error information available through these exceptions or subclasses thereof: - Warning - + Warning + Exception raised for important warnings like data truncations while inserting, etc. It must be a subclass of the Python StandardError (defined in the module exceptions). - - Error + + Error Exception that is the base class of all other error exceptions. You can use this to catch all errors with one @@ -115,7 +115,7 @@ Module Interface errors and thus should not use this class as base. It must be a subclass of the Python StandardError (defined in the module exceptions). - + InterfaceError Exception raised for errors that are related to the @@ -126,50 +126,50 @@ Module Interface Exception raised for errors that are related to the database. It must be a subclass of Error. - + DataError - + Exception raised for errors that are due to problems with the processed data like division by zero, numeric value out of range, etc. It must be a subclass of DatabaseError. - + OperationalError - + Exception raised for errors that are related to the database's operation and not necessarily under the control of the programmer, e.g. an unexpected disconnect occurs, the data source name is not found, a transaction could not be processed, a memory allocation error occurred during processing, etc. It must be a subclass of DatabaseError. - - IntegrityError - + + IntegrityError + Exception raised when the relational integrity of the database is affected, e.g. a foreign key check fails. It must be a subclass of DatabaseError. - - InternalError - + + InternalError + Exception raised when the database encounters an internal error, e.g. the cursor is not valid anymore, the transaction is out of sync, etc. It must be a subclass of DatabaseError. - + ProgrammingError - + Exception raised for programming errors, e.g. table not found or already exists, syntax error in the SQL statement, wrong number of parameters specified, etc. It must be a subclass of DatabaseError. - + NotSupportedError - + Exception raised in case a method or database API was used which is not supported by the database, e.g. requesting a .rollback() on a connection that does not support transaction or has transactions turned off. It must be a subclass of DatabaseError. - + This is the exception inheritance layout: StandardError @@ -183,17 +183,17 @@ Module Interface |__InternalError |__ProgrammingError |__NotSupportedError - + Note: The values of these exceptions are not defined. They should give the user a fairly good idea of what went wrong, though. - + Connection Objects Connection Objects should respond to the following methods: - .close() - + .close() + Close the connection now (rather than whenever __del__ is called). The connection will be unusable from this point forward; an Error (or subclass) exception will be raised @@ -203,52 +203,52 @@ Connection Objects committing the changes first will cause an implicit rollback to be performed. - + .commit() - + Commit any pending transaction to the database. Note that if the database supports an auto-commit feature, this must be initially off. An interface method may be provided to turn it back on. - + Database modules that do not support transactions should implement this method with void functionality. - - .rollback() - + + .rollback() + This method is optional since not all databases provide transaction support. [3] - + In case a database does provide transactions this method causes the the database to roll back to the start of any pending transaction. Closing a connection without committing the changes first will cause an implicit rollback to be performed. - + .cursor() - + Return a new Cursor Object using the connection. If the database does not provide a direct cursor concept, the module will have to emulate cursors using other means to the extent needed by this specification. [4] - + Cursor Objects These objects represent a database cursor, which is used to - manage the context of a fetch operation. Cursors created from + manage the context of a fetch operation. Cursors created from the same connection are not isolated, i.e., any changes done to the database by a cursor are immediately visible by the other cursors. Cursors created from different connections can or can not be isolated, depending on how the transaction support - is implemented (see also the connection's rollback() and commit() + is implemented (see also the connection's rollback() and commit() methods.) - + Cursor Objects should respond to the following methods and attributes: - .description - + .description + This read-only attribute is a sequence of 7-item sequences. Each of these sequences contains information describing one result column: (name, type_code, @@ -260,17 +260,17 @@ Cursor Objects This attribute will be None for operations that do not return rows or if the cursor has not had an operation invoked via the executeXXX() method yet. - + The type_code can be interpreted by comparing it to the Type Objects specified in the section below. - - .rowcount - + + .rowcount + This read-only attribute specifies the number of rows that the last executeXXX() produced (for DQL statements like 'select') or affected (for DML statements like 'update' or 'insert'). - + The attribute is -1 in case no executeXXX() has been performed on the cursor or the rowcount of the last operation is not determinable by the interface. [7] @@ -278,96 +278,96 @@ Cursor Objects Note: Future versions of the DB API specification could redefine the latter case to have the object return None instead of -1. - + .callproc(procname[,parameters]) - + (This method is optional since not all databases provide stored procedures. [3]) - + Call a stored database procedure with the given name. The sequence of parameters must contain one entry for each argument that the procedure expects. The result of the call is returned as modified copy of the input sequence. Input parameters are left untouched, output and input/output parameters replaced with possibly new values. - + The procedure may also provide a result set as output. This must then be made available through the standard fetchXXX() methods. - + .close() - + Close the cursor now (rather than whenever __del__ is called). The cursor will be unusable from this point forward; an Error (or subclass) exception will be raised if any operation is attempted with the cursor. - - .execute(operation[,parameters]) - + + .execute(operation[,parameters]) + Prepare and execute a database operation (query or command). Parameters may be provided as sequence or mapping and will be bound to variables in the operation. Variables are specified in a database-specific notation (see the module's paramstyle attribute for details). [5] - + A reference to the operation will be retained by the cursor. If the same operation object is passed in again, then the cursor can optimize its behavior. This is most effective for algorithms where the same operation is used, but different parameters are bound to it (many times). - + For maximum efficiency when reusing an operation, it is best to use the setinputsizes() method to specify the parameter types and sizes ahead of time. It is legal for a parameter to not match the predefined information; the implementation should compensate, possibly with a loss of efficiency. - + The parameters may also be specified as list of tuples to e.g. insert multiple rows in a single operation, but this kind of usage is depreciated: executemany() should be used instead. - + Return values are not defined. - - .executemany(operation,seq_of_parameters) - + + .executemany(operation,seq_of_parameters) + Prepare a database operation (query or command) and then execute it against all parameter sequences or mappings found in the sequence seq_of_parameters. - + Modules are free to implement this method using multiple calls to the execute() method or by using array operations to have the database process the sequence as a whole in one call. - + Use of this method for an operation which produces one or more result sets constitutes undefined behavior, and the - implementation is permitted (but not required) to raise + implementation is permitted (but not required) to raise an exception when it detects that a result set has been created by an invocation of the operation. - + The same comments as for execute() also apply accordingly to this method. - + Return values are not defined. - - .fetchone() - + + .fetchone() + Fetch the next row of a query result set, returning a single sequence, or None when no more data is available. [6] - + An Error (or subclass) exception is raised if the previous call to executeXXX() did not produce any result set or no call was issued yet. fetchmany([size=cursor.arraysize]) - + Fetch the next set of rows of a query result, returning a sequence of sequences (e.g. a list of tuples). An empty sequence is returned when no more rows are available. - + The number of rows to fetch per call is specified by the parameter. If it is not given, the cursor's arraysize determines the number of rows to be fetched. The method @@ -375,62 +375,62 @@ Cursor Objects parameter. If this is not possible due to the specified number of rows not being available, fewer rows may be returned. - + An Error (or subclass) exception is raised if the previous call to executeXXX() did not produce any result set or no call was issued yet. - + Note there are performance considerations involved with the size parameter. For optimal performance, it is usually best to use the arraysize attribute. If the size parameter is used, then it is best for it to retain the same value from one fetchmany() call to the next. - - .fetchall() + + .fetchall() Fetch all (remaining) rows of a query result, returning them as a sequence of sequences (e.g. a list of tuples). Note that the cursor's arraysize attribute can affect the performance of this operation. - + An Error (or subclass) exception is raised if the previous call to executeXXX() did not produce any result set or no call was issued yet. - - .nextset() - + + .nextset() + (This method is optional since not all databases support multiple result sets. [3]) - + This method will make the cursor skip to the next available set, discarding any remaining rows from the current set. - + If there are no more sets, the method returns None. Otherwise, it returns a true value and subsequent calls to the fetch methods will return rows from the next result set. - + An Error (or subclass) exception is raised if the previous call to executeXXX() did not produce any result set or no call was issued yet. .arraysize - + This read/write attribute specifies the number of rows to fetch at a time with fetchmany(). It defaults to 1 meaning to fetch a single row at a time. - + Implementations must observe this value with respect to the fetchmany() method, but are free to interact with the database a single row at a time. It may also be used in the implementation of executemany(). - + .setinputsizes(sizes) - + This can be used before a call to executeXXX() to predefine memory areas for the operation's parameters. - + sizes is specified as a sequence -- one item for each input parameter. The item should be a Type Object that corresponds to the input that will be used, or it should @@ -438,27 +438,27 @@ Cursor Objects parameter. If the item is None, then no predefined memory area will be reserved for that column (this is useful to avoid predefined areas for large inputs). - + This method would be used before the executeXXX() method is invoked. - + Implementations are free to have this method do nothing and users are free to not use it. - + .setoutputsize(size[,column]) - + Set a column buffer size for fetches of large columns (e.g. LONGs, BLOBs, etc.). The column is specified as an index into the result sequence. Not specifying the column will set the default size for all large columns in the cursor. - + This method would be used before the executeXXX() method is invoked. - + Implementations are free to have this method do nothing and users are free to not use it. - + Type Objects and Constructors @@ -485,15 +485,15 @@ Type Objects and Constructors Implementation Hints below for details). The module exports the following constructors and singletons: - + Date(year,month,day) This function constructs an object holding a date value. - + Time(hour,minute,second) This function constructs an object holding a time value. - + Timestamp(year,month,day,hour,minute,second) This function constructs an object holding a time stamp @@ -507,12 +507,12 @@ Type Objects and Constructors module for details). TimeFromTicks(ticks) - + This function constructs an object holding a time value from the given ticks value (number of seconds since the epoch; see the documentation of the standard Python time module for details). - + TimestampFromTicks(ticks) This function constructs an object holding a time stamp @@ -521,10 +521,10 @@ Type Objects and Constructors time module for details). Binary(string) - + This function constructs an object capable of holding a binary (long) string value. - + STRING @@ -535,22 +535,22 @@ Type Objects and Constructors This type object is used to describe (long) binary columns in a database (e.g. LONG, RAW, BLOBs). - + NUMBER This type object is used to describe numeric columns in a database. DATETIME - + This type object is used to describe date/time columns in a database. - + ROWID - + This type object is used to describe the "Row ID" column in a database. - + SQL NULL values are represented by the Python None singleton on input and output. @@ -563,7 +563,7 @@ Implementation Hints for Module Authors * The preferred object types for the date/time objects are those defined in the mxDateTime package. It provides all necessary constructors and methods both at Python and C level. - + * The preferred object type for Binary objects are the buffer types available in standard Python starting with version 1.5.2. Please see the Python documentation for @@ -577,7 +577,7 @@ Implementation Hints for Module Authors processing. However, it should be noted that this does not expose a C API like mxDateTime does which means that integration with C based database modules is more difficult. - + * Here is a sample implementation of the Unix ticks based constructors for date/time delegating work to the generic constructors: @@ -645,7 +645,7 @@ Implementation Hints for Module Authors class NotSupportedError(DatabaseError): pass - + In C you can use the PyErr_NewException(fullname, base, NULL) API to create the exception objects. @@ -760,7 +760,7 @@ Optional DB API Extensions Warning Message: "DB-API extension connection.messages used" Cursor Method .next() - + Return the next row from the currently executing SQL statement using the same semantics as .fetchone(). A StopIteration exception is raised when the result set is exhausted for Python @@ -790,13 +790,13 @@ Optional DB API Extensions Warning Message: "DB-API extension cursor.lastrowid used" - + Optional Error Handling Extension The core DB API specification only introduces a set of exceptions which can be raised to report errors to the user. In some cases, exceptions may be too disruptive for the flow of a program or even - render execution impossible. + render execution impossible. For these cases and in order to simplify error handling when dealing with databases, database module authors may choose to @@ -806,7 +806,7 @@ Optional Error Handling Extension Cursor/Connection Attribute .errorhandler Read/write attribute which references an error handler to call - in case an error condition is met. + in case an error condition is met. The handler must be a Python callable taking the following arguments: errorhandler(connection, cursor, errorclass, @@ -836,7 +836,7 @@ Frequently Asked Questions specification. This section covers some of the issues people sometimes have with the specification. - Question: + Question: How can I construct a dictionary out of the tuples returned by .fetchxxx(): @@ -855,7 +855,7 @@ Frequently Asked Questions * Some databases don't support case-sensitive column names or auto-convert them to all lowercase or all uppercase characters. - + * Columns in the result set which are generated by the query (e.g. using SQL functions) don't map to table column names and databases usually generate names for these columns in a @@ -872,9 +872,9 @@ Major Changes from Version 1.0 to Version 2.0 compared to the 1.0 version. Because some of these changes will cause existing DB API 1.0 based scripts to break, the major version number was adjusted to reflect this change. - + These are the most important changes from 1.0 to 2.0: - + * The need for a separate dbi module was dropped and the functionality merged into the module interface itself. @@ -886,10 +886,10 @@ Major Changes from Version 1.0 to Version 2.0 * New constants (apilevel, threadlevel, paramstyle) and methods (executemany, nextset) were added to provide better database bindings. - + * The semantics of .callproc() needed to call stored procedures are now clearly defined. - + * The definition of the .execute() return value changed. Previously, the return value was based on the SQL statement type (which was hard to implement right) -- it is undefined @@ -898,7 +898,7 @@ Major Changes from Version 1.0 to Version 2.0 values, but these are no longer mandated by the specification and should be considered database interface dependent. - + * Class based exceptions were incorporated into the specification. Module implementors are free to extend the exception layout defined in this specification by @@ -916,10 +916,10 @@ Open Issues questions that were left open in the 1.0 version, there are still some remaining issues which should be addressed in future versions: - + * Define a useful return value for .nextset() for the case where a new result set is available. - + * Create a fixed point numeric type for use as loss-less monetary and decimal interchange format. @@ -929,17 +929,17 @@ Footnotes [1] As a guideline the connection constructor parameters should be implemented as keyword parameters for more intuitive use and follow this order of parameters: - + dsn Data source name as string user User name as string (optional) password Password as string (optional) host Hostname (optional) database Database name (optional) - + E.g. a connect could look like this: - + connect(dsn='myhost:MYDB',user='guido',password='234$') - + [2] Module implementors should prefer 'numeric', 'named' or 'pyformat' over the other formats because these offer more clarity and flexibility. @@ -947,41 +947,41 @@ Footnotes [3] If the database does not support the functionality required by the method, the interface should throw an exception in case the method is used. - + The preferred approach is to not implement the method and thus have Python generate an AttributeError in case the method is requested. This allows the programmer to check for database capabilities using the standard hasattr() function. - + For some dynamically configured interfaces it may not be appropriate to require dynamically making the method available. These interfaces should then raise a NotSupportedError to indicate the non-ability to perform the roll back when the method is invoked. - + [4] a database interface may choose to support named cursors by allowing a string argument to the method. This feature is not part of the specification, since it complicates semantics of the .fetchXXX() methods. - + [5] The module will use the __getitem__ method of the parameters object to map either positions (integers) or names (strings) to parameter values. This allows for both sequences and mappings to be used as input. - + The term "bound" refers to the process of binding an input value to a database execution buffer. In practical terms, this means that the input value is directly used as a value in the operation. The client should not be required to "escape" the value so that it can be used -- the value should be equal to the actual database value. - + [6] Note that the interface may implement row fetching using arrays and other optimizations. It is not guaranteed that a call to this method will only move the associated cursor forward by one row. - + [7] The rowcount attribute may be coded in a way that updates its value dynamically. This can be useful for databases that return usable rowcount values only after the first call to diff --git a/doc/release.rst b/doc/release.rst index 00399c7f..3576cdc9 100644 --- a/doc/release.rst +++ b/doc/release.rst @@ -36,7 +36,7 @@ How to make a psycopg2 release - Create a signed tag with the content of the relevant NEWS bit and push it. E.g.:: - $ git tag -a -s 2_7 + $ git tag -a -s 2_7 Psycopg 2.7 released diff --git a/doc/src/advanced.rst b/doc/src/advanced.rst index b2b8afe3..d1683b8b 100644 --- a/doc/src/advanced.rst +++ b/doc/src/advanced.rst @@ -188,7 +188,7 @@ representation into the previously defined `!Point` class: ... return Point(float(m.group(1)), float(m.group(2))) ... else: ... raise InterfaceError("bad point representation: %r" % value) - + In order to create a mapping from a PostgreSQL type (either standard or user-defined), its OID must be known. It can be retrieved either by the second diff --git a/doc/src/connection.rst b/doc/src/connection.rst index 454f80d7..6f13ecd3 100644 --- a/doc/src/connection.rst +++ b/doc/src/connection.rst @@ -22,7 +22,7 @@ The ``connection`` class :ref:`thread-safety` for details. .. method:: cursor(name=None, cursor_factory=None, scrollable=None, withhold=False) - + Return a new `cursor` object using the connection. If *name* is specified, the returned cursor will be a :ref:`server @@ -274,8 +274,8 @@ The ``connection`` class .. __: http://jdbc.postgresql.org/ - Xids returned by `!tpc_recover()` also have extra attributes - `~psycopg2.extensions.Xid.prepared`, `~psycopg2.extensions.Xid.owner`, + Xids returned by `!tpc_recover()` also have extra attributes + `~psycopg2.extensions.Xid.prepared`, `~psycopg2.extensions.Xid.owner`, `~psycopg2.extensions.Xid.database` populated with the values read from the server. @@ -626,7 +626,7 @@ The ``connection`` class pair: Server; Parameters .. method:: get_parameter_status(parameter) - + Look up a current parameter setting of the server. Potential values for ``parameter`` are: ``server_version``, @@ -708,7 +708,7 @@ The ``connection`` class The number is formed by converting the major, minor, and revision numbers into two-decimal-digit numbers and appending them together. For example, version 8.1.5 will be returned as ``80105``. - + .. seealso:: libpq docs for `PQserverVersion()`__ for details. .. __: http://www.postgresql.org/docs/current/static/libpq-status.html#LIBPQ-PQSERVERVERSION @@ -722,7 +722,7 @@ The ``connection`` class .. attribute:: status A read-only integer representing the status of the connection. - Symbolic constants for the values are defined in the module + Symbolic constants for the values are defined in the module `psycopg2.extensions`: see :ref:`connection-status-constants` for the available values. diff --git a/doc/src/cursor.rst b/doc/src/cursor.rst index 417ab6cf..5a6935e6 100644 --- a/doc/src/cursor.rst +++ b/doc/src/cursor.rst @@ -34,10 +34,10 @@ The ``cursor`` class many cursors from the same connection and should use each cursor from a single thread. See :ref:`thread-safety` for details. - - .. attribute:: description - This read-only attribute is a sequence of 7-item sequences. + .. attribute:: description + + This read-only attribute is a sequence of 7-item sequences. Each of these sequences is a named tuple (a regular tuple if :func:`collections.namedtuple` is not available) containing information @@ -65,7 +65,7 @@ The ``cursor`` class This attribute will be `!None` for operations that do not return rows or if the cursor has not had an operation invoked via the |execute*|_ methods yet. - + .. |pg_type| replace:: :sql:`pg_type` .. _pg_type: http://www.postgresql.org/docs/current/static/catalog-pg-type.html .. _PQgetlength: http://www.postgresql.org/docs/current/static/libpq-exec.html#LIBPQ-PQGETLENGTH @@ -78,7 +78,7 @@ The ``cursor`` class regular tuples. .. method:: close() - + Close the cursor now (rather than whenever `del` is executed). The cursor will be unusable from this point forward; an `~psycopg2.InterfaceError` will be raised if any operation is @@ -88,7 +88,7 @@ The ``cursor`` class the method is automatically called at the end of the ``with`` block. - + .. attribute:: closed Read-only boolean attribute: specifies if the cursor is closed @@ -235,7 +235,7 @@ The ``cursor`` class The `mogrify()` method is a Psycopg extension to the |DBAPI|. .. method:: setinputsizes(sizes) - + This method is exposed in compliance with the |DBAPI|. It currently does nothing but it is safe to call it. @@ -281,17 +281,17 @@ The ``cursor`` class >>> cur.execute("SELECT * FROM test WHERE id = %s", (3,)) >>> cur.fetchone() (3, 42, 'bar') - + A `~psycopg2.ProgrammingError` is raised if the previous call to |execute*|_ did not produce any result set or no call was issued yet. .. method:: fetchmany([size=cursor.arraysize]) - + Fetch the next set of rows of a query result, returning a list of tuples. An empty list is returned when no more rows are available. - + The number of rows to fetch per call is specified by the parameter. If it is not given, the cursor's `~cursor.arraysize` determines the number of rows to be fetched. The method should try to fetch as @@ -309,7 +309,7 @@ The ``cursor`` class A `~psycopg2.ProgrammingError` is raised if the previous call to |execute*|_ did not produce any result set or no call was issued yet. - + Note there are performance considerations involved with the size parameter. For optimal performance, it is usually best to use the `~cursor.arraysize` attribute. If the size parameter is used, @@ -344,7 +344,7 @@ The ``cursor`` class `~psycopg2.ProgrammingError` is raised and the cursor position is not changed. - .. note:: + .. note:: According to the |DBAPI|_, the exception raised for a cursor out of bound should have been `!IndexError`. The best option is @@ -364,7 +364,7 @@ The ``cursor`` class .. attribute:: arraysize - + This read/write attribute specifies the number of rows to fetch at a time with `~cursor.fetchmany()`. It defaults to 1 meaning to fetch a single row at a time. @@ -378,20 +378,20 @@ The ``cursor`` class default is 2000. .. versionadded:: 2.4 - + .. extension:: The `itersize` attribute is a Psycopg extension to the |DBAPI|. - .. attribute:: rowcount - + .. attribute:: rowcount + This read-only attribute specifies the number of rows that the last |execute*|_ produced (for :abbr:`DQL (Data Query Language)` statements - like :sql:`SELECT`) or affected (for + like :sql:`SELECT`) or affected (for :abbr:`DML (Data Manipulation Language)` statements like :sql:`UPDATE` or :sql:`INSERT`). - + The attribute is -1 in case no |execute*| has been performed on the cursor or the row count of the last operation if it can't be determined by the interface. @@ -400,7 +400,7 @@ The ``cursor`` class The |DBAPI|_ interface reserves to redefine the latter case to have the object return `!None` instead of -1 in future versions of the specification. - + .. attribute:: rownumber @@ -457,7 +457,7 @@ The ``cursor`` class command: >>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)", (42, 'bar')) - >>> cur.statusmessage + >>> cur.statusmessage 'INSERT 0 1' .. extension:: @@ -490,13 +490,13 @@ The ``cursor`` class .. method:: nextset() - + This method is not supported (PostgreSQL does not have multiple data sets) and will raise a `~psycopg2.NotSupportedError` exception. .. method:: setoutputsize(size [, column]) - + This method is exposed in compliance with the |DBAPI|. It currently does nothing but it is safe to call it. diff --git a/doc/src/faq.rst b/doc/src/faq.rst index 0ef4c708..5824d2b4 100644 --- a/doc/src/faq.rst +++ b/doc/src/faq.rst @@ -334,4 +334,3 @@ Psycopg raises *ImportError: cannot import name tz* on import in mod_wsgi / ASP, .. _egg: http://peak.telecommunity.com/DevCenter/PythonEggs .. __: http://stackoverflow.com/questions/2192323/what-is-the-python-egg-cache-python-egg-cache .. __: http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIPythonEggs - diff --git a/doc/src/index.rst b/doc/src/index.rst index 30ba8fa1..852bbc2c 100644 --- a/doc/src/index.rst +++ b/doc/src/index.rst @@ -65,4 +65,3 @@ Psycopg 2 is both Unicode and Python 3 friendly. **To Do items in the documentation** .. todolist:: - diff --git a/doc/src/pool.rst b/doc/src/pool.rst index 32bca548..7624dc88 100644 --- a/doc/src/pool.rst +++ b/doc/src/pool.rst @@ -57,8 +57,7 @@ be used. .. autoclass:: PersistentConnectionPool - .. note:: + .. note:: This pool class is mostly designed to interact with Zope and probably not useful in generic applications. - diff --git a/doc/src/tools/lib/dbapi_extension.py b/doc/src/tools/lib/dbapi_extension.py index 7ab656fa..23d91652 100755 --- a/doc/src/tools/lib/dbapi_extension.py +++ b/doc/src/tools/lib/dbapi_extension.py @@ -49,4 +49,3 @@ def setup(app): text=(visit_extension_node, depart_extension_node)) app.add_directive('extension', Extension) - diff --git a/doc/src/tools/lib/sql_role.py b/doc/src/tools/lib/sql_role.py index 8fb8ab84..43347b4a 100644 --- a/doc/src/tools/lib/sql_role.py +++ b/doc/src/tools/lib/sql_role.py @@ -12,10 +12,9 @@ from docutils import nodes, utils from docutils.parsers.rst import roles def sql_role(name, rawtext, text, lineno, inliner, options={}, content=[]): - text = utils.unescape(text) + text = utils.unescape(text) options['classes'] = ['sql'] return [nodes.literal(rawtext, text, **options)], [] - + def setup(app): roles.register_local_role('sql', sql_role) - diff --git a/doc/src/tools/lib/ticket_role.py b/doc/src/tools/lib/ticket_role.py index d8ded227..0ee3d636 100644 --- a/doc/src/tools/lib/ticket_role.py +++ b/doc/src/tools/lib/ticket_role.py @@ -56,4 +56,3 @@ def setup(app): app.add_config_value('ticket_remap_offset', None, 'env') app.add_role('ticket', ticket_role) app.add_role('tickets', ticket_role) - diff --git a/doc/src/tools/stitch_text.py b/doc/src/tools/stitch_text.py index c9ed99aa..b36727b5 100755 --- a/doc/src/tools/stitch_text.py +++ b/doc/src/tools/stitch_text.py @@ -57,7 +57,6 @@ def emit(basename, txt_dir): # some space between sections sys.stdout.write("\n\n") - + if __name__ == '__main__': sys.exit(main()) - diff --git a/doc/src/tz.rst b/doc/src/tz.rst index 6e2e9811..06333f86 100644 --- a/doc/src/tz.rst +++ b/doc/src/tz.rst @@ -8,9 +8,8 @@ This module holds two different tzinfo implementations that can be used as the `tzinfo` argument to `~datetime.datetime` constructors, directly passed to Psycopg functions or used to set the `cursor.tzinfo_factory` attribute in -cursors. +cursors. .. autoclass:: psycopg2.tz.FixedOffsetTimezone .. autoclass:: psycopg2.tz.LocalTimezone - diff --git a/doc/src/usage.rst b/doc/src/usage.rst index 5dcab8fe..e52bb9fa 100644 --- a/doc/src/usage.rst +++ b/doc/src/usage.rst @@ -1017,4 +1017,3 @@ For further details see the documentation for the above methods. .. __: http://www.opengroup.org/bookstore/catalog/c193.htm .. __: http://jdbc.postgresql.org/ - diff --git a/examples/binary.py b/examples/binary.py index 3ba1ff89..665d962f 100644 --- a/examples/binary.py +++ b/examples/binary.py @@ -62,7 +62,7 @@ for row in curs.fetchall(): open(new_name, 'wb').write(row[2]) print "done" print " python type of image data is", type(row[2]) - + # extract exactly the same data but using a binary cursor print "Extracting the images using a binary cursor:" @@ -78,7 +78,7 @@ for row in curs.fetchall(): open(new_name, 'wb').write(row[0]) print "done" print " python type of image data is", type(row[0]) - + # this rollback is required because we can't drop a table with a binary cursor # declared and still open conn.rollback() diff --git a/examples/copy_from.py b/examples/copy_from.py index e258a077..57986dbf 100644 --- a/examples/copy_from.py +++ b/examples/copy_from.py @@ -1,4 +1,4 @@ -# copy_from.py -- example about copy_from +# copy_from.py -- example about copy_from # # Copyright (C) 2002 Tom Jenkins # Copyright (C) 2005 Federico Di Gregorio @@ -172,6 +172,3 @@ conn.rollback() curs.execute("DROP TABLE test_copy") os.unlink('copy_from.txt') conn.commit() - - - diff --git a/examples/copy_to.py b/examples/copy_to.py index 225155c1..34c90715 100644 --- a/examples/copy_to.py +++ b/examples/copy_to.py @@ -1,4 +1,4 @@ -# copy_to.py -- example about copy_to +# copy_to.py -- example about copy_to # # Copyright (C) 2002 Tom Jenkins # Copyright (C) 2005 Federico Di Gregorio diff --git a/examples/cursor.py b/examples/cursor.py index 465967c3..58c8cf67 100644 --- a/examples/cursor.py +++ b/examples/cursor.py @@ -49,7 +49,7 @@ class Cursor(psycopg2.extensions.cursor): if d is None: raise NoDataError("no more data") return d - + curs = conn.cursor(cursor_factory=Cursor) curs.execute("SELECT 1 AS foo") print("Result of fetchone():", curs.fetchone()) diff --git a/examples/dialtone.py b/examples/dialtone.py index a89021ce..8654ffde 100644 --- a/examples/dialtone.py +++ b/examples/dialtone.py @@ -6,14 +6,14 @@ Mapping arbitrary objects to a PostgreSQL database with psycopg2 - Problem You need to store arbitrary objects in a PostgreSQL database without being -intrusive for your classes (don't want inheritance from an 'Item' or +intrusive for your classes (don't want inheritance from an 'Item' or 'Persistent' object). - Solution """ from datetime import datetime - + import psycopg2 from psycopg2.extensions import adapt, register_adapter @@ -24,7 +24,7 @@ except: seq.sort() return seq -# Here is the adapter for every object that we may ever need to +# Here is the adapter for every object that we may ever need to # insert in the database. It receives the original object and does # its job on that instance @@ -33,7 +33,7 @@ class ObjectMapper(object): self.orig = orig self.tmp = {} self.items, self.fields = self._gatherState() - + def _gatherState(self): adaptee_name = self.orig.__class__.__name__ fields = sorted([(field, getattr(self.orig, field)) @@ -42,19 +42,19 @@ class ObjectMapper(object): for item, value in fields: items.append(item) return items, fields - + def getTableName(self): return self.orig.__class__.__name__ - + def getMappedValues(self): tmp = [] for i in self.items: tmp.append("%%(%s)s"%i) return ", ".join(tmp) - + def getValuesDict(self): return dict(self.fields) - + def getFields(self): return self.items @@ -66,14 +66,14 @@ class ObjectMapper(object): return qry, self.getValuesDict() # Here are the objects -class Album(object): - id = 0 +class Album(object): + id = 0 def __init__(self): self.creation_time = datetime.now() self.album_id = self.id Album.id = Album.id + 1 self.binary_data = buffer('12312312312121') - + class Order(object): id = 0 def __init__(self): @@ -84,7 +84,7 @@ class Order(object): register_adapter(Album, ObjectMapper) register_adapter(Order, ObjectMapper) - + # Describe what is needed to save on each object # This is actually just configuration, you can use xml with a parser if you # like to have plenty of wasted CPU cycles ;P. @@ -92,7 +92,7 @@ register_adapter(Order, ObjectMapper) persistent_fields = {'Album': ['album_id', 'creation_time', 'binary_data'], 'Order': ['order_id', 'items', 'price'] } - + print adapt(Album()).generateInsert() print adapt(Album()).generateInsert() print adapt(Album()).generateInsert() @@ -103,42 +103,42 @@ print adapt(Order()).generateInsert() """ - Discussion -Psycopg 2 has a great new feature: adaptation. The big thing about -adaptation is that it enables the programmer to glue most of the +Psycopg 2 has a great new feature: adaptation. The big thing about +adaptation is that it enables the programmer to glue most of the code out there without many difficulties. -This recipe tries to focus attention on a way to generate SQL queries to -insert completely new objects inside a database. As you can see objects do -not know anything about the code that is handling them. We specify all the +This recipe tries to focus attention on a way to generate SQL queries to +insert completely new objects inside a database. As you can see objects do +not know anything about the code that is handling them. We specify all the fields that we need for each object through the persistent_fields dict. The most important lines of this recipe are: register_adapter(Album, ObjectMapper) register_adapter(Order, ObjectMapper) -In these lines we notify the system that when we call adapt with an Album instance -as an argument we want it to istantiate ObjectMapper passing the Album instance +In these lines we notify the system that when we call adapt with an Album instance +as an argument we want it to istantiate ObjectMapper passing the Album instance as argument (self.orig in the ObjectMapper class). The output is something like this (for each call to generateInsert): - -('INSERT INTO Album (album_id, binary_data, creation_time) VALUES - (%(album_id)s, %(binary_data)s, %(creation_time)s)', - - {'binary_data': , - 'creation_time': datetime.datetime(2004, 9, 10, 20, 48, 29, 633728), + +('INSERT INTO Album (album_id, binary_data, creation_time) VALUES + (%(album_id)s, %(binary_data)s, %(creation_time)s)', + + {'binary_data': , + 'creation_time': datetime.datetime(2004, 9, 10, 20, 48, 29, 633728), 'album_id': 1} ) -This is a tuple of {SQL_QUERY, FILLING_DICT}, and all the quoting/converting -stuff (from python's datetime to postgres s and from python's buffer to -postgres' blob) is handled with the same adaptation process hunder the hood +This is a tuple of {SQL_QUERY, FILLING_DICT}, and all the quoting/converting +stuff (from python's datetime to postgres s and from python's buffer to +postgres' blob) is handled with the same adaptation process hunder the hood by psycopg2. -At last, just notice that ObjectMapper is working for both Album and Order -instances without any glitches at all, and both classes could have easily been -coming from closed source libraries or C coded ones (which are not easily -modified), whereas a common pattern in todays ORMs or OODBs is to provide -a basic 'Persistent' object that already knows how to store itself in the +At last, just notice that ObjectMapper is working for both Album and Order +instances without any glitches at all, and both classes could have easily been +coming from closed source libraries or C coded ones (which are not easily +modified), whereas a common pattern in todays ORMs or OODBs is to provide +a basic 'Persistent' object that already knows how to store itself in the database. """ diff --git a/examples/dict.py b/examples/dict.py index ede014a5..1503722c 100644 --- a/examples/dict.py +++ b/examples/dict.py @@ -29,7 +29,7 @@ print "Opening connection using dsn:", DSN conn = psycopg2.connect(DSN) print "Encoding for this connection is", conn.encoding - + curs = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) curs.execute("SELECT 1 AS foo, 'cip' AS bar, date(now()) as zot") print "Cursor's row factory is", curs.row_factory diff --git a/examples/encoding.py b/examples/encoding.py index c702b054..77fd871b 100644 --- a/examples/encoding.py +++ b/examples/encoding.py @@ -38,7 +38,7 @@ encs.sort() for a, b in encs: print " ", a, "<->", b -print "Using STRING typecaster" +print "Using STRING typecaster" print "Setting backend encoding to LATIN1 and executing queries:" conn.set_client_encoding('LATIN1') curs = conn.cursor() diff --git a/examples/myfirstrecipe.py b/examples/myfirstrecipe.py index c6680c31..e0df879f 100644 --- a/examples/myfirstrecipe.py +++ b/examples/myfirstrecipe.py @@ -34,7 +34,7 @@ a not-yet well defined protocol that we can call ISQLQuote: def getbinary(self): "Returns a binary quoted string representing the bound variable." - + def getbuffer(self): "Returns the wrapped object itself." @@ -86,10 +86,10 @@ class AsIs(object): self.__obj = obj def getquoted(self): return self.__obj - + class SQL_IN(object): """Adapt a tuple to an SQL quotable object.""" - + def __init__(self, seq): self._seq = seq @@ -103,10 +103,10 @@ class SQL_IN(object): qobjs = [str(psycoadapt(o).getquoted()) for o in self._seq] return '(' + ', '.join(qobjs) + ')' - + __str__ = getquoted - + # add our new adapter class to psycopg list of adapters register_adapter(tuple, SQL_IN) register_adapter(float, AsIs) @@ -117,7 +117,7 @@ register_adapter(int, AsIs) # conn = psycopg.connect("...") # curs = conn.cursor() # curs.execute("SELECT ...", (("this", "is", "the", "tuple"),)) -# +# # but we have no connection to a database right now, so we just check # the SQL_IN class by calling psycopg's adapt() directly: diff --git a/examples/threads.py b/examples/threads.py index d24b0a5d..e0be7e0c 100644 --- a/examples/threads.py +++ b/examples/threads.py @@ -44,7 +44,7 @@ if len(sys.argv) > 1: DSN = sys.argv[1] if len(sys.argv) > 2: MODE = int(sys.argv[2]) - + print "Opening connection using dsn:", DSN conn = psycopg2.connect(DSN) curs = conn.cursor() @@ -70,7 +70,7 @@ def insert_func(conn_or_pool, rows): conn = conn_or_pool else: conn = conn_or_pool.getconn() - + for i in range(rows): if divmod(i, COMMIT_STEP)[1] == 0: conn.commit() @@ -91,14 +91,14 @@ def insert_func(conn_or_pool, rows): ## a nice select function that prints the current number of rows in the ## database (and transfer them, putting some pressure on the network) - + def select_func(conn_or_pool, z): name = threading.currentThread().getName() if MODE == 0: conn = conn_or_pool conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) - + for i in range(SELECT_SIZE): if divmod(i, SELECT_STEP)[1] == 0: try: @@ -125,7 +125,7 @@ else: m = len(INSERT_THREADS) + len(SELECT_THREADS) n = m/2 conn_insert = conn_select = ThreadedConnectionPool(n, m, DSN) - + ## create the threads threads = [] diff --git a/examples/typecast.py b/examples/typecast.py index 0df883e7..169f3aca 100644 --- a/examples/typecast.py +++ b/examples/typecast.py @@ -63,5 +63,3 @@ print "Some text from cursor with typecaster:", curs.fetchone()[0] curs = conn.cursor() curs.execute("SELECT 'some text.'::text AS foo") print "Some text from connection with typecaster again:", curs.fetchone()[0] - - diff --git a/examples/tz.py b/examples/tz.py index 9a484f1e..c3dd8e43 100644 --- a/examples/tz.py +++ b/examples/tz.py @@ -60,10 +60,10 @@ print "Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d) curs.execute("SELECT * FROM test_tz") for d in curs: u = d[0].utcoffset() or ZERO - print "UTC time: ", d[0] - u + print "UTC time: ", d[0] - u print "Local time:", d[0] print "Time zone:", d[0].tzinfo.tzname(d[0]), d[0].tzinfo.utcoffset(d[0]) - + curs.execute("DROP TABLE test_tz") conn.commit() diff --git a/examples/usercast.py b/examples/usercast.py index 87c2eea4..26a5c16e 100644 --- a/examples/usercast.py +++ b/examples/usercast.py @@ -58,7 +58,7 @@ class Rect(object): and eventually as a type-caster for the data extracted from the database (that's why __init__ takes the curs argument.) """ - + def __init__(self, s=None, curs=None): """Init the rectangle from the optional string s.""" self.x = self.y = self.width = self.height = 0.0 @@ -68,7 +68,7 @@ class Rect(object): """This is a terrible hack, just ignore proto and return self.""" if proto == psycopg2.extensions.ISQLQuote: return self - + def from_points(self, x0, y0, x1, y1): """Init the rectangle from points.""" if x0 > x1: (x0, x1) = (x1, x0) @@ -94,7 +94,7 @@ class Rect(object): s = "X: %d\tY: %d\tWidth: %d\tHeight: %d" % ( self.x, self.y, self.width, self.height) return s - + # here we select from the empty table, just to grab the description curs.execute("SELECT b FROM test_cast WHERE 0=1") boxoid = curs.description[0][1] diff --git a/psycopg/bytes_format.c b/psycopg/bytes_format.c index 68662075..12899142 100644 --- a/psycopg/bytes_format.c +++ b/psycopg/bytes_format.c @@ -295,4 +295,3 @@ Bytes_Format(PyObject *format, PyObject *args) } return NULL; } - diff --git a/psycopg/notify_type.c b/psycopg/notify_type.c index e2589a6a..aab9a43b 100644 --- a/psycopg/notify_type.c +++ b/psycopg/notify_type.c @@ -295,5 +295,3 @@ PyTypeObject notifyType = { 0, /*tp_alloc*/ notify_new, /*tp_new*/ }; - - diff --git a/psycopg/typecast_binary.c b/psycopg/typecast_binary.c index 8c380239..e4839dab 100644 --- a/psycopg/typecast_binary.c +++ b/psycopg/typecast_binary.c @@ -312,4 +312,3 @@ psycopg_parse_escape(const char *bufin, Py_ssize_t sizein, Py_ssize_t *sizeout) exit: return ret; } - diff --git a/psycopg/typecast_builtins.c b/psycopg/typecast_builtins.c index 1c4a8ca0..446dd14b 100644 --- a/psycopg/typecast_builtins.c +++ b/psycopg/typecast_builtins.c @@ -69,4 +69,3 @@ static typecastObject_initlist typecast_builtins[] = { {"MACADDRARRAY", typecast_MACADDRARRAY_types, typecast_STRINGARRAY_cast, "STRING"}, {NULL, NULL, NULL, NULL} }; - diff --git a/psycopg/typecast_mxdatetime.c b/psycopg/typecast_mxdatetime.c index 12c734a6..637e4384 100644 --- a/psycopg/typecast_mxdatetime.c +++ b/psycopg/typecast_mxdatetime.c @@ -250,4 +250,3 @@ typecast_MXINTERVAL_cast(const char *str, Py_ssize_t len, PyObject *curs) #define typecast_DATETIME_cast typecast_MXDATE_cast #define typecast_DATETIMETZ_cast typecast_MXDATE_cast #endif - diff --git a/psycopg2.sln b/psycopg2.sln index 829468d2..68327dd8 100644 --- a/psycopg2.sln +++ b/psycopg2.sln @@ -1,5 +1,4 @@ - -Microsoft Visual Studio Solution File, Format Version 10.00 +Microsoft Visual Studio Solution File, Format Version 10.00 # Visual Studio 2008 Project("{2857B73E-F847-4B02-9238-064979017E93}") = "psycopg2", "psycopg2.cproj", "{CFD80D18-3EE5-49ED-992A-E6D433BC7641}" EndProject @@ -26,7 +25,7 @@ Global $2.DirectoryNamespaceAssociation = None $2.ResourceNamePolicy = FileName $0.StandardHeader = $3 - $3.Text = + $3.Text = $3.IncludeInNewFiles = False $0.TextStylePolicy = $4 $4.FileWidth = 72 diff --git a/sandbox/array.py b/sandbox/array.py index 2327a273..d7971561 100644 --- a/sandbox/array.py +++ b/sandbox/array.py @@ -28,4 +28,3 @@ curs = conn.cursor() curs.execute("SELECT %s", ([1,2,None],)) print curs.fetchone() - diff --git a/sandbox/async.py b/sandbox/async.py index 3766fbb9..e9fe2811 100644 --- a/sandbox/async.py +++ b/sandbox/async.py @@ -17,7 +17,7 @@ def sleep(curs): while not curs.isready(): print "." time.sleep(.1) - + #curs.execute(""" # DECLARE zz INSENSITIVE SCROLL CURSOR WITH HOLD FOR # SELECT now(); @@ -33,4 +33,3 @@ print curs.fetchall() curs.execute("SELECT now() AS bar") sleep(curs) - diff --git a/sandbox/gtk.py b/sandbox/gtk.py index f96e1a32..b00cd29b 100644 --- a/sandbox/gtk.py +++ b/sandbox/gtk.py @@ -16,4 +16,3 @@ sql() import gtk print "AFTER" sql() - diff --git a/sandbox/iter.py b/sandbox/iter.py index 74c8aba8..bf245ba9 100644 --- a/sandbox/iter.py +++ b/sandbox/iter.py @@ -7,8 +7,7 @@ curs = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) curs.execute("SELECT '2005-2-12'::date AS foo, 'boo!' as bar") for x in curs.fetchall(): print type(x), x[0], x[1], x['foo'], x['bar'] - + curs.execute("SELECT '2005-2-12'::date AS foo, 'boo!' as bar") for x in curs: print type(x), x[0], x[1], x['foo'], x['bar'] - diff --git a/sandbox/leak.test.py b/sandbox/leak.test.py index 040f31a2..c0bef371 100644 --- a/sandbox/leak.test.py +++ b/sandbox/leak.test.py @@ -14,7 +14,7 @@ two functions: # leak() will cause increasingly more RAM to be used by the script. $ python leak -# noleak() does not have the RAM usage problem. The only difference +# noleak() does not have the RAM usage problem. The only difference # between it and leak() is that 'rows' is created once, before the loop. $ python noleak @@ -80,4 +80,3 @@ except IndexError: # Run leak() or noleak(), whichever was indicated on the command line run_function() - diff --git a/sandbox/misc_dbapi_test.py b/sandbox/misc_dbapi_test.py index 64637d98..137ddb04 100644 --- a/sandbox/misc_dbapi_test.py +++ b/sandbox/misc_dbapi_test.py @@ -28,8 +28,8 @@ import psycopg2 as dbapi conn = dbapi.connect(database='test') - - + + cursor = conn.cursor() cursor.execute(""" @@ -41,5 +41,3 @@ cursor.execute(""" for row in cursor: print row - - diff --git a/sandbox/named.py b/sandbox/named.py index 6a107604..94d89811 100644 --- a/sandbox/named.py +++ b/sandbox/named.py @@ -5,7 +5,7 @@ class Portal(psycopg2.extensions.cursor): def __init__(self, name, curs): psycopg2.extensions.cursor.__init__( self, curs.connection, '"'+name+'"') - + CURSOR = psycopg2.extensions.new_type((1790,), "CURSOR", Portal) psycopg2.extensions.register_type(CURSOR) diff --git a/sandbox/pbool.py b/sandbox/pbool.py index 817e3ea0..d26f8b46 100644 --- a/sandbox/pbool.py +++ b/sandbox/pbool.py @@ -10,4 +10,3 @@ class B(object): return 'It is True' else: return 'It is False' - diff --git a/sandbox/test.py b/sandbox/test.py index e3d31a75..a2a2a6b6 100644 --- a/sandbox/test.py +++ b/sandbox/test.py @@ -31,7 +31,7 @@ def sleep(curs): while not curs.isready(): print "." time.sleep(.1) - + #curs.execute(""" # DECLARE zz INSENSITIVE SCROLL CURSOR WITH HOLD FOR # SELECT now(); @@ -47,4 +47,3 @@ def sleep(curs): #curs.execute("SELECT now() AS bar"); #sleep(curs) - diff --git a/sandbox/test814.py b/sandbox/test814.py index 53e7e7f8..1e20ba0d 100644 --- a/sandbox/test814.py +++ b/sandbox/test814.py @@ -6,4 +6,3 @@ curs = conn.cursor() curs.execute("SELECT true AS foo WHERE 'a' in %s", (("aa", "bb"),)) print curs.fetchall() print curs.query - diff --git a/sandbox/test_copy2.py b/sandbox/test_copy2.py index bdd6e9e6..088c17b3 100644 --- a/sandbox/test_copy2.py +++ b/sandbox/test_copy2.py @@ -40,4 +40,3 @@ dbconn.commit() cursor.close() dbconn.close() - diff --git a/sandbox/textfloat.py b/sandbox/textfloat.py index 22d65b8b..5383bb38 100644 --- a/sandbox/textfloat.py +++ b/sandbox/textfloat.py @@ -6,4 +6,3 @@ c = o.cursor() c.execute("SELECT 1.23::float AS foo") x = c.fetchone()[0] print x, type(x) - diff --git a/sandbox/ticket58.py b/sandbox/ticket58.py index 95520c1e..98b5452b 100644 --- a/sandbox/ticket58.py +++ b/sandbox/ticket58.py @@ -71,5 +71,3 @@ done = 1 cur.close() conn.close() - - diff --git a/sandbox/valgrind-python.supp b/sandbox/valgrind-python.supp index 0938184f..4364ae75 100644 --- a/sandbox/valgrind-python.supp +++ b/sandbox/valgrind-python.supp @@ -335,7 +335,7 @@ { Debian unstable with libc-i686 suppressions - Memcheck:Cond + Memcheck:Cond obj:/lib/ld-2.3.5.so obj:/lib/ld-2.3.5.so obj:/lib/tls/i686/cmov/libc-2.3.5.so @@ -348,10 +348,10 @@ fun:_PyImport_GetDynLoadFunc fun:_PyImport_LoadDynamicModule } - + { Debian unstable with libc-i686 suppressions - Memcheck:Cond + Memcheck:Cond obj:/lib/ld-2.3.5.so obj:/lib/ld-2.3.5.so obj:/lib/ld-2.3.5.so @@ -365,7 +365,7 @@ fun:_PyImport_GetDynLoadFunc fun:_PyImport_LoadDynamicModule } - + { Debian unstable with libc-i686 suppressions Memcheck:Addr4 @@ -471,7 +471,7 @@ { Debian unstable with libc-i686 suppressions Memcheck:Cond - obj:/lib/ld-2.3.5.so + obj:/lib/ld-2.3.5.so obj:/lib/tls/i686/cmov/libc-2.3.5.so obj:/lib/ld-2.3.5.so fun:_dl_open diff --git a/scripts/maketypes.sh b/scripts/maketypes.sh index ab133c17..4165c1fb 100644 --- a/scripts/maketypes.sh +++ b/scripts/maketypes.sh @@ -28,7 +28,7 @@ PGMINOR="`echo $PGVERSION | cut -d. -f2`" echo checking for postgresql major: $PGMAJOR echo checking for postgresql minor: $PGMINOR - + echo -n generating pgtypes.h ... awk '/#define .+OID/ {print "#define " $2 " " $3}' "$PGTYPE" \ > $SRCDIR/pgtypes.h @@ -37,5 +37,3 @@ echo -n generating typecast_builtins.c ... awk '/#define .+OID/ {print $2 " " $3}' "$PGTYPE" | \ python $SCRIPTSDIR/buildtypes.py >$SRCDIR/typecast_builtins.c echo " done" - - diff --git a/tests/dbapi20.py b/tests/dbapi20.py index ff98ddc6..4facf8e8 100644 --- a/tests/dbapi20.py +++ b/tests/dbapi20.py @@ -1,6 +1,6 @@ #!/usr/bin/env python -''' Python DB API 2.0 driver compliance unit test suite. - +''' 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 @@ -79,8 +79,8 @@ def str2bytes(sval): class DatabaseAPI20Test(unittest.TestCase): ''' 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 + 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. @@ -90,9 +90,9 @@ class DatabaseAPI20Test(unittest.TestCase): self.driver, connect_args and connect_kw_args. Class specification should be as follows: - import dbapi20 + import dbapi20 class mytest(dbapi20.DatabaseAPI20Test): - [...] + [...] Don't 'import DatabaseAPI20Test from dbapi20', or you will confuse the unit tester - just 'import dbapi20'. @@ -111,7 +111,7 @@ class DatabaseAPI20Test(unittest.TestCase): xddl2 = 'drop table %sbarflys' % table_prefix lowerfunc = 'lower' # Name of stored procedure to convert string->lowercase - + # Some drivers may need to override these helpers, for example adding # a 'commit' after the execute. def executeDDL1(self,cursor): @@ -135,10 +135,10 @@ class DatabaseAPI20Test(unittest.TestCase): try: cur = con.cursor() for ddl in (self.xddl1,self.xddl2): - try: + try: cur.execute(ddl) con.commit() - except self.driver.Error: + except self.driver.Error: # Assume table didn't exist. Other tests will check if # execute is busted. pass @@ -255,7 +255,7 @@ class DatabaseAPI20Test(unittest.TestCase): con.rollback() except self.driver.NotSupportedError: pass - + def test_cursor(self): con = self._connect() try: @@ -411,7 +411,7 @@ class DatabaseAPI20Test(unittest.TestCase): ) elif self.driver.paramstyle == 'named': cur.execute( - 'insert into %sbooze values (:beer)' % self.table_prefix, + 'insert into %sbooze values (:beer)' % self.table_prefix, {'beer':"Cooper's"} ) elif self.driver.paramstyle == 'format': @@ -551,7 +551,7 @@ class DatabaseAPI20Test(unittest.TestCase): tests. ''' populate = [ - "insert into %sbooze values ('%s')" % (self.table_prefix,s) + "insert into %sbooze values ('%s')" % (self.table_prefix,s) for s in self.samples ] return populate @@ -612,7 +612,7 @@ class DatabaseAPI20Test(unittest.TestCase): self.assertEqual(len(rows),6) rows = [r[0] for r in rows] rows.sort() - + # Make sure we get the right data back out for i in range(0,6): self.assertEqual(rows[i],self.samples[i], @@ -683,10 +683,10 @@ class DatabaseAPI20Test(unittest.TestCase): 'cursor.fetchall should return an empty list if ' 'a select query returns no rows' ) - + finally: con.close() - + def test_mixedfetch(self): con = self._connect() try: @@ -722,7 +722,7 @@ class DatabaseAPI20Test(unittest.TestCase): def help_nextset_setUp(self,cur): ''' Should create a procedure called deleteme - that returns two result sets, first the + that returns two result sets, first the number of rows in booze then "name from booze" ''' raise NotImplementedError('Helper not implemented') @@ -869,4 +869,3 @@ class DatabaseAPI20Test(unittest.TestCase): self.failUnless(hasattr(self.driver,'ROWID'), 'module.ROWID must be defined.' ) - From 94bb238b7035e3e46ebf504d4a9f2c9f330c0721 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Fri, 1 Dec 2017 22:03:38 -0800 Subject: [PATCH 56/96] Remove sorted() workaround; available on all supported Pythons Introduced in Python 2.4 and the oldest supported Python is 2.7. https://docs.python.org/2/library/functions.html#sorted --- examples/dialtone.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/examples/dialtone.py b/examples/dialtone.py index a89021ce..9b30eda4 100644 --- a/examples/dialtone.py +++ b/examples/dialtone.py @@ -17,13 +17,6 @@ from datetime import datetime import psycopg2 from psycopg2.extensions import adapt, register_adapter -try: - sorted() -except: - def sorted(seq): - seq.sort() - return seq - # Here is the adapter for every object that we may ever need to # insert in the database. It receives the original object and does # its job on that instance From 699be52e8b8a08e04e3486b73f748a7e489b35f2 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Tue, 28 Nov 2017 20:58:41 -0800 Subject: [PATCH 57/96] Remove unnecessary script_to_py3; make scripts compatible instead Part of the work towards moving tests out of the installed package. --- tests/test_connection.py | 4 ++-- tests/test_copy.py | 6 +++--- tests/test_module.py | 4 ++-- tests/test_notify.py | 6 +++--- tests/testutils.py | 28 ---------------------------- 5 files changed, 10 insertions(+), 38 deletions(-) diff --git a/tests/test_connection.py b/tests/test_connection.py index f13df18d..369c204c 100755 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -35,7 +35,7 @@ import psycopg2.errorcodes from psycopg2 import extensions as ext from testutils import ( - script_to_py3, unittest, decorate_all_tests, skip_if_no_superuser, + unittest, decorate_all_tests, skip_if_no_superuser, skip_before_postgres, skip_after_postgres, skip_before_libpq, ConnectingTestCase, skip_if_tpc_disabled, skip_if_windows, slow) @@ -1566,7 +1566,7 @@ while True: cur.execute(%(query)r, ("Hello, world!",)) """ % {'dsn': dsn, 'query': query}) - proc = sp.Popen([sys.executable, '-c', script_to_py3(script)], + proc = sp.Popen([sys.executable, '-c', script], stdout=sp.PIPE, stderr=sp.PIPE) (out, err) = proc.communicate() self.assertNotEqual(proc.returncode, 0) diff --git a/tests/test_copy.py b/tests/test_copy.py index c5e79138..a70e58e8 100755 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -32,7 +32,7 @@ from subprocess import Popen, PIPE import psycopg2 import psycopg2.extensions -from testutils import skip_copy_if_green, script_to_py3 +from testutils import skip_copy_if_green from testconfig import dsn @@ -324,7 +324,7 @@ except psycopg2.ProgrammingError: conn.close() """ % {'dsn': dsn}) - proc = Popen([sys.executable, '-c', script_to_py3(script)]) + proc = Popen([sys.executable, '-c', script]) proc.communicate() self.assertEqual(0, proc.returncode) @@ -343,7 +343,7 @@ except psycopg2.ProgrammingError: conn.close() """ % {'dsn': dsn}) - proc = Popen([sys.executable, '-c', script_to_py3(script)], stdout=PIPE) + proc = Popen([sys.executable, '-c', script], stdout=PIPE) proc.communicate() self.assertEqual(0, proc.returncode) diff --git a/tests/test_module.py b/tests/test_module.py index 4a4941c1..01565d45 100755 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -27,7 +27,7 @@ import sys from subprocess import Popen from testutils import (unittest, skip_before_postgres, - ConnectingTestCase, skip_copy_if_green, script_to_py3, slow) + ConnectingTestCase, skip_copy_if_green, slow) import psycopg2 @@ -322,7 +322,7 @@ sys.path.insert(0, %r) import _psycopg """ % (pardir, pkgdir)) - proc = Popen([sys.executable, '-c', script_to_py3(script)]) + proc = Popen([sys.executable, '-c', script]) proc.communicate() self.assertEqual(0, proc.returncode) diff --git a/tests/test_notify.py b/tests/test_notify.py index 0e74e1d5..97fa6d8e 100755 --- a/tests/test_notify.py +++ b/tests/test_notify.py @@ -26,7 +26,7 @@ from testutils import unittest import psycopg2 from psycopg2 import extensions -from testutils import ConnectingTestCase, script_to_py3, slow +from testutils import ConnectingTestCase, slow from testconfig import dsn import sys @@ -61,7 +61,7 @@ import %(module)s as psycopg2 import %(module)s.extensions as ext conn = psycopg2.connect(%(dsn)r) conn.set_isolation_level(ext.ISOLATION_LEVEL_AUTOCOMMIT) -print conn.get_backend_pid() +print(conn.get_backend_pid()) curs = conn.cursor() curs.execute("NOTIFY " %(name)r %(payload)r) curs.close() @@ -70,7 +70,7 @@ conn.close() 'module': psycopg2.__name__, 'dsn': dsn, 'sec': sec, 'name': name, 'payload': payload}) - return Popen([sys.executable, '-c', script_to_py3(script)], stdout=PIPE) + return Popen([sys.executable, '-c', script], stdout=PIPE) @slow def test_notifies_received_on_poll(self): diff --git a/tests/testutils.py b/tests/testutils.py index bd08d6c7..2ee07af2 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -371,34 +371,6 @@ def skip_if_windows(f): return skip_if_windows_ -def script_to_py3(script): - """Convert a script to Python3 syntax if required.""" - if sys.version_info[0] < 3: - return script - - import tempfile - f = tempfile.NamedTemporaryFile(suffix=".py", delete=False) - f.write(script.encode()) - f.flush() - filename = f.name - f.close() - - # 2to3 is way too chatty - import logging - logging.basicConfig(filename=os.devnull) - - from lib2to3.main import main - if main("lib2to3.fixes", ['--no-diffs', '-w', '-n', filename]): - raise Exception('py3 conversion failed') - - f2 = open(filename) - try: - return f2.read() - finally: - f2.close() - os.remove(filename) - - class py3_raises_typeerror(object): def __enter__(self): pass From 7617e1dbf58e2d68641c5d4fcb2f824516320013 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 2 Dec 2017 14:46:03 +0000 Subject: [PATCH 58/96] Dropped .sln file --- psycopg2.sln | 43 ------------------------------------------- 1 file changed, 43 deletions(-) delete mode 100644 psycopg2.sln diff --git a/psycopg2.sln b/psycopg2.sln deleted file mode 100644 index 68327dd8..00000000 --- a/psycopg2.sln +++ /dev/null @@ -1,43 +0,0 @@ -Microsoft Visual Studio Solution File, Format Version 10.00 -# Visual Studio 2008 -Project("{2857B73E-F847-4B02-9238-064979017E93}") = "psycopg2", "psycopg2.cproj", "{CFD80D18-3EE5-49ED-992A-E6D433BC7641}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {CFD80D18-3EE5-49ED-992A-E6D433BC7641}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {CFD80D18-3EE5-49ED-992A-E6D433BC7641}.Debug|Any CPU.Build.0 = Debug|Any CPU - {CFD80D18-3EE5-49ED-992A-E6D433BC7641}.Release|Any CPU.ActiveCfg = Release|Any CPU - {CFD80D18-3EE5-49ED-992A-E6D433BC7641}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(MonoDevelopProperties) = preSolution - StartupItem = psycopg2.cproj - Policies = $0 - $0.TextStylePolicy = $1 - $1.FileWidth = 120 - $1.TabWidth = 4 - $1.inheritsSet = Mono - $1.inheritsScope = text/plain - $0.DotNetNamingPolicy = $2 - $2.DirectoryNamespaceAssociation = None - $2.ResourceNamePolicy = FileName - $0.StandardHeader = $3 - $3.Text = - $3.IncludeInNewFiles = False - $0.TextStylePolicy = $4 - $4.FileWidth = 72 - $4.NoTabsAfterNonTabs = True - $4.RemoveTrailingWhitespace = True - $4.inheritsSet = VisualStudio - $4.inheritsScope = text/plain - $4.scope = text/x-readme - $0.TextStylePolicy = $5 - $5.inheritsSet = VisualStudio - $5.inheritsScope = text/plain - $5.scope = text/plain - name = psycopg2 - EndGlobalSection -EndGlobal From 861e389fcf65bdf1fc168f987506c4103eabe5da Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 4 Dec 2017 11:56:27 +0000 Subject: [PATCH 59/96] Dropped psycopg1 module --- NEWS | 1 + lib/psycopg1.py | 96 ------------------------------------------------- psycopg2.cproj | 1 - 3 files changed, 1 insertion(+), 97 deletions(-) delete mode 100644 lib/psycopg1.py diff --git a/NEWS b/NEWS index 54094e90..500125b4 100644 --- a/NEWS +++ b/NEWS @@ -7,6 +7,7 @@ What's new in psycopg 2.8 Other changes: - Dropped support for Python 2.6, 3.2, 3.3. +- Dropped `psycopg1` module. What's new in psycopg 2.7.4 diff --git a/lib/psycopg1.py b/lib/psycopg1.py deleted file mode 100644 index 0ee79422..00000000 --- a/lib/psycopg1.py +++ /dev/null @@ -1,96 +0,0 @@ -"""psycopg 1.1.x compatibility module - -This module uses the new style connection and cursor types to build a psycopg -1.1.1.x compatibility layer. It should be considered a temporary hack to run -old code while porting to psycopg 2. Import it as follows:: - - from psycopg2 import psycopg1 as psycopg -""" -# psycopg/psycopg1.py - psycopg 1.1.x compatibility module -# -# Copyright (C) 2003-2010 Federico Di Gregorio -# -# psycopg2 is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# In addition, as a special exception, the copyright holders give -# permission to link this program with the OpenSSL library (or with -# modified versions of OpenSSL that use the same license as OpenSSL), -# and distribute linked combinations including the two. -# -# You must obey the GNU Lesser General Public License in all respects for -# all of the code used other than OpenSSL. -# -# psycopg2 is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. - -from psycopg2 import _psycopg as _2psycopg # noqa -from psycopg2.extensions import cursor as _2cursor -from psycopg2.extensions import connection as _2connection - -from psycopg2 import * # noqa -from psycopg2 import extensions as _ext -_2connect = connect - - -def connect(*args, **kwargs): - """connect(dsn, ...) -> new psycopg 1.1.x compatible connection object""" - kwargs['connection_factory'] = connection - conn = _2connect(*args, **kwargs) - conn.set_isolation_level(_ext.ISOLATION_LEVEL_READ_COMMITTED) - return conn - - -class connection(_2connection): - """psycopg 1.1.x connection.""" - - def cursor(self): - """cursor() -> new psycopg 1.1.x compatible cursor object""" - return _2connection.cursor(self, cursor_factory=cursor) - - def autocommit(self, on_off=1): - """autocommit(on_off=1) -> switch autocommit on (1) or off (0)""" - if on_off > 0: - self.set_isolation_level(_ext.ISOLATION_LEVEL_AUTOCOMMIT) - else: - self.set_isolation_level(_ext.ISOLATION_LEVEL_READ_COMMITTED) - - -class cursor(_2cursor): - """psycopg 1.1.x cursor. - - Note that this cursor implements the exact procedure used by psycopg 1 to - build dictionaries out of result rows. The DictCursor in the - psycopg.extras modules implements a much better and faster algorithm. - """ - - def __build_dict(self, row): - res = {} - for i in range(len(self.description)): - res[self.description[i][0]] = row[i] - return res - - def dictfetchone(self): - row = _2cursor.fetchone(self) - if row: - return self.__build_dict(row) - else: - return row - - def dictfetchmany(self, size): - res = [] - rows = _2cursor.fetchmany(self, size) - for row in rows: - res.append(self.__build_dict(row)) - return res - - def dictfetchall(self): - res = [] - rows = _2cursor.fetchall(self) - for row in rows: - res.append(self.__build_dict(row)) - return res diff --git a/psycopg2.cproj b/psycopg2.cproj index f6f85c72..a1ca2a94 100644 --- a/psycopg2.cproj +++ b/psycopg2.cproj @@ -73,7 +73,6 @@ - From 19b1efd62962e4144a3579570828bd2cde02ac5c Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 10 Dec 2017 07:54:15 -0800 Subject: [PATCH 60/96] Drop long deprecated function register_tstz_w_secs() Deprecated in commit b263fbf274f9085a1bddca018ed8a50d37023fc7 on 2010-01-13. The deprecation warning was first released in version 2.2.2. The function used to register an alternate type caster for TIMESTAMP WITH TIME ZONE to deal with historical time zones with seconds in the UTC offset. These are now correctly handled by the default type caster, so currently the function doesn't do anything. --- NEWS | 1 + doc/src/extras.rst | 14 -------------- doc/src/usage.rst | 3 +-- lib/extras.py | 12 ------------ 4 files changed, 2 insertions(+), 28 deletions(-) diff --git a/NEWS b/NEWS index 500125b4..fe770278 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,7 @@ Other changes: - Dropped support for Python 2.6, 3.2, 3.3. - Dropped `psycopg1` module. +- Dropped deprecated ``register_tstz_w_secs()`` (was previously a no-op). What's new in psycopg 2.7.4 diff --git a/doc/src/extras.rst b/doc/src/extras.rst index 1342f13c..15f35e80 100644 --- a/doc/src/extras.rst +++ b/doc/src/extras.rst @@ -1025,20 +1025,6 @@ parameters. By reducing the number of server roundtrips the performance can be .. versionadded:: 2.7 - -.. index:: - single: Time zones; Fractional - -Fractional time zones ---------------------- - -.. autofunction:: register_tstz_w_secs - - .. versionadded:: 2.0.9 - - .. versionchanged:: 2.2.2 - function is no-op: see :ref:`tz-handling`. - .. index:: pair: Example; Coroutine; diff --git a/doc/src/usage.rst b/doc/src/usage.rst index e52bb9fa..db8674c1 100644 --- a/doc/src/usage.rst +++ b/doc/src/usage.rst @@ -560,8 +560,7 @@ rounded to the nearest minute, with an error of up to 30 seconds. .. versionchanged:: 2.2.2 timezones with seconds are supported (with rounding). Previously such - timezones raised an error. In order to deal with them in previous - versions use `psycopg2.extras.register_tstz_w_secs()`. + timezones raised an error. .. index:: diff --git a/lib/extras.py b/lib/extras.py index a6c025e1..68df344c 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -719,18 +719,6 @@ def register_inet(oid=None, conn_or_curs=None): return _ext.INET -def register_tstz_w_secs(oids=None, conn_or_curs=None): - """The function used to register an alternate type caster for - :sql:`TIMESTAMP WITH TIME ZONE` to deal with historical time zones with - seconds in the UTC offset. - - These are now correctly handled by the default type caster, so currently - the function doesn't do anything. - """ - import warnings - warnings.warn("deprecated", DeprecationWarning) - - def wait_select(conn): """Wait until a connection or cursor has data available. From 1047af03da390a36ff20ffe02cbe9f5cd1c40a9c Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 10 Dec 2017 09:57:43 -0800 Subject: [PATCH 61/96] Remove unused scripts maketypes.sh & buildtypes.py Refs #645 --- MANIFEST.in | 1 - psycopg2.cproj | 2 - scripts/buildtypes.py | 119 ------------------------------------------ scripts/maketypes.sh | 39 -------------- 4 files changed, 161 deletions(-) delete mode 100644 scripts/buildtypes.py delete mode 100644 scripts/maketypes.sh diff --git a/MANIFEST.in b/MANIFEST.in index 0d34fd3d..bae0dbdd 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -6,6 +6,5 @@ include doc/README.rst doc/SUCCESS doc/COPYING.LESSER doc/pep-0249.txt include doc/Makefile doc/requirements.txt recursive-include doc/src *.rst *.py *.css Makefile recursive-include scripts *.py *.sh -include scripts/maketypes.sh scripts/buildtypes.py include AUTHORS README.rst INSTALL LICENSE NEWS include MANIFEST.in setup.py setup.cfg Makefile diff --git a/psycopg2.cproj b/psycopg2.cproj index a1ca2a94..331bd68d 100644 --- a/psycopg2.cproj +++ b/psycopg2.cproj @@ -97,8 +97,6 @@ - - diff --git a/scripts/buildtypes.py b/scripts/buildtypes.py deleted file mode 100644 index 5ae6c947..00000000 --- a/scripts/buildtypes.py +++ /dev/null @@ -1,119 +0,0 @@ -# -*- python -*- -# -# Copyright (C) 2001-2003 Federico Di Gregorio -# -# This file is part of the psycopg module. -# -# psycopg2 is free software: you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published -# by the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# psycopg2 is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or -# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public -# License for more details. -# -# this a little script that analyze a file with (TYPE, NUMBER) tuples -# and write out C code ready for inclusion in psycopg. the generated -# code defines the DBAPITypeObject fundamental types and warns for -# undefined types. - -import sys -from string import split, strip - - -# here is the list of the foundamental types we want to import from -# postgresql header files - -basic_types = (['NUMBER', ['INT8', 'INT4', 'INT2', 'FLOAT8', 'FLOAT4', - 'NUMERIC']], - ['LONGINTEGER', ['INT8']], - ['INTEGER', ['INT4', 'INT2']], - ['FLOAT', ['FLOAT8', 'FLOAT4']], - ['DECIMAL', ['NUMERIC']], - ['UNICODE', ['NAME', 'CHAR', 'TEXT', 'BPCHAR', - 'VARCHAR']], - ['STRING', ['NAME', 'CHAR', 'TEXT', 'BPCHAR', - 'VARCHAR']], - ['BOOLEAN', ['BOOL']], - ['DATETIME', ['TIMESTAMP', 'TIMESTAMPTZ', - 'TINTERVAL', 'INTERVAL']], - ['TIME', ['TIME', 'TIMETZ']], - ['DATE', ['DATE']], - ['INTERVAL', ['TINTERVAL', 'INTERVAL']], - ['BINARY', ['BYTEA']], - ['ROWID', ['OID']]) - -# unfortunately we don't have a nice way to extract array information -# from postgresql headers; we'll have to do it hard-coding :/ -array_types = (['LONGINTEGER', [1016]], - ['INTEGER', [1005, 1006, 1007]], - ['FLOAT', [1017, 1021, 1022]], - ['DECIMAL', [1231]], - ['UNICODE', [1002, 1003, 1009, 1014, 1015]], - ['STRING', [1002, 1003, 1009, 1014, 1015]], - ['BOOLEAN', [1000]], - ['DATETIME', [1115, 1185]], - ['TIME', [1183, 1270]], - ['DATE', [1182]], - ['INTERVAL', [1187]], - ['BINARY', [1001]], - ['ROWID', [1028, 1013]]) - -# this is the header used to compile the data in the C module -HEADER = """ -typecastObject_initlist typecast_builtins[] = { -""" - -# then comes the footer -FOOTER = """ {NULL, NULL, NULL, NULL}\n};\n""" - - -# useful error reporting function -def error(msg): - """Report an error on stderr.""" - sys.stderr.write(msg + '\n') - -# read couples from stdin and build list -read_types = [] -for l in sys.stdin.readlines(): - oid, val = split(l) - read_types.append((strip(oid)[:-3], strip(val))) - -# look for the wanted types in the read touples -found_types = {} - -for t in basic_types: - k = t[0] - found_types[k] = [] - for v in t[1]: - found = filter(lambda x, y=v: x[0] == y, read_types) - if len(found) == 0: - error(v + ': value not found') - elif len(found) > 1: - error(v + ': too many values') - else: - found_types[k].append(int(found[0][1])) - -# now outputs to stdout the right C-style definitions -stypes = sstruct = "" -for t in basic_types: - k = t[0] - s = str(found_types[k]) - s = '{' + s[1:-1] + ', 0}' - stypes = stypes + ('static long int typecast_%s_types[] = %s;\n' % (k, s)) - sstruct += (' {"%s", typecast_%s_types, typecast_%s_cast, NULL},\n' - % (k, k, k)) -for t in array_types: - kt = t[0] - ka = t[0] + 'ARRAY' - s = str(t[1]) - s = '{' + s[1:-1] + ', 0}' - stypes = stypes + ('static long int typecast_%s_types[] = %s;\n' % (ka, s)) - sstruct += (' {"%s", typecast_%s_types, typecast_%s_cast, "%s"},\n' - % (ka, ka, ka, kt)) -sstruct = HEADER + sstruct + FOOTER - -print stypes -print sstruct diff --git a/scripts/maketypes.sh b/scripts/maketypes.sh deleted file mode 100644 index 4165c1fb..00000000 --- a/scripts/maketypes.sh +++ /dev/null @@ -1,39 +0,0 @@ -#!/bin/sh - -SCRIPTSDIR="`dirname $0`" -SRCDIR="`dirname $SCRIPTSDIR`/psycopg" - -if [ -z "$1" ] ; then - echo Usage: $0 '' - exit 1 -fi - -echo -n checking for pg_type.h ... -if [ -f "$1/catalog/pg_type.h" ] ; then - PGTYPE="$1/catalog/pg_type.h" -else - if [ -f "$1/server/catalog/pg_type.h" ] ; then - PGTYPE="$1/server/catalog/pg_type.h" - else - echo - echo "error: can't find pg_type.h under $1" - exit 2 - fi -fi -echo " found" - -PGVERSION="`sed -n -e 's/.*PG_VERSION \"\([0-9]\.[0-9]\).*\"/\1/p' $1/pg_config.h`" -PGMAJOR="`echo $PGVERSION | cut -d. -f1`" -PGMINOR="`echo $PGVERSION | cut -d. -f2`" - -echo checking for postgresql major: $PGMAJOR -echo checking for postgresql minor: $PGMINOR - -echo -n generating pgtypes.h ... -awk '/#define .+OID/ {print "#define " $2 " " $3}' "$PGTYPE" \ - > $SRCDIR/pgtypes.h -echo " done" -echo -n generating typecast_builtins.c ... -awk '/#define .+OID/ {print $2 " " $3}' "$PGTYPE" | \ - python $SCRIPTSDIR/buildtypes.py >$SRCDIR/typecast_builtins.c -echo " done" From ef64493b8913e4069c4422ad14da6de405c445f6 Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 3 Dec 2017 18:47:19 -0800 Subject: [PATCH 62/96] Fix use of "async" in test_cursor.py "async" will be a keyword starting with Python 3.7. On Python 3.6, use of "async" causes a deprecation warning. Use the alias "async_" instead. --- tests/test_cursor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_cursor.py b/tests/test_cursor.py index e896ef94..5c94f422 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -553,7 +553,7 @@ class CursorTests(ConnectingTestCase): # Issue #443 is in the async code too. Since the fix is duplicated, # so is the test. control_conn = self.conn - connect_func = lambda: self.connect(async=True) + connect_func = lambda: self.connect(async_=True) wait_func = psycopg2.extras.wait_select self._test_external_close(control_conn, connect_func, wait_func) From 9de46e416e5ac1be1c5ff170d1c1ada5b8d7278f Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 3 Dec 2017 18:47:19 -0800 Subject: [PATCH 63/96] Use print() function instead of print statement throughout project Forward compatible with newer Pythons. --- doc/src/conf.py | 2 +- examples/binary.py | 23 +++++----- examples/copy_to.py | 33 +++++++------- examples/dialtone.py | 12 +++--- examples/dict.py | 40 ++++++++--------- examples/dt.py | 20 ++++----- examples/encoding.py | 52 +++++++++++------------ examples/fetch.py | 14 +++--- examples/lastrowid.py | 10 ++--- examples/lobject.py | 38 ++++++++--------- examples/mogrify.py | 16 +++---- examples/myfirstrecipe.py | 4 +- examples/notify.py | 10 ++--- examples/simple.py | 10 ++--- examples/threads.py | 8 ++-- examples/typecast.py | 14 +++--- examples/tz.py | 20 ++++----- examples/usercast.py | 12 +++--- sandbox/array.py | 2 +- sandbox/async.py | 6 +-- sandbox/crash.py | 2 +- sandbox/dec2float.py | 2 +- sandbox/domainoid.py | 6 +-- sandbox/gtk.py | 8 ++-- sandbox/iter.py | 4 +- sandbox/leak.test.py | 8 ++-- sandbox/misc_dbapi_test.py | 6 +-- sandbox/named.py | 6 +-- sandbox/pbool.py | 2 +- sandbox/stress.py | 2 +- sandbox/stress2.py | 2 +- sandbox/test.py | 16 +++---- sandbox/test814.py | 4 +- sandbox/test_green_error.py | 12 +++--- sandbox/test_isready_connection_closed.py | 12 +++--- sandbox/textfloat.py | 2 +- sandbox/ticket58.py | 6 +-- sandbox/trigger-commit-fail.py | 12 +++--- sandbox/trigger_double_dealloc.py | 12 +++--- sandbox/typecrash.py | 10 ++--- sandbox/tzhalf.py | 2 +- scripts/make_errorcodes.py | 9 ++-- scripts/refcounter.py | 1 + tests/__init__.py | 4 +- 44 files changed, 251 insertions(+), 245 deletions(-) diff --git a/doc/src/conf.py b/doc/src/conf.py index 6afb2869..a27d6cf4 100644 --- a/doc/src/conf.py +++ b/doc/src/conf.py @@ -57,7 +57,7 @@ try: release = psycopg2.__version__.split()[0] version = '.'.join(release.split('.')[:2]) except ImportError: - print "WARNING: couldn't import psycopg to read version." + print("WARNING: couldn't import psycopg to read version.") release = version intersphinx_mapping = { diff --git a/examples/binary.py b/examples/binary.py index 665d962f..df5d1eab 100644 --- a/examples/binary.py +++ b/examples/binary.py @@ -17,6 +17,7 @@ DSN = 'dbname=test' ## don't modify anything below this line (except for experimenting) +from __future__ import print_function import sys import psycopg2 @@ -24,9 +25,9 @@ import psycopg2 if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) -print "Encoding for this connection is", conn.encoding +print("Encoding for this connection is", conn.encoding) curs = conn.cursor() try: @@ -52,20 +53,20 @@ curs.execute("""INSERT INTO test_binary # now we try to extract the images as simple text strings -print "Extracting the images as strings..." +print("Extracting the images as strings...") curs.execute("SELECT * FROM test_binary") for row in curs.fetchall(): name, ext = row[1].split('.') new_name = name + '_S.' + ext - print " writing %s to %s ..." % (name+'.'+ext, new_name), + print(" writing %s to %s ..." % (name+'.'+ext, new_name), end=' ') open(new_name, 'wb').write(row[2]) - print "done" - print " python type of image data is", type(row[2]) + print("done") + print(" python type of image data is", type(row[2])) # extract exactly the same data but using a binary cursor -print "Extracting the images using a binary cursor:" +print("Extracting the images using a binary cursor:") curs.execute("""DECLARE zot CURSOR FOR SELECT img, name FROM test_binary FOR READ ONLY""") @@ -74,10 +75,10 @@ curs.execute("""FETCH ALL FROM zot""") for row in curs.fetchall(): name, ext = row[1].split('.') new_name = name + '_B.' + ext - print " writing %s to %s ..." % (name+'.'+ext, new_name), + print(" writing %s to %s ..." % (name+'.'+ext, new_name), end=' ') open(new_name, 'wb').write(row[0]) - print "done" - print " python type of image data is", type(row[0]) + print("done") + print(" python type of image data is", type(row[0])) # this rollback is required because we can't drop a table with a binary cursor # declared and still open @@ -86,4 +87,4 @@ conn.rollback() curs.execute("DROP TABLE test_binary") conn.commit() -print "\nNow try to load the new images, to check it worked!" +print("\nNow try to load the new images, to check it worked!") diff --git a/examples/copy_to.py b/examples/copy_to.py index 34c90715..d5cd0ff8 100644 --- a/examples/copy_to.py +++ b/examples/copy_to.py @@ -18,6 +18,7 @@ DSN = 'dbname=test' ## don't modify anything below this line (except for experimenting) +from __future__ import print_function import sys import os @@ -27,9 +28,9 @@ import psycopg2 if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) -print "Encoding for this connection is", conn.encoding +print("Encoding for this connection is", conn.encoding) curs = conn.cursor() try: @@ -51,52 +52,52 @@ conn.commit() # copy_to using defaults io = open('copy_to.txt', 'w') curs.copy_to(io, 'test_copy') -print "1) Copy %d records into file object using defaults: " % len (data) + \ - "sep = \\t and null = \\N" +print("1) Copy %d records into file object using defaults: " % len (data) + \ + "sep = \\t and null = \\N") io.close() rows = open('copy_to.txt', 'r').readlines() -print " File has %d rows:" % len(rows) +print(" File has %d rows:" % len(rows)) for r in rows: - print " ", r, + print(" ", r, end=' ') # copy_to using custom separator io = open('copy_to.txt', 'w') curs.copy_to(io, 'test_copy', ':') -print "2) Copy %d records into file object using sep = :" % len(data) +print("2) Copy %d records into file object using sep = :" % len(data)) io.close() rows = open('copy_to.txt', 'r').readlines() -print " File has %d rows:" % len(rows) +print(" File has %d rows:" % len(rows)) for r in rows: - print " ", r, + print(" ", r, end=' ') # copy_to using custom null identifier io = open('copy_to.txt', 'w') curs.copy_to(io, 'test_copy', null='NULL') -print "3) Copy %d records into file object using null = NULL" % len(data) +print("3) Copy %d records into file object using null = NULL" % len(data)) io.close() rows = open('copy_to.txt', 'r').readlines() -print " File has %d rows:" % len(rows) +print(" File has %d rows:" % len(rows)) for r in rows: - print " ", r, + print(" ", r, end=' ') # copy_to using custom separator and null identifier io = open('copy_to.txt', 'w') curs.copy_to(io, 'test_copy', ':', 'NULL') -print "4) Copy %d records into file object using sep = : and null ) NULL" % \ - len(data) +print("4) Copy %d records into file object using sep = : and null ) NULL" % \ + len(data)) io.close() rows = open('copy_to.txt', 'r').readlines() -print " File has %d rows:" % len(rows) +print(" File has %d rows:" % len(rows)) for r in rows: - print " ", r, + print(" ", r, end=' ') curs.execute("DROP TABLE test_copy") os.unlink('copy_to.txt') diff --git a/examples/dialtone.py b/examples/dialtone.py index f20d6fe7..f588eff8 100644 --- a/examples/dialtone.py +++ b/examples/dialtone.py @@ -86,12 +86,12 @@ persistent_fields = {'Album': ['album_id', 'creation_time', 'binary_data'], 'Order': ['order_id', 'items', 'price'] } -print adapt(Album()).generateInsert() -print adapt(Album()).generateInsert() -print adapt(Album()).generateInsert() -print adapt(Order()).generateInsert() -print adapt(Order()).generateInsert() -print adapt(Order()).generateInsert() +print(adapt(Album()).generateInsert()) +print(adapt(Album()).generateInsert()) +print(adapt(Album()).generateInsert()) +print(adapt(Order()).generateInsert()) +print(adapt(Order()).generateInsert()) +print(adapt(Order()).generateInsert()) """ - Discussion diff --git a/examples/dict.py b/examples/dict.py index 1503722c..b5d0b3a4 100644 --- a/examples/dict.py +++ b/examples/dict.py @@ -25,41 +25,41 @@ import psycopg2.extras if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) -print "Encoding for this connection is", conn.encoding +print("Encoding for this connection is", conn.encoding) curs = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) curs.execute("SELECT 1 AS foo, 'cip' AS bar, date(now()) as zot") -print "Cursor's row factory is", curs.row_factory +print("Cursor's row factory is", curs.row_factory) data = curs.fetchone() -print "The type of the data row is", type(data) -print "Some data accessed both as tuple and dict:" -print " ", data['foo'], data['bar'], data['zot'] -print " ", data[0], data[1], data[2] +print("The type of the data row is", type(data)) +print("Some data accessed both as tuple and dict:") +print(" ", data['foo'], data['bar'], data['zot']) +print(" ", data[0], data[1], data[2]) # execute another query and demostrate we can still access the row curs.execute("SELECT 2 AS foo") -print "The type of the data row is", type(data) -print "Some more data accessed both as tuple and dict:" -print " ", data['foo'], data['bar'], data['zot'] -print " ", data[0], data[1], data[2] +print("The type of the data row is", type(data)) +print("Some more data accessed both as tuple and dict:") +print(" ", data['foo'], data['bar'], data['zot']) +print(" ", data[0], data[1], data[2]) curs = conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) curs.execute("SELECT 1 AS foo, 'cip' AS bar, date(now()) as zot") -print "Cursor's row factory is", curs.row_factory +print("Cursor's row factory is", curs.row_factory) data = curs.fetchone() -print "The type of the data row is", type(data) -print "Some data accessed both as tuple and dict:" -print " ", data['foo'], data['bar'], data['zot'] -print " ", "No access using indices: this is a specialized cursor." +print("The type of the data row is", type(data)) +print("Some data accessed both as tuple and dict:") +print(" ", data['foo'], data['bar'], data['zot']) +print(" ", "No access using indices: this is a specialized cursor.") # execute another query and demostrate we can still access the row curs.execute("SELECT 2 AS foo") -print "The type of the data row is", type(data) -print "Some more data accessed both as tuple and dict:" -print " ", data['foo'], data['bar'], data['zot'] -print " ", "No access using indices: this is a specialized cursor." +print("The type of the data row is", type(data)) +print("Some more data accessed both as tuple and dict:") +print(" ", data['foo'], data['bar'], data['zot']) +print(" ", "No access using indices: this is a specialized cursor.") diff --git a/examples/dt.py b/examples/dt.py index 4c2a9a0b..34b25b3d 100644 --- a/examples/dt.py +++ b/examples/dt.py @@ -28,7 +28,7 @@ from psycopg2.extensions import adapt if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) curs = conn.cursor() @@ -52,9 +52,9 @@ mx1 = ( from psycopg2.extensions import adapt import psycopg2.extras -print adapt(mx1) +print(adapt(mx1)) -print "Inserting mx.DateTime values..." +print("Inserting mx.DateTime values...") curs.execute("INSERT INTO test_dt VALUES (%s, %s, %s, %s, %s)", mx1) # build and insert some values using the datetime adapters @@ -65,11 +65,11 @@ dt1 = ( datetime.datetime(2004, 10, 19, 0, 11, 17, 500000), datetime.timedelta(13, 15*3600+17*60+59, 900000)) -print "Inserting Python datetime values..." +print("Inserting Python datetime values...") curs.execute("INSERT INTO test_dt VALUES (%s, %s, %s, %s, %s)", dt1) # now extract the row from database and print them -print "Extracting values inserted with mx.DateTime wrappers:" +print("Extracting values inserted with mx.DateTime wrappers:") curs.execute("SELECT d, t, dt, z FROM test_dt WHERE k = 1") for n, x in zip(mx1[1:], curs.fetchone()): try: @@ -80,10 +80,10 @@ for n, x in zip(mx1[1:], curs.fetchone()): except: s = repr(n) + "\n -> " + str(adapt(n)) + \ "\n -> " + repr(x) + "\n -> " + str(x) - print s -print + print(s) +print() -print "Extracting values inserted with Python datetime wrappers:" +print("Extracting values inserted with Python datetime wrappers:") curs.execute("SELECT d, t, dt, z FROM test_dt WHERE k = 2") for n, x in zip(dt1[1:], curs.fetchone()): try: @@ -92,8 +92,8 @@ for n, x in zip(dt1[1:], curs.fetchone()): s = repr(n) + "\n -> " + repr(x) + "\n -> " + x.isoformat() except: s = repr(n) + "\n -> " + repr(x) + "\n -> " + str(x) - print s -print + print(s) +print() curs.execute("DROP TABLE test_dt") conn.commit() diff --git a/examples/encoding.py b/examples/encoding.py index 77fd871b..693a88dd 100644 --- a/examples/encoding.py +++ b/examples/encoding.py @@ -26,80 +26,80 @@ import psycopg2.extensions if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) -print "Initial encoding for this connection is", conn.encoding +print("Initial encoding for this connection is", conn.encoding) -print "\n** This example is supposed to be run in a UNICODE terminal! **\n" +print("\n** This example is supposed to be run in a UNICODE terminal! **\n") -print "Available encodings:" +print("Available encodings:") encs = psycopg2.extensions.encodings.items() encs.sort() for a, b in encs: - print " ", a, "<->", b + print(" ", a, "<->", b) -print "Using STRING typecaster" -print "Setting backend encoding to LATIN1 and executing queries:" +print("Using STRING typecaster") +print("Setting backend encoding to LATIN1 and executing queries:") conn.set_client_encoding('LATIN1') curs = conn.cursor() curs.execute("SELECT %s::TEXT AS foo", ('àèìòù',)) x = curs.fetchone()[0] -print " ->", unicode(x, 'latin-1').encode('utf-8'), type(x) +print(" ->", unicode(x, 'latin-1').encode('utf-8'), type(x)) curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',)) x = curs.fetchone()[0] -print " ->", unicode(x, 'latin-1').encode('utf-8'), type(x) +print(" ->", unicode(x, 'latin-1').encode('utf-8'), type(x)) -print "Setting backend encoding to UTF8 and executing queries:" +print("Setting backend encoding to UTF8 and executing queries:") conn.set_client_encoding('UNICODE') curs = conn.cursor() curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),)) x = curs.fetchone()[0] -print " ->", x, type(x) +print(" ->", x, type(x)) curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',)) x = curs.fetchone()[0] -print " ->", x, type(x) +print(" ->", x, type(x)) -print "Using UNICODE typecaster" +print("Using UNICODE typecaster") psycopg2.extensions.register_type(psycopg2.extensions.UNICODE) -print "Setting backend encoding to LATIN1 and executing queries:" +print("Setting backend encoding to LATIN1 and executing queries:") conn.set_client_encoding('LATIN1') curs = conn.cursor() curs.execute("SELECT %s::TEXT AS foo", ('àèìòù',)) x = curs.fetchone()[0] -print " ->", x.encode('utf-8'), ":", type(x) +print(" ->", x.encode('utf-8'), ":", type(x)) curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',)) x = curs.fetchone()[0] -print " ->", x.encode('utf-8'), ":", type(x) +print(" ->", x.encode('utf-8'), ":", type(x)) -print "Setting backend encoding to UTF8 and executing queries:" +print("Setting backend encoding to UTF8 and executing queries:") conn.set_client_encoding('UNICODE') curs = conn.cursor() curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),)) x = curs.fetchone()[0] -print " ->", x.encode('utf-8'), ":", type(x) +print(" ->", x.encode('utf-8'), ":", type(x)) curs.execute("SELECT %s::TEXT AS foo", (u'àèìòù',)) x = curs.fetchone()[0] -print " ->", x.encode('utf-8'), ":", type(x) +print(" ->", x.encode('utf-8'), ":", type(x)) -print "Executing full UNICODE queries" +print("Executing full UNICODE queries") -print "Setting backend encoding to LATIN1 and executing queries:" +print("Setting backend encoding to LATIN1 and executing queries:") conn.set_client_encoding('LATIN1') curs = conn.cursor() curs.execute(u"SELECT %s::TEXT AS foo", ('àèìòù',)) x = curs.fetchone()[0] -print " ->", x.encode('utf-8'), ":", type(x) +print(" ->", x.encode('utf-8'), ":", type(x)) curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù',)) x = curs.fetchone()[0] -print " ->", x.encode('utf-8'), ":", type(x) +print(" ->", x.encode('utf-8'), ":", type(x)) -print "Setting backend encoding to UTF8 and executing queries:" +print("Setting backend encoding to UTF8 and executing queries:") conn.set_client_encoding('UNICODE') curs = conn.cursor() curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù'.encode('utf-8'),)) x = curs.fetchone()[0] -print " ->", x.encode('utf-8'), ":", type(x) +print(" ->", x.encode('utf-8'), ":", type(x)) curs.execute(u"SELECT %s::TEXT AS foo", (u'àèìòù',)) x = curs.fetchone()[0] -print " ->", x.encode('utf-8'), ":", type(x) +print(" ->", x.encode('utf-8'), ":", type(x)) diff --git a/examples/fetch.py b/examples/fetch.py index 096a47e7..56b00bef 100644 --- a/examples/fetch.py +++ b/examples/fetch.py @@ -24,9 +24,9 @@ import psycopg2 if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) -print "Encoding for this connection is", conn.encoding +print("Encoding for this connection is", conn.encoding) curs = conn.cursor() try: @@ -68,12 +68,12 @@ conn.commit() ncurs = conn.cursor("crs") ncurs.execute("SELECT * FROM test_fetch") -print "First 10 rows:", flatten(ncurs.fetchmany(10)) +print("First 10 rows:", flatten(ncurs.fetchmany(10))) ncurs.scroll(-5) -print "Moved back cursor by 5 rows (to row 5.)" -print "Another 10 rows:", flatten(ncurs.fetchmany(10)) -print "Another one:", list(ncurs.fetchone()) -print "The remaining rows:", flatten(ncurs.fetchall()) +print("Moved back cursor by 5 rows (to row 5.)") +print("Another 10 rows:", flatten(ncurs.fetchmany(10))) +print("Another one:", list(ncurs.fetchone())) +print("The remaining rows:", flatten(ncurs.fetchall())) conn.rollback() curs.execute("DROP TABLE test_fetch") diff --git a/examples/lastrowid.py b/examples/lastrowid.py index ea5a3b66..da209d61 100644 --- a/examples/lastrowid.py +++ b/examples/lastrowid.py @@ -23,7 +23,7 @@ import sys, psycopg2 if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) curs = conn.cursor() @@ -42,18 +42,18 @@ curs.execute("""INSERT INTO test_oid VALUES (%(name)s, %(surname)s)""", data[0]) foid = curs.lastrowid -print "Oid for %(name)s %(surname)s" % data[0], "is", foid +print("Oid for %(name)s %(surname)s" % data[0], "is", foid) curs.execute("""INSERT INTO test_oid VALUES (%(name)s, %(surname)s)""", data[1]) moid = curs.lastrowid -print "Oid for %(name)s %(surname)s" % data[1], "is", moid +print("Oid for %(name)s %(surname)s" % data[1], "is", moid) curs.execute("SELECT * FROM test_oid WHERE oid = %s", (foid,)) -print "Oid", foid, "selected %s %s" % curs.fetchone() +print("Oid", foid, "selected %s %s" % curs.fetchone()) curs.execute("SELECT * FROM test_oid WHERE oid = %s", (moid,)) -print "Oid", moid, "selected %s %s" % curs.fetchone() +print("Oid", moid, "selected %s %s" % curs.fetchone()) curs.execute("DROP TABLE test_oid") conn.commit() diff --git a/examples/lobject.py b/examples/lobject.py index de18964b..242208eb 100644 --- a/examples/lobject.py +++ b/examples/lobject.py @@ -24,68 +24,68 @@ import psycopg2 if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) -print "Encoding for this connection is", conn.encoding +print("Encoding for this connection is", conn.encoding) # this will create a large object with a new random oid, we'll # use it to make some basic tests about read/write and seek. lobj = conn.lobject() loid = lobj.oid -print "Created a new large object with oid", loid +print("Created a new large object with oid", loid) -print "Manually importing some binary data into the object:" +print("Manually importing some binary data into the object:") data = open("somehackers.jpg").read() len = lobj.write(data) -print " imported", len, "bytes of data" +print(" imported", len, "bytes of data") conn.commit() -print "Trying to (re)open large object with oid", loid +print("Trying to (re)open large object with oid", loid) lobj = conn.lobject(loid) -print "Manually exporting the data from the lobject:" +print("Manually exporting the data from the lobject:") data1 = lobj.read() len = lobj.tell() lobj.seek(0, 0) data2 = lobj.read() if data1 != data2: - print "ERROR: read after seek returned different data" + print("ERROR: read after seek returned different data") open("somehackers_lobject1.jpg", 'wb').write(data1) -print " written", len, "bytes of data to somehackers_lobject1.jpg" +print(" written", len, "bytes of data to somehackers_lobject1.jpg") lobj.unlink() -print "Large object with oid", loid, "removed" +print("Large object with oid", loid, "removed") conn.commit() # now we try to use the import and export functions to do the same lobj = conn.lobject(0, 'n', 0, "somehackers.jpg") loid = lobj.oid -print "Imported a new large object with oid", loid +print("Imported a new large object with oid", loid) conn.commit() -print "Trying to (re)open large object with oid", loid +print("Trying to (re)open large object with oid", loid) lobj = conn.lobject(loid, 'n') -print "Using export() to export the data from the large object:" +print("Using export() to export the data from the large object:") lobj.export("somehackers_lobject2.jpg") -print " exported large object to somehackers_lobject2.jpg" +print(" exported large object to somehackers_lobject2.jpg") lobj.unlink() -print "Large object with oid", loid, "removed" +print("Large object with oid", loid, "removed") conn.commit() # this will create a very large object with a new random oid. lobj = conn.lobject() loid = lobj.oid -print "Created a new large object with oid", loid +print("Created a new large object with oid", loid) -print "Manually importing a lot of data into the object:" +print("Manually importing a lot of data into the object:") data = "data" * 1000000 len = lobj.write(data) -print " imported", len, "bytes of data" +print(" imported", len, "bytes of data") conn.rollback() -print "\nNow try to load the new images, to check it worked!" +print("\nNow try to load the new images, to check it worked!") diff --git a/examples/mogrify.py b/examples/mogrify.py index 3229fe7f..c6e04b5b 100644 --- a/examples/mogrify.py +++ b/examples/mogrify.py @@ -24,10 +24,10 @@ import sys, psycopg2 if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) -print "Encoding for this connection is", conn.encoding +print("Encoding for this connection is", conn.encoding) curs = conn.cursor() curs.execute("SELECT %(foo)s AS foo", {'foo':'bar'}) @@ -37,11 +37,11 @@ curs.execute("SELECT %(foo)s AS foo", {'foo':42}) curs.execute("SELECT %(foo)s AS foo", {'foo':u'yatt�!'}) curs.execute("SELECT %(foo)s AS foo", {'foo':u'bar'}) -print curs.mogrify("SELECT %(foo)s AS foo", {'foo':'bar'}) -print curs.mogrify("SELECT %(foo)s AS foo", {'foo':None}) -print curs.mogrify("SELECT %(foo)s AS foo", {'foo':True}) -print curs.mogrify("SELECT %(foo)s AS foo", {'foo':42}) -print curs.mogrify("SELECT %(foo)s AS foo", {'foo':u'yatt�!'}) -print curs.mogrify("SELECT %(foo)s AS foo", {'foo':u'bar'}) +print(curs.mogrify("SELECT %(foo)s AS foo", {'foo':'bar'})) +print(curs.mogrify("SELECT %(foo)s AS foo", {'foo':None})) +print(curs.mogrify("SELECT %(foo)s AS foo", {'foo':True})) +print(curs.mogrify("SELECT %(foo)s AS foo", {'foo':42})) +print(curs.mogrify("SELECT %(foo)s AS foo", {'foo':u'yatt�!'})) +print(curs.mogrify("SELECT %(foo)s AS foo", {'foo':u'bar'})) conn.rollback() diff --git a/examples/myfirstrecipe.py b/examples/myfirstrecipe.py index e0df879f..1390ad00 100644 --- a/examples/myfirstrecipe.py +++ b/examples/myfirstrecipe.py @@ -122,5 +122,5 @@ register_adapter(int, AsIs) # the SQL_IN class by calling psycopg's adapt() directly: if __name__ == '__main__': - print "Note how the string will be SQL-quoted, but the number will not:" - print psycoadapt(("this is an 'sql quoted' str\\ing", 1, 2.0)) + print("Note how the string will be SQL-quoted, but the number will not:") + print(psycoadapt(("this is an 'sql quoted' str\\ing", 1, 2.0))) diff --git a/examples/notify.py b/examples/notify.py index ca0dcd28..eb0f2fa5 100644 --- a/examples/notify.py +++ b/examples/notify.py @@ -26,20 +26,20 @@ from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) -print "Encoding for this connection is", conn.encoding +print("Encoding for this connection is", conn.encoding) conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) curs = conn.cursor() curs.execute("listen test") -print "Waiting for 'NOTIFY test'" +print("Waiting for 'NOTIFY test'") while 1: if select.select([conn],[],[],5)==([],[],[]): - print "Timeout" + print("Timeout") else: conn.poll() while conn.notifies: - print "Got NOTIFY:", conn.notifies.pop() + print("Got NOTIFY:", conn.notifies.pop()) diff --git a/examples/simple.py b/examples/simple.py index 339419e3..08191eaa 100644 --- a/examples/simple.py +++ b/examples/simple.py @@ -30,17 +30,17 @@ import psycopg2 if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) -print "Encoding for this connection is", conn.encoding +print("Encoding for this connection is", conn.encoding) curs = conn.cursor() curs.execute("SELECT 1 AS foo") -print curs.fetchone() +print(curs.fetchone()) curs.execute("SELECT 1 AS foo") -print curs.fetchmany() +print(curs.fetchmany()) curs.execute("SELECT 1 AS foo") -print curs.fetchall() +print(curs.fetchall()) conn.rollback() diff --git a/examples/threads.py b/examples/threads.py index e0be7e0c..49a495c7 100644 --- a/examples/threads.py +++ b/examples/threads.py @@ -45,7 +45,7 @@ if len(sys.argv) > 1: if len(sys.argv) > 2: MODE = int(sys.argv[2]) -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) curs = conn.cursor() @@ -77,7 +77,7 @@ def insert_func(conn_or_pool, rows): if MODE == 1: conn_or_pool.putconn(conn) s = name + ": COMMIT STEP " + str(i) - print s + print(s) if MODE == 1: conn = conn_or_pool.getconn() c = conn.cursor() @@ -85,8 +85,8 @@ def insert_func(conn_or_pool, rows): c.execute("INSERT INTO test_threads VALUES (%s, %s, %s)", (str(i), i, float(i))) except psycopg2.ProgrammingError as err: - print name, ": an error occurred; skipping this insert" - print err + print(name, ": an error occurred; skipping this insert") + print(err) conn.commit() ## a nice select function that prints the current number of rows in the diff --git a/examples/typecast.py b/examples/typecast.py index 169f3aca..e6eda6c1 100644 --- a/examples/typecast.py +++ b/examples/typecast.py @@ -29,14 +29,14 @@ import psycopg2.extensions if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) -print "Encoding for this connection is", conn.encoding +print("Encoding for this connection is", conn.encoding) curs = conn.cursor() curs.execute("SELECT 'text'::text AS foo") textoid = curs.description[0][1] -print "Oid for the text datatype is", textoid +print("Oid for the text datatype is", textoid) def castA(s, curs): if s is not None: return "(A) " + s @@ -48,18 +48,18 @@ TYPEB = psycopg2.extensions.new_type((textoid,), "TYPEB", castB) curs = conn.cursor() curs.execute("SELECT 'some text.'::text AS foo") -print "Some text from plain connection:", curs.fetchone()[0] +print("Some text from plain connection:", curs.fetchone()[0]) psycopg2.extensions.register_type(TYPEA, conn) curs = conn.cursor() curs.execute("SELECT 'some text.'::text AS foo") -print "Some text from connection with typecaster:", curs.fetchone()[0] +print("Some text from connection with typecaster:", curs.fetchone()[0]) curs = conn.cursor() psycopg2.extensions.register_type(TYPEB, curs) curs.execute("SELECT 'some text.'::text AS foo") -print "Some text from cursor with typecaster:", curs.fetchone()[0] +print("Some text from cursor with typecaster:", curs.fetchone()[0]) curs = conn.cursor() curs.execute("SELECT 'some text.'::text AS foo") -print "Some text from connection with typecaster again:", curs.fetchone()[0] +print("Some text from connection with typecaster again:", curs.fetchone()[0]) diff --git a/examples/tz.py b/examples/tz.py index c3dd8e43..1726a6c2 100644 --- a/examples/tz.py +++ b/examples/tz.py @@ -28,7 +28,7 @@ from psycopg2.tz import ZERO, LOCAL, FixedOffsetTimezone if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) curs = conn.cursor() @@ -42,27 +42,27 @@ conn.commit() d = datetime.datetime(1971, 10, 19, 22, 30, 0, tzinfo=LOCAL) curs.execute("INSERT INTO test_tz VALUES (%s)", (d,)) -print "Inserted timestamp with timezone:", d -print "Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d) +print("Inserted timestamp with timezone:", d) +print("Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d)) tz = FixedOffsetTimezone(-5*60, "EST") d = datetime.datetime(1971, 10, 19, 22, 30, 0, tzinfo=tz) curs.execute("INSERT INTO test_tz VALUES (%s)", (d,)) -print "Inserted timestamp with timezone:", d -print "Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d) +print("Inserted timestamp with timezone:", d) +print("Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d)) curs.execute("SELECT * FROM test_tz") d = curs.fetchone()[0] curs.execute("INSERT INTO test_tz VALUES (%s)", (d,)) -print "Inserted SELECTed timestamp:", d -print "Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d) +print("Inserted SELECTed timestamp:", d) +print("Time zone:", d.tzinfo.tzname(d), "offset:", d.tzinfo.utcoffset(d)) curs.execute("SELECT * FROM test_tz") for d in curs: u = d[0].utcoffset() or ZERO - print "UTC time: ", d[0] - u - print "Local time:", d[0] - print "Time zone:", d[0].tzinfo.tzname(d[0]), d[0].tzinfo.utcoffset(d[0]) + print("UTC time: ", d[0] - u) + print("Local time:", d[0]) + print("Time zone:", d[0].tzinfo.tzname(d[0]), d[0].tzinfo.utcoffset(d[0])) curs.execute("DROP TABLE test_tz") diff --git a/examples/usercast.py b/examples/usercast.py index 26a5c16e..b9050427 100644 --- a/examples/usercast.py +++ b/examples/usercast.py @@ -33,9 +33,9 @@ import psycopg2.extras if len(sys.argv) > 1: DSN = sys.argv[1] -print "Opening connection using dsn:", DSN +print("Opening connection using dsn:", DSN) conn = psycopg2.connect(DSN) -print "Initial encoding for this connection is", conn.encoding +print("Initial encoding for this connection is", conn.encoding) curs = conn.cursor() try: @@ -98,7 +98,7 @@ class Rect(object): # here we select from the empty table, just to grab the description curs.execute("SELECT b FROM test_cast WHERE 0=1") boxoid = curs.description[0][1] -print "Oid for the box datatype is", boxoid +print("Oid for the box datatype is", boxoid) # and build the user cast object BOX = psycopg2.extensions.new_type((boxoid,), "BOX", Rect) @@ -113,14 +113,14 @@ for i in range(100): whrandom.randint(0,100), whrandom.randint(0,100)) curs.execute("INSERT INTO test_cast VALUES ('%(p1)s', '%(p2)s', %(box)s)", {'box':b, 'p1':p1, 'p2':p2}) -print "Added 100 boxed to the database" +print("Added 100 boxed to the database") # select and print all boxes with at least one point inside curs.execute("SELECT b FROM test_cast WHERE p1 @ b OR p2 @ b") boxes = curs.fetchall() -print "Found %d boxes with at least a point inside:" % len(boxes) +print("Found %d boxes with at least a point inside:" % len(boxes)) for box in boxes: - print " ", box[0].show() + print(" ", box[0].show()) curs.execute("DROP TABLE test_cast") conn.commit() diff --git a/sandbox/array.py b/sandbox/array.py index d7971561..9790d344 100644 --- a/sandbox/array.py +++ b/sandbox/array.py @@ -27,4 +27,4 @@ curs = conn.cursor() #print curs.fetchone() curs.execute("SELECT %s", ([1,2,None],)) -print curs.fetchone() +print(curs.fetchone()) diff --git a/sandbox/async.py b/sandbox/async.py index e9fe2811..3ddaa3a8 100644 --- a/sandbox/async.py +++ b/sandbox/async.py @@ -15,7 +15,7 @@ curs = conn.cursor() def sleep(curs): while not curs.isready(): - print "." + print(".") time.sleep(.1) #curs.execute(""" @@ -24,12 +24,12 @@ def sleep(curs): # FOR READ ONLY;""", async = 1) curs.execute("SELECT now() AS foo", async=1) sleep(curs) -print curs.fetchall() +print(curs.fetchall()) #curs.execute(""" # FETCH FORWARD 1 FROM zz;""", async = 1) curs.execute("SELECT now() AS bar", async=1) -print curs.fetchall() +print(curs.fetchall()) curs.execute("SELECT now() AS bar") sleep(curs) diff --git a/sandbox/crash.py b/sandbox/crash.py index 23f354ca..851fa7ee 100644 --- a/sandbox/crash.py +++ b/sandbox/crash.py @@ -17,7 +17,7 @@ def query_worker(dsn): break if len(sys.argv) != 2: - print 'usage: %s DSN' % sys.argv[0] + print('usage: %s DSN' % sys.argv[0]) sys.exit(1) th = threading.Thread(target=query_worker, args=(sys.argv[1],)) th.setDaemon(True) diff --git a/sandbox/dec2float.py b/sandbox/dec2float.py index 2006e0a5..481defab 100644 --- a/sandbox/dec2float.py +++ b/sandbox/dec2float.py @@ -12,4 +12,4 @@ o = psycopg2.connect("dbname=test") c = o.cursor() c.execute("SELECT NULL::decimal(10,2)") n = c.fetchone()[0] -print n, type(n) +print(n, type(n)) diff --git a/sandbox/domainoid.py b/sandbox/domainoid.py index 0d98399f..c686060c 100644 --- a/sandbox/domainoid.py +++ b/sandbox/domainoid.py @@ -4,15 +4,15 @@ con = psycopg2.connect("dbname=test") cur = con.cursor() cur.execute("SELECT %s::regtype::oid", ('bytea', )) -print cur.fetchone()[0] +print(cur.fetchone()[0]) # 17 cur.execute("CREATE DOMAIN thing AS bytea") cur.execute("SELECT %s::regtype::oid", ('thing', )) -print cur.fetchone()[0] +print(cur.fetchone()[0]) #62148 cur.execute("CREATE TABLE thingrel (thingcol thing)") cur.execute("SELECT * FROM thingrel") -print cur.description +print(cur.description) #(('thingcol', 17, None, -1, None, None, None),) diff --git a/sandbox/gtk.py b/sandbox/gtk.py index b00cd29b..b98c1fec 100644 --- a/sandbox/gtk.py +++ b/sandbox/gtk.py @@ -5,14 +5,14 @@ c = o.cursor() def sql(): c.execute("SELECT 1.23 AS foo") - print 1, c.fetchone() + print(1, c.fetchone()) #print c.description c.execute("SELECT 1.23::float AS foo") - print 2, c.fetchone() + print(2, c.fetchone()) #print c.description -print "BEFORE" +print("BEFORE") sql() import gtk -print "AFTER" +print("AFTER") sql() diff --git a/sandbox/iter.py b/sandbox/iter.py index bf245ba9..c8becca8 100644 --- a/sandbox/iter.py +++ b/sandbox/iter.py @@ -6,8 +6,8 @@ curs = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) curs.execute("SELECT '2005-2-12'::date AS foo, 'boo!' as bar") for x in curs.fetchall(): - print type(x), x[0], x[1], x['foo'], x['bar'] + print(type(x), x[0], x[1], x['foo'], x['bar']) curs.execute("SELECT '2005-2-12'::date AS foo, 'boo!' as bar") for x in curs: - print type(x), x[0], x[1], x['foo'], x['bar'] + print(type(x), x[0], x[1], x['foo'], x['bar']) diff --git a/sandbox/leak.test.py b/sandbox/leak.test.py index c0bef371..bae995f8 100644 --- a/sandbox/leak.test.py +++ b/sandbox/leak.test.py @@ -43,7 +43,7 @@ def leak(): row = {'foo': i} rows.append(row) count += 1 - print "loop count:", count + print("loop count:", count) cursor.executemany(insert, rows) connection.commit() except psycopg2.IntegrityError: @@ -59,7 +59,7 @@ def noleak(): while 1: try: count += 1 - print "loop count:", count + print("loop count:", count) cursor.executemany(insert, rows) connection.commit() except psycopg2.IntegrityError: @@ -72,10 +72,10 @@ try: elif 'noleak' == sys.argv[1]: run_function = noleak else: - print usage + print(usage) sys.exit() except IndexError: - print usage + print(usage) sys.exit() # Run leak() or noleak(), whichever was indicated on the command line diff --git a/sandbox/misc_dbapi_test.py b/sandbox/misc_dbapi_test.py index 137ddb04..6a012808 100644 --- a/sandbox/misc_dbapi_test.py +++ b/sandbox/misc_dbapi_test.py @@ -18,8 +18,8 @@ class O(object): o = O('R%', second='S%') -print o[0] -print o['second'] +print(o[0]) +print(o['second']) #------------------------------------------------------------------------------- @@ -40,4 +40,4 @@ cursor.execute(""" """, (o,)) for row in cursor: - print row + print(row) diff --git a/sandbox/named.py b/sandbox/named.py index 94d89811..4780a242 100644 --- a/sandbox/named.py +++ b/sandbox/named.py @@ -15,10 +15,10 @@ curs = conn.cursor() curs.execute("SELECT reffunc2()") portal = curs.fetchone()[0] -print portal.fetchone() -print portal.fetchmany(2) +print(portal.fetchone()) +print(portal.fetchmany(2)) portal.scroll(0, 'absolute') -print portal.fetchall() +print(portal.fetchall()) #print curs.rowcount diff --git a/sandbox/pbool.py b/sandbox/pbool.py index d26f8b46..b1b9d5c4 100644 --- a/sandbox/pbool.py +++ b/sandbox/pbool.py @@ -3,7 +3,7 @@ class B(object): if x: self._o = True else: self._o = False def __getattribute__(self, attr): - print "ga called", attr + print("ga called", attr) return object.__getattribute__(self, attr) def _sqlquote(self): if self._o: diff --git a/sandbox/stress.py b/sandbox/stress.py index 48e0a59e..03f5b4fb 100644 --- a/sandbox/stress.py +++ b/sandbox/stress.py @@ -8,4 +8,4 @@ for i in range(20000): datafile = os.popen('ps -p %s -o rss' % os.getpid()) line = datafile.readlines(2)[1].strip() datafile.close() - print str(i) + '\t' + line + print(str(i) + '\t' + line) diff --git a/sandbox/stress2.py b/sandbox/stress2.py index 0e36dfc3..e461d860 100644 --- a/sandbox/stress2.py +++ b/sandbox/stress2.py @@ -33,7 +33,7 @@ def g(): line = datafile.readlines(2)[1].strip() datafile.close() n = 30 - print str(k*n) + '\t' + line + print(str(k*n) + '\t' + line) k += 1 while threading.activeCount()>1: diff --git a/sandbox/test.py b/sandbox/test.py index a2a2a6b6..371a27a8 100644 --- a/sandbox/test.py +++ b/sandbox/test.py @@ -10,26 +10,26 @@ conn = psycopg2.connect("dbname=test") #conn.set_client_encoding("xxx") curs = conn.cursor() curs.execute("SELECT '2005-2-12'::date AS foo") -print curs.fetchall() +print(curs.fetchall()) curs.execute("SELECT '10:23:60'::time AS foo") -print curs.fetchall() +print(curs.fetchall()) curs.execute("SELECT '10:23:59.895342'::time AS foo") -print curs.fetchall() +print(curs.fetchall()) curs.execute("SELECT '0:0:12.31423'::time with time zone AS foo") -print curs.fetchall() +print(curs.fetchall()) curs.execute("SELECT '0:0:12+01:30'::time with time zone AS foo") -print curs.fetchall() +print(curs.fetchall()) curs.execute("SELECT '2005-2-12 10:23:59.895342'::timestamp AS foo") -print curs.fetchall() +print(curs.fetchall()) curs.execute("SELECT '2005-2-12 10:23:59.895342'::timestamp with time zone AS foo") -print curs.fetchall() +print(curs.fetchall()) #print curs.fetchmany(2) #print curs.fetchall() def sleep(curs): while not curs.isready(): - print "." + print(".") time.sleep(.1) #curs.execute(""" diff --git a/sandbox/test814.py b/sandbox/test814.py index 1e20ba0d..0e78981d 100644 --- a/sandbox/test814.py +++ b/sandbox/test814.py @@ -4,5 +4,5 @@ import psycopg2.extras conn = psycopg2.connect("dbname=test") curs = conn.cursor() curs.execute("SELECT true AS foo WHERE 'a' in %s", (("aa", "bb"),)) -print curs.fetchall() -print curs.query +print(curs.fetchall()) +print(curs.query) diff --git a/sandbox/test_green_error.py b/sandbox/test_green_error.py index 7477382a..71b176f0 100644 --- a/sandbox/test_green_error.py +++ b/sandbox/test_green_error.py @@ -52,7 +52,7 @@ signal.signal(signal.SIGHUP, handler) def worker(): while 1: - print "I'm working" + print("I'm working") sleep(1) eventlet.spawn(worker) @@ -61,21 +61,21 @@ eventlet.spawn(worker) # You can unplug the network cable etc. here. # Kill -HUP will raise an exception in the callback. -print "PID", os.getpid() +print("PID", os.getpid()) conn = psycopg2.connect(DSN) curs = conn.cursor() try: for i in range(1000): curs.execute("select %s, pg_sleep(1)", (i,)) r = curs.fetchone() - print "selected", r + print("selected", r) except BaseException, e: - print "got exception:", e.__class__.__name__, e + print("got exception:", e.__class__.__name__, e) if conn.closed: - print "the connection is closed" + print("the connection is closed") else: conn.rollback() curs.execute("select 1") - print curs.fetchone() + print(curs.fetchone()) diff --git a/sandbox/test_isready_connection_closed.py b/sandbox/test_isready_connection_closed.py index 25377ac1..e70e8e18 100644 --- a/sandbox/test_isready_connection_closed.py +++ b/sandbox/test_isready_connection_closed.py @@ -5,27 +5,27 @@ import signal import warnings import psycopg2 -print "Testing psycopg2 version %s" % psycopg2.__version__ +print("Testing psycopg2 version %s" % psycopg2.__version__) dbname = os.environ.get('PSYCOPG2_TESTDB', 'psycopg2_test') conn = psycopg2.connect("dbname=%s" % dbname) curs = conn.cursor() curs.isready() -print "Now restart the test postgresql server to drop all connections, press enter when done." +print("Now restart the test postgresql server to drop all connections, press enter when done.") raw_input() try: curs.isready() # No need to test return value curs.isready() except: - print "Test passed" + print("Test passed") sys.exit(0) if curs.isready(): - print "Warning: looks like the connection didn't get killed. This test is probably in-effective" - print "Test inconclusive" + print("Warning: looks like the connection didn't get killed. This test is probably in-effective") + print("Test inconclusive") sys.exit(1) gc.collect() # used to error here -print "Test Passed" +print("Test Passed") diff --git a/sandbox/textfloat.py b/sandbox/textfloat.py index 5383bb38..2bc528dc 100644 --- a/sandbox/textfloat.py +++ b/sandbox/textfloat.py @@ -5,4 +5,4 @@ o = psycopg2.connect("dbname=test") c = o.cursor() c.execute("SELECT 1.23::float AS foo") x = c.fetchone()[0] -print x, type(x) +print(x, type(x)) diff --git a/sandbox/ticket58.py b/sandbox/ticket58.py index 98b5452b..27a857aa 100644 --- a/sandbox/ticket58.py +++ b/sandbox/ticket58.py @@ -42,7 +42,7 @@ cur = conn.cursor() gc_thread.start() # Now do lots of "cursor.copy_from" calls: -print "copy_from" +print("copy_from") for i in range(1000): f = StringIO("42\tfoo\n74\tbar\n") cur.copy_from(f, 'test', columns=('num', 'data')) @@ -51,7 +51,7 @@ for i in range(1000): # python: Modules/gcmodule.c:277: visit_decref: Assertion `gc->gc.gc_refs != 0' failed. # Also exercise the copy_to code path -print "copy_to" +print("copy_to") cur.execute("truncate test") f = StringIO("42\tfoo\n74\tbar\n") cur.copy_from(f, 'test', columns=('num', 'data')) @@ -60,7 +60,7 @@ for i in range(1000): cur.copy_to(f, 'test', columns=('num', 'data')) # And copy_expert too -print "copy_expert" +print("copy_expert") cur.execute("truncate test") for i in range(1000): f = StringIO("42\tfoo\n74\tbar\n") diff --git a/sandbox/trigger-commit-fail.py b/sandbox/trigger-commit-fail.py index 98b23ae9..0655c2f2 100644 --- a/sandbox/trigger-commit-fail.py +++ b/sandbox/trigger-commit-fail.py @@ -6,7 +6,7 @@ db = psycopg2.connect('dbname=test') cursor = db.cursor() -print 'Creating tables and sample data' +print('Creating tables and sample data') cursor.execute(''' CREATE TEMPORARY TABLE foo ( @@ -23,22 +23,22 @@ cursor.execute('INSERT INTO bar VALUES (1, 1)') db.commit() -print 'Deferring constraint and breaking referential integrity' +print('Deferring constraint and breaking referential integrity') cursor.execute('SET CONSTRAINTS bar_foo_fk DEFERRED') cursor.execute('UPDATE bar SET foo_id = 42 WHERE id = 1') -print 'Committing (this should fail)' +print('Committing (this should fail)') try: db.commit() except: traceback.print_exc() -print 'Rolling back connection' +print('Rolling back connection') db.rollback() -print 'Running a trivial query' +print('Running a trivial query') try: cursor.execute('SELECT TRUE') except: traceback.print_exc() -print 'db.closed:', db.closed +print('db.closed:', db.closed) diff --git a/sandbox/trigger_double_dealloc.py b/sandbox/trigger_double_dealloc.py index 3ad7a80b..1312aa3e 100644 --- a/sandbox/trigger_double_dealloc.py +++ b/sandbox/trigger_double_dealloc.py @@ -1,3 +1,5 @@ +from __future__ import print_function + import psycopg2, psycopg2.extensions import threading import gc @@ -20,9 +22,9 @@ class db_user(threading.Thread): # the conn2 desctructor will block indefinitely # on the completion of the query # (and it will not be holding the GIL during that time) - print >> sys.stderr, "begin conn2 del" + print("begin conn2 del", file=sys.stderr) del cursor, conn2 - print >> sys.stderr, "end conn2 del" + print("end conn2 del", file=sys.stderr) def main(): # lock out a db row @@ -43,7 +45,7 @@ def main(): # as it will avoid conn_close() for i in range(10): if gc.collect(): - print >> sys.stderr, "garbage collection done" + print("garbage collection done", file=sys.stderr) break time.sleep(1) @@ -52,9 +54,9 @@ def main(): # concurrent thread destructor of conn2 to # continue and it will end up trying to free # self->dsn a second time. - print >> sys.stderr, "begin conn1 del" + print("begin conn1 del", file=sys.stderr) del cursor, conn1 - print >> sys.stderr, "end conn1 del" + print("end conn1 del", file=sys.stderr) if __name__ == '__main__': diff --git a/sandbox/typecrash.py b/sandbox/typecrash.py index 522ab548..38856753 100644 --- a/sandbox/typecrash.py +++ b/sandbox/typecrash.py @@ -1,7 +1,7 @@ import psycopg2.extensions -print dir(psycopg2._psycopg) -print psycopg2.extensions.new_type( - (600,), "POINT", lambda oids, name, fun: None) -print "ciccia ciccia" -print psycopg2._psycopg +print(dir(psycopg2._psycopg)) +print(psycopg2.extensions.new_type( + (600,), "POINT", lambda oids, name, fun: None)) +print("ciccia ciccia") +print(psycopg2._psycopg) diff --git a/sandbox/tzhalf.py b/sandbox/tzhalf.py index 8e38441a..710833df 100644 --- a/sandbox/tzhalf.py +++ b/sandbox/tzhalf.py @@ -6,4 +6,4 @@ conn = psycopg2.connect("dbname=test") curs = conn.cursor() curs.execute("set timezone = 'Asia/Calcutta'") curs.execute("SELECT now()") -print curs.fetchone()[0] +print(curs.fetchone()[0]) diff --git a/scripts/make_errorcodes.py b/scripts/make_errorcodes.py index 1769ba5a..adc6ee80 100755 --- a/scripts/make_errorcodes.py +++ b/scripts/make_errorcodes.py @@ -15,6 +15,7 @@ The script can be run at a new PostgreSQL release to refresh the module. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License for more details. +from __future__ import print_function import re import sys @@ -26,7 +27,7 @@ from BeautifulSoup import BeautifulSoup as BS def main(): if len(sys.argv) != 2: - print >>sys.stderr, "usage: %s /path/to/errorcodes.py" % sys.argv[0] + print("usage: %s /path/to/errorcodes.py" % sys.argv[0], file=sys.stderr) return 2 filename = sys.argv[1] @@ -39,9 +40,9 @@ def main(): f = open(filename, "w") for line in file_start: - print >>f, line + print(line, file=f) for line in generate_module_data(classes, errors): - print >>f, line + print(line, file=f) def read_base_file(filename): @@ -141,7 +142,7 @@ def fetch_errors(versions): errors = defaultdict(dict) for version in versions: - print >> sys.stderr, version + print(version, file=sys.stderr) tver = tuple(map(int, version.split()[0].split('.'))) if tver < (9, 1): c1, e1 = parse_errors_sgml(errors_sgml_url % version) diff --git a/scripts/refcounter.py b/scripts/refcounter.py index 305dd1bc..f1642e7a 100755 --- a/scripts/refcounter.py +++ b/scripts/refcounter.py @@ -17,6 +17,7 @@ script exits with error 1. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License for more details. +from __future__ import print_function import gc import sys diff --git a/tests/__init__.py b/tests/__init__.py index fa7e3939..2cb219b0 100755 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -66,8 +66,8 @@ def test_suite(): try: cnn = psycopg2.connect(dsn) except Exception as e: - print "Failed connection to test db:", e.__class__.__name__, e - print "Please set env vars 'PSYCOPG2_TESTDB*' to valid values." + print("Failed connection to test db:", e.__class__.__name__, e) + print("Please set env vars 'PSYCOPG2_TESTDB*' to valid values.") sys.exit(1) else: cnn.close() From 1a8e992fcc87b2d15911b60cc9220ec50a70a4ff Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 3 Dec 2017 18:47:19 -0800 Subject: [PATCH 64/96] Use relative imports throughout tests The tests relied on Python2 relative import semantics. Python3 changed import semantics to always search sys.path by default. To import using a relative path it must have a leading dot. Forward compatible with newer Pythons. Works towards the goal of moving tests outside of the installed package. For more information, see PEP-328: https://www.python.org/dev/peps/pep-0328/ --- tests/__init__.py | 52 ++++++++++++++++----------------- tests/dbapi20.py | 6 ++-- tests/test_async.py | 4 +-- tests/test_async_keyword.py | 6 ++-- tests/test_bug_gc.py | 2 +- tests/test_cancel.py | 4 +-- tests/test_connection.py | 4 +-- tests/test_copy.py | 18 ++++++------ tests/test_cursor.py | 2 +- tests/test_dates.py | 2 +- tests/test_errcodes.py | 2 +- tests/test_extras_dictcursor.py | 2 +- tests/test_fast_executemany.py | 2 +- tests/test_green.py | 2 +- tests/test_ipaddress.py | 2 +- tests/test_lobject.py | 2 +- tests/test_module.py | 2 +- tests/test_notify.py | 4 +-- tests/test_psycopg2_dbapi20.py | 10 +++---- tests/test_quote.py | 10 +++---- tests/test_replication.py | 6 ++-- tests/test_sql.py | 2 +- tests/test_transaction.py | 2 +- tests/test_types_basic.py | 6 ++-- tests/test_types_extras.py | 2 +- tests/test_with.py | 2 +- tests/testutils.py | 4 +-- 27 files changed, 81 insertions(+), 81 deletions(-) diff --git a/tests/__init__.py b/tests/__init__.py index 2cb219b0..e58b6fa7 100755 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -28,36 +28,36 @@ import warnings warnings.simplefilter('error') # noqa import sys -from testconfig import dsn +from .testconfig import dsn import unittest -import test_async -import test_bugX000 -import test_bug_gc -import test_cancel -import test_connection -import test_copy -import test_cursor -import test_dates -import test_errcodes -import test_extras_dictcursor -import test_fast_executemany -import test_green -import test_ipaddress -import test_lobject -import test_module -import test_notify -import test_psycopg2_dbapi20 -import test_quote -import test_replication -import test_sql -import test_transaction -import test_types_basic -import test_types_extras -import test_with +from . import test_async +from . import test_bugX000 +from . import test_bug_gc +from . import test_cancel +from . import test_connection +from . import test_copy +from . import test_cursor +from . import test_dates +from . import test_errcodes +from . import test_extras_dictcursor +from . import test_fast_executemany +from . import test_green +from . import test_ipaddress +from . import test_lobject +from . import test_module +from . import test_notify +from . import test_psycopg2_dbapi20 +from . import test_quote +from . import test_replication +from . import test_sql +from . import test_transaction +from . import test_types_basic +from . import test_types_extras +from . import test_with if sys.version_info[:2] < (3, 6): - import test_async_keyword + from . import test_async_keyword def test_suite(): diff --git a/tests/dbapi20.py b/tests/dbapi20.py index 4facf8e8..212d0544 100644 --- a/tests/dbapi20.py +++ b/tests/dbapi20.py @@ -90,12 +90,12 @@ class DatabaseAPI20Test(unittest.TestCase): self.driver, connect_args and connect_kw_args. Class specification should be as follows: - import dbapi20 + from . import dbapi20 class mytest(dbapi20.DatabaseAPI20Test): [...] - Don't 'import DatabaseAPI20Test from dbapi20', or you will - confuse the unit tester - just 'import dbapi20'. + Don't 'from .dbapi20 import DatabaseAPI20Test', or you will + confuse the unit tester - just 'from . import dbapi20'. ''' # The self.driver module. This should be the module where the 'connect' diff --git a/tests/test_async.py b/tests/test_async.py index 2c4bfa3b..6469a252 100755 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -24,7 +24,7 @@ # License for more details. import unittest -from testutils import skip_before_postgres, slow +from .testutils import skip_before_postgres, slow import psycopg2 from psycopg2 import extensions as ext @@ -32,7 +32,7 @@ from psycopg2 import extensions as ext import time import StringIO -from testutils import ConnectingTestCase +from .testutils import ConnectingTestCase class PollableStub(object): diff --git a/tests/test_async_keyword.py b/tests/test_async_keyword.py index e08c2750..fa37500c 100755 --- a/tests/test_async_keyword.py +++ b/tests/test_async_keyword.py @@ -28,11 +28,11 @@ import time import psycopg2 from psycopg2 import extras -from testconfig import dsn +from .testconfig import dsn import unittest -from testutils import ConnectingTestCase, skip_before_postgres, slow +from .testutils import ConnectingTestCase, skip_before_postgres, slow -from test_replication import ReplicationTestCase, skip_repl_if_green +from .test_replication import ReplicationTestCase, skip_repl_if_green from psycopg2.extras import LogicalReplicationConnection, StopReplication diff --git a/tests/test_bug_gc.py b/tests/test_bug_gc.py index 084236ef..c04756be 100755 --- a/tests/test_bug_gc.py +++ b/tests/test_bug_gc.py @@ -27,7 +27,7 @@ import psycopg2.extensions import unittest import gc -from testutils import ConnectingTestCase, skip_if_no_uuid +from .testutils import ConnectingTestCase, skip_if_no_uuid class StolenReferenceTestCase(ConnectingTestCase): diff --git a/tests/test_cancel.py b/tests/test_cancel.py index 7888bbff..5e550aa6 100755 --- a/tests/test_cancel.py +++ b/tests/test_cancel.py @@ -30,9 +30,9 @@ import psycopg2 import psycopg2.extensions from psycopg2 import extras -from testconfig import dsn +from .testconfig import dsn import unittest -from testutils import ConnectingTestCase, skip_before_postgres, slow +from .testutils import ConnectingTestCase, skip_before_postgres, slow class CancelTests(ConnectingTestCase): diff --git a/tests/test_connection.py b/tests/test_connection.py index 369c204c..02db2645 100755 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -34,12 +34,12 @@ import psycopg2 import psycopg2.errorcodes from psycopg2 import extensions as ext -from testutils import ( +from .testutils import ( unittest, decorate_all_tests, skip_if_no_superuser, skip_before_postgres, skip_after_postgres, skip_before_libpq, ConnectingTestCase, skip_if_tpc_disabled, skip_if_windows, slow) -from testconfig import dsn, dbname +from .testconfig import dsn, dbname class ConnectionTests(ConnectingTestCase): diff --git a/tests/test_copy.py b/tests/test_copy.py index 3201fb27..d8eba082 100755 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -25,7 +25,7 @@ import sys import string import unittest -from testutils import (ConnectingTestCase, decorate_all_tests, +from .testutils import (ConnectingTestCase, decorate_all_tests, skip_before_postgres, slow) from cStringIO import StringIO from itertools import cycle, izip @@ -33,8 +33,8 @@ from subprocess import Popen, PIPE import psycopg2 import psycopg2.extensions -from testutils import skip_copy_if_green -from testconfig import dsn +from .testutils import skip_copy_if_green +from .testconfig import dsn if sys.version_info[0] < 3: @@ -99,7 +99,7 @@ class CopyTests(ConnectingTestCase): def test_copy_from_cols(self): curs = self.conn.cursor() f = StringIO() - for i in xrange(10): + for i in range(10): f.write("%s\n" % (i,)) f.seek(0) @@ -111,7 +111,7 @@ class CopyTests(ConnectingTestCase): def test_copy_from_cols_err(self): curs = self.conn.cursor() f = StringIO() - for i in xrange(10): + for i in range(10): f.write("%s\n" % (i,)) f.seek(0) @@ -141,7 +141,7 @@ class CopyTests(ConnectingTestCase): about = abin.decode('latin1').replace('\\', '\\\\') else: - abin = bytes(range(32, 127) + range(160, 256)).decode('latin1') + abin = bytes(list(range(32, 127)) + list(range(160, 256))).decode('latin1') about = abin.replace('\\', '\\\\') curs = self.conn.cursor() @@ -162,7 +162,7 @@ class CopyTests(ConnectingTestCase): abin = ''.join(map(chr, range(32, 127) + range(160, 255))) about = abin.replace('\\', '\\\\') else: - abin = bytes(range(32, 127) + range(160, 255)).decode('latin1') + abin = bytes(list(range(32, 127)) + list(range(160, 255))).decode('latin1') about = abin.replace('\\', '\\\\').encode('latin1') curs = self.conn.cursor() @@ -185,7 +185,7 @@ class CopyTests(ConnectingTestCase): about = abin.replace('\\', '\\\\') else: - abin = bytes(range(32, 127) + range(160, 256)).decode('latin1') + abin = bytes(list(range(32, 127)) + list(range(160, 256))).decode('latin1') about = abin.replace('\\', '\\\\') import io @@ -225,7 +225,7 @@ class CopyTests(ConnectingTestCase): def _copy_from(self, curs, nrecs, srec, copykw): f = StringIO() - for i, c in izip(xrange(nrecs), cycle(string.ascii_letters)): + for i, c in zip(range(nrecs), cycle(string.ascii_letters)): l = c * srec f.write("%s\t%s\n" % (i, l)) diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 5c94f422..840819ac 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -27,7 +27,7 @@ import pickle import psycopg2 import psycopg2.extensions import unittest -from testutils import (ConnectingTestCase, skip_before_postgres, +from .testutils import (ConnectingTestCase, skip_before_postgres, skip_if_no_getrefcount, slow, skip_if_no_superuser, skip_if_windows) diff --git a/tests/test_dates.py b/tests/test_dates.py index ccfdc20a..47ef41cb 100755 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -26,7 +26,7 @@ import math import psycopg2 from psycopg2.tz import FixedOffsetTimezone, ZERO import unittest -from testutils import ConnectingTestCase, skip_before_postgres +from .testutils import ConnectingTestCase, skip_before_postgres def total_seconds(d): diff --git a/tests/test_errcodes.py b/tests/test_errcodes.py index 0fdf8e59..6775d83e 100755 --- a/tests/test_errcodes.py +++ b/tests/test_errcodes.py @@ -23,7 +23,7 @@ # License for more details. import unittest -from testutils import ConnectingTestCase, slow +from .testutils import ConnectingTestCase, slow try: reload diff --git a/tests/test_extras_dictcursor.py b/tests/test_extras_dictcursor.py index b014a81b..5a726ec6 100755 --- a/tests/test_extras_dictcursor.py +++ b/tests/test_extras_dictcursor.py @@ -19,7 +19,7 @@ from datetime import timedelta import psycopg2 import psycopg2.extras import unittest -from testutils import ConnectingTestCase, skip_before_postgres +from .testutils import ConnectingTestCase, skip_before_postgres class ExtrasDictCursorTests(ConnectingTestCase): diff --git a/tests/test_fast_executemany.py b/tests/test_fast_executemany.py index 32b34545..ab0a129a 100755 --- a/tests/test_fast_executemany.py +++ b/tests/test_fast_executemany.py @@ -16,7 +16,7 @@ from datetime import date -import testutils +from . import testutils import unittest import psycopg2 diff --git a/tests/test_green.py b/tests/test_green.py index 8c1c20ce..65f483ca 100755 --- a/tests/test_green.py +++ b/tests/test_green.py @@ -27,7 +27,7 @@ import psycopg2 import psycopg2.extensions import psycopg2.extras -from testutils import ConnectingTestCase, slow +from .testutils import ConnectingTestCase, slow class ConnectionStub(object): diff --git a/tests/test_ipaddress.py b/tests/test_ipaddress.py index ea193bf8..2b4ff6af 100755 --- a/tests/test_ipaddress.py +++ b/tests/test_ipaddress.py @@ -19,7 +19,7 @@ from __future__ import unicode_literals import sys from functools import wraps -import testutils +from . import testutils import unittest import psycopg2 diff --git a/tests/test_lobject.py b/tests/test_lobject.py index 7e91fe7a..a9966313 100755 --- a/tests/test_lobject.py +++ b/tests/test_lobject.py @@ -30,7 +30,7 @@ from functools import wraps import psycopg2 import psycopg2.extensions import unittest -from testutils import (decorate_all_tests, skip_if_tpc_disabled, +from .testutils import (decorate_all_tests, skip_if_tpc_disabled, ConnectingTestCase, skip_if_green, slow) diff --git a/tests/test_module.py b/tests/test_module.py index e0533ac8..e1c065d5 100755 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -27,7 +27,7 @@ import sys from subprocess import Popen import unittest -from testutils import (skip_before_postgres, +from .testutils import (skip_before_postgres, ConnectingTestCase, skip_copy_if_green, slow) import psycopg2 diff --git a/tests/test_notify.py b/tests/test_notify.py index 64835efb..bce061e7 100755 --- a/tests/test_notify.py +++ b/tests/test_notify.py @@ -26,8 +26,8 @@ import unittest import psycopg2 from psycopg2 import extensions -from testutils import ConnectingTestCase, slow -from testconfig import dsn +from .testutils import ConnectingTestCase, slow +from .testconfig import dsn import sys import time diff --git a/tests/test_psycopg2_dbapi20.py b/tests/test_psycopg2_dbapi20.py index 3bcedc41..d69c6073 100755 --- a/tests/test_psycopg2_dbapi20.py +++ b/tests/test_psycopg2_dbapi20.py @@ -22,14 +22,14 @@ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public # License for more details. -import dbapi20 -import dbapi20_tpc -from testutils import skip_if_tpc_disabled +from . import dbapi20 +from . import dbapi20_tpc +from .testutils import skip_if_tpc_disabled import unittest -from testutils import decorate_all_tests +from .testutils import decorate_all_tests import psycopg2 -from testconfig import dsn +from .testconfig import dsn class Psycopg2Tests(dbapi20.DatabaseAPI20Test): diff --git a/tests/test_quote.py b/tests/test_quote.py index 0f60dd9f..a79bb56e 100755 --- a/tests/test_quote.py +++ b/tests/test_quote.py @@ -23,9 +23,9 @@ # License for more details. import sys -import testutils +from . import testutils import unittest -from testutils import ConnectingTestCase +from .testutils import ConnectingTestCase import psycopg2 import psycopg2.extensions @@ -81,7 +81,7 @@ class QuotingTestCase(ConnectingTestCase): if sys.version_info[0] < 3: data += "".join(map(chr, range(256))) else: - data += bytes(range(256)) + data += bytes(list(range(256))) curs = self.conn.cursor() curs.execute("SELECT %s::bytea;", (psycopg2.Binary(data),)) @@ -126,7 +126,7 @@ class QuotingTestCase(ConnectingTestCase): if sys.version_info[0] < 3: data = ''.join(map(chr, range(32, 127) + range(160, 256))) else: - data = bytes(range(32, 127) + range(160, 256)).decode('latin1') + data = bytes(list(range(32, 127)) + list(range(160, 256))).decode('latin1') # as string curs.execute("SELECT %s::text;", (data,)) @@ -150,7 +150,7 @@ class QuotingTestCase(ConnectingTestCase): if sys.version_info[0] < 3: data = ''.join(map(chr, range(32, 127) + range(128, 256))) else: - data = bytes(range(32, 127) + range(128, 256)).decode('koi8_r') + data = bytes(list(range(32, 127)) + list(range(128, 256))).decode('koi8_r') # as string curs.execute("SELECT %s::text;", (data,)) diff --git a/tests/test_replication.py b/tests/test_replication.py index 444dd111..f686d2bc 100755 --- a/tests/test_replication.py +++ b/tests/test_replication.py @@ -26,10 +26,10 @@ import psycopg2 from psycopg2.extras import ( PhysicalReplicationConnection, LogicalReplicationConnection, StopReplication) -import testconfig +from . import testconfig import unittest -from testutils import ConnectingTestCase -from testutils import skip_before_postgres, skip_if_green +from .testutils import ConnectingTestCase +from .testutils import skip_before_postgres, skip_if_green skip_repl_if_green = skip_if_green("replication not supported in green mode") diff --git a/tests/test_sql.py b/tests/test_sql.py index 566cc281..6cb978ff 100755 --- a/tests/test_sql.py +++ b/tests/test_sql.py @@ -25,7 +25,7 @@ import datetime as dt from cStringIO import StringIO import unittest -from testutils import (ConnectingTestCase, +from .testutils import (ConnectingTestCase, skip_before_postgres, skip_before_python, skip_copy_if_green) import psycopg2 diff --git a/tests/test_transaction.py b/tests/test_transaction.py index 26704d8f..060c8d41 100755 --- a/tests/test_transaction.py +++ b/tests/test_transaction.py @@ -24,7 +24,7 @@ import threading import unittest -from testutils import ConnectingTestCase, skip_before_postgres, slow +from .testutils import ConnectingTestCase, skip_before_postgres, slow import psycopg2 from psycopg2.extensions import ( diff --git a/tests/test_types_basic.py b/tests/test_types_basic.py index cd1a4f6b..6e34a477 100755 --- a/tests/test_types_basic.py +++ b/tests/test_types_basic.py @@ -26,9 +26,9 @@ import decimal import sys from functools import wraps -import testutils +from . import testutils import unittest -from testutils import ConnectingTestCase, decorate_all_tests +from .testutils import ConnectingTestCase, decorate_all_tests import psycopg2 @@ -326,7 +326,7 @@ class TypesBasicTests(ConnectingTestCase): self.assertEqual(1, f1) i1 = self.execute("select -%s;", (-1,)) self.assertEqual(1, i1) - l1 = self.execute("select -%s;", (-1L,)) + l1 = self.execute("select -%s;", (long(-1),)) self.assertEqual(1, l1) def testGenericArray(self): diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index 576e13c8..8901bab1 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -23,7 +23,7 @@ from functools import wraps from pickle import dumps, loads import unittest -from testutils import (skip_if_no_uuid, skip_before_postgres, +from .testutils import (skip_if_no_uuid, skip_before_postgres, ConnectingTestCase, decorate_all_tests, py3_raises_typeerror, slow) import psycopg2 diff --git a/tests/test_with.py b/tests/test_with.py index 83612dec..1392d85f 100755 --- a/tests/test_with.py +++ b/tests/test_with.py @@ -26,7 +26,7 @@ import psycopg2 import psycopg2.extensions as ext import unittest -from testutils import ConnectingTestCase +from .testutils import ConnectingTestCase class WithTestCase(ConnectingTestCase): diff --git a/tests/testutils.py b/tests/testutils.py index 2ee07af2..e1744c84 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -29,7 +29,7 @@ import select import platform import unittest from functools import wraps -from testconfig import dsn, repl_dsn +from .testconfig import dsn, repl_dsn # Silence warnings caused by the stubbornness of the Python unittest @@ -338,7 +338,7 @@ def skip_if_green(reason): def skip_if_green_(f): @wraps(f) def skip_if_green__(self): - from testconfig import green + from .testconfig import green if green: return self.skipTest(reason) else: From c86e682153f4cb8d99e490b2af964f3f11e3489f Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 3 Dec 2017 18:47:19 -0800 Subject: [PATCH 65/96] Skip register_hstore(..., unicode=True) tests on Python 3 Per the functions documentation, this argument is not supported on Python 3. Skip it during tests. > :param unicode: if `!True`, keys and values returned from the database > will be `!unicode` instead of `!str`. The option is not available on > Python 3 Work towards moving tests outside of the installed package. --- tests/test_types_extras.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index 8901bab1..abe06103 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -24,7 +24,8 @@ from pickle import dumps, loads import unittest from .testutils import (skip_if_no_uuid, skip_before_postgres, - ConnectingTestCase, decorate_all_tests, py3_raises_typeerror, slow) + ConnectingTestCase, decorate_all_tests, py3_raises_typeerror, slow, + skip_from_python) import psycopg2 import psycopg2.extras @@ -250,6 +251,7 @@ class HstoreTestCase(ConnectingTestCase): self.assertEqual(t[2], {'a': 'b'}) @skip_if_no_hstore + @skip_from_python(3) def test_register_unicode(self): from psycopg2.extras import register_hstore @@ -318,6 +320,7 @@ class HstoreTestCase(ConnectingTestCase): ok(dict(zip(ab, ab))) @skip_if_no_hstore + @skip_from_python(3) def test_roundtrip_unicode(self): from psycopg2.extras import register_hstore register_hstore(self.conn, unicode=True) From 389f6c08d9536060184bfd5c8ea2491a4a91c2fb Mon Sep 17 00:00:00 2001 From: Jon Dufresne Date: Sun, 3 Dec 2017 18:47:19 -0800 Subject: [PATCH 66/96] Avoid installing tests to site-packages For library end users, there is no need to install tests alongside the package itself. This keeps the tests available for development without adding extra packages to user's site-packages directory. Reduces the size of the installed package. Avoids accidental execution of test code by an installed package. --- .appveyor.yml | 3 ++- .travis.yml | 1 + Makefile | 5 ++--- NEWS | 3 +++ doc/src/install.rst | 4 ++-- scripts/refcounter.py | 4 ++-- scripts/travis_test.sh | 4 ++-- setup.py | 4 ++-- tests/test_async.py | 5 ++--- tests/test_copy.py | 5 ++--- tests/test_cursor.py | 2 +- tests/test_errcodes.py | 4 ++-- tests/test_extras_dictcursor.py | 2 +- tests/test_module.py | 3 +-- tests/test_quote.py | 2 +- tests/test_sql.py | 4 ++-- tests/test_types_basic.py | 6 +++--- tests/test_types_extras.py | 16 ++++++++-------- tests/testutils.py | 17 +++++++++++++++++ 19 files changed, 56 insertions(+), 38 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 44dbdbd6..a49c802f 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -238,6 +238,7 @@ build_script: - "%PYTHON%\\python.exe setup.py build_ext --have-ssl --pg-config %PGTOP%\\bin\\pg_config.exe -l libpgcommon -l libpgport -L %OPENSSLTOP%\\lib -I %OPENSSLTOP%\\include" - "%PYTHON%\\python.exe setup.py build" - "%PYTHON%\\python.exe setup.py install" + - RD /S /Q psycopg2.egg-info #after_build: @@ -251,4 +252,4 @@ test_script: - "%PYTHON%\\python.exe -c \"import psycopg2; print(psycopg2.__version__)\"" - "%PYTHON%\\python.exe -c \"import psycopg2; print(psycopg2.__libpq_version__)\"" - "%PYTHON%\\python.exe -c \"import psycopg2; print(psycopg2.extensions.libpq_version())\"" - - "%PYTHON%\\python.exe -c \"from psycopg2 import tests; tests.unittest.main(defaultTest='tests.test_suite')\" --verbose" + - "%PYTHON%\\python.exe -c \"import tests; tests.unittest.main(defaultTest='tests.test_suite')\" --verbose" diff --git a/.travis.yml b/.travis.yml index e6182a75..2fbf7010 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,6 +12,7 @@ python: install: - python setup.py install + - rm -rf psycopg2.egg-info - sudo scripts/travis_prepare.sh script: diff --git a/Makefile b/Makefile index a8f491e4..07fbc215 100644 --- a/Makefile +++ b/Makefile @@ -29,8 +29,7 @@ SOURCE := $(SOURCE_C) $(SOURCE_PY) $(SOURCE_TESTS) $(SOURCE_DOC) PACKAGE := $(BUILD_DIR)/psycopg2 PLATLIB := $(PACKAGE)/_psycopg.so -PURELIB := $(patsubst lib/%,$(PACKAGE)/%,$(SOURCE_PY)) \ - $(patsubst tests/%,$(PACKAGE)/tests/%,$(SOURCE_TESTS)) +PURELIB := $(patsubst lib/%,$(PACKAGE)/%,$(SOURCE_PY)) BUILD_OPT := --build-lib=$(BUILD_DIR) BUILD_EXT_OPT := --build-lib=$(BUILD_DIR) @@ -66,7 +65,7 @@ env: $(MAKE) -C doc $@ check: - PYTHONPATH=$(BUILD_DIR):$(PYTHONPATH) $(PYTHON) -c "from psycopg2 import tests; tests.unittest.main(defaultTest='tests.test_suite')" --verbose + PYTHONPATH=$(BUILD_DIR):$(PYTHONPATH) $(PYTHON) -c "import tests; tests.unittest.main(defaultTest='tests.test_suite')" --verbose testdb: @echo "* Creating $(TESTDB)" diff --git a/NEWS b/NEWS index fe770278..a1085252 100644 --- a/NEWS +++ b/NEWS @@ -9,6 +9,9 @@ Other changes: - Dropped support for Python 2.6, 3.2, 3.3. - Dropped `psycopg1` module. - Dropped deprecated ``register_tstz_w_secs()`` (was previously a no-op). +- The ``psycopg2.test`` package is no longer installed by ``python setup.py + install``. The test source files now are compatible with Python 2 and 3 + without using 2to3. What's new in psycopg 2.7.4 diff --git a/doc/src/install.rst b/doc/src/install.rst index 8b6651da..6d30e343 100644 --- a/doc/src/install.rst +++ b/doc/src/install.rst @@ -267,11 +267,11 @@ Running the test suite ---------------------- Once `!psycopg2` is installed you can run the test suite to verify it is -working correctly. You can run: +working correctly. From the source directory, you can run: .. code-block:: console - $ python -c "from psycopg2 import tests; tests.unittest.main(defaultTest='tests.test_suite')" --verbose + $ python -c "import tests; tests.unittest.main(defaultTest='tests.test_suite')" --verbose The tests run against a database called ``psycopg2_test`` on UNIX socket and the standard port. You can configure a different database to run the test by diff --git a/scripts/refcounter.py b/scripts/refcounter.py index f1642e7a..f41dc211 100755 --- a/scripts/refcounter.py +++ b/scripts/refcounter.py @@ -30,8 +30,8 @@ from collections import defaultdict def main(): opt = parse_args() - import psycopg2.tests - test = psycopg2.tests + import tests + test = tests if opt.suite: test = getattr(test, opt.suite) diff --git a/scripts/travis_test.sh b/scripts/travis_test.sh index 033482b8..0320654a 100755 --- a/scripts/travis_test.sh +++ b/scripts/travis_test.sh @@ -34,13 +34,13 @@ run_test () { export PSYCOPG2_TEST_REPL_DSN= unset PSYCOPG2_TEST_GREEN python -c \ - "from psycopg2 import tests; tests.unittest.main(defaultTest='tests.test_suite')" \ + "import tests; tests.unittest.main(defaultTest='tests.test_suite')" \ $VERBOSE printf "\n\nRunning tests against PostgreSQL $VERSION (green mode)\n\n" export PSYCOPG2_TEST_GREEN=1 python -c \ - "from psycopg2 import tests; tests.unittest.main(defaultTest='tests.test_suite')" \ + "import tests; tests.unittest.main(defaultTest='tests.test_suite')" \ $VERBOSE } diff --git a/setup.py b/setup.py index e6144af9..0e214629 100644 --- a/setup.py +++ b/setup.py @@ -624,8 +624,8 @@ setup(name="psycopg2", long_description="\n".join(readme.split("\n")[2:]).lstrip(), classifiers=[x for x in classifiers.split("\n") if x], data_files=data_files, - package_dir={'psycopg2': 'lib', 'psycopg2.tests': 'tests'}, - packages=['psycopg2', 'psycopg2.tests'], + package_dir={'psycopg2': 'lib'}, + packages=['psycopg2'], cmdclass={ 'build_ext': psycopg_build_ext, 'build_py': build_py, }, diff --git a/tests/test_async.py b/tests/test_async.py index 6469a252..ce5e9b19 100755 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -30,9 +30,8 @@ import psycopg2 from psycopg2 import extensions as ext import time -import StringIO -from .testutils import ConnectingTestCase +from .testutils import ConnectingTestCase, StringIO class PollableStub(object): @@ -241,7 +240,7 @@ class AsyncTests(ConnectingTestCase): # copy should fail self.assertRaises(psycopg2.ProgrammingError, cur.copy_from, - StringIO.StringIO("1\n3\n5\n\\.\n"), "table1") + StringIO("1\n3\n5\n\\.\n"), "table1") def test_lobject_while_async(self): # large objects should be prohibited diff --git a/tests/test_copy.py b/tests/test_copy.py index d8eba082..b16e1ddc 100755 --- a/tests/test_copy.py +++ b/tests/test_copy.py @@ -26,9 +26,8 @@ import sys import string import unittest from .testutils import (ConnectingTestCase, decorate_all_tests, - skip_before_postgres, slow) -from cStringIO import StringIO -from itertools import cycle, izip + skip_before_postgres, slow, StringIO) +from itertools import cycle from subprocess import Popen, PIPE import psycopg2 diff --git a/tests/test_cursor.py b/tests/test_cursor.py index 840819ac..ec76918b 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -29,7 +29,7 @@ import psycopg2.extensions import unittest from .testutils import (ConnectingTestCase, skip_before_postgres, skip_if_no_getrefcount, slow, skip_if_no_superuser, - skip_if_windows) + skip_if_windows, unicode) import psycopg2.extras diff --git a/tests/test_errcodes.py b/tests/test_errcodes.py index 6775d83e..5ce2cef4 100755 --- a/tests/test_errcodes.py +++ b/tests/test_errcodes.py @@ -23,7 +23,7 @@ # License for more details. import unittest -from .testutils import ConnectingTestCase, slow +from .testutils import ConnectingTestCase, slow, reload try: reload @@ -52,7 +52,7 @@ class ErrocodeTests(ConnectingTestCase): except Exception as e: errs.append(e) - for __ in xrange(MAX_CYCLES): + for __ in range(MAX_CYCLES): reload(errorcodes) (t1, t2) = (Thread(target=f), Thread(target=f)) (t1.start(), t2.start()) diff --git a/tests/test_extras_dictcursor.py b/tests/test_extras_dictcursor.py index 5a726ec6..75c22773 100755 --- a/tests/test_extras_dictcursor.py +++ b/tests/test_extras_dictcursor.py @@ -390,7 +390,7 @@ class NamedTupleCursorTest(ConnectingTestCase): recs.extend(curs.fetchmany(5)) recs.append(curs.fetchone()) recs.extend(curs.fetchall()) - self.assertEqual(range(10), [t.i for t in recs]) + self.assertEqual(list(range(10)), [t.i for t in recs]) def test_named_fetchone(self): curs = self.conn.cursor('tmp') diff --git a/tests/test_module.py b/tests/test_module.py index e1c065d5..ddd6b027 100755 --- a/tests/test_module.py +++ b/tests/test_module.py @@ -28,7 +28,7 @@ from subprocess import Popen import unittest from .testutils import (skip_before_postgres, - ConnectingTestCase, skip_copy_if_green, slow) + ConnectingTestCase, skip_copy_if_green, slow, StringIO) import psycopg2 @@ -217,7 +217,6 @@ class ExceptionsTestCase(ConnectingTestCase): @skip_copy_if_green def test_diagnostics_copy(self): - from StringIO import StringIO f = StringIO() cur = self.conn.cursor() try: diff --git a/tests/test_quote.py b/tests/test_quote.py index a79bb56e..fad6ceec 100755 --- a/tests/test_quote.py +++ b/tests/test_quote.py @@ -25,7 +25,7 @@ import sys from . import testutils import unittest -from .testutils import ConnectingTestCase +from .testutils import ConnectingTestCase, unichr import psycopg2 import psycopg2.extensions diff --git a/tests/test_sql.py b/tests/test_sql.py index 6cb978ff..1c20997c 100755 --- a/tests/test_sql.py +++ b/tests/test_sql.py @@ -23,10 +23,10 @@ # License for more details. import datetime as dt -from cStringIO import StringIO import unittest from .testutils import (ConnectingTestCase, - skip_before_postgres, skip_before_python, skip_copy_if_green) + skip_before_postgres, skip_before_python, skip_copy_if_green, + unicode, StringIO) import psycopg2 from psycopg2 import sql diff --git a/tests/test_types_basic.py b/tests/test_types_basic.py index 6e34a477..a93265d9 100755 --- a/tests/test_types_basic.py +++ b/tests/test_types_basic.py @@ -28,7 +28,7 @@ import sys from functools import wraps from . import testutils import unittest -from .testutils import ConnectingTestCase, decorate_all_tests +from .testutils import ConnectingTestCase, decorate_all_tests, long import psycopg2 @@ -54,8 +54,8 @@ class TypesBasicTests(ConnectingTestCase): def testNumber(self): s = self.execute("SELECT %s AS foo", (1971,)) self.failUnless(s == 1971, "wrong integer quoting: " + str(s)) - s = self.execute("SELECT %s AS foo", (1971L,)) - self.failUnless(s == 1971L, "wrong integer quoting: " + str(s)) + s = self.execute("SELECT %s AS foo", (long(1971),)) + self.failUnless(s == long(1971), "wrong integer quoting: " + str(s)) def testBoolean(self): x = self.execute("SELECT %s as foo", (False,)) diff --git a/tests/test_types_extras.py b/tests/test_types_extras.py index abe06103..5cb13534 100755 --- a/tests/test_types_extras.py +++ b/tests/test_types_extras.py @@ -181,7 +181,7 @@ class HstoreTestCase(ConnectingTestCase): kk = m.group(1).split(b", ") vv = m.group(2).split(b", ") - ii = zip(kk, vv) + ii = list(zip(kk, vv)) ii.sort() self.assertEqual(len(ii), len(o)) @@ -306,7 +306,7 @@ class HstoreTestCase(ConnectingTestCase): ok({}) ok({'a': 'b', 'c': None}) - ab = map(chr, range(32, 128)) + ab = list(map(chr, range(32, 128))) ok(dict(zip(ab, ab))) ok({''.join(ab): ''.join(ab)}) @@ -314,7 +314,7 @@ class HstoreTestCase(ConnectingTestCase): if sys.version_info[0] < 3: ab = map(chr, range(32, 127) + range(160, 255)) else: - ab = bytes(range(32, 127) + range(160, 255)).decode('latin1') + ab = bytes(list(range(32, 127)) + list(range(160, 255))).decode('latin1') ok({''.join(ab): ''.join(ab)}) ok(dict(zip(ab, ab))) @@ -371,7 +371,7 @@ class HstoreTestCase(ConnectingTestCase): ds = [{}, {'a': 'b', 'c': None}] - ab = map(chr, range(32, 128)) + ab = list(map(chr, range(32, 128))) ds.append(dict(zip(ab, ab))) ds.append({''.join(ab): ''.join(ab)}) @@ -379,7 +379,7 @@ class HstoreTestCase(ConnectingTestCase): if sys.version_info[0] < 3: ab = map(chr, range(32, 127) + range(160, 255)) else: - ab = bytes(range(32, 127) + range(160, 255)).decode('latin1') + ab = bytes(list(range(32, 127)) + list(range(160, 255))).decode('latin1') ds.append({''.join(ab): ''.join(ab)}) ds.append(dict(zip(ab, ab))) @@ -514,7 +514,7 @@ class AdaptTypeTestCase(ConnectingTestCase): '@,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,[,"\\\\",],' '^,_,`,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,{,|,},' '~,\x7f)', - map(chr, range(1, 128))) + list(map(chr, range(1, 128)))) ok('(,"\x01\x02\x03\x04\x05\x06\x07\x08\t\n\x0b\x0c\r\x0e\x0f' '\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f !' '""#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\\\]' @@ -1641,8 +1641,8 @@ class RangeCasterTestCase(ConnectingTestCase): bounds = ['[)', '(]', '()', '[]'] ranges = [TextRange(low, up, bounds[i % 4]) for i, (low, up) in enumerate(zip( - [None] + map(chr, range(1, 128)), - map(chr, range(1, 128)) + [None], + [None] + list(map(chr, range(1, 128))), + list(map(chr, range(1, 128))) + [None], ))] ranges.append(TextRange()) ranges.append(TextRange(empty=True)) diff --git a/tests/testutils.py b/tests/testutils.py index e1744c84..d70e091e 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -31,6 +31,23 @@ import unittest from functools import wraps from .testconfig import dsn, repl_dsn +# Python 2/3 compatibility + +if sys.version_info[0] == 2: + # Python 2 + from StringIO import StringIO + long = long + reload = reload + unichr = unichr + unicode = unicode +else: + # Python 3 + from io import StringIO + from importlib import reload + long = int + unichr = chr + unicode = str + # Silence warnings caused by the stubbornness of the Python unittest # maintainers From 4be2b75816890c0e64430e0bb2249c868f8edc0d Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 11 Dec 2017 02:22:02 +0000 Subject: [PATCH 67/96] Dropped PYTHONPATH propagation in 'make check' Psycopg doesn't have other dependencies outside the stdlib so what's there is probably just noise. --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 07fbc215..f22ad6be 100644 --- a/Makefile +++ b/Makefile @@ -65,7 +65,7 @@ env: $(MAKE) -C doc $@ check: - PYTHONPATH=$(BUILD_DIR):$(PYTHONPATH) $(PYTHON) -c "import tests; tests.unittest.main(defaultTest='tests.test_suite')" --verbose + PYTHONPATH=$(BUILD_DIR) $(PYTHON) -c "import tests; tests.unittest.main(defaultTest='tests.test_suite')" --verbose testdb: @echo "* Creating $(TESTDB)" From 2cd9a78a97ee787517960ce6f066447e42ef8705 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 11 Dec 2017 02:25:13 +0000 Subject: [PATCH 68/96] Gitignore env2/env3 envs too --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 88f3c787..356570ed 100644 --- a/.gitignore +++ b/.gitignore @@ -12,6 +12,7 @@ doc/html/* doc/psycopg2.txt scripts/pypi_docs_upload.py env +env? .idea .tox /rel From 852884e086706625a69247f2ddafd1a1465b138c Mon Sep 17 00:00:00 2001 From: Glyph Date: Tue, 12 Dec 2017 03:06:18 -0800 Subject: [PATCH 69/96] use accessor macros for pypy3 compatibility --- psycopg/adapter_datetime.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/psycopg/adapter_datetime.c b/psycopg/adapter_datetime.c index 9d04df40..a78311ee 100644 --- a/psycopg/adapter_datetime.c +++ b/psycopg/adapter_datetime.c @@ -100,7 +100,7 @@ _pydatetime_string_delta(pydatetimeObject *self) char buffer[8]; int i; - int a = obj->microseconds; + int a = PyDateTime_DELTA_GET_MICROSECONDS(obj); for (i=0; i < 6 ; i++) { buffer[5-i] = '0' + (a % 10); @@ -109,7 +109,9 @@ _pydatetime_string_delta(pydatetimeObject *self) buffer[6] = '\0'; return Bytes_FromFormat("'%d days %d.%s seconds'::interval", - obj->days, obj->seconds, buffer); + PyDateTime_DELTA_GET_DAYS(obj), + PyDateTime_DELTA_GET_SECONDS(obj), + buffer); } static PyObject * From 3fcb0351265e3077ee4088ba6e90016e5735a2a1 Mon Sep 17 00:00:00 2001 From: Glyph Date: Tue, 12 Dec 2017 03:12:54 -0800 Subject: [PATCH 70/96] define a "polyfill" inline for python 2 compatibility --- psycopg/adapter_datetime.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/psycopg/adapter_datetime.c b/psycopg/adapter_datetime.c index a78311ee..b5135efb 100644 --- a/psycopg/adapter_datetime.c +++ b/psycopg/adapter_datetime.c @@ -93,6 +93,12 @@ error: return rv; } +#ifndef PyDateTime_DELTA_GET_DAYS +#define PyDateTime_DELTA_GET_DAYS(o) (o->days) +#define PyDateTime_DELTA_GET_SECONDS(o) (o->seconds) +#define PyDateTime_DELTA_GET_MICROSECONDS(o) (o->microseconds) +#endif + static PyObject * _pydatetime_string_delta(pydatetimeObject *self) { From b8f2f718190ce5ab73ee8523a18df1ccbe68ad0c Mon Sep 17 00:00:00 2001 From: Ronan Amicel Date: Thu, 14 Dec 2017 14:00:36 +0100 Subject: [PATCH 71/96] Fix typo in comment in utils.c --- psycopg/utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/psycopg/utils.c b/psycopg/utils.c index 7073504f..a197fe22 100644 --- a/psycopg/utils.c +++ b/psycopg/utils.c @@ -282,7 +282,7 @@ exit: /* Make a connection string out of a string and a dictionary of arguments. * - * Helper to call psycopg2.extensions.make_dns() + * Helper to call psycopg2.extensions.make_dsn() */ PyObject * psycopg_make_dsn(PyObject *dsn, PyObject *kwargs) From 77c703395befce2cc87a18a5ec706f5500f04f04 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 10 Jan 2018 22:24:05 +0000 Subject: [PATCH 72/96] Moved datatime compatibility macros with others --- psycopg/adapter_datetime.c | 6 ------ psycopg/python.h | 5 +++++ 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/psycopg/adapter_datetime.c b/psycopg/adapter_datetime.c index b5135efb..a78311ee 100644 --- a/psycopg/adapter_datetime.c +++ b/psycopg/adapter_datetime.c @@ -93,12 +93,6 @@ error: return rv; } -#ifndef PyDateTime_DELTA_GET_DAYS -#define PyDateTime_DELTA_GET_DAYS(o) (o->days) -#define PyDateTime_DELTA_GET_SECONDS(o) (o->seconds) -#define PyDateTime_DELTA_GET_MICROSECONDS(o) (o->microseconds) -#endif - static PyObject * _pydatetime_string_delta(pydatetimeObject *self) { diff --git a/psycopg/python.h b/psycopg/python.h index fc8c2fed..fa894bf3 100644 --- a/psycopg/python.h +++ b/psycopg/python.h @@ -87,6 +87,7 @@ typedef unsigned long Py_uhash_t; #ifndef PyNumber_Int #define PyNumber_Int PyNumber_Long #endif + #endif /* PY_MAJOR_VERSION > 2 */ #if PY_MAJOR_VERSION < 3 @@ -104,6 +105,10 @@ typedef unsigned long Py_uhash_t; #define Bytes_ConcatAndDel PyString_ConcatAndDel #define _Bytes_Resize _PyString_Resize +#define PyDateTime_DELTA_GET_DAYS(o) (((PyDateTime_Delta*)o)->days) +#define PyDateTime_DELTA_GET_SECONDS(o) (((PyDateTime_Delta*)o)->seconds) +#define PyDateTime_DELTA_GET_MICROSECONDS(o) (((PyDateTime_Delta*)o)->microseconds) + #else #define Bytes_Type PyBytes_Type From b3a70e09e960fd8d66e0f6a43962f7bf5cab2ba0 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 10 Jan 2018 22:44:37 +0000 Subject: [PATCH 73/96] pypi3 fix noted in the news file --- NEWS | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS b/NEWS index a1085252..1f1d7c85 100644 --- a/NEWS +++ b/NEWS @@ -24,6 +24,7 @@ What's new in psycopg 2.7.4 (:ticket:`632`). - Fixed `~cursor.rowcount` after `~cursor.executemany()` with :sql:`RETURNING` statements (:ticket:`633`). +- Fixed compatibility problem with pypy3 (:ticket:`#649`). - Wheel packages compiled against PostgreSQL 10.1 libpq and OpenSSL 1.0.2m. From 04f1f06b9f1f627436c1202aaa45434334c8747d Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 10 Jan 2018 22:49:00 +0000 Subject: [PATCH 74/96] Fixed stitch_text on Python 2 --- doc/src/tools/stitch_text.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/src/tools/stitch_text.py b/doc/src/tools/stitch_text.py index dca745bd..e026622f 100755 --- a/doc/src/tools/stitch_text.py +++ b/doc/src/tools/stitch_text.py @@ -5,6 +5,7 @@ import os import sys + def main(): if len(sys.argv) != 3: sys.stderr.write("usage: %s index.rst text-dir\n") @@ -17,20 +18,20 @@ def main(): return 0 + def iter_file_base(fn): f = open(fn) - have_line = iter(f).__next__ - while not have_line().startswith('.. toctree'): + while not next(f).startswith('.. toctree'): pass - while have_line().strip().startswith(':'): + while next(f).strip().startswith(':'): pass yield os.path.splitext(os.path.basename(fn))[0] n = 0 while True: - line = have_line() + line = next(f) if line.isspace(): continue if line.startswith(".."): @@ -44,6 +45,7 @@ def iter_file_base(fn): # maybe format changed? raise Exception("Not enough files found. Format change in index.rst?") + def emit(basename, txt_dir): f = open(os.path.join(txt_dir, basename + ".txt")) for line in f: From e0226fc46ad21f10ca612b32659bab442d2db871 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Wed, 10 Jan 2018 23:06:31 +0000 Subject: [PATCH 75/96] 'cursor.mogrify()' can be called on closed cursors Fix #579. --- NEWS | 1 + psycopg/cursor_type.c | 2 -- tests/test_cursor.py | 6 ++++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 1f1d7c85..3bc13053 100644 --- a/NEWS +++ b/NEWS @@ -18,6 +18,7 @@ What's new in psycopg 2.7.4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Fixed Solaris 10 support (:ticket:`#532`). +- `cursor.mogrify()` can be called on closed cursors (:ticket:`#579`). - Fixed `~psycopg2.extras.MinTimeLoggingCursor` on Python 3 (:ticket:`#609`). - Fixed parsing of array of points as floats (:ticket:`#613`). - Fixed `~psycopg2.__libpq_version__` building with libpq >= 10.1 diff --git a/psycopg/cursor_type.c b/psycopg/cursor_type.c index a70e9d34..b7fd1870 100644 --- a/psycopg/cursor_type.c +++ b/psycopg/cursor_type.c @@ -592,8 +592,6 @@ psyco_curs_mogrify(cursorObject *self, PyObject *args, PyObject *kwargs) return NULL; } - EXC_IF_CURS_CLOSED(self); - return _psyco_curs_mogrify(self, operation, vars); } diff --git a/tests/test_cursor.py b/tests/test_cursor.py index ec76918b..cc8db0f4 100755 --- a/tests/test_cursor.py +++ b/tests/test_cursor.py @@ -118,6 +118,12 @@ class CursorTests(ConnectingTestCase): nref2 = sys.getrefcount(foo) self.assertEqual(nref1, nref2) + def test_modify_closed(self): + cur = self.conn.cursor() + cur.close() + sql = cur.mogrify("select %s", (10,)) + self.assertEqual(sql, b"select 10") + def test_bad_placeholder(self): cur = self.conn.cursor() self.assertRaises(psycopg2.ProgrammingError, From a78ac3c1253717f1f0f8f87f0c868e54f42af440 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 11 Jan 2018 08:52:03 +0900 Subject: [PATCH 76/96] Fixed test in asian time zones Fix #652 --- tests/test_dates.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_dates.py b/tests/test_dates.py index 47ef41cb..74dfc9ab 100755 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -639,7 +639,8 @@ class FromTicksTestCase(unittest.TestCase): def test_date_value_error_sec_59_99(self): from datetime import date s = psycopg2.DateFromTicks(1273173119.99992) - self.assertEqual(s.adapted, date(2010, 5, 6)) + # The returned date is local + self.assert_(s.adapted in [date(2010, 5, 6), date(2010, 5, 7)]) def test_time_value_error_sec_59_99(self): from datetime import time From ddef2e30cd080c21f4a52a757c642069ceb935c5 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 11 Jan 2018 00:14:36 +0000 Subject: [PATCH 77/96] 'key' docs in getconn() improved Fix #569. --- doc/src/pool.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/doc/src/pool.rst b/doc/src/pool.rst index 7624dc88..38cd08fa 100644 --- a/doc/src/pool.rst +++ b/doc/src/pool.rst @@ -24,13 +24,18 @@ directly in the client application. .. method:: getconn(key=None) - Get a free connection and assign it to *key* if not `!None`. + Get a free connection from the pool. + + The *key* parameter is optional: if used, the connection will be + associated to the key and calling `!getconn()` with the same key again + will return the same connection. .. method:: putconn(conn, key=None, close=False) Put away a connection. If *close* is `!True`, discard the connection from the pool. + *key* should be used consistently with `getconn()`. .. method:: closeall From 74d2c4bef934b855726961472ba662141d2e88c9 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 11 Jan 2018 01:59:49 +0000 Subject: [PATCH 78/96] Fixed idempotence check changing connection characteristics --- NEWS | 2 ++ psycopg/connection_int.c | 31 ++++++++++++++++++++++--------- psycopg/connection_type.c | 27 ++++++++++++++------------- tests/test_connection.py | 10 ++++++++++ 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/NEWS b/NEWS index 3bc13053..0f108627 100644 --- a/NEWS +++ b/NEWS @@ -19,6 +19,8 @@ What's new in psycopg 2.7.4 - Fixed Solaris 10 support (:ticket:`#532`). - `cursor.mogrify()` can be called on closed cursors (:ticket:`#579`). +- Fixed setting session characteristics in corner cases on autocommit + connections (:ticket:`#580`). - Fixed `~psycopg2.extras.MinTimeLoggingCursor` on Python 3 (:ticket:`#609`). - Fixed parsing of array of points as floats (:ticket:`#613`). - Fixed `~psycopg2.__libpq_version__` building with libpq >= 10.1 diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c index e8081b9e..3ea5ca32 100644 --- a/psycopg/connection_int.c +++ b/psycopg/connection_int.c @@ -67,6 +67,9 @@ const char *srv_state_guc[] = { }; +const int SRV_STATE_UNCHANGED = -1; + + /* Return a new "string" from a char* from the database. * * On Py2 just get a string, on Py3 decode it in the connection codec. @@ -1188,6 +1191,8 @@ conn_set_session(connectionObject *self, int autocommit, int rv = -1; PGresult *pgres = NULL; char *error = NULL; + int want_autocommit = autocommit == SRV_STATE_UNCHANGED ? + self->autocommit : autocommit; if (deferrable != self->deferrable && self->server_version < 90100) { PyErr_SetString(ProgrammingError, @@ -1209,24 +1214,24 @@ conn_set_session(connectionObject *self, int autocommit, Py_BEGIN_ALLOW_THREADS; pthread_mutex_lock(&self->lock); - if (autocommit) { - /* we are in autocommit state, so no BEGIN will be issued: + if (want_autocommit) { + /* we are or are going in autocommit state, so no BEGIN will be issued: * configure the session with the characteristics requested */ - if (isolevel != self->isolevel) { + if (isolevel != SRV_STATE_UNCHANGED) { if (0 > pq_set_guc_locked(self, "default_transaction_isolation", srv_isolevels[isolevel], &pgres, &error, &_save)) { goto endlock; } } - if (readonly != self->readonly) { + if (readonly != SRV_STATE_UNCHANGED) { if (0 > pq_set_guc_locked(self, "default_transaction_read_only", srv_state_guc[readonly], &pgres, &error, &_save)) { goto endlock; } } - if (deferrable != self->deferrable) { + if (deferrable != SRV_STATE_UNCHANGED) { if (0 > pq_set_guc_locked(self, "default_transaction_deferrable", srv_state_guc[deferrable], &pgres, &error, &_save)) { @@ -1260,10 +1265,18 @@ conn_set_session(connectionObject *self, int autocommit, } } - self->autocommit = autocommit; - self->isolevel = isolevel; - self->readonly = readonly; - self->deferrable = deferrable; + if (autocommit != SRV_STATE_UNCHANGED) { + self->autocommit = autocommit; + } + if (isolevel != SRV_STATE_UNCHANGED) { + self->isolevel = isolevel; + } + if (readonly != SRV_STATE_UNCHANGED) { + self->readonly = readonly; + } + if (deferrable != SRV_STATE_UNCHANGED) { + self->deferrable = deferrable; + } rv = 0; endlock: diff --git a/psycopg/connection_type.c b/psycopg/connection_type.c index 8c5085b5..6a66d48d 100644 --- a/psycopg/connection_type.c +++ b/psycopg/connection_type.c @@ -39,6 +39,7 @@ extern HIDDEN const char *srv_isolevels[]; extern HIDDEN const char *srv_readonly[]; extern HIDDEN const char *srv_deferrable[]; +extern HIDDEN const int SRV_STATE_UNCHANGED; /** DBAPI methods **/ @@ -561,10 +562,10 @@ psyco_conn_set_session(connectionObject *self, PyObject *args, PyObject *kwargs) PyObject *deferrable = Py_None; PyObject *autocommit = Py_None; - int c_isolevel = self->isolevel; - int c_readonly = self->readonly; - int c_deferrable = self->deferrable; - int c_autocommit = self->autocommit; + int c_isolevel = SRV_STATE_UNCHANGED; + int c_readonly = SRV_STATE_UNCHANGED; + int c_deferrable = SRV_STATE_UNCHANGED; + int c_autocommit = SRV_STATE_UNCHANGED; static char *kwlist[] = {"isolation_level", "readonly", "deferrable", "autocommit", NULL}; @@ -637,7 +638,7 @@ psyco_conn_autocommit_set(connectionObject *self, PyObject *pyvalue) if (!_psyco_set_session_check_setter_wrapper(self)) { return -1; } if (-1 == (value = PyObject_IsTrue(pyvalue))) { return -1; } if (0 > conn_set_session(self, value, - self->isolevel, self->readonly, self->deferrable)) { + SRV_STATE_UNCHANGED, SRV_STATE_UNCHANGED, SRV_STATE_UNCHANGED)) { return -1; } @@ -668,8 +669,8 @@ psyco_conn_isolation_level_set(connectionObject *self, PyObject *pyvalue) if (!_psyco_set_session_check_setter_wrapper(self)) { return -1; } if (0 > (value = _psyco_conn_parse_isolevel(pyvalue))) { return -1; } - if (0 > conn_set_session(self, self->autocommit, - value, self->readonly, self->deferrable)) { + if (0 > conn_set_session(self, SRV_STATE_UNCHANGED, + value, SRV_STATE_UNCHANGED, SRV_STATE_UNCHANGED)) { return -1; } @@ -715,13 +716,13 @@ psyco_conn_set_isolation_level(connectionObject *self, PyObject *args) if (level == 0) { if (0 > conn_set_session(self, 1, - self->isolevel, self->readonly, self->deferrable)) { + SRV_STATE_UNCHANGED, SRV_STATE_UNCHANGED, SRV_STATE_UNCHANGED)) { return NULL; } } else { if (0 > conn_set_session(self, 0, - level, self->readonly, self->deferrable)) { + level, SRV_STATE_UNCHANGED, SRV_STATE_UNCHANGED)) { return NULL; } } @@ -767,8 +768,8 @@ psyco_conn_readonly_set(connectionObject *self, PyObject *pyvalue) if (!_psyco_set_session_check_setter_wrapper(self)) { return -1; } if (0 > (value = _psyco_conn_parse_onoff(pyvalue))) { return -1; } - if (0 > conn_set_session(self, self->autocommit, - self->isolevel, value, self->deferrable)) { + if (0 > conn_set_session(self, SRV_STATE_UNCHANGED, + SRV_STATE_UNCHANGED, value, SRV_STATE_UNCHANGED)) { return -1; } @@ -813,8 +814,8 @@ psyco_conn_deferrable_set(connectionObject *self, PyObject *pyvalue) if (!_psyco_set_session_check_setter_wrapper(self)) { return -1; } if (0 > (value = _psyco_conn_parse_onoff(pyvalue))) { return -1; } - if (0 > conn_set_session(self, self->autocommit, - self->isolevel, self->readonly, value)) { + if (0 > conn_set_session(self, SRV_STATE_UNCHANGED, + SRV_STATE_UNCHANGED, SRV_STATE_UNCHANGED, value)) { return -1; } diff --git a/tests/test_connection.py b/tests/test_connection.py index 02db2645..fa9bfc01 100755 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -1381,6 +1381,16 @@ class TransactionControlTests(ConnectingTestCase): cur.execute("SHOW default_transaction_read_only;") self.assertEqual(cur.fetchone()[0], 'off') + def test_idempotence_check(self): + self.conn.autocommit = False + self.conn.readonly = True + self.conn.autocommit = True + self.conn.readonly = True + + cur = self.conn.cursor() + cur.execute("SHOW transaction_read_only") + self.assertEqual(cur.fetchone()[0], 'on') + class AutocommitTests(ConnectingTestCase): def test_closed(self): From 8decf34ad7bc535b063184aec5ba530fd1bd34f0 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Thu, 25 Jan 2018 21:58:05 +0000 Subject: [PATCH 79/96] Dropped warning about unsafe cursor names It was long made secure --- doc/src/connection.rst | 5 ----- 1 file changed, 5 deletions(-) diff --git a/doc/src/connection.rst b/doc/src/connection.rst index 6f13ecd3..2910f301 100644 --- a/doc/src/connection.rst +++ b/doc/src/connection.rst @@ -41,11 +41,6 @@ The ``connection`` class previously only valid PostgreSQL identifiers were accepted as cursor name. - .. warning:: - It is unsafe to expose the *name* to an untrusted source, for - instance you shouldn't allow *name* to be read from a HTML form. - Consider it as part of the query, not as a query parameter. - The *cursor_factory* argument can be used to create non-standard cursors. The class returned must be a subclass of `psycopg2.extensions.cursor`. See :ref:`subclassing-cursor` for From aa2c1727066cc31d1b4431bc2e80abb2edcfb381 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Sat, 13 Jan 2018 20:11:18 +0000 Subject: [PATCH 80/96] Print info about the binary package on build failed The idea is to release a package 'psycopg2-binary' to allow installing binary, and leave the psycopg2 package to be source only, to avoid pushing the unreliability of the wheel pacakge by default (see issue #543). Version number bumped to test with new packages. --- setup.py | 46 ++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/setup.py b/setup.py index 0e214629..ec2b5ae0 100644 --- a/setup.py +++ b/setup.py @@ -39,6 +39,7 @@ except ImportError: from distutils.command.build_ext import build_ext from distutils.sysconfig import get_python_inc from distutils.ccompiler import get_default_compiler +from distutils.errors import CompileError from distutils.util import get_platform try: @@ -104,15 +105,23 @@ class PostgresConfig: if not self.pg_config_exe: self.pg_config_exe = self.autodetect_pg_config_path() if self.pg_config_exe is None: - sys.stderr.write("""\ + sys.stderr.write(""" Error: pg_config executable not found. -Please add the directory containing pg_config to the PATH -or specify the full executable path with the option: +pg_config is required to build psycopg2 from source. Please add the directory +containing pg_config to the $PATH or specify the full executable path with the +option: python setup.py build_ext --pg-config /path/to/pg_config build ... or with the pg_config option in 'setup.cfg'. + +If you prefer to avoid building psycopg2 from source, please install the PyPI +'psycopg2-binary' package instead. + +For further information please check the 'doc/src/install.rst' file (also at +). + """) sys.exit(1) @@ -286,8 +295,37 @@ class psycopg_build_ext(build_ext): else: return build_ext.get_export_symbols(self, extension) + built_files = 0 + def build_extension(self, extension): - build_ext.build_extension(self, extension) + # Count files compiled to print the binary blurb only if the first fails + compile_orig = getattr(self.compiler, '_compile', None) + if compile_orig is not None: + def _compile(*args, **kwargs): + rv = compile_orig(*args, **kwargs) + psycopg_build_ext.built_files += 1 + return rv + + self.compiler._compile = _compile + + try: + build_ext.build_extension(self, extension) + psycopg_build_ext.built_files += 1 + except CompileError: + if self.built_files == 0: + sys.stderr.write(""" +It appears you are missing some prerequisite to build the package from source. + +You may install a binary package by installing 'psycopg2-binary' from PyPI. +If you want to install psycopg2 from source, please install the packages +required for the build and try again. + +For further information please check the 'doc/src/install.rst' file (also at +). + +""") + raise + sysVer = sys.version_info[:2] # For Python versions that use MSVC compiler 2008, re-insert the From 5b69adf797ecb7241d2536140ac173a2404905a6 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 16 Jan 2018 18:28:27 +0000 Subject: [PATCH 81/96] Document the psycopg2-binary package --- README.rst | 32 ++++--- doc/src/install.rst | 198 ++++++++++++++++++++++---------------------- 2 files changed, 121 insertions(+), 109 deletions(-) diff --git a/README.rst b/README.rst index 69b1d83d..9b88239a 100644 --- a/README.rst +++ b/README.rst @@ -25,29 +25,39 @@ Documentation is included in the ``doc`` directory and is `available online`__. .. __: http://initd.org/psycopg/docs/ +For any other resource (source code repository, bug tracker, mailing list) +please check the `project homepage`__. + Installation ------------ -If your ``pip`` version supports wheel_ packages it should be possible to -install a binary version of Psycopg including all the dependencies from PyPI_. -Just run:: +Building Psycopg requires a few prerequisites (a C compiler, some development +packages): please check the install_ and the faq_ documents in the ``doc`` dir +or online for the details. + +If prerequisites are met, you can install psycopg like any other Python +package, using ``pip`` to download it from PyPI_:: - $ pip install -U pip # make sure your pip is up-to-date $ pip install psycopg2 -If you want to build Psycopg from source you will need some prerequisites (a C -compiler, development packages): please check the install_ and the faq_ -documents in the ``doc`` dir for the details. +or using ``setup.py`` if you have downloaded the source package locally:: + + $ python setup.py build + $ sudo python setup.py install + +You can also obtain a stand-alone package, not requiring a compiler or +external libraries, by installing the ``psycopg2-binary`` package from PyPI:: + + $ pip install psycopg2-binary + +The binary package is a practical choice for development and testing but in +production it is advised to use the package built from sources. -.. _wheel: http://pythonwheels.com/ .. _PyPI: https://pypi.python.org/pypi/psycopg2 .. _install: http://initd.org/psycopg/docs/install.html#install-from-source .. _faq: http://initd.org/psycopg/docs/faq.html#faq-compile -For any other resource (source code repository, bug tracker, mailing list) -please check the `project homepage`__. - .. __: http://initd.org/psycopg/ diff --git a/doc/src/install.rst b/doc/src/install.rst index 6d30e343..3fcea867 100644 --- a/doc/src/install.rst +++ b/doc/src/install.rst @@ -12,16 +12,6 @@ to use Psycopg on a different Python implementation (PyPy, Jython, IronPython) there is an experimental `porting of Psycopg for Ctypes`__, but it is not as mature as the C implementation yet. -The current `!psycopg2` implementation supports: - -.. - NOTE: keep consistent with setup.py and the /features/ page. - -- Python version 2.7 -- Python 3 versions from 3.4 to 3.6 -- PostgreSQL server versions from 7.4 to 10 -- PostgreSQL client library version from 9.1 - .. _PostgreSQL: http://www.postgresql.org/ .. _Python: http://www.python.org/ .. _libpq: http://www.postgresql.org/docs/current/static/libpq.html @@ -32,94 +22,20 @@ The current `!psycopg2` implementation supports: .. index:: - single: Install; from PyPI - single: Install; wheel - single: Wheel + single: Prerequisites -Binary install from PyPI ------------------------- +Prerequisites +------------- -`!psycopg2` is `available on PyPI`__ in the form of wheel_ packages for the -most common platform (Linux, OSX, Windows): this should make you able to -install a binary version of the module including all the dependencies simply -using: +The current `!psycopg2` implementation supports: -.. code-block:: console +.. + NOTE: keep consistent with setup.py and the /features/ page. - $ pip install psycopg2 - -Make sure to use an up-to-date version of :program:`pip` (you can upgrade it -using something like ``pip install -U pip``) - -.. __: PyPI_ -.. _PyPI: https://pypi.python.org/pypi/psycopg2/ -.. _wheel: http://pythonwheels.com/ - -.. note:: - - The binary packages come with their own versions of a few C libraries, - among which ``libpq`` and ``libssl``, which will be used regardless of other - libraries available on the client: upgrading the system libraries will not - upgrade the libraries used by `!psycopg2`. Please build `!psycopg2` from - source if you want to maintain binary upgradeability. - -.. warning:: - - Because the `!psycopg` wheel package uses its own ``libssl`` binary, it is - incompatible with other extension modules binding with ``libssl`` as well, - for instance the Python `ssl` module: the result will likely be a - segfault. If you need using both `!psycopg2` and other libraries using - ``libssl`` please :ref:`disable the use of wheel packages for Psycopg - `. - - - -.. index:: - single: Install; disable wheel - single: Wheel; disable - -.. _disable-wheel: - -Disabling wheel packages -^^^^^^^^^^^^^^^^^^^^^^^^ - -If you want to disable the use of wheel binary packages and use the system -system libraries available on your client you can use the :command:`pip` -|--no-binary option|__: - -.. code-block:: console - - $ pip install --no-binary psycopg2 - -.. |--no-binary option| replace:: ``--no-binary`` option -.. __: https://pip.pypa.io/en/stable/reference/pip_install/#install-no-binary - -which can be specified in your :file:`requirements.txt` files too, e.g. use: - -.. code-block:: none - - psycopg2>=2.7,<2.8 --no-binary psycopg2 - -to use the last bugfix release of the `!psycopg2` 2.7 package, specifying to -always compile it from source. Of course in this case you will have to meet -the :ref:`build prerequisites `. - - - -.. index:: - single: Install; from source - -.. _install-from-source: - -Install from source -------------------- - -.. _source-package: - -You can download a copy of Psycopg source files from the `Psycopg download -page`__ or from PyPI_. - -.. __: http://initd.org/psycopg/download/ +- Python version 2.7 +- Python 3 versions from 3.4 to 3.6 +- PostgreSQL server versions from 7.4 to 10 +- PostgreSQL client library version from 9.1 @@ -128,8 +44,8 @@ page`__ or from PyPI_. Build prerequisites ^^^^^^^^^^^^^^^^^^^ -These notes illustrate how to compile Psycopg on Linux. If you want to compile -Psycopg on other platforms you may have to adjust some details accordingly. +The build prerequisites are to be met in order to install Psycopg from source +code, either from a source distribution package or from PyPI. Psycopg is a C wrapper around the libpq_ PostgreSQL client library. To install it from sources you will need: @@ -161,6 +77,12 @@ it from sources you will need: Once everything is in place it's just a matter of running the standard: +.. code-block:: console + + $ pip install psycopg2 + +or, from the directory containing the source code: + .. code-block:: console $ python setup.py build @@ -197,12 +119,92 @@ which is OS-dependent (for instance setting a suitable +.. index:: + single: Install; from PyPI + single: Install; wheel + single: Wheel + +Binary install from PyPI +------------------------ + +`!psycopg2` is also `available on PyPI`__ in the form of wheel_ packages for +the most common platform (Linux, OSX, Windows): this should make you able to +install a binary version of the module, not requiring the above build or +runtime prerequisites, simply using: + +.. code-block:: console + + $ pip install psycopg2-binary + +Make sure to use an up-to-date version of :program:`pip` (you can upgrade it +using something like ``pip install -U pip``) + +.. __: PyPI-binary_ +.. _PyPI-binary: https://pypi.python.org/pypi/psycopg2-binary/ +.. _wheel: http://pythonwheels.com/ + +.. note:: + + The binary packages come with their own versions of a few C libraries, + among which ``libpq`` and ``libssl``, which will be used regardless of other + libraries available on the client: upgrading the system libraries will not + upgrade the libraries used by `!psycopg2`. Please build `!psycopg2` from + source if you want to maintain binary upgradeability. + +.. warning:: + + The `!psycopg2` wheel package comes packaged, among the others, with its + own ``libssl`` binary. This may create conflicts with other extension + modules binding with ``libssl`` as well, for instance with the Python + `ssl` module: in some cases, under concurrency, the interaction between + the two libraries may result in a segfault. In case of doubts you are + advised to use a package built from source. + + + +.. index:: + single: Install; disable wheel + single: Wheel; disable + +.. _disable-wheel: + +Disabling wheel packages for Psycopg 2.7 +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +In version 2.7.x, `pip install psycopg2` would have tried to install the wheel +binary package of Psycopg. Because of the problems the wheel package have +displayed, `psycopg2-binary` has become a separate package, and from 2.8 it +has become the only way to install the binary package. + +If you are using psycopg 2.7 and you want to disable the use of wheel binary +packages, relying on the system system libraries available on your client, you +can use the :command:`pip` |--no-binary option|__: + +.. code-block:: console + + $ pip install --no-binary psycopg2 + +.. |--no-binary option| replace:: ``--no-binary`` option +.. __: https://pip.pypa.io/en/stable/reference/pip_install/#install-no-binary + +which can be specified in your :file:`requirements.txt` files too, e.g. use: + +.. code-block:: none + + psycopg2>=2.7,<2.8 --no-binary psycopg2 + +to use the last bugfix release of the `!psycopg2` 2.7 package, specifying to +always compile it from source. Of course in this case you will have to meet +the :ref:`build prerequisites `. + + + .. index:: single: setup.py single: setup.cfg Non-standard builds -^^^^^^^^^^^^^^^^^^^ +------------------- If you have less standard requirements such as: @@ -242,7 +244,7 @@ order to create a debug package: - Edit the ``setup.cfg`` file adding the ``PSYCOPG_DEBUG`` flag to the ``define`` option. -- :ref:`Compile and install ` the package. +- :ref:`Compile and install ` the package. - Set the :envvar:`PSYCOPG_DEBUG` environment variable: From 76f3e196d31a3e2006b9c0b18c400fb9f89b9117 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 29 Jan 2018 00:45:40 +0000 Subject: [PATCH 82/96] Silence warning on import failing a test --- tests/test_connection.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/test_connection.py b/tests/test_connection.py index fa9bfc01..a82bd499 100755 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -1549,9 +1549,13 @@ import os import sys import time import signal +import warnings import threading -import psycopg2 +# ignore wheel deprecation warning +with warnings.catch_warnings(): + warnings.simplefilter('ignore') + import psycopg2 def handle_sigabort(sig, frame): sys.exit(1) From f976c428d22a1b53cb523e5f01f6f9117834c87e Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 29 Jan 2018 01:33:37 +0000 Subject: [PATCH 83/96] Mention new wheel packages in news file Close #543. --- NEWS | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 0f108627..5095dc66 100644 --- a/NEWS +++ b/NEWS @@ -17,6 +17,9 @@ Other changes: What's new in psycopg 2.7.4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ +- Moving away from installing the wheel package by default. + Packages installed from wheel raise a warning on import. Added package + ``psycopg2-binary`` to install from wheel instead (:ticket:`#543`). - Fixed Solaris 10 support (:ticket:`#532`). - `cursor.mogrify()` can be called on closed cursors (:ticket:`#579`). - Fixed setting session characteristics in corner cases on autocommit @@ -25,8 +28,8 @@ What's new in psycopg 2.7.4 - Fixed parsing of array of points as floats (:ticket:`#613`). - Fixed `~psycopg2.__libpq_version__` building with libpq >= 10.1 (:ticket:`632`). -- Fixed `~cursor.rowcount` after `~cursor.executemany()` with :sql:`RETURNING` statements - (:ticket:`633`). +- Fixed `~cursor.rowcount` after `~cursor.executemany()` with :sql:`RETURNING` + statements (:ticket:`633`). - Fixed compatibility problem with pypy3 (:ticket:`#649`). - Wheel packages compiled against PostgreSQL 10.1 libpq and OpenSSL 1.0.2m. From 4845393c15692fa582a1110b2c97a80640ad1187 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 29 Jan 2018 01:56:10 +0000 Subject: [PATCH 84/96] Build env vars on windows less verbose Copied from psycopg2-wheels --- .appveyor.yml | 62 ++++++++++++++++++++------------------------------- 1 file changed, 24 insertions(+), 38 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index a49c802f..754d7656 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -13,43 +13,14 @@ environment: matrix: # For Python versions available on Appveyor, see # http://www.appveyor.com/docs/installed-software#python - - # Py 2.7 = VS Ver. 9.0 (VS 2008) - # Py 3.4 = VS Ver. 10.0 (VS 2010) - # Py 3.5, 3.6 = VS Ver. 14.0 (VS 2015) - - - PYTHON: C:\Python27-x64 - PYTHON_ARCH: 64 - VS_VER: 9.0 - - - PYTHON: C:\Python27 - PYTHON_ARCH: 32 - VS_VER: 9.0 - - - PYTHON: C:\Python36-x64 - PYTHON_ARCH: 64 - VS_VER: 14.0 - - - PYTHON: C:\Python36 - PYTHON_ARCH: 32 - VS_VER: 14.0 - - - PYTHON: C:\Python35-x64 - PYTHON_ARCH: 64 - VS_VER: 14.0 - - - PYTHON: C:\Python35 - PYTHON_ARCH: 32 - VS_VER: 14.0 - - - PYTHON: C:\Python34-x64 - DISTUTILS_USE_SDK: '1' - PYTHON_ARCH: 64 - VS_VER: 10.0 - - - PYTHON: C:\Python34 - PYTHON_ARCH: 32 - VS_VER: 10.0 + - {PYVER: "27", PYTHON_ARCH: "32"} + - {PYVER: "27", PYTHON_ARCH: "64"} + - {PYVER: "34", PYTHON_ARCH: "32"} + - {PYVER: "34", PYTHON_ARCH: "64"} + - {PYVER: "35", PYTHON_ARCH: "32"} + - {PYVER: "35", PYTHON_ARCH: "64"} + - {PYVER: "36", PYTHON_ARCH: "32"} + - {PYVER: "36", PYTHON_ARCH: "64"} PSYCOPG2_TESTDB: psycopg2_test PSYCOPG2_TESTDB_USER: postgres @@ -77,7 +48,22 @@ cache: init: # Uncomment next line to get RDP access during the build. #- ps: iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1')) - # + + # Set env variable according to the build environment + - SET PYTHON=C:\Python%PYVER% + - IF "%PYTHON_ARCH%"=="64" SET PYTHON=%PYTHON%-x64 + + # Py 2.7 = VS Ver. 9.0 (VS 2008) + # Py 3.3, 3.4 = VS Ver. 10.0 (VS 2010) + # Py 3.5, 3.6 = VS Ver. 14.0 (VS 2015) + - IF "%PYVER%"=="27" SET VS_VER=9.0 + - IF "%PYVER%"=="33" SET VS_VER=10.0 + - IF "%PYVER%"=="34" SET VS_VER=10.0 + - IF "%PYVER%"=="35" SET VS_VER=14.0 + - IF "%PYVER%"=="36" SET VS_VER=14.0 + + - IF "%VS_VER%"=="10.0" IF "%PYTHON_ARCH%"=="64" SET DISTUTILS_USE_SDK=1 + # Set Python to the path - SET PATH=%PYTHON%;%PYTHON%\Scripts;C:\Program Files\Git\mingw64\bin;%PATH% From 8cc0d06e65ae9581b6317a18cf695eb853589c14 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 29 Jan 2018 01:57:17 +0000 Subject: [PATCH 85/96] Define openssl and libpq versions in vars in appveyor build --- .appveyor.yml | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 754d7656..3efac773 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -22,6 +22,9 @@ environment: - {PYVER: "36", PYTHON_ARCH: "32"} - {PYVER: "36", PYTHON_ARCH: "64"} + OPENSSL_VERSION: "1_0_2m" + POSTGRES_VERSION: "10_1" + PSYCOPG2_TESTDB: psycopg2_test PSYCOPG2_TESTDB_USER: postgres PSYCOPG2_TESTDB_PASSWORD: Password12! @@ -139,8 +142,8 @@ install: } # Download OpenSSL source - CD C:\Others - - IF NOT EXIST OpenSSL_1_0_2m.zip ( - curl -fsSL -o OpenSSL_1_0_2m.zip https://github.com/openssl/openssl/archive/OpenSSL_1_0_2m.zip + - IF NOT EXIST OpenSSL_%OPENSSL_VERSION%.zip ( + curl -fsSL -o OpenSSL_%OPENSSL_VERSION%.zip https://github.com/openssl/openssl/archive/OpenSSL_%OPENSSL_VERSION%.zip ) # To use OpenSSL >= 1.1.0, both libpq and psycopg build environments have @@ -152,15 +155,15 @@ install: # - nmake build_libs install_dev - IF NOT EXIST %OPENSSLTOP%\lib\ssleay32.lib ( CD %BUILD_DIR% && - 7z x C:\Others\OpenSSL_1_0_2m.zip && - CD openssl-OpenSSL_1_0_2m && + 7z x C:\Others\OpenSSL_%OPENSSL_VERSION%.zip && + CD openssl-OpenSSL_%OPENSSL_VERSION% && perl Configure %TARGET% no-asm no-shared no-zlib --prefix=%OPENSSLTOP% --openssldir=%OPENSSLTOP% && CALL ms\%DO% && nmake -f ms\nt.mak init headers lib && COPY inc32\openssl\*.h %OPENSSLTOP%\include\openssl && COPY out32\*.lib %OPENSSLTOP%\lib && CD %BASE_DIR% && - RMDIR /S /Q %BUILD_DIR%\openssl-OpenSSL_1_0_2m + RMDIR /S /Q %BUILD_DIR%\openssl-OpenSSL_%OPENSSL_VERSION% ) # Setup directories for building PostgreSQL librarires @@ -174,8 +177,8 @@ install: # Download PostgreSQL source - CD C:\Others - - IF NOT EXIST postgres-REL_10_1.zip ( - curl -fsSL -o postgres-REL_10_1.zip https://github.com/postgres/postgres/archive/REL_10_1.zip + - IF NOT EXIST postgres-REL_%POSTGRES_VERSION%.zip ( + curl -fsSL -o postgres-REL_%POSTGRES_VERSION%.zip https://github.com/postgres/postgres/archive/REL_%POSTGRES_VERSION%.zip ) # Setup build config file (config.pl) @@ -186,11 +189,11 @@ install: # Prepare local include directory for building from # Build pg_config in place # NOTE: Cannot set and use the same variable inside an IF - - SET PGBUILD=%BUILD_DIR%\postgres-REL_10_1 + - SET PGBUILD=%BUILD_DIR%\postgres-REL_%POSTGRES_VERSION% - IF NOT EXIST %PGTOP%\lib\libpq.lib ( CD %BUILD_DIR% && - 7z x C:\Others\postgres-REL_10_1.zip && - CD postgres-REL_10_1\src\tools\msvc && + 7z x C:\Others\postgres-REL_%POSTGRES_VERSION%.zip && + CD postgres-REL_%POSTGRES_VERSION%\src\tools\msvc && ECHO $config-^>{ldap} = 0; > config.pl && ECHO $config-^>{openssl} = "%OPENSSLTOP:\=\\%"; >> config.pl && ECHO.>> config.pl && From 957fd79a272d718be8f23824e04409aa1fb4ee1e Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 29 Jan 2018 01:58:37 +0000 Subject: [PATCH 86/96] Build using OpenSSL 1.0.2n --- .appveyor.yml | 2 +- NEWS | 2 +- scripts/appveyor.cache_rebuild | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/.appveyor.yml b/.appveyor.yml index 3efac773..1761e99f 100644 --- a/.appveyor.yml +++ b/.appveyor.yml @@ -22,7 +22,7 @@ environment: - {PYVER: "36", PYTHON_ARCH: "32"} - {PYVER: "36", PYTHON_ARCH: "64"} - OPENSSL_VERSION: "1_0_2m" + OPENSSL_VERSION: "1_0_2n" POSTGRES_VERSION: "10_1" PSYCOPG2_TESTDB: psycopg2_test diff --git a/NEWS b/NEWS index 5095dc66..c4d65011 100644 --- a/NEWS +++ b/NEWS @@ -31,7 +31,7 @@ What's new in psycopg 2.7.4 - Fixed `~cursor.rowcount` after `~cursor.executemany()` with :sql:`RETURNING` statements (:ticket:`633`). - Fixed compatibility problem with pypy3 (:ticket:`#649`). -- Wheel packages compiled against PostgreSQL 10.1 libpq and OpenSSL 1.0.2m. +- Wheel packages compiled against PostgreSQL 10.1 libpq and OpenSSL 1.0.2n. What's new in psycopg 2.7.3.2 diff --git a/scripts/appveyor.cache_rebuild b/scripts/appveyor.cache_rebuild index da1b2be5..04825149 100644 --- a/scripts/appveyor.cache_rebuild +++ b/scripts/appveyor.cache_rebuild @@ -9,7 +9,7 @@ To invalidate the cache, update this file and check it into git. Currently used modules built in the cache: OpenSSL - Version: 1.0.2m + Version: 1.0.2n PostgreSQL Version: 10.1 From ddb87b7727793ef1d49f3915ad4b2399aeb6ef78 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 29 Jan 2018 02:41:44 +0000 Subject: [PATCH 87/96] Convert fields names into valid Python identifiers in NamedTupleCursor Close #211. --- NEWS | 2 ++ lib/extras.py | 12 +++++++++++- tests/test_extras_dictcursor.py | 8 ++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index c4d65011..efdb4e52 100644 --- a/NEWS +++ b/NEWS @@ -20,6 +20,8 @@ What's new in psycopg 2.7.4 - Moving away from installing the wheel package by default. Packages installed from wheel raise a warning on import. Added package ``psycopg2-binary`` to install from wheel instead (:ticket:`#543`). +- Convert fields names into valid Python identifiers in + `~psycopg2.extras.NamedTupleCursor` (:ticket:`#211`). - Fixed Solaris 10 support (:ticket:`#532`). - `cursor.mogrify()` can be called on closed cursors (:ticket:`#579`). - Fixed setting session characteristics in corner cases on autocommit diff --git a/lib/extras.py b/lib/extras.py index 68df344c..1f85d532 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -363,7 +363,17 @@ class NamedTupleCursor(_cursor): return def _make_nt(self): - return namedtuple("Record", [d[0] for d in self.description or ()]) + def f(s): + # NOTE: Python 3 actually allows unicode chars in fields + s = _re.sub('[^a-zA-Z0-9_]', '_', s) + # Python identifier cannot start with numbers, namedtuple fields + # cannot start with underscore. So... + if _re.match('^[0-9_]', s): + s = 'f' + s + + return s + + return namedtuple("Record", [f(d[0]) for d in self.description or ()]) class LoggingConnection(_connection): diff --git a/tests/test_extras_dictcursor.py b/tests/test_extras_dictcursor.py index 75c22773..99bdeee6 100755 --- a/tests/test_extras_dictcursor.py +++ b/tests/test_extras_dictcursor.py @@ -349,6 +349,14 @@ class NamedTupleCursorTest(ConnectingTestCase): curs.execute("update nttest set s = s") self.assertRaises(psycopg2.ProgrammingError, curs.fetchall) + def test_bad_col_names(self): + curs = self.conn.cursor() + curs.execute('select 1 as "foo.bar_baz", 2 as "?column?", 3 as "3"') + rv = curs.fetchone() + self.assertEqual(rv.foo_bar_baz, 1) + self.assertEqual(rv.f_column_, 2) + self.assertEqual(rv.f3, 3) + def test_minimal_generation(self): # Instrument the class to verify it gets called the minimum number of times. from psycopg2.extras import NamedTupleCursor From 1436ee308cef6650e522f6bb2a30f7fe1c6f96d8 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 29 Jan 2018 03:09:27 +0000 Subject: [PATCH 88/96] Link psycopg2-binary package to PyPI in readme Note: the package doesn't exist yet... --- README.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 9b88239a..b5b048d4 100644 --- a/README.rst +++ b/README.rst @@ -47,7 +47,7 @@ or using ``setup.py`` if you have downloaded the source package locally:: $ sudo python setup.py install You can also obtain a stand-alone package, not requiring a compiler or -external libraries, by installing the ``psycopg2-binary`` package from PyPI:: +external libraries, by installing the `psycopg2-binary`_ package from PyPI:: $ pip install psycopg2-binary @@ -55,6 +55,7 @@ The binary package is a practical choice for development and testing but in production it is advised to use the package built from sources. .. _PyPI: https://pypi.python.org/pypi/psycopg2 +.. _psycopg2-binary: https://pypi.python.org/pypi/psycopg2-binary .. _install: http://initd.org/psycopg/docs/install.html#install-from-source .. _faq: http://initd.org/psycopg/docs/faq.html#faq-compile From c52e49a9daa95ae069e7e804e41f6534d5849ba1 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 29 Jan 2018 12:55:41 +0000 Subject: [PATCH 89/96] Report Python 2.6 wheels no more available [skip ci] --- NEWS | 2 ++ 1 file changed, 2 insertions(+) diff --git a/NEWS b/NEWS index efdb4e52..c400f249 100644 --- a/NEWS +++ b/NEWS @@ -34,6 +34,8 @@ What's new in psycopg 2.7.4 statements (:ticket:`633`). - Fixed compatibility problem with pypy3 (:ticket:`#649`). - Wheel packages compiled against PostgreSQL 10.1 libpq and OpenSSL 1.0.2n. +- Wheel packages for Python 2.6 no more available (support dropped from + wheel building infrastructure). What's new in psycopg 2.7.3.2 From 5309da117dd790aaf9dd74b6083638123a383bfa Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 6 Feb 2018 00:40:20 +0000 Subject: [PATCH 90/96] Autocommit shouldn't change deferrable on servers not supporting it Regression on unsupported Postgres versions after fixing bug #580 --- psycopg/connection_int.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/psycopg/connection_int.c b/psycopg/connection_int.c index 3ea5ca32..a60c4a9b 100644 --- a/psycopg/connection_int.c +++ b/psycopg/connection_int.c @@ -1194,7 +1194,7 @@ conn_set_session(connectionObject *self, int autocommit, int want_autocommit = autocommit == SRV_STATE_UNCHANGED ? self->autocommit : autocommit; - if (deferrable != self->deferrable && self->server_version < 90100) { + if (deferrable != SRV_STATE_UNCHANGED && self->server_version < 90100) { PyErr_SetString(ProgrammingError, "the 'deferrable' setting is only available" " from PostgreSQL 9.1"); @@ -1256,7 +1256,7 @@ conn_set_session(connectionObject *self, int autocommit, goto endlock; } } - if (self->deferrable != STATE_DEFAULT) { + if (self->server_version >= 90100 && self->deferrable != STATE_DEFAULT) { if (0 > pq_set_guc_locked(self, "default_transaction_deferrable", "default", &pgres, &error, &_save)) { From df952c149da2a3a7574c54a8a62c791211223270 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Fri, 9 Feb 2018 16:10:03 +0000 Subject: [PATCH 91/96] Fixed pip invocation example to skip binary packages Close #673 --- doc/src/install.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/src/install.rst b/doc/src/install.rst index 3fcea867..a858cbe3 100644 --- a/doc/src/install.rst +++ b/doc/src/install.rst @@ -178,11 +178,11 @@ has become the only way to install the binary package. If you are using psycopg 2.7 and you want to disable the use of wheel binary packages, relying on the system system libraries available on your client, you -can use the :command:`pip` |--no-binary option|__: +can use the :command:`pip` |--no-binary option|__, e.g.: .. code-block:: console - $ pip install --no-binary psycopg2 + $ pip install --no-binary :all: psycopg2 .. |--no-binary option| replace:: ``--no-binary`` option .. __: https://pip.pypa.io/en/stable/reference/pip_install/#install-no-binary From f766d90704b03cbe277e3fd4b4153d892960f135 Mon Sep 17 00:00:00 2001 From: Mike Gerdts Date: Fri, 16 Feb 2018 22:34:41 +0000 Subject: [PATCH 92/96] Fixed building on SmartOS timeradd is missing on Solaris 10, but is present as a macro in on SmartOS, illumos, and likely Solaris 11. --- psycopg/solaris_support.c | 5 ++++- psycopg/solaris_support.h | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/psycopg/solaris_support.c b/psycopg/solaris_support.c index cf82e2ed..e5f8edf6 100644 --- a/psycopg/solaris_support.c +++ b/psycopg/solaris_support.c @@ -1,6 +1,7 @@ /* solaris_support.c - emulate functions missing on Solaris * * Copyright (C) 2017 My Karlsson + * Copyright (c) 2018, Joyent, Inc. * * This file is part of psycopg. * @@ -28,7 +29,8 @@ #include "psycopg/solaris_support.h" #if defined(__sun) && defined(__SVR4) -/* timeradd is missing on Solaris */ +/* timeradd is missing on Solaris 10 */ +#ifndef timeradd void timeradd(struct timeval *a, struct timeval *b, struct timeval *c) { @@ -51,4 +53,5 @@ timersub(struct timeval *a, struct timeval *b, struct timeval *c) c->tv_sec -= 1; } } +#endif /* timeradd */ #endif /* defined(__sun) && defined(__SVR4) */ diff --git a/psycopg/solaris_support.h b/psycopg/solaris_support.h index 33c2f2b8..880e9f18 100644 --- a/psycopg/solaris_support.h +++ b/psycopg/solaris_support.h @@ -1,6 +1,7 @@ /* solaris_support.h - definitions for solaris_support.c * * Copyright (C) 2017 My Karlsson + * Copyright (c) 2018, Joyent, Inc. * * This file is part of psycopg. * @@ -30,8 +31,10 @@ #if defined(__sun) && defined(__SVR4) #include +#ifndef timeradd extern HIDDEN void timeradd(struct timeval *a, struct timeval *b, struct timeval *c); extern HIDDEN void timersub(struct timeval *a, struct timeval *b, struct timeval *c); #endif +#endif #endif /* !defined(PSYCOPG_SOLARIS_SUPPORT_H) */ From 8670287928501edf0d3868cf468a6796ad841b50 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 19 Feb 2018 11:43:37 +0000 Subject: [PATCH 93/96] Added Solaris 11 fix to news file --- NEWS | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/NEWS b/NEWS index c400f249..dfee39c9 100644 --- a/NEWS +++ b/NEWS @@ -14,6 +14,13 @@ Other changes: without using 2to3. +What's new in psycopg 2.7.5 +^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +- Fixed building on Solaris 11 and derivatives such as SmartOS and illumos + (:ticket:`#677`) + + What's new in psycopg 2.7.4 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ From 32f5a9fc1d24be502cc71d10a2cd396348891710 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 19 Feb 2018 11:28:13 +0000 Subject: [PATCH 94/96] Avoid quoting the string in the psycopg version macro Use a macro trick to add the quotes. This seems more portable than passing the quotes to the command line (see #658). https://gcc.gnu.org/onlinedocs/cpp/Stringizing.html --- psycopg/psycopgmodule.c | 8 ++++++-- setup.py | 5 +---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 6c95bd69..5deaa163 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -72,6 +72,10 @@ HIDDEN PyObject *psyco_null = NULL; /* The type of the cursor.description items */ HIDDEN PyObject *psyco_DescriptionType = NULL; +/* macro trick to stringify a macro expansion */ +#define xstr(s) str(s) +#define str(s) #s + /** connect module-level function **/ #define psyco_connect_doc \ "_connect(dsn, [connection_factory], [async]) -- New database connection.\n\n" @@ -885,7 +889,7 @@ INIT_MODULE(_psycopg)(void) psycopg_debug_enabled = 1; #endif - Dprintf("initpsycopg: initializing psycopg %s", PSYCOPG_VERSION); + Dprintf("initpsycopg: initializing psycopg %s", xstr(PSYCOPG_VERSION)); /* initialize all the new types and then the module */ Py_TYPE(&connectionType) = &PyType_Type; @@ -1017,7 +1021,7 @@ INIT_MODULE(_psycopg)(void) if (!(psyco_DescriptionType = psyco_make_description_type())) { goto exit; } /* set some module's parameters */ - PyModule_AddStringConstant(module, "__version__", PSYCOPG_VERSION); + PyModule_AddStringConstant(module, "__version__", xstr(PSYCOPG_VERSION)); PyModule_AddStringConstant(module, "__doc__", "psycopg PostgreSQL driver"); PyModule_AddIntConstant(module, "__libpq_version__", PG_VERSION_NUM); PyModule_AddIntMacro(module, REPLICATION_PHYSICAL); diff --git a/setup.py b/setup.py index ec2b5ae0..2d608c86 100644 --- a/setup.py +++ b/setup.py @@ -596,10 +596,7 @@ if version_flags: else: PSYCOPG_VERSION_EX = PSYCOPG_VERSION -if not PLATFORM_IS_WINDOWS: - define_macros.append(('PSYCOPG_VERSION', '"' + PSYCOPG_VERSION_EX + '"')) -else: - define_macros.append(('PSYCOPG_VERSION', '\\"' + PSYCOPG_VERSION_EX + '\\"')) +define_macros.append(('PSYCOPG_VERSION', PSYCOPG_VERSION_EX)) if parser.has_option('build_ext', 'have_ssl'): have_ssl = int(parser.get('build_ext', 'have_ssl')) From ede418a009ea1875be37e5537c4b0c9640fc1685 Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Mon, 19 Feb 2018 13:51:45 +0000 Subject: [PATCH 95/96] Report MSYS2 build probably fixed --- NEWS | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index dfee39c9..d67ab1fa 100644 --- a/NEWS +++ b/NEWS @@ -18,7 +18,8 @@ What's new in psycopg 2.7.5 ^^^^^^^^^^^^^^^^^^^^^^^^^^^ - Fixed building on Solaris 11 and derivatives such as SmartOS and illumos - (:ticket:`#677`) + (:ticket:`#677`). +- Maybe fixed building on MSYS2 (as reported in :ticket:`#658`). What's new in psycopg 2.7.4 From ea923b63a48880d3d229241de42813a822e5db1e Mon Sep 17 00:00:00 2001 From: Daniele Varrazzo Date: Tue, 20 Feb 2018 17:33:12 +0000 Subject: [PATCH 96/96] Allow strings subclasses in ensure_bytes Fix #679 --- NEWS | 1 + psycopg/utils.c | 4 ++-- tests/test_connection.py | 14 ++++++++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index d67ab1fa..09b19e5f 100644 --- a/NEWS +++ b/NEWS @@ -20,6 +20,7 @@ What's new in psycopg 2.7.5 - Fixed building on Solaris 11 and derivatives such as SmartOS and illumos (:ticket:`#677`). - Maybe fixed building on MSYS2 (as reported in :ticket:`#658`). +- Allow string subclasses in connection and other places (:ticket:`#679`). What's new in psycopg 2.7.4 diff --git a/psycopg/utils.c b/psycopg/utils.c index a197fe22..261810fd 100644 --- a/psycopg/utils.c +++ b/psycopg/utils.c @@ -168,11 +168,11 @@ psycopg_ensure_bytes(PyObject *obj) PyObject *rv = NULL; if (!obj) { return NULL; } - if (PyUnicode_CheckExact(obj)) { + if (PyUnicode_Check(obj)) { rv = PyUnicode_AsUTF8String(obj); Py_DECREF(obj); } - else if (Bytes_CheckExact(obj)) { + else if (Bytes_Check(obj)) { rv = obj; } else { diff --git a/tests/test_connection.py b/tests/test_connection.py index a82bd499..4625e7e3 100755 --- a/tests/test_connection.py +++ b/tests/test_connection.py @@ -246,6 +246,13 @@ class ConnectionTests(ConnectingTestCase): else: del os.environ['PGCLIENTENCODING'] + def test_connect_no_string(self): + class MyString(str): + pass + + conn = psycopg2.connect(MyString(dsn)) + conn.close() + def test_weakref(self): from weakref import ref import gc @@ -400,6 +407,13 @@ class ParseDsnTestCase(ConnectingTestCase): self.assertRaises(TypeError, ext.parse_dsn, None) self.assertRaises(TypeError, ext.parse_dsn, 42) + def test_str_subclass(self): + class MyString(str): + pass + + res = ext.parse_dsn(MyString("dbname=test")) + self.assertEqual(res, {'dbname': 'test'}) + class MakeDsnTestCase(ConnectingTestCase): def test_empty_arguments(self):