Skip to content

Commit 39a75b6

Browse files
author
Philip Guo
committed
fail gracefully when 'resource' module unavailable
1 parent 890e155 commit 39a75b6

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

‎v3/pg_logger.py‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,12 @@
6363
# (beware that this is NOT foolproof at all ... there are known flaws!)
6464
#
6565
# ALWAYS use defense-in-depth and don't just rely on these simple mechanisms
66-
import resource
66+
try:
67+
import resource
68+
resource_module_loaded = True
69+
except ImportError:
70+
# Google App Engine doesn't seem to have the 'resource' module
71+
resource_module_loaded = False
6772

6873

6974
# ugh, I can't figure out why in Python 2, __builtins__ seems to
@@ -671,12 +676,14 @@ def _runscript(self, script_str):
671676
# memory bombs such as:
672677
# x = 2
673678
# while True: x = x*x
674-
resource.setrlimit(resource.RLIMIT_AS, (200000000, 200000000))
675-
resource.setrlimit(resource.RLIMIT_CPU, (5, 5))
679+
if resource_module_loaded:
680+
resource.setrlimit(resource.RLIMIT_AS, (200000000, 200000000))
681+
resource.setrlimit(resource.RLIMIT_CPU, (5, 5))
682+
683+
# protect against unauthorized filesystem accesses ...
684+
resource.setrlimit(resource.RLIMIT_NOFILE, (0, 0)) # no opened files allowed
685+
resource.setrlimit(resource.RLIMIT_FSIZE, (0, 0)) # (redundancy for paranoia)
676686

677-
# protect against unauthorized filesystem accesses ...
678-
resource.setrlimit(resource.RLIMIT_NOFILE, (0, 0)) # no opened files allowed
679-
resource.setrlimit(resource.RLIMIT_FSIZE, (0, 0)) # (redundancy for paranoia)
680687
self.run(script_str, user_globals, user_globals)
681688
# sys.exit ...
682689
except SystemExit:

0 commit comments

Comments
 (0)