[Tutor] Getting caller name without the help of "sys._getframe(1).f_code.co_name" ?

Luke Paireepinart rabidpoobear at gmail.com
Sun Feb 14 13:10:20 CET 2010


I see why you would want the error messages but why is the default error
message not enough, that is why I am curious, and typically introspection on
objects is not necessary (for example, people often want to convert a string
into a variable name to store a value (say they have the string "foobar1"
and they want to store the value "f" in the variable "foobar1", how do they
change foobar1 to reference a string?  well you can just use exec but the
core issue is that there's really no reason to do it in the first place,
they can just use a dictionary and store dict['foobar1'] = 'f'  and it is
functionally equivalent (without the danger in the code)).  I get the
feeling that your issue is the same sort of thing, where an easier solution
exists but for whatever reason you don't see it.  I don't know if this is
true or not.  Here's my take on this:

>>> class x(object):
    def __init__(self, fname):
        self.temp = open(fname).read()


>>> a = x('foobar')

Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>               # this is the
module it's in
    a = x('foobar')                                                    #
this is the line where I tried to initialize it
  File "<pyshell#15>", line 3, in __init__                  # which called
this function, which is the one that has the error
    self.temp = open(fname).read()                          #and the error
occurred while trying to perform this operation
IOError: [Errno 2] No such file or directory: 'foobar'  #and the error was
that the file 'foobar' could not be found.


This is implicitly stated that 'x' is the class with the method that had the
issue, and it was specifically the __init__ method, and the file that could
not be read was called 'foobar'.  How does this not satisfy your
requirements?  Is it the form of the output that you do not agree with (you
want it all on one line?)

I've never had issues with Python's exception statements, I've always had no
trouble finding where exceptions occurred, and I'm not sure what information
is missing from the traceback that you'd like to convey to the user.


On Sun, Feb 14, 2010 at 5:33 AM, patrice laporte <zepangolin at gmail.com>wrote:

>
> 2010/2/13 Luke Paireepinart <rabidpoobear at gmail.com>
>
>
>>
>> On Sat, Feb 13, 2010 at 9:56 AM, patrice laporte <zepangolin at gmail.com>wrote:
>>
>>>
>>> Hi,
>>>
>>> Being in an exeption of my own, I want to print the name of the caller,
>>> and I'm looking for a way to have it.
>>>
>>> Could you tell us exactly why you want to do this?  It seems sort of
>> strange.  Also couldn't you pass the caller as an argument to your function?
>>
>>
> Hi,
>
> I don't know if it's strange, maybe, so tell me why. Or maybe it's me....
> Maybe the fact I'm à pure C coder for a longtime prevent me from thinking in
> Python, maybe my brain is only capable of thinking and viewing in C..... and
> maybe there's an obvious way to solve my problem that I can't see yet... or
> maybe it's because I'm French, genetically programed to live in the past ?
> ... so this mailing list is my starting point to improve my Python
> understanding.
>
> And now, something different : what I want to do, and why.
>
> I got a class that takes a file name in its  __init__ method (it could be
> elsewhere, but why not here ?). Then, somewhere in that class, a method will
> do something with that file  name, such as "try to open that file".
>
> If the file do esn't exist, bing ! I got an exception "I/O Error n°2 : file
> doesn't exist".
>
> That's nice, I of course catch this exception, but it's not enough for the
> user : What file are we talking about ?  And how to tell the user  what is
> that file, and make him understand  he tell the app to use a file that
> doesn't exist ?
> And this is not enough for developer : where that error happened ? what
> class ? what method ?
>
> There is many solution, and mine is :
>
> - don't check for the existence of the file in __init__
> - don't use a default value for the file name parameter in __init__ : coder
> MUST provide it.
> - try/catch open(this_file_name) and if I got an IOErro exception, I
> re-raise my own exception with :
>    - the name of the class where that IOError occured
>    - the name of the method in that class that make that error occured
>    - the name of the file the method tried to opened
>    - some other info to help diagnostic
>
> Then my exception object can :
>    - log in a file dedicated to the team (so, just me for the moment)
>    - and/or log in a file dedicated to the user
>
> And then the app can popup a message box or something like that.
>
> Currently, I'm thinking about "how to get that class/method name", easily,
> and make something usefull with that.
>
> I don't want to write the name of the method and the class each time I need
> to use my exception class, and I don't want to modify that name each time
> I'm doing a refactoring operation : I just want to do something like :
>
>    except IOError, (errno, errmes):
>             raise myexceptioniwanttobenice ("IO Error %d -> %s ('%s')" %
> (errno, errmes, self.__csv) )
>
> And then I print somewhere a message like :
>
>           Error in csv2db.getColumnFromCsv : IO Error 2 -> No such file or
> directory ('toto')
>
> What I don't want is to write something like :
>
> except IOError, (errno, errmes):
>             raise myexceptioniwanttobenice ("cvs2db.getColumnFromCsv IO
> Error %d -> %s ('%s')" % (errno, errmes, self.__csv) )
>
> I don't want because it's to much to write, and if I change the class name,
> I don't want to rewrite all my call to change that name.
>
> So, I was looking for a way to retrieve :
> The name of the class the object that throw the exception belongs to
> The name of the method in that class
>
> And I finally found those recipe with the sys._getframe call.
>
> My first solution, currently, is (I didn't yet rewrite it with the solution
> with the traceback module) :
>
> class argError(Exception):
>     def __init__(self, callerClass, errorMessage):
>         self.__class   = callerClass.__class__.__name__
>         self.__method  = sys._getframe(1).f_code.co_name
>         self.__message = errorMessage
>
>     def __str__(self):
>         return "Error in %s.%s : %s" % (self.__class, self.__method,
> self.__message)
>
> And how I use it :
>
>        try:
>             f = open(self.__csv)
>         except IOError, (errno, errmes):
>             raise argError (self, "IO Error %d -> %s ('%s')" % (errno,
> errmes, self.__csv) )
>
> I now will :
> - rewrite it with the solution with traceback module
> - read about that module and find how to get rid of the need to pass "self"
> as an argument in the call "raise argError (*self*, "IO Error %d -> %s
> ('%s')" % (errno, errmes, self.__csv) )". That call enable me to get the
> name of the class.
>
> All of that is a way to me to experiment Python, I try a lot of thing to
> understand, and then I will make a choice.
>
> But now, it's time to lunch. Did I gave you enough information about what I
> want ?
>
> Patrice
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100214/f689be63/attachment-0001.htm>


More information about the Tutor mailing list