*** 1.56	1997/11/07 18:24:19
--- runtime/python/Imakefile	1998/01/29 03:10:17
***************
*** 45,51 ****
--- 45,56 ----
  #endif
  #endif
  
+ #if ((PYTHON_MAJOR_VERSION == 1) && (PYTHON_MINOR_VERSION >= 5))
+ LOCALINCLUDES	= -I$(PYTHON_PREFIX)/include/$(PYTHON_INCLUDE_SUFFIX) -I$(PYTHON_EXEC_PREFIX)/include/$(PYTHON_SUFFIX) -I../kernel ${ISEC}
+ #else
  LOCALINCLUDES	= -I$(PYTHON_PREFIX)/include/$(PYTHON_INCLUDE_SUFFIX) -I$(PYTHON_EXEC_PREFIX)/lib/$(PYTHON_EXEC_SUFFIX) -I../kernel ${ISEC}
+ #endif /* ((PYTHON_MAJOR_VERSION == 1) && (PYTHON_MINOR_VERSION >= 5)) */
+ 
  ILUCINCLUDES =
  KERNELHEADERS = ../kernel/iluxport.h ../kernel/iluerror.h ../kernel/iluerrs.h ../kernel/iluconf.h ../kernel/ilubasic.h
  
*** 1.6	1996/10/08 02:10:11
--- runtime/python/pythonthreads.h	1998/01/29 04:31:55
***************
*** 1,20 ****
  /* $Id: patches.html,v 1.45 1998/04/04 01:37:11 janssen Exp janssen $ */
  
! #ifdef ILU_PYTHON_THREADS
  extern ilu_boolean ilupython_threaded_operation;
  #define EXIT_INTERPRETER(if_what) { PyObject *_save; { if (if_what) { _save = PyEval_SaveThread();  } }
  #define ENTER_INTERPRETER(if_what) { if (if_what) { PyEval_RestoreThread(_save); } } }
  #define CALL_KERNEL(unblock_cond, call) { EXIT_INTERPRETER(unblock_cond); call; ENTER_INTERPRETER(unblock_cond); }
  #define ILUPY_ILLEGAL_IN_THREADED(unless_what) if (ilupython_threaded_operation && !unless_what) { char buf[1000]; sprintf(buf, "illegal internal call in threaded runtime:  " # unless_what); Py_FatalError(buf); }
  #define ILUPY_ILLEGAL_IN_UNTHREADED if (!ilupython_threaded_operation) _ilu_Assert(0, "illegal internal call in single-threaded runtime"); 
! #else
  #define ilupython_threaded_operation ilu_FALSE
  #define CALL_KERNEL(unblock_cond, call) call
  #define EXIT_INTERPRETER(if_what)
  #define ENTER_INTERPRETER(if_what)
  #define ILUPY_ILLEGAL_IN_THREADED(unless_what)
  #define ILUPY_ILLEGAL_IN_UNTHREADED _ilu_Assert(0, "illegal internal call in single-threaded runtime");
  #endif /* ILU_PYTHON_THREADS */
  
- #define NEW_THREAD_ENTER { { if (ilupython_threaded_operation) PyEval_RestoreThread(NULL); }
- #define FINISHED_THREAD_EXIT { if (ilupython_threaded_operation) { if (PyEval_SaveThread() != NULL) { fprintf(stderr, "unexpected non-NULL stack from exiting thread\n"); } } } }
--- 1,44 ----
  /* $Id: patches.html,v 1.45 1998/04/04 01:37:11 janssen Exp janssen $ */
  
! #ifdef ILU_PYTHON_THREADS	/* thread support compiled into Python */
! 
  extern ilu_boolean ilupython_threaded_operation;
