diff -ur Python-1.5.2.org/Include/osdefs.h Python-1.5.2/Include/osdefs.h
--- Python-1.5.2.org/Include/osdefs.h	Fri Dec  5 21:39:24 1997
+++ Python-1.5.2/Include/osdefs.h	Thu Aug 10 14:50:48 2000
@@ -49,7 +49,7 @@
 #if defined(MS_WINDOWS) || defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__DJGPP__) || defined(PYOS_OS2)
 #define SEP '\\'
 #define ALTSEP '/'
-#define MAXPATHLEN 256
+#define MAXPATHLEN 260
 #define DELIM ';'
 #endif
 #endif
diff -ur Python-1.5.2.org/Lib/test/test_signal.py Python-1.5.2/Lib/test/test_signal.py
--- Python-1.5.2.org/Lib/test/test_signal.py	Sat Nov 22 21:50:24 1997
+++ Python-1.5.2/Lib/test/test_signal.py	Thu Aug 10 14:50:48 2000
@@ -4,6 +4,7 @@
 import os
 import sys
 
+# In fact, the OS/2 port compiled with EMX+GCC works okay, but for other's sake...
 if sys.platform[:3] in ('win', 'os2'):
     raise ImportError, "Can't test signal on %s" % sys.platform[:3]
 
diff -ur Python-1.5.2.org/Modules/posixmodule.c Python-1.5.2/Modules/posixmodule.c
--- Python-1.5.2.org/Modules/posixmodule.c	Wed Apr  7 15:49:40 1999
+++ Python-1.5.2/Modules/posixmodule.c	Thu Aug 10 14:50:48 2000
@@ -54,6 +54,10 @@
 #define  INCL_DOSPROCESS
 #define  INCL_NOPMAPI
 #include <os2.h>
+#if defined (PYCC_GCC)
+#include <ctype.h>
+#include <io.h>
+#endif
 #endif
 
 #include <sys/types.h>
@@ -112,6 +116,9 @@
 #define HAVE_SYSTEM	1
 #else /* 16-bit Windows */
 #endif /* !MS_WIN32 */
+#else
+#if defined (PYOS_OS2) && defined (PYCC_GCC)
+/* Everything needed is defined in PC/os2emx/config.h */
 #else			/* all other compilers */
 /* Unix functions that the configure script doesn't check for */
 #define HAVE_EXECV      1
@@ -129,6 +136,7 @@
 #define HAVE_SYSTEM	1
 #define HAVE_WAIT       1
 #define HAVE_TTYNAME	1
+#endif /* PYOS_OS2 && PYCC_GCC */
 #endif  /* _MSC_VER */
 #endif  /* __BORLANDC__ */
 #endif  /* ! __WATCOMC__ || __QNX__ */
