aiopg icon indicating copy to clipboard operation
aiopg copied to clipboard

Unicode encoding error over the line

Open paultag opened this issue 10 years ago • 3 comments

When inserting, I get the following internal error from aiopg

  File "/usr/local/lib/python3.4/dist-packages/aiopg/sa/connection.py", line 115, in scalar
    res = yield from self.execute(query, *multiparams, **params)
  File "/usr/local/lib/python3.4/dist-packages/aiopg/sa/connection.py", line 102, in execute
    yield from cursor.execute(str(compiled), post_processed_params[0])
  File "/usr/local/lib/python3.4/dist-packages/aiopg/cursor.py", line 106, in execute
    self._impl.execute(operation, parameters)
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 4198: ordinal not in range(128)

Invocation is:

    @asyncio.coroutine
    def create(self, **kwargs):
        with (yield from self.db.engine) as conn:
            runid = yield from conn.scalar(insert(Run.__table__).values(**kwargs))

It's likely breaking on the log of a Run:

class Run(Base):
    __tablename__ = 'run'
    id = Column(Integer, primary_key=True)
    failed = Column(Boolean)
    job_id = Column(Integer, ForeignKey('job.id'))
    job = relationship("Job", foreign_keys=[job_id], backref='runs')
    log = Column(Text)
    start_time = Column(DateTime)
    end_time = Column(DateTime)

I'm guessing it's the log field

paultag avatar Mar 26 '15 15:03 paultag

The direct call site is hitting it with a Unicode string - is aiopg expecting bytes or unicode here?

log = yield from container.log(stdout=True, stderr=True) 
log = log.decode('utf-8')
runid = yield from self.database.run.create(
    ...
    log=log,
    ...
)

paultag avatar Mar 30 '15 15:03 paultag

Giving it a bytestream (keeping it bytes not str) worked.

paultag avatar Mar 31 '15 00:03 paultag

I have had this error too. It's because database encoding is not utf-8 (sql_ascii or something else). But will be good if library will give easy way for changing connection settings (like set_client_encoding) globally when creating engine (for example django sets utf-8 encoding for all connections as default)

Lagovas avatar Jul 30 '15 18:07 Lagovas