+ 
+ #if ((ILUPYTHON_MAJOR_VERSION == 1) && (ILUPYTHON_MINOR_VERSION >= 5))
+ 
+ extern ilu_boolean _ilupython_holding_interpreter_lock;
+ 
+ void _ilupython_new_thread_enter_interpreter(void);
+ void _ilupython_dying_thread_release_interpreter(void);
+ 
+ #define EXIT_INTERPRETER(if_what) { PyThreadState *_save = ILU_NIL; { if (if_what) { _ilu_Assert(_ilupython_holding_interpreter_lock,"attempt to leave Python interpreter thread lock while not holding it!"); _ilupython_holding_interpreter_lock = ilu_FALSE; _save = PyEval_SaveThread(); _ilu_Assert(_save!=ILU_NIL,"NIL thread state when releasing Python interpreter lock"); } }
+ #define ENTER_INTERPRETER(if_what) { if (if_what) { _ilu_Assert(_save != ILU_NIL,"NIL thread state when attempting to regain interpreter lock"); PyEval_RestoreThread(_save); _ilupython_holding_interpreter_lock = ilu_TRUE; } } }
+ #define NEW_THREAD_ENTER { { if (ilupython_threaded_operation) _ilupython_new_thread_enter_interpreter(); }
+ #define FINISHED_THREAD_EXIT { if (ilupython_threaded_operation) { _ilupython_dying_thread_release_interpreter(); } } }
+ 
+ #else	/* Python version less than 1.5 */
+ 
  #define EXIT_INTERPRETER(if_what) { PyObject *_save; { if (if_what) { _save = PyEval_SaveThread();  } }
  #define ENTER_INTERPRETER(if_what) { if (if_what) { PyEval_RestoreThread(_save); } } }
+ #define NEW_THREAD_ENTER { { if (ilupython_threaded_operation) PyEval_RestoreThread(NULL); }
+ #define FINISHED_THREAD_EXIT { if (ilupython_threaded_operation) { if (PyEval_SaveThread() != NULL) { fprintf(stderr, "unexpected non-NULL stack from exiting thread\n"); } } } }
+ 
+ #endif /* ((ILUPYTHON_MAJOR_VERSION >= 1) && (ILUPYTHON_MINOR_VERSION >= 5)) */
+ 
  #define CALL_KERNEL(unblock_cond, call) { EXIT_INTERPRETER(unblock_cond); call; ENTER_INTERPRETER(unblock_cond); }
  #define ILUPY_ILLEGAL_IN_THREADED(unless_what) if (ilupython_threaded_operation && !unless_what) { char buf[1000]; sprintf(buf, "illegal internal call in threaded runtime:  " # unless_what); Py_FatalError(buf); }
  #define ILUPY_ILLEGAL_IN_UNTHREADED if (!ilupython_threaded_operation) _ilu_Assert(0, "illegal internal call in single-threaded runtime"); 
! 
! #else	/* no threads compiled into python */
! 
  #define ilupython_threaded_operation ilu_FALSE
  #define CALL_KERNEL(unblock_cond, call) call
  #define EXIT_INTERPRETER(if_what)
  #define ENTER_INTERPRETER(if_what)
+ #define NEW_THREAD_ENTER
+ #define FINISHED_THREAD_EXIT
  #define ILUPY_ILLEGAL_IN_THREADED(unless_what)
  #define ILUPY_ILLEGAL_IN_UNTHREADED _ilu_Assert(0, "illegal internal call in single-threaded runtime");
+ 
  #endif /* ILU_PYTHON_THREADS */
  
*** 1.224	1997/11/25 22:18:42
--- runtime/python/iluPrmodule.c	1998/01/29 04:31:53
***************
*** 131,136 ****
--- 131,142 ----
  static ilu_Condition ilupython_MainLoopCondition = ILU_NIL;
  
  #define current_thread_id() PyInt_FromLong(get_thread_ident())
+ 
+ #if ((ILUPYTHON_MAJOR_VERSION >= 1) && (ILUPYTHON_MINOR_VERSION >= 5))
+ PyInterpreterState *_ilupython_interpreter_state = ILU_NIL;
+ ilu_boolean _ilupython_holding_interpreter_lock = ilu_TRUE;
+ #endif
+ 
  #endif /* ILU_PYTHON_THREADS */
  
  /***********************************************************************
***************
*** 8107,8112 ****
--- 8113,8126 ----
        return 0;
      }
  
+ #if ((ILUPYTHON_MAJOR_VERSION >= 1) && (ILUPYTHON_MINOR_VERSION >= 5))
+   {
+     PyThreadState *current = PyThreadState_Get();
+     _ilu_Assert(current != ILU_NIL, "NIL thread state in ilu.ThreadedOperation()");
+     _ilupython_interpreter_state = current->interp;
+   }
+ #endif
+ 
    ilu_NewConnectionGetterForked(&err);
    ilupython_fork_thread(ilupython_collect_outgoing_connections, NULL);
  
***************
*** 8777,8782 ****
--- 8791,8813 ----
  
    /* exit_thread(); */
  }
