Skip to content

Commit 694bd9e

Browse files
committed
break(Lib): use OptionalObj for datetime.tzname, inspect's, signal.strsignal, os.*_cpu_count
1 parent 4e20bc7 commit 694bd9e

File tree

4 files changed

+35
-13
lines changed

4 files changed

+35
-13
lines changed

src/pylib/Lib/datetime.nim

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,28 @@ export n_datetime except tzname, isoformat, strftime, ctime
55

66
import ../pystring/strimpl
77
import ../noneType
8+
import ./typing_impl/str_optional_obj
9+
expOptObjCvt()
810
import ../pyerrors/simperr
911

1012

1113
method tzname*(tz: tzinfo; dt: datetime): PyStr{.base.} =
1214
str n_datetime.tzname(tz, dt)
1315
method tzname*(tz: timezone; dt: datetime): PyStr =
1416
str n_datetime.tzname(tz, dt)
15-
func tzname*(dt: datetime): PyStr =
16-
## .. hint:: this won't returns `None`, but may return a empty string
17-
str n_datetime.tzname(dt)
17+
func tzname*(dt: datetime): OptionalObj[PyStr] =
18+
newStrOptionalObj n_datetime.tzname(dt)
1819

1920
using self: datetime # | date
2021
func strftime*(self; format: PyStr): PyStr = str n_datetime.strftime(self, $format)
2122
func isoformat*(self; sep: StringLike = 'T', timespec="auto"): PyStr =
2223
template lenErr =
2324
raise newException(TypeError, "isoformat() argument 1 must be a unicode character, not str")
24-
when sep is string:
25-
let sep = str sep
25+
when system.`is`(sep, string):
26+
# NIM-BUG: have to prefix by `system` once optional_obj.`is` imported
27+
let sep = str sep
2628
if sep.len != 1: lenErr()
27-
when sep is char:
29+
when system.`is`(sep, char):
2830
let ch = sep
2931
else:
3032
let ch = $sep

src/pylib/Lib/inspect.nim

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export list_decl
1212

1313
import ../pystring/strimpl
1414
export strimpl
15+
import ./typing_impl/str_optional_obj
16+
expOptObjCvt()
1517
from ../pystring/strmeth import splitlines
1618
import ../version
1719

@@ -37,8 +39,11 @@ gen_getmembers_static pysince(3,11)
3739
template wrapPyStr1(fun; T){.dirty.} =
3840
template fun*(obj: T): PyStr =
3941
str n_inspect.fun(obj)
42+
template wrapPyOptStr1(fun; T){.dirty.} =
43+
template fun*(obj: T): OptionalObj[PyStr] =
44+
newStrOptionalObj n_inspect.fun(obj)
4045

41-
wrapPyStr1 getmodulename, PyStr
46+
wrapPyOptStr1 getmodulename, PyStr
4247

4348
wrapExportSincePy(3,12, markcoroutinefunction)
4449

@@ -49,12 +54,13 @@ wrapExportSincePy(3,5, isawaitable)
4954

5055

5156
template wrapPyStr1(fun){.dirty.} = wrapPyStr1(fun, untyped)
57+
template wrapPyOptStr1(fun){.dirty.} = wrapPyOptStr1(fun, untyped)
5258

5359
wrapPyStr1 cleandoc, PyStr
5460
wrapPyStr1 getfile
55-
wrapPyStr1 getsourcefile
61+
wrapPyOptStr1 getsourcefile
5662
wrapPyStr1 getsource
57-
wrapPyStr1 getdoc
63+
wrapPyOptStr1 getdoc
5864

5965
macro getsourcelines*(obj: typed): (PyList[string], int) =
6066
## get source code of the object

src/pylib/Lib/os.nim

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import ./os_impl/private/platform_utils
1111
import ../pyconfig/bootstrap_hash
1212

1313
import ./n_os
14-
export n_os except scandir, DirEntry, urandom, getrandom, genUname, uname, uname_result, sched_getaffinity, sched_setaffinity
15-
14+
export n_os except scandir, DirEntry, urandom, getrandom, genUname, uname, uname_result,
15+
sched_getaffinity, sched_setaffinity,
16+
cpu_count, process_cpu_count
17+
import ./typing_impl/optional_obj
18+
expOptObjCvt()
1619
import ../version
1720
genUname PyStr
1821
template scandir*(): untyped{.pysince(3,5).} = n_os.scandir()
@@ -40,3 +43,12 @@ when HAVE_SCHED_SETAFFINITY:
4043
result = newPySet[int]()
4144
sched_getaffinityImpl(pid) do (x: cint):
4245
result.add int x
46+
47+
template wrapMayNone(name){.dirty.} =
48+
proc name*(): OptionalObj[int] =
49+
let res = n_os.name()
50+
if res > 0: newOptionalObj(res)
51+
else: newOptionalObj[int]()
52+
53+
wrapMayNone cpu_count
54+
wrapMayNone process_cpu_count

src/pylib/Lib/signal.nim

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import ./signal_impl/valid_signals_impl
44

55
import ../builtins/set
66
import ../pystring/strimpl
7+
import ./typing_impl/str_optional_obj
8+
expOptObjCvt()
79
import ../version
810

911
export n_signal except sigpending, pthread_sigmask, strsignal, valid_signals
@@ -26,8 +28,8 @@ proc pthread_sigmask*(how: int, mask: Sigset): PySet[int]{.mayUndef.} =
2628
result.fromIterable oa
2729
]#
2830

29-
proc strsignal*(signalnum: int): PyStr{.pysince(3,8).} =
30-
str n_signal.strsignal signalnum
31+
proc strsignal*(signalnum: int): OptionalObj[PyStr]{.pysince(3,8).} =
32+
newStrOptionalObj n_signal.strsignal signalnum
3133

3234
when have_valid_signals:
3335
proc valid_signals*(): PySet[int]{.pysince(3,8).} =

0 commit comments

Comments
 (0)