-
Notifications
You must be signed in to change notification settings - Fork 334
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
On Python 2, decode empty strings as str not unicode. #225
Conversation
In general on Python 2, simplejson decodes ASCII strings as str, only promoting to unicode when needed: >>> simplejson.loads('["Spaetzle", "Spätzle"]') ['Spaetzle', u'Sp\xe4tzle'] Since 83a493d, though, simplejson has always decoded empty JSON strings as unicode: >>> simplejson.loads('""') u'' This PR restores the old behavior of decoding empty strings as str.
Is there a reason you need this? The reason it was originally implemented this way was for performance (memory usage mostly), but this change won't really have any effect on that since it's a constant. |
Unfortunately, we have a bunch of code that relies on simplejson returning the |
Can you add a test to this PR that verifies that this change (and more importantly, future changes) have the behavior that your app needs? |
ca2cca7
to
d076f24
Compare
Done; sadly this is only the behavior of the C extensions. |
@@ -76,6 +76,9 @@ static PyObject *JSON_Infinity = NULL; | |||
static PyObject *JSON_NegInfinity = NULL; | |||
static PyObject *JSON_NaN = NULL; | |||
static PyObject *JSON_EmptyUnicode = NULL; | |||
#if PY_MAJOR_VERSION < 3 | |||
static PyObject *JSON_EmptyStr = NULL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is JSON_EmptyUnicode
still used in Python 2? If no, then two variables can be merged.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is used on all Python versions.
simplejson/_speedups.c
Outdated
@@ -3331,6 +3329,9 @@ init_constants(void) | |||
#if PY_MAJOR_VERSION >= 3 | |||
JSON_EmptyUnicode = PyUnicode_New(0, 127); | |||
#else /* PY_MAJOR_VERSION >= 3 */ | |||
JSON_EmptyStr = PyString_FromString(""); | |||
if (JSON_EmptyStr == NULL) | |||
return 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use spaces for indentation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok.
This PR updates [simplejson](https://pypi.org/project/simplejson) from **3.15.0** to **3.16.1**. <details> <summary>Changelog</summary> ### 3.16.1 ``` * Added examples for JSON lines use cases simplejson/simplejson#236 * Add wheels for more Python versions and platforms simplejson/simplejson#234 simplejson/simplejson#233 simplejson/simplejson#231 ``` ### 3.16.0 ``` * Restore old behavior with regard to the type of decoded empty strings with speedups enabled on Python 2.x simplejson/simplejson#225 * Add python_requires to setup.py to help pip simplejson/simplejson#224 * Fix CSS in docs when built locally simplejson/simplejson#222 ``` </details> <details> <summary>Links</summary> - PyPI: https://pypi.org/project/simplejson - Changelog: https://pyup.io/changelogs/simplejson/ - Repo: https://github.com/simplejson/simplejson </details>
In general on Python 2, simplejson decodes ASCII strings as str, only promoting
to unicode when needed:
Since 83a493d, though, simplejson has always
decoded empty JSON strings as unicode:
This PR restores the old behavior of decoding empty strings as str.