2

Is it possible to have some function that increments a counter run upon each instruction within a function, and does some behavior if a number is reached?

To explain, I want to be able to raise an exception if a function ends up taking up past a certain number of bytecode instructions in the virtual machine. So, if someone creates a list comprehension that will make an infinite loop, or creates an infinite while loop, it will force a break out early.

Can this be done in Python?

4

1 に答える 1

1

疑わしい。通常、これを行う唯一の確実な方法は、信頼できないコードを別のプロセスで実行し、適切にサンドボックス化することです。

信頼できないコードが重要なリソース (ロックなど) を取得し、時間がかかりすぎて強制終了された場合にどうなるかを考えてみてください。誰かが同じロックを取得しようとすると、プロセス全体がデッドロックになります。

于 2013-03-06T19:40:25.617 に答える