assert

Swift Assertions behaviour in production applications

*爱你&永不变心* 提交于 2019-12-18 14:45:22
问题 I'm reading the Assertions section in the Swift e-book and it looks like assertions work very similarly to their Objective-C counterparts. However, nowhere in the docs can I find anything about runtime behaviour while running as a production app. Objective-C's NSAssert promises never to terminate a production application as a result of an assertion failure. Is it the same case in Swift? 回答1: Based upon the language Apple use in their documentation, I'd say assertions are ignored in a

What is the use of Python's basic optimizations mode? (python -O)

為{幸葍}努か 提交于 2019-12-18 10:59:50
问题 Python has a flag -O that you can execute the interpreter with. The option will generate "optimized" bytecode (written to .pyo files), and given twice, it will discard docstrings. From Python's man page: -O Turn on basic optimizations. This changes the filename extension for compiled (bytecode) files from .pyc to .pyo. Given twice, causes docstrings to be discarded. This option's two major features as I see it are: Strip all assert statements. This trades defense against corrupt program state

Pthread mutex assertion error

南楼画角 提交于 2019-12-18 10:54:43
问题 I'm encountering the following error at unpredictable times in a linux-based (arm) communications application: pthread_mutex_lock.c:82: __pthread_mutex_lock: Assertion `mutex->__data.__owner == 0' failed. Google turns up a lot of references to that error, but little information that seems relevant to my situation. I was wondering if anyone can give me some ideas about how to troubleshoot this error. Does anyone know of a common cause for this assertion? Thanks in advance. 回答1: Rock solid for

How to find the name of the current function at runtime?

最后都变了- 提交于 2019-12-18 10:39:07
问题 After years of using the big ugly MFC ASSERT macro, I have finally decided to ditch it and create the ultimate ASSERT macro. I am fine with getting the file and line number, and even the expression that failed. I can display a messagebox with these in, and Abort/Retry/Cancel buttons. And when I press Retry the VS debugger jumps to the line containing the ASSERT call (as opposed to the disassembly somewhere like some other ASSERT functions). So it's all pretty much working. But what would be

Difference between Raise Try and Assert

巧了我就是萌 提交于 2019-12-18 10:33:37
问题 I have been learning Python for a while and the raise function and assert are (what I realised is that both of them crash the app, unlike try - except) really similar and I can't see a situation where you would use raise or assert over try . So, what is the difference between Raise, Try and Assert? 回答1: Assert: Used when you want to "stop" the script based on a certain condition and return something to help debug faster: list_ = ["a","b","x"] assert "x" in list_, "x is not in the list" print(

Why is assert a macro and not a function?

北城余情 提交于 2019-12-18 10:31:34
问题 My lecturer has asked me that in class, and I was wondering why is it a macro instead of a function? 回答1: The simple explanation would be that the standard requires assert to be a macro, if we look at the draft C99 standard( as far as I can tell the sections are the same in draft C11 standard as well ) section 7.2 Diagnostics paragraph 2 says: The assert macro shall be implemented as a macro, not as an actual function. If the macro definition is suppressed in order to access an actual

In what cases we need to include <cassert>?

▼魔方 西西 提交于 2019-12-18 10:27:35
问题 In what cases should we include cassert ? 回答1: In short, don't use it; use <assert.h> . C++11 removed any formal guarantee of a "c...." header not polluting the global namespace. It was never an in-practice guarantee, and now it's not even a formal guarantee. Hence, with C++11 there is no longer any conceivable advantage in using the "c...." header variants, while there is the distinct and clear disadvantage that code that works well with one compiler and version of that compiler, may fail to

How to handle AssertionError in Python and find out which line or statement it occurred on?

爱⌒轻易说出口 提交于 2019-12-18 10:07:11
问题 I want to handle AssertionError s both to hide unnecessary parts of the stack trace from the user and to print a message as to why the error occurred and what the user should do about it. Is there any way to find out on which line or statement the assert failed within the except block? try: assert True assert 7 == 7 assert 1 == 2 # many more statements like this except AssertionError: print 'Houston, we have a problem.' print print 'An error occurred on line ???? in statement ???' exit(1) I

Is using assert() in C++ bad practice?

为君一笑 提交于 2019-12-18 10:05:59
问题 I tend to add lots of assertions to my C++ code to make debugging easier without affecting the performance of release builds. Now, assert is a pure C macro designed without C++ mechanisms in mind. C++ on the other hand defines std::logic_error , which is meant to be thrown in cases where there is an error in the program's logic (hence the name). Throwing an instance might just be the perfect, more C++ish alternative to assert . The problem is that assert and abort both terminate the program

Can I patch Python's assert to get the output that py.test provides?

无人久伴 提交于 2019-12-18 09:18:58
问题 Pytest's output for failed asserts is much more informative and useful than the default in Python. I would like to leverage this when normally running my Python program, not just when executing tests. Is there a way to, from within my script, overwrite Python's assert behavior to use pytest to print the stacktrace instead while still running my program as python script/pytest_assert.py ? Example program def test_foo(): foo = 12 bar = 42 assert foo == bar if __name__ == '__main__': test_foo()