Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

r""" 

Dynamic documentation for instances of classes 

  

The functionality in this module allows to define specific docstrings 

of *instances* of a class, which are different from the class docstring. 

A typical use case is given by cached methods: the documentation of a 

cached method should not be the documentation of the class 

:class:`CachedMethod`; it should be the documentation of the underlying 

method. 

  

In order to use this, define a class docstring as usual. Also define a 

method ``def _instancedoc_(self)`` which should return the docstring of 

the instance ``self``. Finally, add the decorator ``@instancedoc`` to 

the class. 

  

.. WARNING:: 

  

Since the ``__doc__`` attribute is never inherited, the decorator 

``@instancedoc`` must be added to all subclasses of the class 

defining ``_instancedoc_``. Doing it on the base class is not 

sufficient. 

  

EXAMPLES:: 

  

sage: from sage.docs.instancedoc import instancedoc 

sage: @instancedoc 

....: class X(object): 

....: "Class docstring" 

....: def _instancedoc_(self): 

....: return "Instance docstring" 

sage: X.__doc__ 

'Class docstring' 

sage: X().__doc__ 

'Instance docstring' 

  

For a Cython ``cdef class``, a decorator cannot be used. Instead, call 

:func:`instancedoc` as a function after defining the class:: 

  

sage: cython(''' 

....: from sage.docs.instancedoc import instancedoc 

....: cdef class Y: 

....: "Class docstring" 

....: def _instancedoc_(self): 

....: return "Instance docstring" 

....: instancedoc(Y) 

....: ''') 

sage: Y.__doc__ 

'File:...\nClass docstring' 

sage: Y().__doc__ 

'Instance docstring' 

  

One can still add a custom ``__doc__`` attribute on a particular 

instance:: 

  

sage: obj = X() 

sage: obj.__doc__ = "Very special doc" 

sage: print(obj.__doc__) 

Very special doc 

  

This normally does not work on extension types:: 

  

sage: Y().__doc__ = "Very special doc" 

Traceback (most recent call last): 

... 

AttributeError: attribute '__doc__' of 'Y' objects is not writable 

  

This is an example involving a metaclass, where the instances are 

classes. In this case, the ``_instancedoc_`` from the metaclass is only 

used if the instance of the metaclass (the class) does not have a 

docstring:: 

  

sage: @instancedoc 

....: class Meta(type): 

....: "Metaclass doc" 

....: def _instancedoc_(self): 

....: return "Docstring for {}".format(self) 

sage: from six import with_metaclass 

sage: class T(with_metaclass(Meta, object)): 

....: pass 

sage: print(T.__doc__) 

Docstring for <class '__main__.T'> 

sage: class U(with_metaclass(Meta, object)): 

....: "Special doc for U" 

sage: print(U.__doc__) 

Special doc for U 

  

TESTS: 

  

Check that inheritance works (after passing the subclass to 

:func:`instancedoc`):: 

  

sage: @instancedoc 

....: class A(object): 

....: "Class A docstring" 

....: def _instancedoc_(self): 

....: return "Instance docstring" 

sage: class B(A): 

....: "Class B docstring" 

sage: B.__doc__ 

'Class B docstring' 

sage: B().__doc__ # Ideally, this would return the instance docstring 

'Class B docstring' 

sage: B = instancedoc(B) 

sage: B.__doc__ 

'Class B docstring' 

sage: B().__doc__ 

'Instance docstring' 

""" 

  

#***************************************************************************** 

# Copyright (C) 2017 Jeroen Demeyer <J.Demeyer@UGent.be> 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 2 of the License, or 

# (at your option) any later version. 

# http://www.gnu.org/licenses/ 

#***************************************************************************** 

  

from cpython.object cimport PyObject, PyTypeObject 

  

cdef extern from *: 

cdef int PyDict_SetItemString(PyObject*, const char*, object) except -1 

cdef void PyType_Modified(PyTypeObject*) 

  

cdef inline PyTypeObject* TypeObject(cls) except NULL: 

if not isinstance(cls, type): 

raise TypeError(f"expected type, got {cls!r}") 

return <PyTypeObject*>cls 

  

  

cdef class InstanceDocDescriptor: 

""" 

Descriptor for dynamic documentation, to be installed as the 

``__doc__`` attribute. 

  

INPUT: 

  

- ``classdoc`` -- (string) class documentation 

  

- ``instancedoc`` -- (method) documentation for an instance 

  

- ``attr`` -- (string, default ``__doc__``) attribute name to use 

for custom docstring on the instance. 

  

EXAMPLES:: 

  

sage: from sage.docs.instancedoc import InstanceDocDescriptor 

sage: def instancedoc(self): 

....: return "Instance doc" 

sage: docattr = InstanceDocDescriptor("Class doc", instancedoc) 

sage: class Z(object): 

....: __doc__ = InstanceDocDescriptor("Class doc", instancedoc) 

sage: Z.__doc__ 

'Class doc' 

sage: Z().__doc__ 

'Instance doc' 

  

We can still override the ``__doc__`` attribute of the instance:: 

  

sage: obj = Z() 

sage: obj.__doc__ = "Custom doc" 

sage: obj.__doc__ 

'Custom doc' 

sage: del obj.__doc__ 

sage: obj.__doc__ 

'Instance doc' 

""" 

