Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bef1417

Browse files
committed
Make int, long and float subclassable.
This uses a slightly wimpy and wasteful approach, but it works. :-)
1 parent e705ef1 commit bef1417

3 files changed

Lines changed: 111 additions & 14 deletions

File tree

Objects/floatobject.c

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -626,20 +626,47 @@ float_float(PyObject *v)
626626
}
627627

628628

629+
staticforward PyObject *
630+
float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
631+
629632
static PyObject *
630633
float_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
631634
{
632635
PyObject *x = Py_False; /* Integer zero */
633636
static char *kwlist[] = {"x", 0};
634637

635-
assert(type == &PyFloat_Type);
638+
if (type != &PyFloat_Type)
639+
return float_subtype_new(type, args, kwds); /* Wimp out */
636640
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:float", kwlist, &x))
637641
return NULL;
638642
if (PyString_Check(x))
639643
return PyFloat_FromString(x, NULL);
640644
return PyNumber_Float(x);
641645
}
642646

647+
/* Wimpy, slow approach to tp_new calls for subtypes of float:
648+
first create a regular float from whatever arguments we got,
649+
then allocate a subtype instance and initialize its ob_fval
650+
from the regular float. The regular float is then thrown away.
651+
*/
652+
static PyObject *
653+
float_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
654+
{
655+
PyObject *tmp, *new;
656+
657+
assert(PyType_IsSubtype(type, &PyFloat_Type));
658+
tmp = float_new(&PyFloat_Type, args, kwds);
659+
if (tmp == NULL)
660+
return NULL;
661+
assert(PyFloat_Check(tmp));
662+
new = type->tp_alloc(type, 0);;
663+
if (new == NULL)
664+
return NULL;
665+
((PyFloatObject *)new)->ob_fval = ((PyFloatObject *)tmp)->ob_fval;
666+
Py_DECREF(tmp);
667+
return new;
668+
}
669+
643670
static char float_doc[] =
644671
"float(x) -> floating point number\n\
645672
\n\
@@ -708,7 +735,8 @@ PyTypeObject PyFloat_Type = {
708735
PyObject_GenericGetAttr, /* tp_getattro */
709736
0, /* tp_setattro */
710737
0, /* tp_as_buffer */
711-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
738+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
739+
Py_TPFLAGS_BASETYPE, /* tp_flags */
712740
float_doc, /* tp_doc */
713741
0, /* tp_traverse */
714742
0, /* tp_clear */
@@ -750,7 +778,7 @@ PyFloat_Fini(void)
750778
for (i = 0, p = &list->objects[0];
751779
i < N_FLOATOBJECTS;
752780
i++, p++) {
753-
if (PyFloat_Check(p) && p->ob_refcnt != 0)
781+
if (p->ob_type == &PyFloat_Type && p->ob_refcnt != 0)
754782
frem++;
755783
}
756784
next = list->next;
@@ -760,7 +788,8 @@ PyFloat_Fini(void)
760788
for (i = 0, p = &list->objects[0];
761789
i < N_FLOATOBJECTS;
762790
i++, p++) {
763-
if (!PyFloat_Check(p) || p->ob_refcnt == 0) {
791+
if (p->ob_type != &PyFloat_Type ||
792+
p->ob_refcnt == 0) {
764793
p->ob_type = (struct _typeobject *)
765794
free_list;
766795
free_list = p;
@@ -792,7 +821,8 @@ PyFloat_Fini(void)
792821
for (i = 0, p = &list->objects[0];
793822
i < N_FLOATOBJECTS;
794823
i++, p++) {
795-
if (PyFloat_Check(p) && p->ob_refcnt != 0) {
824+
if (p->ob_type == &PyFloat_Type &&
825+
p->ob_refcnt != 0) {
796826
char buf[100];
797827
PyFloat_AsString(buf, p);
798828
fprintf(stderr,

Objects/intobject.c

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,12 @@ PyInt_FromLong(long ival)
134134
static void
135135
int_dealloc(PyIntObject *v)
136136
{
137-
v->ob_type = (struct _typeobject *)free_list;
138-
free_list = v;
137+
if (v->ob_type == &PyInt_Type) {
138+
v->ob_type = (struct _typeobject *)free_list;
139+
free_list = v;
140+
}
141+
else
142+
v->ob_type->tp_free((PyObject *)v);
139143
}
140144

141145
long
@@ -783,14 +787,18 @@ int_hex(PyIntObject *v)
783787
return PyString_FromString(buf);
784788
}
785789

790+
staticforward PyObject *
791+
int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
792+
786793
static PyObject *
787794
int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
788795
{
789796
PyObject *x = NULL;
790797
int base = -909;
791798
static char *kwlist[] = {"x", "base", 0};
792799

793-
assert(type == &PyInt_Type);
800+
if (type != &PyInt_Type)
801+
return int_subtype_new(type, args, kwds); /* Wimp out */
794802
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:int", kwlist,
795803
&x, &base))
796804
return NULL;
@@ -811,6 +819,29 @@ int_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
811819
return NULL;
812820
}
813821

822+
/* Wimpy, slow approach to tp_new calls for subtypes of int:
823+
first create a regular int from whatever arguments we got,
824+
then allocate a subtype instance and initialize its ob_ival
825+
from the regular int. The regular int is then thrown away.
826+
*/
827+
static PyObject *
828+
int_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
829+
{
830+
PyObject *tmp, *new;
831+
832+
assert(PyType_IsSubtype(type, &PyInt_Type));
833+
tmp = int_new(&PyInt_Type, args, kwds);
834+
if (tmp == NULL)
835+
return NULL;
836+
assert(PyInt_Check(tmp));
837+
new = type->tp_alloc(type, 0);;
838+
if (new == NULL)
839+
return NULL;
840+
((PyIntObject *)new)->ob_ival = ((PyIntObject *)tmp)->ob_ival;
841+
Py_DECREF(tmp);
842+
return new;
843+
}
844+
814845
static char int_doc[] =
815846
"int(x[, base]) -> integer\n\
816847
\n\
@@ -882,7 +913,8 @@ PyTypeObject PyInt_Type = {
882913
PyObject_GenericGetAttr, /* tp_getattro */
883914
0, /* tp_setattro */
884915
0, /* tp_as_buffer */
885-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
916+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
917+
Py_TPFLAGS_BASETYPE, /* tp_flags */
886918
int_doc, /* tp_doc */
887919
0, /* tp_traverse */
888920
0, /* tp_clear */
@@ -934,7 +966,7 @@ PyInt_Fini(void)
934966
for (i = 0, p = &list->objects[0];
935967
i < N_INTOBJECTS;
936968
i++, p++) {
937-
if (PyInt_Check(p) && p->ob_refcnt != 0)
969+
if (p->ob_type == &PyInt_Type && p->ob_refcnt != 0)
938970
irem++;
939971
}
940972
next = list->next;
@@ -944,7 +976,8 @@ PyInt_Fini(void)
944976
for (i = 0, p = &list->objects[0];
945977
i < N_INTOBJECTS;
946978
i++, p++) {
947-
if (!PyInt_Check(p) || p->ob_refcnt == 0) {
979+
if (p->ob_type != &PyInt_Type ||
980+
p->ob_refcnt == 0) {
948981
p->ob_type = (struct _typeobject *)
949982
free_list;
950983
free_list = p;
@@ -986,7 +1019,7 @@ PyInt_Fini(void)
9861019
for (i = 0, p = &list->objects[0];
9871020
i < N_INTOBJECTS;
9881021
i++, p++) {
989-
if (PyInt_Check(p) && p->ob_refcnt != 0)
1022+
if (p->ob_type == &PyInt_Type && p->ob_refcnt != 0)
9901023
fprintf(stderr,
9911024
"# <int at %p, refcnt=%d, val=%ld>\n",
9921025
p, p->ob_refcnt, p->ob_ival);

Objects/longobject.c

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2038,6 +2038,8 @@ long_hex(PyObject *v)
20382038
{
20392039
return long_format(v, 16, 1);
20402040
}
2041+
staticforward PyObject *
2042+
long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
20412043

20422044
static PyObject *
20432045
long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
@@ -2046,7 +2048,8 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
20462048
int base = -909; /* unlikely! */
20472049
static char *kwlist[] = {"x", "base", 0};
20482050

2049-
assert(type == &PyLong_Type);
2051+
if (type != &PyLong_Type)
2052+
return long_subtype_new(type, args, kwds); /* Wimp out */
20502053
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oi:long", kwlist,
20512054
&x, &base))
20522055
return NULL;
@@ -2069,6 +2072,36 @@ long_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
20692072
}
20702073
}
20712074

2075+
/* Wimpy, slow approach to tp_new calls for subtypes of long:
2076+
first create a regular long from whatever arguments we got,
2077+
then allocate a subtype instance and initialize it from
2078+
the regular long. The regular long is then thrown away.
2079+
*/
2080+
static PyObject *
2081+
long_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2082+
{
2083+
PyLongObject *tmp, *new;
2084+
int i, n;
2085+
2086+
assert(PyType_IsSubtype(type, &PyLong_Type));
2087+
tmp = (PyLongObject *)long_new(&PyLong_Type, args, kwds);
2088+
if (tmp == NULL)
2089+
return NULL;
2090+
assert(PyLong_Check(tmp));
2091+
n = tmp->ob_size;
2092+
if (n < 0)
2093+
n = -n;
2094+
new = (PyLongObject *)type->tp_alloc(type, n);
2095+
if (new == NULL)
2096+
return NULL;
2097+
assert(PyLong_Check(new));
2098+
new->ob_size = type->ob_size;
2099+
for (i = 0; i < n; i++)
2100+
new->ob_digit[i] = tmp->ob_digit[i];
2101+
Py_DECREF(tmp);
2102+
return (PyObject *)new;
2103+
}
2104+
20722105
static char long_doc[] =
20732106
"long(x[, base]) -> integer\n\
20742107
\n\
@@ -2140,7 +2173,8 @@ PyTypeObject PyLong_Type = {
21402173
PyObject_GenericGetAttr, /* tp_getattro */
21412174
0, /* tp_setattro */
21422175
0, /* tp_as_buffer */
2143-
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES, /* tp_flags */
2176+
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_CHECKTYPES |
2177+
Py_TPFLAGS_BASETYPE, /* tp_flags */
21442178
long_doc, /* tp_doc */
21452179
0, /* tp_traverse */
21462180
0, /* tp_clear */

0 commit comments

Comments
 (0)