Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-94808: Cover PyOS_mystrnicmp and PyOS_mystricmp #102469

Merged
merged 3 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Modules/Setup.stdlib.in
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@
@MODULE__XXTESTFUZZ_TRUE@_xxtestfuzz _xxtestfuzz/_xxtestfuzz.c _xxtestfuzz/fuzzer.c
@MODULE__TESTBUFFER_TRUE@_testbuffer _testbuffer.c
@MODULE__TESTINTERNALCAPI_TRUE@_testinternalcapi _testinternalcapi.c
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/unicode.c _testcapi/getargs.c _testcapi/pytime.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/unicode.c _testcapi/getargs.c _testcapi/pytime.c _testcapi/datetime.c _testcapi/docstring.c _testcapi/mem.c _testcapi/watchers.c _testcapi/long.c _testcapi/float.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/pyos.c
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c

# Some testing modules MUST be built as shared libraries.
Expand Down
1 change: 1 addition & 0 deletions Modules/_testcapi/parts.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ int _PyTestCapi_Init_Float(PyObject *module);
int _PyTestCapi_Init_Structmember(PyObject *module);
int _PyTestCapi_Init_Exceptions(PyObject *module);
int _PyTestCapi_Init_Code(PyObject *module);
int _PyTestCapi_Init_PyOS(PyObject *module);

#ifdef LIMITED_API_AVAILABLE
int _PyTestCapi_Init_VectorcallLimited(PyObject *module);
Expand Down
60 changes: 60 additions & 0 deletions Modules/_testcapi/pyos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include "parts.h"


static PyObject *
test_PyOS_mystrnicmp(PyObject *self, PyObject *Py_UNUSED(ignored))
{
assert(PyOS_mystrnicmp("", "", 0) == 0);
assert(PyOS_mystrnicmp("", "", 1) == 0);

assert(PyOS_mystrnicmp("insert", "ins", 3) == 0);
assert(PyOS_mystrnicmp("ins", "insert", 3) == 0);
assert(PyOS_mystrnicmp("insect", "insert", 3) == 0);

assert(PyOS_mystrnicmp("insert", "insert", 6) == 0);
assert(PyOS_mystrnicmp("Insert", "insert", 6) == 0);
assert(PyOS_mystrnicmp("INSERT", "insert", 6) == 0);
assert(PyOS_mystrnicmp("insert", "insert", 10) == 0);

assert(PyOS_mystrnicmp("invert", "insert", 6) == ('v' - 's'));
assert(PyOS_mystrnicmp("insert", "invert", 6) == ('s' - 'v'));
assert(PyOS_mystrnicmp("insert", "ins\0rt", 6) == 'e');

// GH-21845
assert(PyOS_mystrnicmp("insert\0a", "insert\0b", 8) == 0);

Py_RETURN_NONE;
}

static PyObject *
test_PyOS_mystricmp(PyObject *self, PyObject *Py_UNUSED(ignored))
{
assert(PyOS_mystricmp("", "") == 0);
assert(PyOS_mystricmp("insert", "insert") == 0);
assert(PyOS_mystricmp("Insert", "insert") == 0);
assert(PyOS_mystricmp("INSERT", "insert") == 0);
assert(PyOS_mystricmp("insert", "ins") == 'e');
assert(PyOS_mystricmp("ins", "insert") == -'e');

// GH-21845
assert(PyOS_mystricmp("insert", "ins\0rt") == 'e');
assert(PyOS_mystricmp("invert", "insert") == ('v' - 's'));

Py_RETURN_NONE;
}

static PyMethodDef test_methods[] = {
{"test_PyOS_mystrnicmp", test_PyOS_mystrnicmp, METH_NOARGS, NULL},
{"test_PyOS_mystricmp", test_PyOS_mystricmp, METH_NOARGS, NULL},
{NULL},
};

int
_PyTestCapi_Init_PyOS(PyObject *mod)
{
if (PyModule_AddFunctions(mod, test_methods) < 0) {
return -1;
}

return 0;
}
3 changes: 3 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -4086,6 +4086,9 @@ PyInit__testcapi(void)
if (_PyTestCapi_Init_Code(m) < 0) {
return NULL;
}
if (_PyTestCapi_Init_PyOS(m) < 0) {
return NULL;
}

#ifndef LIMITED_API_AVAILABLE
PyModule_AddObjectRef(m, "LIMITED_API_AVAILABLE", Py_False);
Expand Down
1 change: 1 addition & 0 deletions PCbuild/_testcapi.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
<ClCompile Include="..\Modules\_testcapi\structmember.c" />
<ClCompile Include="..\Modules\_testcapi\exceptions.c" />
<ClCompile Include="..\Modules\_testcapi\code.c" />
<ClCompile Include="..\Modules\_testcapi\pyos.c" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\PC\python_nt.rc" />
Expand Down
3 changes: 3 additions & 0 deletions PCbuild/_testcapi.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@
<ClCompile Include="..\Modules\_testcapi\code.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\Modules\_testcapi\pyos.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\PC\python_nt.rc">
Expand Down