Re: How to find out in which module an instance of a class is created?

2009-08-16 Thread Chris Rebert
On Sun, Aug 16, 2009 at 6:25 AM, Johannes
Janssenm...@johannes-janssen.de wrote:
 Gabriel Genellina schrieb:

 The try/except around sys._getframe(1) is because that function is not
 mandatory/available on all Python implementations (that's the case for
 jython which doesn't provide it).

 Thanks, shouldn't such information be part of the python documentation of
 sys._getframe()
 (http://docs.python.org/library/sys.html?highlight=sys._getframe#sys._getframe)?

The leading underscore kinda indirectly implies it, but yeah, it's
worth mentioning.
File a bug in the docs: http://bugs.python.org/

Cheers,
Chris
-- 
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to find out in which module an instance of a class is created?

2009-08-16 Thread Johannes Janssen

Gabriel Genellina schrieb:
The try/except around sys._getframe(1) is because that function is not 
mandatory/available on all Python implementations (that's the case for 
jython which doesn't provide it).


Thanks, shouldn't such information be part of the python documentation 
of sys._getframe() 
(http://docs.python.org/library/sys.html?highlight=sys._getframe#sys._getframe)?


Johannes
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to find out in which module an instance of a class is created?

2009-08-16 Thread Johannes Janssen

Chris Rebert schrieb:

On Sun, Aug 16, 2009 at 6:25 AM, Johannes
Janssenm...@johannes-janssen.de wrote:
  

Gabriel Genellina schrieb:


The try/except around sys._getframe(1) is because that function is not
mandatory/available on all Python implementations (that's the case for
jython which doesn't provide it).
  

Thanks, shouldn't such information be part of the python documentation of
sys._getframe()
(http://docs.python.org/library/sys.html?highlight=sys._getframe#sys._getframe)?



The leading underscore kinda indirectly implies it, but yeah, it's
worth mentioning.
File a bug in the docs: http://bugs.python.org/

Cheers,
Chris
  

I filed a bug: http://bugs.python.org/issue6712 .

Johannes
--
http://mail.python.org/mailman/listinfo/python-list


Re: How to find out in which module an instance of a class is created?

2009-08-13 Thread Gabriel Genellina
En Mon, 10 Aug 2009 18:51:59 -0300, Johannes Janssen  
m...@johannes-janssen.de escribió:



class A(object):
def __init__(self, mod=None):
if mod is None:
self.mod = sys._getframe(1).f_globals['__name__']
else:
self.mod = mod

In warnings.warn() they used try around sys._getframe(1). As far as I  
understand what is done in warnings, there it is not sure what object  
caused the warning and therefore it is not sure whether you can or  
cannot use sys._getframe(1). Though in my case it should be quite clear.  
Can I be sure that my code will always work?


The try/except around sys._getframe(1) is because that function is not  
mandatory/available on all Python implementations (that's the case for  
jython which doesn't provide it).


--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list


Re: How to find out in which module an instance of a class is created?

2009-08-10 Thread Johannes Janssen

Christian Heimes schrieb:

Johannes Janssen wrote:

  class A(object):
  def __init__(self, mod=__name__):
  self.mod = mod

 won't work. In this case mod would always be foo.


You have to inspect the stack in order to get the module of the 
caller. The implementation of warnings.warn() gives you some examples 
how to get the module name.


Christian

Thanks for the quick and very helpful reply. Basically just copying from 
warnings.warn(), I came up with this:


import sys

class A(object):
   def __init__(self, mod=None):
   if mod is None:
   self.mod = sys._getframe(1).f_globals['__name__']
   else:
   self.mod = mod

In warnings.warn() they used try around sys._getframe(1). As far as I 
understand what is done in warnings, there it is not sure what object 
caused the warning and therefore it is not sure whether you can or 
cannot use sys._getframe(1). Though in my case it should be quite clear. 
Can I be sure that my code will always work?


Johannes
--
http://mail.python.org/mailman/listinfo/python-list