Example #1
0
/* bpo-33042: Ensure embedding apps can predefine sys module options */
static int test_pre_initialization_sys_options(void)
{
    /* We allocate a couple of the options dynamically, and then delete
     * them before calling Py_Initialize. This ensures the interpreter isn't
     * relying on the caller to keep the passed in strings alive.
     */
    const wchar_t *static_warnoption = L"once";
    const wchar_t *static_xoption = L"also_not_an_option=2";
    size_t warnoption_len = wcslen(static_warnoption);
    size_t xoption_len = wcslen(static_xoption);
    wchar_t *dynamic_once_warnoption = \
             (wchar_t *) calloc(warnoption_len+1, sizeof(wchar_t));
    wchar_t *dynamic_xoption = \
             (wchar_t *) calloc(xoption_len+1, sizeof(wchar_t));
    wcsncpy(dynamic_once_warnoption, static_warnoption, warnoption_len+1);
    wcsncpy(dynamic_xoption, static_xoption, xoption_len+1);

    _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption\n");
    PySys_AddWarnOption(L"default");
    _Py_EMBED_PREINIT_CHECK("Checking PySys_ResetWarnOptions\n");
    PySys_ResetWarnOptions();
    _Py_EMBED_PREINIT_CHECK("Checking PySys_AddWarnOption linked list\n");
    PySys_AddWarnOption(dynamic_once_warnoption);
    PySys_AddWarnOption(L"module");
    PySys_AddWarnOption(L"default");
    _Py_EMBED_PREINIT_CHECK("Checking PySys_AddXOption\n");
    PySys_AddXOption(L"not_an_option=1");
    PySys_AddXOption(dynamic_xoption);

    /* Delete the dynamic options early */
    free(dynamic_once_warnoption);
    dynamic_once_warnoption = NULL;
    free(dynamic_xoption);
    dynamic_xoption = NULL;

    _Py_EMBED_PREINIT_CHECK("Initializing interpreter\n");
    _testembed_Py_Initialize();
    _Py_EMBED_PREINIT_CHECK("Check sys module contents\n");
    PyRun_SimpleString("import sys; "
                       "print('sys.warnoptions:', sys.warnoptions); "
                       "print('sys._xoptions:', sys._xoptions); "
                       "warnings = sys.modules['warnings']; "
                       "latest_filters = [f[0] for f in warnings.filters[:3]]; "
                       "print('warnings.filters[:3]:', latest_filters)");
    _Py_EMBED_PREINIT_CHECK("Finalizing interpreter\n");
    Py_Finalize();

    return 0;
}
Example #2
0
/**
 * z_python_init:
 *
 * Initialize the low level Python-Zorp interface. Called by the Python
 * policy implementation.
 *
 * Returns: TRUE if initialization was successful
 **/
gboolean
z_python_init(void)
{
  char buf[2048];

  if (getenv("PYTHONPATH") == NULL)
    {
      g_snprintf(buf, sizeof(buf), "PYTHONPATH=%s", ZORP_DATADIR "/pylib");
    }
  else
    {
      g_snprintf(buf, sizeof(buf), "PYTHONPATH=%s:%s", ZORP_DATADIR "/pylib", getenv("PYTHONPATH"));
    }
  putenv(buf);
  PySys_AddWarnOption("ignore:hex/oct constants > sys.maxint will return positive values in Python 2.4 and up:FutureWarning");
  PySys_AddWarnOption("ignore:x<<y losing bits or changing sign will return a long in Python 2.4 and up:FutureWarning");
  PySys_AddWarnOption("ignore:Non-ASCII character:DeprecationWarning");
  Py_Initialize();
  PyEval_InitThreads();
  
  
  initial_thread = PyEval_SaveThread();
  return TRUE;
}
Example #3
0
/*
 * A toc entry of type 'o' holds runtime options
 * toc->name is the arg
 * this is so you can freeze in command line args to Python
 */
int setRuntimeOptions(void)
{
	int unbuffered = 0;
	TOC *ptoc = f_tocbuff;
	while (ptoc < f_tocend) {
		if (ptoc->typcd == 'o') {
			VS(ptoc->name);
			VS("\n");
			switch (ptoc->name[0]) {
			case 'v':
#if defined  WIN32 
				*Py_VerboseFlag = 1;
#else
				Py_VerboseFlag = 1;
#endif
			break;
			case 'u':
				unbuffered = 1;
			break;
#ifdef HAVE_WARNINGS
			case 'W':
				if (ntohl(f_cookie.pyvers) >= 21) {
					PySys_AddWarnOption(&ptoc->name[2]);
				}
			break;
#endif
			case 's':
#if defined  WIN32 
				*Py_NoSiteFlag = 0;
#else
				Py_NoSiteFlag = 0;
#endif
			break;
			case 'O':
#if defined  WIN32 
				*Py_OptimizeFlag = 1;
#else
				Py_OptimizeFlag = 1;
#endif
			break;
			}
		}
		ptoc = incrementTocPtr(ptoc);
	}
	if (unbuffered) {
#ifdef WIN32
		_setmode(fileno(stdin), O_BINARY);
		_setmode(fileno(stdout), O_BINARY);
#else
		fflush(stdout);
		fflush(stderr);
#ifdef HAVE_SETVBUF
		setvbuf(stdin, (char *)NULL, _IONBF, 0);
		setvbuf(stdout, (char *)NULL, _IONBF, 0);
		setvbuf(stderr, (char *)NULL, _IONBF, 0);
#else
		setbuf(stdin, (char *)NULL);
		setbuf(stdout, (char *)NULL);
		setbuf(stderr, (char *)NULL);
#endif
#endif
	}
	return 0;
}