File: narrative.rst

package info (click to toggle)
zope.deferredimport 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 280 kB
  • sloc: python: 237; makefile: 150
file content (376 lines) | stat: -rw-r--r-- 9,926 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
==================
 Deferred Imports
==================

.. testsetup::

    import os
    import sys
    import warnings

    import zope.deferredimport

    __file__ = 'docs/narrative.rst'

    class OutErr(object):

        @staticmethod
        def write(message):
            sys.stdout.write(message)

    oldstderr, sys.stderr = sys.stderr, OutErr()

    tmp_d = os.path.join(
                os.path.dirname(zope.deferredimport.__file__), 'samples')

    zope.deferredimport.__path__.append(tmp_d)
    created_modules = []

    def create_module(**modules): #**
        for name, src in modules.items():
            fn = os.path.join(tmp_d, name + '.py')
            needs_update = True
            if os.path.exists(fn):
                with open(fn, 'r') as file:
                    needs_update = file.read() != src
            if needs_update:
                with open(fn, 'w') as file:
                    file.write(src)
            created_modules.append(name)

    def warn(message, type_, stacklevel):
        frame = sys._getframe(stacklevel)
        path = frame.f_globals['__file__']
        file = open(path)
        lineno = frame.f_lineno
        for i in range(lineno):
            line = file.readline()
        file.close()

        print("%s:%s: %s: %s\n  %s" % (
            path,
            frame.f_lineno,
            type_.__name__,
            message,
            line.strip(),
            ))

    oldwarn, warnings.warn = warnings.warn, warn


Often, especially for package modules, you want to import names for
convenience, but not actually perform the imports until necessary.
The zope.deferredimport package provided facilities for defining names
in modules that will be imported from somewhere else when used.  You
can also cause deprecation warnings to be issued when a variable is
used, but we'll get to that later.

The :func:`zope.deferredimport.define` function can be used to define one or
more names to be imported when they are accessed.  Simply provide
names as keyword arguments with import specifiers as values.  The
import specifiers are given as strings of the form "module:name",
where module is the dotted name of the module and name is a, possibly
dotted, name of an object within the module.

To see how this works, we'll create some sample modules within the
zope.deferredimport package.  We'll actually use a helper function
specific to this document to define the modules inline so we can
easily see what's in them.  Let's start by defining a module, sample1,
that defined some things to be imported:

.. doctest::

    >>> create_module(sample1 = '''\
    ... print("Sampe 1 imported!")
    ...
    ... x = 1
    ...
    ...
    ... class C:
    ...     y = 2
    ...
    ...
    ... z = 3
    ... q = 4
    ... ''')

Note that the module starts by printing a message.  This allows us to
see when the module is actually imported.  Now, let's define a module
that imports some names from this module:


.. doctest::

    >>> create_module(sample2 = '''\
    ... import zope.deferredimport
    ...
    ...
    ... zope.deferredimport.define(
    ...     sample1='zope.deferredimport.sample1',
    ...     one='zope.deferredimport.sample1:x',
    ...     two='zope.deferredimport.sample1:C.y',
    ... )
    ...
    ... three = 3
    ... x = 4
    ...
    ...
    ... def getx():
    ...     return x
    ... ''')


In this example, we defined the name 'sample1' as the module
zope.deferredimport.sample1. The module isn't imported immediately,
but will be imported when needed.  Similarly, the name 'one' is
defined as the 'x' attribute of sample1.

The sample1 module prints a message when it is
imported.  When we import sample2, we don't see a message until we
access a variable:

.. doctest::

    >>> import zope.deferredimport.sample2
    >>> print(zope.deferredimport.sample2.one)
    Sampe 1 imported!
    1

    >>> import zope.deferredimport.sample1

    >>> zope.deferredimport.sample2.sample1 is zope.deferredimport.sample1
    True

Note that a deferred attribute appears in a module's dictionary *after*
it is accessed the first time:

.. doctest::

    >>> 'two' in zope.deferredimport.sample2.__dict__
    False

    >>> zope.deferredimport.sample2.two
    2

    >>> 'two' in zope.deferredimport.sample2.__dict__
    True

When deferred imports are used, the original module is replaced with a
proxy.

.. doctest::

    >>> type(zope.deferredimport.sample2)
    <class 'zope.deferredimport.deferredmodule.ModuleProxy'>

But we can use the proxy just like the original.  We can even update
it.

.. doctest::

    >>> zope.deferredimport.sample2.x=5
    >>> zope.deferredimport.sample2.getx()
    5

And the inspect module thinks it's a module:

.. doctest::

   >>> import inspect
   >>> inspect.ismodule(zope.deferredimport.sample2)
   True


In the example above, the modules were fairly simple.  Let's look at a
more complicated example.

