PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/module/cpyext/import_.py

https://bitbucket.org/dac_io/pypy
Python | 125 lines | 123 code | 1 blank | 1 comment | 0 complexity | ff5be80da97108602d9902ac1e779f5c MD5 | raw file
  1. from pypy.interpreter import module
  2. from pypy.module.cpyext.api import (
  3. generic_cpy_call, cpython_api, PyObject, CONST_STRING)
  4. from pypy.module.cpyext.pyobject import borrow_from
  5. from pypy.rpython.lltypesystem import lltype, rffi
  6. from pypy.interpreter.error import OperationError
  7. from pypy.interpreter.module import Module
  8. from pypy.interpreter.pycode import PyCode
  9. from pypy.module.imp import importing
  10. @cpython_api([PyObject], PyObject)
  11. def PyImport_Import(space, w_name):
  12. """
  13. This is a higher-level interface that calls the current "import hook function".
  14. It invokes the __import__() function from the __builtins__ of the
  15. current globals. This means that the import is done using whatever import hooks
  16. are installed in the current environment, e.g. by rexec or ihooks.
  17. Always uses absolute imports."""
  18. caller = space.getexecutioncontext().gettopframe_nohidden()
  19. # Get the builtins from current globals
  20. if caller is not None:
  21. w_globals = caller.w_globals
  22. w_builtin = space.getitem(w_globals, space.wrap('__builtins__'))
  23. else:
  24. # No globals -- use standard builtins, and fake globals
  25. w_builtin = space.getbuiltinmodule('__builtin__')
  26. w_globals = space.newdict()
  27. space.setitem(w_globals, space.wrap("__builtins__"), w_builtin)
  28. # Get the __import__ function from the builtins
  29. if space.is_true(space.isinstance(w_builtin, space.w_dict)):
  30. w_import = space.getitem(w_builtin, space.wrap("__import__"))
  31. else:
  32. w_import = space.getattr(w_builtin, space.wrap("__import__"))
  33. # Call the __import__ function with the proper argument list
  34. # Always use absolute import here.
  35. return space.call_function(w_import,
  36. w_name, w_globals, w_globals,
  37. space.newlist([space.wrap("__doc__")]))
  38. @cpython_api([CONST_STRING], PyObject)
  39. def PyImport_ImportModule(space, name):
  40. return PyImport_Import(space, space.wrap(rffi.charp2str(name)))
  41. @cpython_api([CONST_STRING], PyObject)
  42. def PyImport_ImportModuleNoBlock(space, name):
  43. space.warn('PyImport_ImportModuleNoBlock() is not non-blocking',
  44. space.w_RuntimeWarning)
  45. return PyImport_Import(space, space.wrap(rffi.charp2str(name)))
  46. @cpython_api([PyObject], PyObject)
  47. def PyImport_ReloadModule(space, w_mod):
  48. from pypy.module.imp.importing import reload
  49. return reload(space, w_mod)
  50. @cpython_api([CONST_STRING], PyObject)
  51. def PyImport_AddModule(space, name):
  52. """Return the module object corresponding to a module name. The name
  53. argument may be of the form package.module. First check the modules
  54. dictionary if there's one there, and if not, create a new one and insert
  55. it in the modules dictionary. Return NULL with an exception set on
  56. failure.
  57. This function does not load or import the module; if the module wasn't
  58. already loaded, you will get an empty module object. Use
  59. PyImport_ImportModule() or one of its variants to import a module.
  60. Package structures implied by a dotted name for name are not created if
  61. not already present."""
  62. from pypy.module.imp.importing import check_sys_modules_w
  63. modulename = rffi.charp2str(name)
  64. w_mod = check_sys_modules_w(space, modulename)
  65. if not w_mod or space.is_w(w_mod, space.w_None):
  66. w_mod = Module(space, space.wrap(modulename))
  67. return borrow_from(None, w_mod)
  68. @cpython_api([], PyObject)
  69. def PyImport_GetModuleDict(space):
  70. """Return the dictionary used for the module administration (a.k.a.
  71. sys.modules). Note that this is a per-interpreter variable."""
  72. w_modulesDict = space.sys.get('modules')
  73. return borrow_from(None, w_modulesDict)
  74. @cpython_api([rffi.CCHARP, PyObject], PyObject)
  75. def PyImport_ExecCodeModule(space, name, w_code):
  76. """Given a module name (possibly of the form package.module) and a code
  77. object read from a Python bytecode file or obtained from the built-in
  78. function compile(), load the module. Return a new reference to the module
  79. object, or NULL with an exception set if an error occurred. Before Python
  80. 2.4, the module could still be created in error cases. Starting with Python
  81. 2.4, name is removed from sys.modules in error cases, and even if name was
  82. already in sys.modules on entry to PyImport_ExecCodeModule(). Leaving
  83. incompletely initialized modules in sys.modules is dangerous, as imports of
  84. such modules have no way to know that the module object is an unknown (and
  85. probably damaged with respect to the module author's intents) state.
  86. The module's __file__ attribute will be set to the code object's
  87. co_filename.
  88. This function will reload the module if it was already imported. See
  89. PyImport_ReloadModule() for the intended way to reload a module.
  90. If name points to a dotted name of the form package.module, any package
  91. structures not already created will still not be created.
  92. name is removed from sys.modules in error cases."""
  93. return PyImport_ExecCodeModuleEx(space, name, w_code,
  94. lltype.nullptr(rffi.CCHARP.TO))
  95. @cpython_api([rffi.CCHARP, PyObject, rffi.CCHARP], PyObject)
  96. def PyImport_ExecCodeModuleEx(space, name, w_code, pathname):
  97. """Like PyImport_ExecCodeModule(), but the __file__ attribute of
  98. the module object is set to pathname if it is non-NULL."""
  99. code = space.interp_w(PyCode, w_code)
  100. w_name = space.wrap(rffi.charp2str(name))
  101. if pathname:
  102. pathname = rffi.charp2str(pathname)
  103. else:
  104. pathname = code.co_filename
  105. w_mod = importing.add_module(space, w_name)
  106. space.setattr(w_mod, space.wrap('__file__'), space.wrap(pathname))
  107. importing.exec_code_module(space, w_mod, code)
  108. return w_mod