Index: Python/sysmodule.c =================================================================== --- Python/sysmodule.c (revision 62379) +++ Python/sysmodule.c (working copy) @@ -226,6 +226,36 @@ exit status will be one (i.e., failure)." ); +static PyObject * +sys_setpy3kwarn(PyObject *self, PyObject *mode) +{ + int on; + on = PyObject_IsTrue(mode); + if (on == -1) + return NULL; + Py_Py3kWarningFlag = on; + Py_RETURN_NONE; + +} + +PyDoc_STRVAR(setpy3kwarn_doc, +"setpy3kwarn(mode)\n\ +\n\ +Enable or disable Py3k warnings." +); + +static PyObject * +sys_getpy3kwarn(PyObject *self, PyObject *args) +{ + return PyBool_FromLong(Py_Py3kWarningFlag); +} + +PyDoc_STRVAR(getpy3kwarn_doc, +"getpy3kwarn() -> bool\n\ +\n\ +Get whether Py3k warnings are being issued." +); + #ifdef Py_USING_UNICODE static PyObject * @@ -846,6 +876,7 @@ #ifdef Py_TRACE_REFS {"getobjects", _Py_GetObjects, METH_VARARGS}, #endif + {"getpy3kwarn", sys_getpy3kwarn, METH_NOARGS, getpy3kwarn_doc}, #ifdef Py_REF_DEBUG {"gettotalrefcount", (PyCFunction)sys_gettotalrefcount, METH_NOARGS}, #endif @@ -874,6 +905,7 @@ #endif {"setprofile", sys_setprofile, METH_O, setprofile_doc}, {"getprofile", sys_getprofile, METH_NOARGS, getprofile_doc}, + {"setpy3kwarn", sys_setpy3kwarn, METH_O, setpy3kwarn_doc}, {"setrecursionlimit", sys_setrecursionlimit, METH_VARARGS, setrecursionlimit_doc}, #ifdef WITH_TSC @@ -1349,8 +1381,6 @@ PyString_FromString(Py_GetExecPrefix())); SET_SYS_FROM_STRING("maxint", PyInt_FromLong(PyInt_GetMax())); - SET_SYS_FROM_STRING("py3kwarning", - PyBool_FromLong(Py_Py3kWarningFlag)); SET_SYS_FROM_STRING("float_info", PyFloat_GetInfo()); #ifdef Py_USING_UNICODE Index: Doc/library/sys.rst =================================================================== --- Doc/library/sys.rst (revision 62378) +++ Doc/library/sys.rst (working copy) @@ -430,6 +430,14 @@ .. versionadded:: 2.6 +.. function:: getpy3kwarn() + + Return the status of the Py3k warning flag. It's ``True`` when Python is + started with the -3 option or enabled through :func:`setpy3kwarn`. + + .. versionadded:: 2.6 + + .. function:: gettrace() .. index:: @@ -611,15 +619,6 @@ interpreter prepares to read a new interactive command; this can be used to implement a dynamic prompt. - -.. data:: py3kwarning - - Bool containing the status of the Python 3.0 warning flag. It's ``True`` - when Python is started with the -3 option. - - .. versionadded:: 2.6 - - .. data:: dont_write_bytecode If this is true, Python won't try to write ``.pyc`` or ``.pyo`` files on the @@ -699,6 +698,13 @@ limit can lead to a crash. +.. function:: setpy3kwarning(mode) + + Enable or or disable Py3k warnings. + + .. versionadded:: 2.6 + + .. function:: settrace(tracefunc) .. index:: Index: Lib/warnings.py =================================================================== --- Lib/warnings.py (revision 62378) +++ Lib/warnings.py (working copy) @@ -16,7 +16,7 @@ Warnings are omitted unless Python is started with the -3 option. """ - if sys.py3kwarning: + if sys.getpy3kwarn(): if category is None: category = DeprecationWarning warn(message, category, stacklevel+1) Index: Lib/test/regrtest.py =================================================================== --- Lib/test/regrtest.py (revision 62378) +++ Lib/test/regrtest.py (working copy) @@ -1147,9 +1147,6 @@ self.expected.add('test_sunaudiodev') self.expected.add('test_nis') - if not sys.py3kwarning: - self.expected.add('test_py3kwarn') - self.valid = True def isvalid(self): Index: Lib/test/test_py3kwarn.py =================================================================== --- Lib/test/test_py3kwarn.py (revision 62378) +++ Lib/test/test_py3kwarn.py (working copy) @@ -4,10 +4,7 @@ TestSkipped) import warnings -if not sys.py3kwarning: - raise TestSkipped('%s must be run with the -3 flag' % __name__) - class TestPy3KWarnings(unittest.TestCase): def test_type_inequality_comparisons(self): @@ -125,7 +122,13 @@ def test_main(): - run_unittest(TestPy3KWarnings) + old_warn = sys.getpy3kwarn() + if not old_warn: + sys.setpy3kwarn(True) + try: + run_unittest(TestPy3KWarnings) + finally: + sys.setpy3kwarn(old_warn) if __name__ == '__main__': test_main() Index: Lib/test/test_sys.py =================================================================== --- Lib/test/test_sys.py (revision 62378) +++ Lib/test/test_sys.py (working copy) @@ -219,6 +219,16 @@ self.assertEqual(sys.getdlopenflags(), oldflags+1) sys.setdlopenflags(oldflags) + def test_py3kwarn(self): + old_warn = sys.getpy3kwarn() + sys.setpy3kwarn(True) + try: + with test.test_support.catch_warning() as w: + int < str + finally: + sys.setpy3kwarn(old_warn) + self.assert_(w.message) + def test_refcount(self): self.assertRaises(TypeError, sys.getrefcount) c = sys.getrefcount(None) @@ -354,7 +364,7 @@ def test_sys_flags(self): self.failUnless(sys.flags) - attrs = ("debug", "py3k_warning", "division_warning", "division_new", + attrs = ("debug", "division_warning", "division_new", "inspect", "interactive", "optimize", "dont_write_bytecode", "no_site", "ignore_environment", "tabcheck", "verbose", "unicode")