@@ -603,7 +611,6 @@
 	PyObject *self;
 	PyObject *args;
 {
-	PyObject *file;
 	int id;
 	char *ret;
 
@@ -912,7 +919,7 @@
     if (srchcnt > 0) { /* If Directory is NOT Totally Empty, */
         do {
             if (ep.achName[0] == '.'
-            && (ep.achName[1] == '\0' || ep.achName[1] == '.' && ep.achName[2] == '\0'))
+            && (ep.achName[1] == '\0' || (ep.achName[1] == '.' && ep.achName[2] == '\0')))
                 continue; /* Skip Over "." and ".." Names */
 
             strcpy(namebuf, ep.achName);
@@ -1824,7 +1831,7 @@
 "popen(command [, mode='r' [, bufsize]]) -> pipe\n\
 Open a pipe to/from a command returning a file object.";
 
-#if defined(PYOS_OS2)
+#if defined(PYOS_OS2) && !defined (PYCC_GCC)
 static int
 async_system(const char *command)
 {
@@ -2046,7 +2053,7 @@
 	PyObject *self;
 	PyObject *args;
 {
-	int pid, sts;
+	int pid;
 #ifdef UNION_WAIT
 	union wait status;
 #define status_i (status.w_status)
@@ -3349,7 +3356,7 @@
     case 40: ver = "4.00"; break;
     case 50: ver = "5.00"; break;
     default:
-        sprintf(tmp, "%d-%d", values[QSV_VERSION_MAJOR],
+        sprintf(tmp, "%ld-%ld", values[QSV_VERSION_MAJOR],
                               values[QSV_VERSION_MINOR]);
         ver = &tmp[0];
     }
diff -ur Python-1.5.2.org/Modules/readline.c Python-1.5.2/Modules/readline.c
--- Python-1.5.2.org/Modules/readline.c	Fri Jan 29 21:55:02 1999
+++ Python-1.5.2/Modules/readline.c	Thu Aug 10 14:50:48 2000
@@ -233,6 +233,8 @@
 setup_readline()
 {
 	rl_readline_name = "python";
+	/* Allow $if term= in .inputrc to work */
+        rl_terminal_name = getenv ("TERM");
 	/* Force rebind of TAB to insert-tab */
 	rl_bind_key('\t', rl_insert);
 	/* Bind both ESC-TAB and ESC-ESC to the completion function */
diff -ur Python-1.5.2.org/Modules/selectmodule.c Python-1.5.2/Modules/selectmodule.c
--- Python-1.5.2.org/Modules/selectmodule.c	Fri Dec  4 18:50:04 1998
+++ Python-1.5.2/Modules/selectmodule.c	Thu Aug 10 14:50:48 2000
@@ -50,7 +50,7 @@
 
 #include <sys/types.h>
 
-#if defined(PYOS_OS2)
+#if defined(PYOS_OS2) && !defined (PYCC_GCC)
 #include <sys/time.h>
 #include <utils.h>
 #endif
diff -ur Python-1.5.2.org/Modules/signalmodule.c Python-1.5.2/Modules/signalmodule.c
--- Python-1.5.2.org/Modules/signalmodule.c	Mon Dec 21 19:32:38 1998
+++ Python-1.5.2/Modules/signalmodule.c	Thu Aug 10 14:50:48 2000
@@ -50,7 +50,7 @@
 #define SIG_ERR ((RETSIGTYPE (*)())-1)
 #endif
 
-#if defined(PYOS_OS2)
+#if defined (PYOS_OS2) && !defined (PYCC_GCC)
 #define NSIG 12
 #include <process.h>
 #endif
diff -ur Python-1.5.2.org/Modules/socketmodule.c Python-1.5.2/Modules/socketmodule.c
--- Python-1.5.2.org/Modules/socketmodule.c	Tue Apr 13 04:07:32 1999
+++ Python-1.5.2/Modules/socketmodule.c	Thu Aug 10 14:50:48 2000
@@ -185,7 +185,7 @@
 #define FORCE_ANSI_FUNC_DEFS
 #endif
 
-#if defined(PYOS_OS2)
+#if defined(PYOS_OS2) && !defined (PYCC_GCC)
 #define close soclose
 #define NO_DUP /* Sockets are Not Actual File Handles under OS/2 */
 #define FORCE_ANSI_FUNC_DEFS
@@ -254,7 +254,7 @@
 	else
 #endif
 
-#if defined(PYOS_OS2)
+#if defined(PYOS_OS2) && !defined (PYCC_GCC)
     if (sock_errno() != NO_ERROR) {
         APIRET rc;
         ULONG  msglen;
@@ -662,7 +662,7 @@
 				(void *)(&block), sizeof( int ) );
 #else
 #ifndef MS_WINDOWS
-#ifdef PYOS_OS2
+#if defined (PYOS_OS2) && !defined (PYCC_GCC)
 	block = !block;
 	ioctl(s->sock_fd, FIONBIO, (caddr_t)&block, sizeof(block));
 #else /* !PYOS_OS2 */
@@ -1121,7 +1121,7 @@
 	Py_BEGIN_ALLOW_THREADS
 	n = recvfrom(s->sock_fd, PyString_AsString(buf), len, flags,
 #ifndef MS_WINDOWS
-#if defined(PYOS_OS2)
+#if defined (PYOS_OS2) && !defined (PYCC_GCC)
 		     (struct sockaddr *)addrbuf, &addrlen
 #else
 		     (ANY *)addrbuf, &addrlen
@@ -1866,27 +1866,24 @@
 
 /* Additional initialization and cleanup for OS/2 */
 
-static void
-OS2cleanup()
-{
-    /* No cleanup is necessary for OS/2 Sockets */
-}
-
 static int
 OS2init()
 {
+#if !defined (PYCC_GCC)
     char reason[64];
     int rc = sock_init();
 
-    if (rc == 0) {
-	    atexit(OS2cleanup);
+    if (rc == 0)
 	    return 1; /* Indicate Success */
-    }
 
     sprintf(reason, "OS/2 TCP/IP Error# %d", sock_errno());
     PyErr_SetString(PyExc_ImportError, reason);
 
     return 0;  /* Indicate Failure */
+#else
+    /* no need to initialize sockets under OS/2 with GCC/EMX */
+    return 1;
+#endif
 }
 
 #endif /* PYOS_OS2 */
@@ -1984,7 +1981,7 @@
 		return;
 	m = Py_InitModule3("_socket", PySocket_methods, module_doc);
 #else
-#if defined(__TOS_OS2__)
+#if defined(PYOS_OS2)
 	if (!OS2init())
 		return;
 	m = Py_InitModule3("_socket", PySocket_methods, module_doc);
diff -ur Python-1.5.2.org/Modules/timemodule.c Python-1.5.2/Modules/timemodule.c
--- Python-1.5.2.org/Modules/timemodule.c	Mon Apr  5 21:54:14 1999
+++ Python-1.5.2/Modules/timemodule.c	Thu Aug 10 16:05:16 2000
@@ -53,7 +53,7 @@
 #include <unistd.h>
 #endif
 
-#if defined(HAVE_SELECT) && !defined(__BEOS__)
+#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
 #include "myselect.h"
 #else
 #include "mytime.h"
@@ -87,6 +87,12 @@
 #undef HAVE_CLOCK /* We have our own version down below */
 #endif /* MS_WIN32 */
 
+#ifdef PYOS_OS2
+#define INCL_DOS
+#define INCL_ERRORS
+#include <os2.h>
+#endif
+
 #if defined(PYCC_VACPP)
 #include <sys/time.h>
 #endif
@@ -409,7 +415,10 @@
 #endif /* HAVE_STRFTIME */
 
 #ifdef HAVE_STRPTIME
-/* extern char *strptime(); /* Enable this if it's not declared in <time.h> */
+
+#if 0
+extern char *strptime(); /* Enable this if it's not declared in <time.h> */
+#endif
 
 static PyObject *
 time_strptime(self, args)
@@ -741,7 +750,7 @@
 #endif /* MPW */
 {
 /* XXX Should test for MS_WIN32 first! */
-#if defined(HAVE_SELECT) && !defined(__BEOS__)
+#if defined(HAVE_SELECT) && !defined(__BEOS__) && !defined(__EMX__)
 	struct timeval t;
 	double frac;
 	frac = fmod(secs, 1.0);
diff -ur Python-1.5.2.org/Objects/fileobject.c Python-1.5.2/Objects/fileobject.c
--- Python-1.5.2.org/Objects/fileobject.c	Sat Apr 10 15:48:22 1999
+++ Python-1.5.2/Objects/fileobject.c	Thu Aug 10 14:50:48 2000
@@ -60,6 +60,10 @@
 #define NO_FOPEN_ERRNO
 #endif
 
+#if defined (PYOS_OS2) && defined (PYCC_GCC)
+#include <io.h>
+#endif
+
 #define BUF(v) PyString_AS_STRING((PyStringObject *)v)
 
 #include <errno.h>
diff -ur Python-1.5.2.org/Objects/floatobject.c Python-1.5.2/Objects/floatobject.c
--- Python-1.5.2.org/Objects/floatobject.c	Fri Mar 19 20:59:40 1999
+++ Python-1.5.2/Objects/floatobject.c	Thu Aug 10 14:50:48 2000
@@ -685,7 +685,8 @@
 					PyFloat_AsString(buf, p);
 					fprintf(stderr,
 			     "#   <float at %lx, refcnt=%d, val=%s>\n",
-						p, p->ob_refcnt, buf);
+						(long unsigned int)p,
+						p->ob_refcnt, buf);
 				}
 			}
 			list = list->next;
diff -ur Python-1.5.2.org/Objects/intobject.c Python-1.5.2/Objects/intobject.c
--- Python-1.5.2.org/Objects/intobject.c	Fri Mar 19 20:30:38 1999
+++ Python-1.5.2/Objects/intobject.c	Thu Aug 10 14:50:50 2000
@@ -893,7 +893,8 @@
 				if (PyInt_Check(p) && p->ob_refcnt != 0)
 					fprintf(stderr,
 				"#   <int at %lx, refcnt=%d, val=%ld>\n",
-						p, p->ob_refcnt, p->ob_ival);
+						(long unsigned int)p,
+						p->ob_refcnt, p->ob_ival);
 			}
 			list = list->next;
 		}
diff -ur Python-1.5.2.org/PC/getpathp.c Python-1.5.2/PC/getpathp.c
--- Python-1.5.2.org/PC/getpathp.c	Tue Aug 11 20:35:56 1998
+++ Python-1.5.2/PC/getpathp.c	Thu Aug 10 14:50:50 2000
@@ -40,6 +40,11 @@
 extern BOOL PyWin_IsWin32s();
 #endif
 
+#if defined (PYOS_OS2)
+#define INCL_DOS
+#include <os2.h>
+#endif
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <string.h>
@@ -275,12 +280,21 @@
 get_progpath()
 {
 	extern char *Py_GetProgramName();
+	extern void Py_SetProgramName(char *);
 	char *path = getenv("PATH");
 	char *prog = Py_GetProgramName();
 
 #ifdef MS_WIN32
 	if (GetModuleFileName(NULL, progpath, MAXPATHLEN))
 		return;
+#endif
+#if defined (PYOS_OS2)
+	{
+		PPIB pib;
+		if ((DosGetInfoBlocks (NULL, &pib) == 0)
+		 && (DosQueryModuleName (pib->pib_hmte, sizeof (progpath), progpath) == 0))
+			return;
+	}
 #endif
 	if (prog == NULL || *prog == '\0')
 		prog = "python";
diff -ur Python-1.5.2.org/PC/os2vacpp/getpathp.c Python-1.5.2/PC/os2vacpp/getpathp.c
--- Python-1.5.2.org/PC/os2vacpp/getpathp.c	Mon Jul 27 13:49:04 1998
+++ Python-1.5.2/PC/os2vacpp/getpathp.c	Thu Aug 10 14:50:50 2000
@@ -344,6 +344,7 @@
 	get_progpath();
 	strcpy(argv0_path, progpath);
 	reduce(argv0_path);
+
 	if (pythonhome == NULL || *pythonhome == '\0') {
 		if (search_for_prefix(argv0_path, LANDMARK))
 			pythonhome = prefix;
@@ -486,7 +487,7 @@
 	if (!module_search_path)
 		calculate_path();
 
-	return prefix;
+	return Py_GetPath;
 }
 
 char *
diff -ur Python-1.5.2.org/Python/fmod.c Python-1.5.2/Python/fmod.c
--- Python-1.5.2.org/Python/fmod.c	Fri Oct 25 14:43:04 1996
+++ Python-1.5.2/Python/fmod.c	Thu Aug 10 14:50:50 2000
@@ -36,6 +36,9 @@
 #include "mymath.h"
 #include <errno.h>
 
+/* BUG WARNING: fmod (-10,1) returns -1!!! */
+/* if someone cares to fix it, please do */
+
 double
 fmod(double x, double y)
 {
diff -ur Python-1.5.2.org/Python/importdl.c Python-1.5.2/Python/importdl.c
--- Python-1.5.2.org/Python/importdl.c	Wed Jan 27 17:53:10 1999
+++ Python-1.5.2/Python/importdl.c	Thu Aug 10 14:50:50 2000
@@ -589,8 +589,8 @@
 		if (rc != NO_ERROR) {
 			char errBuf[256];
 			sprintf(errBuf,
-				"DLL load failed, rc = %d, problem '%s': %s",
-				rc, failreason);
+				"%s: DLL load failed, rc = %ld, problem '%s'",
+				pathname, rc, failreason);
 			PyErr_SetString(PyExc_ImportError, errBuf);
 			return NULL;
 		}
diff -ur Python-1.5.2.org/Python/strtod.c Python-1.5.2/Python/strtod.c
--- Python-1.5.2.org/Python/strtod.c	Wed Apr  7 16:07:22 1999
+++ Python-1.5.2/Python/strtod.c	Thu Aug 10 14:50:50 2000
@@ -1,4 +1,5 @@
 #include "config.h"
+#include <stdio.h>
 
 /* comp.sources.misc strtod(), as posted in comp.lang.tcl,
    with bugfix for "123000.0" and acceptance of space after 'e' sign nuked.
@@ -79,7 +80,7 @@
 	dp = buffer;	
 	*dp++ = '0'; *dp++ = '.';
 	buforg = dp, buflim = buffer+48;
-	for (save = sp; c = *sp; sp++)
+	for (save = sp; (c = *sp); sp++)
 	    if (c == '.') {
 		if (dotseen) break;
 		dotseen++;
diff -ur Python-1.5.2.org/Python/thread_os2.h Python-1.5.2/Python/thread_os2.h
--- Python-1.5.2.org/Python/thread_os2.h	Mon Dec 21 19:32:32 1998
+++ Python-1.5.2/Python/thread_os2.h	Thu Aug 10 15:48:26 2000
@@ -38,8 +38,15 @@
 
 #include "process.h"
 
-long PyThread_get_thread_ident(void);
+#ifdef __EMX__
+#  include <sys/builtin.h>
+#  include <sys/fmutex.h>
+#endif
 
+/*
+#undef dprintf
+#define dprintf(args) printf args
+*/
 
 /*
  * Initialization of the C package, should not be needed.
@@ -53,156 +60,197 @@
  */
 int PyThread_start_new_thread(void (*func)(void *), void *arg)
 {
-  int aThread;
-  int success = 1;
+	int aThread;
+	int success = 1;
 
-  aThread = _beginthread(func,NULL,65536,arg);
+	aThread = _beginthread(func,NULL,65536,arg);
 
-  if( aThread == -1 ) {
-    success = 0;
-    fprintf(stderr,"aThread failed == %d",aThread);
-    dprintf(("_beginthread failed. return %ld\n", errno));
-  }
+	if (aThread == -1)
+	{
+		success = 0;
+		fprintf(stderr,"aThread failed == %d",aThread);
+		dprintf (("_beginthread failed. return %d\n", errno));
+	}
 
-  return success;
+	return success;
 }
 
 long PyThread_get_thread_ident(void)
 {
-  PPIB pib;
-  PTIB tib;
+#if defined (PYCC_GCC)
+	if (!initialized)
+		PyThread_init_thread();
+	return _gettid ();
+#else
+	PPIB pib;
+	PTIB tib;
 
-  if (!initialized)
-    PyThread_init_thread();
+	if (!initialized)
+		PyThread_init_thread();
         
-  DosGetInfoBlocks(&tib,&pib);
-  return tib->tib_ptib2->tib2_ultid;
+	DosGetInfoBlocks(&tib,&pib);
+	return tib->tib_ptib2->tib2_ultid;
+#endif
 }
 
 static void do_PyThread_exit_thread(int no_cleanup)
 {
-  dprintf(("%ld: PyThread_exit_thread called\n", PyThread_get_thread_ident()));
-  if (!initialized)
-    if (no_cleanup)
-      _exit(0);
-    else
-      exit(0);
-  _endthread();
+	dprintf (("%ld: PyThread_exit_thread(%d)\n", PyThread_get_thread_ident(), no_cleanup));
+	if (!initialized)
+	{
+		if (no_cleanup)
+			_exit(0);
+		else
+			exit(0);
+	}
+	_endthread();
 }
 
 void PyThread_exit_thread(void)
 {
-  do_PyThread_exit_thread(0);
+	dprintf (("%ld: PyThread_exit_thread()\n", PyThread_get_thread_ident()));
+	do_PyThread_exit_thread(0);
 }
 
 void PyThread__exit_thread(void)
 {
-  do_PyThread_exit_thread(1);
+	dprintf (("%ld: PyThread__exit_thread()\n", PyThread_get_thread_ident()));
+	do_PyThread_exit_thread(1);
 }
 
 #ifndef NO_EXIT_PROG
 static void do_PyThread_exit_prog(int status, int no_cleanup)
 {
-  dprintf(("PyThread_exit_prog(%d) called\n", status));
-  if (!initialized)
-    if (no_cleanup)
-      _exit(status);
-    else
-      exit(status);
+	dprintf(("PyThread_exit_prog(%d) called\n", status));
+	if (!initialized)
+		if (no_cleanup)
+			_exit(status);
+		else
+			exit(status);
 }
 
 void PyThread_exit_prog(int status)
 {
-  do_PyThread_exit_prog(status, 0);
+	do_PyThread_exit_prog(status, 0);
 }
 
 void PyThread__exit_prog _P1(int status)
 {
-  do_PyThread_exit_prog(status, 1);
+	do_PyThread_exit_prog(status, 1);
 }
 #endif /* NO_EXIT_PROG */
 
 /*
  * Lock support. It has too be implemented as semaphores.
- * I [Dag] tried to implement it with mutex but I could find a way to
- * tell whether a thread already own the lock or not.
+ * With gcc/emx use fast RAM semaphores; this has several advantages:
+ *	- EMX fast semaphores are not owned by threads: this means
+ *	  secondary acquirement of same lock will result in normal
+ *	  "wait-to-be-released" operation, in contrast to OS/2
+ *	  mutex semaphores which work different way.
+ *	- Because of same reason releasing a lock from other thread
+ *	  works okay (as expected by Python) while with HMTX it doesn't.
+ *	- Signals works okay during semaphore wait. This particularily
+ *	  means you can Ctrl-C your bugs.
  */
 PyThread_type_lock PyThread_allocate_lock(void)
 {
-  HMTX   aLock;
-  APIRET rc;
-
-  dprintf(("PyThread_allocate_lock called\n"));
-  if (!initialized)
-    PyThread_init_thread();
+#ifdef __EMX__
+	_fmutex *sem = malloc (sizeof (_fmutex));
+	if (!initialized)
+		PyThread_init_thread ();
+	dprintf (("%ld: PyThread_allocate_lock() -> %lx\n", PyThread_get_thread_ident(), (long)sem));
+	if (_fmutex_create (sem, 0))
+	{
+		free (sem);
+		sem = NULL;
+	}
+	return (PyThread_type_lock)sem;
+#else
+	HMTX   aLock;
+
+	if (!initialized)
+		PyThread_init_thread();
 
-  DosCreateMutexSem(NULL,  /* Sem name      */
-                    &aLock, /* the semaphone */
-                    0,     /* shared ?      */
-                    0);    /* initial state */  
+	DosCreateMutexSem (NULL, &aLock, 0, 0);
 
-  dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", PyThread_get_thread_ident(), (long)aLock));
+	dprintf(("%ld: PyThread_allocate_lock() -> %lx\n", PyThread_get_thread_ident(), (long)aLock));
 
-  return (PyThread_type_lock) aLock;
+	return (PyThread_type_lock) aLock;
+#endif
 }
 
 void PyThread_free_lock(PyThread_type_lock aLock)
 {
-  dprintf(("%ld: PyThread_free_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
-
-  DosCloseMutexSem((HMTX)aLock);
+	dprintf (("%ld: PyThread_free_lock(%lx)\n", PyThread_get_thread_ident(),(long)aLock));
+#ifdef __EMX__
+	_fmutex_close ((_fmutex *)aLock);
+	free ((_fmutex *)aLock);
+#else
+	DosCloseMutexSem((HMTX)aLock);
+#endif
 }
 
 /*
  * Return 1 on success if the lock was acquired
- *
- * and 0 if the lock was not acquired. This means a 0 is returned
- * if the lock has already been acquired by this thread!
  */
 int PyThread_acquire_lock(PyThread_type_lock aLock, int waitflag)
 {
-  int   success = 1;
-  ULONG rc, count;
-  PID   pid = 0;
-  TID   tid = 0;
-
-  dprintf(("%ld: PyThread_acquire_lock(%lx, %d) called\n", PyThread_get_thread_ident(),
-           (long)aLock, waitflag));
-
-  DosQueryMutexSem((HMTX)aLock,&pid,&tid,&count);
-  if( tid == PyThread_get_thread_ident() ) { /* if we own this lock */
-    success = 0;
-  } else {
-    rc = DosRequestMutexSem((HMTX) aLock,
+#ifdef __EMX__
+	dprintf (("%ld: PyThread_acquire_lock(%lx, %d)\n", PyThread_get_thread_ident(),	(long)aLock, waitflag));
+	if (_fmutex_request ((_fmutex *)aLock, waitflag ? 0 : _FMR_NOWAIT))
+		return 0;
+	return 1;
+#else
+	int   success = 1;
+	ULONG rc, count;
+	PID   pid = 0;
+	TID   tid = 0;
+
+	dprintf (("%ld: PyThread_acquire_lock(%lx, %d)\n", PyThread_get_thread_ident(),	(long)aLock, waitflag));
+
+	DosQueryMutexSem((HMTX)aLock,&pid,&tid,&count);
+	if (tid == PyThread_get_thread_ident ())
+	{ /* if we own this lock */
+	    success = 0;
+	}
+	else
+	{
+		rc = DosRequestMutexSem((HMTX) aLock,
                             (waitflag == 1 ? SEM_INDEFINITE_WAIT : 0));
     
-    if( rc != 0) {
-      success = 0;                /* We failed */
+		if( rc != 0)
+			success = 0;                /* We failed */
     }
-  }
 
-  dprintf(("%ld: PyThread_acquire_lock(%lx, %d) -> %d\n",
-           PyThread_get_thread_ident(),(long)aLock, waitflag, success));
+	dprintf(("%ld: PyThread_acquire_lock(%lx, %d) -> %d\n",
+		PyThread_get_thread_ident(),(long)aLock, waitflag, success));
 
-  return success;
+	return success;
+#endif
 }
 
 void PyThread_release_lock(PyThread_type_lock aLock)
 {
-  dprintf(("%ld: PyThread_release_lock(%lx) called\n", PyThread_get_thread_ident(),(long)aLock));
-
-  if ( DosReleaseMutexSem( (HMTX) aLock ) != 0 ) {
-    dprintf(("%ld: Could not PyThread_release_lock(%lx) error: %l\n",
-             PyThread_get_thread_ident(), (long)aLock, GetLastError()));
-  }
+#ifdef __EMX__
+	dprintf(("%ld: PyThread_release_lock(%lx)\n", PyThread_get_thread_ident(), (long)aLock));
+	_fmutex_release ((_fmutex *)aLock);
+#else
+	int rc;
+	dprintf(("%ld: PyThread_release_lock(%lx)\n", PyThread_get_thread_ident(), (long)aLock));
+
+	rc = DosReleaseMutexSem ((HMTX) aLock);
+	if (rc)
+		dprintf (("%ld: Could not PyThread_release_lock(%lx) error: %d\n",
+			PyThread_get_thread_ident(), (long)aLock, rc));
+#endif
 }
 
 /*
- * Semaphore support.
+ * Semaphore support: not implemented since semaphores are not used
  */
 PyThread_type_sema PyThread_allocate_sema(int value)
 {
-  return (PyThread_type_sema) 0;
+	return (PyThread_type_sema) 0;
 }
 
 void PyThread_free_sema(PyThread_type_sema aSemaphore)
@@ -212,10 +260,10 @@
 
 int PyThread_down_sema(PyThread_type_sema aSemaphore, int waitflag)
 {
-  return -1;
+	return -1;
 }
 
 void PyThread_up_sema(PyThread_type_sema aSemaphore)
 {
-  dprintf(("%ld: PyThread_up_sema(%lx)\n", PyThread_get_thread_ident(), (long)aSemaphore));
+	dprintf(("%ld: PyThread_up_sema(%lx)\n", PyThread_get_thread_ident(), (long)aSemaphore));
 }
