Issue #7042: Use a better mechanism for testing timers in test_signal.
[python.git] / Tools / framer / example.py
blob96f627858879948274500458bcc66dbc42a029aa
1 """Generate the skeleton for cStringIO as an example of framer."""
3 from framer.bases import Module, Type
4 from framer.member import member
6 class cStringIO(Module):
7 """A simple fast partial StringIO replacement.
9 This module provides a simple useful replacement for the StringIO
10 module that is written in C. It does not provide the full
11 generality of StringIO, but it provides enough for most
12 applications and is especially useful in conjunction with the
13 pickle module.
15 Usage:
17 from cStringIO import StringIO
19 an_output_stream = StringIO()
20 an_output_stream.write(some_stuff)
21 ...
22 value = an_output_stream.getvalue()
24 an_input_stream = StringIO(a_string)
25 spam = an_input_stream.readline()
26 spam = an_input_stream.read(5)
27 an_input_stream.seek(0) # OK, start over
28 spam = an_input_stream.read() # and read it all
29 """
31 __file__ = "cStringIO.c"
33 def StringIO(o):
34 """Return a StringIO-like stream for reading or writing"""
35 StringIO.pyarg = "|O"
37 class InputType(Type):
38 "Simple type for treating strings as input file streams"
40 abbrev = "input"
42 struct = """\
43 typedef struct {
44 PyObject_HEAD
45 char *buf;
46 int pos;
47 int size;
48 PyObject *pbuf;
49 } InputObject;
50 """
52 def flush(self):
53 """Does nothing"""
55 def getvalue(self):
56 """Get the string value.
58 If use_pos is specified and is a true value, then the
59 string returned will include only the text up to the
60 current file position.
61 """
63 def isatty(self):
64 """Always returns False"""
66 def read(self, s):
67 """Return s characters or the rest of the string."""
68 read.pyarg = "|i"
70 def readline(self):
71 """Read one line."""
73 def readlines(self, hint):
74 """Read all lines."""
75 readlines.pyarg = "|i"
77 def reset(self):
78 """Reset the file position to the beginning."""
80 def tell(self):
81 """Get the current position."""
83 def truncate(self, pos):
84 """Truncate the file at the current position."""
85 truncate.pyarg = "|i"
87 def seek(self, position, mode=0):
88 """Set the current position.
90 The optional mode argument can be 0 for absolute, 1 for relative,
91 and 2 for relative to EOF. The default is absolute.
92 """
93 seek.pyarg = "i|i"
95 def close(self):
96 pass
98 class OutputType(InputType):
99 "Simple type for output strings."
101 abbrev = "output"
103 struct = """\
104 typedef struct {
105 PyObject_HEAD
106 char *buf;
107 int pos;
108 int size;
109 int softspace;
110 } OutputObject;
113 softspace = member()
115 def close(self):
116 """Explicitly release resources."""
118 def write(self, s):
119 """Write a string to the file."""
120 # XXX Hack: writing None resets the buffer
122 def writelines(self, lines):
123 """Write each string in lines."""
126 cStringIO.gen()