summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Israelsson Tampe <stefan.itampe@gmail.com>2018-03-29 22:28:51 +0200
committerStefan Israelsson Tampe <stefan.itampe@gmail.com>2018-03-29 22:28:51 +0200
commitc8877ccaf7d6e9021899da2933221b7f276b440c (patch)
treeb7a13bc229dd57eef699965ccc1c94ac9340acc6
parent0ffcc9684a1f8fe8c3a8fc4d9915b7eb4eb61e7c (diff)
finishing the os module
-rw-r--r--modules/language/python/dict.scm13
-rw-r--r--modules/language/python/module.scm16
-rw-r--r--modules/language/python/module/os.scm343
-rw-r--r--modules/oop/pf-objects.scm24
4 files changed, 362 insertions, 34 deletions
diff --git a/modules/language/python/dict.scm b/modules/language/python/dict.scm
index a234281..c034503 100644
--- a/modules/language/python/dict.scm
+++ b/modules/language/python/dict.scm
@@ -554,8 +554,17 @@
(slot-set! self 'n (slot-ref r 'n))))
((self x)
(__init__ self)
- (for ((k v : x)) ()
- (pylist-set! self k v))))))
+ (catch #t
+ (lambda ()
+ (for ((k v : x)) ()
+ (pylist-set! self k v)))
+ (lambda y
+ (for ((k : x)) ()
+ (if (pair? k)
+ (pylist-set! self (car k) (cdr k))
+ (raise TypeError
+ "wrong type of argument to dict" k)))))))))
+
__init__)))
(define-python-class weak-key-dict (<py-hashtable>)
diff --git a/modules/language/python/module.scm b/modules/language/python/module.scm
index 9884c2b..0a4ae2e 100644
--- a/modules/language/python/module.scm
+++ b/modules/language/python/module.scm
@@ -137,7 +137,7 @@
(if (rawref self '_module)
(let ((k (_k k))
(m (_m self)))
- (let ((x (module-ref m k e)))
+ (let ((x (pk (module-ref m k e))))
(if (eq? e x)
(fail)
x)))
@@ -146,16 +146,16 @@
(define __setattr__
(lambda (self k v)
(let ((k (_k k))
- (fail (lambda () (raise KeyError "getattr in Module"))))
+ (fail (lambda () (raise KeyError "getattr in Module" k))))
(if (rawref self k)
(fail)
(aif m (rawref self '_module)
(catch #t
- (lambda ()
- (if (module-defined? m k)
- (module-set! m k v)
- (module-define! m k v)))
- (lambda x (fail x)))
+ (lambda ()
+ (if (module-defined? m k)
+ (module-set! m k v)
+ (module-define! m k v)))
+ (lambda x (fail)))
(fail))))))
(define __delattr__
@@ -174,7 +174,7 @@
(define __getitem__
(lambda (self k)
(define k (if (string? k) (string->symbol k) k))
- (__getattr__ self k)))
+ (__getattribute__ self k)))
(define __iter__
(lambda (self)
diff --git a/modules/language/python/module/os.scm b/modules/language/python/module/os.scm
index 3d8b852..03b0848 100644
--- a/modules/language/python/module/os.scm
+++ b/modules/language/python/module/os.scm
@@ -6,6 +6,7 @@
#:use-module (oop pf-objects)
#:use-module (oop goops)
#:use-module (rnrs bytevectors)
+ #:use-module (rnrs io ports)
#:use-module (language python for)
#:use-module (language python persist)
#:use-module (language python try)
@@ -84,6 +85,13 @@
sched_setaffinity sched_getaffinity
supports_dir_fs support_effective_ids supports_fd
+
+ confstr confstr_names cpu_count sysconf sysconf_names
+
+ RTLD_LAZY RTLD_NOW RTLD_GLOBAL RTLD_LOCAL RTLD_NODELETE
+ RTLD_NOLOAD RTLD_DEEPBIND
+
+ getrandom urandom GRND_NONBLOCK GRND_RANDOM
))
(define supports_dir_fs
@@ -110,7 +118,7 @@
(let ((r (ca code)))
(if (< r 0)
(raise error (errno) ((@ (guile) strerror) (errno)))
- (values))))
+ r)))
(define (py-add s x)
((ref s 'add) x))
@@ -153,17 +161,6 @@
(define code1 (lambda x (ca (apply (@ (guile) code2) x)))))))
-(define path "posixpath")
-(define curdir ".")
-(define pardir "..")
-(define sep "/")
-(define extsep ".")
-(define altsep None)
-(define pathsep ":")
-(define linesep "\n")
-(define defpath "/usr/bin")
-(define devnull "/dev/null")
-
(define name "posix")
(guile ctermid)
@@ -1997,3 +1994,325 @@
;; MISC SYSTEM INFORMATION
+(defineu confstr_ ()
+ (let ((f (pointer->procedure int
+ (dynamic-func "confstr"
+ (dynamic-link))
+ (list int '* int))))
+ (lambda (id)
+ (let* ((v (make-bytevector 1024))
+ (vp (bytevector->pointer v)))
+ (rm (f id vp 1024))
+ (pointer->string vp)))))
+
+(define confstr_names
+ (dict
+ '(("LIBC_VERSION" . 2)
+ ("LIBPTHREAD_VERSION" . 3)
+ ("PATH" . 0))))
+
+(define (confstr id)
+ (let ((id2 (if (number? id) id (pylist-ref confstr_names id))))
+ (if id2
+ (confstr_ id2)
+ (raise KeyError "no confstr for " id))))
+
+(defineu cpu_count ()
+ (let ((f (pointer->procedure int
+ (dynamic-func "get_nprocs"
+ (dynamic-link))
+ (list))))
+ (lambda ()
+ (rm (f)))))
+
+
+(defineu sysconf_ ()
+ (let ((f (pointer->procedure long
+ (dynamic-func "sysconf"
+ (dynamic-link))
+ (list int))))
+ (lambda (id)
+ (rm (f id)))))
+
+(define i 0)
+(define (f) (let ((r i)) (set! i (+ i 1)) r))
+(define sysconf_names
+ (dict
+ `(("ARG_MAX" . ,(f))
+ ("CHILD_MAX" . ,(f))
+ ("CLK_TCK" . ,(f))
+ ("NGROUPS_MAX" . ,(f))
+ ("OPEN_MAX" . ,(f))
+ ("STREAM_MAX" . ,(f))
+ ("TZNAME_MAX" . ,(f))
+ ("JOB_CONTROL" . ,(f))
+ ("SAVED_IDS" . ,(f))
+ ("REALTIME_SIGNALS" . ,(f))
+ ("PRIORITY_SCHEDULING" . ,(f))
+ ("TIMERS" . ,(f))
+ ("ASYNCHRONOÙS_IO" . ,(f))
+ ("PRIORITIZED_IO" . ,(f))
+ ("SYNCHRONIZED_IO" . ,(f))
+ ("FSYNC" . ,(f))
+ ("MAPPED_FILES" . ,(f))
+ ("MEMLOCK" . ,(f))
+ ("MEMLOCK_RANGE" . ,(f))
+ ("MEMORY_PROTECTION" . ,(f))
+ ("MESSAGE_PASSING" . ,(f))
+ ("SEMAPHORES" . ,(f))
+ ("SHARED_MEMORY_OBJECTS" . ,(f))
+ ("AIO_LISTIO_MAX" . ,(f))
+ ("AIO_MAX" . ,(f))
+ ("AIO_PRIO_DELTA_MAX" . ,(f))
+ ("AIO_DELAYTIMER_MAX" . ,(f))
+ ("MQ_OPEN_MAX" . ,(f))
+ ("MQ_PRIO_MAX" . ,(f))
+ ("POSIX_VERSION" . ,(f))
+ ("PAGESIZE" . ,(f))
+ ("RTSIG_MAX" . ,(f))
+ ("SEM_NSEMS_MAX" . ,(f))
+ ("SEM_VALUE_MAX" . ,(f))
+ ("SIGQUEUE_MAX" . ,(f))
+ ("TIMER_MAX" . ,(f))
+
+ ("BC_BASE_MAX" . ,(f))
+ ("BC_DIM_MAX" . ,(f))
+ ("BC_SCALE_MAX" . ,(f))
+ ("BC_STRING_MAX" . ,(f))
+ ("COLL_WEIGHTS_MAX" . ,(f))
+ ("EQUIV_CLASS_MAX" . ,(f))
+ ("EXPR_NEST_MAX" . ,(f))
+ ("LINE_MAX" . ,(f))
+ ("RE_DUP_MAX" . ,(f))
+ ("CHARCLASS_NAME_MAX" . ,(f))
+ ("POSIX2_VERSION" . ,(f))
+ ("2_C_BIND" . ,(f))
+ ("2_C_DEV" . ,(f))
+ ("2_FORT_DEV" . ,(f))
+ ("2_FORT_RUN" . ,(f))
+ ("2_SW_DEF" . ,(f))
+ ("2_LOCALEDEF" . ,(f))
+ ("PII" . ,(f))
+ ("PII_XTI" . ,(f))
+ ("PII_SOCKET" . ,(f))
+ ("PII_INTERNET" . ,(f))
+ ("PII_OSI" . ,(f))
+ ("POLL" . ,(f))
+ ("SELECT" . ,(f))
+ ("UIO_MAXIOV" . ,i)
+ ("IOV_MAX" . ,(f))
+ ("PII_INTERNET_STREAM" . ,(f))
+ ("PII_INTERNET_DGRAM" . ,(f))
+ ("PII_OSI_COTS" . ,(f))
+ ("PII_OSI_CLTS" . ,(f))
+ ("PII_OSI_M" . ,(f))
+ ("T_IOV_MAX" . ,(f))
+ ("THREADS" . ,(f))
+ ("THREAD_SAFE_FUNCTIONS" . ,(f))
+ ("GETGR_R_SIZE_MAX" . ,(f))
+ ("GETPW_R_SIZE_MAX" . ,(f))
+ ("LOGIN_NAME_MAX" . ,(f))
+ ("TTY_NAME_MAX" . ,(f))
+ ("THREAD_DESTRUCTOR_ITERATIONS" . ,(f))
+ ("THREAD_KEYS_MAX" . ,(f))
+ ("THREAD_STACK_MIN" . ,(f))
+ ("THREAD_THREADS_MAX" . ,(f))
+ ("THREAD_ATTR_STACKADDR" . ,(f))
+ ("THREAD_ATTR_STACKSIZE" . ,(f))
+ ("THREAD_PRIORITY_SCHEDULING" . ,(f))
+ ("THREAD_PRIO_INHERIT" . ,(f))
+ ("THREAD_PRIO_PROTECT" . ,(f))
+ ("THREAD_PROCESS_SHARED" . ,(f))
+ ("NPROCESSORS_CONF" . ,(f))
+ ("NPROCESSORS_ONLN" . ,(f))
+ ("PHYS_PAGES" . ,(f))
+ ("AVPHYS_PAGES" . ,(f))
+ ("ATEXIT_MAX" . ,(f))
+ ("PASS_MAX" . ,(f))
+ ("XOPEN_VERSION" . ,(f))
+ ("XOPEN_XCU_VERSION" . ,(f))
+ ("XOPEN_UNIX" . ,(f))
+ ("XOPEN_CRYPT" . ,(f))
+ ("XOPEN_ENH_I18N" . ,(f))
+ ("XOPEN_SHM" . ,(f))
+ ("2_CHAR_TERM" . ,(f))
+ ("2_C_VERSION" . ,(f))
+ ("2_UPE" . ,(f))
+ ("XOPEN_XPG2" . ,(f))
+ ("XOPEN_XPG3" . ,(f))
+ ("XOPEN_XPG4" . ,(f))
+ ("CHAR_BIT" . ,(f))
+ ("CHAR_MAX" . ,(f))
+ ("CHAR_MIN" . ,(f))
+ ("INT_MAX" . ,(f))
+ ("INT_MIN" . ,(f))
+ ("LONG_BIT" . ,(f))
+ ("WORD_BIT" . ,(f))
+ ("MB_LEN_MAX" . ,(f))
+ ("NZERO" . ,(f))
+ ("SSIZE_MAX" . ,(f))
+ ("SCHAR_MAX" . ,(f))
+ ("SCHAR_MIN" . ,(f))
+ ("SHRT_MAX" . ,(f))
+ ("SHRT_MIN" . ,(f))
+ ("UCHAR_MAX" . ,(f))
+ ("UINT_MAX" . ,(f))
+ ("ULONG_MAX" . ,(f))
+ ("USHRT_MAX" . ,(f))
+ ("NL_ARGMAX" . ,(f))
+ ("NL_LANGMAX" . ,(f))
+ ("NL_MSGMAX" . ,(f))
+ ("NL_NMAX" . ,(f))
+ ("NL_SETMAX" . ,(f))
+ ("NL_TEXTMAX" . ,(f))
+ ("XBS5_ILP32_OFF32" . ,(f))
+ ("XBS5_ILP32_OFFBIG" . ,(f))
+ ("XBS5_LP64_OFF64" . ,(f))
+ ("XBS5_LPBIG_OFFBIG" . ,(f))
+ ("XOPEN_LEGACY" . ,(f))
+ ("XOPEN_REALTIME" . ,(f))
+ ("XOPEN_REALTIME_THREADS" . ,(f))
+ ("ADVISORY_INFO" . ,(f))
+ ("BARRIERS" . ,(f))
+ ("BASE" . ,(f))
+ ("C_LANG_SUPPORT" . ,(f))
+ ("C_LANG_SUPPORT_R" . ,(f))
+ ("CLOCK_SELECTION" . ,(f))
+ ("CPUTIME" . ,(f))
+ ("THREAD_CPUTIME" . ,(f))
+ ("DEVICE_IO" . ,(f))
+ ("DEVICE_SPECIFIC" . ,(f))
+ ("DEVICE_SPECIFIC_R" . ,(f))
+ ("FD_MGMT" . ,(f))
+ ("FIFO" . ,(f))
+ ("PIPE" . ,(f))
+ ("FILE_ATTRIBUTES" . ,(f))
+ ("FILE_LOCKING" . ,(f))
+ ("FILE_SYSTEM" . ,(f))
+ ("MONOTONIC_CLOCK" . ,(f))
+ ("MULTI_PROCESS" . ,(f))
+ ("SINGLE_PROCESS" . ,(f))
+ ("NETWORKING" . ,(f))
+ ("READER_WRITER_LOCKS" . ,(f))
+ ("SPIN_LOCKS" . ,(f))
+ ("REGEXP" . ,(f))
+ ("REGEX_VERSION" . ,(f))
+ ("SHELL" . ,(f))
+ ("SIGNALS" . ,(f))
+ ("SPAWN" . ,(f))
+ ("SPORADIC_SERVER" . ,(f))
+ ("THREAD_SPORADIC_SERVER" . ,(f))
+ ("SYSTEM_DATABASE" . ,(f))
+ ("SYSTEM_DATABASE_R" . ,(f))
+ ("TIMEOUTS" . ,(f))
+ ("TYPED_MEMORY_OBJECTS" . ,(f))
+ ("USER_GROUPS" . ,(f))
+ ("USER_GROUPS_R" . ,(f))
+ ("2_PBS" . ,(f))
+ ("2_PBS_ACCOUNTING" . ,(f))
+ ("2_PBS_LOCATE" . ,(f))
+ ("2_PBS_MESSAGE" . ,(f))
+ ("2_PBS_TRACK" . ,(f))
+ ("SYMLOOP_MAX" . ,(f))
+ ("STREAMS" . ,(f))
+ ("2_PBS_CHECKPOINT" . ,(f))
+ ("V6_ILP32_OFF32" . ,(f))
+ ("V6_ILP32_OFFBIG" . ,(f))
+ ("V6_LP64_OFF64" . ,(f))
+ ("V6_LPBIG_OFFBIG" . ,(f))
+ ("HOST_NAME_MAX" . ,(f))
+ ("TRACE" . ,(f))
+ ("TRACE_EVENT_FILTER" . ,(f))
+ ("TRACE_INHERIT" . ,(f))
+ ("TRACE_LOG" . ,(f))
+ ("LEVEL1_ICACHE_SIZE" . ,(f))
+ ("LEVEL1_ICACHE_ASSOC" . ,(f))
+ ("LEVEL1_ICACHE_LINESIZE" . ,(f))
+ ("LEVEL1_DCACHE_SIZE" . ,(f))
+ ("LEVEL1_DCACHE_ASSOC" . ,(f))
+ ("LEVEL1_DCACHE_LINESIZE" . ,(f))
+ ("LEVEL2_CACHE_SIZE" . ,(f))
+ ("LEVEL2_CACHE_ASSOC" . ,(f))
+ ("LEVEL2_CACHE_LINESIZE" . ,(f))
+ ("LEVEL3_CACHE_SIZE" . ,(f))
+ ("LEVEL3_CACHE_ASSOC" . ,(f))
+ ("LEVEL3_CACHE_LINESIZE" . ,(f))
+ ("LEVEL4_CACHE_SIZE" . ,(f))
+ ("LEVEL4_CACHE_ASSOC" . ,(f))
+ ("LEVEL4_CACHE_LINESIZE" . ,(f))
+ ("IPV6 = _SC_LEVEL1_ICACHE_SIZE + 50" . ,(f))
+ ("RAW_SOCKETS" . ,(f))
+ ("V7_ILP32_OFF32" . ,(f))
+ ("V7_ILP32_OFFBIG" . ,(f))
+ ("V7_LP64_OFF64" . ,(f))
+ ("V7_LPBIG_OFFBIG" . ,(f))
+ ("SS_REPL_MAX" . ,(f))
+ ("TRACE_EVENT_NAME_MAX" . ,(f))
+ ("TRACE_NAME_MAX" . ,(f))
+ ("TRACE_SYS_MAX" . ,(f))
+ ("TRACE_USER_EVENT_MAX" . ,(f))
+ ("XOPEN_STREAMS" . ,(f))
+ ("THREAD_ROBUST_PRIO_INHERIT" . ,(f))
+ ("THREAD_ROBUST_PRIO_PROTECT" . ,(f)))))
+
+(define (sysconf id)
+ (let ((id2 (if (number? id) id (pylist-ref sysconf_names id))))
+ (if id2
+ (sysconf_ id2)
+ (raise KeyError "no confstr for " id))))
+
+(defineu getloadavg ()
+ (let ((f (pointer->procedure long
+ (dynamic-func "getloadavg"
+ (dynamic-link))
+ (list '* int))))
+ (lambda (id)
+ (let* ((v (make-bytevector (* 3 8)))
+ (vp (bytevector->pointer v)))
+ (rm (f vp 3))
+ (list (bytevector-ieee-double-ref v 0 (native-endianness))
+ (bytevector-ieee-double-ref v 8 (native-endianness))
+ (bytevector-ieee-double-ref v 16 (native-endianness)))))))
+
+(define path "posixpath")
+(define curdir ".")
+(define pardir "..")
+(define sep "/")
+(define extsep ".")
+(define altsep None)
+(define pathsep ":")
+(define linesep "\n")
+(define defpath "/usr/bin/")
+(define devnull "/dev/null")
+
+(define RTLD_LAZY 1)
+(define RTLD_NOW 2)
+(define RTLD_GLOBAL #x100)
+(define RTLD_LOCAL 0)
+(define RTLD_NODELETE #x1000)
+(define RTLD_NOLOAD 4)
+(define RTLD_DEEPBIND 8)
+
+(define GRND_NONBLOCK 1)
+(define GRND_RANDOM 2)
+
+(define* (getrandom_ size #:optional (flags 0))
+ (define filename (if (> (logand flags GRND_RANDOM) 0)
+ "/dev/random"
+ "/dev/urandom"))
+ (define port (open-file filename "r"))
+ (if port
+ (dynamic-wind
+ (lambda () (values))
+ (lambda ()
+ (get-bytevector-n port size))
+ (lambda () ((@ (guile) close) port)))
+ '()))
+
+
+(define (urandom size)
+ (bytes (getrandom_ size)))
+
+(define (getrandom . l)
+ (py-list (apply getrandom_ l)))
+
diff --git a/modules/oop/pf-objects.scm b/modules/oop/pf-objects.scm
index a1a5960..792a89a 100644
--- a/modules/oop/pf-objects.scm
+++ b/modules/oop/pf-objects.scm
@@ -351,7 +351,7 @@ explicitly tell it to not update etc.
(gox xx (mrefx xx key l))
(catch #t
(lambda ()
- (gox xx (f key)))
+ (f xx key))
(lambda x
(gox xx (mrefx xx key l))))))))
@@ -969,17 +969,17 @@ explicitly tell it to not update etc.
(o (make-p <p>)))
(set c '__super__ #t)
(set c '__mro__ parents)
- (set c '__getattribute__ (lambda (self key . l)
- (aif it (ref c key)
- (if (procedure? it)
- (if (eq? (procedure-property
- it
- 'py-special)
- 'class)
- (it cl)
- (it obj))
- it)
- (error "no attribute"))))
+ (set c '__getattribute__ (lambda (self key . l)
+ (aif it (ref c key)
+ (if (procedure? it)
+ (if (eq? (procedure-property
+ it
+ 'py-special)
+ 'class)
+ (it cl)
+ (it obj))
+ it)
+ (error "no attribute"))))
(set o '__class__ c)
o)))