Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
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
Kirill Smelkov
cpython
Commits
af1692a2
Commit
af1692a2
authored
May 09, 2009
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
convert some more special methods to use _PyObject_LookupSpecial
parent
f9b01fe6
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
30 additions
and
28 deletions
+30
-28
Lib/test/test_descr.py
Lib/test/test_descr.py
+15
-3
Objects/abstract.c
Objects/abstract.c
+8
-13
Python/sysmodule.c
Python/sysmodule.c
+7
-12
No files found.
Lib/test/test_descr.py
View file @
af1692a2
import
__builtin__
import
__builtin__
import
sys
import
types
import
types
import
unittest
import
unittest
import
warnings
import
warnings
...
@@ -1678,13 +1679,20 @@ order (MRO) for bases """
...
@@ -1678,13 +1679,20 @@ order (MRO) for bases """
return
"hello"
return
"hello"
def
empty_seq
(
self
):
def
empty_seq
(
self
):
return
[]
return
[]
def
zero
(
self
):
return
0
def
stop
(
self
):
raise
StopIteration
# It would be nice to have every special method tested here, but I'm
# It would be nice to have every special method tested here, but I'm
# only listing the ones I can remember outside of typeobject.c, since it
# only listing the ones I can remember outside of typeobject.c, since it
# does it right.
# does it right.
specials
=
[
specials
=
[
(
"__unicode__"
,
unicode
,
hello
),
(
"__unicode__"
,
unicode
,
hello
,
{}),
(
"__reversed__"
,
reversed
,
empty_seq
),
(
"__reversed__"
,
reversed
,
empty_seq
,
{}),
(
"__length_hint__"
,
list
,
zero
,
{
"__iter__"
:
iden
,
"next"
:
stop
}),
(
"__sizeof__"
,
sys
.
getsizeof
,
zero
,
{}),
# These two fail because the compiler generates LOAD_ATTR to look
# These two fail because the compiler generates LOAD_ATTR to look
# them up. We'd have to add a new opcode to fix this, and it's
# them up. We'd have to add a new opcode to fix this, and it's
# probably not worth it.
# probably not worth it.
...
@@ -1705,15 +1713,19 @@ order (MRO) for bases """
...
@@ -1705,15 +1713,19 @@ order (MRO) for bases """
return
self
.
impl
.
__get__
(
obj
,
owner
)
return
self
.
impl
.
__get__
(
obj
,
owner
)
for
name
,
runner
,
meth_impl
in
specials
:
for
name
,
runner
,
meth_impl
,
env
in
specials
:
class
X
(
Checker
):
class
X
(
Checker
):
pass
pass
for
attr
,
obj
in
env
.
iteritems
():
setattr
(
X
,
attr
,
obj
)
setattr
(
X
,
name
,
meth_impl
)
setattr
(
X
,
name
,
meth_impl
)
runner
(
X
())
runner
(
X
())
record
=
[]
record
=
[]
class
X
(
Checker
):
class
X
(
Checker
):
pass
pass
for
attr
,
obj
in
env
.
iteritems
():
setattr
(
X
,
attr
,
obj
)
setattr
(
X
,
name
,
SpecialDescr
(
meth_impl
))
setattr
(
X
,
name
,
SpecialDescr
(
meth_impl
))
runner
(
X
())
runner
(
X
())
self
.
assertEqual
(
record
,
[
1
],
name
)
self
.
assertEqual
(
record
,
[
1
],
name
)
...
...
Objects/abstract.c
View file @
af1692a2
...
@@ -93,7 +93,7 @@ Py_ssize_t
...
@@ -93,7 +93,7 @@ Py_ssize_t
_PyObject_LengthHint
(
PyObject
*
o
,
Py_ssize_t
defaultvalue
)
_PyObject_LengthHint
(
PyObject
*
o
,
Py_ssize_t
defaultvalue
)
{
{
static
PyObject
*
hintstrobj
=
NULL
;
static
PyObject
*
hintstrobj
=
NULL
;
PyObject
*
ro
;
PyObject
*
ro
,
*
hintmeth
;
Py_ssize_t
rv
;
Py_ssize_t
rv
;
/* try o.__len__() */
/* try o.__len__() */
...
@@ -107,20 +107,15 @@ _PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
...
@@ -107,20 +107,15 @@ _PyObject_LengthHint(PyObject *o, Py_ssize_t defaultvalue)
PyErr_Clear
();
PyErr_Clear
();
}
}
/* cache a hashed version of the attribute string */
if
(
hintstrobj
==
NULL
)
{
hintstrobj
=
PyString_InternFromString
(
"__length_hint__"
);
if
(
hintstrobj
==
NULL
)
return
-
1
;
}
/* try o.__length_hint__() */
/* try o.__length_hint__() */
ro
=
PyObject_CallMethodObjArgs
(
o
,
hintstrobj
,
NULL
);
hintmeth
=
_PyObject_LookupSpecial
(
o
,
"__length_hint__"
,
&
hintstrobj
);
if
(
hintmeth
==
NULL
)
return
defaultvalue
;
ro
=
PyObject_CallFunctionObjArgs
(
hintmeth
,
NULL
);
Py_DECREF
(
hintmeth
);
if
(
ro
==
NULL
)
{
if
(
ro
==
NULL
)
{
if
(
!
PyErr_ExceptionMatches
(
PyExc_TypeError
)
&&
if
(
!
PyErr_ExceptionMatches
(
PyExc_TypeError
))
!
PyErr_ExceptionMatches
(
PyExc_AttributeError
))
return
-
1
;
return
-
1
;
PyErr_Clear
();
return
defaultvalue
;
return
defaultvalue
;
}
}
rv
=
PyLong_Check
(
ro
)
?
PyLong_AsSsize_t
(
ro
)
:
defaultvalue
;
rv
=
PyLong_Check
(
ro
)
?
PyLong_AsSsize_t
(
ro
)
:
defaultvalue
;
...
...
Python/sysmodule.c
View file @
af1692a2
...
@@ -643,7 +643,7 @@ static PyObject *
...
@@ -643,7 +643,7 @@ static PyObject *
sys_getsizeof
(
PyObject
*
self
,
PyObject
*
args
,
PyObject
*
kwds
)
sys_getsizeof
(
PyObject
*
self
,
PyObject
*
args
,
PyObject
*
kwds
)
{
{
PyObject
*
res
=
NULL
;
PyObject
*
res
=
NULL
;
static
PyObject
*
str__sizeof__
,
*
gc_head_size
=
NULL
;
static
PyObject
*
str__sizeof__
=
NULL
,
*
gc_head_size
=
NULL
;
static
char
*
kwlist
[]
=
{
"object"
,
"default"
,
0
};
static
char
*
kwlist
[]
=
{
"object"
,
"default"
,
0
};
PyObject
*
o
,
*
dflt
=
NULL
;
PyObject
*
o
,
*
dflt
=
NULL
;
...
@@ -651,13 +651,6 @@ sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
...
@@ -651,13 +651,6 @@ sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
kwlist
,
&
o
,
&
dflt
))
kwlist
,
&
o
,
&
dflt
))
return
NULL
;
return
NULL
;
/* Initialize static variable needed by _PyType_Lookup */
if
(
str__sizeof__
==
NULL
)
{
str__sizeof__
=
PyString_InternFromString
(
"__sizeof__"
);
if
(
str__sizeof__
==
NULL
)
return
NULL
;
}
/* Initialize static variable for GC head size */
/* Initialize static variable for GC head size */
if
(
gc_head_size
==
NULL
)
{
if
(
gc_head_size
==
NULL
)
{
gc_head_size
=
PyInt_FromSsize_t
(
sizeof
(
PyGC_Head
));
gc_head_size
=
PyInt_FromSsize_t
(
sizeof
(
PyGC_Head
));
...
@@ -674,14 +667,16 @@ sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
...
@@ -674,14 +667,16 @@ sys_getsizeof(PyObject *self, PyObject *args, PyObject *kwds)
res
=
PyInt_FromSsize_t
(
PyInstance_Type
.
tp_basicsize
);
res
=
PyInt_FromSsize_t
(
PyInstance_Type
.
tp_basicsize
);
/* all other objects */
/* all other objects */
else
{
else
{
PyObject
*
method
=
_Py
Type_Lookup
(
Py_TYPE
(
o
)
,
PyObject
*
method
=
_Py
Object_LookupSpecial
(
o
,
"__sizeof__"
,
str__sizeof__
);
&
str__sizeof__
);
if
(
method
==
NULL
)
if
(
method
==
NULL
)
PyErr_Format
(
PyExc_TypeError
,
PyErr_Format
(
PyExc_TypeError
,
"Type %.100s doesn't define __sizeof__"
,
"Type %.100s doesn't define __sizeof__"
,
Py_TYPE
(
o
)
->
tp_name
);
Py_TYPE
(
o
)
->
tp_name
);
else
else
{
res
=
PyObject_CallFunctionObjArgs
(
method
,
o
,
NULL
);
res
=
PyObject_CallFunctionObjArgs
(
method
,
NULL
);
Py_DECREF
(
method
);
}
}
}
/* Has a default value been given? */
/* Has a default value been given? */
...
...
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