Skip to content

fix: win_py13 SystemError from pytest #122

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

Merged
merged 5 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/matrix-and-codecov-on-merge-to-main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
uses: Billingegroup/release-scripts/.github/workflows/_matrix-and-codecov-on-merge-to-main.yml@v0
with:
project: diffpy.pdffit2
python_versions: "3.11, 3.12"
python_versions: "3.11, 3.12, 3.13"
c_extension: true
headless: false
secrets:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish-docs-on-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ jobs:
project: diffpy.pdffit2
c_extension: true
headless: false
python_version: 3.12
python_version: 3.13
2 changes: 1 addition & 1 deletion .github/workflows/tests-on-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ jobs:
project: diffpy.pdffi2
c_extension: true
headless: false
python_version: 3.12
python_version: 3.13
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
6 changes: 3 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ If you use diffpy.pdffit2 in a scientific publication, we would like you to cite
Installation
------------

diffpy.pdffit2 supports Python 3.11 and 3.12.
diffpy.pdffit2 supports Python 3.11, 3.12, and 3.13.

Windows, macOS (non-Arm64), Linux
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -102,7 +102,7 @@ macOS (Arm64)

Create a new conda environment ``diffpy.pdffit2_env``: ::

conda create -n diffpy.pdffit2_env python=3.12
conda create -n diffpy.pdffit2_env python=3.13

Activate the environment: ::

Expand All @@ -121,7 +121,7 @@ Build from source

For advanced users, obtain the source archive, and in the ``diffpy.pdffit2`` directory, run ::

conda create -n diffpy.pdffit2_env python=3.12 \
conda create -n diffpy.pdffit2_env python=3.13 \
--file requirements/test.txt \
--file requirements/conda.txt \
--file requirements/build.txt
Expand Down
24 changes: 24 additions & 0 deletions news/win_memory.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
**Added:**

* Added `restore_stdout` function and wrapper.
* Added Python 3.13 support.

**Changed:**

* Changed `pytest` `capture_output` fixture. Now automatically restores `sys.stdout`.

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* Fixed `SystemError` when running `pytest` on Windows with Python 3.13.

**Security:**

* <news item>
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ maintainers = [
description = "PDFfit2 - real space structure refinement program."
keywords = ["PDF", "structure refinement"]
readme = "README.rst"
requires-python = ">=3.11, <3.13"
requires-python = ">=3.11, <3.14"
classifiers = [
'Development Status :: 5 - Production/Stable',
'Environment :: Console',
Expand All @@ -27,6 +27,7 @@ classifiers = [
'Operating System :: Unix',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
'Programming Language :: Python :: 3.13',
'Topic :: Scientific/Engineering :: Physics',
'Topic :: Scientific/Engineering :: Chemistry',
]
Expand Down
9 changes: 9 additions & 0 deletions src/diffpy/pdffit2/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,13 @@ def redirect_stdout(dst):
return


def restore_stdout():
"""Restore the standard output."""
from diffpy.pdffit2.pdffit2 import restore_stdout

restore_stdout()
global stdout
return


# End of file
4 changes: 4 additions & 0 deletions src/extensions/pdffit2module/bindings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,10 @@ struct PyMethodDef pypdffit2_methods[] = {
{pypdffit2_redirect_stdout__name__, pypdffit2_redirect_stdout,
METH_VARARGS, pypdffit2_redirect_stdout__doc__},

//restore_stdout
{pypdffit2_restore_stdout__name__, pypdffit2_restore_stdout,
METH_VARARGS, pypdffit2_restore_stdout__doc__},

//is_element
{pypdffit2_is_element__name__, pypdffit2_is_element,
METH_VARARGS, pypdffit2_is_element__doc__},
Expand Down
30 changes: 30 additions & 0 deletions src/extensions/pdffit2module/misc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2201,6 +2201,36 @@ PyObject * pypdffit2_redirect_stdout(PyObject *, PyObject *args)
return Py_None;
}

// restore_stdout
char pypdffit2_restore_stdout__doc__[] =
"Restore engine output to the default stream (std::cout).";
char pypdffit2_restore_stdout__name__[] =
"restore_stdout";

PyObject * pypdffit2_restore_stdout(PyObject *, PyObject *args)
{
// no arguments.
if (!PyArg_ParseTuple(args, ""))
return 0;

// If the global output stream pointer is not std::cout, then delete the custom stream.
if (NS_PDFFIT2::pout != &std::cout)
{
delete NS_PDFFIT2::pout;
NS_PDFFIT2::pout = &std::cout;
}

// Clean up the custom stream buffer
if (py_stdout_streambuf)
{
delete py_stdout_streambuf;
py_stdout_streambuf = nullptr;
}

Py_INCREF(Py_None);
return Py_None;
}

// is_element
char pypdffit2_is_element__doc__[] = "Check if element or isotope is defined in the built-in periodic table.";
char pypdffit2_is_element__name__[] = "is_element";
Expand Down
6 changes: 6 additions & 0 deletions src/extensions/pdffit2module/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,12 @@ extern char pypdffit2_redirect_stdout__name__[];
extern "C"
PyObject * pypdffit2_redirect_stdout(PyObject *, PyObject *);

// restore_stdout
extern char pypdffit2_restore_stdout__doc__[];
extern char pypdffit2_restore_stdout__name__[];
extern "C"
PyObject * pypdffit2_restore_stdout(PyObject *, PyObject *);

// is_element
extern char pypdffit2_is_element__doc__[];
extern char pypdffit2_is_element__name__[];
Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def _capture(f, *args, **kwargs):
f(*args, **kwargs)
finally:
diffpy.pdffit2.redirect_stdout(savestdout)
diffpy.pdffit2.output.restore_stdout()
return fp.getvalue()

return _capture
Loading