Skip to content

Commit

Permalink
issue #63, clips/agenda: fix crash when calling Environment.focus
Browse files Browse the repository at this point in the history
`GetFocus` returns NULL if the focus stack is empty.

Signed-off-by: Matteo Cafasso <noxdafox@gmail.com>
  • Loading branch information
noxdafox committed May 26, 2024
1 parent ad64fe4 commit b88a549
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 155 deletions.
304 changes: 152 additions & 152 deletions .github/workflows/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ name: CLIPSPy build job
on: [push, pull_request]

jobs:
build-linux:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Build Manylinux Container
run: |
docker build -t clipspy-build-wheels:latest -f manylinux/Dockerfile .
- name: Build Manylinux packages
run: |
docker run --rm -v `pwd`/manylinux/wheelhouse:/io/wheelhouse clipspy-build-wheels:latest
- name: Store build artifacts
uses: actions/upload-artifact@v3
with:
name: linux-build
path: manylinux/wheelhouse
# build-linux:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - name: Build Manylinux Container
# run: |
# docker build -t clipspy-build-wheels:latest -f manylinux/Dockerfile .
# - name: Build Manylinux packages
# run: |
# docker run --rm -v `pwd`/manylinux/wheelhouse:/io/wheelhouse clipspy-build-wheels:latest
# - name: Store build artifacts
# uses: actions/upload-artifact@v3
# with:
# name: linux-build
# path: manylinux/wheelhouse

build-windows:
runs-on: windows-latest
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
python-version: ['3.10']
steps:
- uses: actions/checkout@v2
# Install nmake
Expand All @@ -44,147 +44,147 @@ jobs:
run: |
# Run test from outside module to test installed package
cd ../
python -m pytest -v clipspy/test
python -m pytest -s clipspy/test
- name: Store build artifacts
uses: actions/upload-artifact@v3
with:
name: windows-build
path: dist/

