[Python-checkins] CVS: python/dist/src/Python codecs.c,2.3,2.4

Guido van Rossum python-dev@python.org
Fri, 31 Mar 2000 12:25:30 -0500 (EST)


Update of /projects/cvsroot/python/dist/src/Python
In directory eric:/home/guido/hp/mal/py-patched/Python

Modified Files:
	codecs.c 
Log Message:
Marc-Andre Lemburg: Error reporting in the codec registry and lookup
mechanism is enhanced to be more informative.


Index: codecs.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Python/codecs.c,v
retrieving revision 2.3
retrieving revision 2.4
diff -C2 -r2.3 -r2.4
*** codecs.c	2000/03/24 20:52:23	2.3
--- codecs.c	2000/03/31 17:25:23	2.4
***************
*** 28,37 ****
     not downgrade startup time of scripts not needing it.
  
!    Errors are silently ignored by this function. Only one try is made.
  
  */
  
  static
! void import_encodings() 
  {
      PyObject *mod;
--- 28,38 ----
     not downgrade startup time of scripts not needing it.
  
!    ImportErrors are silently ignored by this function. Only one try is
!    made.
  
  */
  
  static
! int import_encodings() 
  {
      PyObject *mod;
***************
*** 40,67 ****
      mod = PyImport_ImportModule("encodings");
      if (mod == NULL) {
! 	PyErr_Clear();
! 	return;
      }
      Py_DECREF(mod);
  }
  
  /* Register a new codec search function.
  
     The search_function's refcount is incremented by this function. */
  
  int PyCodec_Register(PyObject *search_function)
  {
!     if (!import_encodings_called)
! 	import_encodings();
      if (search_function == NULL) {
  	PyErr_BadArgument();
! 	return -1;
      }
      if (!PyCallable_Check(search_function)) {
  	PyErr_SetString(PyExc_TypeError,
  			"argument must be callable");
! 	return -1;
      }
      return PyList_Append(_PyCodec_SearchPath, search_function);
  }
  
--- 41,86 ----
      mod = PyImport_ImportModule("encodings");
      if (mod == NULL) {
! 	if (PyErr_ExceptionMatches(PyExc_ImportError)) {
! 	    /* Ignore ImportErrors... this is done so that
! 	       distributions can disable the encodings package. Note
! 	       that other errors are not masked, e.g. SystemErrors
! 	       raised to inform the user of an error in the Python
! 	       configuration are still reported back to the user. */
! 	    PyErr_Clear();
! 	    return 0;
! 	}
! 	return -1;
      }
      Py_DECREF(mod);
+     return 0;
  }
  
  /* Register a new codec search function.
  
+    As side effect, this tries to load the encodings package, if not
+    yet done, to make sure that it is always first in the list of
+    search functions.
+ 
     The search_function's refcount is incremented by this function. */
  
  int PyCodec_Register(PyObject *search_function)
  {
!     if (!import_encodings_called) {
! 	if (import_encodings())
! 	    goto onError;
!     }
      if (search_function == NULL) {
  	PyErr_BadArgument();
! 	goto onError;
      }
      if (!PyCallable_Check(search_function)) {
  	PyErr_SetString(PyExc_TypeError,
  			"argument must be callable");
! 	goto onError;
      }
      return PyList_Append(_PyCodec_SearchPath, search_function);
+ 
+  onError:
+     return -1;
  }
  
***************
*** 89,94 ****
     characters. This makes encodings looked up through this mechanism
     effectively case-insensitive.
  
!    If no codec is found, a KeyError is set and NULL returned.  */
  
  PyObject *_PyCodec_Lookup(const char *encoding)
--- 108,119 ----
     characters. This makes encodings looked up through this mechanism
     effectively case-insensitive.
+ 
+    If no codec is found, a KeyError is set and NULL returned. 
  
!    As side effect, this tries to load the encodings package, if not
!    yet done. This is part of the lazy load strategy for the encodings
!    package.
! 
! */
  
  PyObject *_PyCodec_Lookup(const char *encoding)
***************
*** 97,107 ****
      int i, len;
  
!     if (_PyCodec_SearchCache == NULL || _PyCodec_SearchPath == NULL) {
  	PyErr_SetString(PyExc_SystemError,
  			"codec module not properly initialized");
  	goto onError;
      }
!     if (!import_encodings_called)
! 	import_encodings();
  
      /* Convert the encoding to a lower-cased Python string */
--- 122,135 ----
      int i, len;
  
!     if (_PyCodec_SearchCache == NULL || 
! 	_PyCodec_SearchPath == NULL) {
  	PyErr_SetString(PyExc_SystemError,
  			"codec module not properly initialized");
  	goto onError;
      }
!     if (!import_encodings_called) {
! 	if (import_encodings())
! 	    goto onError;
!     }
  
      /* Convert the encoding to a lower-cased Python string */
***************
*** 128,131 ****
--- 156,165 ----
      if (len < 0)
  	goto onError;
+     if (len == 0) {
+ 	PyErr_SetString(PyExc_LookupError,
+ 			"no codec search functions registered: "
+ 			"can't find encoding");
+ 	goto onError;
+     }
  
      for (i = 0; i < len; i++) {