Skip to content

Commit 29ac8bf

Browse files
committed
Add test for native storage cleanup
1 parent 2af3a19 commit 29ac8bf

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3737
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3838
# SOFTWARE.
39+
import gc
3940

4041
import sys
4142
import os
@@ -400,6 +401,7 @@ def test(self):
400401
assert cresult == presult, ("%r == %r in %s(%s)" % (cresult, presult, self.name, pargs[i]))
401402
else:
402403
assert self.cmpfunc(cresult, presult), ("%r == %r in %s(%s)" % (cresult, presult, self.name, pargs[i]))
404+
gc.collect()
403405

404406
def __get__(self, instance, typ=None):
405407
if typ is None:

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_bytes.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,31 @@ def compile_module(self, name):
132132
resulttype="int"
133133
)
134134

135+
test_native_storage = CPyExtFunction(
136+
lambda args: args[0].encode('utf-8')[-1],
137+
lambda: (("hello",), ("world",)),
138+
argspec="O",
139+
arguments=["PyObject* arg"],
140+
resultspec="i",
141+
# The code is creating the bytes objects in such a roundabout way in order to make sure the native storage will
142+
# get collected after the test
143+
code="""
144+
int wrap_test_native_storage(PyObject* str) {
145+
PyObject* bytes = PyUnicode_AsUTF8String(str);
146+
if (bytes == NULL)
147+
return -1;
148+
char* s;
149+
Py_ssize_t sz;
150+
if (PyBytes_AsStringAndSize(bytes, &s, &sz) < 0)
151+
return -1;
152+
int ret = s[sz - 1];
153+
Py_DECREF(bytes);
154+
return ret;
155+
}
156+
""",
157+
callfunction='wrap_test_native_storage',
158+
)
159+
135160
# PyBytes_Size
136161
test_PyBytes_Size = CPyExtFunction(
137162
lambda b: len(b[0]),

0 commit comments

Comments
 (0)