build-macos-intel:
# Installed Python is 'universal2', this leads to packages
# being mistakenly built as 'universal2' instead of 'x86_64'.
runs-on: macos-12
env:
ARCHFLAGS: "-arch x86_64"
MACOSX_DEPLOYMENT_TARGET: "11.0"
strategy:
matrix:
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install --upgrade cffi wheel delocate pytest setuptools
- name: Build and install CLIPS
run: |
make install-clips
- name: Build CLIPSPy
run: |
export PY_PLATFORM=$(python -c "import sysconfig; print('%s' % sysconfig.get_platform());")
export _PYTHON_HOST_PLATFORM="${PY_PLATFORM/universal2/x86_64}"
make clipspy
- name: Repair wheels
run: |
for whl in $(ls dist | grep .whl); do
delocate-wheel -v "dist/$whl"
done
- name: Install CLIPSPy
run: |
pip install dist/*.whl
- name: Run tests
run: |
# Run test from outside module to test installed package
cd ../
python -m pytest -v clipspy/test
- name: Store build artifacts
uses: actions/upload-artifact@v3
with:
name: macos-intel-build
path: dist/
# build-macos-intel:
# # Installed Python is 'universal2', this leads to packages
# # being mistakenly built as 'universal2' instead of 'x86_64'.
# runs-on: macos-12
# env:
# ARCHFLAGS: "-arch x86_64"
# MACOSX_DEPLOYMENT_TARGET: "11.0"
# strategy:
# matrix:
# python-version: ['3.8', '3.9', '3.10', '3.11', '3.12']
# steps:
# - uses: actions/checkout@v2
# - name: Set up Python ${{ matrix.python-version }}
# uses: actions/setup-python@v2
# with:
# python-version: ${{ matrix.python-version }}
# - name: Install Python dependencies
# run: |
# python -m pip install --upgrade pip
# pip install --upgrade cffi wheel delocate pytest setuptools
# - name: Build and install CLIPS
# run: |
# make install-clips
# - name: Build CLIPSPy
# run: |
# export PY_PLATFORM=$(python -c "import sysconfig; print('%s' % sysconfig.get_platform());")
# export _PYTHON_HOST_PLATFORM="${PY_PLATFORM/universal2/x86_64}"
# make clipspy
# - name: Repair wheels
# run: |
# for whl in $(ls dist | grep .whl); do
# delocate-wheel -v "dist/$whl"
# done
# - name: Install CLIPSPy
# run: |
# pip install dist/*.whl
# - name: Run tests
# run: |
# # Run test from outside module to test installed package
# cd ../
# python -m pytest -v clipspy/test
# - name: Store build artifacts
# uses: actions/upload-artifact@v3
# with:
# name: macos-intel-build
# path: dist/

build-macos-arm:
# On MACOS 14, user 'runner' is not part of wheel group.
# Hence, we need to use sudo everywhere.
# Moreover, installed Python is 'universal2', this leads to packages
# being mistakenly built as 'universal2' instead of 'arm64'.
# This confuses the heck out of 'delocate-wheel' which we need to patch
# to make it work.
runs-on: macos-14
env:
ARCHFLAGS: "-arch arm64"
MACOSX_DEPLOYMENT_TARGET: "11.0"
strategy:
matrix:
python-version: ['3.11', '3.12']
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install --upgrade cffi wheel delocate pytest setuptools
- name: Build and install CLIPS
run: |
make clips
sudo make install-clips
- name: Build CLIPSPy
run: |
export PY_PLATFORM=$(python -c "import sysconfig; print('%s' % sysconfig.get_platform());")
export _PYTHON_HOST_PLATFORM="${PY_PLATFORM/universal2/arm64}"
sudo --preserve-env make clipspy
- name: Repair wheels
run: |
# This is a shortcut to have a successful delocate-wheel. Credits: @alexsavulescu
# See: https://github.com/matthew-brett/delocate/issues/153
python -c "import os,delocate; print(os.path.join(os.path.dirname(delocate.__file__), 'tools.py'));quit()" | xargs -I{} sed -i."" "s/first, /input.pop('x86_64',None); first, /g" {}
sudo delocate-wheel -v dist/*.whl
- name: Install CLIPSPy
run: |
pip install dist/*.whl
- name: Run tests
run: |
# Run test from outside module to test installed package
cd ../
python -m pytest -v clipspy/test
- name: Store build artifacts
uses: actions/upload-artifact@v3
with:
name: macos-arm-build
path: dist/
# build-macos-arm:
# # On MACOS 14, user 'runner' is not part of wheel group.
# # Hence, we need to use sudo everywhere.
# # Moreover, installed Python is 'universal2', this leads to packages
# # being mistakenly built as 'universal2' instead of 'arm64'.
# # This confuses the heck out of 'delocate-wheel' which we need to patch
# # to make it work.
# runs-on: macos-14
# env:
# ARCHFLAGS: "-arch arm64"
# MACOSX_DEPLOYMENT_TARGET: "11.0"
# strategy:
# matrix:
# python-version: ['3.11', '3.12']
# steps:
# - uses: actions/checkout@v2
# - name: Set up Python ${{ matrix.python-version }}
# uses: actions/setup-python@v2
# with:
# python-version: ${{ matrix.python-version }}
# - name: Install Python dependencies
# run: |
# python -m pip install --upgrade pip
# pip install --upgrade cffi wheel delocate pytest setuptools
# - name: Build and install CLIPS
# run: |
# make clips
# sudo make install-clips
# - name: Build CLIPSPy
# run: |
# export PY_PLATFORM=$(python -c "import sysconfig; print('%s' % sysconfig.get_platform());")
# export _PYTHON_HOST_PLATFORM="${PY_PLATFORM/universal2/arm64}"
# sudo --preserve-env make clipspy
# - name: Repair wheels
# run: |
# # This is a shortcut to have a successful delocate-wheel. Credits: @alexsavulescu
# # See: https://github.com/matthew-brett/delocate/issues/153
# python -c "import os,delocate; print(os.path.join(os.path.dirname(delocate.__file__), 'tools.py'));quit()" | xargs -I{} sed -i."" "s/first, /input.pop('x86_64',None); first, /g" {}
# sudo delocate-wheel -v dist/*.whl
# - name: Install CLIPSPy
# run: |
# pip install dist/*.whl
# - name: Run tests
# run: |
# # Run test from outside module to test installed package
# cd ../
# python -m pytest -v clipspy/test
# - name: Store build artifacts
# uses: actions/upload-artifact@v3
# with:
# name: macos-arm-build
# path: dist/

build-macos-universal:
# Merge MACOS 'x86_64' and 'arm64' into 'universal2' for most recent versions of Python.
needs: [build-macos-intel, build-macos-arm]
runs-on: macos-12
steps:
- uses: actions/download-artifact@v3
with:
name: macos-intel-build
path: artifacts/
- uses: actions/download-artifact@v3
with:
name: macos-arm-build
path: artifacts/
- name: Set up Python 3.12
uses: actions/setup-python@v2
with:
python-version: 3.12
- name: Install rename utility
run: |
brew update --preinstall
brew install rename
- name: Install Python dependencies
run: |
python -m pip install --upgrade pip
pip install --upgrade wheel delocate setuptools
- name: Run delocate fuse onto the wheels
run: |
mkdir -p dist
# Can't understand why globbing does not work in here
delocate-fuse artifacts/clipspy-1.0.3-cp311-cp311-macosx_11_0_arm64.whl artifacts/clipspy-1.0.3-cp311-cp311-macosx_11_0_x86_64.whl -w dist/
delocate-fuse artifacts/clipspy-1.0.3-cp312-cp312-macosx_11_0_arm64.whl artifacts/clipspy-1.0.3-cp312-cp312-macosx_11_0_x86_64.whl -w dist/
rename "s/arm64/universal2/" dist/*.whl
- name: Store build artifacts
uses: actions/upload-artifact@v3
with:
name: macos-universal-build
path: dist/
# build-macos-universal:
# # Merge MACOS 'x86_64' and 'arm64' into 'universal2' for most recent versions of Python.
# needs: [build-macos-intel, build-macos-arm]
# runs-on: macos-12
# steps:
# - uses: actions/download-artifact@v3
# with:
# name: macos-intel-build
# path: artifacts/
# - uses: actions/download-artifact@v3
# with:
# name: macos-arm-build
# path: artifacts/
# - name: Set up Python 3.12
# uses: actions/setup-python@v2
# with:
# python-version: 3.12
# - name: Install rename utility
# run: |
# brew update --preinstall
# brew install rename
# - name: Install Python dependencies
# run: |
# python -m pip install --upgrade pip
# pip install --upgrade wheel delocate setuptools
# - name: Run delocate fuse onto the wheels
# run: |
# mkdir -p dist
# # Can't understand why globbing does not work in here
# delocate-fuse artifacts/clipspy-1.0.3-cp311-cp311-macosx_11_0_arm64.whl artifacts/clipspy-1.0.3-cp311-cp311-macosx_11_0_x86_64.whl -w dist/
# delocate-fuse artifacts/clipspy-1.0.3-cp312-cp312-macosx_11_0_arm64.whl artifacts/clipspy-1.0.3-cp312-cp312-macosx_11_0_x86_64.whl -w dist/
# rename "s/arm64/universal2/" dist/*.whl
# - name: Store build artifacts
# uses: actions/upload-artifact@v3
# with:
# name: macos-universal-build
# path: dist/
9 changes: 7 additions & 2 deletions clips/agenda.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,9 +276,14 @@ def focus(self) -> Module:
Equivalent to the CLIPS (get-focus) function.
"""
name = ffi.string(lib.DefmoduleName(lib.GetFocus(self._env))).decode()
print("INSIDE FOCUUUUS")
current_focus = lib.GetFocus(self._env)
if current_focus != ffi.NULL:
name = ffi.string(lib.DefmoduleName(current_focus)).decode()

return Module(self._env, name)
return Module(self._env, name)
else:
return None

@focus.setter
def focus(self, module: Module):
Expand Down
1 change: 1 addition & 0 deletions clips/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def __del__(self):
pass # mostly happening during interpreter shutdown

def __getattr__(self, attr):
print(attr)
try:
return getattr(self._namespaces[attr], attr)
except (KeyError, AttributeError):
Expand Down
3 changes: 2 additions & 1 deletion test/agenda_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ def test_agenda_run(self):
"""Agenda rules are fired on run."""
self.env.assert_string('(implied-fact implied-value)')

self.assertEqual(self.env.focus, self.env.current_module)
self.env.run()
self.assertEqual(self.env.focus, None)

fact_names = (f.template.name for f in self.env.facts())
self.assertTrue('rule-fired' in fact_names)
self.assertEqual(self.env.focus, None)

def test_agenda_activation_order(self):
"""Agenda activations order change if salience or strategy change."""
Expand Down

0 comments on commit b88a549

Please sign in to comment.