+ 
+ void
+   _ilupython_new_thread_enter_interpreter (void)
+ {
+   PyThreadState *newstate = PyThreadState_New(_ilupython_interpreter_state);
+   PyEval_AcquireLock();
+   PyThreadState_Swap(newstate);
+ }
+ 
+ void
+   _ilupython_dying_thread_release_interpreter (void)
+ {
+   PyThreadState *oldstate = PyThreadState_Swap(NULL);
+   PyThreadState_Delete(oldstate);
+   PyEval_ReleaseLock();
+ }
+ 
  #endif /* ILU_PYTHON_THREADS */
  
  static void printPyObject(PyObject *o)
*** 1.1	1998/07/17 01:43:12
--- runtime/python/iluPrmodule.c	1998/07/17 02:12:40
***************
*** 134,140 ****
  
  #if ((ILUPYTHON_MAJOR_VERSION >= 1) && (ILUPYTHON_MINOR_VERSION >= 5))
  PyInterpreterState *_ilupython_interpreter_state = ILU_NIL;
- ilu_boolean _ilupython_holding_interpreter_lock = ilu_TRUE;
  #endif
  
  #endif /* ILU_PYTHON_THREADS */
--- 134,139 ----
***************
*** 8569,8584 ****
  
    ILUPY_ILLEGAL_IN_UNTHREADED;
  
-   /* I'm not sure this thread ever really needs to be in the interpreter at all. */
-   NEW_THREAD_ENTER;
- 
    while (!closed)
      {
-       EXIT_INTERPRETER(ilupython_threaded_operation);
        ilu_WaitForPortConnectionRequest(port, &err);
- 
        conn = ilu_HandleNewConnection(port, &closed, &err);
-       ENTER_INTERPRETER(ilupython_threaded_operation);
  
        if (ILU_ERRNOK(err))
  	{
--- 8568,8577 ----
***************
*** 8590,8597 ****
  	    ilupython_fork_thread(runConnection, (void *)conn);
  	}
      }
- 
-   FINISHED_THREAD_EXIT;
  
    exit_thread();
  }
--- 8583,8588 ----
*** 1.1	1998/07/17 02:07:32
--- runtime/python/pythonthreads.h	1998/07/17 02:09:10
***************
*** 6,18 ****
  
  #if ((ILUPYTHON_MAJOR_VERSION == 1) && (ILUPYTHON_MINOR_VERSION >= 5))
  
- extern ilu_boolean _ilupython_holding_interpreter_lock;
- 
  void _ilupython_new_thread_enter_interpreter(void);
  void _ilupython_dying_thread_release_interpreter(void);
  
! #define EXIT_INTERPRETER(if_what) { PyThreadState *_save = ILU_NIL; { if (if_what) { _ilu_Assert(_ilupython_holding_interpreter_lock,"attempt to leave Python interpreter thread lock while not holding it!"); _ilupython_holding_interpreter_lock = ilu_FALSE; _save = PyEval_SaveThread(); _ilu_Assert(_save!=ILU_NIL,"NIL thread state when releasing Python interpreter lock"); } }
! #define ENTER_INTERPRETER(if_what) { if (if_what) { _ilu_Assert(_save != ILU_NIL,"NIL thread state when attempting to regain interpreter lock"); PyEval_RestoreThread(_save); _ilupython_holding_interpreter_lock = ilu_TRUE; } } }
  #define NEW_THREAD_ENTER { { if (ilupython_threaded_operation) _ilupython_new_thread_enter_interpreter(); }
  #define FINISHED_THREAD_EXIT { if (ilupython_threaded_operation) { _ilupython_dying_thread_release_interpreter(); } } }
  
--- 6,16 ----
  
  #if ((ILUPYTHON_MAJOR_VERSION == 1) && (ILUPYTHON_MINOR_VERSION >= 5))
  
  void _ilupython_new_thread_enter_interpreter(void);
  void _ilupython_dying_thread_release_interpreter(void);
  
! #define EXIT_INTERPRETER(if_what) { PyThreadState *_save = ILU_NIL; { if (if_what) { _save = PyEval_SaveThread(); _ilu_Assert(_save!=ILU_NIL,"NIL thread state when releasing Python interpreter lock"); } }
! #define ENTER_INTERPRETER(if_what) { if (if_what) { _ilu_Assert(_save != ILU_NIL,"NIL thread state when attempting to regain interpreter lock"); PyEval_RestoreThread(_save); } } }
  #define NEW_THREAD_ENTER { { if (ilupython_threaded_operation) _ilupython_new_thread_enter_interpreter(); }
  #define FINISHED_THREAD_EXIT { if (ilupython_threaded_operation) { _ilupython_dying_thread_release_interpreter(); } } }
  
