diff --git a/src/pike_compiler.h b/src/pike_compiler.h
index d7971f4398db52372b37b3a402dbd3688b2f8d31..b661db1d8931abc5b02040718d687d2cce59882a 100644
--- a/src/pike_compiler.h
+++ b/src/pike_compiler.h
@@ -2,7 +2,7 @@
 || This file is part of Pike. For copyright information see COPYRIGHT.
 || Pike is distributed under GPL, LGPL and MPL. See the file COPYING
 || for more information.
-|| $Id: pike_compiler.h,v 1.9 2008/04/26 19:04:26 grubba Exp $
+|| $Id: pike_compiler.h,v 1.10 2008/05/02 10:56:06 grubba Exp $
 */
 
 #ifndef PIKE_COMPILER_H
@@ -69,6 +69,7 @@ struct compilation
 #define CE_PIKE_COMPILER_FUN_NUM			3
 #define CE_GET_COMPILATION_HANDLER_FUN_NUM		4
 #define CE_GET_DEFAULT_MODULE_FUN_NUM			5
+#define CE_HANDLE_INHERIT_FUN_NUM			6
 
 /* PikeCompiler function numbers. */
 #define PC_REPORT_FUN_NUM				0
@@ -78,5 +79,6 @@ struct compilation
 #define PC_GET_COMPILATION_HANDLER_FUN_NUM		4
 #define PC_GET_DEFAULT_MODULE_FUN_NUM			5
 #define PC_CHANGE_COMPILER_COMPATIBILITY_FUN_NUM	6
+#define PC_HANDLE_INHERIT_FUN_NUM			7
 
 #endif	/* !PIKE_COMPILER_H */
diff --git a/src/program.c b/src/program.c
index 4ab4b781c0fdc36dce7c2dc8ce8e93f65c691c9f..0a24fa2a862dc15c83a316897368130e5824d04a 100644
--- a/src/program.c
+++ b/src/program.c
@@ -2,7 +2,7 @@
 || This file is part of Pike. For copyright information see COPYRIGHT.
 || Pike is distributed under GPL, LGPL and MPL. See the file COPYING
 || for more information.
-|| $Id: program.c,v 1.681 2008/05/02 04:15:14 mast Exp $
+|| $Id: program.c,v 1.682 2008/05/02 10:56:06 grubba Exp $
 */
 
 #include "global.h"
@@ -4507,23 +4507,15 @@ int call_handle_inherit(struct pike_string *s)
 
   CHECK_COMPILER();
 
-  reference_shared_string(s);
-  push_string(s);
+  ref_push_string(s);
   if (!TEST_COMPAT(7,6)) {
     /* In Pike 7.7 and later filenames belonging to Pike are assumed
      * to be encoded according to UTF-8.
      */
     f_string_to_utf8(1);
   }
-  ref_push_string(c->lex.current_file);
-  if (c->handler && c->handler->prog) {
-    ref_push_object(c->handler);
-    args = 3;
-  }
-  else args = 2;
 
-  if (safe_apply_handler("handle_inherit", c->handler, c->compat_handler,
-			 args, BIT_PROGRAM|BIT_FUNCTION|BIT_ZERO))
+  if (safe_apply_current2(PC_HANDLE_INHERIT_FUN_NUM, 1, NULL))
     if (Pike_sp[-1].type != T_INT)
       return 1;
     else {
@@ -7368,6 +7360,22 @@ static void f_compilation_env_get_default_module(INT32 args)
   }
 }
 
+/*! @decl program handle_inherit(string inh, string current_file, @
+ *!                              object|void handler)
+ *!
+ *!   Look up an inherit @[inh].
+ */
+static void f_compilation_env_handle_inherit(INT32 args)
+{
+  if(get_master())
+  {
+    APPLY_MASTER("handle_inherit", args);
+  } else {
+    pop_n_elems(args);
+    push_undefined();
+  }
+}
+
 /*! @class PikeCompiler
  *!
  *!   The Pike compiler.
@@ -8310,6 +8318,35 @@ static void f_compilation_change_compiler_compatibility(INT32 args)
   push_int(0);
 }
 
+/*! @decl program handle_inherit(string inh)
+ *!
+ *!   Look up an inherit @[inh] in the current program.
+ */
+static void f_compilation_handle_inherit(INT32 args)
+{
+  struct compilation *c = THIS_COMPILATION;
+  struct object *handler;
+  int fun = -1;
+
+  if (args > 1) pop_n_elems(args-1);
+
+  ref_push_string(c->lex.current_file);
+  if (c->handler && c->handler->prog) {
+    ref_push_object(c->handler);
+    args = 3;
+  }
+  else args = 2;
+
+  if (((handler = c->handler) && handler->prog &&
+       ((fun = find_identifier("handle_inherit", handler->prog)) != -1)) ||
+      ((handler = c->compat_handler) && handler->prog &&
+       ((fun = find_identifier("handle_inherit", handler->prog)) != -1))) {
+    apply_low(handler, fun, args);
+  } else {
+    apply_external(1, CE_HANDLE_INHERIT_FUN_NUM, args);
+  }
+}
+
 static void f_compilation__sprintf(INT32 args)
 {
   struct compilation *c = THIS_COMPILATION;
@@ -8578,6 +8615,9 @@ static void compile_compiler(void)
 	       f_compilation_change_compiler_compatibility,
 	       tFunc(tInt tInt, tVoid), 0);
 
+  ADD_FUNCTION("handle_inherit", f_compilation_handle_inherit,
+	       tFunc(tStr, tPrg(tObj)), 0);
+
   ADD_FUNCTION("_sprintf", f_compilation__sprintf,
 	       tFunc(tInt tOr(tMap(tStr, tMix), tVoid), tStr), ID_STATIC);
 
@@ -8630,6 +8670,9 @@ static void compile_compiler(void)
 	       f_compilation_env_get_default_module,
 	       tFunc(tNone, tOr(tMap(tStr, tMix), tObj)), 0);
 
+  ADD_FUNCTION("handle_inherit", f_compilation_env_handle_inherit,
+	       tFunc(tStr tStr tOr(tObj, tVoid), tPrg(tObj)), 0);
+
   {
     struct pike_string *type_name;
     struct svalue type_value;