cdef classdoc 

cdef instancedoc 

cdef attr 

  

def __init__(self, classdoc, instancedoc, attr="__doc__"): 

""" 

TESTS:: 

  

sage: from sage.docs.instancedoc import InstanceDocDescriptor 

sage: InstanceDocDescriptor(None, None) 

<sage.docs.instancedoc.InstanceDocDescriptor object at ...> 

""" 

self.classdoc = classdoc 

self.instancedoc = instancedoc 

self.attr = intern(attr) 

  

def __get__(self, obj, typ): 

""" 

TESTS:: 

  

sage: from sage.docs.instancedoc import InstanceDocDescriptor 

sage: def instancedoc(self): 

....: return "Doc for {!r}".format(self) 

sage: descr = InstanceDocDescriptor("Class doc", instancedoc) 

sage: descr.__get__(None, object) 

'Class doc' 

sage: descr.__get__(42, type(42)) 

'Doc for 42' 

""" 

if obj is None: 

return self.classdoc 

  

# First, try the attribute self.attr (typically __doc__) 

# on the instance 

try: 

objdict = obj.__dict__ 

except AttributeError: 

pass 

else: 

doc = objdict.get(self.attr) 

if doc is not None: 

return doc 

  

return self.instancedoc(obj) 

  

def __set__(self, obj, value): 

""" 

TESTS:: 

  

sage: from sage.docs.instancedoc import InstanceDocDescriptor 

sage: def instancedoc(self): 

....: return "Doc for {!r}".format(self) 

sage: descr = InstanceDocDescriptor("Class doc", instancedoc) 

sage: class X(object): pass 

sage: obj = X() 

sage: descr.__set__(obj, "Custom doc") 

sage: obj.__doc__ 

'Custom doc' 

  

sage: descr.__set__([], "Custom doc") 

Traceback (most recent call last): 

... 

AttributeError: attribute '__doc__' of 'list' objects is not writable 

sage: descr.__set__(object, "Custom doc") 

Traceback (most recent call last): 

... 

AttributeError: attribute '__doc__' of 'type' objects is not writable 

""" 

try: 

obj.__dict__[self.attr] = value 

except (AttributeError, TypeError): 

raise AttributeError(f"attribute '{self.attr}' of '{type(obj).__name__}' objects is not writable") 

  

def __delete__(self, obj): 

""" 

TESTS:: 

  

sage: from sage.docs.instancedoc import InstanceDocDescriptor 

sage: def instancedoc(self): 

....: return "Doc for {!r}".format(self) 

sage: descr = InstanceDocDescriptor("Class doc", instancedoc) 

sage: class X(object): pass 

sage: obj = X() 

sage: obj.__doc__ = "Custom doc" 

sage: descr.__delete__(obj) 

sage: print(obj.__doc__) 

None 

sage: descr.__delete__(obj) 

Traceback (most recent call last): 

... 

AttributeError: 'X' object has no attribute '__doc__' 

  

sage: descr.__delete__([]) 

Traceback (most recent call last): 

... 

AttributeError: attribute '__doc__' of 'list' objects is not writable 

sage: descr.__delete__(object) 

Traceback (most recent call last): 

... 

AttributeError: attribute '__doc__' of 'type' objects is not writable 

""" 

try: 

del obj.__dict__[self.attr] 

except (AttributeError, TypeError): 

raise AttributeError(f"attribute '{self.attr}' of '{type(obj).__name__}' objects is not writable") 

except KeyError: 

raise AttributeError(f"'{type(obj).__name__}' object has no attribute '{self.attr}'") 

  

  

def instancedoc(cls): 

""" 

Add support for ``_instancedoc_`` to the class ``cls``. 

  

Typically, this will be used as decorator. 

  

INPUT: 

  

- ``cls`` -- a new-style class 

  

OUTPUT: ``cls`` 

  

.. WARNING:: 

  

``instancedoc`` mutates the given class. So you are *not* supposed 

to use it as ``newcls = instancedoc(cls)`` because that would 

mutate ``cls`` (and ``newcls`` would be the same object as ``cls``) 

  

TESTS: 

  

We get a useful error message if ``_instancedoc_`` is not defined:: 

  

sage: from sage.docs.instancedoc import instancedoc 

sage: class X(object): pass 

sage: instancedoc(X) 

Traceback (most recent call last): 

... 

TypeError: instancedoc requires <class '__main__.X'> to have an '_instancedoc_' attribute 

  

This does not work on old-style classes or things which are not a 

class at all:: 

  

sage: instancedoc(7) 

Traceback (most recent call last): 

... 

TypeError: expected type, got 7 

sage: class OldStyle: pass 

sage: instancedoc(OldStyle) 

Traceback (most recent call last): 

... 

TypeError: expected type, got <class __main__.OldStyle at ...> 

""" 

cdef PyTypeObject* tp = TypeObject(cls) 

try: 

instdoc = cls._instancedoc_ 

except AttributeError: 

raise TypeError(f"instancedoc requires {cls!r} to have an '_instancedoc_' attribute") 

docattr = InstanceDocDescriptor(cls.__doc__, instdoc) 

PyDict_SetItemString(tp.tp_dict, "__doc__", docattr) 

tp.tp_doc = NULL 

PyType_Modified(tp) 

return cls