News for hackers. Stuff that matters.
Re:How the Python import system works by Fordec on Saturday July 24, 2021 @12:39PM (#27942338)
Wait, what?

> that you create

As in existing language supplied eg. Perl, Java, etc., or literally anything? Like bootstrapping your own home made language from scratch?

Re:How the Python import system works by sillysaurusx on Saturday July 24, 2021 @01:14PM (#27942658)
Literally anything. 'Tis a homemade homegrown lisp, grown by Scott Bell for several years till I took it all for myself. Nom nom.

It starts with reader.l: https://github.com/shawwn/pymen/blob/ml/reader.l where the raw character stream is turned into a bunch of nested arrays. E.g. (+ 1 2) becomes ["+", 1, 2]

Then it's punted over to compiler.l https://github.com/shawwn/pymen/blob/ml/compiler.l where it's passed through `expand`, which does a `macroexpand` followed by a `lower`. E.g. (do (do (do (print 'hi)))) becomes ["print", "\"hi\""]

Then the final thing is thrown to the compile function, which spits out print("hi") -- the final valid Python code that gets passed into the standard python `exec` function.

Works with all the standard python things, like async functions and `with` contexts. Been screwing around with it for a few years.

Re:How the Python import system works by Fordec on Saturday July 24, 2021 @01:22PM (#27942746)
That's absolutely disgusting. But in a Web Assembly sort of way... I don't know whether to spit on it or give it a medal.
Re:How the Python import system works by jazzyjackson on Sunday July 25, 2021 @02:19AM (#27947152)
I have a homemade language that's not a lisp, but is lispy in some ways. I've only got to the point where I expand the code to an abstract syntax tree, and deciding how to go from there is the hard part for me right now. It's never crossed my mind to just compile it to valid python code. Thanks for the inspiration!
Re:How the Python import system works by aasasd on Saturday July 24, 2021 @04:53PM (#27944387)
If the import system allows your code to run instead of the ‘import’ statement, and to produce the module however you want, then of course you can do whatever: load code from Google or StackOverflow results, if you wish.
Re:How the Python import system works by masklinn on Saturday July 24, 2021 @01:42PM (#27942920)
> As in existing language supplied eg. Perl, Java, etc., or literally anything? Like bootstrapping your own home made language from scratch?

Should work with anything as long as you can ultimately generate Python bytecode (and provide a module object). The import system is not simple, but it's really open.