Skip to content

Commit

Permalink
Add functionality for enable_patching in cinder_unittest
Browse files Browse the repository at this point in the history
Summary: Add functionality for `enable_patching` field in `cinder_unittest`

Reviewed By: alexmalyshev

Differential Revision: D63842727

fbshipit-source-id: 4cfb80564857270149eaa278acd776e67ab88cb9
  • Loading branch information
Subbarao Garlapati authored and facebook-github-bot committed Oct 16, 2024
1 parent 0a169e2 commit 6cf6307
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions Include/cpython/initconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ typedef struct PyConfig {
int lazy_imports;
int use_py_compiler;
int install_strict_loader;
int enable_patching;

/* --- Path configuration inputs ------------ */
int pathconfig_warnings;
Expand Down
3 changes: 1 addition & 2 deletions Lib/test/test_cinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,8 +1011,7 @@ def test_strictmodule_delattr(self):

def test_strictmodule_setattr_with_patch_enabled(self):
foo = create_strict_module(x=1, enable_patching=True)
with self.assertRaises(AttributeError):
foo.x = 2
foo.x = 2

def test_strictmodule_patch_disabled(self):
foo = create_strict_module(x=1)
Expand Down
6 changes: 6 additions & 0 deletions Python/initconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -972,6 +972,7 @@ _PyConfig_Copy(PyConfig *config, const PyConfig *config2)
COPY_ATTR(_isolated_interpreter);
COPY_WSTRLIST(orig_argv);
COPY_ATTR(lazy_imports);
COPY_ATTR(enable_patching);
COPY_ATTR(use_py_compiler);
COPY_ATTR(install_strict_loader);

Expand Down Expand Up @@ -1921,6 +1922,11 @@ config_read_complex_options(PyConfig *config)
config->install_strict_loader = 1;
}

if (config_get_env(config, "PYTHONENABLEPATCHING")
|| config_get_xoption(config, L"enable-patching")) {
config->enable_patching = 1;
}

PyStatus status;
if (config->tracemalloc < 0) {
status = config_init_tracemalloc(config);
Expand Down
20 changes: 14 additions & 6 deletions Python/pylifecycle.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* Python interpreter top-level routines, including init/exit */

#include "Python.h"
#include <stdbool.h>

#include "cinder/hooks.h"

Expand Down Expand Up @@ -263,7 +264,7 @@ init_importlib_external(PyThreadState *tstate)


static int
_install_importlib_pycompile_helper(const char* loader_installer) {
_install_importlib_pycompile_helper(const char* loader_installer, PyObject *arg) {
// need to ensure global lazy imports are off while importing python
// compiler; it needs to be fully eagerly imported while we are still under
// the C compiler, since it won't be able to compile itself later.
Expand All @@ -272,14 +273,20 @@ _install_importlib_pycompile_helper(const char* loader_installer) {
interp->lazy_imports = 0;
PyObject* value;
PyObject *py_loader_module = PyImport_ImportModule("cinderx.compiler.pysourceloader");
PyObject *name = PyUnicode_FromString(loader_installer);
if (py_loader_module == NULL) {
return -1;
}
value = PyObject_CallMethod(py_loader_module, loader_installer, "");
if (arg) {
value = PyObject_CallMethodOneArg(py_loader_module, name, arg);
} else {
value = PyObject_CallMethodNoArgs(py_loader_module, name);
}
if (value == NULL) {
PyErr_Print();
return -1;
}
Py_CLEAR(name);
Py_XDECREF(value);
Py_XDECREF(py_loader_module);
interp->lazy_imports = orig_lazy_imports;
Expand All @@ -289,13 +296,14 @@ _install_importlib_pycompile_helper(const char* loader_installer) {
static int
install_importlib_pycompile()
{
return _install_importlib_pycompile_helper("_install_py_loader");
return _install_importlib_pycompile_helper("_install_py_loader", NULL);
}

static int
install_importlib_strict_compile()
install_importlib_strict_compile(bool enable_patching)
{
return _install_importlib_pycompile_helper("_install_strict_loader");
PyObject *arg = enable_patching ? Py_True : Py_False;
return _install_importlib_pycompile_helper("_install_strict_loader", arg);
}

/* Helper functions to better handle the legacy C locale
Expand Down Expand Up @@ -1259,7 +1267,7 @@ init_interp_main(PyThreadState *tstate)
}
if (config->install_strict_loader) {
/* install the strict/static loader */
if(install_importlib_strict_compile()) {
if(install_importlib_strict_compile(config->enable_patching)) {
fprintf(stderr, "installing strict/static compiler failed, traceback:\n");
PyErr_Print();
return _PyStatus_ERR("can't install strict/static compiler");
Expand Down

0 comments on commit 6cf6307

Please sign in to comment.