Commit 0b0db8e3 authored by Guido van Rossum's avatar Guido van Rossum

Added separate main program for the Mac: macmain.c

stdwinmodule.c: wsetfont can now return an error
Makefile: add CL_USE and CL_LIB*S; config.c: move CL part around
New things in imgfile; also in Makefile.
longobject.c: fix comparison of negative long ints...  [REAL BUG!]
marshal.c: add dumps() and loads() to read/write strings
timemodule.c: make sure there's always a floatsleep()
posixmodule.c: rationalize struct returned by times()
Makefile: add test target, disable imgfile by default
thread.c: Improved coexistance with dl module (sjoerd)
stdwinmodule.c: Change include stdwin.h if macintosh
rotormodule.c: added missing last argument to RTR_?_region calls
confic.c: merged with configmac.c, added 1993 to copyright message
fileobject.c: int compared to NULL in writestring(); change fopenRF ifdef
timemodule.c: simplify times() using mkvalue; include myselect.h
  earlier (for sequent).
posixmodule: for sequent, include unistd.h instead of explicit
  extern definitions and don't define rename()
Makefile: change misleading/wrong MD5 comments
parent 80530ce8
...@@ -25,15 +25,30 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -25,15 +25,30 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/* Configurable Python configuration file */ /* Configurable Python configuration file */
/* These modules are normally always included, but *may* be taken out */ /* These modules are normally always included, but *may* be taken out */
#define USE_GRP 1 /* Use together with pwd */
#define USE_MARSHAL 1 /* This is linked anyway */ #define USE_MARSHAL 1 /* This is linked anyway */
#define USE_MATH 1 #define USE_MATH 1
#define USE_PWD 1 /* Use together with grp */ #define USE_STRUCT 1
#define USE_STROP 1
#define USE_TIME 1
#ifdef macintosh
#define USE_AUDIOOP 1
#define USE_IMAGEOP 1
#define USE_MAC 1
#define USE_REGEX 1
#define USE_ROTOR 1
#define USE_STDWIN 1 /* You may turn this off */
#endif
#ifdef unix
#define USE_GRP 1 /* Use together with pwd */
#define USE_POSIX 1 #define USE_POSIX 1
#define USE_PWD 1 /* Use together with grp */
#define USE_SELECT 1 #define USE_SELECT 1
#define USE_SOCKET 1 #define USE_SOCKET 1
#define USE_STRUCT 1 #endif
#define USE_TIME 1
#include <stdio.h>
#include "PROTO.h" #include "PROTO.h"
#include "mymalloc.h" #include "mymalloc.h"
...@@ -45,18 +60,20 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -45,18 +60,20 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifdef __DATE__ #ifdef __DATE__
#define DATE __DATE__ #define DATE __DATE__
#else #else
#define DATE ">= 8 Jan 1993" #define DATE ">= 11 Jan 1993"
#endif #endif
#include <stdio.h>
#ifdef USE_STDWIN #ifdef USE_STDWIN
#include <stdwin.h> #ifdef macintosh
#endif #include ":::src:stdwin:H:stdwin.h"
#else /* !macintosh */
#include "stdwin.h"
#endif /* !macintosh */
#endif /* USE_STDWIN */
char version[80]; char version[80];
char *argv0; char *argv0; /* For dynamic loading in import.c */
/*ARGSUSED*/ /*ARGSUSED*/
void void
...@@ -69,13 +86,16 @@ initargs(p_argc, p_argv) ...@@ -69,13 +86,16 @@ initargs(p_argc, p_argv)
argv0 = **p_argv; argv0 = **p_argv;
#ifdef USE_STDWIN #ifdef USE_STDWIN
wargs(p_argc, p_argv); #ifdef THINK_C_3_0
wsetstdio(1);
#endif #endif
wargs(p_argc, p_argv);
#endif /* USE_STDWIN */
if (*p_argc < 2 && isatty(0) && isatty(1)) if (*p_argc < 2 && isatty(0) && isatty(1))
{ {
printf("Python %s.\n", version); printf("Python %s.\n", version);
printf( printf(
"Copyright 1990, 1991, 1992 Stichting Mathematisch Centrum, Amsterdam\n"); "Copyright 1990, 1991, 1992, 1993 Stichting Mathematisch Centrum, Amsterdam\n");
} }
} }
...@@ -96,14 +116,22 @@ donecalls() ...@@ -96,14 +116,22 @@ donecalls()
} }
#ifndef PYTHONPATH #ifndef PYTHONPATH
#ifdef macintosh
/* On the Mac, the search path is a space-separated list of directories */
#define PYTHONPATH ": :lib :demo"
#else /* !macintosh */
#define PYTHONPATH ".:/usr/local/lib/python" #define PYTHONPATH ".:/usr/local/lib/python"
#endif #endif /* !macintosh */
#endif /* !PYTHONPATH */
extern char *getenv(); extern char *getenv();
char * char *
getpythonpath() getpythonpath()
{ {
#ifdef macintosh
return PYTHONPATH;
#else /* !macintosh */
char *path = getenv("PYTHONPATH"); char *path = getenv("PYTHONPATH");
char *defpath = PYTHONPATH; char *defpath = PYTHONPATH;
char *buf; char *buf;
...@@ -119,6 +147,7 @@ getpythonpath() ...@@ -119,6 +147,7 @@ getpythonpath()
strcat(buf, ":"); strcat(buf, ":");
strcat(buf, defpath); strcat(buf, defpath);
return buf; return buf;
#endif /* !macintosh */
} }
...@@ -142,6 +171,9 @@ extern void initaudioop(); ...@@ -142,6 +171,9 @@ extern void initaudioop();
#ifdef USE_CD #ifdef USE_CD
extern void initcd(); extern void initcd();
#endif #endif
#ifdef USE_CL
extern void initcl();
#endif
#ifdef USE_DBM #ifdef USE_DBM
extern void initdbm(); extern void initdbm();
#endif #endif
...@@ -166,6 +198,9 @@ extern void initimgfile(); ...@@ -166,6 +198,9 @@ extern void initimgfile();
#ifdef USE_JPEG #ifdef USE_JPEG
extern void initjpeg(); extern void initjpeg();
#endif #endif
#ifdef USE_MAC
extern void initmac();
#endif
#ifdef USE_MARSHAL #ifdef USE_MARSHAL
extern void initmarshal(); extern void initmarshal();
#endif #endif
...@@ -217,9 +252,6 @@ extern void initthread(); ...@@ -217,9 +252,6 @@ extern void initthread();
#ifdef USE_SV #ifdef USE_SV
extern void initsv(); extern void initsv();
#endif #endif
#ifdef USE_CL
extern void initcl();
#endif
#ifdef USE_TIME #ifdef USE_TIME
extern void inittime(); extern void inittime();
#endif #endif
...@@ -259,6 +291,10 @@ struct { ...@@ -259,6 +291,10 @@ struct {
{"cd", initcd}, {"cd", initcd},
#endif #endif
#ifdef USE_CL
{"cl", initcl},
#endif
#ifdef USE_DBM #ifdef USE_DBM
{"dbm", initdbm}, {"dbm", initdbm},
#endif #endif
...@@ -291,6 +327,10 @@ struct { ...@@ -291,6 +327,10 @@ struct {
{"jpeg", initjpeg}, {"jpeg", initjpeg},
#endif #endif
#ifdef USE_MAC
{"mac", initmac},
#endif
#ifdef USE_MARSHAL #ifdef USE_MARSHAL
{"marshal", initmarshal}, {"marshal", initmarshal},
#endif #endif
...@@ -355,10 +395,6 @@ struct { ...@@ -355,10 +395,6 @@ struct {
{"sv", initsv}, {"sv", initsv},
#endif #endif
#ifdef USE_CL
{"cl", initcl},
#endif
#ifdef USE_THREAD #ifdef USE_THREAD
{"thread", initthread}, {"thread", initthread},
#endif #endif
......
...@@ -75,6 +75,9 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -75,6 +75,9 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "modsupport.h" #include "modsupport.h"
#include "ceval.h" #include "ceval.h"
#ifdef _SEQUENT_
#include <unistd.h>
#else /* _SEQUENT_ */
/* XXX Aren't these always declared in unistd.h? */ /* XXX Aren't these always declared in unistd.h? */
extern char *strerror PROTO((int)); extern char *strerror PROTO((int));
extern int chmod PROTO((const char *, mode_t)); extern int chmod PROTO((const char *, mode_t));
...@@ -87,6 +90,7 @@ extern int rmdir PROTO((const char *)); ...@@ -87,6 +90,7 @@ extern int rmdir PROTO((const char *));
extern int stat PROTO((const char *, struct stat *)); extern int stat PROTO((const char *, struct stat *));
extern int unlink PROTO((const char *)); extern int unlink PROTO((const char *));
extern int pclose PROTO((FILE *)); extern int pclose PROTO((FILE *));
#endif /* _SEQUENT_ */
#ifdef NO_LSTAT #ifdef NO_LSTAT
#define lstat stat #define lstat stat
#else #else
...@@ -375,7 +379,7 @@ posix_nice(self, args) ...@@ -375,7 +379,7 @@ posix_nice(self, args)
} }
#endif #endif
#ifdef i386 #if i386 && ! _SEQUENT_
int int
rename(from, to) rename(from, to)
char *from; char *from;
...@@ -388,7 +392,7 @@ rename(from, to) ...@@ -388,7 +392,7 @@ rename(from, to)
return status; return status;
return unlink(from); return unlink(from);
} }
#endif /* i386 */ #endif /* i386 && ! _SEQUENT_ */
static object * static object *
posix_rename(self, args) posix_rename(self, args)
...@@ -833,7 +837,6 @@ posix_times(self, args) ...@@ -833,7 +837,6 @@ posix_times(self, args)
{ {
struct tms t; struct tms t;
clock_t c; clock_t c;
object *tuple;
if (!getnoarg(args)) if (!getnoarg(args))
return NULL; return NULL;
errno = 0; errno = 0;
...@@ -842,18 +845,11 @@ posix_times(self, args) ...@@ -842,18 +845,11 @@ posix_times(self, args)
err_errno(PosixError); err_errno(PosixError);
return NULL; return NULL;
} }
tuple = newtupleobject(4); return mkvalue("dddd",
if (tuple == NULL) (double)t.tms_utime / HZ,
return NULL; (double)t.tms_stime / HZ,
settupleitem(tuple, 0, newfloatobject((double)t.tms_utime / HZ)); (double)t.tms_cutime / HZ,
settupleitem(tuple, 1, newfloatobject((double)t.tms_stime / HZ)); (double)t.tms_cstime / HZ);
settupleitem(tuple, 2, newfloatobject((double)t.tms_cutime / HZ));
settupleitem(tuple, 3, newfloatobject((double)t.tms_cstime / HZ));
if (err_occurred()) {
DECREF(tuple);
return NULL;
}
return tuple;
} }
#endif /* DO_TIMES */ #endif /* DO_TIMES */
......
...@@ -590,7 +590,7 @@ static void encrypt_region(r, region, len) ...@@ -590,7 +590,7 @@ static void encrypt_region(r, region, len)
unsigned char *region; unsigned char *region;
int len; int len;
{ {
RTR_e_region(r,region,len); RTR_e_region(r,region,len,TRUE);
} }
/*(defun decrypt-region (beg end key) /*(defun decrypt-region (beg end key)
...@@ -602,7 +602,7 @@ static void decrypt_region(r, region, len) ...@@ -602,7 +602,7 @@ static void decrypt_region(r, region, len)
unsigned char *region; unsigned char *region;
int len; int len;
{ {
RTR_d_region(r,region,len); RTR_d_region(r,region,len,TRUE);
} }
/* Rotor methods */ /* Rotor methods */
......
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
...@@ -66,7 +66,11 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -66,7 +66,11 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "modsupport.h" #include "modsupport.h"
#include "ceval.h" #include "ceval.h"
#ifdef macintosh
#include ":::src:stdwin:H:stdwin.h"
#else /* !macintosh */
#include "stdwin.h" #include "stdwin.h"
#endif /* !macintosh */
#ifdef USE_THREAD #ifdef USE_THREAD
...@@ -585,8 +589,12 @@ drawing_setfont(self, args) ...@@ -585,8 +589,12 @@ drawing_setfont(self, args)
return NULL; return NULL;
} }
} }
if (font != NULL) if (font != NULL) {
wsetfont(font); if (!wsetfont(font)) {
err_setstr(StdwinError, "font not found");
return NULL;
}
}
if (size != 0) if (size != 0)
wsetsize(size); wsetsize(size);
switch (style) { switch (style) {
......
...@@ -35,6 +35,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -35,6 +35,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#ifdef BSD_TIME #ifdef BSD_TIME
#define HAVE_GETTIMEOFDAY #define HAVE_GETTIMEOFDAY
#include "myselect.h" /* Implies <sys/types.h>, <sys/time.h>, <sys/param.h> */
#endif #endif
#ifdef macintosh #ifdef macintosh
...@@ -70,6 +71,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -70,6 +71,7 @@ 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 #ifdef DO_TIMES
#include <sys/times.h> #include <sys/times.h>
#include <sys/param.h> #include <sys/param.h>
...@@ -215,7 +217,6 @@ time_times(self, args) ...@@ -215,7 +217,6 @@ time_times(self, args)
{ {
struct tms t; struct tms t;
clock_t c; clock_t c;
object *tuple;
if (!getnoarg(args)) if (!getnoarg(args))
return NULL; return NULL;
errno = 0; errno = 0;
...@@ -224,18 +225,11 @@ time_times(self, args) ...@@ -224,18 +225,11 @@ time_times(self, args)
err_errno(IOError); err_errno(IOError);
return NULL; return NULL;
} }
tuple = newtupleobject(4); return mkvalue("dddd",
if (tuple == NULL) (double)t.tms_utime / HZ,
return NULL; (double)t.tms_stime / HZ,
settupleitem(tuple, 0, newfloatobject((double)t.tms_utime / HZ)); (double)t.tms_cutime / HZ,
settupleitem(tuple, 1, newfloatobject((double)t.tms_stime / HZ)); (double)t.tms_cstime / HZ);
settupleitem(tuple, 2, newfloatobject((double)t.tms_cutime / HZ));
settupleitem(tuple, 3, newfloatobject((double)t.tms_cstime / HZ));
if (err_occurred()) {
DECREF(tuple);
return NULL;
}
return tuple;
} }
#endif #endif
...@@ -285,7 +279,7 @@ floatsleep(secs) ...@@ -285,7 +279,7 @@ floatsleep(secs)
{ {
register long deadline; register long deadline;
deadline = MacTicks + long(secs * 60.0); deadline = MacTicks + (long)(secs * 60.0);
while (MacTicks < deadline) { while (MacTicks < deadline) {
if (intrcheck()) if (intrcheck())
sleep_catcher(SIGINT); sleep_catcher(SIGINT);
...@@ -301,9 +295,9 @@ millitimer() ...@@ -301,9 +295,9 @@ millitimer()
#endif /* macintosh */ #endif /* macintosh */
#ifdef BSD_TIME #ifdef unix
#include "myselect.h" /* Implies <sys/types.h>, <sys/time.h>, <sys/param.h> */ #ifdef BSD_TIME
long long
millitimer() millitimer()
...@@ -329,7 +323,17 @@ floatsleep(secs) ...@@ -329,7 +323,17 @@ floatsleep(secs)
(void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t); (void) select(0, (fd_set *)0, (fd_set *)0, (fd_set *)0, &t);
} }
#endif /* BSD_TIME */ #else /* !BSD_TIME */
floatsleep(secs)
double secs;
{
sleep((int)secs);
}
#endif /* !BSD_TIME */
#endif /* unix */
#ifdef TURBO_C /* Maybe also for MS-DOS? */ #ifdef TURBO_C /* Maybe also for MS-DOS? */
...@@ -338,14 +342,13 @@ floatsleep(secs) ...@@ -338,14 +342,13 @@ floatsleep(secs)
#define CLOCKS_PER_SEC 55 /* 54.945 msec per tick (18.2 HZ clock) */ #define CLOCKS_PER_SEC 55 /* 54.945 msec per tick (18.2 HZ clock) */
#endif #endif
static
floatsleep(secs) floatsleep(secs)
double secs; double secs;
{ {
delay(long(secs/1000.0)); delay(long(secs/1000.0));
} }
static long long
millitimer() millitimer()
{ {
clock_t ticks; clock_t ticks;
......
...@@ -86,7 +86,7 @@ newfileobject(name, mode) ...@@ -86,7 +86,7 @@ newfileobject(name, mode)
f = (fileobject *) newopenfileobject((FILE *)NULL, name, mode, fclose); f = (fileobject *) newopenfileobject((FILE *)NULL, name, mode, fclose);
if (f == NULL) if (f == NULL)
return NULL; return NULL;
#ifdef THINK_C #ifdef USE_FOPENRF
if (*mode == '*') { if (*mode == '*') {
FILE *fopenRF(); FILE *fopenRF();
f->f_fp = fopenRF(name, mode+1); f->f_fp = fopenRF(name, mode+1);
...@@ -670,7 +670,7 @@ writestring(s, f) ...@@ -670,7 +670,7 @@ writestring(s, f)
err_clear(); err_clear();
} }
else { else {
if (writeobject(v, f, PRINT_RAW) != NULL) if (writeobject(v, f, PRINT_RAW) != 0)
err_clear(); err_clear();
DECREF(v); DECREF(v);
} }
......
...@@ -606,8 +606,11 @@ long_compare(a, b) ...@@ -606,8 +606,11 @@ long_compare(a, b)
; ;
if (i < 0) if (i < 0)
sign = 0; sign = 0;
else else {
sign = (int)a->ob_digit[i] - (int)b->ob_digit[i]; sign = (int)a->ob_digit[i] - (int)b->ob_digit[i];
if (a->ob_size < 0)
sign = -sign;
}
} }
return sign < 0 ? -1 : sign > 0 ? 1 : 0; return sign < 0 ? -1 : sign > 0 ? 1 : 0;
} }
......
/*********************************************************** /***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Netherlands. Amsterdam, The Netherlands.
All Rights Reserved All Rights Reserved
...@@ -48,86 +48,133 @@ extern int errno; ...@@ -48,86 +48,133 @@ extern int errno;
#define TYPE_CODE 'C' #define TYPE_CODE 'C'
#define TYPE_UNKNOWN '?' #define TYPE_UNKNOWN '?'
#define wr_byte(c, fp) putc((c), (fp)) typedef struct {
FILE *fp;
/* If fp == NULL, the following are valid: */
object *str;
char *ptr;
char *end;
} WFILE;
void #define w_byte(c, p) if (((p)->fp)) putc((c), (p)->fp); \
wr_short(x, fp) else if ((p)->ptr != (p)->end) *(p)->ptr++ = (c); \
else w_more(c, p)
static void
w_more(c, p)
char c;
WFILE *p;
{
int size, newsize;
if (p->str == NULL)
return; /* An error already occurred */
size = getstringsize(p->str);
newsize = size + 1024;
if (resizestring(&p->str, newsize) != 0) {
p->ptr = p->end = NULL;
}
else {
p->ptr = GETSTRINGVALUE((stringobject *)p->str) + size;
p->end = GETSTRINGVALUE((stringobject *)p->str) + newsize;
*p->ptr++ = c;
}
}
static void
w_string(s, n, p)
char *s;
int n;
WFILE *p;
{
if (p->fp != NULL) {
fwrite(s, 1, n, p->fp);
}
else {
while (--n >= 0) {
w_byte(*s, p);
s++;
}
}
}
static void
w_short(x, p)
int x; int x;
FILE *fp; WFILE *p;
{ {
wr_byte( x & 0xff, fp); w_byte( x & 0xff, p);
wr_byte((x>> 8) & 0xff, fp); w_byte((x>> 8) & 0xff, p);
} }
void static void
wr_long(x, fp) w_long(x, p)
long x; long x;
FILE *fp; WFILE *p;
{ {
wr_byte((int)( x & 0xff), fp); w_byte((int)( x & 0xff), p);
wr_byte((int)((x>> 8) & 0xff), fp); w_byte((int)((x>> 8) & 0xff), p);
wr_byte((int)((x>>16) & 0xff), fp); w_byte((int)((x>>16) & 0xff), p);
wr_byte((int)((x>>24) & 0xff), fp); w_byte((int)((x>>24) & 0xff), p);
} }
void static void
wr_object(v, fp) w_object(v, p)
object *v; object *v;
FILE *fp; WFILE *p;
{ {
long i, n; long i, n;
if (v == NULL) if (v == NULL)
wr_byte(TYPE_NULL, fp); w_byte(TYPE_NULL, p);
else if (v == None) else if (v == None)
wr_byte(TYPE_NONE, fp); w_byte(TYPE_NONE, p);
else if (is_intobject(v)) { else if (is_intobject(v)) {
wr_byte(TYPE_INT, fp); w_byte(TYPE_INT, p);
wr_long(getintvalue(v), fp); w_long(getintvalue(v), p);
} }
else if (is_longobject(v)) { else if (is_longobject(v)) {
longobject *ob = (longobject *)v; longobject *ob = (longobject *)v;
wr_byte(TYPE_LONG, fp); w_byte(TYPE_LONG, p);
n = ob->ob_size; n = ob->ob_size;
wr_long((long)n, fp); w_long((long)n, p);
if (n < 0) if (n < 0)
n = -n; n = -n;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
wr_short(ob->ob_digit[i], fp); w_short(ob->ob_digit[i], p);
} }
else if (is_floatobject(v)) { else if (is_floatobject(v)) {
extern void float_buf_repr PROTO((char *, floatobject *)); extern void float_buf_repr PROTO((char *, floatobject *));
char buf[256]; /* Plenty to format any double */ char buf[256]; /* Plenty to format any double */
float_buf_repr(buf, (floatobject *)v); float_buf_repr(buf, (floatobject *)v);
n = strlen(buf); n = strlen(buf);
wr_byte(TYPE_FLOAT, fp); w_byte(TYPE_FLOAT, p);
wr_byte((int)n, fp); w_byte((int)n, p);
fwrite(buf, 1, (int)n, fp); w_string(buf, (int)n, p);
} }
else if (is_stringobject(v)) { else if (is_stringobject(v)) {
wr_byte(TYPE_STRING, fp); w_byte(TYPE_STRING, p);
n = getstringsize(v); n = getstringsize(v);
wr_long(n, fp); w_long(n, p);
fwrite(getstringvalue(v), 1, (int)n, fp); w_string(getstringvalue(v), (int)n, p);
} }
else if (is_tupleobject(v)) { else if (is_tupleobject(v)) {
wr_byte(TYPE_TUPLE, fp); w_byte(TYPE_TUPLE, p);
n = gettuplesize(v); n = gettuplesize(v);
wr_long(n, fp); w_long(n, p);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
wr_object(gettupleitem(v, (int)i), fp); w_object(gettupleitem(v, (int)i), p);
} }
} }
else if (is_listobject(v)) { else if (is_listobject(v)) {
wr_byte(TYPE_LIST, fp); w_byte(TYPE_LIST, p);
n = getlistsize(v); n = getlistsize(v);
wr_long(n, fp); w_long(n, p);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
wr_object(getlistitem(v, (int)i), fp); w_object(getlistitem(v, (int)i), p);
} }
} }
else if (is_dictobject(v)) { else if (is_dictobject(v)) {
wr_byte(TYPE_DICT, fp); w_byte(TYPE_DICT, p);
/* This one is NULL object terminated! */ /* This one is NULL object terminated! */
n = getdictsize(v); n = getdictsize(v);
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
...@@ -135,58 +182,96 @@ wr_object(v, fp) ...@@ -135,58 +182,96 @@ wr_object(v, fp)
key = getdict2key(v, (int)i); key = getdict2key(v, (int)i);
if (key != NULL) { if (key != NULL) {
val = dict2lookup(v, key); /* Can't be NULL */ val = dict2lookup(v, key); /* Can't be NULL */
wr_object(key, fp); w_object(key, p);
wr_object(val, fp); w_object(val, p);
} }
} }
wr_object((object *)NULL, fp); w_object((object *)NULL, p);
} }
else if (is_codeobject(v)) { else if (is_codeobject(v)) {
codeobject *co = (codeobject *)v; codeobject *co = (codeobject *)v;
wr_byte(TYPE_CODE, fp); w_byte(TYPE_CODE, p);
wr_object((object *)co->co_code, fp); w_object((object *)co->co_code, p);
wr_object(co->co_consts, fp); w_object(co->co_consts, p);
wr_object(co->co_names, fp); w_object(co->co_names, p);
wr_object(co->co_filename, fp); w_object(co->co_filename, p);
} }
else { else {
wr_byte(TYPE_UNKNOWN, fp); w_byte(TYPE_UNKNOWN, p);
} }
} }
#define rd_byte(fp) getc(fp) void
wr_long(x, fp)
long x;
FILE *fp;
{
WFILE wf;
wf.fp = fp;
w_long(x, &wf);
}
int void
rd_short(fp) wr_object(x, fp)
object *x;
FILE *fp; FILE *fp;
{
WFILE wf;
wf.fp = fp;
w_object(x, &wf);
}
typedef WFILE RFILE; /* Same struct with different invariants */
#define r_byte(p) ((p)->fp ? getc((p)->fp) \
: ((p)->ptr != (p)->end) ? *(p)->ptr++ : EOF)
static int
r_string(s, n, p)
char *s;
int n;
RFILE *p;
{
if (p->fp != NULL)
return fread(s, 1, n, p->fp);
if (p->end - p->ptr < n)
n = p->end - p->ptr;
memcpy(s, p->ptr, n);
p->ptr += n;
return n;
}
static int
r_short(p)
RFILE *p;
{ {
register short x; register short x;
x = rd_byte(fp); x = r_byte(p);
x |= rd_byte(fp) << 8; x |= r_byte(p) << 8;
/* XXX If your short is > 16 bits, add sign-extension here!!! */ /* XXX If your short is > 16 bits, add sign-extension here!!! */
return x; return x;
} }
long static long
rd_long(fp) r_long(p)
FILE *fp; RFILE *p;
{ {
register long x; register long x;
x = rd_byte(fp); x = r_byte(p);
x |= (long)rd_byte(fp) << 8; x |= (long)r_byte(p) << 8;
x |= (long)rd_byte(fp) << 16; x |= (long)r_byte(p) << 16;
x |= (long)rd_byte(fp) << 24; x |= (long)r_byte(p) << 24;
/* XXX If your long is > 32 bits, add sign-extension here!!! */ /* XXX If your long is > 32 bits, add sign-extension here!!! */
return x; return x;
} }
object * static object *
rd_object(fp) r_object(p)
FILE *fp; RFILE *p;
{ {
object *v; object *v;
long i, n; long i, n;
int type = rd_byte(fp); int type = r_byte(p);
switch (type) { switch (type) {
...@@ -202,20 +287,20 @@ rd_object(fp) ...@@ -202,20 +287,20 @@ rd_object(fp)
return None; return None;
case TYPE_INT: case TYPE_INT:
return newintobject(rd_long(fp)); return newintobject(r_long(p));
case TYPE_LONG: case TYPE_LONG:
{ {
int size; int size;
longobject *ob; longobject *ob;
n = rd_long(fp); n = r_long(p);
size = n<0 ? -n : n; size = n<0 ? -n : n;
ob = alloclongobject(size); ob = alloclongobject(size);
if (ob == NULL) if (ob == NULL)
return NULL; return NULL;
ob->ob_size = n; ob->ob_size = n;
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
ob->ob_digit[i] = rd_short(fp); ob->ob_digit[i] = r_short(p);
return (object *)ob; return (object *)ob;
} }
...@@ -225,8 +310,8 @@ rd_object(fp) ...@@ -225,8 +310,8 @@ rd_object(fp)
char buf[256]; char buf[256];
double res; double res;
char *end; char *end;
n = rd_byte(fp); n = r_byte(p);
if (fread(buf, 1, (int)n, fp) != n) { if (r_string(buf, (int)n, p) != n) {
err_setstr(EOFError, err_setstr(EOFError,
"EOF read where object expected"); "EOF read where object expected");
return NULL; return NULL;
...@@ -247,10 +332,10 @@ rd_object(fp) ...@@ -247,10 +332,10 @@ rd_object(fp)
} }
case TYPE_STRING: case TYPE_STRING:
n = rd_long(fp); n = r_long(p);
v = newsizedstringobject((char *)NULL, n); v = newsizedstringobject((char *)NULL, n);
if (v != NULL) { if (v != NULL) {
if (fread(getstringvalue(v), 1, (int)n, fp) != n) { if (r_string(getstringvalue(v), (int)n, p) != n) {
DECREF(v); DECREF(v);
v = NULL; v = NULL;
err_setstr(EOFError, err_setstr(EOFError,
...@@ -260,21 +345,21 @@ rd_object(fp) ...@@ -260,21 +345,21 @@ rd_object(fp)
return v; return v;
case TYPE_TUPLE: case TYPE_TUPLE:
n = rd_long(fp); n = r_long(p);
v = newtupleobject((int)n); v = newtupleobject((int)n);
if (v == NULL) if (v == NULL)
return v; return v;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
settupleitem(v, (int)i, rd_object(fp)); settupleitem(v, (int)i, r_object(p));
return v; return v;
case TYPE_LIST: case TYPE_LIST:
n = rd_long(fp); n = r_long(p);
v = newlistobject((int)n); v = newlistobject((int)n);
if (v == NULL) if (v == NULL)
return v; return v;
for (i = 0; i < n; i++) for (i = 0; i < n; i++)
setlistitem(v, (int)i, rd_object(fp)); setlistitem(v, (int)i, r_object(p));
return v; return v;
case TYPE_DICT: case TYPE_DICT:
...@@ -283,10 +368,10 @@ rd_object(fp) ...@@ -283,10 +368,10 @@ rd_object(fp)
return NULL; return NULL;
for (;;) { for (;;) {
object *key, *val; object *key, *val;
key = rd_object(fp); key = r_object(p);
if (key == NULL) if (key == NULL)
break; break;
val = rd_object(fp); val = r_object(p);
dict2insert(v, key, val); dict2insert(v, key, val);
DECREF(key); DECREF(key);
XDECREF(val); XDECREF(val);
...@@ -295,10 +380,10 @@ rd_object(fp) ...@@ -295,10 +380,10 @@ rd_object(fp)
case TYPE_CODE: case TYPE_CODE:
{ {
object *code = rd_object(fp); object *code = r_object(p);
object *consts = rd_object(fp); object *consts = r_object(p);
object *names = rd_object(fp); object *names = r_object(p);
object *filename = rd_object(fp); object *filename = r_object(p);
if (!err_occurred()) { if (!err_occurred()) {
v = (object *) newcodeobject(code, v = (object *) newcodeobject(code,
consts, names, filename); consts, names, filename);
...@@ -320,39 +405,113 @@ rd_object(fp) ...@@ -320,39 +405,113 @@ rd_object(fp)
} }
} }
long
rd_long(fp)
FILE *fp;
{
RFILE rf;
rf.fp = fp;
return r_long(&rf);
}
object *
rd_object(fp)
FILE *fp;
{
RFILE rf;
rf.fp = fp;
return r_object(&rf);
}
/* And an interface for Python programs... */ /* And an interface for Python programs... */
static object * static object *
dump(self, args) marshal_dump(self, args)
object *self; object *self;
object *args; object *args;
{ {
WFILE wf;
object *x;
object *f; object *f;
if (args == NULL || !is_tupleobject(args) || gettuplesize(args) != 2) { if (!getargs(args, "(OO)", &x, &f))
err_badarg();
return NULL; return NULL;
} if (!is_fileobject(f)) {
f = gettupleitem(args, 1); err_setstr(TypeError, "marshal.dump() 2nd arg must be file");
if (f == NULL || !is_fileobject(f)) {
err_badarg();
return NULL; return NULL;
} }
wr_object(gettupleitem(args, 0), getfilefile(f)); wf.fp = getfilefile(f);
wf.str = NULL;
wf.ptr = wf.end = NULL;
w_object(x, &wf);
INCREF(None); INCREF(None);
return None; return None;
} }
static object * static object *
load(self, f) marshal_load(self, args)
object *self; object *self;
object *f; object *args;
{ {
RFILE rf;
object *f;
object *v; object *v;
if (f == NULL || !is_fileobject(f)) { if (!getargs(args, "O", &f))
err_badarg(); return NULL;
if (!is_fileobject(f)) {
err_setstr(TypeError, "marshal.load() arg must be file");
return NULL; return NULL;
} }
v = rd_object(getfilefile(f)); rf.fp = getfilefile(f);
rf.str = NULL;
rf.ptr = rf.end = NULL;
err_clear();
v = r_object(&rf);
if (err_occurred()) {
XDECREF(v);
v = NULL;
}
return v;
}
static object *
marshal_dumps(self, args)
object *self;
object *args;
{
WFILE wf;
object *x;
if (!getargs(args, "O", &x))
return NULL;
wf.fp = NULL;
wf.str = newsizedstringobject((char *)NULL, 50);
if (wf.str == NULL)
return NULL;
wf.ptr = GETSTRINGVALUE((stringobject *)wf.str);
wf.end = wf.ptr + getstringsize(wf.str);
w_object(x, &wf);
if (wf.str != NULL)
resizestring(&wf.str,
(int) (wf.ptr - GETSTRINGVALUE((stringobject *)wf.str)));
return wf.str;
}
static object *
marshal_loads(self, args)
object *self;
object *args;
{
RFILE rf;
object *v;
char *s;
int n;
if (!getargs(args, "s#", &s, &n))
return NULL;
rf.fp = NULL;
rf.str = args;
rf.ptr = s;
rf.end = s + n;
err_clear();
v = r_object(&rf);
if (err_occurred()) { if (err_occurred()) {
XDECREF(v); XDECREF(v);
v = NULL; v = NULL;
...@@ -361,8 +520,10 @@ load(self, f) ...@@ -361,8 +520,10 @@ load(self, f)
} }
static struct methodlist marshal_methods[] = { static struct methodlist marshal_methods[] = {
{"dump", dump}, {"dump", marshal_dump},
{"load", load}, {"load", marshal_load},
{"dumps", marshal_dumps},
{"loads", marshal_loads},
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment