Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

connect() ignores cursor_factory if connection_factory is also specified #1019

Closed
dandanlen opened this issue Dec 6, 2019 · 8 comments
Closed

Comments

@dandanlen
Copy link

Hi there,

I tried setting the connection_factory and cursor_factory args through the psycopg2.connect method, but it looks like the cursor factory isn't being used. As a result, rows are returned as tuples instead of dicts.

When you use the cursor_factory in isolation it works fine:

with psycopg2.connect(dsn,
                      cursor_factory=DictCursor) as conn:
    with conn.cursor() as cur:
        print(cur.row_factory)
        # prints: <class 'psycopg2.extras.DictRow'>

However if you also set a connection_factory (Am I using this wrong? 🤷‍♂ ):

with psycopg2.connect(dsn,
                      connection_factory=LoggingConnection,
                      cursor_factory=DictCursor) as conn:
    conn.initialize(logging.Logger('test'))
    with conn.cursor() as cur:
        print(cur.row_factory)
        # prints: None

I can work around it by specifying the cursor_factory when I create the cursor.

with psycopg2.connect(dsn, connection_factory=LoggingConnection) as conn:
    conn.initialize(logging.Logger('test'))
    with conn.cursor(cursor_factory=DictCursor) as cur:
        print(cur.row_factory)
        # prints: <class 'psycopg2.extras.DictRow'>

I'm using version 2.8.4, Python 3.7.4 on mac.

@dvarrazzo
Copy link
Member

It's a contrived use case, but a genuine bug, affecting pretty much all the provided connection subclasses.

@dandanlen
Copy link
Author

Well, it was easy to work around but I wouldn't call it a contrived use case... Anyway, thanks for the quick response!

@dvarrazzo
Copy link
Member

The fact nobody stumbled in it in 15 years the adapter is used in all the world makes it a pretty unlikely use case.

bors bot added a commit to chronhq/backend that referenced this issue Apr 6, 2020
230: Update psycopg2 requirement from ~=2.8.4 to ~=2.8.5 in /config r=whirish a=dependabot-preview[bot]

Updates the requirements on [psycopg2](https://github.com/psycopg/psycopg2) to permit the latest version.
<details>
<summary>Changelog</summary>
<p><em>Sourced from <a href="https://github.com/psycopg/psycopg2/blob/master/NEWS">psycopg2's changelog</a>.</em></p>
<blockquote>
<h2>Current release</h2>
<p>What's new in psycopg 2.8.5
^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>Fixed use of <code>!connection_factory</code> and <code>!cursor_factory</code> together
(:ticket:<code>[#1019](https://github.com/psycopg/psycopg2/issues/1019)</code>).</li>
<li>Added support for <code>~logging.LoggerAdapter</code> in
<code>~psycopg2.extras.LoggingConnection</code> (:ticket:<code>[#1026](https://github.com/psycopg/psycopg2/issues/1026)</code>).</li>
<li><code>~psycopg2.extensions.Column</code> objects in <code>cursor.description</code> can be sliced
(:ticket:<code>[#1034](https://github.com/psycopg/psycopg2/issues/1034)</code>).</li>
<li>Added AIX support (:ticket:<code>[#1061](https://github.com/psycopg/psycopg2/issues/1061)</code>).</li>
<li>Fixed <code>~copy.copy()</code> of <code>~psycopg2.extras.DictCursor</code> rows (:ticket:<code>[#1073](https://github.com/psycopg/psycopg2/issues/1073)</code>).</li>
</ul>
<p>What's new in psycopg 2.8.4
^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>Fixed building with Python 3.8 (:ticket:<code>[#854](https://github.com/psycopg/psycopg2/issues/854)</code>).</li>
<li>Don't swallow keyboard interrupts on connect when a password is specified
in the connection string (:ticket:<code>[#898](https://github.com/psycopg/psycopg2/issues/898)</code>).</li>
<li>Don't advance replication cursor when the message wasn't confirmed
(:ticket:<code>[#940](https://github.com/psycopg/psycopg2/issues/940)</code>).</li>
<li>Fixed inclusion of <code>time.h</code> on linux (:ticket:<code>[#951](https://github.com/psycopg/psycopg2/issues/951)</code>).</li>
<li>Fixed int overflow for large values in <code>~psycopg2.extensions.Column.table_oid</code>
and <code>~psycopg2.extensions.Column.type_code</code> (:ticket:<code>[#961](https://github.com/psycopg/psycopg2/issues/961)</code>).</li>
<li><code>~psycopg2.errorcodes</code> map and <code>~psycopg2.errors</code> classes updated to
PostgreSQL 12.</li>
<li>Wheel package compiled against OpenSSL 1.1.1d and PostgreSQL at least 11.4.</li>
</ul>
<p>What's new in psycopg 2.8.3
^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>Added <em>interval_status</em> parameter to
<code>~psycopg2.extras.ReplicationCursor.start_replication()</code> method and other
facilities to send automatic replication keepalives at periodic intervals
(:ticket:<code>[#913](https://github.com/psycopg/psycopg2/issues/913)</code>).</li>
<li>Fixed namedtuples caching introduced in 2.8 (:ticket:<code>[#928](https://github.com/psycopg/psycopg2/issues/928)</code>).</li>
</ul>
<p>What's new in psycopg 2.8.2
^^^^^^^^^^^^^^^^^^^^^^^^^^^</p>
<ul>
<li>Fixed <code>~psycopg2.extras.RealDictCursor</code> when there are repeated columns
(:ticket:<code>[#884](https://github.com/psycopg/psycopg2/issues/884)</code>).</li>
<li>Binary packages built with openssl 1.1.1b. Should fix concurrency problems
(:tickets:<code>[#543](psycopg/psycopg2#543), [#836](https://github.com/psycopg/psycopg2/issues/836)</code>).</li>
</ul>
</tr></table> ... (truncated)
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li>See full diff in <a href="https://github.com/psycopg/psycopg2/commits">compare view</a></li>
</ul>
</details>
<br />


Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
- `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language
- `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language
- `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language
- `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language
- `@dependabot badge me` will comment on this PR with code to add a "Dependabot enabled" badge to your readme

Additionally, you can set the following in your Dependabot [dashboard](https://app.dependabot.com):
- Update frequency (including time of day and day of week)
- Pull request limits (per update run and/or open at any time)
- Automerge options (never/patch/minor, and dev/runtime dependencies)
- Out-of-range updates (receive only lockfile updates, if desired)
- Security updates (receive only security updates, if desired)



</details>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
@danielfoguelman
Copy link

danielfoguelman commented Nov 2, 2022

The fact nobody stumbled in it in 15 years the adapter is used in all the world makes it a pretty unlikely use case.

I stumbled upon it. Do you still think it is an unlikely use case?

@dvarrazzo
Copy link
Member

Considering that the bug was closed 3 years ago, yes, pretty unlikely.

@danielfoguelman
Copy link

I would say, if you find an unlikely event to happen twice in a finite time interval, it is not as unlikely as you thought. But well, the reporter of this issue was kind enough to provide a way to overcome this issue.

@dvarrazzo
Copy link
Member

@danielfoguelman I don't understand: this issue was closed in 2019 in 2.8.5. Are you using an older version than that, and complaining, or have you found the same issue in a more recent version?

@danielfoguelman
Copy link

It is true that I'm using an older version and complaining, Danielle. Sorry about that. From this ticket history I thought that it was closed as a not-fix and it amused me to think that two persons could find an unlikely bug. Now I see you merged the fix in 2.9.5 version.
Again, I didn't ment to be rude, I thought wrong about the whole thing. Have a nice week!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants