Python 3.9.6 to 3.10.6 upgrade give pyarena methods error


I have a C++ project in which we are using python libraries, and the version of python that is currently being used is 3.9.6

In the code we are using PyArena_New(), PyParser_ASTFromString(),PyArena_Free() functions

This was working fine until we upgraded the python version to 3.10.6

As per the Change Log of python 3.10 these methods along with the pyarena.h has be removed

Instead, they have been moved to includes/internal/ directory as pycore_pyarena.h

Based on what we have noticed the reference for the pycore header files is no way to be found in Python.h

Even after manually adding the header files and Macro in the project the references are not properly getting resolved

_PyArena_New() , _PyParser_ASTFromString() ,_PyArena_Free()

And these are the errors that persist

Can you please let us know the proper reference and macro that needs to be uses

I found this: bpo-43244: Remove pyarena.h header by vstinner · Pull Request #25007 · python/cpython · GitHub. It looks like the functions you are using were never documented and were never meant for external use, so they were removed.

In addition, PyParser_ASTFromString is no longer supported. In Python 3.9 and before this returned a concrete parse tree. The parser was rewritten and the whole concept of a concrete parse tree no longer exists. See PEP 617. There are corresponding APIs to get an AST.

can you please suggest an alternative for these functions in 3.10.6

bool runPythonScript(const IString& strScript, PyObject* globals, PyObject* locals, bool bExeSingleLine = false, const IString& scriptFileName = _DNGI("<string>"))
		PyObject* res = nullptr;
		auto arena = PyArena_New();
		if (arena)
			auto mod = PyParser_ASTFromString(Ns::ToHString(strScript).c_str(), Ns::ToHString(scriptFileName).c_str(), bExeSingleLine ? Py_single_input : Py_file_input, nullptr, arena);
			if (mod)
				auto co = PyAST_Compile(mod, Ns::ToHString(scriptFileName).c_str(), nullptr, arena);
				if (co)
					res = PyEval_EvalCode((PyObject*)(co), globals, locals);
			return true;
			return false;


You’re using the old removed low-level API. Why not calling the built-in compile() function instead to compile a string to a Python object instead? Sorry, I don’t recall if there is a public C API just for that.

PyEval_GetBuiltins() can be used to get built-in functions.

You probably want to use on of the Py_CompileStringExFlags or one of the other compile variants to compile, or even PyRun_String without explicitly compiling.