mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
[3.12] gh-128911: Add tests on the PyImport C API (GH-128915) (GH-128960) (#128989)
* Add Modules/_testlimitedcapi/import.c * Add Lib/test/test_capi/test_import.py * Remove _testcapi.check_pyimport_addmodule(): tests already covered by newly added tests. (cherry picked from commit34ded1a1a1) Co-authored-by: Victor Stinner <vstinner@python.org> Co-authored-by: Serhiy Storchaka <storchaka@gmail.com> (cherry picked from commitd95ba9fa11)
This commit is contained in:
parent
6d638c2655
commit
83de72e5ec
7 changed files with 609 additions and 1 deletions
313
Lib/test/test_capi/test_import.py
Normal file
313
Lib/test/test_capi/test_import.py
Normal file
|
|
@ -0,0 +1,313 @@
|
|||
import importlib.util
|
||||
import os.path
|
||||
import sys
|
||||
import types
|
||||
import unittest
|
||||
from test.support import os_helper
|
||||
from test.support import import_helper
|
||||
from test.support.warnings_helper import check_warnings
|
||||
|
||||
_testcapi = import_helper.import_module('_testcapi')
|
||||
NULL = None
|
||||
|
||||
|
||||
class ImportTests(unittest.TestCase):
|
||||
def test_getmagicnumber(self):
|
||||
# Test PyImport_GetMagicNumber()
|
||||
magic = _testcapi.PyImport_GetMagicNumber()
|
||||
self.assertEqual(magic,
|
||||
int.from_bytes(importlib.util.MAGIC_NUMBER, 'little'))
|
||||
|
||||
def test_getmagictag(self):
|
||||
# Test PyImport_GetMagicTag()
|
||||
tag = _testcapi.PyImport_GetMagicTag()
|
||||
self.assertEqual(tag, sys.implementation.cache_tag)
|
||||
|
||||
def test_getmoduledict(self):
|
||||
# Test PyImport_GetModuleDict()
|
||||
modules = _testcapi.PyImport_GetModuleDict()
|
||||
self.assertIs(modules, sys.modules)
|
||||
|
||||
def check_import_loaded_module(self, import_module):
|
||||
for name in ('os', 'sys', 'test', 'unittest'):
|
||||
with self.subTest(name=name):
|
||||
self.assertIn(name, sys.modules)
|
||||
old_module = sys.modules[name]
|
||||
module = import_module(name)
|
||||
self.assertIsInstance(module, types.ModuleType)
|
||||
self.assertIs(module, old_module)
|
||||
|
||||
def check_import_fresh_module(self, import_module):
|
||||
old_modules = dict(sys.modules)
|
||||
try:
|
||||
for name in ('colorsys', 'math'):
|
||||
with self.subTest(name=name):
|
||||
sys.modules.pop(name, None)
|
||||
module = import_module(name)
|
||||
self.assertIsInstance(module, types.ModuleType)
|
||||
self.assertIs(module, sys.modules[name])
|
||||
self.assertEqual(module.__name__, name)
|
||||
finally:
|
||||
sys.modules.clear()
|
||||
sys.modules.update(old_modules)
|
||||
|
||||
def test_getmodule(self):
|
||||
# Test PyImport_GetModule()
|
||||
getmodule = _testcapi.PyImport_GetModule
|
||||
self.check_import_loaded_module(getmodule)
|
||||
|
||||
nonexistent = 'nonexistent'
|
||||
self.assertNotIn(nonexistent, sys.modules)
|
||||
self.assertIs(getmodule(nonexistent), KeyError)
|
||||
self.assertIs(getmodule(''), KeyError)
|
||||
self.assertIs(getmodule(object()), KeyError)
|
||||
|
||||
self.assertRaises(TypeError, getmodule, []) # unhashable
|
||||
# CRASHES getmodule(NULL)
|
||||
|
||||
def check_addmodule(self, add_module, accept_nonstr=False):
|
||||
# create a new module
|
||||
names = ['nonexistent']
|
||||
if accept_nonstr:
|
||||
names.append(b'\xff') # non-UTF-8
|
||||
for name in names:
|
||||
with self.subTest(name=name):
|
||||
self.assertNotIn(name, sys.modules)
|
||||
try:
|
||||
module = add_module(name)
|
||||
self.assertIsInstance(module, types.ModuleType)
|
||||
self.assertEqual(module.__name__, name)
|
||||
self.assertIs(module, sys.modules[name])
|
||||
finally:
|
||||
sys.modules.pop(name, None)
|
||||
|
||||
# get an existing module
|
||||
self.check_import_loaded_module(add_module)
|
||||
|
||||
def test_addmoduleobject(self):
|
||||
# Test PyImport_AddModuleObject()
|
||||
addmoduleobject = _testcapi.PyImport_AddModuleObject
|
||||
self.check_addmodule(addmoduleobject, accept_nonstr=True)
|
||||
|
||||
self.assertRaises(TypeError, addmoduleobject, []) # unhashable
|
||||
# CRASHES addmoduleobject(NULL)
|
||||
|
||||
def test_addmodule(self):
|
||||
# Test PyImport_AddModule()
|
||||
addmodule = _testcapi.PyImport_AddModule
|
||||
self.check_addmodule(addmodule)
|
||||
|
||||
self.assertRaises(UnicodeDecodeError, addmodule, b'\xff')
|
||||
# CRASHES addmodule(NULL)
|
||||
|
||||
def check_import_func(self, import_module):
|
||||
self.check_import_loaded_module(import_module)
|
||||
self.check_import_fresh_module(import_module)
|
||||
self.assertRaises(ModuleNotFoundError, import_module, 'nonexistent')
|
||||
self.assertRaises(ValueError, import_module, '')
|
||||
|
||||
def test_import(self):
|
||||
# Test PyImport_Import()
|
||||
import_ = _testcapi.PyImport_Import
|
||||
self.check_import_func(import_)
|
||||
|
||||
self.assertRaises(TypeError, import_, b'os')
|
||||
self.assertRaises(SystemError, import_, NULL)
|
||||
|
||||
def test_importmodule(self):
|
||||
# Test PyImport_ImportModule()
|
||||
importmodule = _testcapi.PyImport_ImportModule
|
||||
self.check_import_func(importmodule)
|
||||
|
||||
self.assertRaises(UnicodeDecodeError, importmodule, b'\xff')
|
||||
# CRASHES importmodule(NULL)
|
||||
|
||||
def test_importmodulenoblock(self):
|
||||
# Test deprecated PyImport_ImportModuleNoBlock()
|
||||
importmodulenoblock = _testcapi.PyImport_ImportModuleNoBlock
|
||||
self.check_import_func(importmodulenoblock)
|
||||
|
||||
self.assertRaises(UnicodeDecodeError, importmodulenoblock, b'\xff')
|
||||
# CRASHES importmodulenoblock(NULL)
|
||||
|
||||
def check_frozen_import(self, import_frozen_module):
|
||||
# Importing a frozen module executes its code, so start by unloading
|
||||
# the module to execute the code in a new (temporary) module.
|
||||
old_zipimport = sys.modules.pop('zipimport')
|
||||
try:
|
||||
self.assertEqual(import_frozen_module('zipimport'), 1)
|
||||
|
||||
# import zipimport again
|
||||
self.assertEqual(import_frozen_module('zipimport'), 1)
|
||||
finally:
|
||||
sys.modules['zipimport'] = old_zipimport
|
||||
|
||||
# not a frozen module
|
||||
self.assertEqual(import_frozen_module('sys'), 0)
|
||||
self.assertEqual(import_frozen_module('nonexistent'), 0)
|
||||
self.assertEqual(import_frozen_module(''), 0)
|
||||
|
||||
def test_importfrozenmodule(self):
|
||||
# Test PyImport_ImportFrozenModule()
|
||||
importfrozenmodule = _testcapi.PyImport_ImportFrozenModule
|
||||
self.check_frozen_import(importfrozenmodule)
|
||||
|
||||
self.assertRaises(UnicodeDecodeError, importfrozenmodule, b'\xff')
|
||||
# CRASHES importfrozenmodule(NULL)
|
||||
|
||||
def test_importfrozenmoduleobject(self):
|
||||
# Test PyImport_ImportFrozenModuleObject()
|
||||
importfrozenmoduleobject = _testcapi.PyImport_ImportFrozenModuleObject
|
||||
self.check_frozen_import(importfrozenmoduleobject)
|
||||
self.assertEqual(importfrozenmoduleobject(b'zipimport'), 0)
|
||||
self.assertEqual(importfrozenmoduleobject(NULL), 0)
|
||||
|
||||
def test_importmoduleex(self):
|
||||
# Test PyImport_ImportModuleEx()
|
||||
importmoduleex = _testcapi.PyImport_ImportModuleEx
|
||||
self.check_import_func(lambda name: importmoduleex(name, NULL, NULL, NULL))
|
||||
|
||||
self.assertRaises(ModuleNotFoundError, importmoduleex, 'nonexistent', NULL, NULL, NULL)
|
||||
self.assertRaises(ValueError, importmoduleex, '', NULL, NULL, NULL)
|
||||
self.assertRaises(UnicodeDecodeError, importmoduleex, b'\xff', NULL, NULL, NULL)
|
||||
# CRASHES importmoduleex(NULL, NULL, NULL, NULL)
|
||||
|
||||
def check_importmodulelevel(self, importmodulelevel):
|
||||
self.check_import_func(lambda name: importmodulelevel(name, NULL, NULL, NULL, 0))
|
||||
|
||||
self.assertRaises(ModuleNotFoundError, importmodulelevel, 'nonexistent', NULL, NULL, NULL, 0)
|
||||
self.assertRaises(ValueError, importmodulelevel, '', NULL, NULL, NULL, 0)
|
||||
|
||||
if __package__:
|
||||
self.assertIs(importmodulelevel('test_import', globals(), NULL, NULL, 1),
|
||||
sys.modules['test.test_capi.test_import'])
|
||||
self.assertIs(importmodulelevel('test_capi', globals(), NULL, NULL, 2),
|
||||
sys.modules['test.test_capi'])
|
||||
self.assertRaises(ValueError, importmodulelevel, 'os', NULL, NULL, NULL, -1)
|
||||
with self.assertWarns(ImportWarning):
|
||||
self.assertRaises(KeyError, importmodulelevel, 'test_import', {}, NULL, NULL, 1)
|
||||
self.assertRaises(TypeError, importmodulelevel, 'test_import', [], NULL, NULL, 1)
|
||||
|
||||
def test_importmodulelevel(self):
|
||||
# Test PyImport_ImportModuleLevel()
|
||||
importmodulelevel = _testcapi.PyImport_ImportModuleLevel
|
||||
self.check_importmodulelevel(importmodulelevel)
|
||||
|
||||
self.assertRaises(UnicodeDecodeError, importmodulelevel, b'\xff', NULL, NULL, NULL, 0)
|
||||
# CRASHES importmodulelevel(NULL, NULL, NULL, NULL, 0)
|
||||
|
||||
def test_importmodulelevelobject(self):
|
||||
# Test PyImport_ImportModuleLevelObject()
|
||||
importmodulelevel = _testcapi.PyImport_ImportModuleLevelObject
|
||||
self.check_importmodulelevel(importmodulelevel)
|
||||
|
||||
self.assertRaises(TypeError, importmodulelevel, b'os', NULL, NULL, NULL, 0)
|
||||
self.assertRaises(ValueError, importmodulelevel, NULL, NULL, NULL, NULL, 0)
|
||||
|
||||
def check_executecodemodule(self, execute_code, *args):
|
||||
name = 'test_import_executecode'
|
||||
try:
|
||||
# Create a temporary module where the code will be executed
|
||||
self.assertNotIn(name, sys.modules)
|
||||
module = _testcapi.PyImport_AddModule(name)
|
||||
self.assertFalse(hasattr(module, 'attr'))
|
||||
|
||||
# Execute the code
|
||||
code = compile('attr = 1', '<test>', 'exec')
|
||||
module2 = execute_code(name, code, *args)
|
||||
self.assertIs(module2, module)
|
||||
|
||||
# Check the function side effects
|
||||
self.assertEqual(module.attr, 1)
|
||||
finally:
|
||||
sys.modules.pop(name, None)
|
||||
return module.__spec__.origin
|
||||
|
||||
def test_executecodemodule(self):
|
||||
# Test PyImport_ExecCodeModule()
|
||||
execcodemodule = _testcapi.PyImport_ExecCodeModule
|
||||
self.check_executecodemodule(execcodemodule)
|
||||
|
||||
code = compile('attr = 1', '<test>', 'exec')
|
||||
self.assertRaises(UnicodeDecodeError, execcodemodule, b'\xff', code)
|
||||
# CRASHES execcodemodule(NULL, code)
|
||||
# CRASHES execcodemodule(name, NULL)
|
||||
|
||||
def test_executecodemoduleex(self):
|
||||
# Test PyImport_ExecCodeModuleEx()
|
||||
execcodemoduleex = _testcapi.PyImport_ExecCodeModuleEx
|
||||
|
||||
# Test NULL path (it should not crash)
|
||||
self.check_executecodemodule(execcodemoduleex, NULL)
|
||||
|
||||
# Test non-NULL path
|
||||
pathname = b'pathname'
|
||||
origin = self.check_executecodemodule(execcodemoduleex, pathname)
|
||||
self.assertEqual(origin, os.path.abspath(os.fsdecode(pathname)))
|
||||
|
||||
pathname = os_helper.TESTFN_UNDECODABLE
|
||||
if pathname:
|
||||
origin = self.check_executecodemodule(execcodemoduleex, pathname)
|
||||
self.assertEqual(origin, os.path.abspath(os.fsdecode(pathname)))
|
||||
|
||||
code = compile('attr = 1', '<test>', 'exec')
|
||||
self.assertRaises(UnicodeDecodeError, execcodemoduleex, b'\xff', code, NULL)
|
||||
# CRASHES execcodemoduleex(NULL, code, NULL)
|
||||
# CRASHES execcodemoduleex(name, NULL, NULL)
|
||||
|
||||
def check_executecode_pathnames(self, execute_code_func, object=False):
|
||||
# Test non-NULL pathname and NULL cpathname
|
||||
|
||||
# Test NULL paths (it should not crash)
|
||||
self.check_executecodemodule(execute_code_func, NULL, NULL)
|
||||
|
||||
pathname = 'pathname'
|
||||
origin = self.check_executecodemodule(execute_code_func, pathname, NULL)
|
||||
self.assertEqual(origin, os.path.abspath(os.fsdecode(pathname)))
|
||||
origin = self.check_executecodemodule(execute_code_func, NULL, pathname)
|
||||
if not object:
|
||||
self.assertEqual(origin, os.path.abspath(os.fsdecode(pathname)))
|
||||
|
||||
pathname = os_helper.TESTFN_UNDECODABLE
|
||||
if pathname:
|
||||
if object:
|
||||
pathname = os.fsdecode(pathname)
|
||||
origin = self.check_executecodemodule(execute_code_func, pathname, NULL)
|
||||
self.assertEqual(origin, os.path.abspath(os.fsdecode(pathname)))
|
||||
self.check_executecodemodule(execute_code_func, NULL, pathname)
|
||||
|
||||
# Test NULL pathname and non-NULL cpathname
|
||||
pyc_filename = importlib.util.cache_from_source(__file__)
|
||||
py_filename = importlib.util.source_from_cache(pyc_filename)
|
||||
origin = self.check_executecodemodule(execute_code_func, NULL, pyc_filename)
|
||||
if not object:
|
||||
self.assertEqual(origin, py_filename)
|
||||
|
||||
def test_executecodemodulewithpathnames(self):
|
||||
# Test PyImport_ExecCodeModuleWithPathnames()
|
||||
execute_code_func = _testcapi.PyImport_ExecCodeModuleWithPathnames
|
||||
self.check_executecode_pathnames(execute_code_func)
|
||||
|
||||
code = compile('attr = 1', '<test>', 'exec')
|
||||
self.assertRaises(UnicodeDecodeError, execute_code_func, b'\xff', code, NULL, NULL)
|
||||
# CRASHES execute_code_func(NULL, code, NULL, NULL)
|
||||
# CRASHES execute_code_func(name, NULL, NULL, NULL)
|
||||
|
||||
def test_executecodemoduleobject(self):
|
||||
# Test PyImport_ExecCodeModuleObject()
|
||||
execute_code_func = _testcapi.PyImport_ExecCodeModuleObject
|
||||
self.check_executecode_pathnames(execute_code_func, object=True)
|
||||
|
||||
code = compile('attr = 1', '<test>', 'exec')
|
||||
self.assertRaises(TypeError, execute_code_func, [], code, NULL, NULL)
|
||||
# CRASHES execute_code_func(NULL, code, NULL, NULL)
|
||||
# CRASHES execute_code_func(name, NULL, NULL, NULL)
|
||||
|
||||
# TODO: test PyImport_GetImporter()
|
||||
# TODO: test PyImport_ReloadModule()
|
||||
# TODO: test PyImport_ExtendInittab()
|
||||
# PyImport_AppendInittab() is tested by test_embed
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
@ -168,7 +168,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/abstract.c _testcapi/bytearray.c _testcapi/bytes.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.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/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyos.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/heaptype_relative.c _testcapi/gc.c _testcapi/sys.c
|
||||
@MODULE__TESTCAPI_TRUE@_testcapi _testcapimodule.c _testcapi/vectorcall.c _testcapi/vectorcall_limited.c _testcapi/heaptype.c _testcapi/abstract.c _testcapi/bytearray.c _testcapi/bytes.c _testcapi/unicode.c _testcapi/dict.c _testcapi/set.c _testcapi/list.c _testcapi/tuple.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/complex.c _testcapi/numbers.c _testcapi/structmember.c _testcapi/exceptions.c _testcapi/code.c _testcapi/buffer.c _testcapi/pyos.c _testcapi/run.c _testcapi/file.c _testcapi/codec.c _testcapi/immortal.c _testcapi/heaptype_relative.c _testcapi/gc.c _testcapi/sys.c _testcapi/import.c
|
||||
@MODULE__TESTCLINIC_TRUE@_testclinic _testclinic.c
|
||||
|
||||
# Some testing modules MUST be built as shared libraries.
|
||||
|
|
|
|||
287
Modules/_testcapi/import.c
Normal file
287
Modules/_testcapi/import.c
Normal file
|
|
@ -0,0 +1,287 @@
|
|||
#define PY_SSIZE_T_CLEAN
|
||||
|
||||
#include "parts.h"
|
||||
#include "util.h"
|
||||
|
||||
|
||||
/* Test PyImport_GetMagicNumber() */
|
||||
static PyObject *
|
||||
pyimport_getmagicnumber(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
|
||||
{
|
||||
long magic = PyImport_GetMagicNumber();
|
||||
return PyLong_FromLong(magic);
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_GetMagicTag() */
|
||||
static PyObject *
|
||||
pyimport_getmagictag(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
|
||||
{
|
||||
const char *tag = PyImport_GetMagicTag();
|
||||
return PyUnicode_FromString(tag);
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_GetModuleDict() */
|
||||
static PyObject *
|
||||
pyimport_getmoduledict(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
|
||||
{
|
||||
return Py_XNewRef(PyImport_GetModuleDict());
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_GetModule() */
|
||||
static PyObject *
|
||||
pyimport_getmodule(PyObject *Py_UNUSED(module), PyObject *name)
|
||||
{
|
||||
assert(!PyErr_Occurred());
|
||||
NULLABLE(name);
|
||||
PyObject *module = PyImport_GetModule(name);
|
||||
if (module == NULL && !PyErr_Occurred()) {
|
||||
return Py_NewRef(PyExc_KeyError);
|
||||
}
|
||||
return module;
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_AddModuleObject() */
|
||||
static PyObject *
|
||||
pyimport_addmoduleobject(PyObject *Py_UNUSED(module), PyObject *name)
|
||||
{
|
||||
NULLABLE(name);
|
||||
return Py_XNewRef(PyImport_AddModuleObject(name));
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_AddModule() */
|
||||
static PyObject *
|
||||
pyimport_addmodule(PyObject *Py_UNUSED(module), PyObject *args)
|
||||
{
|
||||
const char *name;
|
||||
Py_ssize_t size;
|
||||
if (!PyArg_ParseTuple(args, "z#", &name, &size)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return Py_XNewRef(PyImport_AddModule(name));
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_Import() */
|
||||
static PyObject *
|
||||
pyimport_import(PyObject *Py_UNUSED(module), PyObject *name)
|
||||
{
|
||||
NULLABLE(name);
|
||||
return PyImport_Import(name);
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_ImportModule() */
|
||||
static PyObject *
|
||||
pyimport_importmodule(PyObject *Py_UNUSED(module), PyObject *args)
|
||||
{
|
||||
const char *name;
|
||||
Py_ssize_t size;
|
||||
if (!PyArg_ParseTuple(args, "z#", &name, &size)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return PyImport_ImportModule(name);
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_ImportModuleNoBlock() */
|
||||
static PyObject *
|
||||
pyimport_importmodulenoblock(PyObject *Py_UNUSED(module), PyObject *args)
|
||||
{
|
||||
const char *name;
|
||||
Py_ssize_t size;
|
||||
if (!PyArg_ParseTuple(args, "z#", &name, &size)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
_Py_COMP_DIAG_PUSH
|
||||
_Py_COMP_DIAG_IGNORE_DEPR_DECLS
|
||||
return PyImport_ImportModuleNoBlock(name);
|
||||
_Py_COMP_DIAG_POP
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_ImportModuleEx() */
|
||||
static PyObject *
|
||||
pyimport_importmoduleex(PyObject *Py_UNUSED(module), PyObject *args)
|
||||
{
|
||||
const char *name;
|
||||
Py_ssize_t size;
|
||||
PyObject *globals, *locals, *fromlist;
|
||||
if (!PyArg_ParseTuple(args, "z#OOO",
|
||||
&name, &size, &globals, &locals, &fromlist)) {
|
||||
return NULL;
|
||||
}
|
||||
NULLABLE(globals);
|
||||
NULLABLE(locals);
|
||||
NULLABLE(fromlist);
|
||||
|
||||
return PyImport_ImportModuleEx(name, globals, locals, fromlist);
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_ImportModuleLevel() */
|
||||
static PyObject *
|
||||
pyimport_importmodulelevel(PyObject *Py_UNUSED(module), PyObject *args)
|
||||
{
|
||||
const char *name;
|
||||
Py_ssize_t size;
|
||||
PyObject *globals, *locals, *fromlist;
|
||||
int level;
|
||||
if (!PyArg_ParseTuple(args, "z#OOOi",
|
||||
&name, &size, &globals, &locals, &fromlist, &level)) {
|
||||
return NULL;
|
||||
}
|
||||
NULLABLE(globals);
|
||||
NULLABLE(locals);
|
||||
NULLABLE(fromlist);
|
||||
|
||||
return PyImport_ImportModuleLevel(name, globals, locals, fromlist, level);
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_ImportModuleLevelObject() */
|
||||
static PyObject *
|
||||
pyimport_importmodulelevelobject(PyObject *Py_UNUSED(module), PyObject *args)
|
||||
{
|
||||
PyObject *name, *globals, *locals, *fromlist;
|
||||
int level;
|
||||
if (!PyArg_ParseTuple(args, "OOOOi",
|
||||
&name, &globals, &locals, &fromlist, &level)) {
|
||||
return NULL;
|
||||
}
|
||||
NULLABLE(name);
|
||||
NULLABLE(globals);
|
||||
NULLABLE(locals);
|
||||
NULLABLE(fromlist);
|
||||
|
||||
return PyImport_ImportModuleLevelObject(name, globals, locals, fromlist, level);
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_ImportFrozenModule() */
|
||||
static PyObject *
|
||||
pyimport_importfrozenmodule(PyObject *Py_UNUSED(module), PyObject *args)
|
||||
{
|
||||
const char *name;
|
||||
Py_ssize_t size;
|
||||
if (!PyArg_ParseTuple(args, "z#", &name, &size)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
RETURN_INT(PyImport_ImportFrozenModule(name));
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_ImportFrozenModuleObject() */
|
||||
static PyObject *
|
||||
pyimport_importfrozenmoduleobject(PyObject *Py_UNUSED(module), PyObject *name)
|
||||
{
|
||||
NULLABLE(name);
|
||||
RETURN_INT(PyImport_ImportFrozenModuleObject(name));
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_ExecCodeModule() */
|
||||
static PyObject *
|
||||
pyimport_executecodemodule(PyObject *Py_UNUSED(module), PyObject *args)
|
||||
{
|
||||
const char *name;
|
||||
Py_ssize_t size;
|
||||
PyObject *code;
|
||||
if (!PyArg_ParseTuple(args, "z#O", &name, &size, &code)) {
|
||||
return NULL;
|
||||
}
|
||||
NULLABLE(code);
|
||||
|
||||
return PyImport_ExecCodeModule(name, code);
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_ExecCodeModuleEx() */
|
||||
static PyObject *
|
||||
pyimport_executecodemoduleex(PyObject *Py_UNUSED(module), PyObject *args)
|
||||
{
|
||||
const char *name;
|
||||
Py_ssize_t size;
|
||||
PyObject *code;
|
||||
const char *pathname;
|
||||
if (!PyArg_ParseTuple(args, "z#Oz#", &name, &size, &code, &pathname, &size)) {
|
||||
return NULL;
|
||||
}
|
||||
NULLABLE(code);
|
||||
|
||||
return PyImport_ExecCodeModuleEx(name, code, pathname);
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_ExecCodeModuleWithPathnames() */
|
||||
static PyObject *
|
||||
pyimport_executecodemodulewithpathnames(PyObject *Py_UNUSED(module), PyObject *args)
|
||||
{
|
||||
const char *name;
|
||||
Py_ssize_t size;
|
||||
PyObject *code;
|
||||
const char *pathname;
|
||||
const char *cpathname;
|
||||
if (!PyArg_ParseTuple(args, "z#Oz#z#", &name, &size, &code, &pathname, &size, &cpathname, &size)) {
|
||||
return NULL;
|
||||
}
|
||||
NULLABLE(code);
|
||||
|
||||
return PyImport_ExecCodeModuleWithPathnames(name, code,
|
||||
pathname, cpathname);
|
||||
}
|
||||
|
||||
|
||||
/* Test PyImport_ExecCodeModuleObject() */
|
||||
static PyObject *
|
||||
pyimport_executecodemoduleobject(PyObject *Py_UNUSED(module), PyObject *args)
|
||||
{
|
||||
PyObject *name, *code, *pathname, *cpathname;
|
||||
if (!PyArg_ParseTuple(args, "OOOO", &name, &code, &pathname, &cpathname)) {
|
||||
return NULL;
|
||||
}
|
||||
NULLABLE(name);
|
||||
NULLABLE(code);
|
||||
NULLABLE(pathname);
|
||||
NULLABLE(cpathname);
|
||||
|
||||
return PyImport_ExecCodeModuleObject(name, code, pathname, cpathname);
|
||||
}
|
||||
|
||||
|
||||
static PyMethodDef test_methods[] = {
|
||||
{"PyImport_GetMagicNumber", pyimport_getmagicnumber, METH_NOARGS},
|
||||
{"PyImport_GetMagicTag", pyimport_getmagictag, METH_NOARGS},
|
||||
{"PyImport_GetModuleDict", pyimport_getmoduledict, METH_NOARGS},
|
||||
{"PyImport_GetModule", pyimport_getmodule, METH_O},
|
||||
{"PyImport_AddModuleObject", pyimport_addmoduleobject, METH_O},
|
||||
{"PyImport_AddModule", pyimport_addmodule, METH_VARARGS},
|
||||
{"PyImport_Import", pyimport_import, METH_O},
|
||||
{"PyImport_ImportModule", pyimport_importmodule, METH_VARARGS},
|
||||
{"PyImport_ImportModuleNoBlock", pyimport_importmodulenoblock, METH_VARARGS},
|
||||
{"PyImport_ImportModuleEx", pyimport_importmoduleex, METH_VARARGS},
|
||||
{"PyImport_ImportModuleLevel", pyimport_importmodulelevel, METH_VARARGS},
|
||||
{"PyImport_ImportModuleLevelObject", pyimport_importmodulelevelobject, METH_VARARGS},
|
||||
{"PyImport_ImportFrozenModule", pyimport_importfrozenmodule, METH_VARARGS},
|
||||
{"PyImport_ImportFrozenModuleObject", pyimport_importfrozenmoduleobject, METH_O},
|
||||
{"PyImport_ExecCodeModule", pyimport_executecodemodule, METH_VARARGS},
|
||||
{"PyImport_ExecCodeModuleEx", pyimport_executecodemoduleex, METH_VARARGS},
|
||||
{"PyImport_ExecCodeModuleWithPathnames", pyimport_executecodemodulewithpathnames, METH_VARARGS},
|
||||
{"PyImport_ExecCodeModuleObject", pyimport_executecodemoduleobject, METH_VARARGS},
|
||||
{NULL},
|
||||
};
|
||||
|
||||
|
||||
int
|
||||
_PyTestCapi_Init_Import(PyObject *module)
|
||||
{
|
||||
return PyModule_AddFunctions(module, test_methods);
|
||||
}
|
||||
|
|
@ -55,6 +55,7 @@ int _PyTestCapi_Init_Codec(PyObject *module);
|
|||
int _PyTestCapi_Init_Immortal(PyObject *module);
|
||||
int _PyTestCapi_Init_GC(PyObject *module);
|
||||
int _PyTestCapi_Init_Sys(PyObject *module);
|
||||
int _PyTestCapi_Init_Import(PyObject *module);
|
||||
|
||||
#ifdef LIMITED_API_AVAILABLE
|
||||
int _PyTestCapi_Init_VectorcallLimited(PyObject *module);
|
||||
|
|
|
|||
|
|
@ -4071,6 +4071,9 @@ PyInit__testcapi(void)
|
|||
if (_PyTestCapi_Init_GC(m) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (_PyTestCapi_Init_Import(m) < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifndef LIMITED_API_AVAILABLE
|
||||
PyModule_AddObjectRef(m, "LIMITED_API_AVAILABLE", Py_False);
|
||||
|
|
|
|||
|
|
@ -105,6 +105,7 @@
|
|||
<ClCompile Include="..\Modules\_testcapi\unicode.c" />
|
||||
<ClCompile Include="..\Modules\_testcapi\dict.c" />
|
||||
<ClCompile Include="..\Modules\_testcapi\set.c" />
|
||||
<ClCompile Include="..\Modules\_testcapi\import.c" />
|
||||
<ClCompile Include="..\Modules\_testcapi\list.c" />
|
||||
<ClCompile Include="..\Modules\_testcapi\tuple.c" />
|
||||
<ClCompile Include="..\Modules\_testcapi\pytime.c" />
|
||||
|
|
|
|||
|
|
@ -108,6 +108,9 @@
|
|||
<ClCompile Include="..\Modules\_testcapi\run.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\Modules\_testcapi\import.c">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ResourceCompile Include="..\PC\python_nt.rc">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue