Python 3.14.7 for MorphOS - developer package (embedding, libnix)
================================================================

*** IMPORTANT: if the Shell says a program is not executable, run
***     Protect <program> +e   (e.g. Protect examples/embed +e)
*** (needed after unpacking a .tar.gz; harmless otherwise).

This package lets you embed the Python interpreter in a MorphOS program
compiled with -noixemul (libnix).  Built with GCC 9.5.0 from the
MorphOS SDK 3.20 (2026-05-29).  An ixemul program CANNOT use it (libnix
and ixemul cannot be mixed in one executable).

COMPILER: GCC 9 REQUIRED (or newer)
The Python 3.14 headers need GCC >= 4.8 (C11 atomics) and libpython was
built with GCC 9.  On MorphOS, "ppc-morphos-gcc" is the SDK's compiler
selector (ENV:GCCSelect) and may still run GCC 2.95.3, which gives
  pyatomic.h: #error "no available pyatomic implementation ..."
Call ppc-morphos-gcc-9 explicitly (the SDK 3.20 "GCC 9" package), or
set GCCSelect to GCC 9.  This package's pyconfig.h stops with a clear
message if the compiler is too old.  The program's files that include
Python.h, and the final link, must use GCC 9.


Contents
--------

  include/python3.14/     Python headers (libnix pyconfig.h)
  include/morphos/        morphos_compat.h, morphos_path.h
                          (Unix <-> AmigaDOS path conversion)
  lib/libpython3.14.a     the interpreter and all built-in C modules
  lib/libmorphoscompat.a  POSIX compatibility layer + Unix paths
  lib/libsqlite3.a        SQLite 3.54.1 (sqlite3 module)
  lib/libmpdec.a, libexpat.a, libHacl_*.a
                          libraries bundled with CPython
                          (_decimal, pyexpat, hashlib)
  python-embed.mk         PY_CFLAGS / PY_LDFLAGS / PY_LIBS variables
  examples/               embed.c, embed_demo.py, Makefile, link.rsp
  LICENSES/               licence texts

libssl/libcrypto (OpenSSL 4.0.0) are not included: they are the SDK's
libnix ones (GG:ppc-morphos/lib/libnix/libssl.a and libcrypto.a), found
automatically by gcc with -noixemul.

At run time the Python standard library is also needed: the
lib/python3.14 directory of Python3.14.7-MorphOS.tar.gz.


Compiling and linking
---------------------

In your program's Makefile (GNU make, native or cross):

  PYDEV = ../Python3.14-dev
  include $(PYDEV)/python-embed.mk
  CC = ppc-morphos-gcc-9
  CFLAGS += $(PY_CFLAGS)
  myprog: $(OBJS)
          $(CC) $(LDFLAGS) $(PY_LDFLAGS) -o $@ $(OBJS) $(LIBS) $(PY_LIBS)

Library order (static archives, order matters):

  -Wl,--start-group -lpython3.14 -lmpdec -lexpat -lHacl_HMAC
     -lHacl_Hash_BLAKE2 -lHacl_Hash_SHA3 -lHacl_Hash_SHA2
     -lHacl_Hash_SHA1 -lHacl_Hash_MD5 -Wl,--end-group
  -lsqlite3 -lssl -lcrypto -lmorphoscompat
  -lpthread -ldl -lzstd_shared -lz_shared -lauto -latomic -lm

PY_LDFLAGS also contains:
  -Wl,--wrap=open,stat,lstat,access,opendir,readdir,rewinddir,closedir,
     chdir,getcwd,mkdir,rmdir,unlink,remove,rename,chmod,chown,truncate,
     lseek,lseek64,read,write,fstat,close,dup,dup2
     (Unix paths; lseek past end of file as on Unix; st_nlink >= 1)
  -Wl,-u,__wrap_open      (pulls the path layer in before libc.a)
  -Wl,-u,__atomic_...     (libpthread needs libatomic)


Building the example
--------------------

On MorphOS (SDK 3.20 with the GCC 9 package), in a Shell:

  cd Python3.14-dev/examples
  make

(the Makefile calls ppc-morphos-gcc-9; use make CC=ppc-morphos-gcc-15
for another modern GCC; the same works when cross-compiling on Linux).
All paths are relative and the Makefiles run no shell commands.

