Updated version of RISCOS support. SF patch 411213 by Dietmar Schwertberger
diff --git a/Lib/plat-riscos/riscospath.py b/Lib/plat-riscos/riscospath.py
index b83c632..a38b22c 100644
--- a/Lib/plat-riscos/riscospath.py
+++ b/Lib/plat-riscos/riscospath.py
@@ -203,21 +203,30 @@
   """
 Test whether a path exists.
 """
-  return swi.swi('OS_File', '5s;i', p)!=0
+  try:
+    return swi.swi('OS_File', '5s;i', p)!=0
+  except swi.error:
+    return 0
 
 
 def isdir(p):
   """
 Is a path a directory? Includes image files.
 """
-  return swi.swi('OS_File', '5s;i', p) in [2, 3]
+  try:
+    return swi.swi('OS_File', '5s;i', p) in [2, 3]
+  except swi.error:
+    return 0
 
 
 def isfile(p):
   """
 Test whether a path is a file, including image files.
 """
-  return swi.swi('OS_File', '5s;i', p) in [1, 3]
+  try:
+    return swi.swi('OS_File', '5s;i', p) in [1, 3]
+  except swi.error:
+    return 0
 
 
 def islink(p):
diff --git a/Modules/main.c b/Modules/main.c
index b4566c9..0183647 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -26,10 +26,18 @@
 static char **orig_argv;
 static int  orig_argc;
 
-/* For my_readline when running under RISCOS */
-#ifdef RISCOS
+/* command line options */
+#define BASE_OPTS "c:diOStuUvxXhVW:"
+
+#ifndef RISCOS
+#define PROGRAM_OPTS BASE_OPTS
+#else /*RISCOS*/
+/* extra option saying that we are running under a special task window
+   frontend; especially my_readline will behave different */
+#define PROGRAM_OPTS BASE_OPTS "w"
+/* corresponding flag */
 extern int Py_RISCOSWimpFlag;
-#endif
+#endif /*RISCOS*/
 
 /* Short usage message (with %s for argv0) */
 static char *usage_line =
@@ -115,11 +123,7 @@
 
 	PySys_ResetWarnOptions();
 
