This file is indexed.

/usr/lib/python3/dist-packages/postgresql/types/__init__.py is in python3-postgresql 1.1.0-1build1.

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
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
##
# types. - Package for I/O and PostgreSQL specific types.
##
"""
PostgreSQL types and identifiers.
"""
# XXX: Would be nicer to generate these from a header file...
InvalidOid = 0

RECORDOID = 2249
BOOLOID = 16
BITOID = 1560
VARBITOID = 1562
ACLITEMOID = 1033

CHAROID = 18
NAMEOID = 19
TEXTOID = 25
BYTEAOID = 17
BPCHAROID = 1042
VARCHAROID = 1043
CSTRINGOID = 2275
UNKNOWNOID = 705
REFCURSOROID = 1790
UUIDOID = 2950

TSVECTOROID = 3614
GTSVECTOROID = 3642
TSQUERYOID = 3615
REGCONFIGOID = 3734
REGDICTIONARYOID = 3769

JSONOID = 114
XMLOID = 142

MACADDROID = 829
INETOID = 869
CIDROID = 650

TYPEOID = 71
PROCOID = 81
CLASSOID = 83
ATTRIBUTEOID = 75

DATEOID = 1082
TIMEOID = 1083
TIMESTAMPOID = 1114
TIMESTAMPTZOID = 1184
INTERVALOID = 1186
TIMETZOID = 1266
ABSTIMEOID = 702
RELTIMEOID = 703
TINTERVALOID = 704

INT8OID = 20
INT2OID = 21
INT4OID = 23
OIDOID = 26
TIDOID = 27
XIDOID = 28
CIDOID = 29
CASHOID = 790
FLOAT4OID = 700
FLOAT8OID = 701
NUMERICOID = 1700

POINTOID = 600
LINEOID = 628
LSEGOID = 601
PATHOID = 602
BOXOID = 603
POLYGONOID = 604
CIRCLEOID = 718

OIDVECTOROID = 30
INT2VECTOROID = 22
INT4ARRAYOID = 1007

REGPROCOID = 24
REGPROCEDUREOID = 2202
REGOPEROID = 2203
REGOPERATOROID = 2204
REGCLASSOID = 2205
REGTYPEOID = 2206
REGTYPEARRAYOID = 2211

TRIGGEROID = 2279
LANGUAGE_HANDLEROID = 2280
INTERNALOID = 2281
OPAQUEOID = 2282
VOIDOID = 2278
ANYARRAYOID = 2277
ANYELEMENTOID = 2283
ANYOID = 2276
ANYNONARRAYOID = 2776
ANYENUMOID = 3500

#: Mapping of type Oid to SQL type name.
oid_to_sql_name = {
	BPCHAROID : 'CHARACTER',
	VARCHAROID : 'CHARACTER VARYING',
	# *OID : 'CHARACTER LARGE OBJECT',

	# SELECT X'0F' -> bit. XXX: Does bytea have any play here?
	#BITOID : 'BINARY',
	#BYTEAOID : 'BINARY VARYING',
	# *OID : 'BINARY LARGE OBJECT',

	BOOLOID : 'BOOLEAN',

# exact numeric types
	INT2OID : 'SMALLINT',
	INT4OID : 'INTEGER',
	INT8OID : 'BIGINT',
	NUMERICOID : 'NUMERIC',

# approximate numeric types
	FLOAT4OID : 'REAL',
	FLOAT8OID : 'DOUBLE PRECISION',

# datetime types
	TIMEOID : 'TIME WITHOUT TIME ZONE',
	TIMETZOID : 'TIME WITH TIME ZONE',
	TIMESTAMPOID : 'TIMESTAMP WITHOUT TIME ZONE',
	TIMESTAMPTZOID : 'TIMESTAMP WITH TIME ZONE',
	DATEOID : 'DATE',

# interval types
	INTERVALOID : 'INTERVAL',

	XMLOID : 'XML',
}

