[PyPy-issue] [issue686] PyPy dir() implementation differs from CPython's

Vetoshkin Nikita pypy-dev-issue at codespeak.net
Mon Apr 18 09:04:05 CEST 2011

New submission from Vetoshkin Nikita <nikita.vetoshkin at gmail.com>:

Here's a bit patological case
class AsString(object):
	def __getattribute__(self, name):
		return name

print dir(AsString())

Passes on cpython, failes on PyPy. I raised this issue once on #pypy, but it's 
still there 
:) So - here's a ticket. PyPy calls obj.__dir__ instead of type(obj).__dir__
Patch is pretty straighforward:

diff -r 2d96e93e4a47 pypy/module/__builtin__/app_inspect.py
--- a/pypy/module/__builtin__/app_inspect.py    Mon Apr 18 08:07:53 2011 +0200
+++ b/pypy/module/__builtin__/app_inspect.py    Mon Apr 18 12:40:17 2011 +0600
@@ -76,8 +76,8 @@
         return result

-    elif hasattr(obj, '__dir__'):
-        result = obj.__dir__()
+    elif hasattr(type(obj), '__dir__'):
+        result = type(obj).__dir__()
         if not isinstance(result, list):
             raise TypeError("__dir__() must return a list, not %r" % (

But example still failes, cause cpython 
(http://hg.python.org/cpython/file/ac563fba7161/Objects/object.c#l1315) makes a 
new dict if 
__dict__ isn't a dictionary - pypy doesn't.

effort: easy
messages: 2373
nosy: nekto0n, pypy-issue
priority: bug
release: ???
status: unread
title: PyPy dir() implementation differs from CPython's

PyPy development tracker <pypy-dev-issue at codespeak.net>

More information about the Pypy-issue mailing list