Skip to content

Commit 7185c44

Browse files
authored
Enable some ctypes tests (#1407)
* Enable CPython.ctypes.test_structures * Enable CPython.ctypes.test_values * Enable Src/IronPythonTest/Cases/CPythonCasesManifest.ini
1 parent c55190b commit 7185c44

6 files changed

Lines changed: 19 additions & 20 deletions

File tree

Src/IronPython.Modules/_ctypes/ArrayType.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ object INativeType.SetValue(MemoryHolder address, int offset, object value) {
243243

244244
return null;
245245
}
246-
throw PythonOps.TypeError("bytes expected instead of {0} instance", PythonOps.GetPythonTypeName(value));
246+
throw PythonOps.TypeError("expected bytes, {0} found", PythonOps.GetPythonTypeName(value));
247247
}
248248
if (st._type == SimpleTypeKind.WChar) {
249249
if (value is string str) {

Src/IronPython.Modules/_ctypes/MemoryHolder.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,8 @@ internal static Bytes ReadBytes(IntPtr addr, int offset, int length) {
216216
}
217217

218218
internal static Bytes ReadBytes(IntPtr addr, int offset) {
219+
if (addr == IntPtr.Zero && offset == 0) return null;
220+
219221
// instead of Marshal.PtrToStringAnsi we do this because
220222
// ptrToStringAnsi gives special treatment to values >= 128.
221223
MemoryStream res = new MemoryStream();

Src/IronPython.Modules/_ctypes/StructType.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,11 @@ object INativeType.SetValue(MemoryHolder/*!*/ address, int offset, object value)
168168
try {
169169
return SetValueInternal(address, offset, value);
170170
} catch (ArgumentTypeException e) {
171-
throw PythonOps.RuntimeError("({0}) <type 'exceptions.TypeError'>: {1}",
171+
throw PythonOps.RuntimeError("({0}) <class 'TypeError'>: {1}",
172172
Name,
173173
e.Message);
174174
} catch (ArgumentException e) {
175-
throw PythonOps.RuntimeError("({0}) <type 'exceptions.ValueError'>: {1}",
175+
throw PythonOps.RuntimeError("({0}) <class 'ValueError'>: {1}",
176176
Name,
177177
e.Message);
178178
}

Src/IronPythonTest/Cases/CPythonCasesManifest.ini

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,10 @@ Ignore=true
5555
[CPython.ctypes.test_slicing]
5656
Ignore=true # https://github.com/IronLanguages/ironpython3/issues/1299
5757

58-
[CPython.ctypes.test_stringptr]
59-
Ignore=true
60-
61-
[CPython.ctypes.test_structures]
62-
Ignore=true
63-
6458
[CPython.ctypes.test_unicode]
6559
Ignore=true
6660
Reason=https://github.com/IronLanguages/ironpython2/issues/405
6761

68-
[CPython.ctypes.test_values]
69-
Ignore=true
70-
7162
[CPython.ctypes.test_win32]
7263
Ignore=true
7364

Src/StdLib/Lib/ctypes/test/test_structures.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
from ctypes import *
33
from ctypes.test import need_symbol
44
from struct import calcsize
5-
import _testcapi
5+
try:
6+
import _testcapi
7+
except ImportError:
8+
_testcapi = None
69

710
class SubclassesTest(unittest.TestCase):
811
def test_subclass(self):
@@ -201,13 +204,14 @@ class X(Structure):
201204
"_pack_": -1}
202205
self.assertRaises(ValueError, type(Structure), "X", (Structure,), d)
203206

204-
# Issue 15989
205-
d = {"_fields_": [("a", c_byte)],
206-
"_pack_": _testcapi.INT_MAX + 1}
207-
self.assertRaises(ValueError, type(Structure), "X", (Structure,), d)
208-
d = {"_fields_": [("a", c_byte)],
209-
"_pack_": _testcapi.UINT_MAX + 2}
210-
self.assertRaises(ValueError, type(Structure), "X", (Structure,), d)
207+
if _testcapi:
208+
# Issue 15989
209+
d = {"_fields_": [("a", c_byte)],
210+
"_pack_": _testcapi.INT_MAX + 1}
211+
self.assertRaises(ValueError, type(Structure), "X", (Structure,), d)
212+
d = {"_fields_": [("a", c_byte)],
213+
"_pack_": _testcapi.UINT_MAX + 2}
214+
self.assertRaises(ValueError, type(Structure), "X", (Structure,), d)
211215

212216
def test_initializers(self):
213217
class Person(Structure):

Src/StdLib/Lib/ctypes/test/test_values.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import unittest
66
import sys
7+
from test import support
78
from ctypes import *
89

910
import _ctypes_test
@@ -28,6 +29,7 @@ def test_undefined(self):
2829
ctdll = CDLL(_ctypes_test.__file__)
2930
self.assertRaises(ValueError, c_int.in_dll, ctdll, "Undefined_Symbol")
3031

32+
@support.cpython_only
3133
class PythonValuesTestCase(unittest.TestCase):
3234
"""This test only works when python itself is a dll/shared library"""
3335

0 commit comments

Comments
 (0)