Debug」タグアーカイブ

coreのダンプとgdbでのデバッグ

coreをダンプするように設定して、デバッグする手順

1. プログラムのコンパイル

$ g++ -O0 -g example.cpp

最適化無効、デバッグ情報付加でビルド

2. coreをダンプするように設定

$ ulimit -c unlimited

最近の環境ではデフォルトでcoreを出力しない設定になっていることが多いです。

3. ビルドしたプログラムの実行

$ ./a.out
$ ls
a.out core example.cpp

4. デバッグ

$ gdb a.out core

Pythonのtypesとassert

Pythonで型チェックの方法です。

types

型の取得には組み込み関数typeを使います。

>>> from types import *
>>> type(1) == IntType
True
>>> type('') == StringType
True
>>> type([]) == ListType
True

assert

C言語など他の言語と同様Pythonにもassertがあります。
assertの直後に評価したい式を書きます。

>>> assert True
>>> assert False
Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
AssertionEror

型チェック

typesassertを組み合わせて型チェックができます。

>>> x = 1
>>> assert type(x) == IntType
>>> try:
...   assert type(x) == StringType, 'type error'
... except AssertionError, e:
...   print(e.message)
...
type error

注) この記事はPython2向けの内容となっています。

Python3ではtypesの代わりに

>>> type(1) == int
True
>>> type('') == str
True
>>> type([]) == list
True

のような感じで使えます。

ちなみに、intstrlistなどに使える値は

print([t.__name__ for t in __builtins__.__dict__.values() if isinstance(t, type)])

で一覧表示できます。

参考リンク

python – List of classinfo Types – Stack Overflow
https://stackoverflow.com/questions/51567356/list-of-classinfo-types