#: Mapping of type Oid to name.
oid_to_name = {
	RECORDOID : 'record',
	BOOLOID : 'bool',
	BITOID : 'bit',
	VARBITOID : 'varbit',
	ACLITEMOID : 'aclitem',

	CHAROID : 'char',
	NAMEOID : 'name',
	TEXTOID : 'text',
	BYTEAOID : 'bytea',
	BPCHAROID : 'bpchar',
	VARCHAROID : 'varchar',
	CSTRINGOID : 'cstring',
	UNKNOWNOID : 'unknown',
	REFCURSOROID : 'refcursor',
	UUIDOID : 'uuid',

	TSVECTOROID : 'tsvector',
	GTSVECTOROID : 'gtsvector',
	TSQUERYOID : 'tsquery',
	REGCONFIGOID : 'regconfig',
	REGDICTIONARYOID : 'regdictionary',

	XMLOID : 'xml',

	MACADDROID : 'macaddr',
	INETOID : 'inet',
	CIDROID : 'cidr',

	TYPEOID : 'type',
	PROCOID : 'proc',
	CLASSOID : 'class',
	ATTRIBUTEOID : 'attribute',

	DATEOID : 'date',
	TIMEOID : 'time',
	TIMESTAMPOID : 'timestamp',
	TIMESTAMPTZOID : 'timestamptz',
	INTERVALOID : 'interval',
	TIMETZOID : 'timetz',
	ABSTIMEOID : 'abstime',
	RELTIMEOID : 'reltime',
	TINTERVALOID : 'tinterval',

	INT8OID : 'int8',
	INT2OID : 'int2',
	INT4OID : 'int4',
	OIDOID : 'oid',
	TIDOID : 'tid',
	XIDOID : 'xid',
	CIDOID : 'cid',
	CASHOID : 'cash',
	FLOAT4OID : 'float4',
	FLOAT8OID : 'float8',
	NUMERICOID : 'numeric',

	POINTOID : 'point',
	LINEOID : 'line',
	LSEGOID : 'lseg',
	PATHOID : 'path',
	BOXOID : 'box',
	POLYGONOID : 'polygon',
	CIRCLEOID : 'circle',

	OIDVECTOROID : 'oidvector',
	INT2VECTOROID : 'int2vector',
	INT4ARRAYOID : 'int4array',

	REGPROCOID : 'regproc',
	REGPROCEDUREOID : 'regprocedure',
	REGOPEROID : 'regoper',
	REGOPERATOROID : 'regoperator',
	REGCLASSOID : 'regclass',
	REGTYPEOID : 'regtype',
	REGTYPEARRAYOID : 'regtypearray',

	TRIGGEROID : 'trigger',
	LANGUAGE_HANDLEROID : 'language_handler',
	INTERNALOID : 'internal',
	OPAQUEOID : 'opaque',
	VOIDOID : 'void',
	ANYARRAYOID : 'anyarray',
	ANYELEMENTOID : 'anyelement',
	ANYOID : 'any',
	ANYNONARRAYOID : 'anynonarray',
	ANYENUMOID : 'anyenum',
}

name_to_oid = dict(
	[(v,k) for k,v in oid_to_name.items()]
)

