Skip to content

Commit 120beed

Browse files
committed
Some random change
1 parent 1285eb1 commit 120beed

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

src/main.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#define _PYXMLSEC_FREE_XMLSEC 1
2323
#define _PYXMLSEC_FREE_CRYPTOLIB 2
2424
#define _PYXMLSEC_FREE_ALL 3
25+
#define _PYXMLSEC_FREE_ALL_BUT_CRYPTOLIB 4
2526

2627
static int free_mode = _PYXMLSEC_FREE_NONE;
2728

@@ -52,6 +53,12 @@ static void PyXmlSec_Free(int what) {
5253
#endif
5354
case _PYXMLSEC_FREE_XMLSEC:
5455
xmlSecShutdown();
56+
break;
57+
case _PYXMLSEC_FREE_ALL_BUT_CRYPTOLIB:
58+
xmlSecCryptoShutdown();
59+
xmlSecCryptoAppShutdown();
60+
xmlSecShutdown();
61+
break;
5562
}
5663
free_mode = _PYXMLSEC_FREE_NONE;
5764
}
@@ -95,7 +102,10 @@ static int PyXmlSec_Init(void) {
95102
// We thus reinstall our callback now.
96103
PyXmlSec_InstallErrorCallback();
97104

98-
free_mode = _PYXMLSEC_FREE_ALL;
105+
// Keep the dynamically loaded crypto backend resident for the lifetime of
106+
// the process. Python-level constants cache xmlsec transform/keydata ids,
107+
// and unloading the backend invalidates those pointers after shutdown/init.
108+
free_mode = _PYXMLSEC_FREE_ALL_BUT_CRYPTOLIB;
99109
return 0;
100110
}
101111

tests/test_xmlsec.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
from pathlib import Path
12
import subprocess
23
import sys
4+
import unittest
35

46
import xmlsec
57
from tests import base
@@ -15,14 +17,32 @@ def test_reinitialize_module(self):
1517
xmlsec.shutdown()
1618
xmlsec.init()
1719

20+
21+
class TestInterpreterShutdown(unittest.TestCase):
1822
def test_interpreter_exit_with_live_xmlsec_objects(self):
19-
key_path = self.path('rsakey.pem')
23+
key_path = Path(__file__).with_name('data') / 'rsakey.pem'
2024
script = f"""
2125
import xmlsec
2226
23-
key = xmlsec.Key.from_file({key_path!r}, format=xmlsec.constants.KeyDataFormatPem)
27+
key = xmlsec.Key.from_file({str(key_path)!r}, format=xmlsec.constants.KeyDataFormatPem)
2428
ctx = xmlsec.SignatureContext()
2529
ctx.key = key
30+
"""
31+
proc = subprocess.run([sys.executable, '-c', script], capture_output=True, text=True)
32+
self.assertEqual(proc.returncode, 0, proc.stderr)
33+
34+
def test_reinitialize_module_preserves_constants(self):
35+
script = """
36+
import xmlsec
37+
38+
transform = xmlsec.constants.TransformExclC14N
39+
keydata = xmlsec.constants.KeyDataAes
40+
41+
xmlsec.shutdown()
42+
xmlsec.init()
43+
44+
assert transform.name == "exc-c14n"
45+
assert keydata.name == "aes"
2646
"""
2747
proc = subprocess.run([sys.executable, '-c', script], capture_output=True, text=True)
2848
self.assertEqual(proc.returncode, 0, proc.stderr)

0 commit comments

Comments
 (0)