Wow: list of immediate subclasses?

Roman Suzi rnd at onego.ru
Fri Aug 1 06:19:23 CEST 2003


I am very surprised that modern classes (inherited from object)
in Python 2.3 show their ancestry, eg:

>>> object.__subclasses__()
[<type 'type'>, <type 'int'>, <type 'basestring'>, <type 'list'>, <type 
'NoneType'>, <type 'NotImplementedType'>, <type 'module'>, <type 
'zipimport.zipimporter'>, <type 'posix.stat_result'>, <type 
'posix.statvfs_result'>, <type 'dict'>, <type 'function'>, <type 'dictproxy'>, 
<type 'member_descriptor'>, <type 'getset_descriptor'>, <type 'tuple'>, <type 
'wrapper_descriptor'>, <type 'builtin_function_or_method'>, <type 
'method_descriptor'>, <type 'file'>]

This seems cool introspection tool but raises questions:

1- I wonder if it has security consequences (knowing what other
classes are in there, probably in some other modules). As it adds
a hole starting from base class "object". Earlier it was possible
to "chroot" to some custom builtin namespace and provide
restricted execution.

2- how do I get rid of a class completely?

This doesn't work:

>>> object.__subclasses__()
[<type 'type'>, <type 'int'>, <type 'basestring'>, <type 'list'>, <type 
'NoneType'>, <type 'NotImplementedType'>, <type 'module'>, <type 
'zipimport.zipimporter'>, <type 'posix.stat_result'>, <type 
'posix.statvfs_result'>, <type 'dict'>, <type 'function'>]
>>> class A(object): pass
...
>>> object.__subclasses__()
[<type 'type'>, <type 'int'>, <type 'basestring'>, <type 'list'>, <type 
'NoneType'>, <type 'NotImplementedType'>, <type 'module'>, <type 
'zipimport.zipimporter'>, <type 'posix.stat_result'>, <type 
'posix.statvfs_result'>, <type 'dict'>, <type 'function'>, <class 
'__main__.A'>]
>>> del A
>>> object.__subclasses__()
[<type 'type'>, <type 'int'>, <type 'basestring'>, <type 'list'>, <type 
'NoneType'>, <type 'NotImplementedType'>, <type 'module'>, <type 
'zipimport.zipimporter'>, <type 'posix.stat_result'>, <type 
'posix.statvfs_result'>, <type 'dict'>, <type 'function'>, <class 
'__main__.A'>]
>>>

Sincerely yours, Roman Suzi
-- 
rnd at onego.ru =\= My AI powered by GNU/Linux RedHat 7.3






More information about the Python-list mailing list