Skip to content

pyword.c is a pythonic C<->CPython harness with LSP-like features; an easy to-use python 3.12+ module for CICD and reflective/reflexive introspection of dynamic C libraries.

License

Notifications You must be signed in to change notification settings

Morphological-Source-Code/pleroma

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyword.c

pyword.c is a pythonic C<->CPython harness with LSP-like features; an easy to-use python 3.12+ module for CICD and reflective/reflexive introspection of dynamic C libraries.

I can't wait for Guido to save me, but I suspect Python will eventually have its-own native LSP stdlib. Until then.. this will have to do, for me.

install + run

(optional) virtual-env: python -m venv .venv && source .venv/bin/activate finally: python -m pip install -e .

>>> import pyword
>>> w = pyword.PyWord()
>>> w.set_bytes(b"hello world")
>>> w.get_bytes()
b'hello world'
>>> len(w)
11

python -c "import pyword, sys; print(pyword.PyWord, file=sys.stderr)"
# <class 'pyword.PyWord'>

python main.py
[INFO] 20...  ...[BENCH] Logger initialized.

CPythonification Guidance

  1. PyObject Manipulation:

    • Access PyObject fields via ctypes:
      frame = CPythonFrame.from_object("test")
      py_obj = py_object(frame)
      py_struct = PyObject.from_address(id(frame))
    • Manipulate ob_refcnt and ob_type for custom objects.
  2. Type System:

    • Create PyTypeObject for CPythonFrame:
      from ctypes import Structure, c_char_p, c_int
      class PyTypeObject(Structure):
          _fields_ = [("ob_base", PyObject), ("tp_name", c_char_p)]
    • Define methods via PyMethodDef.
  3. Reflexivity:

    • Use inspect to extract and modify source code:
      source = inspect.getsource(CPythonFrame)
      frame.value = source  # Store own source
    • Generate quines by recompiling source via compile().
  4. Memory Management:

    • Extend PyWord with a cache:
      class PyWordCache:
          def __init__(self, capacity: int):
              self.cache = {}
              self.capacity = capacity
          def get(self, key: int) -> Optional[PyWord]:
              return self.cache.get(key)
          def put(self, key: int, word: PyWord):
              if len(self.cache) >= self.capacity:
                  self.cache.pop(next(iter(self.cache)))
              self.cache[key] = word
    • Use PyMem_Malloc for allocations.
  5. Resources:

Future

LSP, CICD, after I build tests and bug test the existing bit/memoryview harness.

About

pyword.c is a pythonic C<->CPython harness with LSP-like features; an easy to-use python 3.12+ module for CICD and reflective/reflexive introspection of dynamic C libraries.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •