Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
Pyston
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
Pyston
Commits
c91ea1b5
Commit
c91ea1b5
authored
Sep 20, 2016
by
Kevin Modzelewski
Committed by
GitHub
Sep 20, 2016
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1370 from kmod/subclasscheck
Add type.__instancecheck__ and __subclasscheck__
parents
70993f0a
77658381
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
47 additions
and
0 deletions
+47
-0
src/capi/abstract.cpp
src/capi/abstract.cpp
+4
-0
src/capi/typeobject.cpp
src/capi/typeobject.cpp
+12
-0
src/runtime/types.cpp
src/runtime/types.cpp
+28
-0
test/tests/subclasscheck_test.py
test/tests/subclasscheck_test.py
+3
-0
No files found.
src/capi/abstract.cpp
View file @
c91ea1b5
...
...
@@ -185,6 +185,10 @@ extern "C" int PyObject_IsInstance(PyObject* inst, PyObject* cls) noexcept {
return
recursive_isinstance
(
inst
,
cls
);
}
extern
"C"
int
_PyObject_RealIsInstance
(
PyObject
*
inst
,
PyObject
*
cls
)
noexcept
{
return
recursive_isinstance
(
inst
,
cls
);
}
extern
"C"
int
PyObject_IsSubclass
(
PyObject
*
derived
,
PyObject
*
cls
)
noexcept
{
static
PyObject
*
name
=
NULL
;
...
...
src/capi/typeobject.cpp
View file @
c91ea1b5
...
...
@@ -1924,6 +1924,18 @@ static const slotdef* update_one_slot(BoxedClass* type, const slotdef* p) noexce
descr
=
NULL
;
}
static
BoxedString
*
instancecheck_str
=
getStaticString
(
"__instancecheck__"
);
if
(
p
->
name_strobj
==
instancecheck_str
)
{
if
(
descr
==
type_cls
->
getattr
(
instancecheck_str
))
descr
=
NULL
;
}
static
BoxedString
*
subclasscheck_str
=
getStaticString
(
"__subclasscheck__"
);
if
(
p
->
name_strobj
==
subclasscheck_str
)
{
if
(
descr
==
type_cls
->
getattr
(
subclasscheck_str
))
descr
=
NULL
;
}
static
BoxedString
*
getattribute_str
=
getStaticString
(
"__getattribute__"
);
if
(
p
->
name_strobj
==
getattribute_str
)
{
if
(
type
->
tp_getattr
==
NULL
&&
descr
&&
descr
->
cls
==
&
PyWrapperDescr_Type
...
...
src/runtime/types.cpp
View file @
c91ea1b5
...
...
@@ -1400,6 +1400,28 @@ static int type_sub_set_dict(BORROWED(Box*) obj, BORROWED(Box*) val, void* conte
return
0
;
}
static
PyObject
*
type___instancecheck__
(
PyObject
*
type
,
PyObject
*
inst
)
noexcept
{
switch
(
_PyObject_RealIsInstance
(
inst
,
type
))
{
case
-
1
:
return
NULL
;
case
0
:
Py_RETURN_FALSE
;
default:
Py_RETURN_TRUE
;
}
}
static
PyObject
*
type___subclasscheck__
(
PyObject
*
type
,
PyObject
*
inst
)
noexcept
{
switch
(
_PyObject_RealIsSubclass
(
inst
,
type
))
{
case
-
1
:
return
NULL
;
case
0
:
Py_RETURN_FALSE
;
default:
Py_RETURN_TRUE
;
}
}
extern
"C"
void
PyType_SetDict
(
PyTypeObject
*
type
,
STOLEN
(
PyObject
*
)
dict
)
noexcept
{
type_sub_set_dict
(
type
,
dict
,
NULL
);
Box
*
old_dict
=
type
->
tp_dict
;
...
...
@@ -4399,6 +4421,12 @@ void setupRuntime() {
type_cls
->
giveAttrDescriptor
(
"__abstractmethods__"
,
(
getter
)
type_abstractmethods
,
(
setter
)
type_set_abstractmethods
);
type_cls
->
giveAttr
(
"__call__"
,
new
BoxedFunction
(
typeCallObj
));
type_cls
->
giveAttr
(
"__subclasscheck__"
,
new
BoxedFunction
(
BoxedCode
::
create
((
void
*
)
type___subclasscheck__
,
BOXED_BOOL
,
2
,
"type.__subclasscheck__"
)));
type_cls
->
giveAttr
(
"__instancecheck__"
,
new
BoxedFunction
(
BoxedCode
::
create
((
void
*
)
type___instancecheck__
,
BOXED_BOOL
,
2
,
"type.__instancecheck__"
)));
type_cls
->
giveAttr
(
"__new__"
,
new
BoxedFunction
(
BoxedCode
::
create
((
void
*
)
typeNewGeneric
,
UNKNOWN
,
4
,
false
,
false
,
"type.__new__"
),
...
...
test/tests/subclasscheck_test.py
View file @
c91ea1b5
assert
hasattr
(
object
,
"__subclasscheck__"
)
assert
hasattr
(
object
,
"__instancecheck__"
)
class
M
(
type
):
def
__instancecheck__
(
self
,
rhs
):
print
"M.instancecheck"
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment