From 76f095f06a15ba6290f676cce290892f1ff5e822 Mon Sep 17 00:00:00 2001 From: Matti Picus Date: Tue, 2 Mar 2021 10:49:08 +0200 Subject: sync Py_.*Flags with sys.flags, issue 3409 --- pypy/interpreter/app_main.py | 2 ++ pypy/module/cpyext/api.py | 55 ++++++++++++++++++++++++++++++---- pypy/module/cpyext/include/pythonrun.h | 4 +-- pypy/module/cpyext/src/missing.c | 3 +- pypy/module/cpyext/test/test_cpyext.py | 12 ++++++++ pypy/module/sys/app.py | 6 ++-- 6 files changed, 71 insertions(+), 11 deletions(-) diff --git a/pypy/interpreter/app_main.py b/pypy/interpreter/app_main.py index 3ba312c7c8..b695384c24 100755 --- a/pypy/interpreter/app_main.py +++ b/pypy/interpreter/app_main.py @@ -381,6 +381,8 @@ def set_io_encoding(io_encoding, io_encoding_output, errors, overridden): set_file_encoding(f, encoding, errors) # Order is significant! +# Keep synchronized with pypy.module.sys.app.sysflags and +# pypy.module.cpyext._flags sys_flags = ( "debug", "py3k_warning", diff --git a/pypy/module/cpyext/api.py b/pypy/module/cpyext/api.py index 84ec9ed150..237285be5c 100644 --- a/pypy/module/cpyext/api.py +++ b/pypy/module/cpyext/api.py @@ -622,12 +622,8 @@ SYMBOLS_C = [ 'PyFunction_Type', 'PyMethod_Type', 'PyRange_Type', 'PyTraceBack_Type', - 'Py_DebugFlag', 'Py_VerboseFlag', 'Py_InteractiveFlag', 'Py_InspectFlag', - 'Py_OptimizeFlag', 'Py_NoSiteFlag', 'Py_BytesWarningFlag', 'Py_UseClassExceptionsFlag', - 'Py_FrozenFlag', 'Py_TabcheckFlag', 'Py_UnicodeFlag', 'Py_IgnoreEnvironmentFlag', - 'Py_DivisionWarningFlag', 'Py_DontWriteBytecodeFlag', 'Py_NoUserSiteDirectory', - '_Py_QnewFlag', 'Py_Py3kWarningFlag', 'Py_HashRandomizationFlag', '_Py_PackageContext', - 'PyOS_InputHook', + 'Py_UseClassExceptionsFlag', 'Py_FrozenFlag', # not part of sys.flags + '_Py_PackageContext', 'PyOS_InputHook', '_PyTraceMalloc_Track', '_PyTraceMalloc_Untrack', 'PyMem_Malloc', 'PyObject_Free', 'PyObject_GC_Del', 'PyType_GenericAlloc', '_PyObject_New', '_PyObject_NewVar', @@ -640,6 +636,30 @@ FORWARD_DECLS = [] INIT_FUNCTIONS = [] BOOTSTRAP_FUNCTIONS = [] +# Keep synchronized with pypy.interpreter.app_main.sys_flags and +# module.sys.app.sysflags. Synchronized in an init_function +_flags = ( + # c name, sys.flags name + ('Py_DebugFlag', 'debug'), + ('Py_Py3kWarningFlag', 'py3k_warning'), + ('Py_DivisionWarningFlag', 'division_warning'), + ('_Py_QnewFlag', 'division_new'), + ('Py_InspectFlag', 'inspect'), + ('Py_InteractiveFlag', 'interactive'), + ('Py_OptimizeFlag', 'optimize'), + ('Py_DontWriteBytecodeFlag', 'dont_write_bytecode'), + ('Py_NoUserSiteDirectory', 'no_user_site'), + ('Py_NoSiteFlag', 'no_site'), + ('Py_IgnoreEnvironmentFlag', 'ignore_environment'), + ('Py_TabcheckFlag', 'tabcheck'), + ('Py_VerboseFlag', 'verbose'), + ('Py_UnicodeFlag', 'unicode'), + ('Py_BytesWarningFlag', 'bytes_warning'), + ('Py_HashRandomizationFlag', 'hash_randomization'), +) + +SYMBOLS_C += [c_name for c_name, _ in _flags] + # this needs to include all prebuilt pto, otherwise segfaults occur register_global('_Py_NoneStruct', 'PyObject*', 'space.w_None', header=pypy_decl) @@ -1168,6 +1188,22 @@ def attach_c_functions(space, eci, prefix): state.C.tuple_new = rffi.llexternal( '_PyPy_tuple_new', [PyTypeObjectPtr, PyObject, PyObject], PyObject, compilation_info=eci, _nowrapper=True) + if we_are_translated(): + eci_flags = eci + else: + # To get this to work in tests, we need a new eci + libs = eci.get_module_files()[1].libraries + eci_flags = ExternalCompilationInfo( + include_dirs=include_dirs, + includes=['Python.h'], + link_extra = libs, + ) + for c_name, attr in _flags: + _, setter = rffi.CExternVariable(rffi.SIGNED, c_name, eci_flags, + _nowrapper=True, c_type='int') + setattr(state.C, 'set_' + attr, setter) + + def init_function(func): INIT_FUNCTIONS.append(func) @@ -1181,6 +1217,13 @@ def run_bootstrap_functions(space): for func in BOOTSTRAP_FUNCTIONS: func(space) +@bootstrap_function +def init_flags(space): + state = space.fromcache(State) + for _, attr in _flags: + f = getattr(state.C, 'set_' + attr) + f(space.sys.get_flag(attr)) + #_____________________________________________________ # Build the bridge DLL, Allow extension DLLs to call # back into Pypy space functions diff --git a/pypy/module/cpyext/include/pythonrun.h b/pypy/module/cpyext/include/pythonrun.h index 27917e5676..098c94f63b 100644 --- a/pypy/module/cpyext/include/pythonrun.h +++ b/pypy/module/cpyext/include/pythonrun.h @@ -16,8 +16,8 @@ PyAPI_DATA(int) Py_InspectFlag; PyAPI_DATA(int) Py_OptimizeFlag; PyAPI_DATA(int) Py_NoSiteFlag; PyAPI_DATA(int) Py_BytesWarningFlag; -PyAPI_DATA(int) Py_UseClassExceptionsFlag; -PyAPI_DATA(int) Py_FrozenFlag; +PyAPI_DATA(int) Py_UseClassExceptionsFlag; /* Unused, removed in 3.7 */ +PyAPI_DATA(int) Py_FrozenFlag; /* set when the python is "frozen" */ PyAPI_DATA(int) Py_TabcheckFlag; PyAPI_DATA(int) Py_UnicodeFlag; PyAPI_DATA(int) Py_IgnoreEnvironmentFlag; diff --git a/pypy/module/cpyext/src/missing.c b/pypy/module/cpyext/src/missing.c index 2d28016dad..5971f72bf5 100644 --- a/pypy/module/cpyext/src/missing.c +++ b/pypy/module/cpyext/src/missing.c @@ -12,7 +12,8 @@ int Py_DebugFlag = 1; int Py_VerboseFlag = 0; int Py_InteractiveFlag = 0; int Py_InspectFlag = 0; -int Py_OptimizeFlag = 0; +/* intentionally set to -1 for test, should be reset at startup */ +int Py_OptimizeFlag = -1; int Py_NoSiteFlag = 0; int Py_BytesWarningFlag = 0; int Py_UseClassExceptionsFlag = 0; diff --git a/pypy/module/cpyext/test/test_cpyext.py b/pypy/module/cpyext/test/test_cpyext.py index 3b5d41bf38..c7d2671a05 100644 --- a/pypy/module/cpyext/test/test_cpyext.py +++ b/pypy/module/cpyext/test/test_cpyext.py @@ -939,3 +939,15 @@ class AppTestCpythonExtension(AppTestCpythonExtensionBase): ''' ), ]) + + def test_consistent_flags(self): + import sys + mod = self.import_extension('foo', [ + ('test_optimize', 'METH_NOARGS', + ''' + return PyLong_FromLong(Py_OptimizeFlag); + '''), + ]) + # This is intentionally set to -1 by default from missing.c + # and should be set to sys.flags.optimize at startup + assert mod.test_optimize() == sys.flags.optimize diff --git a/pypy/module/sys/app.py b/pypy/module/sys/app.py index ea1100517e..52d42880e8 100644 --- a/pypy/module/sys/app.py +++ b/pypy/module/sys/app.py @@ -75,11 +75,11 @@ def callstats(): return None copyright_str = """ -Copyright 2003-2016 PyPy development team. +Copyright 2003-2021 PyPy development team. All Rights Reserved. For further information, see -Portions Copyright (c) 2001-2016 Python Software Foundation. +Portions Copyright (c) 2001-2021 Python Software Foundation. All Rights Reserved. Portions Copyright (c) 2000 BeOpen.com. @@ -92,6 +92,8 @@ Portions Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam. All Rights Reserved. """ +# Keep synchronized with pypy.interpreter.app_main.sys_flags and +# pypy.module.cpyext._flags # This is tested in test_app_main.py class sysflags: -- cgit v1.2.3-65-gdbad