Skip to content

Commit cacfc07

Browse files
committed
- A new type object, 'string', is added. This is a common base type
for 'str' and 'unicode', and can be used instead of types.StringTypes, e.g. to test whether something is "a string": isinstance(x, string) is True for Unicode and 8-bit strings. This is an abstract base class and cannot be instantiated directly.
1 parent 9ee4b94 commit cacfc07

File tree

6 files changed

+69
-2
lines changed

6 files changed

+69
-2
lines changed

Include/stringobject.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ typedef struct {
3939
char ob_sval[1];
4040
} PyStringObject;
4141

42+
extern DL_IMPORT(PyTypeObject) PyBaseString_Type;
4243
extern DL_IMPORT(PyTypeObject) PyString_Type;
4344

4445
#define PyString_Check(op) PyObject_TypeCheck(op, &PyString_Type)

Misc/NEWS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ Type/class unification and new-style classes
66

77
Core and builtins
88

9+
- A new type object, 'string', is added. This is a common base type
10+
for 'str' and 'unicode', and can be used instead of
11+
types.StringTypes, e.g. to test whether something is "a string":
12+
isinstance(x, string) is True for Unicode and 8-bit strings. This
13+
is an abstract base class and cannot be instantiated directly.
14+
915
- Deprecated features of xrange objects have been removed as
1016
promised. The start, stop, and step attributes and the tolist()
1117
method no longer exist. xrange repetition and slicing have been

Objects/object.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1779,6 +1779,9 @@ _Py_ReadyTypes(void)
17791779
if (PyType_Ready(&PyBool_Type) < 0)
17801780
Py_FatalError("Can't initialize 'bool'");
17811781

1782+
if (PyType_Ready(&PyString_Type) < 0)
1783+
Py_FatalError("Can't initialize 'str'");
1784+
17821785
if (PyType_Ready(&PyList_Type) < 0)
17831786
Py_FatalError("Can't initialize 'list'");
17841787

Objects/stringobject.c

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2855,6 +2855,60 @@ str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
28552855
return pnew;
28562856
}
28572857

2858+
static PyObject *
2859+
basestring_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2860+
{
2861+
PyErr_SetString(PyExc_TypeError,
2862+
"The string type cannot be instantiated");
2863+
return NULL;
2864+
}
2865+
2866+
static char basestring_doc[] =
2867+
"Type string cannot be instantiated; it is the base for str and unicode.";
2868+
2869+
PyTypeObject PyBaseString_Type = {
2870+
PyObject_HEAD_INIT(&PyType_Type)
2871+
0,
2872+
"string",
2873+
0,
2874+
0,
2875+
0, /* tp_dealloc */
2876+
0, /* tp_print */
2877+
0, /* tp_getattr */
2878+
0, /* tp_setattr */
2879+
0, /* tp_compare */
2880+
0, /* tp_repr */
2881+
0, /* tp_as_number */
2882+
0, /* tp_as_sequence */
2883+
0, /* tp_as_mapping */
2884+
0, /* tp_hash */
2885+
0, /* tp_call */
2886+
0, /* tp_str */
2887+
0, /* tp_getattro */
2888+
0, /* tp_setattro */
2889+
0, /* tp_as_buffer */
2890+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2891+
basestring_doc, /* tp_doc */
2892+
0, /* tp_traverse */
2893+
0, /* tp_clear */
2894+
0, /* tp_richcompare */
2895+
0, /* tp_weaklistoffset */
2896+
0, /* tp_iter */
2897+
0, /* tp_iternext */
2898+
0, /* tp_methods */
2899+
0, /* tp_members */
2900+
0, /* tp_getset */
2901+
&PyBaseObject_Type, /* tp_base */
2902+
0, /* tp_dict */
2903+
0, /* tp_descr_get */
2904+
0, /* tp_descr_set */
2905+
0, /* tp_dictoffset */
2906+
0, /* tp_init */
2907+
0, /* tp_alloc */
2908+
basestring_new, /* tp_new */
2909+
0, /* tp_free */
2910+
};
2911+
28582912
static char string_doc[] =
28592913
"str(object) -> string\n\
28602914
\n\
@@ -2893,7 +2947,7 @@ PyTypeObject PyString_Type = {
28932947
string_methods, /* tp_methods */
28942948
0, /* tp_members */
28952949
0, /* tp_getset */
2896-
0, /* tp_base */
2950+
&PyBaseString_Type, /* tp_base */
28972951
0, /* tp_dict */
28982952
0, /* tp_descr_get */
28992953
0, /* tp_descr_set */

Objects/unicodeobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5835,7 +5835,7 @@ PyTypeObject PyUnicode_Type = {
58355835
unicode_methods, /* tp_methods */
58365836
0, /* tp_members */
58375837
0, /* tp_getset */
5838-
0, /* tp_base */
5838+
&PyBaseString_Type, /* tp_base */
58395839
0, /* tp_dict */
58405840
0, /* tp_descr_get */
58415841
0, /* tp_descr_set */
@@ -5859,6 +5859,8 @@ void _PyUnicode_Init(void)
58595859
strcpy(unicode_default_encoding, "ascii");
58605860
for (i = 0; i < 256; i++)
58615861
unicode_latin1[i] = NULL;
5862+
if (PyType_Ready(&PyUnicode_Type) < 0)
5863+
Py_FatalError("Can't initialize 'unicode'");
58625864
}
58635865

58645866
/* Finalize the Unicode implementation */

Python/bltinmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1905,6 +1905,7 @@ _PyBuiltin_Init(void)
19051905
SETBUILTIN("object", &PyBaseObject_Type);
19061906
SETBUILTIN("staticmethod", &PyStaticMethod_Type);
19071907
SETBUILTIN("str", &PyString_Type);
1908+
SETBUILTIN("string", &PyBaseString_Type);
19081909
SETBUILTIN("super", &PySuper_Type);
19091910
SETBUILTIN("tuple", &PyTuple_Type);
19101911
SETBUILTIN("type", &PyType_Type);

0 commit comments

Comments
 (0)