Skip to content

Commit 0d21cb8

Browse files
Track without evading PyObject
1 parent b03f43c commit 0d21cb8

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

Include/object.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,8 @@ PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
465465
#ifdef Py_USING_UNICODE
466466
PyAPI_FUNC(PyObject *) PyObject_Unicode(PyObject *);
467467
#endif
468+
PyAPI_DATA(Py_ssize_t) PyObject_GetBState(PyObject *);
469+
PyAPI_FUNC() PyObject_Unicode(PyObject *);
468470
PyAPI_FUNC(int) PyObject_Compare(PyObject *, PyObject *);
469471
PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
470472
PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);

Objects/object.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,57 @@ PyObject_Str(PyObject *v)
471471
return res;
472472
}
473473

474+
PyObject *
475+
PyObject_GetBState(PyObject *v)
476+
{
477+
PyUnicodeObject *uni;
478+
PyStringObject *str;
479+
PyBytesObject *byt;
480+
Py_ssize_t bbstate;
481+
482+
if (v == NULL) {
483+
return 0
484+
}
485+
else {
486+
if (!PyUnicode_CheckExact(v) || !PyByte_CheckExact(v)) {
487+
return -1;
488+
}
489+
if (PyUnicode_CheckExact(v)) {
490+
uni = (PyUnicodeObject *) PyUnicode_FromObject(v);
491+
if (uni == NULL)
492+
return 0;
493+
bbstate = uni->ob_bstate;
494+
if (bbstate == NULL)
495+
Py_XDECREF(uni);
496+
return 0;
497+
Py_DECREF(uni);
498+
return bbstate;
499+
}
500+
if (PyString_CheckExact(v)) {
501+
str = (PyStringObject *) v;
502+
if (str == NULL)
503+
return 0;
504+
bbstate = str->ob_bstate;
505+
if (bbstate == NULL)
506+
Py_XDECREF(str);
507+
return 0;
508+
Py_DECREF(str);
509+
return bbstate;
510+
}
511+
if (PyBytes_CheckExact(v)) {
512+
byt = (PyBytesObject *) v;
513+
if (byt == NULL)
514+
return 0;
515+
bbstate = byt->ob_bstate;
516+
if (bbstate == NULL)
517+
Py_XDECREF(byt);
518+
return 0;
519+
Py_DECREF(byt);
520+
return bbstate;
521+
}
522+
}
523+
}
524+
474525
#ifdef Py_USING_UNICODE
475526
PyObject *
476527
PyObject_Unicode(PyObject *v)

Objects/stringobject.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3728,6 +3728,11 @@ string_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
37283728
return NULL;
37293729
if (x == NULL)
37303730
return PyString_FromString("");
3731+
if (PyBytes_CheckExact(v)) {
3732+
(PyStringObject *)x->ob_bstate = BSTATE_NOT_SURE;
3733+
} else {
3734+
(PyBytesObject *)x->ob_bstate = BSTATE_BYTE;
3735+
}
37313736
return PyObject_Str(x);
37323737
}
37333738

Objects/unicodeobject.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8859,6 +8859,7 @@ unicode_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
88598859
return NULL;
88608860
if (x == NULL)
88618861
return (PyObject *)_PyUnicode_New(0);
8862+
(PyUnicodeObject *)x->ob_bstate = BSTATE_UNICODE;
88628863
if (encoding == NULL && errors == NULL)
88638864
return PyObject_Unicode(x);
88648865
else

0 commit comments

Comments
 (0)