From 4524db4d404e3b113b3aa9bbff231569277d49cd Mon Sep 17 00:00:00 2001 From: flywind Date: Mon, 24 Jan 2022 14:23:58 +0800 Subject: [PATCH 01/12] move io out of system --- lib/{system/io.nim => std/sysios.nim} | 0 lib/system.nim | 7 +++++-- 2 files changed, 5 insertions(+), 2 deletions(-) rename lib/{system/io.nim => std/sysios.nim} (100%) diff --git a/lib/system/io.nim b/lib/std/sysios.nim similarity index 100% rename from lib/system/io.nim rename to lib/std/sysios.nim diff --git a/lib/system.nim b/lib/system.nim index c424cbc1b1c0f..df6849ba1bf01 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -3129,8 +3129,11 @@ when defined(genode): import system/widestrs export widestrs -import system/io -export io +when not defined(nimPreviewSlimSystem): + {.deprecated: """io is about to move out of system; use `-d:nimPreviewSlimSystem` and + import `std/sysios`.""".} + import std/sysios + export sysios when not defined(createNimHcr) and not defined(nimscript): include nimhcr From 3f78d2f785470621a21d74e0a0ee6aaef1be4e18 Mon Sep 17 00:00:00 2001 From: flywind Date: Mon, 24 Jan 2022 14:28:51 +0800 Subject: [PATCH 02/12] fix tests --- lib/std/sysios.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/std/sysios.nim b/lib/std/sysios.nim index 04a43328d5b22..d7a374e15f878 100644 --- a/lib/std/sysios.nim +++ b/lib/std/sysios.nim @@ -10,9 +10,9 @@ ## This is a part of `system.nim`, you should not manually import it. -include inclrtl +include system/inclrtl import std/private/since -import formatfloat +import system/formatfloat # ----------------- IO Part ------------------------------------------------ type From 1be97a0c6dc5a275f1b0e71fd7e298db61a55731 Mon Sep 17 00:00:00 2001 From: flywind Date: Mon, 24 Jan 2022 14:55:52 +0800 Subject: [PATCH 03/12] fix tests --- compiler/vmops.nim | 2 +- tools/kochdocs.nim | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/vmops.nim b/compiler/vmops.nim index 018e7b9c621ab..15dc4cd4cc93b 100644 --- a/compiler/vmops.nim +++ b/compiler/vmops.nim @@ -47,7 +47,7 @@ template systemop(op) {.dirty.} = registerCallback(c, "stdlib.system." & astToStr(op), `op Wrapper`) template ioop(op) {.dirty.} = - registerCallback(c, "stdlib.io." & astToStr(op), `op Wrapper`) + registerCallback(c, "stdlib.sysios." & astToStr(op), `op Wrapper`) template macrosop(op) {.dirty.} = registerCallback(c, "stdlib.macros." & astToStr(op), `op Wrapper`) diff --git a/tools/kochdocs.nim b/tools/kochdocs.nim index 4fbea8c77e4fb..8fd9c84a5d35d 100644 --- a/tools/kochdocs.nim +++ b/tools/kochdocs.nim @@ -187,7 +187,6 @@ proc getDocList(): seq[string] = # don't ignore these even though in lib/system (not include files) const goodSystem = """ -lib/system/io.nim lib/system/nimscript.nim lib/system/assertions.nim lib/system/iterators.nim From 788d7d9adc749f5c1415a39cdd891dca36bf4cd7 Mon Sep 17 00:00:00 2001 From: flywind Date: Mon, 24 Jan 2022 20:45:49 +0800 Subject: [PATCH 04/12] next step --- compiler/vmops.nim | 2 +- doc/tut1.rst | 8 +++--- lib/pure/htmlparser.nim | 3 +++ lib/pure/json.nim | 3 +++ lib/pure/logging.nim | 11 +++++--- lib/pure/memfiles.nim | 4 +-- lib/pure/os.nim | 3 +++ lib/pure/parsecfg.nim | 4 +++ lib/pure/parsecsv.nim | 3 +++ lib/pure/pegs.nim | 2 ++ lib/pure/ropes.nim | 3 +++ lib/pure/streams.nim | 9 ++++--- lib/pure/terminal.nim | 3 +++ lib/pure/xmlparser.nim | 3 +++ lib/std/{sysios.nim => ioutils.nim} | 40 ++++++++++++++--------------- lib/system.nim | 8 +++--- lib/system_overview.rst | 1 - lib/windows/winlean.nim | 3 +++ 18 files changed, 73 insertions(+), 40 deletions(-) rename lib/std/{sysios.nim => ioutils.nim} (96%) diff --git a/compiler/vmops.nim b/compiler/vmops.nim index 15dc4cd4cc93b..f53fad3438c8a 100644 --- a/compiler/vmops.nim +++ b/compiler/vmops.nim @@ -47,7 +47,7 @@ template systemop(op) {.dirty.} = registerCallback(c, "stdlib.system." & astToStr(op), `op Wrapper`) template ioop(op) {.dirty.} = - registerCallback(c, "stdlib.sysios." & astToStr(op), `op Wrapper`) + registerCallback(c, "stdlib.ioutils." & astToStr(op), `op Wrapper`) template macrosop(op) {.dirty.} = registerCallback(c, "stdlib.macros." & astToStr(op), `op Wrapper`) diff --git a/doc/tut1.rst b/doc/tut1.rst index 3107aef4557d7..ec42a569b753e 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -84,8 +84,8 @@ done with spaces only, tabulators are not allowed. String literals are enclosed in double-quotes. The `var` statement declares a new variable named `name` of type `string` with the value that is -returned by the `readLine `_ procedure. Since the -compiler knows that `readLine `_ returns a string, +returned by the `readLine `_ procedure. Since the +compiler knows that `readLine `_ returns a string, you can leave out the type in the declaration (this is called `local type inference`:idx:). So this will work too: @@ -97,7 +97,7 @@ Note that this is basically the only form of type inference that exists in Nim: it is a good compromise between brevity and readability. The "hello world" program contains several identifiers that are already known -to the compiler: `echo`, `readLine `_, etc. +to the compiler: `echo`, `readLine `_, etc. These built-ins are declared in the system_ module which is implicitly imported by any other module. @@ -594,7 +594,7 @@ Procedures ========== To define new commands like `echo `_ -and `readLine `_ in the examples, the concept of a +and `readLine `_ in the examples, the concept of a *procedure* is needed. You might be used to them being called *methods* or *functions* in other languages, but Nim `differentiates these concepts `_. In diff --git a/lib/pure/htmlparser.nim b/lib/pure/htmlparser.nim index 21dff69ff3f78..2c758552425b6 100644 --- a/lib/pure/htmlparser.nim +++ b/lib/pure/htmlparser.nim @@ -51,6 +51,9 @@ import strutils, streams, parsexml, xmltree, unicode, strtabs +when defined(nimPreviewSlimSystem): + import std/ioutils + type HtmlTag* = enum ## list of all supported HTML tags; order will always be ## alphabetically diff --git a/lib/pure/json.nim b/lib/pure/json.nim index 922cd4e2f756c..ee1c82dba44c4 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -164,6 +164,9 @@ import hashes, tables, strutils, lexbase, streams, macros, parsejson import options # xxx remove this dependency using same approach as https://github.com/nim-lang/Nim/pull/14563 import std/private/since +when defined(nimPreviewSlimSystem): + import std/ioutils + export tables.`$` diff --git a/lib/pure/logging.nim b/lib/pure/logging.nim index b2ace79ab74ce..ca91a26082ecc 100644 --- a/lib/pure/logging.nim +++ b/lib/pure/logging.nim @@ -48,7 +48,7 @@ ## .. warning:: ## For loggers that log to a console or to files, only error and fatal ## messages will cause their output buffers to be flushed immediately. -## Use the `flushFile proc `_ to flush the buffer +## Use the `flushFile proc `_ to flush the buffer ## manually if needed. ## ## Handlers @@ -146,6 +146,9 @@ import strutils, times when not defined(js): import os +when defined(nimPreviewSlimSystem): + import std/ioutils + type Level* = enum ## \ ## Enumeration of logging levels. @@ -346,7 +349,7 @@ method log*(logger: ConsoleLogger, level: Level, args: varargs[string, `$`]) = ## ## **Note:** Only error and fatal messages will cause the output buffer ## to be flushed immediately. Use the `flushFile proc - ## `_ to flush the buffer manually if needed. + ## `_ to flush the buffer manually if needed. ## ## See also: ## * `log method<#log.e,FileLogger,Level,varargs[string,]>`_ @@ -422,7 +425,7 @@ when not defined(js): ## **Notes:** ## * Only error and fatal messages will cause the output buffer ## to be flushed immediately. Use the `flushFile proc - ## `_ to flush the buffer manually if needed. + ## `_ to flush the buffer manually if needed. ## * This method is not available for the JavaScript backend. ## ## See also: @@ -600,7 +603,7 @@ when not defined(js): ## **Notes:** ## * Only error and fatal messages will cause the output buffer ## to be flushed immediately. Use the `flushFile proc - ## `_ to flush the buffer manually if needed. + ## `_ to flush the buffer manually if needed. ## * This method is not available for the JavaScript backend. ## ## See also: diff --git a/lib/pure/memfiles.nim b/lib/pure/memfiles.nim index 407a358faaf8b..2ff8149420e61 100644 --- a/lib/pure/memfiles.nim +++ b/lib/pure/memfiles.nim @@ -431,7 +431,7 @@ iterator memSlices*(mfile: MemFile, delim = '\l', eat = '\r'): MemSlice {.inline iterator lines*(mfile: MemFile, buf: var string, delim = '\l', eat = '\r'): string {.inline.} = ## Replace contents of passed buffer with each new line, like - ## `readLine(File) `_. + ## `readLine(File) `_. ## `delim`, `eat`, and delimiting logic is exactly as for `memSlices ## <#memSlices.i,MemFile,char,char>`_, but Nim strings are returned. ## @@ -450,7 +450,7 @@ iterator lines*(mfile: MemFile, buf: var string, delim = '\l', iterator lines*(mfile: MemFile, delim = '\l', eat = '\r'): string {.inline.} = ## Return each line in a file as a Nim string, like - ## `lines(File) `_. + ## `lines(File) `_. ## `delim`, `eat`, and delimiting logic is exactly as for `memSlices ## <#memSlices.i,MemFile,char,char>`_, but Nim strings are returned. ## diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 81b432d7f42c8..930bc8edfa970 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -34,6 +34,9 @@ import std/private/since import strutils, pathnorm +when defined(nimPreviewSlimSystem): + import std/ioutils + const weirdTarget = defined(nimscript) or defined(js) since (1, 1): diff --git a/lib/pure/parsecfg.nim b/lib/pure/parsecfg.nim index 0ee19912caba5..9e9b7ea205534 100644 --- a/lib/pure/parsecfg.nim +++ b/lib/pure/parsecfg.nim @@ -175,8 +175,12 @@ import strutils, lexbase, streams, tables import std/private/decode_helpers import std/private/since +when defined(nimPreviewSlimSystem): + import std/ioutils + include "system/inclrtl" + type CfgEventKind* = enum ## enumeration of all events that may occur when parsing cfgEof, ## end of file reached diff --git a/lib/pure/parsecsv.nim b/lib/pure/parsecsv.nim index 6db7946824f19..ad5faad71cccb 100644 --- a/lib/pure/parsecsv.nim +++ b/lib/pure/parsecsv.nim @@ -67,6 +67,9 @@ import lexbase, streams +when defined(nimPreviewSlimSystem): + import std/ioutils + type CsvRow* = seq[string] ## A row in a CSV file. CsvParser* = object of BaseLexer ## The parser object. diff --git a/lib/pure/pegs.nim b/lib/pure/pegs.nim index bac8b1a0e9d4e..2d9b4bed689b4 100644 --- a/lib/pure/pegs.nim +++ b/lib/pure/pegs.nim @@ -16,6 +16,8 @@ ## include "system/inclrtl" +when defined(nimPreviewSlimSystem): + import std/ioutils const useUnicode = true ## change this to deactivate proper UTF-8 support diff --git a/lib/pure/ropes.nim b/lib/pure/ropes.nim index 1300b4479f879..ade003dcd738e 100644 --- a/lib/pure/ropes.nim +++ b/lib/pure/ropes.nim @@ -19,6 +19,9 @@ include system/inclrtl import streams +when defined(nimPreviewSlimSystem): + import std/ioutils + {.push debugger: off.} # the user does not want to trace a part # of the standard library! diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim index 7dc81148f6868..ec85aa872c7d6 100644 --- a/lib/pure/streams.nim +++ b/lib/pure/streams.nim @@ -92,10 +92,13 @@ ## See also ## ======== ## * `asyncstreams module `_ -## * `io module `_ for `FileMode enum `_ +## * `io module `_ for `FileMode enum `_ import std/private/since +when defined(nimPreviewSlimSystem): + import std/ioutils + proc newEIO(msg: string): owned(ref IOError) = new(result) result.msg = msg @@ -1331,7 +1334,7 @@ proc newFileStream*(f: File): owned FileStream = ## * `newStringStream proc <#newStringStream,string>`_ creates a new stream ## from string. ## * `newFileStream proc <#newFileStream,string,FileMode,int>`_ is the same - ## as using `open proc `_ + ## as using `open proc `_ ## on Examples. ## * `openFileStream proc <#openFileStream,string,FileMode,int>`_ creates a ## file stream from the file name and the mode. @@ -1370,7 +1373,7 @@ proc newFileStream*(filename: string, mode: FileMode = fmRead, ## Creates a new stream from the file named `filename` with the mode `mode`. ## ## If the file cannot be opened, `nil` is returned. See the `io module - ## `_ for a list of available `FileMode enums `_. + ## `_ for a list of available `FileMode enums `_. ## ## **Note:** ## * **This function returns nil in case of failure.** diff --git a/lib/pure/terminal.nim b/lib/pure/terminal.nim index c9aafc037f808..c1b8d0e2bc349 100644 --- a/lib/pure/terminal.nim +++ b/lib/pure/terminal.nim @@ -66,6 +66,9 @@ import colors when defined(windows): import winlean +when defined(nimPreviewSlimSystem): + import std/ioutils + type PTerminal = ref object trueColorIsSupported: bool diff --git a/lib/pure/xmlparser.nim b/lib/pure/xmlparser.nim index 3d9c288eded95..50aa54133bf09 100644 --- a/lib/pure/xmlparser.nim +++ b/lib/pure/xmlparser.nim @@ -11,6 +11,9 @@ import streams, parsexml, strtabs, xmltree +when defined(nimPreviewSlimSystem): + import std/ioutils + type XmlError* = object of ValueError ## Exception that is raised ## for invalid XML. diff --git a/lib/std/sysios.nim b/lib/std/ioutils.nim similarity index 96% rename from lib/std/sysios.nim rename to lib/std/ioutils.nim index d7a374e15f878..580710def500e 100644 --- a/lib/std/sysios.nim +++ b/lib/std/ioutils.nim @@ -1,14 +1,12 @@ # # # Nim's Runtime Library -# (c) Copyright 2019 Nim contributors +# (c) Copyright 2022 Nim contributors # # See the file "copying.txt", included in this # distribution, for details about the copyright. # -## This is a part of `system.nim`, you should not manually import it. - include system/inclrtl import std/private/since @@ -179,7 +177,7 @@ proc checkErr(f: File) = {.push stackTrace: off, profiler: off.} proc readBuffer*(f: File, buffer: pointer, len: Natural): int {. tags: [ReadIOEffect], benign.} = - ## reads `len` bytes into the buffer pointed to by `buffer`. Returns + ## Reads `len` bytes into the buffer pointed to by `buffer`. Returns ## the actual number of bytes that have been read which may be less than ## `len` (if not as many bytes are remaining), but not greater. result = cast[int](c_fread(buffer, 1, cast[csize_t](len), f)) @@ -188,13 +186,13 @@ proc readBuffer*(f: File, buffer: pointer, len: Natural): int {. proc readBytes*(f: File, a: var openArray[int8|uint8], start, len: Natural): int {. tags: [ReadIOEffect], benign.} = - ## reads `len` bytes into the buffer `a` starting at `a[start]`. Returns + ## Reads `len` bytes into the buffer `a` starting at `a[start]`. Returns ## the actual number of bytes that have been read which may be less than ## `len` (if not as many bytes are remaining), but not greater. result = readBuffer(f, addr(a[start]), len) proc readChars*(f: File, a: var openArray[char]): int {.tags: [ReadIOEffect], benign.} = - ## reads up to `a.len` bytes into the buffer `a`. Returns + ## Reads up to `a.len` bytes into the buffer `a`. Returns ## the actual number of bytes that have been read which may be less than ## `a.len` (if not as many bytes are remaining), but not greater. result = readBuffer(f, addr(a[0]), a.len) @@ -202,7 +200,7 @@ proc readChars*(f: File, a: var openArray[char]): int {.tags: [ReadIOEffect], be proc readChars*(f: File, a: var openArray[char], start, len: Natural): int {. tags: [ReadIOEffect], benign, deprecated: "use other `readChars` overload, possibly via: readChars(toOpenArray(buf, start, len-1))".} = - ## reads `len` bytes into the buffer `a` starting at `a[start]`. Returns + ## Reads `len` bytes into the buffer `a` starting at `a[start]`. Returns ## the actual number of bytes that have been read which may be less than ## `len` (if not as many bytes are remaining), but not greater. if (start + len) > len(a): @@ -216,7 +214,7 @@ proc write*(f: File, c: cstring) {.tags: [WriteIOEffect], benign.} = proc writeBuffer*(f: File, buffer: pointer, len: Natural): int {. tags: [WriteIOEffect], benign.} = - ## writes the bytes of buffer pointed to by the parameter `buffer` to the + ## Writes the bytes of buffer pointed to by the parameter `buffer` to the ## file `f`. Returns the number of actual written bytes, which may be less ## than `len` in case of an error. result = cast[int](c_fwrite(buffer, 1, cast[csize_t](len), f)) @@ -224,7 +222,7 @@ proc writeBuffer*(f: File, buffer: pointer, len: Natural): int {. proc writeBytes*(f: File, a: openArray[int8|uint8], start, len: Natural): int {. tags: [WriteIOEffect], benign.} = - ## writes the bytes of `a[start..start+len-1]` to the file `f`. Returns + ## Writes the bytes of `a[start..start+len-1]` to the file `f`. Returns ## the number of actual written bytes, which may be less than `len` in case ## of an error. var x = cast[ptr UncheckedArray[int8]](a) @@ -232,7 +230,7 @@ proc writeBytes*(f: File, a: openArray[int8|uint8], start, len: Natural): int {. proc writeChars*(f: File, a: openArray[char], start, len: Natural): int {. tags: [WriteIOEffect], benign.} = - ## writes the bytes of `a[start..start+len-1]` to the file `f`. Returns + ## Writes the bytes of `a[start..start+len-1]` to the file `f`. Returns ## the number of actual written bytes, which may be less than `len` in case ## of an error. var x = cast[ptr UncheckedArray[int8]](a) @@ -341,7 +339,7 @@ proc flushFile*(f: File) {.tags: [WriteIOEffect].} = discard c_fflush(f) proc getFileHandle*(f: File): FileHandle = - ## returns the file handle of the file `f`. This is only useful for + ## Returns the file handle of the file `f`. This is only useful for ## platform specific programming. ## Note that on Windows this doesn't return the Windows-specific handle, ## but the C library's notion of a handle, whatever that means. @@ -349,7 +347,7 @@ proc getFileHandle*(f: File): FileHandle = c_fileno(f) proc getOsFileHandle*(f: File): FileHandle = - ## returns the OS file handle of the file `f`. This is only useful for + ## Returns the OS file handle of the file `f`. This is only useful for ## platform specific programming. when defined(windows): result = FileHandle getOsfhandle(cint getFileHandle(f)) @@ -380,7 +378,7 @@ when defined(nimdoc) or (defined(posix) and not defined(nimscript)) or defined(w proc readLine*(f: File, line: var string): bool {.tags: [ReadIOEffect], benign.} = - ## reads a line of text from the file `f` into `line`. May throw an IO + ## Reads a line of text from the file `f` into `line`. May throw an IO ## exception. ## A line of text may be delimited by `LF` or `CRLF`. The newline ## character(s) are not part of the returned string. Returns `false` @@ -500,7 +498,7 @@ proc readLine*(f: File, line: var string): bool {.tags: [ReadIOEffect], line.setLen(pos+sp) proc readLine*(f: File): string {.tags: [ReadIOEffect], benign.} = - ## reads a line of text from the file `f`. May throw an IO exception. + ## Reads a line of text from the file `f`. May throw an IO exception. ## A line of text may be delimited by `LF` or `CRLF`. The newline ## character(s) are not part of the returned string. result = newStringOfCap(80) @@ -602,7 +600,7 @@ proc readAll*(file: File): string {.tags: [ReadIOEffect], benign.} = proc writeLine*[Ty](f: File, x: varargs[Ty, `$`]) {.inline, tags: [WriteIOEffect], benign.} = - ## writes the values `x` to `f` and then writes "\\n". + ## Writes the values `x` to `f` and then writes "\\n". ## May throw an IO exception. for i in items(x): write(f, i) @@ -723,7 +721,7 @@ proc open*(f: var File, filename: string, proc reopen*(f: File, filename: string, mode: FileMode = fmRead): bool {. tags: [], benign.} = - ## reopens the file `f` with given `filename` and `mode`. This + ## Reopens the file `f` with given `filename` and `mode`. This ## is often used to redirect the `stdin`, `stdout` or `stderr` ## file variables. ## @@ -765,19 +763,19 @@ proc open*(filename: string, sysFatal(IOError, "cannot open: " & filename) proc setFilePos*(f: File, pos: int64, relativeTo: FileSeekPos = fspSet) {.benign.} = - ## sets the position of the file pointer that is used for read/write + ## Sets the position of the file pointer that is used for read/write ## operations. The file's first byte has the index zero. if c_fseek(f, pos, cint(relativeTo)) != 0: raiseEIO("cannot set file position") proc getFilePos*(f: File): int64 {.benign.} = - ## retrieves the current position of the file pointer that is used to + ## Retrieves the current position of the file pointer that is used to ## read from the file `f`. The file's first byte has the index zero. result = c_ftell(f) if result < 0: raiseEIO("cannot retrieve file position") proc getFileSize*(f: File): int64 {.tags: [ReadIOEffect], benign.} = - ## retrieves the file size (in bytes) of `f`. + ## Retrieves the file size (in bytes) of `f`. let oldPos = getFilePos(f) discard c_fseek(f, 0, 2) # seek the end of the file result = getFilePos(f) @@ -906,7 +904,7 @@ proc writeFile*(filename: string, content: openArray[byte]) {.since: (1, 1).} = raise newException(IOError, "cannot open: " & filename) proc readLines*(filename: string, n: Natural): seq[string] = - ## read `n` lines from the file named `filename`. Raises an IO exception + ## Reads `n` lines from the file named `filename`. Raises an IO exception ## in case of an error. Raises EOF if file does not contain at least `n` lines. ## Available at compile time. A line of text may be delimited by `LF` or `CRLF`. ## The newline character(s) are not part of the returned strings. @@ -948,7 +946,7 @@ iterator lines*(filename: string): string {.tags: [ReadIOEffect].} = close(f) iterator lines*(f: File): string {.tags: [ReadIOEffect].} = - ## Iterate over any line in the file `f`. + ## Iterates over any line in the file `f`. ## ## The trailing newline character(s) are removed from the iterated lines. ## diff --git a/lib/system.nim b/lib/system.nim index 3b71a14bdf974..8796ed6da831e 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2680,7 +2680,7 @@ proc slurp*(filename: string): string {.magic: "Slurp".} ## This is an alias for `staticRead <#staticRead,string>`_. proc staticRead*(filename: string): string {.magic: "Slurp".} - ## Compile-time `readFile `_ proc for easy + ## Compile-time `readFile `_ proc for easy ## `resource`:idx: embedding: ## ## The maximum file size limit that `staticRead` and `slurp` can read is @@ -3152,9 +3152,9 @@ export widestrs when not defined(nimPreviewSlimSystem): {.deprecated: """io is about to move out of system; use `-d:nimPreviewSlimSystem` and - import `std/sysios`.""".} - import std/sysios - export sysios + import `std/ioutils`.""".} + import std/ioutils + export ioutils when not defined(createNimHcr) and not defined(nimscript): include nimhcr diff --git a/lib/system_overview.rst b/lib/system_overview.rst index 0e4ffaf17c2cf..768fdcd6d82bc 100644 --- a/lib/system_overview.rst +++ b/lib/system_overview.rst @@ -7,7 +7,6 @@ is in separate files: * `iterators `_ * `assertions `_ * `dollars `_ -* `io `_ * `widestrs `_ diff --git a/lib/windows/winlean.nim b/lib/windows/winlean.nim index 6e626e4bacd80..6093e3faf6806 100644 --- a/lib/windows/winlean.nim +++ b/lib/windows/winlean.nim @@ -17,6 +17,9 @@ when defined(nimHasStyleChecks): {.passc: "-DWIN32_LEAN_AND_MEAN".} +when defined(nimPreviewSlimSystem): + from std/ioutils import FileHandle + const useWinUnicode* = not defined(useWinAnsi) From d511071d14dc77081f4e898c1902cbeaef0680ff Mon Sep 17 00:00:00 2001 From: flywind Date: Fri, 28 Jan 2022 17:53:31 +0800 Subject: [PATCH 05/12] rename to syncio --- changelog.md | 2 ++ compiler/vmops.nim | 2 +- doc/tut1.rst | 8 ++++---- lib/pure/htmlparser.nim | 2 +- lib/pure/json.nim | 2 +- lib/pure/logging.nim | 10 +++++----- lib/pure/memfiles.nim | 4 ++-- lib/pure/os.nim | 2 +- lib/pure/parsecfg.nim | 2 +- lib/pure/parsecsv.nim | 2 +- lib/pure/pegs.nim | 2 +- lib/pure/ropes.nim | 2 +- lib/pure/streams.nim | 8 ++++---- lib/pure/terminal.nim | 2 +- lib/pure/xmlparser.nim | 2 +- lib/system.nim | 8 ++++---- lib/windows/winlean.nim | 2 +- tests/config.nims | 1 + 18 files changed, 33 insertions(+), 30 deletions(-) diff --git a/changelog.md b/changelog.md index bbbaf6e97e527..412e046ebe73d 100644 --- a/changelog.md +++ b/changelog.md @@ -20,6 +20,8 @@ - `addr` is now available for all addressable locations, `unsafeAddr` is deprecated and becomes an alias for `addr`. +- io is about to move out of system; use `-d:nimPreviewSlimSystem` and import `std/syncio`. + ## Standard library additions and changes - `macros.parseExpr` and `macros.parseStmt` now accept an optional diff --git a/compiler/vmops.nim b/compiler/vmops.nim index f53fad3438c8a..6b06cc68d5732 100644 --- a/compiler/vmops.nim +++ b/compiler/vmops.nim @@ -47,7 +47,7 @@ template systemop(op) {.dirty.} = registerCallback(c, "stdlib.system." & astToStr(op), `op Wrapper`) template ioop(op) {.dirty.} = - registerCallback(c, "stdlib.ioutils." & astToStr(op), `op Wrapper`) + registerCallback(c, "stdlib.syncio." & astToStr(op), `op Wrapper`) template macrosop(op) {.dirty.} = registerCallback(c, "stdlib.macros." & astToStr(op), `op Wrapper`) diff --git a/doc/tut1.rst b/doc/tut1.rst index ec42a569b753e..405df57b1ddce 100644 --- a/doc/tut1.rst +++ b/doc/tut1.rst @@ -84,8 +84,8 @@ done with spaces only, tabulators are not allowed. String literals are enclosed in double-quotes. The `var` statement declares a new variable named `name` of type `string` with the value that is -returned by the `readLine `_ procedure. Since the -compiler knows that `readLine `_ returns a string, +returned by the `readLine `_ procedure. Since the +compiler knows that `readLine `_ returns a string, you can leave out the type in the declaration (this is called `local type inference`:idx:). So this will work too: @@ -97,7 +97,7 @@ Note that this is basically the only form of type inference that exists in Nim: it is a good compromise between brevity and readability. The "hello world" program contains several identifiers that are already known -to the compiler: `echo`, `readLine `_, etc. +to the compiler: `echo`, `readLine `_, etc. These built-ins are declared in the system_ module which is implicitly imported by any other module. @@ -594,7 +594,7 @@ Procedures ========== To define new commands like `echo `_ -and `readLine `_ in the examples, the concept of a +and `readLine `_ in the examples, the concept of a *procedure* is needed. You might be used to them being called *methods* or *functions* in other languages, but Nim `differentiates these concepts `_. In diff --git a/lib/pure/htmlparser.nim b/lib/pure/htmlparser.nim index 2c758552425b6..6b1300f11bf17 100644 --- a/lib/pure/htmlparser.nim +++ b/lib/pure/htmlparser.nim @@ -52,7 +52,7 @@ import strutils, streams, parsexml, xmltree, unicode, strtabs when defined(nimPreviewSlimSystem): - import std/ioutils + import std/syncio type HtmlTag* = enum ## list of all supported HTML tags; order will always be diff --git a/lib/pure/json.nim b/lib/pure/json.nim index ee1c82dba44c4..bdc9fe5ab625b 100644 --- a/lib/pure/json.nim +++ b/lib/pure/json.nim @@ -165,7 +165,7 @@ import options # xxx remove this dependency using same approach as https://githu import std/private/since when defined(nimPreviewSlimSystem): - import std/ioutils + import std/syncio export tables.`$` diff --git a/lib/pure/logging.nim b/lib/pure/logging.nim index ca91a26082ecc..6751a372a8543 100644 --- a/lib/pure/logging.nim +++ b/lib/pure/logging.nim @@ -48,7 +48,7 @@ ## .. warning:: ## For loggers that log to a console or to files, only error and fatal ## messages will cause their output buffers to be flushed immediately. -## Use the `flushFile proc `_ to flush the buffer +## Use the `flushFile proc `_ to flush the buffer ## manually if needed. ## ## Handlers @@ -147,7 +147,7 @@ when not defined(js): import os when defined(nimPreviewSlimSystem): - import std/ioutils + import std/syncio type Level* = enum ## \ @@ -349,7 +349,7 @@ method log*(logger: ConsoleLogger, level: Level, args: varargs[string, `$`]) = ## ## **Note:** Only error and fatal messages will cause the output buffer ## to be flushed immediately. Use the `flushFile proc - ## `_ to flush the buffer manually if needed. + ## `_ to flush the buffer manually if needed. ## ## See also: ## * `log method<#log.e,FileLogger,Level,varargs[string,]>`_ @@ -425,7 +425,7 @@ when not defined(js): ## **Notes:** ## * Only error and fatal messages will cause the output buffer ## to be flushed immediately. Use the `flushFile proc - ## `_ to flush the buffer manually if needed. + ## `_ to flush the buffer manually if needed. ## * This method is not available for the JavaScript backend. ## ## See also: @@ -603,7 +603,7 @@ when not defined(js): ## **Notes:** ## * Only error and fatal messages will cause the output buffer ## to be flushed immediately. Use the `flushFile proc - ## `_ to flush the buffer manually if needed. + ## `_ to flush the buffer manually if needed. ## * This method is not available for the JavaScript backend. ## ## See also: diff --git a/lib/pure/memfiles.nim b/lib/pure/memfiles.nim index 2ff8149420e61..f65ca125e11f5 100644 --- a/lib/pure/memfiles.nim +++ b/lib/pure/memfiles.nim @@ -431,7 +431,7 @@ iterator memSlices*(mfile: MemFile, delim = '\l', eat = '\r'): MemSlice {.inline iterator lines*(mfile: MemFile, buf: var string, delim = '\l', eat = '\r'): string {.inline.} = ## Replace contents of passed buffer with each new line, like - ## `readLine(File) `_. + ## `readLine(File) `_. ## `delim`, `eat`, and delimiting logic is exactly as for `memSlices ## <#memSlices.i,MemFile,char,char>`_, but Nim strings are returned. ## @@ -450,7 +450,7 @@ iterator lines*(mfile: MemFile, buf: var string, delim = '\l', iterator lines*(mfile: MemFile, delim = '\l', eat = '\r'): string {.inline.} = ## Return each line in a file as a Nim string, like - ## `lines(File) `_. + ## `lines(File) `_. ## `delim`, `eat`, and delimiting logic is exactly as for `memSlices ## <#memSlices.i,MemFile,char,char>`_, but Nim strings are returned. ## diff --git a/lib/pure/os.nim b/lib/pure/os.nim index 930bc8edfa970..08ca404d82133 100644 --- a/lib/pure/os.nim +++ b/lib/pure/os.nim @@ -35,7 +35,7 @@ import std/private/since import strutils, pathnorm when defined(nimPreviewSlimSystem): - import std/ioutils + import std/syncio const weirdTarget = defined(nimscript) or defined(js) diff --git a/lib/pure/parsecfg.nim b/lib/pure/parsecfg.nim index 9e9b7ea205534..54584a2536547 100644 --- a/lib/pure/parsecfg.nim +++ b/lib/pure/parsecfg.nim @@ -176,7 +176,7 @@ import std/private/decode_helpers import std/private/since when defined(nimPreviewSlimSystem): - import std/ioutils + import std/syncio include "system/inclrtl" diff --git a/lib/pure/parsecsv.nim b/lib/pure/parsecsv.nim index ad5faad71cccb..a8d1cfaabc34e 100644 --- a/lib/pure/parsecsv.nim +++ b/lib/pure/parsecsv.nim @@ -68,7 +68,7 @@ import lexbase, streams when defined(nimPreviewSlimSystem): - import std/ioutils + import std/syncio type CsvRow* = seq[string] ## A row in a CSV file. diff --git a/lib/pure/pegs.nim b/lib/pure/pegs.nim index 2d9b4bed689b4..0eed1c3881dce 100644 --- a/lib/pure/pegs.nim +++ b/lib/pure/pegs.nim @@ -17,7 +17,7 @@ include "system/inclrtl" when defined(nimPreviewSlimSystem): - import std/ioutils + import std/syncio const useUnicode = true ## change this to deactivate proper UTF-8 support diff --git a/lib/pure/ropes.nim b/lib/pure/ropes.nim index ade003dcd738e..84b5b47c2f8c2 100644 --- a/lib/pure/ropes.nim +++ b/lib/pure/ropes.nim @@ -20,7 +20,7 @@ include system/inclrtl import streams when defined(nimPreviewSlimSystem): - import std/ioutils + import std/syncio {.push debugger: off.} # the user does not want to trace a part # of the standard library! diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim index ec85aa872c7d6..7ad81685fad51 100644 --- a/lib/pure/streams.nim +++ b/lib/pure/streams.nim @@ -92,12 +92,12 @@ ## See also ## ======== ## * `asyncstreams module `_ -## * `io module `_ for `FileMode enum `_ +## * `io module `_ for `FileMode enum `_ import std/private/since when defined(nimPreviewSlimSystem): - import std/ioutils + import std/syncio proc newEIO(msg: string): owned(ref IOError) = new(result) @@ -1334,7 +1334,7 @@ proc newFileStream*(f: File): owned FileStream = ## * `newStringStream proc <#newStringStream,string>`_ creates a new stream ## from string. ## * `newFileStream proc <#newFileStream,string,FileMode,int>`_ is the same - ## as using `open proc `_ + ## as using `open proc `_ ## on Examples. ## * `openFileStream proc <#openFileStream,string,FileMode,int>`_ creates a ## file stream from the file name and the mode. @@ -1373,7 +1373,7 @@ proc newFileStream*(filename: string, mode: FileMode = fmRead, ## Creates a new stream from the file named `filename` with the mode `mode`. ## ## If the file cannot be opened, `nil` is returned. See the `io module - ## `_ for a list of available `FileMode enums `_. + ## `_ for a list of available `FileMode enums `_. ## ## **Note:** ## * **This function returns nil in case of failure.** diff --git a/lib/pure/terminal.nim b/lib/pure/terminal.nim index c1b8d0e2bc349..5755e142ac024 100644 --- a/lib/pure/terminal.nim +++ b/lib/pure/terminal.nim @@ -67,7 +67,7 @@ when defined(windows): import winlean when defined(nimPreviewSlimSystem): - import std/ioutils + import std/syncio type PTerminal = ref object diff --git a/lib/pure/xmlparser.nim b/lib/pure/xmlparser.nim index 50aa54133bf09..6785fa66ec93e 100644 --- a/lib/pure/xmlparser.nim +++ b/lib/pure/xmlparser.nim @@ -12,7 +12,7 @@ import streams, parsexml, strtabs, xmltree when defined(nimPreviewSlimSystem): - import std/ioutils + import std/syncio type XmlError* = object of ValueError ## Exception that is raised diff --git a/lib/system.nim b/lib/system.nim index 8796ed6da831e..56a6e28879132 100644 --- a/lib/system.nim +++ b/lib/system.nim @@ -2680,7 +2680,7 @@ proc slurp*(filename: string): string {.magic: "Slurp".} ## This is an alias for `staticRead <#staticRead,string>`_. proc staticRead*(filename: string): string {.magic: "Slurp".} - ## Compile-time `readFile `_ proc for easy + ## Compile-time `readFile `_ proc for easy ## `resource`:idx: embedding: ## ## The maximum file size limit that `staticRead` and `slurp` can read is @@ -3152,9 +3152,9 @@ export widestrs when not defined(nimPreviewSlimSystem): {.deprecated: """io is about to move out of system; use `-d:nimPreviewSlimSystem` and - import `std/ioutils`.""".} - import std/ioutils - export ioutils + import `std/syncio`.""".} + import std/syncio + export syncio when not defined(createNimHcr) and not defined(nimscript): include nimhcr diff --git a/lib/windows/winlean.nim b/lib/windows/winlean.nim index 6093e3faf6806..c02cd97750dab 100644 --- a/lib/windows/winlean.nim +++ b/lib/windows/winlean.nim @@ -18,7 +18,7 @@ when defined(nimHasStyleChecks): {.passc: "-DWIN32_LEAN_AND_MEAN".} when defined(nimPreviewSlimSystem): - from std/ioutils import FileHandle + from std/syncio import FileHandle const useWinUnicode* = not defined(useWinAnsi) diff --git a/tests/config.nims b/tests/config.nims index 894c4bea0adc1..f7a477194fec8 100644 --- a/tests/config.nims +++ b/tests/config.nims @@ -40,3 +40,4 @@ switch("define", "nimPreviewFloatRoundtrip") switch("define", "nimPreviewDotLikeOps") switch("define", "nimPreviewJsonutilsHoleyEnum") switch("define", "nimPreviewHashRef") +switch("define", "nimPreviewSlimSystem") From c49f36f4d44af2bf7aac1c45a5b2e8a0dbddf6d6 Mon Sep 17 00:00:00 2001 From: flywind Date: Fri, 28 Jan 2022 17:57:35 +0800 Subject: [PATCH 06/12] rename --- lib/std/{ioutils.nim => syncio.nim} | 1 + 1 file changed, 1 insertion(+) rename lib/std/{ioutils.nim => syncio.nim} (99%) diff --git a/lib/std/ioutils.nim b/lib/std/syncio.nim similarity index 99% rename from lib/std/ioutils.nim rename to lib/std/syncio.nim index 580710def500e..eab96254a8973 100644 --- a/lib/std/ioutils.nim +++ b/lib/std/syncio.nim @@ -7,6 +7,7 @@ # distribution, for details about the copyright. # +## This module implements various synchronized I/O operations. include system/inclrtl import std/private/since From b3476a1029c7a4c1b458d6e4989285e8d5a6718f Mon Sep 17 00:00:00 2001 From: flywind Date: Fri, 28 Jan 2022 18:23:36 +0800 Subject: [PATCH 07/12] fix nimscript --- tests/test_nimscript.nims | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_nimscript.nims b/tests/test_nimscript.nims index 22a566307164c..a6f044d18cf7d 100644 --- a/tests/test_nimscript.nims +++ b/tests/test_nimscript.nims @@ -30,7 +30,7 @@ import std/[ # but times.getTime() implemented for VM # Generic operator system services: - os, streams, distros, + os, streams, distros, std/syncio # fails due to FFI: memfiles, osproc, terminal # works but uses FFI: dynlib # intentionally fails: marshal From bc7aec157ff4514ecc38a328fc8c0d133d2502be Mon Sep 17 00:00:00 2001 From: flywind Date: Fri, 28 Jan 2022 18:33:07 +0800 Subject: [PATCH 08/12] comma --- tests/test_nimscript.nims | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_nimscript.nims b/tests/test_nimscript.nims index a6f044d18cf7d..1c27b0d7118b6 100644 --- a/tests/test_nimscript.nims +++ b/tests/test_nimscript.nims @@ -30,7 +30,7 @@ import std/[ # but times.getTime() implemented for VM # Generic operator system services: - os, streams, distros, std/syncio + os, streams, distros, std/syncio, # fails due to FFI: memfiles, osproc, terminal # works but uses FFI: dynlib # intentionally fails: marshal From 9d6e5fc3c2509bf89cd021cc413e6deb7f9e5252 Mon Sep 17 00:00:00 2001 From: flywind Date: Fri, 28 Jan 2022 18:44:24 +0800 Subject: [PATCH 09/12] fix --- tests/test_nimscript.nims | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_nimscript.nims b/tests/test_nimscript.nims index 1c27b0d7118b6..19689e3c08c8a 100644 --- a/tests/test_nimscript.nims +++ b/tests/test_nimscript.nims @@ -30,7 +30,7 @@ import std/[ # but times.getTime() implemented for VM # Generic operator system services: - os, streams, distros, std/syncio, + os, streams, distros, syncio, # fails due to FFI: memfiles, osproc, terminal # works but uses FFI: dynlib # intentionally fails: marshal From 53fd62e0ca318a3d73569c3dbf2e53b19dbaba36 Mon Sep 17 00:00:00 2001 From: flywind Date: Fri, 28 Jan 2022 19:27:47 +0800 Subject: [PATCH 10/12] fix parts of errors --- lib/posix/posix.nim | 3 +++ tests/controlflow/tblock1.nim | 3 +++ tests/cpp/t6986.nim | 3 +++ 3 files changed, 9 insertions(+) diff --git a/lib/posix/posix.nim b/lib/posix/posix.nim index 912e395a7ee25..c10fc6ac92586 100644 --- a/lib/posix/posix.nim +++ b/lib/posix/posix.nim @@ -37,6 +37,9 @@ when defined(nimHasStyleChecks): {.push styleChecks: off.} +when defined(nimSlimSystem): + import std/syncio + # TODO these constants don't seem to be fetched from a header file for unknown # platforms - where do they come from and why are they here? when false: diff --git a/tests/controlflow/tblock1.nim b/tests/controlflow/tblock1.nim index 70c8445131aaf..4364d132d6477 100644 --- a/tests/controlflow/tblock1.nim +++ b/tests/controlflow/tblock1.nim @@ -6,6 +6,9 @@ discard """ # check for forward label and # for failure when label is not declared +when defined(nimSlimSystem): + import std/syncio + proc main = block endLess: write(stdout, "Muaahh!\N") diff --git a/tests/cpp/t6986.nim b/tests/cpp/t6986.nim index ffd277adb04f9..16e455c3ba6d5 100644 --- a/tests/cpp/t6986.nim +++ b/tests/cpp/t6986.nim @@ -5,6 +5,9 @@ discard """ import sequtils, strutils +when defined(nimPreviewSlimSystem): + import std/syncio + let rules = toSeq(lines("input")) .mapIt(it.split(" => ").mapIt(it.replace("/", ""))) From 341f18dda6a9ffba258f725d71153219ac498ffd Mon Sep 17 00:00:00 2001 From: flywind Date: Fri, 28 Jan 2022 20:01:15 +0800 Subject: [PATCH 11/12] good for now --- compiler/pathutils.nim | 3 +++ tests/config.nims | 1 - tests/test_nimscript.nims | 5 ++++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/compiler/pathutils.nim b/compiler/pathutils.nim index d8f3613b02203..8138a245acb25 100644 --- a/compiler/pathutils.nim +++ b/compiler/pathutils.nim @@ -12,6 +12,9 @@ import os, pathnorm +when defined(nimSlimSystem): + import std/syncio + type AbsoluteFile* = distinct string AbsoluteDir* = distinct string diff --git a/tests/config.nims b/tests/config.nims index f7a477194fec8..894c4bea0adc1 100644 --- a/tests/config.nims +++ b/tests/config.nims @@ -40,4 +40,3 @@ switch("define", "nimPreviewFloatRoundtrip") switch("define", "nimPreviewDotLikeOps") switch("define", "nimPreviewJsonutilsHoleyEnum") switch("define", "nimPreviewHashRef") -switch("define", "nimPreviewSlimSystem") diff --git a/tests/test_nimscript.nims b/tests/test_nimscript.nims index 19689e3c08c8a..b380570ffa90e 100644 --- a/tests/test_nimscript.nims +++ b/tests/test_nimscript.nims @@ -5,6 +5,9 @@ from stdtest/specialpaths import buildDir +when defined(nimSlimSystem): + import std/syncio + import std/[ # Core: bitops, typetraits, lenientops, macros, volatile, @@ -30,7 +33,7 @@ import std/[ # but times.getTime() implemented for VM # Generic operator system services: - os, streams, distros, syncio, + os, streams, distros, # fails due to FFI: memfiles, osproc, terminal # works but uses FFI: dynlib # intentionally fails: marshal From a291ff3f64f1b6f8c4739237fe526240918548fc Mon Sep 17 00:00:00 2001 From: flywind Date: Fri, 28 Jan 2022 22:10:33 +0800 Subject: [PATCH 12/12] fix test --- tests/controlflow/tblock1.nim | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/controlflow/tblock1.nim b/tests/controlflow/tblock1.nim index 4364d132d6477..70c8445131aaf 100644 --- a/tests/controlflow/tblock1.nim +++ b/tests/controlflow/tblock1.nim @@ -6,9 +6,6 @@ discard """ # check for forward label and # for failure when label is not declared -when defined(nimSlimSystem): - import std/syncio - proc main = block endLess: write(stdout, "Muaahh!\N")