473,406 Members | 2,336 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

atexit.register does not return the registered function. IMHO, it should.

Hi all!

Since that the decorator syntax is upon us, I think it would be good if
atexit.register() was returning the function passed as argument. This
simple change to the library would solve a problem with the use of
atexit.register as a decorator (and I can't think of any use case where
this change would break any code).

I describe the problem in the following text.

Problem using atexit.register as a decorator
============================================

In his April 2005 article titled `Python 2.4 Decorators: Reducing code
duplication and consolidating knowledge`_ , Phillip Eby describes how
you can use `atexit.register()`_ from the standard Python library. He
shows how to use the decorator syntax to register a function that will
execute at program termination. Here is how it goes::

@atexit.register
def goodbye():
print "Goodbye, terminating..."
However, there is one fundamental problem with this: atexit.register()
returns None. Since the above code corresponds to::
def goodbye():
print "Goodbye, terminating..."
goodbye = atexit.register(goodbye)

the code registers goodbye but right after it binds goodbye to None!
You can see this in the following session::
>>import atexit
@atexit.register
... def goodbye():
... print "Goodbye, terminating..."
...
>>goodbye()
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: 'NoneType' object is not callable
>>>
goodbye
type(goodbye)
<type 'NoneType'>
>>>
There is two solutions to this problem:

1. Use another function to register and decorate.
2. Change atexit.register() in the Python library so that
it returns the function it registers.

Solution 1 can be implemented right away::

def atexit_register(fct):
atexit.register(fct)
return fct

@atexit_register
def goodbye2():
print "Goodbye 2!!"

and it works: it registers the function for execution at termination
but leaves goodbye2 callable::
>>def atexit_register(fct):
... atexit.register(fct)
... return fct
...
>>@atexit_register
... def goodbye2():
... print "Goodbye 2!!"
...
>>goodbye2()
Goodbye 2!!
>>goodbye2
<function goodbye2 at 0x009DD930>
>>>
... References

... _atexit.register():
http://www.python.org/doc/current/li...le-atexit.html
... _Python 2.4 Decorators\: Reducing code duplication and consolidating
knowledge: http://www.ddj.com/184406073

Nov 16 '06 #1
5 1877
Since that the decorator syntax is upon us, I think it would be good if
atexit.register() was returning the function passed as argument. This
simple change to the library would solve a problem with the use of
atexit.register as a decorator (and I can't think of any use case where
this change would break any code).
....

Can you submit a bug report to the SourceForge bug tracker? I'll take
care of the problem when I have access to the subversion repository.

Skip
Nov 16 '06 #2
On 11/16/06, Skip Montanaro <sk************@gmail.comwrote:
Since that the decorator syntax is upon us, I think it would be good if
atexit.register() was returning the function passed as argument. This
simple change to the library would solve a problem with the use of
atexit.register as a decorator (and I can't think of any use case where
this change would break any code).
...

Can you submit a bug report to the SourceForge bug tracker? I'll take
care of the problem when I have access to the subversion repository.
Done: http://sourceforge.net/tracker/index...70&atid=105470

I select Python 2.5 as the category. It affects all versions but most
likely to cause a problem in Python 2.4 and after.

--

Pierre R.
Nov 16 '06 #3
On Thu, 2006-11-16 at 08:03 -0800, pr*********@gmail.com wrote:
@atexit.register
def goodbye():
print "Goodbye, terminating..."
However, there is one fundamental problem with this: atexit.register()
returns None. Since the above code corresponds to::
def goodbye():
print "Goodbye, terminating..."
goodbye = atexit.register(goodbye)

the code registers goodbye but right after it binds goodbye to None!
While it wouldn't hurt to have atexit.register return the function it
registered, this "problem" is only a problem if you wish to call the
function manually, since atexit already registered the reference to the
intended function before your reference to it gets rebound to None.
Normally one would register a function with atexit precisely because
they don't want to call it manually.

-Carsten
Nov 16 '06 #4


On Nov 16, 11:38 am, Carsten Haese <cars...@uniqsys.comwrote:
On Thu, 2006-11-16 at 08:03 -0800, prouleau...@gmail.com wrote:
@atexit.register
def goodbye():
print "Goodbye, terminating..."
However, there is one fundamental problem with this: atexit.register()
returns None. Since the above code corresponds to::
def goodbye():
print "Goodbye, terminating..."
goodbye = atexit.register(goodbye)
the code registers goodbye but right after it binds goodbye to None!While it wouldn't hurt to have atexit.register return the function it
registered, this "problem" is only a problem if you wish to call the
function manually, since atexit already registered the reference to the
intended function before your reference to it gets rebound to None.
Normally one would register a function with atexit precisely because
they don't want to call it manually.
There are *two* problems.

1 - As you said, most of the time you would not call the function
explicitly, but in some situation you might want to.
2- If you want to document your code using introspection, or use an
IDE to look at the function, if the function disappeared from sight,
you won't be able to.
The second problem is similar to what happens when a decorator changes
the signature of a function.

--

P.R.

Nov 16 '06 #5
Skip Montanaro schrieb:
>Since that the decorator syntax is upon us, I think it would be good if
atexit.register() was returning the function passed as argument. This
simple change to the library would solve a problem with the use of
atexit.register as a decorator (and I can't think of any use case where
this change would break any code).
...

Can you submit a bug report to the SourceForge bug tracker? I'll take
care of the problem when I have access to the subversion repository.
Sorry, didn't read this thread before the bug report, which is why I
already handled this one ;)

cheers,
Georg
Nov 16 '06 #6

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

Similar topics

0
by: Bengt Richter | last post by:
Still looking for a unifying concept ;-) It struck me that @deco1 @deco2 def foo():pass uses '@' to register at-next-name-binding processing in a way similar to atexit's registering of...
3
by: Serve Laurijssen | last post by:
Does the C++ standard say whether destructors are called before or after "atexit" functions are called. Or is nothing said about that?
2
by: Steve Lambert | last post by:
Hi, Is it possible for the functions registered with atexit() to have access to parameter to exit i.e EXIT_SUCCESS or EXIT_FAILURE? I'd like to register a set of routines to be executed on...
7
by: David Rushby | last post by:
Consider the following program (underscores are used to force indentation): ------------------------------------------------ import atexit, threading, time def atExitFunc(): ____print...
20
by: Aek | last post by:
We recently moved our large codebase over from VS7 to 8 and found that we now get access violations in atexit calls at shutdown when debugging the application in VS2005. This occurs in static...
3
by: carl.dhalluin | last post by:
Hi, I am playing with the atexit module but I don't find a way to see the difference between a script calling sys.exit(<returncode>) and the interpreting arriving at the end of the source code...
16
by: Laurent Deniau | last post by:
I would like to know if the use of the pointer ref in the function cleanup() below is valid or if something in the norm prevents this kind of cross-reference during exit(). I haven't seen anything...
2
by: Christopher Pisz | last post by:
I am attempting to write a "Phoenix Singleton" using the book "Modern C++ Design" by Alexandrescu I do not understand his use of... #ifndef ATEXIT_FIXED std::atexit(Kill); #endif ....in the...
18
by: lak | last post by:
I am studying the Advanced programming in the unix environment. There they says that we can register upto 32 functions with atexit(). Why that is limited to 32 functions? can any one tell the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.