Without make, two commands in examples/ (link.rsp holds all link
options, in order):

  ppc-morphos-gcc-9 -noixemul -O2 -Wall -I../include/python3.14 -I../include/morphos -c embed.c -o embed.o
  ppc-morphos-gcc-9 -o embed embed.o @link.rsp

Then, optionally: ppc-morphos-strip --strip-unneeded embed

To try it: copy embed and embed_demo.py into the Python3.14 directory
of the runtime package and run embed from there.  Expected output:

  Python 3.14.7 on morphos
  prefix: /Work/Python3.14
  compute(21, 'Work:data/file.txt'): exists=False
  compute() -> 42
  describe() -> embed embeds Python (cwd=/Work/Python3.14)
  expected error:
  Traceback (most recent call last):
    File "/Work/Python3.14/embed_demo.py", line 16, in fails
      raise ValueError("raised on purpose by embed_demo.fails()")
  ValueError: raised on purpose by embed_demo.fails()


Important points
----------------

1. Paths: the translation ALSO applies to the host program.
   --wrap replaces open(), stat(), opendir(), chdir(), getcwd()... for
   the WHOLE executable:
   - AmigaDOS paths with a volume ("Work:x", "PROGDIR:y", "T:z") and
     simple relative paths ("a/b") are unchanged;
   - a relative AmigaDOS path starting with "/" (= parent directory in
     AmigaDOS) is read as an ABSOLUTE Unix path: "/x" becomes the
     volume "x:".  Write "../x" or a full path;
   - "a//b" (AmigaDOS parent) becomes "a/b" on the Unix side: avoid it;
   - getcwd() returns a Unix path ("/Work/MyProg");
   - no more "insert volume" requesters for these calls.
   Direct dos.library calls (Open(), Lock()...) are NOT affected.

2. Stack: libmorphoscompat defines __stack = 0x240000 (2.25 MB), read
   by the libnix startup code; Python assumes 2 MB of stack.  If your
   program already defines __stack, the linker reports a duplicate:
   keep a single definition, >= 0x240000.  Python threads also get
   2.25 MB.

3. Initialisation: call Py_InitializeFromConfig() (or Py_Initialize())
   ONCE at start-up and Py_FinalizeEx() ONCE at the end.

4. Where lib/python3.14 must be: config.home must point (as a Unix
   path) to the directory that CONTAINS lib/python3.14, e.g.
   "/Work/MyProg/Python3.14".  The example uses the EMBED_PYTHONHOME
   variable if set, else PROGDIR: itself if it contains lib/python3.14,
   else PROGDIR:Python3.14.

5. GIL and threads: only one thread at a time runs Python code.
   - After initialisation the main thread holds the GIL.
   - From another (pthread) thread:
       PyGILState_STATE g = PyGILState_Ensure();
       ... Python calls ...
       PyGILState_Release(g);
     Only threads created with (libnix) pthread are safe; an exec task
     created with CreateTask/CreateNewProc must not call Python.
   - To keep a long C computation from blocking other threads:
       Py_BEGIN_ALLOW_THREADS ... C code without Python ...
       Py_END_ALLOW_THREADS
   - Ctrl-C: the example disables Python's signal handlers
     (install_signal_handlers = 0).

6. Errors: any API function returning NULL (or -1) has set an
   exception; PyErr_Print() prints the traceback to stderr and clears
   the error.

7. References: PyObject_Call*(), PyImport_ImportModule()... return new
   references to release with Py_DECREF/Py_XDECREF; PySys_GetObject()
   returns a borrowed reference (do not release it).


Limitations
-----------

The same as the runtime package (see its README): no subprocess/
fork/exec, no mmap, ctypes, curses, readline; IPv4 only.


Licences
--------

libpython3.14.a: PSF License Agreement.  libmorphoscompat and the porting
patches: PSF License Agreement as well.  Programs linked with this
package also statically link MorphOS SDK 3.20 runtime libraries (libnix,
libauto, libabox, libpthread, the *_shared stubs), which remain under the
MorphOS SDK's terms and their authors' copyright.  Full texts: see
LICENSES/INDEX.txt.

Port: Digitally.
