[Python-checkins] python/dist/src/Lib/test test_compile.py, 1.10.10.1, 1.10.10.2

jhylton at users.sourceforge.net jhylton at users.sourceforge.net
Fri Apr 23 00:14:10 EDT 2004


Update of /cvsroot/python/python/dist/src/Lib/test
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv11430/test

Modified Files:
      Tag: ast-branch
	test_compile.py 
Log Message:
Change most of test_compile to a doctest.


Index: test_compile.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/test/test_compile.py,v
retrieving revision 1.10.10.1
retrieving revision 1.10.10.2
diff -C2 -d -r1.10.10.1 -r1.10.10.2
*** test_compile.py	28 Apr 2003 17:28:59 -0000	1.10.10.1
--- test_compile.py	23 Apr 2004 04:14:07 -0000	1.10.10.2
***************
*** 1,80 ****
! from test.test_support import verbose, TestFailed
  
! if verbose:
!     print "Testing whether compiler catches assignment to __debug__"
  
! try:
!     compile('__debug__ = 1', '?', 'single')
! except SyntaxError:
!     pass
  
! import __builtin__
! prev = __builtin__.__debug__
! setattr(__builtin__, '__debug__', 'sure')
! setattr(__builtin__, '__debug__', prev)
  
! if verbose:
!     print 'Running tests on argument handling'
  
! try:
!     exec 'def f(a, a): pass'
!     raise TestFailed, "duplicate arguments"
! except SyntaxError:
!     pass
  
! if verbose:
!     print "compiling string with syntax error"
  
! try:
!     compile("1+*3", "filename", "exec")
! except SyntaxError, detail:
!     if not detail.filename == "filename":
!         raise TestFailed, "expected 'filename', got %r" % detail.filename
  
! try:
!     exec 'def f(a = 0, a = 1): pass'
!     raise TestFailed, "duplicate keyword arguments"
! except SyntaxError:
!     pass
  
! try:
!     exec 'def f(a): global a; a = 1'
!     raise TestFailed, "variable is global and local"
! except SyntaxError:
!     pass
  
! if verbose:
!     print "testing complex args"
  
! def comp_args((a, b)):
!     print a,b
  
! comp_args((1, 2))
  
! def comp_args((a, b)=(3, 4)):
!     print a, b
  
! comp_args((1, 2))
! comp_args()
  
! def comp_args(a, (b, c)):
!     print a, b, c
  
! comp_args(1, (2, 3))
  
! def comp_args(a=2, (b, c)=(3, 4)):
!     print a, b, c
  
! comp_args(1, (2, 3))
! comp_args()
  
! try:
!     exec 'def f(a=1, (b, c)): pass'
!     raise TestFailed, "non-default args after default"
! except SyntaxError:
!     pass
  
! if verbose:
!     print "testing bad float literals"
  
  def expect_error(s):
--- 1,113 ----
! """Test a variety of compilation edge cases.
  
! Several ways to assign to __debug__
! -----------------------------------
  
! >>> __debug__ = 1
! Traceback (most recent call last):
!  ...
! SyntaxError: can not assign to __debug__ (<string>, line 1)
  
! >>> import __debug__
! Traceback (most recent call last):
!  ...
! SyntaxError: can not assign to __debug__ (<string>, line 1)
  
! >>> try:
! ...     1
! ... except MemoryError, __debug__:
! ...     pass
! Traceback (most recent call last):
!  ...
! SyntaxError: can not assign to __debug__ (<string>, line 2)
  
! Shouldn't that be line 3?
  
! >>> def __debug__():
! ...     pass
! Traceback (most recent call last):
!  ...
! SyntaxError: can not assign to __debug__ (<string>, line 1)
  
! Not sure what the next few lines is trying to test.
  
! >>> import __builtin__
! >>> prev = __builtin__.__debug__
! >>> setattr(__builtin__, "__debug__", "sure")
! >>> setattr(__builtin__, "__debug__", prev)
  
! Parameter passing
! -----------------
  
! >>> def f(a = 0, a = 1):
! ...     pass
! Traceback (most recent call last):
!  ...
! SyntaxError: duplicate argument 'a' in function definition (<string>, line 1)
  
! Details of SyntaxError object
! -----------------------------
  
! >>> 1+*3
! Traceback (most recent call last):
!  ...
! SyntaxError: invalid syntax
  
! In this case, let's explore the details fields of the exception object.
! >>> try:
! ...     compile("1+*3", "filename", "exec")
! ... except SyntaxError, err:
! ...     pass
! >>> err.filename, err.lineno, err.offset
! ('filename', 1, 3)
! >>> err.text, err.msg
! ('1+*3', 'invalid syntax')
  
! Complex parameter passing
! -------------------------
  
! >>> def comp_params((a, b)):
! ...     print a, b
! >>> comp_params((1, 2))
! 1 2
  
! >>> def comp_params((a, b)=(3, 4)):
! ...     print a, b
! >>> comp_params((1, 2))
! 1 2
! >>> comp_params()
! 3 4
  
! >>> def comp_params(a, (b, c)):
! ...     print a, b, c
! >>> comp_params(1, (2, 3))
! 1 2 3
  
! >>> def comp_params(a=2, (b, c)=(3, 4)):
! ...     print a, b, c
! >>> comp_params(1, (2, 3))
! 1 2 3
! >>> comp_params()
! 2 3 4
  
! """
  
! from test.test_support import verbose, TestFailed, run_doctest
! 
! ##try:
! ##    exec 'def f(a): global a; a = 1'
! ##    raise TestFailed, "variable is global and local"
! ##except SyntaxError:
! ##    pass
! 
! ##try:
! ##    exec 'def f(a=1, (b, c)): pass'
! ##    raise TestFailed, "non-default args after default"
! ##except SyntaxError:
! ##    pass
! 
! # It takes less space to deal with bad float literals using a helper
! # function than it does with doctest.  The doctest needs to include
! # the exception every time.
  
  def expect_error(s):
***************
*** 157,158 ****
--- 190,195 ----
  expect_same("-" + all_one_bits, 1)
  """
+ 
+ def test_main(verbose=None):
+     from test import test_compile
+     run_doctest(test_compile, verbose)




More information about the Python-checkins mailing list