Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.888
diff -u -r1.888 common.inc
--- includes/common.inc	27 Apr 2009 20:19:35 -0000	1.888
+++ includes/common.inc	5 May 2009 23:48:18 -0000
@@ -1931,6 +1931,28 @@
 }
 
 /**
+ * Attempt to set the PHP maximum execution time.
+ *
+ * A wrapper around PHP's set_time_limit(). To avoid PHP warnings it checks if
+ * PHP is not running in 'safe_mode' and if the function is not disabled
+ * (through php.ini or Suhosin).
+ *
+ * @param $time_limit
+ *   An integer specifying the new time limit, in seconds. A value of 0
+ *   indicates unlimited execution time.
+ */
+function drupal_set_time_limit($time_limit) {
+  // Unfortunately, ini_get() may return many different values, and we can't
+  // be certain which values mean 'on', so we instead check for 'not off'
+  // We can only guarantee safe_mode is off if the value returned
+  // is 'off', '', or 0.
+  $safe_mode = trim(ini_get('safe_mode'));
+  if ((empty($safe_mode) || strtolower($safe_mode) == 'off') && function_exists('set_time_limit') && strpos(ini_get('suhosin.executor.func.blacklist'), 'set_time_limit') === FALSE) {
+    set_time_limit($time_limit);
+  }
+}
+
+/**
  * Returns the path to a system item (module, theme, etc.).
  *
  * @param $type
@@ -3091,8 +3113,8 @@
   // Allow execution to continue even if the request gets canceled.
   @ignore_user_abort(TRUE);
 
-  // Increase the maximum execution time.
-  @set_time_limit(240);
+  // Try to reset the execution time and set a sufficient new time limit.
+  drupal_set_time_limit(240);
 
   // Fetch the cron semaphore
   $semaphore = variable_get('cron_semaphore', FALSE);
Index: includes/locale.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/locale.inc,v
retrieving revision 1.214
diff -u -r1.214 locale.inc
--- includes/locale.inc	27 Apr 2009 20:19:35 -0000	1.214
+++ includes/locale.inc	5 May 2009 23:38:37 -0000
@@ -1165,10 +1165,8 @@
  *   Text group to import PO file into (eg. 'default' for interface translations)
  */
 function _locale_import_po($file, $langcode, $mode, $group = NULL) {
-  // If not in 'safe mode', increase the maximum execution time.
-  if (!ini_get('safe_mode')) {
-    set_time_limit(240);
-  }
+  // Try to reset the execution time and set a sufficient new time limit.
+  drupal_set_time_limit(240);
 
   // Check if we have the language already in the database.
   if (!db_query("SELECT COUNT(language) FROM {languages} WHERE language = :language", array(':language' => $langcode))->fetchField()) {
Index: modules/node/node.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/node/node.module,v
retrieving revision 1.1043
diff -u -r1.1043 node.module
--- modules/node/node.module	29 Apr 2009 17:48:11 -0000	1.1043
+++ modules/node/node.module	5 May 2009 23:38:34 -0000
@@ -2715,10 +2715,9 @@
       batch_set($batch);
     }
     else {
-      // If not in 'safe mode', increase the maximum execution time.
-      if (!ini_get('safe_mode')) {
-        set_time_limit(240);
-      }
+      // Try to reset the execution time and set a sufficient new time limit.
+      drupal_set_time_limit(240);
+
       $nids = db_query("SELECT nid FROM {node}")->fetchCol();
       foreach ($nids as $nid) {
         $node = node_load($nid, NULL, TRUE);
Index: modules/simpletest/drupal_web_test_case.php
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/drupal_web_test_case.php,v
retrieving revision 1.101
diff -u -r1.101 drupal_web_test_case.php
--- modules/simpletest/drupal_web_test_case.php	29 Apr 2009 21:35:38 -0000	1.101
+++ modules/simpletest/drupal_web_test_case.php	5 May 2009 23:38:31 -0000
@@ -907,7 +907,7 @@
     // Create the files directory.
     file_check_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
 
-    set_time_limit($this->timeLimit);
+    drupal_set_time_limit($this->timeLimit);
   }
 
   /**
Index: modules/simpletest/tests/common.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v
retrieving revision 1.36
diff -u -r1.36 common.test
--- modules/simpletest/tests/common.test	27 Apr 2009 20:19:38 -0000	1.36
+++ modules/simpletest/tests/common.test	5 May 2009 23:38:19 -0000
@@ -967,3 +967,31 @@
   }
 }
 
+/**
+ * Test setting a new PHP maximum execution time.
+ *
+ * Note that these tests will fail if the server environment is not appropriate,
+ * i.e. if safe_mode is on, or if PHP's set_time_limit() is disabled.
+ *
+ * Also note that drupal_set_time_limit() doesn't do any checking on the
+ * validity of the supplied $time_limit, so we do not test that here.  
+ */
+class CommonTimeLimitTestCase extends DrupalWebTestCase {
+  function getInfo() {
+    return array(
+      'name' => t('Set PHP maximum execution time'),
+      'description' => t('Test setting a new PHP maximmum execution time. Requires safe_mode off, and PHP\'s set_time_limit() not disabled.'),
+      'group' => t('System'),
+    );
+  }
+
+  function testSetTimeLimit() {
+    // Check we can set the time limit.
+    drupal_set_time_limit(101);
+    $this->assertEqual(ini_get('max_execution_time'), 101, t('Check time limit can be set.'));
+
+    // Check we can set the time limit to 0 (unlimited).
+    drupal_set_time_limit(0);
+    $this->assertEqual(ini_get('max_execution_time'), 0, t('Check time limit can be set to unlimited.'));
+  }
+}
Index: scripts/run-tests.sh
===================================================================
RCS file: /cvs/drupal/drupal/scripts/run-tests.sh,v
retrieving revision 1.26
diff -u -r1.26 run-tests.sh
--- scripts/run-tests.sh	13 Apr 2009 12:23:26 -0000	1.26
+++ scripts/run-tests.sh	5 May 2009 23:39:29 -0000
@@ -63,10 +63,8 @@
 
 $test_list = simpletest_script_get_test_list();
 
-// If not in 'safe mode', increase the maximum execution time.
-if (!ini_get('safe_mode')) {
-  set_time_limit(0);
-}
+// Try to reset the execution time and set it to unlimited.
+drupal_set_time_limit(0);
 
 simpletest_script_reporter_init();
 