.. doctest::

    # >>> create_module(sample3 = '''\
    # ... import zope.deferredimport
    # ... import zope.deferredimport.sample4
    # ...
    # ... zope.deferredimport.define(
    # ...     sample1 = 'zope.deferredimport.sample1',
    # ...     one = 'zope.deferredimport.sample1:x',
    # ...     two = 'zope.deferredimport.sample1:C.y',
    # ... )
    # ...
    # ... x = 1
    # ...
    # ... ''')

    # >>> create_module(sample4 = '''\
    # ... import sample3
    # ...
    # ... def getone():
    # ...     return sample3.one
    # ...
    # ... ''')

Here, we have a circular import between sample3 and sample4.  When
sample3 is imported, it imports sample 4, which then imports sample3.
Let's see what happens when we use these modules in an unfortunate
order:

.. code-block:: python

   # XXX: Relative imports like this are not possible on Python 3 anymore.
   # PY2
   #
   #    >>> import zope.deferredimport.sample3
   #    >>> import zope.deferredimport.sample4
   #
   #    >>> zope.deferredimport.sample4.getone()
   #    Traceback (most recent call last):
   #    ...
   #    AttributeError: 'module' object has no attribute 'one'
   #
   #Hm.  Let's try accessing one through sample3:
   #
   #    >>> zope.deferredimport.sample3.one
   #    1
   #
   #Funny, let's try getone again:
   #
   #    >>> zope.deferredimport.sample4.getone()
   #    1

The problem is that sample4 obtained sample3 before sample4 was
replaced by a proxy.  This example is slightly pathological because it
requires a circular import and a relative import, but the bug
introduced is very subtle.  To guard against this, you should define
deferred imports before importing any other modules.  Alternatively,
you can call the initialize function before importing any other
modules, as in:

.. doctest::

    >>> create_module(sample5 = '''\
    ... import zope.deferredimport
    ...
    ...
    ... zope.deferredimport.initialize()
    ...
    ... import zope.deferredimport.sample6  # noqa: E402 import not at top
    ...
    ...
    ... zope.deferredimport.define(
    ...     sample1='zope.deferredimport.sample1',
    ...     one='zope.deferredimport.sample1:x',
    ...     two='zope.deferredimport.sample1:C.y',
    ... )
    ...
    ... x = 1
    ... ''')

    >>> create_module(sample6 = '''\
    ... import zope.deferredimport.sample5
    ...
    ...
    ... def getone():
    ...     return zope.deferredimport.sample5.one
    ... ''')

    >>> import zope.deferredimport.sample5
    >>> import zope.deferredimport.sample6

    >>> zope.deferredimport.sample6.getone()
    1


Deprecation
===========

Deferred attributes can also be marked as deprecated, in which case, a
message will be printed the first time they are accessed.

Lets define a module that has deprecated attributes defined as
deferred imports:

.. doctest::

    >>> create_module(sample7 = '''\
    ... import zope.deferredimport
    ...
    ...
    ... zope.deferredimport.initialize()
    ...
    ... zope.deferredimport.deprecated(
    ...     "Import from sample1 instead",
    ...     x='zope.deferredimport.sample1:x',
    ...     y='zope.deferredimport.sample1:C.y',
    ...     z='zope.deferredimport.sample1:z',
    ... )
    ... ''')

Now, if we use one of these variables, we'll get a deprecation
warning:

.. doctest::

    >>> import zope.deferredimport.sample7
    >>> zope.deferredimport.sample7.x # doctest: +NORMALIZE_WHITESPACE
    docs/narrative.rst:1: DeprecationWarning:
                x is deprecated. Import from sample1 instead
      ==================
    1

but only the first time:

.. doctest::

    >>> zope.deferredimport.sample7.x
    1

Importing multiple names from the same module
=============================================

Sometimes, you want to get multiple things from the same module.  You
can use :func:`~.defineFrom` or :func:`~.deprecatedFrom` to do that:


.. doctest::

    >>> create_module(sample8 = '''\
    ... import zope.deferredimport
    ...
    ...
    ... zope.deferredimport.deprecatedFrom(
    ...     "Import from sample1 instead",
    ...     'zope.deferredimport.sample1',
    ...     'x', 'z', 'q',
    ... )
    ...
    ... zope.deferredimport.defineFrom(
    ...     'zope.deferredimport.sample9',
    ...     'a', 'b', 'c',
    ... )
    ... ''')

    >>> create_module(sample9 = '''\
    ... print('Imported sample 9')
    ... a, b, c = range(10, 13)
    ... ''')

    >>> import zope.deferredimport.sample8
    >>> zope.deferredimport.sample8.q #doctest: +NORMALIZE_WHITESPACE
    docs/narrative.rst:1: DeprecationWarning:
            q is deprecated. Import from sample1 instead
      ==================
    4

    >>> zope.deferredimport.sample8.c
    Imported sample 9
    12

Note, as in the example above, that you can make multiple
deferred-import calls in a module.

.. testcleanup::

    sys.stderr = oldstderr
    warnings.warn = oldwarn
    zope.deferredimport.__path__.pop()
    for name in created_modules:
        sys.modules.pop(name, None)