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

On Python 2, decode empty strings as str not unicode. #225

Merged
merged 3 commits into from
Jun 27, 2018

Conversation

benjaminp
Copy link
Contributor

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.

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.
@etrepum
Copy link
Member

etrepum commented Jun 26, 2018

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.

@benjaminp
Copy link
Contributor Author

Unfortunately, we have a bunch of code that relies on simplejson returning the str for ASCII. I ran into this problem trying to upgrade our ancient simplejson version.

@etrepum
Copy link
Member

etrepum commented Jun 26, 2018

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?

@benjaminp benjaminp force-pushed the empty-str branch 2 times, most recently from ca2cca7 to d076f24 Compare June 26, 2018 23:45
@benjaminp
Copy link
Contributor Author

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;
Copy link
Contributor

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.

Copy link
Contributor Author

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.

@@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

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

Use spaces for indentation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok.

@serhiy-storchaka
Copy link
Contributor

Other issues which reported about the same were closed: #84 and #190.

@etrepum etrepum merged commit 131d225 into simplejson:master Jun 27, 2018
MarcDufresne pushed a commit to MarcDufresne/TrelloToPhabricatorMigration that referenced this pull request Sep 9, 2018
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>
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

3 participants