我将尝试解释关于简单示例程序的问题(我的问题要复杂得多,因为我的程序要复杂得多)。
假设我有一个包含2行的程序,可以创建2个函数:
data = long_one() #takes 2 hours in DEBUG mode, 15min in RUN mode
short_one(data) #i want to DEBUG this one
让我们说准备data
变量非常困难,获得它的唯一方法是运行函数long_one()。
有没有办法在Pycharm中运行long_one()
和DEBUG short_one()
?
换句话说,有一种方法可以执行:
long_one()
应在RUN模式下处理short_one()
的规范?答案 0 :(得分:0)
import sys
def do_the_thing_for_debug():
print('doing the debug version')
def do_the_thing():
print('doing the release version')
if __name__ == '__main__':
"""
Detecting if you're in the PyCharm debugger or not
WARNING: This is a hacky & there is probably a better
way of doing this by looking at other items in the
global() namespace or environment for things that
begin with PyCharm and stuff.
Also, this could break in future versions of PyCharm!
If you put a breakpoint HERE and look at the callstack you will
see the entry point is in 'pydevd.py'
In debug mode, it copies off sys.argv: "sys.original_argv = sys.argv[:]"
We abuse this knowledge to test for the PyCharm debugger.
"""
if hasattr(sys, 'original_argv'):
do_the_thing = do_the_thing_for_debug
do_the_thing()
当我“运行”它时来自PyCharm的输出
doing the release version
当我“调试”它时来自PyCharm的输出
doing the debug version
答案 1 :(得分:0)
Asagen建议:
int main() {
FILE *fptr;
string fileName;
cout<<"Please provide the name of the file: "<<endl;
cin>>fileName;
fileName = "Test Images/"+fileName+".bmp";
fptr = fopen(fileName.c_str(), "rb");
cout<<"FileName : "<<fileName<<endl;
if(fptr == NULL){
cout<<"File Not found"<<endl;
getch();
return -1;
}
unsigned char total[54];
fread(total, sizeof(unsigned char), 54, fptr);
cout<<endl;
cout<<"Type: "<< *(char*)&total[0x00]<<*(char*)&total[0x01] <<endl;
cout<<"size: "<< *(int*)&total[0x02]<<endl;
cout<<"offset: "<< *(int*)&total[0x0A]<<endl;
cout<<"Info header size: "<< *(int*)&total[0x0E]<<endl;
cout<<"Bitmap width: "<< *(int*)&total[0x12]<<endl;
cout<<"Bimap height "<< *(int*)&total[0x16]<<endl;
cout<<"Color planes: "<< *(int*)&total[0x1A]<<endl;
cout<<"BPP: "<< *(short int*)&total[0x1C]<<endl;
cout<<"Compression Method "<< *(int*)&total[0x1E]<<endl;
cout<<"Raw bmp data size: "<< *(int*)&total[0x22]<<endl;
cout<<"Horizontal Resolution: "<< *(int*)&total[0x26]<<endl;
cout<<"Vertical Resolution: "<< *(int*)&total[0x2A]<<endl;
cout<<"No of Color: "<< *(int*)&total[0x2E]<<endl;
cout<<"No of imp colors: "<< *(int*)&total[0x32]<<endl;
fclose(fptr);
getch();
return 0;
}
并选择了我的流程。调试器从我这样做的那一刻开始,并在它遇到的第一个断点处停止。
有一个不便之处 - 我必须知道何时开始调试(在哪个时刻将调试器连接到进程)。我提出了一个解决方法:
Tools/Attach to Process
data = long_one() # takes 2 hours in DEBUG mode, 15min in RUN mode
infinite_loop = True
print "OK man, it is the time to start debugging!"
while infinite_loop:
time.sleep(0.2) # add breakpoint here
short_one(data) #i want to DEBUG this one
,以便离开循环就是这样,之前运行完整代码后你现在处于DEBUG模式,
如果要返回RUN模式,只需停止调试即可。可以在RUN和DEBUG之间多次和在任何你想要的地方切换