473,883 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a Python bug in processing __del__ method ??

Today I was playing with a small Python program using Python 2.4
on Cygwin (up-to-date version, on Windows XP), but ran into a
strange error on the following small program (named bug.py):

-------------------------------
#!/usr/bin/python

class Person:
population = 0
def __del__(self):
Person.populati on -= 1

peter = Person()
-------------------------------

The error returned is this:

$ python bug.py
Exception exceptions.Attr ibuteError: "'NoneType' object has no
attribute 'population'" in <bound method Person.__del__ of
<__main__.Perso n instance at 0xa0c9fec>> ignored

However, if I rename variable name 'peter' to something like 'peter1'
or 'david', the error is gone. Looks to me the
error only happens to variable name 'peter'.

Does anyone know what is wrong? Is this a bug only on Cygwin?
- Baoqiu

Jul 18 '05 #1
4 6544
Baoqiu Cui wrote:
The error returned is this:

$ python bug.py
Exception exceptions.Attr ibuteError: "'NoneType' object has no
attribute 'population'" in <bound method Person.__del__ of
<__main__.Perso n instance at 0xa0c9fec>> ignored

However, if I rename variable name 'peter' to something like 'peter1'
or 'david', the error is gone. Looks to me the
error only happens to variable name 'peter'.

Does anyone know what is wrong? Is this a bug only on Cygwin?


it is not a bug, and the documentation has the answer:

language reference -> index -> __del__

http://docs.python.org/ref/customization.html#l2h-175

Warning: Due to the precarious circumstances under which __del__()
methods are invoked, exceptions that occur during their execution are
ignored, and a warning is printed to sys.stderr instead. Also, when
__del__() is invoked in response to a module being deleted (e.g.,
when execution of the program is done), other globals referenced by
the __del__() method may already have been deleted. For this reason,
__del__() methods should do the absolute minimum needed to
maintain external invariants.

if you absolutely need to have reliable access to globals, you need to make
sure they're available as instance variables, or are bound in some other way.

</F>

Jul 18 '05 #2
Baoqiu Cui wrote:
Today I was playing with a small Python program using Python 2.4
on Cygwin (up-to-date version, on Windows XP), but ran into a
strange error on the following small program (named bug.py):

-------------------------------
#!/usr/bin/python

class Person:
population = 0
def __del__(self):
Person.populati on -= 1

peter = Person()
-------------------------------

The error returned is this:

$ python bug.py
Exception exceptions.Attr ibuteError: "'NoneType' object has no
attribute 'population'" in <bound method Person.__del__ of
<__main__.Perso n instance at 0xa0c9fec>> ignored

However, if I rename variable name 'peter' to something like 'peter1'
or 'david', the error is gone. Looks to me the
error only happens to variable name 'peter'.

Does anyone know what is wrong? Is this a bug only on Cygwin?


I get this error on a normal Python WinXP install too. Looks like
Python deletes globals in order when exiting, so the Person class gets
deleted before the Person instances:

---------- bug.py ----------
class Person(object):
population = 0
def __del__(self):
Person.populati on -= 1

<name> = Person()
print list(globals())
----------------------------

I tried this with a few different names. Here are the results for
'apple', 'banana' and 'cranberry'. Only when the name appears after
'Person' in the globals do I get the error that you get.

[D:\Steve]$ bug.py
['apple', '__builtins__', '__file__', 'Person', '__name__', '__doc__']

[D:\Steve]$ bug.py
['__builtins__', '__file__', 'banana', 'Person', '__name__', '__doc__']

[D:\Steve]$ bug.py
['__builtins__', '__file__', 'Person', 'cranberry', '__name__', '__doc__']
Exception exceptions.Attr ibuteError: "'NoneType' object has no attribute
'population'" in <bound method Person.__del__ of <__main__.Perso n object
at 0x009D1A50>> ignored

I don't know whether this is a bug or a "feature". ;)

Steve
Jul 18 '05 #3
Fredrik Lundh wrote:
Baoqiu Cui wrote:
The error returned is this:

$ python bug.py
Exception exceptions.Attr ibuteError: "'NoneType' object has no
attribute 'population'" in <bound method Person.__del__ of
<__main__.Per son instance at 0xa0c9fec>> ignored

However, if I rename variable name 'peter' to something like 'peter1'
or 'david', the error is gone. Looks to me the
error only happens to variable name 'peter'.

Does anyone know what is wrong? Is this a bug only on Cygwin?


it is not a bug, and the documentation has the answer:

language reference -> index -> __del__

http://docs.python.org/ref/customization.html#l2h-175

Warning: Due to the precarious circumstances under which __del__()
methods are invoked, exceptions that occur during their execution are
ignored, and a warning is printed to sys.stderr instead. Also, when
__del__() is invoked in response to a module being deleted (e.g.,
when execution of the program is done), other globals referenced by
the __del__() method may already have been deleted. For this reason,
__del__() methods should do the absolute minimum needed to
maintain external invariants.

if you absolutely need to have reliable access to globals, you need to make
sure they're available as instance variables, or are bound in some other way.


Along these lines, I think changing your code to:

class Person(object):
population = 0
def __del__(self):
self.__class__. population -= 1

peter = Person()

solves the problem. (At least it did for me.)

Steve
Jul 18 '05 #4
Fredrik and Steve,

Thank you so much for the help. Now I know more about Python. :-)

Steve's test does explain why 'peter1' is OK while 'peter' is not:
'peter1' appears
before 'Person' in the globals while 'peter' is after.

The error message is just a little confusing to a Python newbie, I
think.

Thanks again!

- Baoqiu

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4765
by: Pierre Rouleau | last post by:
As much as I love Python, I sometimes find myself wishing Python supported the RAII idiom (resource acquisition is initialization) that is available in C++, the emerging D language, and others. In some situations (like controlling non-memory resources) it is nice to be able to create an object that will execute some code on its destruction. For example, an object that controls access to a critical section: the creation of the object...
1
4823
by: Rajorshi | last post by:
Hi, I have two classes, say, Base1 and Base2. Now Derv is a class derived from both like class Derv(Base1,Base2)..... In the destructor of the derived class, if I write something like this def __del__(self): for b in self.__class__.__bases__: b.__del__(self)
2
2614
by: Manlio Perillo | last post by:
Hi. This post follows "does python have useless destructors". I'm not an expert, so I hope what I will write is meaningfull and clear. Actually in Python there is no possibility to write code that follows C++ RAII pattern. Of course Python objects are not statics like in C++, but in C++ the auto_ptr class is used for enforcing this pattern for dynamical
0
2735
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24 to obtain SHIP_wrap.cpp and SHIP.py; the latter contains the line "import _SHIP". I compile SHIP_wrap.cpp and a bunch of files into a DLL which I have the
2
1699
by: skip | last post by:
We encountered a situation today where it appeared that a Boost.Python-provided class didn't participate in Python's cyclic garbage collection. The wrapped C++ instance held a reference to a method in the Python object which referenced the Boostified C++ instance, e.g.: class Foo: def __init__(self, ...): self.over_there = BoostifiedClass() self.over_there.set_callback(self.boost_callback) ...
0
9942
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9792
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10743
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9574
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4220
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3233
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.