class Array(object):
	"""
	Type used to mimic PostgreSQL arrays. While there are many semantic
	differences, the primary one is that the elements contained by an Array
	instance are not strongly typed. The purpose of this class is to provide
	some consistency with PostgreSQL with respect to the structure of an Array.

	The structure consists of three parts:

	 * The elements of the array.
	 * The lower boundaries.
	 * The upper boundaries.

	There is also a `dimensions` property, but it is derived from the
	`lowerbounds` and `upperbounds` to yield a normalized description of the
	ARRAY's structure.

	The Python interfaces, such as __getitem__, are *not* subjected to the
	semantics of the lower and upper bounds. Rather, the normalized dimensions
	provide the primary influence for these interfaces. So, unlike SQL
	indirection, getting an index that does *not* exist will raise a Python
	`IndexError`.
	"""
	# return an iterator over the absolute elements of a nested sequence
	@classmethod
	def unroll_nest(typ, hier, dimensions, depth = 0):
		dsize = dimensions and dimensions[depth] or 0
		if len(hier) != dsize:
			raise ValueError("list size not consistent with dimensions at depth " + str(depth))
		r = []
		ndepth = depth + 1
		if ndepth == len(dimensions):
			# at the bottom
			r = hier
		else:
			# go deeper
			for x in hier:
				r.extend(typ.unroll_nest(x, dimensions, ndepth))
		return r

	# Detect the dimensions of a nested sequence
	@staticmethod
	def detect_dimensions(hier, len = len):
		# if the list is empty, it's a zero-dimension array.
		if hier:
			yield len(hier)
			hier = hier[0]
			depth = 1
			while hier.__class__ is list:
				depth += 1
				l = len(hier)
				if l < 1:
					raise ValueError("axis {0} is empty".format(depth))
				yield l
				hier = hier[0]

	@classmethod
	def from_elements(typ,
		elements : "iterable of elements in the array",
		lowerbounds : "beginning of each axis" = None,
		upperbounds : "upper bounds; size of each axis" = None,
		len = len,
	):
		"""
		Instantiate an Array from the given elements, lowerbounds, and upperbounds.

		The given elements are bound to the array which provides them with the
		structure defined by the lower boundaries and the upper boundaries.

		A `ValueError` will be raised in the following situations:

		 * The number of elements given are inconsistent with the number of elements
		   described by the upper and lower bounds.
		 * The lower bounds at a given axis exceeds the upper bounds at a given
		   axis.
		 * The number of lower bounds is inconsistent with the number of upper
		   bounds.
		"""
		# resolve iterable
		elements = list(elements)
		nelements = len(elements)

		# If ndims is zero, lowerbounds will be ()
		if lowerbounds is None:
			if upperbounds:
				lowerbounds = (1,) * len(upperbounds)
			elif nelements == 0:
				# special for empty ARRAY; no dimensions.
				lowerbounds = ()
			else:
				# one dimension.
				lowerbounds = (1,)
		else:
			lowerbounds = tuple(lowerbounds)

		if upperbounds is not None:
			upperbounds = tuple(upperbounds)
			dimensions = []
			# upperbounds were given, so check.
			if upperbounds:
				elcount = 1
				for lb, ub in zip(lowerbounds, upperbounds):
					x = ub - lb + 1
					if x < 1:
						# special case empty ARRAYs
						if nelements == 0:
							upperbounds = ()
							lowerbounds = ()
							dimensions = ()
							elcount = 0
							break
						raise ValueError("lowerbounds exceeds upperbounds")
					# physical dimensions.
					dimensions.append(x)
					elcount = x * elcount
			else:
				elcount = 0
			if nelements != elcount:
				raise ValueError("element count inconsistent with boundaries")
			dimensions = tuple(dimensions)
		else:
			# fill in default
			if nelements == 0:
				upperbounds = ()
				dimensions = ()
			else:
				upperbounds = (nelements,)
				dimensions = (nelements,)

		# consistency..
		if len(lowerbounds) != len(upperbounds):
			raise ValueError("number of lowerbounds inconsistent with upperbounds")

		rob = super().__new__(typ)
		rob._elements = elements
		rob.lowerbounds = lowerbounds
		rob.upperbounds = upperbounds
		rob.dimensions = dimensions
		rob.ndims = len(dimensions)
		rob._weight = len(rob._elements) // (dimensions and dimensions[0] or 1)
		return rob

	# Method used to create an Array() from nested lists.
	@classmethod
	def from_nest(typ, nest):
		dims = tuple(typ.detect_dimensions(nest))
		return typ.from_elements(
			list(typ.unroll_nest(nest, dims)),
			upperbounds = dims,
			# lowerbounds is implied to (1,)*len(upper)
		)

	def __new__(typ, nested_elements):
		"""
		Create an types.Array() using the given nested lists. The boundaries of
		the array are detected by traversing the first items of the nested
		lists::

			Array([[1,2,4],[3,4,8]])

		Lists are used to define the boundaries so that tuples may be used to
		represent any complex elements. The above array will the `lowerbounds`
		``(1,1)``, and the `upperbounds` ``(2,3)``.
		"""
		if nested_elements.__class__ is Array:
			return nested_elements
		return typ.from_nest(list(nested_elements))

	def __getnewargs__(self):
		return (self.nest(),)

	def elements(self):
		"""
		Returns an iterator to the elements of the Array. The elements are
		produced in physical order.
		"""
		return iter(self._elements)

	def nest(self, seqtype = list):
		"""
		Transform the array into a nested list.

		The `seqtype` keyword can be used to override the type used to represent
		the elements of a given axis.
		"""
		if self.ndims < 2:
			return seqtype(self._elements)
		else:
			rl = []
			for x in self:
				rl.append(x.nest(seqtype = seqtype))
			return seqtype(rl)

	def get_element(self, address,
		idxerr = "index {0} at axis {1} is out of range {2}".format
	):
		"""
		Get an element in the array using the given axis sequence.

		>>> a=Array([[1,2],[3,4]])
		>>> a.get_element((0,0)) == 1
		True
		>>> a.get_element((1,1)) == 4
		True

		This is similar to getting items in a nested list::

		>>> l=[[1,2],[3,4]]
		>>> l[0][0] == 1
		True
		"""
		if not self.dimensions:
			raise IndexError("array is empty")
		if len(address) != len(self.dimensions):
			raise ValueError("given axis sequence is inconsistent with number of dimensions")

		# normalize axis specification (-N + DIM), check for IndexErrors, and
		# resolve the element's position.
		cur = 0
		nelements = len(self._elements)
		for n, a, dim in zip(range(len(address)), address, self.dimensions):
			if a < 0:
				a = a + dim
				if a < 0:
					raise IndexError(idxerr(a, n, dim))
			else:
				if a >= dim:
					raise IndexError(idxerr(a, n, dim))
			nelements = nelements // dim
			cur += (a * nelements)
		return self._elements[cur]

	def sql_get_element(self, address):
		"""
		Like `get_element`, but with SQL indirection semantics. Notably, returns
		`None` on IndexError.
		"""
		try:
			a = [a - lb for (a, lb) in zip(address, self.lowerbounds)]
			# get_element accepts negatives, so check the converted sequence.
			for x in a:
				if x < 0:
					return None
			return self.get_element(a)
		except IndexError:
			return None

	def __repr__(self):
		return '%s.%s(%r)' %(
			type(self).__module__,
			type(self).__name__,
			self.nest()
		)

	def __len__(self):
		return self.dimensions and self.dimensions[0] or 0

	def __eq__(self, ob):
		return list(self) == ob

	def __ne__(self, ob):
		return list(self) != ob

	def __gt__(self, ob):
		return list(self) > ob

	def __lt__(self, ob):
		return list(self) < ob

	def __le__(self, ob):
		return list(self) <= ob

	def __ge__(self, ob):
		return list(self) >= ob

	def __getitem__(self, item):
		if self.ndims < 2:
			# Array with 1dim is more or less a list.
			return self._elements[item]
		if isinstance(item, slice):
			# get a sub-array slice
			l = len(self)
			n = 0
			r = []
			# for each offset in the slice, get the elements and add them
			# to the new elements list used to build the new Array().
			for x in range(*(item.indices(l))):
				n = n + 1
				r.extend(
					self._elements[slice(self._weight*x,self._weight*(x+1))]
				)
			if n:
				return self.__class__.from_elements(r,
					lowerbounds = (1,) + self.lowerbounds[1:],
					upperbounds = (n,) + self.upperbounds[1:],
				)
			else:
				# Empty
				return self.__class__.from_elements(())
		else:
			# get a sub-array
			l = len(self)
			if item > l:
				raise IndexError("index {0} is out of range".format(l))
			return self.__class__.from_elements(
				self._elements[self._weight*item:self._weight*(item+1)],
				lowerbounds = self.lowerbounds[1:],
				upperbounds = self.upperbounds[1:],
			)

	def __iter__(self):
		if self.ndims < 2:
			# Special case empty and single dimensional ARRAYs
			return self.elements()
		return (self[x] for x in range(len(self)))

