Index: includes/common.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/common.inc,v
retrieving revision 1.976
diff -u -p -r1.976 common.inc
--- includes/common.inc	26 Aug 2009 10:53:45 -0000	1.976
+++ includes/common.inc	26 Aug 2009 12:22:21 -0000
@@ -424,6 +424,9 @@ function drupal_not_found() {
 
   drupal_set_page_content($return);
   $page = element_info('page');
+  // Optionally omit the blocks to conserve CPU and bandwidth.
+  $page['#show_blocks'] = variable_get('site_404_blocks', FALSE);
+
   print drupal_render_page($page);
 }
 
@@ -3890,6 +3893,7 @@ function drupal_set_page_content($conten
  *   A string or array representing the content of a page. The array consists of
  *   the following keys:
  *   - #type: Value is always 'page'. This pushes the theming through page.tpl.php (required).
+ *   - #show_blocks: A marker which suppresses sidebar regions if FALSE (optional).
  *   - #show_messages: Suppress drupal_get_message() items. Used by Batch API (optional).
  *
  * @see hook_page_alter()
@@ -4254,7 +4258,7 @@ function drupal_common_theme() {
       'template' => 'page',
     ),
     'maintenance_page' => array(
-      'arguments' => array('content' => NULL, 'show_messages' => TRUE),
+      'arguments' => array('content' => NULL, 'show_blocks' => TRUE, 'show_messages' => TRUE),
       'template' => 'maintenance-page',
       'path' => 'includes',
       'file' => 'theme.maintenance.inc',
Index: includes/theme.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/theme.inc,v
retrieving revision 1.516
diff -u -p -r1.516 theme.inc
--- includes/theme.inc	26 Aug 2009 10:53:45 -0000	1.516
+++ includes/theme.inc	26 Aug 2009 12:22:24 -0000
@@ -2022,6 +2022,7 @@ function template_process(&$variables, $
  */
 function template_preprocess_page(&$variables) {
   // Move some variables to the top level for themer convenience and template cleanliness.
+  $variables['show_blocks'] = $variables['page']['#show_blocks'];
   $variables['show_messages'] = $variables['page']['#show_messages'];
 
   // Add favicon.
Index: includes/theme.maintenance.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/theme.maintenance.inc,v
retrieving revision 1.38
diff -u -p -r1.38 theme.maintenance.inc
--- includes/theme.maintenance.inc	26 Aug 2009 10:53:45 -0000	1.38
+++ includes/theme.maintenance.inc	26 Aug 2009 12:22:25 -0000
@@ -208,6 +208,7 @@ function theme_update_page($content, $sh
  *
  * The $variables array contains the following arguments:
  * - $content
+ * - $show_blocks
  *
  * @see maintenance-page.tpl.php
  */
Index: modules/block/block.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/block/block.module,v
retrieving revision 1.368
diff -u -p -r1.368 block.module
--- modules/block/block.module	26 Aug 2009 10:53:45 -0000	1.368
+++ modules/block/block.module	26 Aug 2009 12:22:26 -0000
@@ -246,21 +246,24 @@ function block_page_alter($page) {
 
   // Load all region content assigned via blocks.
   foreach (array_keys($all_regions) as $region) {
-    // Assign blocks to region.
-    if ($blocks = block_get_blocks_by_region($region)) {
-      $page[$region] = $blocks;
-    }
-
-    // Append region description if we are rendering the block admin page.
-    $item = menu_get_item();
-    if ($item['path'] == 'admin/structure/block') {
-      $visible_regions = system_region_list($theme, REGIONS_VISIBLE);
-      if (isset($visible_regions[$region])) {
-        $description = '<div class="block-region">' . $all_regions[$region] . '</div>';
-        $page[$region]['block_description'] = array(
-          '#markup' => $description,
-          '#weight' => 15,
-        );
+    // Prevent sidebar regions from rendering blocks when 'show_blocks' == FALSE.
+    if (!empty($page['#show_blocks']) || (strpos($region, 'sidebar_') !== 0)) {
+      // Assign blocks to region.
+      if ($blocks = block_get_blocks_by_region($region)) {
+        $page[$region] = $blocks;
+      }
+
+      // Append region description if we are rendering the block admin page.
+      $item = menu_get_item();
+      if ($item['path'] == 'admin/structure/block') {
+        $visible_regions = system_region_list($theme, REGIONS_VISIBLE);
+        if (isset($visible_regions[$region])) {
+          $description = '<div class="block-region">' . $all_regions[$region] . '</div>';
+          $page[$region]['block_description'] = array(
+            '#markup' => $description,
+            '#weight' => 15,
+          );
+        }
       }
     }
   }
Index: modules/block/block.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/block/block.test,v
retrieving revision 1.26
diff -u -p -r1.26 block.test
--- modules/block/block.test	26 Aug 2009 10:53:45 -0000	1.26
+++ modules/block/block.test	26 Aug 2009 12:22:27 -0000
@@ -104,21 +104,9 @@ class BlockTestCase extends DrupalWebTes
    */
   function testBlockVisibility() {
     $block = array();
-    
-    // Create a random title for the block
-    $title = $this->randomName(8);
-    
-    // Create the custom block
-    $box = array();
-    $box['info'] = $this->randomName(8);
-    $box['title'] = $title;
-    $box['body'] = $this->randomName(32);
-    $this->drupalPost('admin/structure/block/add', $box, t('Save block'));
-    
-    $bid = db_query("SELECT bid FROM {box} WHERE info = :info", array(':info' => $box['info']))->fetchField();
-    $block['module'] = 'block';
-    $block['delta'] = $bid;
-    $block['title'] = $title;
+    $block['title'] = 'Syndicate';
+    $block['module'] = 'node';
+    $block['delta'] = 'syndicate';
 
     // Set the block to be hidden on any user path, and to be shown only to
     // authenticated users.
@@ -131,15 +119,15 @@ class BlockTestCase extends DrupalWebTes
     $this->moveBlockToRegion($block, $this->regions[1]);
 
     $this->drupalGet('');
-    $this->assertText($title, t('Block was displayed on the front page.'));
+    $this->assertText('Syndicate', t('Block was displayed on the front page.'));
 
-    $this->drupalGet('user');
-    $this->assertNoText($title, t('Block was not displayed according to block visibility rules.'));
+    $this->drupalGet('user*');
+    $this->assertNoText('Syndicate', t('Block was not displayed according to block visibility rules.'));
 
     // Confirm that the block is not displayed to anonymous users.
     $this->drupalLogout();
     $this->drupalGet('');
-    $this->assertNoText($title, t('Block was not displayed to anonymous users.'));
+    $this->assertNoText('Syndicate', t('Block was not displayed to anonymous users.'));
   }
 
   /**
Index: modules/system/system.admin.inc
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.admin.inc,v
retrieving revision 1.197
diff -u -p -r1.197 system.admin.inc
--- modules/system/system.admin.inc	26 Aug 2009 10:53:45 -0000	1.197
+++ modules/system/system.admin.inc	26 Aug 2009 12:22:27 -0000
@@ -1308,6 +1308,12 @@ function system_site_information_setting
     '#description' => t('This page is displayed when no other content matches the requested document. Leave blank to display a generic "page not found" page.'),
     '#field_prefix' => url(NULL, array('absolute' => TRUE)) . (variable_get('clean_url', 0) ? '' : '?q=')
   );
+  $form['site_404_blocks'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Display blocks on default 404 (not found) page'),
+    '#description' => t('Render all blocks on the default 404 (not found) page. Disabling blocks can help with performance but might leave users with a less functional site.'),
+    '#default_value' => variable_get('site_404_blocks', 0)
+  );
   $form['cron_safe_threshold'] = array(
     '#type' => 'select',
     '#title' => t('Automatically run cron'),
@@ -1935,6 +1941,7 @@ function system_batch_page() {
     // display a list of collected messages later.
     drupal_set_page_content($output);
     $page = element_info('page');
+    $page['#show_blocks'] = FALSE;
     $page['#show_messages'] = FALSE;
     return $page;
   }
Index: modules/system/system.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.module,v
retrieving revision 1.777
diff -u -p -r1.777 system.module
--- modules/system/system.module	26 Aug 2009 10:53:45 -0000	1.777
+++ modules/system/system.module	26 Aug 2009 12:22:28 -0000
@@ -291,6 +291,7 @@ function system_elements() {
 
   $type['page'] = array(
     '#show_messages' => TRUE,
+    '#show_blocks' => TRUE,
     '#theme' => 'page',
   );
 
Index: modules/system/system.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.test,v
retrieving revision 1.74
diff -u -p -r1.74 system.test
--- modules/system/system.test	26 Aug 2009 10:53:45 -0000	1.74
+++ modules/system/system.test	26 Aug 2009 12:22:29 -0000
@@ -626,6 +626,24 @@ class PageNotFoundTestCase extends Drupa
 
     $this->drupalGet($this->randomName(10));
     $this->assertText($node->title, t('Found the custom 404 page'));
+
+    // Logout and check that the user login block is not shown on custom 404 pages.
+    $this->drupalLogout();
+
+    $this->drupalGet($this->randomName(10));
+    $this->assertText($node->title, t('Found the custom 404 page'));
+    $this->assertNoText(t('User login'), t('Blocks are not shown on the custom 404 page'));
+
+    // Log back in and remove the custom 404 page.
+    $this->drupalLogin($this->admin_user);
+    $this->drupalPost('admin/settings/site-information', array('site_404' => ''), t('Save configuration'));
+
+    // Logout and check that the user login block is not shown on default 404 pages.
+    $this->drupalLogout();
+
+    $this->drupalGet($this->randomName(10));
+    $this->assertText(t('Page not found'), t('Found the default 404 page'));
+    $this->assertNoText(t('User login'), t('Blocks are not shown on the default 404 page'));
   }
 }
 
