Skip to content
This repository was archived by the owner on Jan 23, 2024. It is now read-only.

Commit 9b0c8d1

Browse files
xinghuadou-googleXinghua Dou
authored andcommitted
Set default of the level argument of __import__ to 0 in Python 3.
#5 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=191107673
1 parent 8a11d28 commit 9b0c8d1

1 file changed

Lines changed: 10 additions & 1 deletion

File tree

src/googleclouddebugger/imphook2.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,14 +169,23 @@ def _ProcessImportBySuffix(name, fromlist, globals):
169169

170170
# pylint: disable=redefined-builtin, g-doc-args, g-doc-return-or-yield
171171
def _ImportHookBySuffix(
172-
name, globals=None, locals=None, fromlist=None, level=-1):
172+
name, globals=None, locals=None, fromlist=None, level=None):
173173
"""Callback when an import statement is executed by the Python interpreter.
174174
175175
Argument names have to exactly match those of __import__. Otherwise calls
176176
to __import__ that use keyword syntax will fail: __import('a', fromlist=[]).
177177
"""
178178
_IncrementNestLevel()
179179

180+
if level is None:
181+
# A level of 0 means absolute import, positive values means relative
182+
# imports, and -1 means to try both an absolute and relative import.
183+
# Since imports were disambiguated in Python 3, -1 is not a valid value.
184+
# The default values are 0 and -1 for Python 3 and 3 respectively.
185+
# https://docs.python.org/2/library/functions.html#__import__
186+
# https://docs.python.org/3/library/functions.html#__import__
187+
level = 0 if six.PY3 else -1
188+
180189
try:
181190
# Really import modules.
182191
module = _real_import(name, globals, locals, fromlist, level)

0 commit comments

Comments
 (0)