Skip to content

Commit 26d4905

Browse files
committed
feat(sugar): declarePyType sup singleton
1 parent f7595aa commit 26d4905

3 files changed

Lines changed: 18 additions & 12 deletions

File tree

‎Objects/boolobject.nim‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pyobject
22
import ./numobjects/intobject/decl
33

4-
declarePyType Bool(tpToken, base(Int)):
4+
declarePyType Bool(tpToken, base(Int), singleton):
55
b: bool
66

77
proc newPyBoolInner(b: bool): PyBoolObject =

‎Objects/noneobject.nim‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pyobject
22
import ./stringobject
33

4-
declarePyType None(tpToken):
4+
declarePyType None(tpToken, singleton):
55
discard
66

77
let pyNone* = newPyNoneSimple() ## singleton

‎Objects/pyobject.nim‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,10 @@ macro declarePyType*(prototype, fields: untyped): untyped =
727727
## - base: BASE
728728
## - typeName: TYPE_NAME;
729729
## TYPE_NAME defaults to lowerAscii of `prototype[0]`
730-
## - tpToken, dict, mutable, reprLock
730+
## - tpToken, singleton, mutable, dict, reprLock
731731
prototype.expectKind(nnkCall)
732732
fields.expectKind(nnkStmtList)
733-
var tpToken, mutable, dict, reprLock: bool
733+
var tpToken, singleton, mutable, dict, reprLock: bool
734734
var baseStr = ""
735735
var typeName: string
736736
# parse options the silly way
@@ -747,13 +747,16 @@ macro declarePyType*(prototype, fields: untyped): untyped =
747747

748748
option.expectKind(nnkIdent)
749749
let property = option.strVal
750-
if property == "tpToken":
750+
case property
751+
of "tpToken":
751752
tpToken = true
752-
elif property == "mutable":
753+
of "singleton":
754+
singleton = true
755+
of "mutable":
753756
mutable = true
754-
elif property == "dict":
757+
of "dict":
755758
dict = true
756-
elif property == "reprLock":
759+
of "reprLock":
757760
reprLock = true
758761
else:
759762
error("unexpected property: " & property)
@@ -883,7 +886,7 @@ macro declarePyType*(prototype, fields: untyped): untyped =
883886
result.add(decObjNode)
884887

885888
# boilerplates for pyobject type
886-
template initTypeTmpl(tbase, pyObjType, fullNameIdent, name, nameStr, hasTpToken, hasDict) =
889+
template initTypeTmpl(tbase, pyObjType, fullNameIdent, name, nameStr, hasTpToken, hasDict, isSingleton) =
887890
let `pyObjType`* {. inject .} = newBltinPyType[`fullNameIdent`](nameStr, tbase)
888891

889892
proc `ofExactPy name Object`*(obj: PyObject): bool {. cdecl, inline .} =
@@ -901,14 +904,16 @@ macro declarePyType*(prototype, fields: untyped): untyped =
901904
cur = cur.base
902905
return false
903906

904-
proc `newPy name Simple`*: `Py name Object` {. cdecl .}=
907+
proc `newPy name Simple`: `Py name Object` {. cdecl .}=
905908
# use `result` here seems to be buggy
906909
let obj = `Py name Object` `pyObjType`.tp_alloc(`pyObjType`, 0)
907910
when defined(js):
908911
obj.giveId
909912
when hasDict:
910913
obj.dict = newPyDict()
911914
obj
915+
when not isSingleton:
916+
export `newPy name Simple`
912917

913918
# default for __new__ hook, could be overrided at any time
914919
proc `newPy name Default`(args: openArray[PyObject]; kwargs: PyObject = nil): PyObject {. cdecl .} =
@@ -920,7 +925,8 @@ macro declarePyType*(prototype, fields: untyped): untyped =
920925
result.add(getAst(initTypeTmpl(ident(baseTypeObjStr), pyObjType, fullNameIdent, nameIdent,
921926
typeName,
922927
newLit(tpToken),
923-
newLit(dict)
928+
newLit(dict),
929+
newLit(singleton),
924930
)))
925931

926932
if members.len > 0:
@@ -932,7 +938,7 @@ macro declarePyType*(prototype, fields: untyped): untyped =
932938

933939
result.add(getAst(methodMacroTmpl(nameIdent)))
934940

935-
declarePyType NotImplemented():
941+
declarePyType NotImplemented(singleton):
936942
discard
937943

938944
let pyNotImplemented* = newPyNotImplementedSimple() ## singleton

0 commit comments

Comments
 (0)