Skip to content

Commit

Permalink
Merge pull request #39 from nanjekyejoannah/warn_unicode_parsing
Browse files Browse the repository at this point in the history
Add state tracking for unicode
  • Loading branch information
ltratt authored Apr 9, 2024
2 parents b03f43c + 9312ad1 commit 71af9e4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
4 changes: 2 additions & 2 deletions Lib/test/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -743,13 +743,13 @@ def __str__(self):
self.assertRaises(
TypeError,
unicode,
u'decoding unicode is not supported',
u"'decode()' is not supported on Unicode in 3.x: convert the string to bytes.",
'utf-8',
'strict'
)

self.assertEqual(
unicode('strings are decoded to unicode', 'utf-8', 'strict'),
unicode("'decode()' is not supported on Unicode in 3.x: convert the string to bytes.", 'utf-8', 'strict'),
u'strings are decoded to unicode'
)

Expand Down
22 changes: 17 additions & 5 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1210,9 +1210,15 @@ PyObject *PyUnicode_FromEncodedObject(register PyObject *obj,
}
#else
if (PyUnicode_Check(obj)) {
PyErr_SetString(PyExc_TypeError,
"decoding Unicode is not supported");
return NULL;
if (PyString_CheckExact(self)) {
self->ob_bstate = BSTATE_BYTE;
}

if ((self->ob_bstate == BSTATE_BYTE) &&
PyErr_WarnPy3k(
"'decode()' is not supported on Unicode in 3.x: convert the string to bytes.", 1) < 0) {
return NULL;
}
}
#endif

Expand Down Expand Up @@ -1304,8 +1310,14 @@ PyObject *PyUnicode_AsDecodedObject(PyObject *unicode,
goto onError;
}

if (PyErr_WarnPy3k("decoding Unicode is not supported in 3.x", 1) < 0)
goto onError;
if (PyString_CheckExact(self)) {
self->ob_bstate = BSTATE_BYTE;
}

if ((self->ob_bstate == BSTATE_BYTE) &&
PyErr_WarnPy3k("'decode()' is not supported on Unicode in 3.x: convert the string to bytes.", 1) < 0) {
return NULL;
}

if (encoding == NULL)
encoding = PyUnicode_GetDefaultEncoding();
Expand Down

0 comments on commit 71af9e4

Please sign in to comment.