chiark / gitweb /
@@@ mLib-python Pyke wip
[mLib-python] / mLib / __init__.py
1 ### -*-python-*-
2 ###
3 ### Setup for mLib bindings
4 ###
5 ### (c) 2019 Straylight/Edgeware
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of the Python interface to mLib.
11 ###
12 ### mLib/Python is free software: you can redistribute it and/or modify it
13 ### under the terms of the GNU General Public License as published by the
14 ### Free Software Foundation; either version 2 of the License, or (at your
15 ### option) any later version.
16 ###
17 ### mLib/Python is distributed in the hope that it will be useful, but
18 ### WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ### General Public License for more details.
21 ###
22 ### You should have received a copy of the GNU General Public License
23 ### along with mLib/Python.  If not, write to the Free Software
24 ### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25 ### USA.
26
27 import sys as _sys
28 import types as _types
29
30 ###--------------------------------------------------------------------------
31 ### Import the main C extension module.
32
33 if _sys.version_info >= (3,): from . import _base
34 else: import _base
35
36 ###--------------------------------------------------------------------------
37 ### Basic stuff.
38
39 ## Register our module.
40 _base._ready(_sys.modules[__name__])
41 def default_lostexchook(why, ty, val, tb):
42   """`mLib.lostexchook(WHY, TY, VAL, TB)' reports lost exceptions."""
43   _sys.stderr.write("\n\n!!! LOST EXCEPTION: %s\n" % why)
44   _sys.excepthook(ty, val, tb)
45   _sys.stderr.write("\n")
46 lostexchook = default_lostexchook
47
48 ## For the benefit of the reporter functions, we need the program name.
49 _base.ego(_sys.argv[0])
50
51 ## Initialize the module.
52 def _init():
53   d = globals()
54   b = _base.__dict__;
55   for i in b:
56     if i[0] != '_': d[i] = b[i];
57 _init()
58
59 ## A handy function for our work: add the methods of a named class to an
60 ## existing class.  This is how we write the Python-implemented parts of our
61 ## mostly-C types.
62 def _augment(c, cc):
63   for i in cc.__dict__:
64     a = cc.__dict__[i]
65     if type(a) is _types.MethodType:
66       a = a.im_func
67     elif type(a) not in (_types.FunctionType, staticmethod, classmethod):
68       continue
69     setattr(c, i, a)
70
71 ###--------------------------------------------------------------------------
72 ### Atoms.
73
74 DEFAULT_ATOMTABLE = AtomTable()
75
76 if _sys.version_info >= (3,):
77   def atoms(): return iter(DEFAULT_ATOMTABLE.values())
78 else:
79   def atoms(): return DEFAULT_ATOMTABLE.itervalues()
80
81 class _tmp:
82   def __repr__(me): return "Atom(%r)" % me.name
83   __str__ = __repr__
84 _augment(Atom, _tmp)
85
86 ###--------------------------------------------------------------------------
87 ### User-interface funtions.
88
89 def pquis(msg, file = _sys.stdout):
90   "pquis(MSG, [file = sys.stdout]): write MSG, replacing `$' by program name"
91   file.write(msg.replace("$", quis))
92
93 ###----- That's all, folks --------------------------------------------------