from operator import itemgetter
get0 = itemgetter(0)
get1 = itemgetter(1)
del itemgetter

class Row(tuple):
	"Name addressable items tuple; mapping and sequence"
	@classmethod
	def from_mapping(typ, keymap, map, get1 = get1):
		iter = [
			map.get(k) for k,_ in sorted(keymap.items(), key = get1)
		]
		r = typ(iter)
		r.keymap = keymap
		return r

	@classmethod
	def from_sequence(typ, keymap, seq):
		r = typ(seq)
		r.keymap = keymap
		return r

	def __getitem__(self, i, gi = tuple.__getitem__):
		if isinstance(i, (int, slice)):
			return gi(self, i)
		idx = self.keymap[i]
		return gi(self, idx)

	def get(self, i, gi = tuple.__getitem__, len = len):
		if type(i) is int:
			l = len(self)
			if -l < i < l:
				return gi(self, i)
		else:
			idx = self.keymap.get(i)
			if idx is not None:
				return gi(self, idx)
		return None

	def keys(self):
		return self.keymap.keys()

	def values(self):
		return iter(self)

	def items(self):
		return zip(iter(self.column_names), iter(self))

	def index_from_key(self, key):
		return self.keymap.get(key)

	def key_from_index(self, index):
		for k,v in self.keymap.items():
			if v == index:
				return k
		return None

	@property
	def column_names(self, get0 = get0, get1 = get1):
		l=list(self.keymap.items())
		l.sort(key=get1)
		return tuple(map(get0, l))

	def transform(self, *args, **kw):
		"""
		Make a new Row after processing the values with the callables associated
		with the values either by index, \*args, or my column name, \*\*kw.

			>>> r=Row.from_sequence({'col1':0,'col2':1}, (1,'two'))
			>>> r.transform(str)
			('1','two')
			>>> r.transform(col2 = str.upper)
			(1,'TWO')
			>>> r.transform(str, col2 = str.upper)
			('1','TWO')

		Combine with methodcaller and map to transform lots of rows:

			>>> rowseq = [r]
			>>> xf = operator.methodcaller('transform', col2 = str.upper)
			>>> list(map(xf, rowseq))
			[(1,'TWO')]

		"""
		r = list(self)
		i = 0
		for x in args:
			if x is not None:
				r[i] = x(tuple.__getitem__(self, i))
			i = i + 1
		for k,v in kw.items():
			if v is not None:
				i = self.index_from_key(k)
				if i is None:
					raise KeyError("row has no such key, " + repr(k))
				r[i] = v(self[k])
		return type(self).from_sequence(self.keymap, r)