Determin if a racket program is in a sandbox

别等时光非礼了梦想. 提交于 2019-12-24 13:02:53

问题


Is it possible to determine if a Racket program is being run in a sandbox?

The reason I ask is because I have a Racket macro that creates a file. And the DrRacket background expander prevents a file from being created (as it should). However, in doing so, it causes an error to appear at the bottom of the window saying the file could not be created.

So, I would like to determine if I am in a sandbox, and if I am, don't create the file, and kindly finish up the macro.


回答1:


In general, you cannot determine if you are in a sandbox. However, you do have a chance to catch the errors that are thrown when you try to perform a restricted operation. However, the catch is that you do not know what type of error is going to be thrown. So one thing that you can do is to just catch all of them. Use with-handlers to catch the error and exn:fail? to catch all errors.

(with-handlers ([exn:fail?
                 (lambda (x) (displayln "failing cleanly"))])
    (make-temporary-file))

Be careful here that an error here may occur that is not related to being in a sandbox. For example, you could potentially get an error just because a file could not be created:

(with-handlers ([exn:fail:filesystem?
                 (lambda (x) (displayln "Coudln't open file"))]
                [exn:fail?
                 (lambda (x) (displayln "failing gracefully"))])
  (make-temporary-file))


来源:https://stackoverflow.com/questions/35093348/determin-if-a-racket-program-is-in-a-sandbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!