[Python-checkins] CVS: python/dist/src/Modules posixmodule.c,2.120,2.121

Fred L. Drake fdrake@weyr.cnri.reston.va.us
Wed, 15 Dec 1999 13:31:13 -0500 (EST)


Update of /projects/cvsroot/python/dist/src/Modules
In directory weyr:/home/fdrake/projects/python/Modules

Modified Files:
	posixmodule.c 
Log Message:

Rip out the code to check the ordering of the tables used to map
strings to integers for the *conf*() functions.

Added code to sort the tables at module initialization.  Three
dictionaries, confstr_names, sysconf_names, and pathconf_names, are
added to the module as well.  These map known configuration setting
names to the numeric value which is used to represent the setting in
the system call.  This code is always called.

Updated related comments.


Index: posixmodule.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Modules/posixmodule.c,v
retrieving revision 2.120
retrieving revision 2.121
diff -C2 -r2.120 -r2.121
*** posixmodule.c	1999/12/15 15:34:33	2.120
--- posixmodule.c	1999/12/15 18:31:10	2.121
***************
*** 3370,3373 ****
--- 3370,3377 ----
   * rarely-used constants.  There are three separate tables that use
   * these definitions.
+  *
+  * This code is always included, even if none of the interfaces that
+  * need it are included.  The #if hackery needed to avoid it would be
+  * sufficiently pervasive that it's not worth the loss of readability.
   */
  struct constdef {
***************
*** 4277,4331 ****
  
  
! #ifdef CHECK_CONFNAME_TABLES
! /* This code should not be enabled by default; it's only purpose is to
!  * test the order of constants in the configuration name tables.  The
!  * second function defined here, check_confname_tables(), is called
!  * during module initialization if CHECK_CONFNAME_TABLES is defined.
!  * This only needs to be done when changes are made to the tables.
   */
! static void
! check_confname_table(table, tablesize, tablename)
       struct constdef *table;
       size_t tablesize;
!      char *tablename;
  {
!     if (tablesize > 1) {
          int i = 0;
  
!         for (; i < (tablesize - 1); ++i) {
!             if (strcmp(table[i].name, table[i + 1].name) >= 0) {
!                 char buffer[256];
! 
!                 snprintf(buffer, sizeof(buffer),
!                          "confname table '%s' out of order!\n", tablename);
!                 Py_FatalError(buffer);
              }
          }
      }
  }
  
! static void
! check_confname_tables()
  {
  #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
!         check_confname_table(posix_constants_pathconf,
                               sizeof(posix_constants_pathconf)
                                 / sizeof(struct constdef),
!                              "pathconf");
  #endif
  #ifdef HAVE_CONFSTR
!         check_confname_table(posix_constants_confstr,
                               sizeof(posix_constants_confstr)
                                 / sizeof(struct constdef),
!                              "confstr");
  #endif
  #ifdef HAVE_SYSCONF
!         check_confname_table(posix_constants_sysconf,
                               sizeof(posix_constants_sysconf)
                                 / sizeof(struct constdef),
!                              "sysconf");
  #endif
  }
- #endif
  
  
--- 4281,4366 ----
  
  
! /* This code is used to ensure that the tables of configuration value names
!  * are in sorted order as required by conv_confname(), and also to build the
!  * the exported dictionaries that are used to publish information about the
!  * names available on the host platform.
!  *
!  * Sorting the table at runtime ensures that the table is properly ordered
!  * when used, even for platforms we're not able to test on.  It also makes
!  * it easier to add additional entries to the tables.
   */
! 
! static int
! cmp_constdefs(v1, v2)
!      const void *v1;
!      const void *v2;
! {
!     const struct constdef *c1 =
!         (const struct constdef *) v1;
!     const struct constdef *c2 =
!         (const struct constdef *) v2;
! 
!     return strcmp(c1->name, c2->name);
! }
! 
! static int
! setup_confname_table(table, tablesize, tablename, moddict)
       struct constdef *table;
       size_t tablesize;
!      char * tablename;
!      PyObject *moddict;
  {
!     PyObject *d = NULL;
! 
!     qsort(table, tablesize, sizeof(struct constdef), cmp_constdefs);
!     d = PyDict_New();
!     if (d != NULL) {
!         PyObject *o;
          int i = 0;
  
!         for (; i < tablesize; ++i) {
!             o = PyInt_FromLong(table[i].value);
!             if (o == NULL
!                 || PyDict_SetItemString(d, table[i].name, o) == -1) {
!                 Py_DECREF(d);
!                 d = NULL;
!                 return -1;
              }
          }
+         if (PyDict_SetItemString(moddict, tablename, d) == -1)
+             return -1;
+         return 0;
      }
+     return -1;
  }
  
! /* Return -1 on failure, 0 on success. */
! static int
! setup_confname_tables(moddict)
!      PyObject *moddict;
  {
  #if defined(HAVE_FPATHCONF) || defined(HAVE_PATHCONF)
!     if (setup_confname_table(posix_constants_pathconf,
                               sizeof(posix_constants_pathconf)
                                 / sizeof(struct constdef),
!                              "pathconf_names", moddict))
!         return -1;
  #endif
  #ifdef HAVE_CONFSTR
!     if (setup_confname_table(posix_constants_confstr,
                               sizeof(posix_constants_confstr)
                                 / sizeof(struct constdef),
!                              "confstr_names", moddict))
!         return -1;
  #endif
  #ifdef HAVE_SYSCONF
!     if (setup_confname_table(posix_constants_sysconf,
                               sizeof(posix_constants_sysconf)
                                 / sizeof(struct constdef),
!                              "sysconf_names", moddict))
!         return -1;
  #endif
+     return 0;
  }
  
  
***************
*** 4347,4352 ****
      return NULL;
  }
-                 
  
  static PyMethodDef posix_methods[] = {
  	{"access",	posix_access, METH_VARARGS, posix_access__doc__},
--- 4382,4387 ----
      return NULL;
  }
  
+ 
  static PyMethodDef posix_methods[] = {
  	{"access",	posix_access, METH_VARARGS, posix_access__doc__},
***************
*** 4744,4753 ****
                  return;
  
  	PyDict_SetItemString(d, "error", PyExc_OSError);
  
  	posix_putenv_garbage = PyDict_New();
- 
- #ifdef CHECK_CONFNAME_TABLES
-         check_confname_tables();
- #endif
  }
--- 4779,4787 ----
                  return;
  
+         if (setup_confname_tables(d))
+                 return;
+ 
  	PyDict_SetItemString(d, "error", PyExc_OSError);
  
  	posix_putenv_garbage = PyDict_New();
  }