Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e90a67

Browse files
committed
* pythonmain.c: -k option, usage message, more environment flags.
(the latter also in frozenmain.c) * ceval.c: global 'killprint' flag raises exception when printing an expression statement's value (useful for finding stray output) * timemodule.c: add asctime() and ctime(). Change julian date to 1-based origin (as intended and documented). * Removed unused DO_TIMES stuff from timemodule.c. Added 'epoch' and 'day0' globals (year where time.time() == 0 and day of the week the epoch started).
1 parent 5ef74b8 commit 9e90a67

5 files changed

Lines changed: 118 additions & 59 deletions

File tree

Modules/timemodule.c

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,6 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
7171
#include <time.h>
7272
#endif /* !unix */
7373

74-
/* XXX This is bogus -- times() is defined in posixmodule.c */
75-
#ifdef DO_TIMES
76-
#include <sys/times.h>
77-
#include <sys/param.h>
78-
#include <errno.h>
79-
#endif
80-
8174
#ifdef SYSV
8275
/* Access timezone stuff */
8376
#ifdef OLDTZ /* ANSI prepends underscore to these */
@@ -227,32 +220,6 @@ time_millitimer(self, args)
227220

228221
#endif /* DO_MILLI */
229222

230-
#ifdef DO_TIMES
231-
232-
static object *
233-
time_times(self, args)
234-
object *self;
235-
object *args;
236-
{
237-
struct tms t;
238-
clock_t c;
239-
if (!getnoarg(args))
240-
return NULL;
241-
errno = 0;
242-
c = times(&t);
243-
if (c == (clock_t) -1) {
244-
err_errno(IOError);
245-
return NULL;
246-
}
247-
return mkvalue("(dddd)",
248-
(double)t.tms_utime / HZ,
249-
(double)t.tms_stime / HZ,
250-
(double)t.tms_cutime / HZ,
251-
(double)t.tms_cstime / HZ);
252-
}
253-
254-
#endif
255-
256223

257224
static object *
258225
time_convert(when, function)
@@ -268,7 +235,7 @@ time_convert(when, function)
268235
p->tm_min,
269236
p->tm_sec,
270237
(p->tm_wday + 6) % 7, /* Want Monday == 0 */
271-
p->tm_yday,
238+
p->tm_yday + 1, /* Want January, 1 == 1 */
272239
p->tm_isdst);
273240
}
274241

@@ -294,6 +261,62 @@ time_localtime(self, args)
294261
return time_convert((time_t)when, localtime);
295262
}
296263

264+
static int
265+
gettmarg(args, p)
266+
object *args;
267+
struct tm *p;
268+
{
269+
if (!getargs(args, "(iiiiiiiii)",
270+
&p->tm_year,
271+
&p->tm_mon,
272+
&p->tm_mday,
273+
&p->tm_hour,
274+
&p->tm_min,
275+
&p->tm_sec,
276+
&p->tm_wday,
277+
&p->tm_yday,
278+
&p->tm_isdst))
279+
return 0;
280+
if (p->tm_year >= 1900)
281+
p->tm_year -= 1900;
282+
p->tm_mon--;
283+
p->tm_wday = (p->tm_wday + 1) % 7;
284+
p->tm_yday--;
285+
return 1;
286+
}
287+
288+
static object *
289+
time_asctime(self, args)
290+
object *self;
291+
object *args;
292+
{
293+
struct tm buf;
294+
char *p;
295+
if (!gettmarg(args, &buf))
296+
return NULL;
297+
p = asctime(&buf);
298+
if (p[24] == '\n')
299+
p[24] = '\0';
300+
return newstringobject(p);
301+
}
302+
303+
static object *
304+
time_ctime(self, args)
305+
object *self;
306+
object *args;
307+
{
308+
double dt;
309+
time_t tt;
310+
char *p;
311+
if (!getargs(args, "d", &dt))
312+
return NULL;
313+
tt = dt;
314+
p = ctime(&tt);
315+
if (p[24] == '\n')
316+
p[24] = '\0';
317+
return newstringobject(p);
318+
}
319+
297320
/* Some very old systems may not have mktime(). Comment it out then! */
298321

299322
static object *
@@ -302,20 +325,8 @@ time_mktime(self, args)
302325
object *args;
303326
{
304327
struct tm buf;
305-
if (!getargs(args, "(iiiiiiiii)",
306-
&buf.tm_year,
307-
&buf.tm_mon,
308-
&buf.tm_mday,
309-
&buf.tm_hour,
310-
&buf.tm_min,
311-
&buf.tm_sec,
312-
&buf.tm_wday,
313-
&buf.tm_yday,
314-
&buf.tm_isdst))
328+
if (!gettmarg(args, &buf))
315329
return NULL;
316-
if (buf.tm_year >= 1900)
317-
buf.tm_year -= 1900;
318-
buf.tm_mon--;
319330
return newintobject((long)mktime(&buf));
320331
}
321332

