mirror of
https://github.com/python/cpython.git
synced 2025-07-12 13:55:34 +00:00
* 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).
This commit is contained in:
parent
5ef74b8f8e
commit
9e90a672b4
5 changed files with 118 additions and 59 deletions
|
@ -71,13 +71,6 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#endif /* !unix */
|
#endif /* !unix */
|
||||||
|
|
||||||
/* XXX This is bogus -- times() is defined in posixmodule.c */
|
|
||||||
#ifdef DO_TIMES
|
|
||||||
#include <sys/times.h>
|
|
||||||
#include <sys/param.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef SYSV
|
#ifdef SYSV
|
||||||
/* Access timezone stuff */
|
/* Access timezone stuff */
|
||||||
#ifdef OLDTZ /* ANSI prepends underscore to these */
|
#ifdef OLDTZ /* ANSI prepends underscore to these */
|
||||||
|
@ -227,32 +220,6 @@ time_millitimer(self, args)
|
||||||
|
|
||||||
#endif /* DO_MILLI */
|
#endif /* DO_MILLI */
|
||||||
|
|
||||||
#ifdef DO_TIMES
|
|
||||||
|
|
||||||
static object *
|
|
||||||
time_times(self, args)
|
|
||||||
object *self;
|
|
||||||
object *args;
|
|
||||||
{
|
|
||||||
struct tms t;
|
|
||||||
clock_t c;
|
|
||||||
if (!getnoarg(args))
|
|
||||||
return NULL;
|
|
||||||
errno = 0;
|
|
||||||
c = times(&t);
|
|
||||||
if (c == (clock_t) -1) {
|
|
||||||
err_errno(IOError);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return mkvalue("(dddd)",
|
|
||||||
(double)t.tms_utime / HZ,
|
|
||||||
(double)t.tms_stime / HZ,
|
|
||||||
(double)t.tms_cutime / HZ,
|
|
||||||
(double)t.tms_cstime / HZ);
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
time_convert(when, function)
|
time_convert(when, function)
|
||||||
|
@ -268,7 +235,7 @@ time_convert(when, function)
|
||||||
p->tm_min,
|
p->tm_min,
|
||||||
p->tm_sec,
|
p->tm_sec,
|
||||||
(p->tm_wday + 6) % 7, /* Want Monday == 0 */
|
(p->tm_wday + 6) % 7, /* Want Monday == 0 */
|
||||||
p->tm_yday,
|
p->tm_yday + 1, /* Want January, 1 == 1 */
|
||||||
p->tm_isdst);
|
p->tm_isdst);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -294,6 +261,62 @@ time_localtime(self, args)
|
||||||
return time_convert((time_t)when, localtime);
|
return time_convert((time_t)when, localtime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
gettmarg(args, p)
|
||||||
|
object *args;
|
||||||
|
struct tm *p;
|
||||||
|
{
|
||||||
|
if (!getargs(args, "(iiiiiiiii)",
|
||||||
|
&p->tm_year,
|
||||||
|
&p->tm_mon,
|
||||||
|
&p->tm_mday,
|
||||||
|
&p->tm_hour,
|
||||||
|
&p->tm_min,
|
||||||
|
&p->tm_sec,
|
||||||
|
&p->tm_wday,
|
||||||
|
&p->tm_yday,
|
||||||
|
&p->tm_isdst))
|
||||||
|
return 0;
|
||||||
|
if (p->tm_year >= 1900)
|
||||||
|
p->tm_year -= 1900;
|
||||||
|
p->tm_mon--;
|
||||||
|
p->tm_wday = (p->tm_wday + 1) % 7;
|
||||||
|
p->tm_yday--;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static object *
|
||||||
|
time_asctime(self, args)
|
||||||
|
object *self;
|
||||||
|
object *args;
|
||||||
|
{
|
||||||
|
struct tm buf;
|
||||||
|
char *p;
|
||||||
|
if (!gettmarg(args, &buf))
|
||||||
|
return NULL;
|
||||||
|
p = asctime(&buf);
|
||||||
|
if (p[24] == '\n')
|
||||||
|
p[24] = '\0';
|
||||||
|
return newstringobject(p);
|
||||||
|
}
|
||||||
|
|
||||||
|
static object *
|
||||||
|
time_ctime(self, args)
|
||||||
|
object *self;
|
||||||
|
object *args;
|
||||||
|
{
|
||||||
|
double dt;
|
||||||
|
time_t tt;
|
||||||
|
char *p;
|
||||||
|
if (!getargs(args, "d", &dt))
|
||||||
|
return NULL;
|
||||||
|
tt = dt;
|
||||||
|
p = ctime(&tt);
|
||||||
|
if (p[24] == '\n')
|
||||||
|
p[24] = '\0';
|
||||||
|
return newstringobject(p);
|
||||||
|
}
|
||||||
|
|
||||||
/* Some very old systems may not have mktime(). Comment it out then! */
|
/* Some very old systems may not have mktime(). Comment it out then! */
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
|
@ -302,20 +325,8 @@ time_mktime(self, args)
|
||||||
object *args;
|
object *args;
|
||||||
{
|
{
|
||||||
struct tm buf;
|
struct tm buf;
|
||||||
if (!getargs(args, "(iiiiiiiii)",
|
if (!gettmarg(args, &buf))
|
||||||
&buf.tm_year,
|
|
||||||
&buf.tm_mon,
|
|
||||||
&buf.tm_mday,
|
|
||||||
&buf.tm_hour,
|
|
||||||
&buf.tm_min,
|
|
||||||
&buf.tm_sec,
|
|
||||||
&buf.tm_wday,
|
|
||||||
&buf.tm_yday,
|
|
||||||
&buf.tm_isdst))
|
|
||||||
return NULL;
|
return NULL;
|
||||||
if (buf.tm_year >= 1900)
|
|
||||||
buf.tm_year -= 1900;
|
|
||||||
buf.tm_mon--;
|
|
||||||
return newintobject((long)mktime(&buf));
|
return newintobject((long)mktime(&buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -324,13 +335,12 @@ static struct methodlist time_methods[] = {
|
||||||
{"millisleep", time_millisleep},
|
{"millisleep", time_millisleep},
|
||||||
{"millitimer", time_millitimer},
|
{"millitimer", time_millitimer},
|
||||||
#endif /* DO_MILLI */
|
#endif /* DO_MILLI */
|
||||||
#ifdef DO_TIMES
|
|
||||||
{"times", time_times},
|
|
||||||
#endif
|
|
||||||
{"sleep", time_sleep},
|
{"sleep", time_sleep},
|
||||||
{"time", time_time},
|
{"time", time_time},
|
||||||
{"gmtime", time_gmtime},
|
{"gmtime", time_gmtime},
|
||||||
{"localtime", time_localtime},
|
{"localtime", time_localtime},
|
||||||
|
{"asctime", time_asctime},
|
||||||
|
{"ctime", time_ctime},
|
||||||
{"mktime", time_mktime},
|
{"mktime", time_mktime},
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
|
@ -40,7 +40,7 @@ listtree(n)
|
||||||
|
|
||||||
static int level, atbol;
|
static int level, atbol;
|
||||||
|
|
||||||
void
|
static void
|
||||||
listnode(fp, n)
|
listnode(fp, n)
|
||||||
FILE *fp;
|
FILE *fp;
|
||||||
node *n;
|
node *n;
|
||||||
|
|
|
@ -49,6 +49,9 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
#define CHECKEXC 1 /* Double-check exception checking */
|
#define CHECKEXC 1 /* Double-check exception checking */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* Global option, may be set by main() */
|
||||||
|
int killprint;
|
||||||
|
|
||||||
|
|
||||||
/* Forward declarations */
|
/* Forward declarations */
|
||||||
|
|
||||||
|
@ -639,6 +642,11 @@ eval_code(co, globals, locals, owner, arg)
|
||||||
softspace(x, 1);
|
softspace(x, 1);
|
||||||
err = writeobject(v, x, 0);
|
err = writeobject(v, x, 0);
|
||||||
flushline();
|
flushline();
|
||||||
|
if (killprint) {
|
||||||
|
err_setstr(RuntimeError,
|
||||||
|
"printing expression statement");
|
||||||
|
x = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
DECREF(v);
|
DECREF(v);
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -30,28 +30,43 @@ extern char *getenv();
|
||||||
|
|
||||||
extern int debugging;
|
extern int debugging;
|
||||||
extern int verbose;
|
extern int verbose;
|
||||||
|
extern int killprint;
|
||||||
|
|
||||||
main(argc, argv)
|
main(argc, argv)
|
||||||
int argc;
|
int argc;
|
||||||
char **argv;
|
char **argv;
|
||||||
{
|
{
|
||||||
char *p;
|
char *p;
|
||||||
|
int n, inspect, sts;
|
||||||
int n;
|
int n;
|
||||||
|
|
||||||
if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
|
if ((p = getenv("PYTHONDEBUG")) && *p != '\0')
|
||||||
debugging = 1;
|
debugging = 1;
|
||||||
if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
|
if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
|
||||||
verbose = 1;
|
verbose = 1;
|
||||||
initargs(&argc, &argv); /* Defined in config*.c */
|
if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
|
||||||
|
inspect = 1;
|
||||||
|
if ((p = getenv("PYTHONKILLPRINT")) && *p != '\0')
|
||||||
|
killprint = 1;
|
||||||
|
|
||||||
|
initargs(&argc, &argv);
|
||||||
initall();
|
initall();
|
||||||
setpythonargv(argc, argv);
|
setpythonargv(argc, argv);
|
||||||
|
|
||||||
n = init_frozen("__main__");
|
n = init_frozen("__main__");
|
||||||
if (n == 0)
|
if (n == 0)
|
||||||
fatal("__main__ not frozen");
|
fatal("__main__ not frozen");
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
print_error();
|
print_error();
|
||||||
goaway(1);
|
sts = 1;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
goaway(0);
|
sts = 0;
|
||||||
|
|
||||||
|
if (inspect && isatty((int)fileno(stdin)) &&
|
||||||
|
(filename != NULL || command != NULL))
|
||||||
|
sts = run(stdin, "<stdin>") != 0;
|
||||||
|
|
||||||
|
goaway(sts);
|
||||||
/*NOTREACHED*/
|
/*NOTREACHED*/
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,8 +26,9 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||||
|
|
||||||
#include "allobjects.h"
|
#include "allobjects.h"
|
||||||
|
|
||||||
extern int debugging; /* Needed by parser.c */
|
extern int debugging; /* Defined in parser.c */
|
||||||
extern int verbose; /* Needed by import.c */
|
extern int verbose; /* Defined in import.c */
|
||||||
|
extern int killprint; /* Defined in ceval.c */
|
||||||
|
|
||||||
/* Interface to getopt(): */
|
/* Interface to getopt(): */
|
||||||
extern int optind;
|
extern int optind;
|
||||||
|
@ -52,10 +53,14 @@ main(argc, argv)
|
||||||
debugging = 1;
|
debugging = 1;
|
||||||
if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
|
if ((p = getenv("PYTHONVERBOSE")) && *p != '\0')
|
||||||
verbose = 1;
|
verbose = 1;
|
||||||
|
if ((p = getenv("PYTHONINSPECT")) && *p != '\0')
|
||||||
|
inspect = 1;
|
||||||
|
if ((p = getenv("PYTHONKILLPRINT")) && *p != '\0')
|
||||||
|
killprint = 1;
|
||||||
|
|
||||||
initargs(&argc, &argv); /* Defined in config*.c */
|
initargs(&argc, &argv);
|
||||||
|
|
||||||
while ((c = getopt(argc, argv, "c:div")) != EOF) {
|
while ((c = getopt(argc, argv, "c:dikv")) != EOF) {
|
||||||
if (c == 'c') {
|
if (c == 'c') {
|
||||||
/* -c is the last option; following arguments
|
/* -c is the last option; following arguments
|
||||||
that look like options are left for the
|
that look like options are left for the
|
||||||
|
@ -77,6 +82,10 @@ main(argc, argv)
|
||||||
inspect++;
|
inspect++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'k':
|
||||||
|
killprint++;
|
||||||
|
break;
|
||||||
|
|
||||||
case 'v':
|
case 'v':
|
||||||
verbose++;
|
verbose++;
|
||||||
break;
|
break;
|
||||||
|
@ -85,8 +94,25 @@ main(argc, argv)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fprintf(stderr,
|
fprintf(stderr,
|
||||||
"usage: %s [-c cmd | file | -] [arg] ...\n",
|
"usage: %s [-d] [-i] [-k] [-v] [-c cmd | file | -] [arg] ...\n",
|
||||||
argv[0]);
|
argv[0]);
|
||||||
|
fprintf(stderr, "\
|
||||||
|
\n\
|
||||||
|
Options and arguments (and corresponding environment variables):\n\
|
||||||
|
-d : debug output from parser (also PYTHONDEBUG=x)\n\
|
||||||
|
-i : inspect interactively after running script (also PYTHONINSPECT=x)\n\
|
||||||
|
-k : kill printing expression statement (also PYTHONKILLPRINT=x)\n\
|
||||||
|
-v : verbose (trace import statements) (also PYTHONVERBOSE=x)\n\
|
||||||
|
-c cmd : program passed in as string (terminates option list)\n\
|
||||||
|
file : program read from script file\n\
|
||||||
|
- : program read from stdin (default; interactive mode if a tty)\n\
|
||||||
|
arg ...: arguments passed to program in sys.argv[1:]\n\
|
||||||
|
\n\
|
||||||
|
Other environment variables:\n\
|
||||||
|
PYTHONSTARTUP: file executed on interactive startup (no default)\n\
|
||||||
|
PYTHONPATH : colon-separated list of directories prefixed to the\n\
|
||||||
|
default module search path. The result is sys.path.\n\
|
||||||
|
");
|
||||||
exit(2);
|
exit(2);
|
||||||
/*NOTREACHED*/
|
/*NOTREACHED*/
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue