我正在学习Erica Sadun的iPhone开发者手册中的iPhone编程.当我按照模拟器中第81页开始的温度转换示例中的步骤运行我创建的应用程序时,它会因未捕获的异常而终止.(有关我发布到iPhoneSDK Google 网上论坛的问题,请参阅http://groups.google.com/group/iphonesdk/browse_frm/thread/6f44a90fdb8da28a?hl=en.)
从main()调用UIApplicationMain()后抛出异常.如果我查看调试器中的堆栈跟踪,我看到的只是(当然)汇编.我怎样才能知道抛出了什么样的异常?
更新:
从调试器控制台了解异常的详细信息足以帮助我解决问题.(请参阅http://groups.google.com/group/iphonesdk/browse_frm/thread/6f44a90fdb8da28a?hl=en.)我确认我可以设置符号断点objc_exception_throw,但我没有查看是否有回溯来自会有所帮助.
如何编写一个装饰器,将当前工作目录恢复到调用修饰函数之前的状态?换句话说,如果我在执行a的函数上使用装饰器,则在调用函数os.chdir()后不会更改cwd.
我正在尝试挑选我定义的(新式)类的对象.但是我收到以下错误:
>>> with open('temp/connection.pickle','w') as f:
... pickle.dump(c,f)
...
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python2.5/pickle.py", line 1362, in dump
Pickler(file, protocol).dump(obj)
File "/usr/lib/python2.5/pickle.py", line 224, in dump
self.save(obj)
File "/usr/lib/python2.5/pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "/usr/lib/python2.5/pickle.py", line 419, in save_reduce
save(state)
File "/usr/lib/python2.5/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.5/pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "/usr/lib/python2.5/pickle.py", line 663, in _batch_setitems
save(v)
File …Run Code Online (Sandbox Code Playgroud) 你将什么插入到Python源代码中以使其进入pdb(当执行到达那个位置时)?
我正在使用UnitTest ++来允许我为某些C++代码(应该在Linux或Mac OS X上构建)创建单元测试.我有这样的目录结构:
src
- Foo.cpp
- Bar.cpp
test
- FooTest.cpp
- BarTest.cpp
- Main.cpp
- Makefile
UnitTest++
- libUnitTest++.a
Run Code Online (Sandbox Code Playgroud)
这个Makefile(改编自UnitTest ++ Makefile)可以很好地工作(使用GNU make):
test = TestFooAndBar
src = ../src/Foo.cpp \
../src/Bar.cpp
test_src = Main.cpp \
FooTest.cpp \
BarTest.cpp
lib = ../UnitTest++/libUnitTest++.a
objects = $(patsubst %.cpp,%.o,$(src))
test_objects = $(patsubst %.cpp,%.o,$(test_src))
.PHONY: all
all: $(test)
@echo Running unit tests...
@./$(test)
$(test): $(lib) $(test_objects) $(objects)
@echo Linking $(test)...
@$(CXX) $(LDFLAGS) -o $(test) $(test_objects) $(objects) $(lib)
.PHONY: clean
clean:
-@$(RM) -f $(objects) …Run Code Online (Sandbox Code Playgroud) 我更新了我的问题" 调整UILabel(在iPhone SDK中)以适应? ",并在建议的解决方案中描述了我的问题,但没有得到答案.也许我会通过提出一个新问题来获得更好的结果......
我在以下代码后设置了一个断点:
NSString *theText = @"When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them with another, and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.";
CGSize …Run Code Online (Sandbox Code Playgroud) 我的SUT看起来像:
foo.py
bar.py
tests/__init__.py [empty]
tests/foo_tests.py
tests/bar_tests.py
tests/integration/__init__.py [empty]
tests/integration/foo_tests.py
tests/integration/bar_tests.py
Run Code Online (Sandbox Code Playgroud)
当我跑步时nosetests --with-coverage,我得到了我宁愿忽略的各种模块的细节.但是我不能使用该
--cover-package=PACKAGE选项,因为foo.py&bar.py不在包中.(请参阅http://lists.idyll.org/pipermail/testing-in-python/2008-November/001091.html之后的帖子,
以了解我没有将它们放入包中的原因.)
我可以将coverage输出限制为foo.py和bar.py吗?
更新 - 假设没有比下面的Nadia更好的答案,我问了一个跟进问题:"如何编写一些(bash)shell脚本将目录中所有匹配的文件名转换为命令行选项? "
我正在尝试在Ubuntu 10.04 LTS上构建和安装M2Crypto.我下载并解压缩了M2Crypto-0.20.2.tar,并从我试过的M2Crypto-0.20.2目录下载python setup.py build.我收到错误,因为我没有swig.所以我跑了sudo apt-get install swig.然后我python setup.py build再次尝试并得到:
/usr/lib/python2.6/distutils/dist.py:266: UserWarning: Unknown distribution option: 'test_suite'
warnings.warn(msg)
running build
running build_py
running build_ext
building 'M2Crypto.__m2crypto' extension
swigging SWIG/_m2crypto.i to SWIG/_m2crypto_wrap.c
swig -python -I/usr/include/python2.6 -I/usr/include -includeall -o SWIG/_m2crypto_wrap.c SWIG/_m2crypto.i
SWIG/_bio.i:64: Warning(454): Setting a pointer/reference variable may leak memory.
SWIG/_rand.i:19: Warning(454): Setting a pointer/reference variable may leak memory.
SWIG/_evp.i:156: Warning(454): Setting a pointer/reference variable may leak memory.
SWIG/_dh.i:36: Warning(454): Setting a pointer/reference variable may leak memory. …Run Code Online (Sandbox Code Playgroud) 我有一个变量名列表,如下所示:
['foo', 'bar', 'baz']
Run Code Online (Sandbox Code Playgroud)
(我最初问过如何转换变量列表.请参阅下面的Greg Hewgill的答案.)
如何将其转换为字典,其中键是变量名称(作为字符串),值是变量的值?
{'foo': foo, 'bar': bar, 'baz': baz}
Run Code Online (Sandbox Code Playgroud)
现在我正在重新提出这个问题,我提出了:
d = {}
for name in list_of_variable_names:
d[name] = eval(name)
Run Code Online (Sandbox Code Playgroud)
可以改进吗?
更新,回答问题(在评论中)为什么我想这样做:
我经常发现自己使用%运算符来设置要插入的名称和值字典的字符串.通常,字符串中的名称只是局部变量的名称.所以(下面的答案)我可以做这样的事情:
message = '''Name: %(name)s
ZIP: %(zip)s
Dear %(name)s,
...''' % dict((x, locals()[x]) for x in ['name', 'zip'])
Run Code Online (Sandbox Code Playgroud) 我正在尝试单元测试一些看起来像这样的代码:
def main():
parser = optparse.OptionParser(description='This tool is cool', prog='cool-tool')
parser.add_option('--foo', action='store', help='The foo option is self-explanatory')
options, arguments = parser.parse_args()
if not options.foo:
parser.error('--foo option is required')
print "Your foo is %s." % options.foo
return 0
if __name__ == '__main__':
sys.exit(main())
Run Code Online (Sandbox Code Playgroud)
使用如下代码:
@patch('optparse.OptionParser')
def test_main_with_missing_p4clientsdir_option(self, mock_optionparser):
#
# setup
#
optionparser_mock = Mock()
mock_optionparser.return_value = optionparser_mock
options_stub = Mock()
options_stub.foo = None
optionparser_mock.parse_args.return_value = (options_stub, sentinel.arguments)
def parser_error_mock(message):
self.assertEquals(message, '--foo option is required')
sys.exit(2)
optionparser_mock.error = parser_error_mock
#
# …Run Code Online (Sandbox Code Playgroud)