Fix test failure in test_cmd_line by initializing the hash secret at the earliest point.
diff --git a/Include/pygetopt.h b/Include/pygetopt.h
index 4de8c00..425c7dd 100644
--- a/Include/pygetopt.h
+++ b/Include/pygetopt.h
@@ -9,6 +9,8 @@
 PyAPI_DATA(int) _PyOS_opterr;
 PyAPI_DATA(int) _PyOS_optind;
 PyAPI_DATA(wchar_t *) _PyOS_optarg;
+
+PyAPI_FUNC(void) _PyOS_ResetGetOpt(void);
 #endif
 
 PyAPI_FUNC(int) _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring);
diff --git a/Include/pythonrun.h b/Include/pythonrun.h
index eeba1b7..74ab986 100644
--- a/Include/pythonrun.h
+++ b/Include/pythonrun.h
@@ -193,6 +193,7 @@
 PyAPI_FUNC(int) _PyFrame_Init(void);
 PyAPI_FUNC(void) _PyFloat_Init(void);
 PyAPI_FUNC(int) PyByteArray_Init(void);
+PyAPI_FUNC(void) _PyRandom_Init(void);
 #endif
 
 /* Various internal finalizers */
diff --git a/Modules/main.c b/Modules/main.c
index a820a9e..d026033 100644
--- a/Modules/main.c
+++ b/Modules/main.c
@@ -339,7 +339,33 @@
     orig_argc = argc;           /* For Py_GetArgcArgv() */
     orig_argv = argv;
 
+    /* Hash randomization needed early for all string operations
+       (including -W and -X options). */
+    while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
+        if (c == 'm' || c == 'c') {
+            /* -c / -m is the last option: following arguments are
+               not interpreter options. */
+            break;
+        }
+        switch (c) {
+        case 'E':
+            Py_IgnoreEnvironmentFlag++;
+            break;
+        case 'R':
+            Py_HashRandomizationFlag++;
+            break;
+        }
+    }
+    /* The variable is only tested for existence here; _PyRandom_Init will
+       check its value further. */
+    if (!Py_HashRandomizationFlag &&
+        (p = Py_GETENV("PYTHONHASHSEED")) && *p != '\0')
+        Py_HashRandomizationFlag = 1;
+
+    _PyRandom_Init();
+
     PySys_ResetWarnOptions();
+    _PyOS_ResetGetOpt();
 
     while ((c = _PyOS_GetOpt(argc, argv, PROGRAM_OPTS)) != EOF) {
         if (c == 'c') {
@@ -400,7 +426,7 @@
             break;
 
         case 'E':
-            Py_IgnoreEnvironmentFlag++;
+            /* Already handled above */
             break;
 
         case 't':
@@ -442,7 +468,7 @@
             break;
 
         case 'R':
-            Py_HashRandomizationFlag++;
+            /* Already handled above */
             break;
 
         /* This space reserved for other options */
diff --git a/Python/getopt.c b/Python/getopt.c
index 064a187..cc42431 100644
--- a/Python/getopt.c
+++ b/Python/getopt.c
@@ -41,9 +41,18 @@
 int _PyOS_optind = 1;          /* index into argv array   */
 wchar_t *_PyOS_optarg = NULL;     /* optional argument       */
 
+static wchar_t *opt_ptr = L"";
+
+void _PyOS_ResetGetOpt(void)
+{
+    _PyOS_opterr = 1;
+    _PyOS_optind = 1;
+    _PyOS_optarg = NULL;
+    opt_ptr = L"";
+}
+
 int _PyOS_GetOpt(int argc, wchar_t **argv, wchar_t *optstring)
 {
-    static wchar_t *opt_ptr = L"";
     wchar_t *ptr;
     wchar_t option;
 
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 54d39a5..a642c0b 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -73,7 +73,6 @@
 extern void PyLong_Fini(void);
 extern int _PyFaulthandler_Init(void);
 extern void _PyFaulthandler_Fini(void);
-extern void _PyRandom_Init(void);
 
 #ifdef WITH_THREAD
 extern void _PyGILState_Init(PyInterpreterState *, PyThreadState *);