Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect PURE_PYTHON environment variable set to 0 #115

Merged
merged 1 commit into from
Oct 2, 2024

Conversation

perrinjerome
Copy link
Contributor

Setting to 0 should be equivalent to not setting the variable.

Refs zopefoundation/meta#283

@perrinjerome perrinjerome marked this pull request as draft September 30, 2024 06:09
@perrinjerome
Copy link
Contributor Author

oh tests are failing, I set to draft

Copy link
Contributor

@d-maurer d-maurer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The checker module contains a docstring specifying that the value of the PURE_PYTHON envvar is not relevant. It should get changed to be in line with this modification.

@perrinjerome
Copy link
Contributor Author

The checker module contains a docstring specifying that the value of the PURE_PYTHON envvar is not relevant. It should get changed to be in line with this modification.

Thank you, I have changed this.

oh tests are failing, I set to draft

actually it was OK, I opened the same pull request for another package, but tests here seems to be OK

@perrinjerome
Copy link
Contributor Author

perrinjerome commented Sep 30, 2024

actually it was OK, I opened the same pull request for another package, but tests here seems to be OK

... sorry . No, the tests also fail here. It seems there is a circular dependency between zopefoundation/zope.proxy#71 and this one, or something I don't understand yet.

@d-maurer
Copy link
Contributor

d-maurer commented Sep 30, 2024 via email

@d-maurer
Copy link
Contributor

Instead, the test should import "Proxy", which is the C implementation if available otherwise the Python implementation.

If we do this, we may want to add an additional test: if not PURE_PYTHON, then we should test that the C implementation is available. Currently, zope.security automatically switches to the Python implementation when the C implementation is unavailable. The test should ensure that this is only the case when we expect it to be so.

@perrinjerome
Copy link
Contributor Author

I understood the problem with the test, it's because the changes from this pull request depend on zopefoundation/zope.proxy#71 - there's a circular dependency between these two pull requests. The solution seems to be to merge both and release both packages.

Also, the test importing from zope.security.proxy import _Proxy seems correct, this is the test for the C version so it uses the full name as the target class, not the alias, to be sure to test the C class. The equivalent python test also uses the full name of the equivalent python class (from zope.security.proxy import ProxyPy) and the alias is not used here. This seems correct after all.


The long story about the analysis of the test errors ImportError: cannot import name '_Proxy' from 'zope.security.proxy' (/io/.tox/py310/lib/python3.10/site-packages/zope/security/proxy.py). This error happens here when running with PURE_PYTHON=0. In that test run, zope.security understands PURE_PYTHON=0 as "use C", but zope.proxy does not understands it and consider as "do not use C", because it is the current version from pypi without the changes from zopefoundation/zope.proxy#71

The test code is here:

@unittest.skipIf(PURE_PYTHON,
"Needs C extension")
class ProxyCTests(AbstractProxyTestBase,
unittest.TestCase):
def _getTargetClass(self): # pragma: no cover
from zope.security.proxy import _Proxy
return _Proxy

This test is not skipped because PURE_PYTHON is False.

from zope.security.proxy import _Proxy is re-exported here:

_c_available = not PURE_PYTHON
if _c_available: # pragma: no cover
try:
from zope.security._proxy import _Proxy
except (ImportError, AttributeError):
_c_available = False

The module import happens in C here:

if (Proxy_Import() < 0)
return MOD_ERROR_VAL;

static int
Proxy_Import(void)
{
if (_proxy_api == NULL) {
PyObject *m = PyImport_ImportModule("zope.proxy");
if (m != NULL) {
PyObject *tmp = PyObject_GetAttrString(m, "_CAPI");
if (tmp != NULL) {
if (PyCapsule_CheckExact(tmp))
_proxy_api = (ProxyInterface *)
PyCapsule_GetPointer(tmp, NULL);
Py_DECREF(tmp);
}
}
}
return (_proxy_api == NULL) ? -1 : 0;
}

In plain python, this would be from zope.proxy import _CAPI.

And in zope.proxy, this is still checking if 'PURE_PYTHON' not in os.environ: here

https://github.com/zopefoundation/zope.proxy/blob/cbb23c424826d89538b90bb17dc35d48bacc356a/src/zope/proxy/__init__.py#L483

and once zope.proxy is updated to use if not int(os.environ.get('PURE_PYTHON', '0')): the test will pass again.

@perrinjerome
Copy link
Contributor Author

In short, what we can do is merge #115 and zopefoundation/zope.proxy#71 although test fail for both pull requests, once boths are merged and released, the test should pass again.

@perrinjerome perrinjerome requested a review from d-maurer October 2, 2024 01:10
perrinjerome added a commit to zopefoundation/zope.proxy that referenced this pull request Oct 2, 2024
zope.proxy depends on zope.security for tests, tests relying on
PURE_PYTHON=0 need zope.security to also support it.

See zopefoundation/zope.security#115
perrinjerome added a commit to zopefoundation/zope.proxy that referenced this pull request Oct 2, 2024
zope.proxy depends on zope.security for tests, tests relying on
PURE_PYTHON=0 need zope.security to also support it.

See zopefoundation/zope.security#115
@perrinjerome
Copy link
Contributor Author

I pushed a tox.ini change to use zope.proxy from zopefoundation/zope.proxy#71 , this change is not planned to be merged, it's just to be sure that tests will be OK with this change.

@perrinjerome perrinjerome force-pushed the perrinjerome/PURE_PYTHON branch from 2da1909 to afc6ee6 Compare October 2, 2024 12:32
@perrinjerome perrinjerome marked this pull request as ready for review October 2, 2024 12:39
@perrinjerome
Copy link
Contributor Author

I cannot merge this one either because of the test status, but I believe it is in a state where it can be merged.
/cc @dataflake

@dataflake dataflake merged commit 057a7bf into master Oct 2, 2024
52 of 53 checks passed
@dataflake dataflake deleted the perrinjerome/PURE_PYTHON branch October 2, 2024 13:19
@d-maurer
Copy link
Contributor

d-maurer commented Oct 2, 2024 via email

@perrinjerome
Copy link
Contributor Author

...environment variable PURE_PYTHON (converted to int) has a nonzero value ..

Thank you, I did not notice your message before this was merged. I made a PR with your suggestion #116

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants