Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #19134

Re: importing and nose

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.chainon-marquant.org!nntpfeed.proxad.net!proxad.net!feeder1-1.proxad.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'suppose': 0.05; 'exec': 0.07; 'nameerror:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'def': 0.13; '"global"': 0.16; '(module)': 0.16; '70,': 0.16; 'clue?': 0.16; "method's": 0.16; 'namespace,': 0.16; 'namespace.': 0.16; 'prof': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'setup(self):': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'trying': 0.20; '(most': 0.21; "doesn't": 0.22; 'from:addr:web.de': 0.23; 'tests.': 0.23; 'defined': 0.24; 'traceback': 0.24; 'code': 0.25; 'seconds': 0.25; 'function': 0.27; 'import': 0.27; 'looks': 0.27; "i'm": 0.27; 'skip:" 30': 0.28; 'script': 0.28; 'print': 0.29; 'skip:p 30': 0.29; 'class': 0.29; 'cmd': 0.30; 'fails,': 0.30; 'actually': 0.32; 'to:addr:python-list': 0.33; 'last):': 0.34; 'header:X-Complaints-To:1': 0.34; 'running': 0.35; 'test': 0.35; 'file': 0.35; 'run': 0.37; 'but': 0.37; 'received:org': 0.37; 'another': 0.37; 'some': 0.38; 'sense': 0.39; 'subject:: ': 0.39; 'to:addr:python.org': 0.40; 'you.': 0.64; 'believe': 0.65; 'profile': 0.73; 'demo': 0.80; "'foo'": 0.84; '0.000': 0.84; 'andrea': 0.84; 'dict,': 0.84; 'locals': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: importing and nose
Date Thu, 19 Jan 2012 18:36:27 +0100
Organization None
References <4F184EAF.5080603@gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084bab4.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4868.1326994610.27778.python-list@python.org> (permalink)
Lines 64
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1326994610 news.xs4all.nl 6881 [2001:888:2000:d::a6]:38685
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:19134

Show key headers only | View raw


Andrea Crotti wrote:

> I'm writing some code to analyse pstats statistics, and I'm trying to
> have some working unit tests.
> Suppose I have in the test directory another directory 'profiling',
> which contains 'x.py', and 'b.py'.
> 
> Now running the following code in a script works perfectly,

I don't believe you.

> class TestStatParser(unittest.TestCase):
> 
>      def setUp(self):
>          self.temp_file = tempfile.mktemp()
>          prof_path = path.join(path.dirname(__file__), 'profiling')
>          sys.path.append(prof_path)
>          import x
>          profile.run('x.f1()', filename=self.temp_file)
> 
> 
> But running it within nose I get the following
> 
>      exec cmd in globals, locals
>    File "<string>", line 1, in <module>
> NameError: name 'x' is not defined
> 
> 
> Which doesn't make sense to me, because the import doesn't actually
> fails, so
> how can x not be defined???
> 
> Any clue?

profile.run() doesn't have access to the setUp() method's local namespace, 
it looks into the global (module) namespace. 
A stripped-down example:

>>> import profile
>>> def demo():
...     def foo(): print "local"
...     profile.run("foo()")
...
>>> demo()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in demo
  File "/usr/lib/python2.6/profile.py", line 70, in run
    prof = prof.run(statement)
  File "/usr/lib/python2.6/profile.py", line 456, in run
    return self.runctx(cmd, dict, dict)
  File "/usr/lib/python2.6/profile.py", line 462, in runctx
    exec cmd in globals, locals
  File "<string>", line 1, in <module>
NameError: name 'foo' is not defined
>>> def foo():
...     print "global"
...
>>> demo()
global
         4 function calls in 0.000 CPU seconds
[snip]

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: importing and nose Peter Otten <__peter__@web.de> - 2012-01-19 18:36 +0100

csiph-web