Skip to content

Commit

Permalink
add os.system
Browse files Browse the repository at this point in the history
  • Loading branch information
litlighilit committed Apr 6, 2024
1 parent 09a4386 commit 60648a2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/pylib/Lib/os.nim
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@

## see docs.python.org/3/library/os.html
##
## Also export everything of std/os
##
## as the following will fail to compile:
##
## ```Nim
## import std/os
## import pylib/Lib/os
## os.path.join ...
## ```
##
## not completed yet

import std/os
export os

import ./os_impl/[consts, posix_like, utils, path]
export consts, posix_like, utils, path



## see docs.python.org/3/library/os.html
##
## Also export everything of std/os
##
## as the following will fail to compile:
##
## ```Nim
## import std/os
## import pylib/Lib/os
## os.path.join ...
## ```
##
## not completed yet

import std/os
export os

import ./os_impl/[consts, posix_like, subp, utils, path]
export consts, posix_like, subp, utils, path


26 changes: 26 additions & 0 deletions src/pylib/Lib/os_impl/subp.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@


when not defined(js):
proc c_system(cmd: cstring): cint{.importc: "system", header: "<stdlib.h>".}

func system*(cmd: string): int{.discardable.} =
## os.system
runnableExamples:
const
# can use `os.devnull`
mydevnull = when defined(windows): "nul" else: "/dev/null"
e2null = "echo 1 >" & mydevnull
assert system(e2null) == 0

when defined(js):
let jsStr = cmd.cstring
var res: c_int
asm """
const {exec} = require('node:child_process');
let childProcess = exec(`jsStr`);
`res` = childProcess.exitCode;
"""
result = res.int
else:
c_system cmd.cstring

0 comments on commit 60648a2

Please sign in to comment.