Index: includes/tablesort.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/tablesort.inc,v
retrieving revision 1.60
diff -u -p -r1.60 tablesort.inc
--- includes/tablesort.inc	28 Oct 2010 02:27:08 -0000	1.60
+++ includes/tablesort.inc	30 Nov 2010 06:01:26 -0000
@@ -80,7 +80,7 @@ class TableSort extends SelectQueryExten
     // User has not specified a sort. Use default if specified; otherwise use "asc".
     else {
       foreach ($this->header as $header) {
-        if (isset($header['sort'])) {
+        if (is_array($header) && isset($header['sort'])) {
           return $header['sort'];
         }
       }
@@ -278,8 +278,9 @@ function tablesort_get_sort($headers) {
   }
   // User has not specified a sort. Use default if specified; otherwise use "asc".
   else {
+    $ts = tablesort_get_order($headers);
     foreach ($headers as $header) {
-      if (isset($header['sort'])) {
+      if (is_array($header) && $header['data'] == $ts['name'] && isset($header['sort'])) {
         return $header['sort'];
       }
     }
Index: includes/theme.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/theme.inc,v
retrieving revision 1.625
diff -u -p -r1.625 theme.inc
--- includes/theme.inc	29 Nov 2010 04:53:32 -0000	1.625
+++ includes/theme.inc	30 Nov 2010 06:01:27 -0000
@@ -363,7 +363,6 @@ function drupal_theme_rebuild() {
  */
 function _theme_process_registry(&$cache, $name, $type, $theme, $path) {
   $result = array();
-  $function = $name . '_theme';
 
   // Processor functions work in two distinct phases with the process
   // functions always being executed after the preprocess functions.
@@ -372,24 +371,43 @@ function _theme_process_registry(&$cache
     'process functions'    => 'process',
   );
 
+  $hook_defaults = array(
+    'variables' => TRUE,
+    'render element' => TRUE,
+    'pattern' => TRUE,
+    'base hook' => TRUE,
+  );
+
+  // Invoke the hook_theme() implementation, process what is returned, and
+  // merge it into $cache.
+  $function = $name . '_theme';
   if (function_exists($function)) {
     $result = $function($cache, $type, $theme, $path);
     foreach ($result as $hook => $info) {
+      // When a theme or engine overrides a module's theme function
+      // $result[$hook] will only contain key/value pairs for information being
+      // overridden.  Pull the rest of the information from what was defined by
+      // an earlier hook.
+
+      // Fill in the type and path of the module, theme, or engine that
+      // implements this theme function.
       $result[$hook]['type'] = $type;
       $result[$hook]['theme path'] = $path;
-      // if function and file are left out, default to standard naming
+
+      // If function and file are omitted, default to standard naming
       // conventions.
       if (!isset($info['template']) && !isset($info['function'])) {
         $result[$hook]['function'] = ($type == 'module' ? 'theme_' : $name . '_') . $hook;
       }
-      // If a path is set in the info, use what was set. Otherwise use the
-      // default path. This is mostly so system.module can declare theme
-      // functions on behalf of core .include files.
-      // All files are included to be safe. Conditionally included
-      // files can prevent them from getting registered.
+
       if (isset($cache[$hook]['includes'])) {
         $result[$hook]['includes'] = $cache[$hook]['includes'];
       }
+
+      // If the theme implementation defines a file, then also use the path
+      // that it defined. Otherwise use the default path. This allows
+      // system.module to declare theme functions on behalf of core .include
+      // files.
       if (isset($info['file'])) {
         $include_file = isset($info['path']) ? $info['path'] : $path;
         $include_file .= '/' . $info['file'];
@@ -397,14 +415,10 @@ function _theme_process_registry(&$cache
         $result[$hook]['includes'][] = $include_file;
       }
 
-      // If these keys are left unspecified within overridden entries returned
-      // by hook_theme(), carry them forward from the prior entry. This is so
-      // that themes don't need to specify this information, since the module
-      // that registered the theme hook already has.
-      foreach (array('variables', 'render element', 'pattern', 'base hook') as $key) {
-        if (!isset($info[$key]) && isset($cache[$hook][$key])) {
-          $result[$hook][$key] = $cache[$hook][$key];
-        }
+      // If the default keys are not set, use the default values registered
+      // by the module.
+      if (isset($cache[$hook])) {
+        $result[$hook] += array_intersect_key($cache[$hook], $hook_defaults);
       }
 
       // The following apply only to theming hooks implemented as templates.
@@ -469,7 +483,7 @@ function _theme_process_registry(&$cache
     }
 
     // Merge the newly created theme hooks into the existing cache.
-    $cache = array_merge($cache, $result);
+    $cache = $result + $cache;
   }
 
   // Let themes have variable processors even if they didn't register a template.
Index: modules/simpletest/simpletest.info
===================================================================
RCS file: /cvs/drupal/drupal/modules/simpletest/simpletest.info,v
retrieving revision 1.26
diff -u -p -r1.26 simpletest.info
--- modules/simpletest/simpletest.info	22 Oct 2010 16:36:14 -0000	1.26
+++ modules/simpletest/simpletest.info	30 Nov 2010 06:01:27 -0000
@@ -35,6 +35,7 @@ files[] = tests/path.test
 files[] = tests/registry.test
 files[] = tests/schema.test
 files[] = tests/session.test
+files[] = tests/tablesort.test
 files[] = tests/theme.test
 files[] = tests/unicode.test
 files[] = tests/update.test
Index: modules/simpletest/tests/tablesort.test
===================================================================
RCS file: modules/simpletest/tests/tablesort.test
diff -N modules/simpletest/tests/tablesort.test
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ modules/simpletest/tests/tablesort.test	30 Nov 2010 06:01:27 -0000
@@ -0,0 +1,114 @@
+<?php
+// $Id$
+
+/**
+ * @file
+ * Various tablesort tests.
+ */
+
+/**
+ * Test unicode handling features implemented in unicode.inc.
+ */
+class TableSortTest extends DrupalUnitTestCase {
+
+  /**
+   * Storage for initial value of $_GET.
+   *
+   * @var array
+   */
+  protected $GET = array();
+
+  public static function getInfo() {
+    return array(
+      'name' => 'Tablesort',
+      'description' => 'Tests table sorting.',
+      'group' => 'System',
+    );
+  }
+
+  function setUp() {
+    // Save the original $_GET to be restored later.
+    $this->GET = $_GET;
+
+    parent::setUp();
+  }
+
+  function tearDown() {
+    // Revert $_GET.
+    $_GET = $this->GET;
+
+    parent::tearDown();
+  }
+
+  /**
+   * Test tablesort_init().
+   */
+  function testTableSortInit() {
+
+    // Test simple table headers.
+    $header = array('foo', 'bar', 'baz');
+    $_GET = array('q' => 'jahwohl');
+    $expected_ts = array(
+      'name' => 'foo',
+      'sql' => '',
+      'sort' => 'asc',
+      'query' => array(),
+    );
+    $ts = tablesort_init($header);
+    $this->verbose(t('$ts: <pre>%ts</pre>', array('%ts' => filter_xss_admin(var_export($ts, TRUE)))));
+    $this->assertEqual($ts, $expected_ts, t('Simple table headers sorted correctly.'));
+
+    // Test with simple table header plus $_GET parameters.
+    $_GET = array(
+      'q' => 'jahwohl',
+      'sort' => 'bar',
+      'order' => 'DESC',
+      'alpha' => 'beta',
+    );
+    $expected_ts['query'] = array('alpha' => 'beta');
+    $ts = tablesort_init($header);
+    $this->verbose(t('$ts: <pre>%ts</pre>', array('%ts' => filter_xss_admin(var_export($ts, TRUE)))));
+    $this->assertEqual($ts, $expected_ts, t('Simple table headers plus $_GET parameters sorted correctly.'));
+
+    // Test complex table headers.
+    $header = array(
+      'foo',
+      array(
+        'data' => '1',
+        'field' => 'one',
+        'sort' => 'asc',
+        'colspan' => 1,
+      ),
+      array(
+        'data' => '2',
+        'field' => 'two',
+        'sort' => 'desc',
+      ),
+    );
+    $_GET = array('q' => 'jahwohl');
+    $ts = tablesort_init($header);
+    $expected_ts = array(
+      'name' => '2',
+      'sql' => 'two',
+      'sort' => 'desc',
+      'query' => array(),
+    );
+    $this->verbose(t('$ts: <pre>%ts</pre>', array('%ts' => filter_xss_admin(var_export($ts, TRUE)))));
+    $this->assertEqual($ts, $expected_ts, t('Complex table headers sorted correctly.'));
+
+    // Test with complex table header plus $_GET parameters.
+    $_GET = array(
+      'q' => 'jahwohl',
+      'sort' => 'bar',
+      'order' => 'DESC',
+      'alpha' => 'beta',
+    );
+    $expected_ts['query'] = array('alpha' => 'beta');
+    $expected_ts['sort'] = 'asc';
+    $ts = tablesort_init($header);
+    $this->verbose(t('$ts: <pre>%ts</pre>', array('%ts' => filter_xss_admin(var_export($ts, TRUE)))));
+    $this->assertEqual($ts, $expected_ts, t('Complex table headers plus $_GET parameters sorted correctly.'));
+    unset($_GET['sort'], $_GET['order'], $_GET['alpha']);
+
+  }
+}