@@ -324,13 +335,12 @@ static struct methodlist time_methods[] = {
324335
{"millisleep", time_millisleep},
325336
{"millitimer", time_millitimer},
326337
#endif /* DO_MILLI */
327-
#ifdef DO_TIMES
328-
{"times", time_times},
329-
#endif
330338
{"sleep", time_sleep},
331339
{"time", time_time},
332340
{"gmtime", time_gmtime},
333341
{"localtime", time_localtime},
342+
{"asctime", time_asctime},
343+
{"ctime", time_ctime},
334344
{"mktime", time_mktime},
335345
{NULL, NULL} /* sentinel */
336346
};

Parser/listnode.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ listtree(n)
4040

4141
static int level, atbol;
4242

43-
void
43+
static void
4444
listnode(fp, n)
4545
FILE *fp;
4646
node *n;

Python/ceval.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4949
#define CHECKEXC 1 /* Double-check exception checking */
5050
#endif
5151

52+
/* Global option, may be set by main() */
53+
int killprint;
54+
5255

5356
/* Forward declarations */
5457

@@ -639,6 +642,11 @@ eval_code(co, globals, locals, owner, arg)
639642
softspace(x, 1);
640643
err = writeobject(v, x, 0);
641644
flushline();
645+
if (killprint) {
646+
err_setstr(RuntimeError,
647+
"printing expression statement");
648+
x = 0;
649+
}
642650
}
643651
DECREF(v);
644652
break;

Python/frozenmain.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,43 @@ extern char *getenv();
3030

3131
extern int debugging;
3232
extern int verbose;
33+
extern int killprint;
3334

3435
main(argc, argv)
3536
int argc;
3637
char **argv;
3738
{
3839
char *p;
40+
int n, inspect, sts;
3941
int n;
42+
4043
if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
4144
debugging = 1;
4245
if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
4346
verbose = 1;
44-
initargs(&argc, &argv); /* Defined in config*.c */
47+
if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
48+
inspect = 1;
49+
if ((p = getenv("PYTHONKILLPRINT")) && *p != '\0')
50+
killprint = 1;
51+
52+
initargs(&argc, &argv);
4553
initall();
4654
setpythonargv(argc, argv);
55+
4756
n = init_frozen("__main__");
4857
if (n == 0)
4958
fatal("__main__ not frozen");
5059
if (n < 0) {
5160
print_error();
52-
goaway(1);
61+
sts = 1;
5362
}
5463
else
55-
goaway(0);
64+
sts = 0;
65+
66+
if (inspect && isatty((int)fileno(stdin)) &&
67+
(filename != NULL || command != NULL))
68+
sts = run(stdin, "<stdin>") != 0;
69+
70+
goaway(sts);
5671
/*NOTREACHED*/
5772
}

Python/pythonmain.c

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2626

2727
#include "allobjects.h"
2828

29-
extern int debugging; /* Needed by parser.c */
30-
extern int verbose; /* Needed by import.c */
29+
extern int debugging; /* Defined in parser.c */
30+
extern int verbose; /* Defined in import.c */
31+
extern int killprint; /* Defined in ceval.c */
3132

3233
/* Interface to getopt(): */
3334
extern int optind;
@@ -52,10 +53,14 @@ main(argc, argv)
5253
debugging = 1;
5354
if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
5455
verbose = 1;
56+
if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
57+
inspect = 1;
58+
if ((p = getenv("PYTHONKILLPRINT")) && *p != '\0')
59+
killprint = 1;
5560

56-
initargs(&argc, &argv); /* Defined in config*.c */
61+
initargs(&argc, &argv);
5762

58-
while ((c = getopt(argc, argv, "c:div")) != EOF) {
63+
while ((c = getopt(argc, argv, "c:dikv")) != EOF) {
5964
if (c == 'c') {
6065
/* -c is the last option; following arguments
6166
that look like options are left for the
@@ -77,6 +82,10 @@ main(argc, argv)
7782
inspect++;
7883
break;
7984

85+
case 'k':
86+
killprint++;
87+
break;
88+
8089
case 'v':
8190
verbose++;
8291
break;
@@ -85,8 +94,25 @@ main(argc, argv)
8594

8695
default:
8796
fprintf(stderr,
88-
"usage: %s [-c cmd | file | -] [arg] ...\n",
97+
"usage: %s [-d] [-i] [-k] [-v] [-c cmd | file | -] [arg] ...\n",
8998
argv[0]);
99+
fprintf(stderr, "\
100+
\n\
101+
Options and arguments (and corresponding environment variables):\n\
102+
-d : debug output from parser (also PYTHONDEBUG=x)\n\
103+
-i : inspect interactively after running script (also PYTHONINSPECT=x)\n\
104+
-k : kill printing expression statement (also PYTHONKILLPRINT=x)\n\
105+
-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
106+
-c cmd : program passed in as string (terminates option list)\n\
107+
file : program read from script file\n\
108+
- : program read from stdin (default; interactive mode if a tty)\n\
109+
arg ...: arguments passed to program in sys.argv[1:]\n\
110+
\n\
111+
Other environment variables:\n\
112+
PYTHONSTARTUP: file executed on interactive startup (no default)\n\
113+
PYTHONPATH : colon-separated list of directories prefixed to the\n\
114+
default module search path. The result is sys.path.\n\
115+
");
90116
exit(2);
91117
/*NOTREACHED*/
92118

0 commit comments

Comments
 (0)