question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'encoding' cannot be specified in python 3

See original GitHub issue

https://github.com/getsentry/raven-python/blob/1a0d697dd11459bfa8ce933b3b53264098dad60d/raven/utils/json.py#L50

The ‘json.dumps’ command no longer have an encoding argument in python 3 and causes this stacktrace:

File "/usr/local/lib/python3.6/site-packages/raven/handlers/logging.py", line 89, in emit
 return self.emit(record)
 File "/usr/local/lib/python3.6/site-packages/raven/handlers/logging.py", line 182, in _emit
 *kwargs)
 File "/usr/local/lib/python3.6/site-packages/raven/base.py", line 645, in capture
 self.send(*data)
 File "/usr/local/lib/python3.6/site-packages/raven/base.py", line 740, in send
 message = self.encode(data)
 File "/usr/local/lib/python3.6/site-packages/raven/base.py", line 782, in encode
 return zlib.compress(json.dumps(data).encode('utf8'))
 File "/usr/local/lib/python3.6/site-packages/raven/utils/json.py", line 51, in dumps
 return json.dumps(value, cls=BetterJSONEncoder, **kwargs)
 File "/usr/local/lib/python3.6/json/init.py", line 238, in dumps
 **kw).encode(obj)
TypeError: __init_() got an unexpected keyword argument 'encoding'

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
miracle2kcommented, Jun 3, 2018

I don’t think this should be closed.

I am seeing this still, and it’s a bit confusing, but here is what is happening. We have this code in Sentry:

https://github.com/getsentry/raven-python/blob/1a0d697dd11459bfa8ce933b3b53264098dad60d/raven/utils/json.py#L46-L51

The logic here seems to be: Try to serialize everything to JSON, and if that fails for any reason, try it again, but this time use a special codec that ignores any errors. (Question: Why use a catch-all here, rather than just catching encoding-related exceptions?).

The problem, as stated above, is that on Python 3, the encoding argument is no longer supported. So if for any reason this fallback-branch triggers, we get an exception like this:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/raven/handlers/logbook.py", line 49, in emit
    return self._emit(record)
  File "/usr/local/lib/python3.6/site-packages/raven/handlers/logbook.py", line 100, in _emit
    **handler_kwargs
  File "/usr/local/lib/python3.6/site-packages/raven/base.py", line 651, in capture
    self.send(**data)
  File "/usr/local/lib/python3.6/site-packages/raven/base.py", line 746, in send
    message = self.encode(data)
  File "/usr/local/lib/python3.6/site-packages/raven/base.py", line 788, in encode
    return zlib.compress(json.dumps(data).encode('utf8'))
  File "/usr/local/lib/python3.6/site-packages/raven/utils/json.py", line 63, in dumps
    return json.dumps(value, cls=BetterJSONEncoder, **kwargs)
  File "/usr/local/lib/python3.6/json/__init__.py", line 238, in dumps
    **kw).encode(obj)
TypeError: __init__() got an unexpected keyword argument 'encoding'

So the task here would probably to a) find a way to support the fallback-branch on Python 3, or disable the fallback branch on Py3 so the exception is less confusing.

0reactions
yangjielcommented, May 5, 2020

@yangjiel I already did: #1350

Thank you for the information. What I am using is the latest released version 6.10.0. It doesn’t include the fix. I think Sentry stops continuing on this package as they didn’t release new version on PYPI for about two years. I moved to the sentry_sdk package instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Python 3 character encoding issue - Stack Overflow
My issue relates to using a jinja2 template. Where the template doesn't contain any unicode everything is OK, however, once there is a...
Read more >
Python String encode() Method - W3Schools
The encode() method encodes the string, using the specified encoding. ... These examples uses ascii encoding, and a character that cannot be encoded, ......
Read more >
Unicode HOWTO — Python 3.11.1 documentation
Encodings are specified as strings containing the encoding's name. Python comes with roughly 100 different encodings; see the Python Library Reference at ...
Read more >
Unicode & Character Encodings in Python: A Painless Guide
Encoding and Decoding in Python 3 ... Python 3's str type is meant to represent human-readable text and can contain any Unicode character....
Read more >
Python 3 Notes: Reading and Writing Methods
"UnicodeDecodeError" means you have a file encoding issue. Each computer has its own system-wide default encoding, and the file you are trying to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found