Skip to content

Commit

Permalink
pythongh-105751: Reenable disable test_ctypes tests
Browse files Browse the repository at this point in the history
Reenable 3 tests:

* test_overflow()
* test_basic_wstrings()
* test_toolong()

Changes:

* Replace sys.maxint with sys.maxsize
* Replace c_wstring() with create_unicode_buffer()
* Update issue number
  • Loading branch information
vstinner committed Jun 15, 2023
1 parent da911a6 commit d5927bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 31 deletions.
7 changes: 3 additions & 4 deletions Lib/test/test_ctypes/test_memfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,15 @@


class MemFunctionsTest(unittest.TestCase):
@unittest.skip('test disabled')
def test_overflow(self):
# string_at and wstring_at must use the Python calling
# convention (which acquires the GIL and checks the Python
# error flag). Provoke an error and catch it; see also issue
# #3554: <http://bugs.python.org/issue3554>
# gh-47804.
self.assertRaises((OverflowError, MemoryError, SystemError),
lambda: wstring_at(u"foo", sys.maxint - 1))
lambda: wstring_at(u"foo", sys.maxsize - 1))
self.assertRaises((OverflowError, MemoryError, SystemError),
lambda: string_at("foo", sys.maxint - 1))
lambda: string_at("foo", sys.maxsize - 1))

def test_memmove(self):
# large buffers apparently increase the chance that the memory
Expand Down
49 changes: 22 additions & 27 deletions Lib/test/test_ctypes/test_strings.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest
from ctypes import create_string_buffer, sizeof, byref, c_char, c_wchar
from ctypes import (create_string_buffer, create_unicode_buffer,
sizeof, byref, c_char, c_wchar)


class StringArrayTestCase(unittest.TestCase):
Expand Down Expand Up @@ -91,41 +92,35 @@ def test_wchar(self):
repr(byref(c_wchar("x")))
c_wchar("x")

@unittest.skip('test disabled')
def test_basic_wstrings(self):
cs = c_wstring("abcdef")

# XXX This behaviour is about to change:
# len returns the size of the internal buffer in bytes.
# This includes the terminating NUL character.
self.assertEqual(sizeof(cs), 14)

# The value property is the string up to the first terminating NUL.
cs = create_unicode_buffer("abcdef")
self.assertEqual(cs.value, "abcdef")
self.assertEqual(c_wstring("abc\000def").value, "abc")

self.assertEqual(c_wstring("abc\000def").value, "abc")
# value can be changed
cs.value = "abc"
self.assertEqual(cs.value, "abc")

# string is truncated at NUL character
cs.value = "def\0z"
self.assertEqual(cs.value, "def")

# The raw property is the total buffer contents:
self.assertEqual(cs.raw, "abcdef\000")
self.assertEqual(c_wstring("abc\000def").raw, "abc\000def\000")
self.assertEqual(create_unicode_buffer("abc\0def").value, "abc")

# We can change the value:
cs.value = "ab"
self.assertEqual(cs.value, "ab")
self.assertEqual(cs.raw, "ab\000\000\000\000\000")
# created with an empty string
cs = create_unicode_buffer(3)
self.assertEqual(cs.value, "")

self.assertRaises(TypeError, c_wstring, "123")
self.assertRaises(ValueError, c_wstring, 0)
cs.value = "abc"
self.assertEqual(cs.value, "abc")

@unittest.skip('test disabled')
def test_toolong(self):
cs = c_wstring("abcdef")
# Much too long string:
self.assertRaises(ValueError, setattr, cs, "value", "123456789012345")
cs = create_unicode_buffer("abc")
with self.assertRaises(ValueError):
cs.value = "abcdef"

# One char too long values:
self.assertRaises(ValueError, setattr, cs, "value", "1234567")
cs = create_unicode_buffer(4)
with self.assertRaises(ValueError):
cs.value = "abcdef"


def run_test(rep, msg, func, arg):
Expand Down

0 comments on commit d5927bc

Please sign in to comment.