Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
gajim
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Weblate
gajim
Commits
b2bb1a84
Commit
b2bb1a84
authored
13 years ago
by
Dicson
Browse files
Options
Downloads
Patches
Plain Diff
update demandimport module from mercurial
parent
166d7b93
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/common/demandimport.py
+20
-10
20 additions, 10 deletions
src/common/demandimport.py
with
20 additions
and
10 deletions
src/common/demandimport.py
+
20
−
10
View file @
b2bb1a84
...
...
@@ -27,6 +27,17 @@ These imports will not be delayed:
import
__builtin__
_origimport
=
__import__
nothing
=
object
()
try
:
_origimport
(
__builtin__
.
__name__
,
{},
{},
None
,
-
1
)
except
TypeError
:
# no level argument
def
_import
(
name
,
globals
,
locals
,
fromlist
,
level
):
"
call _origimport with no level argument
"
return
_origimport
(
name
,
globals
,
locals
,
fromlist
)
else
:
_import
=
_origimport
class
_demandmod
(
object
):
"""
module demand-loader and proxy
"""
def
__init__
(
self
,
name
,
globals
,
locals
):
...
...
@@ -50,7 +61,7 @@ class _demandmod(object):
h
,
t
=
p
,
None
if
'
.
'
in
p
:
h
,
t
=
p
.
split
(
'
.
'
,
1
)
if
not
has
attr
(
mod
,
h
)
:
if
get
attr
(
mod
,
h
,
nothing
)
is
nothing
:
setattr
(
mod
,
h
,
_demandmod
(
p
,
mod
.
__dict__
,
mod
.
__dict__
))
elif
t
:
subload
(
getattr
(
mod
,
h
),
t
)
...
...
@@ -78,20 +89,17 @@ class _demandmod(object):
self
.
_load
()
setattr
(
self
.
_module
,
attr
,
val
)
def
_demandimport
(
name
,
globals
=
None
,
locals
=
None
,
fromlist
=
None
,
level
=
None
):
def
_demandimport
(
name
,
globals
=
None
,
locals
=
None
,
fromlist
=
None
,
level
=
-
1
):
if
not
locals
or
name
in
ignore
or
fromlist
==
(
'
*
'
,):
# these cases we can't really delay
if
level
is
None
:
return
_origimport
(
name
,
globals
,
locals
,
fromlist
)
else
:
return
_origimport
(
name
,
globals
,
locals
,
fromlist
,
level
)
return
_import
(
name
,
globals
,
locals
,
fromlist
,
level
)
elif
not
fromlist
:
# import a [as b]
if
'
.
'
in
name
:
# a.b
base
,
rest
=
name
.
split
(
'
.
'
,
1
)
# email.__init__ loading email.mime
if
globals
and
globals
.
get
(
'
__name__
'
,
None
)
==
base
:
return
_
orig
import
(
name
,
globals
,
locals
,
fromlist
)
return
_import
(
name
,
globals
,
locals
,
fromlist
,
level
)
# if a is already demand-loaded, add b to its submodule list
if
base
in
locals
:
if
isinstance
(
locals
[
base
],
_demandmod
):
...
...
@@ -99,19 +107,19 @@ def _demandimport(name, globals=None, locals=None, fromlist=None, level=None):
return
locals
[
base
]
return
_demandmod
(
name
,
globals
,
locals
)
else
:
if
level
is
not
None
:
if
level
!=
-
1
:
# from . import b,c,d or from .a import b,c,d
return
_origimport
(
name
,
globals
,
locals
,
fromlist
,
level
)
# from a import b,c,d
mod
=
_origimport
(
name
,
globals
,
locals
)
# recurse down the module chain
for
comp
in
name
.
split
(
'
.
'
)[
1
:]:
if
not
has
attr
(
mod
,
comp
)
:
if
get
attr
(
mod
,
comp
,
nothing
)
is
nothing
:
setattr
(
mod
,
comp
,
_demandmod
(
comp
,
mod
.
__dict__
,
mod
.
__dict__
))
mod
=
getattr
(
mod
,
comp
)
for
x
in
fromlist
:
# set requested submodules for demand load
if
not
has
attr
(
mod
,
x
)
:
if
get
attr
(
mod
,
x
,
nothing
)
is
nothing
:
setattr
(
mod
,
x
,
_demandmod
(
x
,
mod
.
__dict__
,
locals
))
return
mod
...
...
@@ -134,6 +142,8 @@ ignore = [
# raise ImportError if x not defined
'
__main__
'
,
'
_ssl
'
,
# conditional imports in the stdlib, issue1964
'
rfc822
'
,
'
mimetools
'
,
]
def
enable
():
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment