This file is indexed.

/usr/share/pyshared/apptools/persistence/spickle.py is in python-apptools 4.1.0-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
"""A special unpickler that gives you a state object and a special
pickler that lets you re-pickle that state.

The nice thing about creating state objects is that it does not import
any modules and does not create any instances.  Instead of instances
it creates `State` instances which have the same attributes as the
real object.  With this you can load a pickle (without even having the
modules on the machine), modify it and re-pickle it back.

NOTE: This module is not likely to work for very complex pickles but
 it should work for most common cases.

"""
# Author: Prabhu Ramachandran <prabhu@aero.iitb.ac.in>
# Copyright (c) 2006-2007, Prabhu Ramachandran
# License: BSD Style.

import warnings
import pickle
import struct
from pickle import Pickler, Unpickler, dumps, BUILD, NEWOBJ, REDUCE, \
     MARK, OBJ, INST, BUILD, TupleType, PicklingError, GLOBAL, \
     EXT1, EXT2, EXT4, _extension_registry, _keep_alive

from cStringIO import StringIO


######################################################################
# `State` class
######################################################################
class State(dict):
    """Used to encapsulate the state of an instance in a very
    convenient form.  The '__METADATA__' attribute/key is a dictionary
    that has class specific details like the class name, module name
    etc.
    """
    def __init__(self, **kw):
        dict.__init__(self, **kw)
        self.__dict__ = self


######################################################################
# `StatePickler` class
######################################################################
class StatePickler(Pickler):
    """Pickles a `State` object back as a regular pickle that may be
    unpickled.
    """

    def __init__(self, file, protocol, bin=None):
        Pickler.__init__(self, file, protocol)
        self.bin = bin

    def save(self, obj):
        # Check the memo
        x = self.memo.get(id(obj))
        if x:
            self.write(self.get(x[0]))
            return

        if isinstance(obj, State):
            md = obj.__METADATA__
            typ = md['type']
            if typ == 'instance':
                self._state_instance(obj)
            elif typ in ['newobj', 'reduce']:
                self._state_reduce(obj)
            elif typ in ['class']:
                self._save_global(obj)
        else:
            Pickler.save(self, obj)

    def _state_instance(self, obj):
        md = obj.__METADATA__
        cls = md.get('class')
        cls_md = cls.__METADATA__

        memo  = self.memo
        write = self.write
        save  = self.save

        args = md.get('initargs')
        if len(args) > 0:
            _keep_alive(args, memo)

        write(MARK)

        if self.bin:
            save(cls)
            for arg in args:
                save(arg)
            write(OBJ)
        else:
            for arg in args:
                save(arg)
            write(INST + cls_md.get('module') + '\n' + cls_md.get('name') + '\n')

        self.memoize(obj)

        stuff = dict(obj.__dict__)
        stuff.pop('__METADATA__')

        if '__setstate_data__' in stuff:
            data = stuff.pop('__setstate_data__')
            _keep_alive(data, memo)
            save(data)
        else:
            save(stuff)
        write(BUILD)

    def _state_reduce(self, obj):
        # FIXME: this code is not as complete as pickle's reduce
        # handling code and is likely to not work in all cases.

        md = obj.__METADATA__
        func = md.get('class')
        func_md = func.__METADATA__
        args = md.get('initargs')
        state = dict(obj.__dict__)
        state.pop('__METADATA__')

        # This API is called by some subclasses

        # Assert that args is a tuple or None
        if not isinstance(args, TupleType):
            if args is None:
                # A hack for Jim Fulton's ExtensionClass, now deprecated.
                # See load_reduce()
                warnings.warn("__basicnew__ special case is deprecated",
                              DeprecationWarning)
            else:
                raise PicklingError(
                    "args from reduce() should be a tuple")

        # Assert that func is callable
        #if not callable(func):
        #    raise PicklingError("func from reduce should be callable")

        save = self.save
        write = self.write

        # Protocol 2 special case: if func's name is __newobj__, use NEWOBJ
        if self.proto >= 2 and func_md.get("name", "") == "__newobj__":
            # FIXME: this is unlikely to work.
            cls = args[0]
            if not hasattr(cls, "__new__"):
                raise PicklingError(
                    "args[0] from __newobj__ args has no __new__")
            if obj is not None and cls is not obj.__class__:
                raise PicklingError(
                    "args[0] from __newobj__ args has the wrong class")
            args = args[1:]
            save(cls)
            save(args)
            write(NEWOBJ)
        else:
            save(func)
            save(args)
            write(REDUCE)

        if obj is not None:
            self.memoize(obj)

        if state is not None:
            if '__setstate_data__' in state:
                data = state.pop('__setstate_data__')
                save(data)
            else:
                save(state)
            write(BUILD)

    def _save_global(self, obj, name=None, pack=struct.pack):
        write = self.write
        memo = self.memo

        md = obj.__METADATA__

        if name is None:
            name = md.get('name')

        module = md.get('module')

        if self.proto >= 2:
            code = _extension_registry.get((module, name))
            if code:
                assert code > 0
                if code <= 0xff:
                    write(EXT1 + chr(code))
                elif code <= 0xffff:
                    write("%c%c%c" % (EXT2, code&0xff, code>>8))
                else:
                    write(EXT4 + pack("<i", code))
                return

        write(GLOBAL + module + '\n' + name + '\n')
        self.memoize(obj)


######################################################################
# `StateUnpickler` class
######################################################################
class StateUnpickler(Unpickler):
    def _instantiate(self, klass, k):
        args = tuple(self.stack[k+1:])
        del self.stack[k:]
        metadata = {'initargs': args, 'class': klass, 'type': 'instance'}
        value = State(__METADATA__=metadata)
        self.append(value)

    def load_build(self):
        stack = self.stack
        state = stack.pop()
        # Inst here is a State object.
        inst = stack[-1]
        slotstate = None
        if isinstance(state, tuple) and len(state) == 2:
            state, slotstate = state
        if state:
            if isinstance(state, tuple):
                inst.__dict__['__setstate_data__'] = state
            else:
                inst.__dict__.update(state)
        if slotstate:
            for k, v in slotstate.items():
                setattr(inst, k, v)
    Unpickler.dispatch[BUILD] = load_build

    def load_newobj(self):
        args = self.stack.pop()
        cls = self.stack[-1]
        cls_md = cls.__METADATA__
        metadata = {'initargs': args, 'class': cls, 'type': 'newobj'}
        obj = State(__METADATA__ = metadata)
        #obj = cls.__new__(cls, *args)
        self.stack[-1] = obj
    Unpickler.dispatch[NEWOBJ] = load_newobj

    def load_reduce(self):
        stack = self.stack
        args = stack.pop()
        func = stack[-1]
        func_md = func.__METADATA__
        metadata = {'initargs': args, 'class': func, 'type': 'reduce'}
        value = State(__METADATA__ = metadata)
        stack[-1] = value
    Unpickler.dispatch[REDUCE] = load_reduce

    def load(self):
        # We overload the load_build method so update the dispatch dict.
        dispatch = self.dispatch
        dispatch[BUILD] = StateUnpickler.load_build
        dispatch[NEWOBJ] = StateUnpickler.load_newobj
        dispatch[REDUCE] = StateUnpickler.load_reduce
        # call the super class' method.
        ret = Unpickler.load(self)
        # Reset the Unpickler's dispatch
        dispatch[BUILD] = Unpickler.load_build
        dispatch[NEWOBJ] = Unpickler.load_newobj
        dispatch[REDUCE] = Unpickler.load_reduce
        return ret

    def find_class(self, module, name):
        metadata = {'module': module, 'name': name, 'type': 'class'}
        value = State(__METADATA__ = metadata)
        return value


######################################################################
# Utility functions.
######################################################################
def get_state(obj):
    """Return a State object given an object.  Useful for testing."""
    str = dumps(obj)
    return StateUnpickler(StringIO(str)).load()

def dump_state(state, file, protocol=None, bin=None):
    """Dump the state (potentially modified) to given file."""
    StatePickler(file, protocol, bin).dump(state)

def dumps_state(state, protocol=None, bin=None):
    """Dump the state (potentially modified) to a string and return
    the string."""
    file = StringIO()
    StatePickler(file, protocol, bin).dump(state)
    return file.getvalue()

def state2object(state):
    """Creates an object from a state."""
    s = dumps_state(state)
    return pickle.loads(s)

def load_state(file):
    """Loads the state from a file like object.  This does not import
    any modules."""
    return StateUnpickler(file).load()

def loads_state(string):
    """Loads the state from a string object.  This does not import any
    modules."""
    return StateUnpickler(StringIO(string)).load()