-#ifdef RISCOS
-	while ((c = getopt(argc, argv, "c:diOStuUvwxXhV")) != EOF) {
-#else
-	while ((c = _PyOS_GetOpt(argc, argv, "c:diOStuUvxXhVW:")) != EOF) {
-#endif
+	while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
 		if (c == 'c') {
 			/* -c is the last option; following arguments
 			   that look like options are left for the
diff --git a/Modules/timemodule.c b/Modules/timemodule.c
index cfce06b..0c5c257 100644
--- a/Modules/timemodule.c
+++ b/Modules/timemodule.c
@@ -756,9 +756,7 @@
 #if defined(__WATCOMC__) && !defined(__QNX__)
 	/* XXX Can't interrupt this sleep */
 	Py_BEGIN_ALLOW_THREADS
-#ifndef RISCOS
 	delay((int)(secs * 1000 + 0.5));  /* delay() uses milliseconds */
-#endif
 	Py_END_ALLOW_THREADS
 #else /* !__WATCOMC__ || __QNX__ */
 #ifdef MSDOS
@@ -831,10 +829,20 @@
 		Py_END_ALLOW_THREADS
 	}
 #else /* !__BEOS__ */
+#ifdef RISCOS
+	if (secs <= 0.0)
+		return 0;
+	Py_BEGIN_ALLOW_THREADS
+	/* This sleep *CAN BE* interrupted. */
+	if ( sleep(secs) )
+		return -1;
+	Py_END_ALLOW_THREADS
+#else /* !RISCOS */
 	/* XXX Can't interrupt this sleep */
 	Py_BEGIN_ALLOW_THREADS
 	sleep((int)secs);
 	Py_END_ALLOW_THREADS
+#endif /* !RISCOS */
 #endif /* !__BEOS__ */
 #endif /* !PYOS_OS2 */
 #endif /* !MS_WIN32 */
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index 9a0a43b..52fbbc8 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -808,7 +808,11 @@
 		if (argc > 0 && argv0 != NULL)
 			p = strrchr(argv0, SEP);
 		if (p != NULL) {
+#ifndef RISCOS
 			n = p + 1 - argv0;
+#else /* don't include trailing separator */
+			n = p - argv0;
+#endif /* RISCOS */
 #if SEP == '/' /* Special case for Unix filename syntax */
 			if (n > 1)
 				n--; /* Drop trailing separator */
diff --git a/RISCOS/Makefile b/RISCOS/Makefile
index 38973f0..e970d13 100644
--- a/RISCOS/Makefile
+++ b/RISCOS/Makefile
@@ -12,9 +12,9 @@
 OBJSCAN = $(DLKLIB).objscan
 MAKEDLK = $(DLKLIB).makedlk
 
-# change from time to time
+# change from time to time (don't forget to change !Boot also)
 TARGET=Python21
-BUILD=10
+BUILD=12
 
 
 #
@@ -23,7 +23,7 @@
 OSLIBS = OSLib:Computer,OSLib:Core,OSLib:User
 
 DLKFLAG= -DDLK
-DLKOBJS = $(DLKLIB).o.dlk_load o.linktab
+DLKOBJS = $(DLKLIB).o.dlk_load @.o.linktab
 
 HEADERS = @,@.^.Include,@.^.Modules,@.^.Objects,@.^.Python,$(CLIB),$(OSLIBS),$(DLKLIB)
 
@@ -32,7 +32,7 @@
 CCEXPAT = cc -c -j$(HEADERS),$(EXPAT) $(DLKFLAG) -DHAVE_EXPAT_H -DRISCOS -DHAVE_CONFIG_H -wad -throwback
 
 LINK = link
-LINKFLAGS = -aif -NOUNUSED #-d
+LINKFLAGS = -aif #-NOUNUSED #-d
 LOADLIBS = $(CLIB).o.Stubs $(OSLIB).o.OSLib $(DLKOBJS)
 
 
@@ -41,143 +41,135 @@
 
 
 # code for main Python binary
-MODULES_STATIC = \
-	@.^.Modules.o.python \
-	@.^.Modules.o.main \
-	Modules.o.config \
-	@.^.Modules.o.getbuildinfo \
-	Modules.o.getpath_riscos \
-	Modules.o.riscosmodule \
-	@.^.Modules.o._sre
+MODULES_STATIC =\
+	@.^.Modules.o.python\
+	@.^.Modules.o.main\
+	Modules.o.config\
+	@.^.Modules.o.getbuildinfo\
+	Modules.o.getpath_riscos\
+	Modules.o.riscosmodule
 
 
 # dynamic Modules
-MODULES_DYNAMIC = \
-	@.^.Lib.array/pyd \
-	@.^.Lib.audioop/pyd \
-	@.^.Lib.binascii/pyd \
-	@.^.Lib.cmath/pyd \
-	@.^.Lib.cPickle/pyd \
-	@.^.Lib.cStringIO/pyd \
-	@.^.Lib.errno/pyd \
-	@.^.Lib.imageop/pyd \
-	@.^.Lib.math/pyd \
-	@.^.Lib.md5/pyd \
-	@.^.Lib.new/pyd \
-	@.^.Lib.operator/pyd \
-	@.^.Lib.parser/pyd \
-	@.^.Lib.pcre/pyd \
-	@.^.Lib.regex/pyd \
-	@.^.Lib.rgbimg/pyd \
-	@.^.Lib.rotor/pyd \
-	@.^.Lib.sha/pyd \
-	@.^.Lib.signal/pyd \
-	@.^.Lib.struct/pyd \
-	@.^.Lib.time/pyd \
-	@.^.Lib._locale/pyd \
-	@.^.Lib.zlib/pyd \
-	@.^.Lib.select/pyd \
-	@.^.Lib._socket/pyd \
-	@.^.Lib._codecs/pyd \
-	@.^.Lib._weakref/pyd \
-	@.^.Lib._testcapi/pyd \
-	@.^.Lib.unicodedata/pyd \
-	@.^.Lib.xreadlines/pyd \
-	@.^.Lib.pyexpat/pyd \
-	@.^.Lib.plat-riscos.drawf/pyd \
-	@.^.Lib.plat-riscos.swi/pyd
-
-	# @.^.Lib.soundex/pyd \
-	# leave  strop out? It's no longer in use for string operations
-	# @.^.Lib.mmap/pyd would it make sense? I read about a mmap
-	# implementation for RISC OS recently in usenet news.
-
-	#@.^.Lib.strop/pyd \
-	#@.^.Lib._sre/pyd \
+MODULES_DYNAMIC =\
+	@.^.Lib.array/pyd\
+	@.^.Lib.audioop/pyd\
+	@.^.Lib.binascii/pyd\
+	@.^.Lib.cmath/pyd\
+	@.^.Lib.cPickle/pyd\
+	@.^.Lib.cStringIO/pyd\
+	@.^.Lib.errno/pyd\
+	@.^.Lib.imageop/pyd\
+	@.^.Lib.math/pyd\
+	@.^.Lib.md5/pyd\
+	@.^.Lib.new/pyd\
+	@.^.Lib.operator/pyd\
+	@.^.Lib.parser/pyd\
+	@.^.Lib.pcre/pyd\
+	@.^.Lib.regex/pyd\
+	@.^.Lib.rgbimg/pyd\
+	@.^.Lib.rotor/pyd\
+	@.^.Lib.sha/pyd\
+	@.^.Lib.signal/pyd\
+	@.^.Lib.struct/pyd\
+	@.^.Lib.time/pyd\
+	@.^.Lib._locale/pyd\
+	@.^.Lib.zlib/pyd\
+	@.^.Lib.select/pyd\
+	@.^.Lib._socket/pyd\
+	@.^.Lib._codecs/pyd\
+	@.^.Lib._weakref/pyd\
+	@.^.Lib._testcapi/pyd\
+	@.^.Lib.unicodedata/pyd\
+	@.^.Lib.xreadlines/pyd\
+	@.^.Lib.pyexpat/pyd\
+	@.^.Lib.plat-riscos.drawf/pyd\
+	@.^.Lib.plat-riscos.swi/pyd\
+	@.^.Lib._sre/pyd
 
 
-OBJECTS_PYTHON = \
-	@.^.Python.o.traceback \
-	@.^.Python.o.sysmodule \
-	@.^.Python.o.structmember \
-	@.^.Python.o.strdup \
-	@.^.Python.o.sigcheck \
-	@.^.Python.o.pythonrun \
-	@.^.Python.o.pystate \
-	@.^.Python.o.pyfpe \
-	@.^.Python.o.mystrtoul \
-	@.^.Python.o.modsupport \
-	@.^.Python.o.marshal \
-	@.^.Python.o.importdl \
-	@.^.Python.o.import \
-	@.^.Python.o.graminit \
-	@.^.Python.o.getversion \
-	@.^.Python.o.getplatform \
-	@.^.Python.o.getopt \
-	@.^.Python.o.getcopyright \
-	@.^.Python.o.getcompiler \
-	@.^.Python.o.getargs \
-	@.^.Python.o.frozenmain \
-	@.^.Python.o.frozen \
-	@.^.Python.o.errors \
-	@.^.Python.o.compile \
-	@.^.Python.o.ceval \
-	@.^.Python.o.bltinmodule \
-	@.^.Python.o.exceptions \
-	@.^.Python.o.hypot \
-	@.^.Python.o.codecs \
-	@.^.Python.o.symtable
-# @.^.Python.o.atof @.^.Python.o.strerror
+OBJECTS_PYTHON =\
+	@.^.Python.o.traceback\
+	@.^.Python.o.sysmodule\
+	@.^.Python.o.structmember\
+	@.^.Python.o.strdup\
+	@.^.Python.o.sigcheck\
+	@.^.Python.o.pythonrun\
+	@.^.Python.o.pystate\
+	@.^.Python.o.pyfpe\
+	@.^.Python.o.mystrtoul\
+	@.^.Python.o.modsupport\
+	@.^.Python.o.marshal\
+	@.^.Python.o.importdl\
+	@.^.Python.o.import\
+	@.^.Python.o.graminit\
+	@.^.Python.o.getversion\
+	@.^.Python.o.getplatform\
+	@.^.Python.o.getopt\
+	@.^.Python.o.getcopyright\
+	@.^.Python.o.getcompiler\
+	@.^.Python.o.getargs\
+	@.^.Python.o.frozenmain\
+	@.^.Python.o.frozen\
+	@.^.Python.o.errors\
+	@.^.Python.o.compile\
+	@.^.Python.o.ceval\
+	@.^.Python.o.bltinmodule\
+	@.^.Python.o.exceptions\
+	@.^.Python.o.hypot\
+	@.^.Python.o.codecs\
+	@.^.Python.o.symtable\
+	@.^.Python.o.future
 
 
-OBJECTS_RISCOS =  \
-	@.Python.o.dynload_riscos \
-	@.Python.o.getcwd_riscos \
-	@.Python.o.getmtime_riscos \
+OBJECTS_RISCOS = \
+	@.Python.o.dynload_riscos\
+	@.Python.o.getcwd_riscos\
+	@.Python.o.getmtime_riscos\
 	@.o.unixstuff
 
 
-OBJECTS_OBJECTS = \
-	@.^.Objects.o.typeobject \
-	@.^.Objects.o.tupleobject \
-	@.^.Objects.o.stringobject \
-	@.^.Objects.o.sliceobject \
-	@.^.Objects.o.rangeobject \
-	@.^.Objects.o.object \
-	@.^.Objects.o.moduleobject \
-	@.^.Objects.o.methodobject \
-	@.^.Objects.o.longobject \
-	@.^.Objects.o.listobject \
-	@.^.Objects.o.intobject \
-	@.^.Objects.o.funcobject \
-	@.^.Objects.o.frameobject \
-	@.^.Objects.o.floatobject \
-	@.^.Objects.o.fileobject \
-	@.^.Objects.o.dictobject \
-	@.^.Objects.o.complexobject \
-	@.^.Objects.o.cobject \
-	@.^.Objects.o.classobject \
-	@.^.Objects.o.cellobject \
-	@.^.Objects.o.bufferobject \
-	@.^.Objects.o.abstract \
-	@.^.Objects.o.unicodectype \
+OBJECTS_OBJECTS =\
+	@.^.Objects.o.typeobject\
+	@.^.Objects.o.tupleobject\
+	@.^.Objects.o.stringobject\
+	@.^.Objects.o.sliceobject\
+	@.^.Objects.o.rangeobject\
+	@.^.Objects.o.object\
+	@.^.Objects.o.moduleobject\
+	@.^.Objects.o.methodobject\
+	@.^.Objects.o.longobject\
+	@.^.Objects.o.listobject\
+	@.^.Objects.o.intobject\
+	@.^.Objects.o.funcobject\
+	@.^.Objects.o.frameobject\
+	@.^.Objects.o.floatobject\
+	@.^.Objects.o.fileobject\
+	@.^.Objects.o.dictobject\
+	@.^.Objects.o.complexobject\
+	@.^.Objects.o.cobject\
+	@.^.Objects.o.classobject\
+	@.^.Objects.o.cellobject\
+	@.^.Objects.o.bufferobject\
+	@.^.Objects.o.abstract\
+	@.^.Objects.o.unicodectype\
 	@.^.Objects.o.unicodeobject
 
 
-OBJECTS_PARSER = \
-	@.^.Parser.o.tokenizer \
-	@.^.Parser.o.printgrammar \
-	@.^.Parser.o.parsetok \
-	@.^.Parser.o.parser \
-	@.^.Parser.o.node \
-	@.^.Parser.o.myreadline \
-	@.^.Parser.o.metagrammar \
-	@.^.Parser.o.listnode \
-	@.^.Parser.o.intrcheck \
-	@.^.Parser.o.grammar1 \
-	@.^.Parser.o.grammar \
-	@.^.Parser.o.firstsets \
-	@.^.Parser.o.bitset \
+OBJECTS_PARSER =\
+	@.^.Parser.o.tokenizer\
+	@.^.Parser.o.printgrammar\
+	@.^.Parser.o.parsetok\
+	@.^.Parser.o.parser\
+	@.^.Parser.o.node\
+	@.^.Parser.o.myreadline\
+	@.^.Parser.o.metagrammar\
+	@.^.Parser.o.listnode\
+	@.^.Parser.o.intrcheck\
+	@.^.Parser.o.grammar1\
+	@.^.Parser.o.grammar\
+	@.^.Parser.o.firstsets\
+	@.^.Parser.o.bitset\
 	@.^.Parser.o.acceler
 
 SUPPORT_FILES = @.^.!Boot @.^.!Run @.^.!Sprites @.^.!Sprites22 @.^.AddToPath
@@ -196,7 +188,8 @@
 
 
 #########################################################################
-# Support files
+# RISC OS support files
+#
 @.^.!Boot: support.!Boot
 	copy support.!Boot @.^.!Boot ~C~VF
 	settype @.^.!Boot feb
@@ -297,9 +290,6 @@
 	$(LINK) -aof -o Modules.o.swilink Modules.o.swimodule $(OSLIB).o.OSLIB
 	$(MAKEDLK) -d @.^.Lib.plat-riscos.swi/pyd -s s.linktab -o Modules.o.swilink -e initswi
 
-@.^.Lib.time/pyd: @.^.Modules.o.timemodule s.linktab
-	$(MAKEDLK) -d @.^.Lib.time/pyd -s s.linktab -o @.^.Modules.o.timemodule -e inittime
-
 @.^.Lib._locale/pyd: @.^.Modules.o._localemodule s.linktab
 	$(MAKEDLK) -d @.^.Lib._locale/pyd -s s.linktab -o @.^.Modules.o._localemodule -e init_locale
 
@@ -322,10 +312,6 @@
 	$(MAKEDLK) -d @.^.Lib.xreadlines/pyd -s s.linktab -o @.^.Modules.o.xreadlinesmodule -e initxreadlines
 
 
-##@.^.Lib.mmap/pyd: @.^.Modules.o.mmapmodule s.linktab
-##	$(MAKEDLK) -d @.^.Lib.mmap/pyd -s s.linktab -o @.^.Modules.o.mmapmodule -e initmmap
-
-
 
 ############################################################################
 # Dynamic Modules with other dependencies
@@ -353,6 +339,11 @@
 	$(CC) -I$(ZLIB) -o $@ @.^.Modules.c.zlibmodule
 
 
+@.^.Lib.time/pyd: @.^.Modules.o.timemodule s.linktab @.o.sleep
+	$(LINK) -aof -o @.^.Modules.o.timelink @.^.Modules.o.timemodule @.o.sleep $(OSLIB).o.OSLib
+	$(MAKEDLK) -d @.^.Lib.time/pyd -s s.linktab -o @.^.Modules.o.timelink -e inittime
+
+
 @.^.Lib.pyexpat/pyd: @.^.Modules.o.pyexpat s.linktab
 	$(LINK) -aof -o @.^.Modules.o.pyexpatlink @.^.Modules.o.pyexpat $(EXPAT).expat_lib
 	$(MAKEDLK) -d @.^.Lib.pyexpat/pyd -s s.linktab -o @.^.Modules.o.pyexpatlink -e initpyexpat
@@ -362,35 +353,42 @@
 
 
 ##########################################################################
-
+# dynamic linking symbol table
+#
 o.linktab: s.linktab
 	ObjAsm s.linktab o.linktab
 
 s.linktab: $(OBJECTS)
 	$(OBJSCAN) -s s.linktab -o $(OBJECTS) $(clib).o.stubs
 
-
-clean:
-	create @.^.Objects.o.dummy
-	create @.^.Parser.o.dummy
-	create @.^.Modules.o.dummy
-	create o.dummy
-	create @.^.Python.o.dummy
+##########################################################################
+# special targets
+#
+libclean:
 	create @.^.Lib.dummy/pyc
 	create @.^.Lib.dummy/pyo
 	create @.^.Lib.plat-riscos.dummy/pyc
 	create @.^.Lib.plat-riscos.dummy/pyo
 	create @.^.Lib.test.dummy/pyc
-	wipe @.^.Modules.o.* ~C ~V
-	wipe @.^.Objects.o.* ~C ~V
-	wipe @.^.Parser.o.* ~C ~V
-	wipe @.^.Python.o.* ~C ~V
-	wipe o.* ~C ~V
+	create @.^.Lib.test.dummy/pyo
 	wipe @.^.Lib.*/pyc ~C~V
 	wipe @.^.Lib.*/pyo ~C~V
 	wipe @.^.Lib.plat-riscos.*/pyc ~C~V
 	wipe @.^.Lib.plat-riscos.*/pyo ~C~V
 	wipe @.^.Lib.test.*/pyc ~C~V
+	wipe @.^.Lib.test.*/pyo ~C~V
+
+clean: libclean
+	create @.^.Objects.o.dummy
+	create @.^.Parser.o.dummy
+	create @.^.Modules.o.dummy
+	create o.dummy
+	create @.^.Python.o.dummy
+	wipe @.^.Modules.o.* ~C ~V
+	wipe @.^.Objects.o.* ~C ~V
+	wipe @.^.Parser.o.* ~C ~V
+	wipe @.^.Python.o.* ~C ~V
+	wipe o.* ~C ~V
 
 rebuild: clean
 	create @.^.Lib.dummy/pyd
diff --git a/RISCOS/sleep.c b/RISCOS/sleep.c
new file mode 100644
index 0000000..0aeba55
--- /dev/null
+++ b/RISCOS/sleep.c
@@ -0,0 +1,41 @@
+#include "osmodule.h"
+#include <stdio.h>
+#include "kernel.h"
+#include <limits.h>
+#include <errno.h>
+#include "taskwindow.h"
+#include "Python.h"
+
+
+int sleep(double delay)
+{
+	os_t starttime, endtime, time; /* monotonic times (centiseconds) */
+	int *pollword, ret;
+	bool claimed;
+
+        /* calculate end time */
+	starttime = os_read_monotonic_time();
+	if (starttime + 100.0*delay >INT_MAX)
+		endtime = INT_MAX;
+	else
+		endtime = (os_t)(starttime + 100.0*delay);
+
+	/* allocate (in RMA) and set pollword for xupcall_sleep */
+	pollword = osmodule_alloc(4);
+	*pollword = 1;
+
+	time = starttime;
+	ret = 0;
+	while ( time<endtime && time>=starttime ) {
+		xupcall_sleep (pollword, &claimed);
+		if (PyErr_CheckSignals()) {
+			ret = 1;
+			break;
+		}
+		time = os_read_monotonic_time();
+	}
+
+	/* deallocate pollword */
+	osmodule_free(pollword);
+	return ret;
+}
diff --git "a/RISCOS/support/\041Boot" "b/RISCOS/support/\041Boot"
new file mode 100644
index 0000000..668d6b1
--- /dev/null
+++ "b/RISCOS/support/\041Boot"
@@ -0,0 +1,12 @@
+set Python$Dir <Obey$Dir>
+set PythonApp$Path <Obey$Dir>.
+
+IconSprites <Obey$Dir>.!Sprites
+
+<Obey$Dir>.AddToPath Python$Path PythonApp:Lib
+<Obey$Dir>.AddToPath Python$Path PythonApp:Lib.plat-riscos
+<Obey$Dir>.AddToPath Python$Path PythonApp:Lib.site-packages
+set Alias$@RunType_ae5 TaskWindow |"python %%*0|" -name |"Python|" -quit
+| -display
+set File$Type_ae5 Python
+set Alias$Python Run <Python$Dir>.python21 %*0
\ No newline at end of file
diff --git "a/RISCOS/support/\041Run" "b/RISCOS/support/\041Run"
new file mode 100644
index 0000000..6919579
--- /dev/null
+++ "b/RISCOS/support/\041Run"
@@ -0,0 +1,2 @@
+<Obey$Dir>.!Boot
+TaskWindow "python" -name "Python" -quit -display
\ No newline at end of file
diff --git "a/RISCOS/support/\041Sprites" "b/RISCOS/support/\041Sprites"
new file mode 100644
index 0000000..cdf4a65
--- /dev/null
+++ "b/RISCOS/support/\041Sprites"
Binary files differ
diff --git "a/RISCOS/support/\041Sprites22" "b/RISCOS/support/\041Sprites22"
new file mode 100644
index 0000000..5ca88f5
--- /dev/null
+++ "b/RISCOS/support/\041Sprites22"
Binary files differ
diff --git a/RISCOS/support/AddToPath b/RISCOS/support/AddToPath
new file mode 100644
index 0000000..a16e3ad
--- /dev/null
+++ b/RISCOS/support/AddToPath
Binary files differ