From 41a6af8e1ab46c7cdf666a5d930a1086868b4693 Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Thu, 3 Oct 2019 14:18:24 +0100 Subject: [PATCH] provide six.pkgname to replace __package__ --- six.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/six.py b/six.py index 4c303ff97..9683dad9f 100644 --- a/six.py +++ b/six.py @@ -928,6 +928,22 @@ def python_2_unicode_compatible(klass): return klass +def pkgname(globals): + """ + Return the package name of a module. + + On Python 2 under __future__.absolute_import __package__ is None until an + explcit relative import is attempted. + + six.pgkname(globals()) is equivalent to __package__ on Python 3. + """ + pkgname_ = globals.get("__package__") + if pkgname_ is not None: + return pkgname_ + modname = globals.get("__name__") + return modename if "__path__" in globals else modname.rpartition(".")[0] + + # Complete the moves implementation. # This code is at the end of this module to speed up module loading. # Turn this module into a package.