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

Fix Connection class _copy_in private method #555

Closed
wants to merge 1 commit into from
Closed

Fix Connection class _copy_in private method #555

wants to merge 1 commit into from

Conversation

ABCDeath
Copy link
Contributor

I've found an issue in Connection.copy_to_table() method with a path-like source argument. The following code throws exception io.UnsupportedOperation: read:

import asyncio
import asyncpg

async def main():
    conn = await asyncpg.connect(connection_string)
    await conn.execute('CREATE TABLE test_table(a text, "b~" text, i int);')
    await conn.copy_to_table('test_table', source=filename)
    
filename = 'table_content'
f = open(filename, 'wb')
f.write(
    '\n'.join([
        'a1\tb1\t1',
        'a2\tb2\t2',
        'a3\tb3\t3',
        'a4\tb4\t4',
        'a5\tb5\t5',
        ''
    ]).encode('utf-8')
)
f.close()

asyncio.run(main())

It looks like Connection._copy_in() method opens file in a wrong mode (wb instead of rb).
So this fix:

  • Change file opening mode to rb;
  • Add test case for Connection.copy_to_table() method with a path-like source.

 * Change file opening mode to 'rb'
 * Add test case for Connection.copy_to_table with path-like source
Copy link
Member

@elprans elprans left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Thanks!

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

Successfully merging this pull request may close these issues.

None yet

2 participants