[Python-checkins] CVS: python/nondist/sandbox/typecheck/src addchecks.py,1.1,1.2 typecheck.py,1.1,1.2

Paul Prescod prescod@users.sourceforge.net
Tue, 20 Mar 2001 13:02:37 -0800


Update of /cvsroot/python/python/nondist/sandbox/typecheck/src
In directory usw-pr-cvs1:/tmp/cvs-serv2837/src

Modified Files:
	addchecks.py typecheck.py 
Log Message:
Added strict mode. Strict mode is off by default.

This is useful because it means that mistaken  declarations will 
not crash Python. That's important while we work out the "right" 
set of declarations. 


Index: addchecks.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/typecheck/src/addchecks.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** addchecks.py	2001/03/20 03:01:01	1.1
--- addchecks.py	2001/03/20 21:02:35	1.2
***************
*** 33,39 ****
  
  checkparam_code = gencode("""
  try: __paramcheck__(locals(), __types__)
  except InterfaceError, e:
!     raise e
  """)[0]
  
--- 33,41 ----
  
  checkparam_code = gencode("""
+ 
  try: __paramcheck__(locals(), __types__)
  except InterfaceError, e:
!     try: __typewarn__(e)
!     except InterfaceError: raise  # if it kicks it back to me...
  """)[0]
  
***************
*** 43,48 ****
  # as exception objects are.
  import_code = gencode("""
! from typecheck import __paramcheck__, __rccheck__, __declare__
! from typecheck import *""")
  
  
--- 45,59 ----
  # as exception objects are.
  import_code = gencode("""
! try:
!     from typecheck import __paramcheck__, __rccheck__, __declare__, __typewarn__
! 
!     # one day I'll change this to list the builtin types
!     from typecheck import *
! except ImportError, e:
!     print "Error importing typecheck module:", e
!     def __paramcheck__(*args, **args):
!         pass
!     __rccheck__ = __declare__ = __typewarn__ = __paramcheck__
! """) 
  
  

Index: typecheck.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/typecheck/src/typecheck.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -r1.1 -r1.2
*** typecheck.py	2001/03/20 03:01:01	1.1
--- typecheck.py	2001/03/20 21:02:35	1.2
***************
*** 216,219 ****
--- 216,228 ----
          return iface.__check__(val)
  
+ ####### Strictness (exception throwing or warning) ############
+ _g_strict = None
+ 
+ def setstrict(strict):
+     global _g_strict 
+     _g_strict = strict
+ 
+ ######## Magic runtime functions ############
+ 
  def __paramcheck__(localsdict=None, typesdict=None):
      """Magic function to check all params in a localsdict against
***************
*** 237,242 ****
--- 246,266 ----
      return val
  
+ def __typewarn__(exception):
+     # XXX use Python 2.1 warning features when available
+     if _g_strict is None:
+         import os
+         setstrict( os.getenv("PYTHON_STRICT_TYPECHECK"))
+ 
+     if _g_strict:
+         raise exception
+     else:
+         # of course this should use the Python 2.1 warning
+         # framework eventually!
+         print "Type error:", exception
+ 
  def __declare__(*args, **args): 
      "dummy function for declaration recognition purposes"
+ 
+ ############ test code ##################
  
  def _test1(module, integer, func, stream):