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
a361d317
Commit
a361d317
authored
Jun 11, 2011
by
Benjamin Peterson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow __dir__ to return any sequence
parent
c16b21e6
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
29 additions
and
32 deletions
+29
-32
Doc/reference/datamodel.rst
Doc/reference/datamodel.rst
+2
-1
Lib/test/test_builtin.py
Lib/test/test_builtin.py
+9
-1
Misc/NEWS
Misc/NEWS
+3
-0
Objects/object.c
Objects/object.c
+15
-30
No files found.
Doc/reference/datamodel.rst
View file @
a361d317
...
...
@@ -1343,7 +1343,8 @@ access (use of, assignment to, or deletion of ``x.name``) for class instances.
.. method:: object.__dir__(self)
Called when :func:`dir` is called on the object. A list must be returned.
Called when :func:`dir` is called on the object. A sequence must be
returned. :func:`dir` converts the returned sequence to a list and sorts it.
.. _descriptors:
...
...
Lib/test/test_builtin.py
View file @
a361d317
...
...
@@ -372,7 +372,15 @@ class BuiltinTest(unittest.TestCase):
f
=
Foo
()
self
.
assertTrue
(
dir
(
f
)
==
[
"ga"
,
"kan"
,
"roo"
])
# dir(obj__dir__not_list)
# dir(obj__dir__tuple)
class
Foo
(
object
):
def
__dir__
(
self
):
return
(
"b"
,
"c"
,
"a"
)
res
=
dir
(
Foo
())
self
.
assertIsInstance
(
res
,
list
)
self
.
assertTrue
(
res
==
[
"a"
,
"b"
,
"c"
])
# dir(obj__dir__not_sequence)
class
Foo
(
object
):
def
__dir__
(
self
):
return
7
...
...
Misc/NEWS
View file @
a361d317
...
...
@@ -10,6 +10,9 @@ What's New in Python 3.3 Alpha 1?
Core and Builtins
-----------------
- Loosen type restrictions on the __dir__ method. __dir__ can now return any
sequence, which will be converted to a list and sorted by dir().
- Issue #12265: Make error messages produced by passing an invalid set of
arguments to a function more informative.
...
...
Objects/object.c
View file @
a361d317
...
...
@@ -1205,6 +1205,10 @@ _dir_locals(void)
Py_DECREF
(
names
);
return
NULL
;
}
if
(
PyList_Sort
(
names
))
{
Py_DECREF
(
names
);
return
NULL
;
}
/* the locals don't need to be DECREF'd */
return
names
;
}
...
...
@@ -1213,7 +1217,7 @@ _dir_locals(void)
static
PyObject
*
_dir_object
(
PyObject
*
obj
)
{
PyObject
*
result
;
PyObject
*
result
,
*
sorted
;
static
PyObject
*
dir_str
=
NULL
;
PyObject
*
dirfunc
=
_PyObject_LookupSpecial
(
obj
,
"__dir__"
,
&
dir_str
);
...
...
@@ -1228,18 +1232,16 @@ _dir_object(PyObject *obj)
Py_DECREF
(
dirfunc
);
if
(
result
==
NULL
)
return
NULL
;
/* result must be a list */
/* XXX(gbrandl): could also check if all items are strings */
if
(
!
PyList_Check
(
result
))
{
PyErr_Format
(
PyExc_TypeError
,
"__dir__() must return a list, not %.200s"
,
Py_TYPE
(
result
)
->
tp_name
);
Py_DECREF
(
result
);
result
=
NULL
;
/* return sorted(result) */
sorted
=
PySequence_List
(
result
);
Py_DECREF
(
result
);
if
(
sorted
==
NULL
)
return
NULL
;
if
(
PyList_Sort
(
sorted
))
{
Py_DECREF
(
sorted
);
return
NULL
;
}
return
result
;
return
sorted
;
}
/* Implementation of dir() -- if obj is NULL, returns the names in the current
...
...
@@ -1249,24 +1251,7 @@ _dir_object(PyObject *obj)
PyObject
*
PyObject_Dir
(
PyObject
*
obj
)
{
PyObject
*
result
;
if
(
obj
==
NULL
)
/* no object -- introspect the locals */
result
=
_dir_locals
();
else
/* object -- introspect the object */
result
=
_dir_object
(
obj
);
assert
(
result
==
NULL
||
PyList_Check
(
result
));
if
(
result
!=
NULL
&&
PyList_Sort
(
result
)
!=
0
)
{
/* sorting the list failed */
Py_DECREF
(
result
);
result
=
NULL
;
}
return
result
;
return
(
obj
==
NULL
)
?
_dir_locals
()
:
_dir_object
(
obj
);
}
/*
...
...
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