#esoteric-python

1 messages · Page 26 of 1

past plank
#

just like how windows 10 is gonna be the last version of windows right swagtroll

sly ibex
#
a,b=0,1
x=[]
for i in range(int(input())):x.append(a);a,b=b,a+b
print(*x)```

this is the fibonacci sequence, but im trying to golf it, some advices? this looks too begginer
low lynx
#

there's a code golf challenge for the fibonacci numbers that I think would be really similar to that so I'll avoid explaining too much

#

but it's probably better not to keep a list and just print stuff

sly ibex
#

but thanks stickie

#

i could do it shorter

#

13... characters -

a,b,x=0,1,[]
exec('x+=a,;a,b=b,a+b;'*int(input()))
print(*x)```
quartz wave
#

oh

#

ok i c

low lynx
#

is it possible to set something up to run code whenever globals is modified?

sick hound
#

Tho this is very wrong

rugged sparrow
#

We have done it a few times here but you can swap the globals dictionary with a subclass of it

low lynx
#

ooh nice

fleet bridge
#

accidental list(range(2*10**9) almost killed my computer

#

i do not recommend running that code

astral rover
#

i think itd be fine to run that 😉

sick hound
#

Let's see

#

!e ```py
list(range(2*10**9))

night quarryBOT
#

@sick hound :x: Your 3.11 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 1, in <module>
003 |     list(range(2*10**9))
004 | MemoryError
astral rover
#

!e list(range(2*10**9)

night quarryBOT
#

@astral rover :x: Your 3.11 eval job has completed with return code 1.

001 |   File "/home/main.py", line 1
002 |     list(range(2*10**9)
003 |         ^
004 | SyntaxError: '(' was never closed
tough willow
versed eagle
#

you can change the type of the globals dict to hook its __setitem__
you could use a tracing function
you could also do bytecode parsing but that gets super complicated when you're dealing with multiple versions of python

#

hooking the globals type is probably the best/easiest option

meager zinc
#

yeah last time I did it I used bytecode parsing and it is super portable (real)

vast basin
fleet bridge
arctic skiff
#

!epy tree=lambda d:[f:=d,[(f:=f-1,print(f"{' '*f}{'*'*((j+1)*2-1)}"))for j in range(d)]] tree(11)

night quarryBOT
#

@arctic skiff :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 |           *
002 |          ***
003 |         *****
004 |        *******
005 |       *********
006 |      ***********
007 |     *************
008 |    ***************
009 |   *****************
010 |  *******************
011 | *********************
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/cilatijubo.txt?noredirect

arctic skiff
#

Guess it also takes \n

last locust
#

It's a bug

#

bot#2492

glass drumBOT
tough willow
#

!e

tree=lambda d:[print(('*'*i).center(d*2))for i in range(1,d*2,2)]
tree(11)
night quarryBOT
#

@tough willow :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 |           *           
002 |          ***          
003 |         *****         
004 |        *******        
005 |       *********       
006 |      ***********      
007 |     *************     
008 |    ***************    
009 |   *****************   
010 |  *******************  
011 | ********************* 
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/ivaqiyalud.txt?noredirect

wheat river
#
tree=lambda d:[print(f"{'*'*i:^{d*2}}")for i in range(1,d*2,2)]
quartz wave
#

62 -> 59 ```py
def tree(d,i=1):
while i<d2:print(f"{''i:^{d2}}");i+=2

quartz wave
night quarryBOT
#

@quartz wave :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 |           *           
002 |          ***          
003 |         *****         
004 |        *******        
005 |       *********       
006 |      ***********      
007 |     *************     
008 |    ***************    
009 |   *****************   
010 |  *******************  
011 | ********************* 
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/livaxudoba.txt?noredirect

quartz wave
arctic skiff
quartz wave
arctic skiff
quartz wave
#

60 ```py
def tree(d,i=1):exec("print(f"{'*'i:^{d2}}")\ni+=2\n"*d)

arctic skiff
#

Sopy tree=lambda d,i=1:exec("print(f\"{'*'*i:^{d*2}}\")\ni+=2\n"*d) 

quartz wave
#

ok i'm making my own set of rules

#

least character amount wins

gleaming linden
#

Degen solutions are dumb

quartz wave
wheat river
wheat river
#

ah, ok 👍

kindred stone
#

just slapped some random things together and got tired while writing this, so i didnt completed the code

#

!e

print((classmethod(type(slice.__mro__[0]).__mro__[1].__doc__).__class__.__name__[8].upper()).__add__(....__repr__()[::-1][-1].lower()).__add__(type(int.from_bytes(b"\xFF\xFF", byteorder="little").__eq__(int(str("190326753248076643792647923649875".__add__("69593707450387")[::-1][-1]))-1)).__name__[-1].__mul__(memoryview(b"69").nbytes)).__add__(chr(memoryview(bytes(int.from_bytes(b"\0x", byteorder="big") & int.from_bytes(b"\0x", byteorder="big"))).nbytes - 9)).__add__(str(['x+=a,;a,b=b,a+b;', '*x']).split("'").__getitem__(int("101", 2).__mul__(int("10", 8).__sub__(int("3F", 16))).__mod__(int("11", 2)).__add__(int("101", 2)).__sub__(4))))
night quarryBOT
#

@kindred stone :white_check_mark: Your 3.11 eval job has completed with return code 0.

Hello, 
eager ore
#

Hello,

unique heath
#

golfing challenge: invert boolean (true -> false, false -> true)

#
lambda b:b-1if b else b+1```
#

!e

print((lambda b:b-1if b else b+1)(True))
night quarryBOT
#

@unique heath :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | /home/main.py:1: SyntaxWarning: invalid decimal literal
002 |   print((lambda b:b-1if b else b+1)(True))
003 | 0
unique heath
#

!e

print((lambda b:b-1if b else b+1)(False))
night quarryBOT
#

@unique heath :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | /home/main.py:1: SyntaxWarning: invalid decimal literal
002 |   print((lambda b:b-1if b else b+1)(False))
003 | 1
unique heath
#

it works lfmao

#

im dumb lambda b:not b exists

fleet bridge
#
f = lambda b:b-1if b else b+1
f = lambda b:0if b else 1
f = lambda b:not b
f = lambda b:1-b
f = 1 .__sub__
f = 0 .__pow__
import operator as o
f = o.not_
languid hare
#

!e

NOT = 1 .__sub__

print(NOT(True))
print(NOT(False))
night quarryBOT
#

@languid hare :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | 0
002 | 1
languid hare
#

aw a little slower than denball

fleet bridge
#
f = lambda b:b-1if b else b+1
f = lambda b:0if b else 1
low lynx
#

1-b

unique heath
#

how thfeuf kc dopes that wokr

arctic skiff
night quarryBOT
#

@arctic skiff :white_check_mark: Your 3.11 eval job has completed with return code 0.

False
arctic skiff
#

!epy print(not True)

night quarryBOT
#

@arctic skiff :white_check_mark: Your 3.11 eval job has completed with return code 0.

False
unique heath
arctic skiff
unique heath
#

lambda b: bool(0if b else 1) shorter

arctic skiff
arctic skiff
arctic skiff
night quarryBOT
#

@arctic skiff :white_check_mark: Your 3.11 eval job has completed with return code 0.

-2
unique heath
#

thats still True

arctic skiff
#

Yeah i see

#

Though isnt ~ bitwise NOT

#

Ahh i see why it doesn't work

arctic skiff
night quarryBOT
#

@arctic skiff :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | /home/main.py:1: SyntaxWarning: invalid decimal literal
002 |   print((lambda _:_-1if _ else~_)(True))
003 | /home/main.py:2: SyntaxWarning: invalid decimal literal
004 |   print((lambda _:_-1if _ else~_)(False))
005 | 0
006 | -1
languid hare
#

its shorter and returns a boolean

#

!e

f = 0 .__eq__

print(f(True))
print(f(False))
night quarryBOT
#

@languid hare :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | False
002 | True
versed eagle
#

it flips the sign bit

low lynx
#

~ is one's complement

#
  • is two's complement
rough moat
#

while("False")

sick hound
rough moat
#

yeah

#

it runs tho lmao

sick hound
#

yeah got it

rough moat
#

it's because an empty string acts like false and a well, not empty string acts like true

sick hound
#

yeah I know

#

imagine showing a beginner how while loops work with this

gentle plover
versed eagle
#

1 .__sub__ was already said

#

0 .__eq__ was also mentioned iirc

dusty zodiac
#
a,y=map(int,open(0))
print(sum(366 if i%4<1 and(i%100>0 or i%400<1)else 365 for i in range(y-a,y))+1)
#

anyone know a better way to shorten this further?

low lynx
#

removing spaces and brackets firstly

vague cairn
#

a,y=map(int,open(0)) doesn't work for me, ValueError: invalid literal for int() with base 10: '\n'

night quarryBOT
#

:incoming_envelope: :ok_hand: applied timeout to @queen jetty until <t:1681625403:f> (10 minutes) (reason: duplicates spam - sent 4 duplicate messages).

The <@&831776746206265384> have been alerted for review.

next flame
#

I think piping in with echo should work

restive void
#

Only if you add -n, IIRC

wheat river
dusty zodiac
#

oh wait

#

yeah that works

#

ty

wheat river
#
a,y=map(int,open(0))
print(sum(365+(i%16*(i%25<1)<(i%4<1))for i in range(y-a,y))+1)
quartz wave
#

maybe not

quiet rose
#

/help

#

!e

night quarryBOT
#
Missing required argument

code

languid hare
quartz wave
languid hare
quartz wave
urban ledge
#

if i obfuscate my code with pyarmor, the other ppl that executes the code needs pyarmor aswell?

serene stratus
#

it will be included in a folder called pytransform

leaden valley
#

using fishhook how can i do this ```py
a = "Hello World!"
print(a) # Hi World!

rugged sparrow
#

You would be better off hooking globals

#

I sent some code to do it a bit above

leaden valley
rugged sparrow
leaden valley
#

its actually perfect

meager zinc
#

reimplementing std library

def isinstance(a, b):
    if type(b) == tuple:
        return any(c in type.mro(a.__class__) for c in b)
    else:
        return b in type.mro(a.__class__)
sick hound
# meager zinc reimplementing std library ```py def isinstance(a, b): if type(b) == tuple: ...
a=type(int|str)
def i(i):
 if isinstance(i,a):return i.__args__
 if not isinstance(i,tuple):raise TypeError("isinstance() arg 2 must be a type, a tuple of types, or a union")
 i,u=[i],set()
 while i:
  for e in i.pop():isinstance(e,a)and(e:=e.__args__),i.append(e)if isinstance(e,tuple)else u.add(e)
 return u
def u(i,u):
 try:return getattr(i,"__instancecheck__")(u)
 except:0
def isinstance(e,o):return any(a in type(e).__mro__ or u(a,e)for a in([o]if type in type(o).__mro__ else i(o)))
meager zinc
#

thamks

#

is_even is a great module btw

sick hound
#

lol thanks

fleet bridge
misty parcel
#

как быстро выучить python? помогите, господа

misty parcel
#

ok, sorry

fleet bridge
night quarryBOT
#

4. Use English to the best of your ability. Be polite if someone speaks English imperfectly.

grave rover
#

ok how the hell does this new impl work

rugged sparrow
# grave rover ok how the hell does this new impl work

ooh that one is a fun one. there is no verification that the tuple used by partial to store args is the same length as when the function started inside of the partial.__repr__. basically like this: py def __repr__(self): args_len = len(self.args) for i in range(args_len): add_to_str(repr(self.args[i]))

#

so WeirdRepr replaces the tuple with a specific pair of items (a tuple and an array's backing right after each other)

#

and then partial tries to call tp_repr on the items located in that array backing because the new tuple is shorter

#

so you can make a fake instance of Fake with a custom address in its slots and use that to fake an arbitrary object

#

*and get a reference to it

grave rover
#

This is incredibly cursed

#

I love it

rugged sparrow
#

its probably the most contrived exploit i have written

grave rover
#

Meanwhile I might have to use your getmem impl at work

rugged sparrow
#

oh god

grave rover
#

My supervisor asked me for a task that python doesn't really support out of the box
But using memhax I could accomplish it :)
Unfortunately most systems run windows so no /proc/self/memory for me to use

rugged sparrow
#

and also i would recommend writing a C extension over using memhacks. that would be more stable

grave rover
rugged sparrow
#

ah

#

*write the assembly yourself /s

grave rover
#

Ah yes, writing x86, my favorite

#

Honestly I can't wait for CPUs to switch to ARM (they won't, but I pray every night they do)

rugged sparrow
#

also if you actually need to get the memory of the entire process, I would use ctypes instead of any of my bugs. ```py
from ctypes import c_ubyte
import sys

mem = (c_ubyte * sys.maxsize).from_address(0)

#

that would be more stable then any bug would be

#

*stable is relative with memhacks tho

grave rover
rugged sparrow
#

i mean fishhook would work for that but you said you can't non-whitelisted software

grave rover
#

Yup

#

Forbiddenfruit is also blacklisted unfortunately

rugged sparrow
#

makes sense tbh

grave rover
#

Oh actually forbiddenfruit has a funny way to bypass mappingproxy

rugged sparrow
#

does it use the __req__ trick?

grave rover
#
def patchable_builtin(klass):
    refs = gc.get_referents(klass.__dict__)
    return refs[0]``` this returns the dict backing the class' mappingproxy
rugged sparrow
#

oh yea i used to use that trick in fishhook v1

#

but __req__ didn't require gc

grave rover
#

req?

rugged sparrow
grave rover
#

But what's this req thing you're talking about 👀

rugged sparrow
#

!e ```py
def getdict(cls, E=type('',(),{'eq':lambda s,o:o})()):
'''
Obtains a writeable dictionary of a classes namespace
Note that any modifications to this dictionary should be followed by a
call to PyType_Modified(cls)
'''
return cls.dict == E

print(getdict(int))```

night quarryBOT
#

@rugged sparrow :white_check_mark: Your 3.11 eval job has completed with return code 0.

{'__new__': <built-in method __new__ of type object at 0x7f8c9fc579a0>, '__repr__': <slot wrapper '__repr__' of 'int' objects>, '__hash__': <slot wrapper '__hash__' of 'int' objects>, '__getattribute__': <slot wrapper '__getattribute__' of 'int' objects>, '__lt__': <slot wrapper '__lt__' of 'int' objects>, '__le__': <slot wrapper '__le__' of 'int' objects>, '__eq__': <slot wrapper '__eq__' of 'int' objects>, '__ne__': <slot wrapper '__ne__' of 'int' objects>, '__gt__': <slot wrapper '__gt__' of 'int' objects>, '__ge__': <slot wrapper '__ge__' of 'int' objects>, '__add__': <slot wrapper '__add__' of 'int' objects>, '__radd__': <slot wrapper '__radd__' of 'int' objects>, '__sub__': <slot wrapper '__sub__' of 'int' objects>, '__rsub__': <slot wrapper '__rsub__' of 'int' objects>, '__mul__': <slot wrapper '__mul__' of 'int' objects>, '__rmul__': <slot wrapper '__rmul__' of 'int' objects>, '__mod__': <slot wrapper '__mod__' of 'int' objects>, '__rmod__': <slot wrapper '__rmod__' of 'int' ob
... (truncated - too long)

Full output: https://paste.pythondiscord.com/ahasajetix.txt?noredirect

rugged sparrow
#

mappingproxy.__req__ dispatches to dict.__req__ on the wrapped dictionary

grave rover
#

Lmao

rugged sparrow
#

and dict.__req__ calls arg.__eq__(mp)

grave rover
#

This is so stupid, I love it

#

Thank you python for truthy values

rugged sparrow
grave rover
#

Honestly at this point I want to just drop my work and look through the interpreter to see just how many ways we have to bypass mappingproxy and/or access raw memory

rugged sparrow
#

involves the GC being called in odd places

grave rover
#

What I'm curious about

#

Are there any that work on other impls than cpython

rugged sparrow
#

not that I have found, but thats because I only really look at cpython

#

other impls would have different bugs in different places probably

grave rover
#

I wonder if pypy allows for the extended_argument exploit

rugged sparrow
#

no idea

#

i can look

#

*but pypy does not implement id the same way so you would need to get an info leak as well

grave rover
#

dang

rugged sparrow
grave rover
#

Oh I was talking about the 256 bytecode one from a while back

rugged sparrow
#

ohh the custom code object one

rugged sparrow
#

but its a different exploit process to get it stable

grave rover
#

Lol

#

I see

rugged sparrow
#

*it does not technically

rugged sparrow
#

but __eq__ still does reverse dispatch

leaden valley
#

could you mess with python to make the syntax like javascripts?

etc. True is true
{} for code blocks

#

and are there any tutorials or books to learn more about esoteric python and code golfing

#

im very interested in hooking

sick hound
#

ig for true you can do

#

true = True

#

But for others you need ctypes or cython

leaden valley
#

well yeah that's obvious

next flame
grave rover
#

Custom codec, custom import hook for preprocessing, patching compiler at runtime

Just some ways you could probably do it

rugged sparrow
quartz wave
sick hound
#

how wtf

quartz wave
leaden valley
#

can you modify the code of functions from modules you imported

quartz wave
leaden valley
#

colorama

#

if u wanted green u would do colorama.Fore.GREEN

#

but

#

nvm thats a bad example

#

i just want to change the code of functions in a module

restive void
leaden valley
restive void
leaden valley
grave rover
leaden valley
leaden valley
#

fixed it but now i get this error.. py ALL_OPS = {v: eval(k) for k, v in opmap.items()} ^^^^^^^ File "<string>", line 1, in <module> NameError: name 'CACHE' is not defined

leaden valley
vast wave
#

hm

grave rover
leaden valley
grave rover
leaden valley
#

aw that sucks i dont have 3.10

#

ill install 3.10 later

rugged sparrow
#

if you forbid __ in input, can you still eval arbitrary code? py inp = input('> ') if '__' in inp:exit('fail') else:eval(inp, {'__builtins__':{}}) I have been trying some stuff, wondering if i am missing something simple

rugged sparrow
#

no builtins ^

fleet bridge
#

your eval will eval my eval

#

ah

rugged sparrow
#

{'__builtins__':{}} this makes it so python doesn't add in its builtins

#

so you can't access __subclasses__ on something to get to a useful class

fleet bridge
#

i can do something stupid like 10**10**10**10

rugged sparrow
#

i mean yea thats fair

#

but im trying to find code exec not just resource usage

fleet bridge
#

there is some package that can execute code safely
it basically forbids dunders and some builtins

rugged sparrow
#

what package?

fleet bridge
#

i dont remember

rugged sparrow
#

ah that evals ast

#

im more curious about bypasses for just blocking __ and all builtins

#

Ik there are some exotic ones

#

but wondering if there are any simple ones

fleet bridge
# rugged sparrow ah that evals ast

no, it is not the package i am thinking about
i cant find it, but i remember that it is evaluating string using eval with no builtins after checking that code has no suspicious expressions (such as dunders)

rugged sparrow
#

ill keep looking for it. can you think of any bypasses?

fleet bridge
#

no, this package has many stars on github and i dont think there were any vulnerabilities
so blocking __ and builtins is enough

rugged sparrow
fleet bridge
#

without dunders and builtins you can get only builtin objects (int, strings, lists, ...) and call theirs methods
there is no way to get reference to any class, because you cannot do type(...) and .__class__

#

funny way to get bytes object that contains numbers from 0 to 255: b''.maketrans(b'',b'')

#

!e assert b''.maketrans(b'',b'') == bytes(range(256))

night quarryBOT
#

@fleet bridge :warning: Your 3.11 eval job has completed with return code 0.

[No output]
rugged sparrow
#

clever

fleet bridge
#

I think it is possible to replace all literals (strings, lists, ints, ...) with our new fully compatible reimplementation of these classes, but with additional restriction on length of these objects. So 10**10**10**10 or [1]*10**10 doesn't explode but raises some exception.

You can replace literals by ast modification.
Maybe you also should wrap all calls, getitems and getattrs into wrapper that returns "restricted" objects instead of raw ints/lists. You also should disallow shadowing your wrapper function.
You should make sure that there is absolutely no way to get raw unrestricted objects.

vast wave
#

even if you delete __builtins__, your other vars are still available in that context

#

better to just entirely nuke python out and implement a pure math expr parser

finite blaze
dusty zodiac
#
x={"FIRE":"WATER","WATER":"EARTH","EARTH":"FIRE","FRIEND":"HEAL","FOE":"ATTACK"}
for i in range(int(input())):t,e=input().split();print([f"{x[t]} {x[e]}",f"{x[t]} {e}"][t[1]=='R'])
rugged sparrow
rugged sparrow
vast wave
#

you have nothing to help you escape, just standard python syntax

#

no functions

rugged sparrow
# vast wave hesitant to say impossible, but i don't see a way to do it

jail.py

import builtins
# remove all the stuff we don't need
for key in vars(builtins).copy():
    if key not in {'input', 'print', 'Exception', 'repr', 'eval'}:
        del builtins.__dict__[key]

del builtins
while True:
    try:
        inp = input('>>> ')
        for blocked in {'__', ':', ' ', '\t', '\n'}:
            if blocked in inp:
                raise Exception('Invalid Input')
        else:
            print(repr(eval(inp, {'__builtins__': {}})))
    except Exception as e:
        print('Error', e)
``` this is the current jail
#

it is possible to break btw @vast wave

#

just tricky

vast wave
#

how

rugged sparrow
#

@fleet bridge @quartz wave challenge for yall ^

vast wave
#

oh wait

#

i have an idea

rugged sparrow
vast wave
#

lemme test it

rugged sparrow
vast wave
#

nvm

#

doesnt work, the other solution would be invalid

#

yeah no i dont see a way to do it

#

what's the solution

rugged sparrow
#

i'm not gonna say until more time has passed

#

@versed eagle @dry mirage you might also want to try this challenge ^

fleet bridge
rugged sparrow
fleet bridge
#

yeah, in this case it works 👍

rugged sparrow
#

yea the builtins clearing messes with a lot of repl stuff

#

but it also makes the challenge more difficult

fleet bridge
#

Same code with some convenient features (better errors and ingoring empty lines): ```py
import builtins

remove all the stuff we don't need

for key in vars(builtins).copy():
if key not in {'input', 'print', 'Exception', 'repr', 'eval'}:
del builtins.dict[key]
del builtins
while True:
try:
inp = input('>>> ')
if not inp:
continue
for blocked in {'__', ':', ' ', '\t', '\n'}:
if blocked in inp:
raise Exception(f'Invalid input: {blocked!r}')
else:
print(repr(eval(inp, {'builtins': {}})))
except Exception as e:
print('Error:', e)

rugged sparrow
#

I can also host this somewhere so that it can be accessed via netcat

#

i wonder if replit would let me

fleet bridge
#

i wonder if it is even possible to create any dict except for empty one

#

comprehensions can be done like this: [(x)for(x)in[1,2,3]]

#

no statements are allowed (because of no and :)

#

wait, it is eval, so anyway i cannot use statements

#

no walrus, no lambdas, no dunders

#

empty set: {1}&{2}

#

some dicts can be made like this: ```py

''.maketrans('13','24')
{49: 50, 51: 52}

#

another way to create dicts: {}.fromkeys({1,2,3})

#
>>> {}.keys().mapping
mappingproxy({})
rugged sparrow
#

if you store the dict somehow you can add and remove items with update and pop/remove

fleet bridge
#

arbitrary dicts:```py

[(d.update([('a','b')]),d)[-1]for(d)in[{}]][0]
{'a': 'b'}

#

list comprehension is used for assignment

#

string with dunder: '_''_init_''_'

#
>>> type('',(),[(d.update([('_''_init_''_',[].append)]),d)[-1]for(d)in[{}]][0])()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list.append() takes exactly one argument (0 given)
>>> type('',(),[(d.update([('_''_init_''_','{}'.format)]),d)[-1]for(d)in[{}]][0])()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: Replacement index 0 out of range for positional args tuple
>>> type('',(),[(d.update([('_''_init_''_',''.format)]),d)[-1]for(d)in[{}]][0])()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: __init__() should return None, not 'str'
``` bound functions are not descriptors, so i dont know how to capture self (if we figure out how to get `type`)
fleet bridge
#

this is hard, i like it

#

TIL int.from_bytes accepts any Iterable[int]: ```py

0 .from_bytes([1,2,3,4])
16_909_060

rugged sparrow
fleet bridge
#

i figured out how to get some code object and some frame object

rugged sparrow
#

how?

fleet bridge
#

generators

rugged sparrow
#

noice

fleet bridge
#

so i think i can construct almost any code object

#

AttributeError: attribute 'f_code' of 'frame' objects is not writable
😭

#

i also can do iter(x)

rugged sparrow
#

did you get a reference to iter? or using a listcomp?

fleet bridge
#
>>> g=((0)for(_)in[0])
>>> g.gi_frame.f_locals
{'.0': <tuple_iterator object at 0x000001875C267EB0>}
>>> g.gi_frame.f_locals['.0']
<tuple_iterator object at 0x000001875C267EB0>
rugged sparrow
#

get arbitary code exec from the limited eval

versed eagle
#

okey

fleet bridge
#

i have access to globals() and to globals()['__builtins__'] so i can add arbitrary vars to global and builtin scopes

rugged sparrow
#

@fleet bridge when you solve it make sure to spoiler

fleet bridge
#

it is ok that i am posting here some thoughts?

languid hare
#

ooh is there a challenge somewhere

rugged sparrow
rugged sparrow
#

run it as python3 jail.py, the goal is to get full code exec from the limited eval

languid hare
#

ah

fleet bridge
#

how to create new global var: ```py

[()for(d)in[(()for()in()).gi_frame.f_globals]for(d['var'])in['value']],var
([()], 'value')

leaden valley
#

im definitely stealing that

rugged sparrow
fleet bridge
#

yes, but you can use it instead of walrus to make arbitrary complex expression with assignments

vast wave
rugged sparrow
#

true, it does allow for more complex expressions

rugged sparrow
# vast wave

replace returns a new code object so you would have to find a way to assign it somewhere

fleet bridge
#

any of these things will lead to breaking out of jail: getattr, type, <global frame>, 1 allowed dunder

#

maybe reference to any python function, but im not sure

rugged sparrow
fleet bridge
#

hmm

#

you can create any type...

rugged sparrow
#

you don't have dunders tho so you can't get to useful ones like FrozenImporter

fleet bridge
#

ah, you can get frametype, generatortype and construct new generator with arbitrary code

rugged sparrow
#

and you can't make methods

earnest wing
#

gonna try this hold on

rugged sparrow
#

!e py type(()for()in())()

night quarryBOT
#

@rugged sparrow :x: Your 3.11 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 1, in <module>
003 |     type(()for()in())()
004 | TypeError: cannot create 'generator' instances
earnest wing
#

which version

rugged sparrow
#

3.11 works

leaden valley
#

have you bypassed it? @rugged sparrow

rugged sparrow
#

yes

fleet bridge
#

reference to almost any module would also be very useful

rugged sparrow
#

it is possible to solve it

#

I am also working on a harder one now 🙂

leaden valley
#

could i know it secretly

rugged sparrow
#

nope, you gotta solve it

leaden valley
#

it was worth a shot

earnest wing
#

just to be clear, is the goal builtin retrieval

fleet bridge
#

i think the goal is to do os.system('...') or eval('...') or exec('...')

earnest wing
#

kay

rugged sparrow
#

my POC does os.system

#

actually I would say that the goal is just os.system or something similar

fleet bridge
#

How to get any char if you know its ord():

#
>>> ord(':')
58
>>> '\0'.translate([(d.update([(0,58)]),d)[-1]for(d)in[{}]][0])
':'
#

wait, it can be done in a lot easier way: ```py

'\x3a'
':'

rugged sparrow
#

that will help once you have eval in order to get imports working again

fleet bridge
#

i'm giving up, i'll try it tomorrow

rugged sparrow
#

@earnest wing lmk how far you get

sudden osprey
#

well I think I know a way of getting dunder methods

#

yeah, should be pretty doable from there

rugged sparrow
sudden osprey
rugged sparrow
#

oh that is not an intended solution

#

wtf

#

but fair enough

#

(those will be blocked in the next one tho)

lunar marsh
#

classic pyjail cheese lmao

sudden osprey
#

haha, did sort of feel like cheating :P

rugged sparrow
#

or it doesnt work?

#

odd

rugged sparrow
leaden valley
#

you can do 1 .__dunder__ to save a char

earnest wing
#

nope

#

read the challenge

#

no spaces

leaden valley
#

ah

rugged sparrow
#

||i assume because it tries to import some encoding corrector (but that fails due to lost __import__)||

sudden osprey
#

Ah interesting

leaden valley
#

that makes sense

rugged sparrow
#

yea it attempts to import unicodedata

#

so the cheese does not work thats good

#

but ill explicitly block that on the next one

meager zinc
#

I think I have a solution

#

but I gtg and I haven't written the full thing out

rugged sparrow
#

@sudden osprey thanks tho, the next one will have this: inp = normalize('NFKD', input('>>> '))

#

to avoid the odd error, this is the new challenge code

import builtins
from unicodedata import normalize
# remove all the stuff we don't need
for key in vars(builtins).copy():
    if key not in {'input', 'print', 'Exception', 'repr', 'eval'}:
        del builtins.__dict__[key]

del builtins
while True:
    try:
        inp = normalize('NFKD', input('>>> '))
        if not inp:
            continue
        for blocked in {'__', ':', ' ', '\t', '\n'}:
            if blocked in inp:
                raise Exception(f'Invalid Input {blocked!r}')
        else:
            print(repr(eval(inp, {'__builtins__': {}})))
    except Exception as e:
        print('Error', e)
``` @sudden osprey
earnest wing
rugged sparrow
#

fair enough

earnest wing
#

but not far, mostly toyed around with ||generator frames / code objects||

#

as well as the funny ||.0 local||

low lynx
rugged sparrow
leaden valley
#

im trying to think of a way to bypass " "

#

in my string

#

starting to think its impossible

rugged sparrow
#

i guarantee it is not impossible

lunar marsh
rugged sparrow
#

you can dm it or just spoilertag it here

gentle plover
leaden valley
#

||my attempt was to use load_module but of course that uses __import__ so didnt work||

gentle plover
#

WAIT
I FOUND SOMETHING

#

||"<{a.b} encoded using \x-stuff>".format(a)||

wide crest
#

what is this channel for?

rugged sparrow
#

python weirdness

wide crest
#

hm

#

yea i have something i guess

#

its only using class conversion so

#

i cant use and functions

rugged sparrow
#

like call os.system or something like that

#

get out of the python jail

quartz wave
#

ok

#

i figured out how to get type but it's stuck in a string

rugged sparrow
quartz wave
#

i forgot

rugged owl
restive void
rugged sparrow
restive void
#

Okay, I figured out how to set global variables. Not sure if that helps me yet :D

fleet bridge
fleet bridge
#

||```py

'{x.class.base.subclasses}'.format(x=())
<built-in method subclasses of type object at 0x00007FFED0B78DF0>

rugged sparrow
#

probably custom terminal stuff?

night quarryBOT
#

@half escarp :x: Your 3.11 eval job has completed with return code 143 (SIGTERM).

001 | Error name 'getattr' is not defined
002 | Error name 'getattr' is not defined
003 | Error name 'getattr' is not defined
004 | Error name 'getattr' is not defined
005 | Error name 'getattr' is not defined
006 | Error name 'getattr' is not defined
007 | Error name 'getattr' is not defined
008 | Error name 'getattr' is not defined
009 | Error name 'getattr' is not defined
010 | Error name 'getattr' is not defined
011 | Error name 'getattr' is not defined
... (truncated - too many lines)

Full output: too long to upload

rugged sparrow
#

probably not

#

ill check it

#

its an exception raised by the module repr

rugged sparrow
#

Try to spoiler tag solutions

#

there ya go

#

that solution uses the intended strategy of || using generator frames to step out to the main frame||

quartz wave
#

is it supposed to be ran on a file to make it work?

#

this is a very version specific solution

#

doesn't work in 3.11

#

||had to adjust offset by 1 backwards||

rugged sparrow
quartz wave
rugged sparrow
#

Ah yea that won't quite work

#

I mean it's still escapable but the repl makes it a degree easier

rugged sparrow
# quartz wave how?

Certain stuff is initialized upon entering the repl that puts a lot more things into local scope

quartz wave
#

||no-offset|| way ||```py
[*([x.append(((x[0].gi_frame.f_back.f_back.f_globals)for()in((),)))or(x[0])for(x)in[[]]][0])][0]["\x5f\x5fbuiltins\x5f\x5f"].eval('((x.{0}call{0}.{0}globals{0})for(x)in"".{0}class{0}.{0}bases{0}[0].{0}subclasses{0}()if"{0}globals{0}"in(x).{0}call{0}.{0}dir{0}()).{0}next{0}()["sys"].modules["os"]'.format('\x5f\x5f')).system

rugged sparrow
#

||Part of the challenge is getting os.system after breaking out of the eval||

#

The one I am working on right now will definitely not work in the repl and should be way more difficult then this one

quartz wave
#

eh idk

restive void
fleet bridge
#

my code also can set builtins

vast wave
#

i was gone for one day

leaden valley
#

is there a way to obfuscate function kwarg names

#
func(OBFUSCATED_NAME="Hi")
``` OBFUSCATED_NAME will be interpreted as another name inside func
#

thats my goal

low lynx
#

just pass in **{}

leaden valley
rugged sparrow
leaden valley
#

it doesnt need to be using generator frames

kindred stone
#
# .env
SENSITIVE="sensitive data" #\n
MORE="more data"
read_dotenv=lambda k: [v[1]for l in open(".env", "r")if (v:=l.replace("\n", "").split("="))[0]==k]or None
assert read_dotenv("SENSITIVE") == "sensitive data"
assert read_dotenv("MORE") == "more data"
assert read_dotenv("NON_EXISTENT_KEY") is None

looking to shorten this

rugged sparrow
last locust
#

Might actually be shorter without the zip, not sure

#

Not strictly the same, but would give the same result (one assert instead of 3, and does ==None instead of None)

kindred stone
#

these asserts where there just to tell what are the expected results of the function, i was looking to the actual implementation of read_dotenv being shorter

last locust
#

You also don't need the , "r" in the open() since r mode is the default

arctic elm
#

because of comments

kindred stone
#

oh bruh

#

need to ignore #

arctic elm
#

:^)

exec(open('.env').read())                                                   read_dotenv=lambda x:globals().get(x,None)
kindred stone
#

sure

arctic elm
#

actually

#
exec(open('.env').read())                                                   read_dotenv=globals().get
#

not in the spirit of what you want, but passes your tests

kindred stone
#

none of my tests passes on my own function🤦‍♂️

#

i wrote them right before sending and didnt tested them, mb

#

only the last one that checks for None works 😆

kindred stone
arctic elm
#

"works" is debatable

#

it happens to do what you tested

kindred stone
#

yeah

#

if you dont set the variables with quotes it wont work

#

but its nice for what was proposed

arctic elm
#

just don't use it in any real code...

kindred stone
#

obviously

leaden valley
#

is hexadecimal good enough string obfuscation for scanners

tough willow
#

What Scanners

leaden valley
# tough willow What Scanners

there are no specific ones, im just trying to avoid regex patterns by obfuscating strings (in this case im obfuscating "__builtins__")

#

yet i have no clue what is good enough

tough willow
#

Why do you want to do that

leaden valley
#

im learning evasion techniques

tough willow
#

Evasion for what

leaden valley
#

...

#

just in general

tough willow
#

I don't know what you are talking about, sorry

leaden valley
#

idk the proper term for it

unique heath
#

im bored

def print(*args, sep=" ", end="\n", file=None, flush=False):
    if not file:
        file = open(1, "w")

    file.write(sep.join(map(str, args))
    file.write(end)
    
    if flush:
        file.flush()


def input(prompt=''):
    return open(0).readlines(1).strip("\n")
versed eagle
#

now implement builtins.open

#

and whatever _io classes it uses

versed eagle
#

from posix import * is your friend

fleet bridge
#

🧐

rugged sparrow
#

!e @fleet bridge researching building functions without functions as I build another challenge: || py print([ [ SumArgs.append((1,2,3,4))or(SumCall(None)), SumArgs.append((5,6,7,8))or(SumCall(None)), ] for(SumArgs)in[[]] for(SumCall)in[( [ temp.insert(0,temp.pop()+arg) for(arg)in(args) ]and(temp[0]) for(args)in(SumArgs) for(temp)in[[0]] ).send] ]) ||

night quarryBOT
#

@rugged sparrow :white_check_mark: Your 3.11 eval job has completed with return code 0.

[[10, 26]]
versed eagle
#

or a vm

rugged sparrow
rugged sparrow
#

for the next one you will have to build functional classes using that strategy

fleet bridge
#

Now i understand how your code does what it does. It is very tricky

rugged sparrow
#

Right now I'm trying to research how to build a wrapper so that arguments and execution can happen at the same time like a regular function

fleet bridge
#

SumArgs is changing size, and at the same time you are iterating over it. Why it is not causing any exceptions?
Or changing container size is forbidden only for dicts?

rugged sparrow
#

Changing the container size is only forbidden on types where it's dangerous

fleet bridge
#

So, dicts + sets + frozensets?

rugged sparrow
#

Yes

#

Although there's a trick where you can actually change the values of a set while iterating over it

#

But you run into implementation quirks of the underlying Hash table

#

Like you can only swap out eight items

#

I'll dig around and see if I can find the code

quartz wave
leaden valley
versed eagle
#

whats the regex you're trying to avoid matching

#

pretty hard to avoid matching a regex if you don't know what it is

fleet bridge
#

Docs says that objects equality should imply hashes equality. What if our object is returning different hashes?
I can put two objects that returns hashes 1 and 2, then do 🪄 and then their hashes are now 2 and 1 (instead of 1 and 2). And if i try using one object as key i will get value corresponding to other key.

I think y'all can make a lot of fun with that

leaden valley
#

i have a way to get __import__ but i cant access the attributes because getattr is blocked

#

and the attribute itself is blocked

quartz wave
versed eagle
#

vars(obj)[attrstr]

leaden valley
fleet bridge
#

You can scroll up a bit and find code that can execute arbitrary code and has no dunders or getattr

#

I posted a small code that constructs one char string from its ord

#

You can combine these ideas

leaden valley
#

i'll have a look and thanks @versed eagle that will work i think

leaden valley
#

i need Popen

versed eagle
#

you probably don't

fleet bridge
#

You can do os.system(py -c 'import subprocess') or something

versed eagle
#

from posix import * is your friend
or nt if you're on windows ig

#

actually why not just import builtins

leaden valley
#

doing that seems lazy imo

#

octal is, not sure about binary

#

doesnt octal use hex?

fleet bridge
leaden valley
#

actually nvm

rough moat
spiral merlin
#

how do I code a bonzi buddy

#

but one I can set to any picture I want

#

you know

#

like a desktop stripper

vast wave
unique heath
#

esopy challenge:

golf an accurate version of print

#
def print(*a,s='',e='\n',p=0,o=0):(f:=open(p,'w'),f.write(s.join(map(str,a))+e),f.flush()if o else ...)
``` 1liner
#

102 chars

quartz wave
#

s should be ' '

low lynx
#

isn't that 104

#

nor is that accurate since the kwargs are different

unique heath
#

fine

def print(*a,sep='',end='\n',file=0,flush=0):(f:=open(file,'w'),f.write(sep.join(map(str,a))+end),f.flush()if flush else ...)
arctic skiff
unique heath
night quarryBOT
#

@unique heath :x: Your 3.11 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 1, in <module>
003 |     print(''.join([1,2,3,4]))
004 |           ^^^^^^^^^^^^^^^^^^
005 | TypeError: sequence item 0: expected str instance, int found
arctic skiff
#

Ok

quartz wave
#

!e ```py
def print(*a,sep='',end='\n',file=0,flush=0):(f:=open(file,'w'),f.write(sep.join(map(str,a))+end),f.flush()if flush else ...)
print(5, 6, 7)

night quarryBOT
#

@quartz wave :warning: Your 3.11 eval job has completed with return code 0.

[No output]
quartz wave
#

wth

#

!e ```py
def print(*a,sep='',end='\n',file=0,flush=0):(f:=open(file,'w'),f.write(sep.join(map(str,a))+end),f.flush()if flush else ...)
print(5, 6, 7, flush=1)

night quarryBOT
#

@quartz wave :warning: Your 3.11 eval job has completed with return code 0.

[No output]
quartz wave
#

ok it doesn't work in the sandbox

arctic skiff
night quarryBOT
#

@arctic skiff :white_check_mark: Your 3.11 eval job has completed with return code 0.

blah
arctic skiff
#

!epy print(open(0))

night quarryBOT
#

@arctic skiff :white_check_mark: Your 3.11 eval job has completed with return code 0.

<_io.TextIOWrapper name=0 mode='r' encoding='utf-8'>
quartz wave
#

oh they have the fd wrong

quartz wave
arctic skiff
quartz wave
#

golfed ```py
def print(*a,sep=' ',end='\n',file=1,flush=0):(f:=open(file,'w')).write(sep.join(map(str,a))+end),flush and f.flush()

quartz wave
arctic skiff
#

I thought 0 leads to stdout

arctic skiff
quartz wave
#

if i add a parenthesis at the end then i forgot to start a tuple

arctic skiff
#

Nevermind got it

low lynx
#

can't you do f=open(...);f.write(...)

quartz wave
rugged sparrow
#

Arguably using 1 for the file would not be correct. Default print always uses whatever file is in sys.stdout

sudden osprey
#

Also, the file argument is meant to take a file object, not a path

fleet bridge
#

Why there is list[::] but there is no dict[::]?

#

Also why slices are unhashable?

proper vault
#

I don't think anyone really thought too hard about hashing slices, especially since you can just use a tuple and seq[slice(*tup)]

restive void
rugged sparrow
night quarryBOT
#

@rugged sparrow :white_check_mark: Your 3.11 eval job has completed with return code 0.

slice([], [], [])
astral rover
#

wouldnt it make sense though if the underlying items are hashable to have them be hashable much like MappingProxy

fleet bridge
rugged sparrow
#

!e @fleet bridge figured out that multiple loop comprehensions can modify prior variables so Sum is neater py print([ [ SumCall(SumArgs.append((1,2,3,4))), SumCall(SumArgs.append((5,6,7,8))) ] for(SumArgs)in[[]] for(SumCall)in[( [ (sum) for(sum)in[0] for(arg)in(args) for(sum)in[sum+arg] ][-1] for(args)in(SumArgs) ).send] ])

night quarryBOT
#

@rugged sparrow :white_check_mark: Your 3.11 eval job has completed with return code 0.

[[10, 26]]
earnest wing
#

a slice is just an object so it should have no special treatment there, but also there may be an implicit assumption that slicing "works" on dicts

#

this can create silently incorrect, innocuous seeming programs which is not super nice

rough moat
#

like ‘i want everything from ‘banana’ to 13.12’

rugged sparrow
rough moat
#

yeah i guess but you could simply slice through the keys or values tbh

rugged sparrow
#

yea it would not be a smart feature

rough moat
#

now imagine set slicing lmao

fleet bridge
#

Imagine integer slicing

#

And slice slicing

rugged sparrow
night quarryBOT
#

@rugged sparrow :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | /home/main.py:7: SyntaxWarning: 'int' object is not subscriptable; perhaps you missed a comma?
002 |   print(12345[2:4])
003 | 34
versed eagle
#

it might be more useful to check what representation the int used

#

since presumably if the user does 0b01010100[::-1] (84), they want 0b00101010 (24) as a result, rather than 48

sick hound
#

!e ```py
from ctypes import CFUNCTYPE as y,cast as x,c_int as n,c_ssize_t as e,c_void_p as k,py_object as b;a=k16;z=a.from_address;i=z(z(id(list))[13]);u=x(i[5],y(n,b,e,b));e=x(i[3],y(b,b,e));i=a();z(id(int))[14]=x(i,k);i[:3]=0,x(y(b,b,b)(lambdaa:e(*a[::~0])),k),x(y(n,b,b,b)(lambda a,i,e:u(i,a,e)),k)

arr = [2, 3, 4, 5]
1[arr] = 2[arr]

print(f"{arr = }")
print(f"{0[arr] = }")

night quarryBOT
#

@sick hound :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | /home/main.py:4: SyntaxWarning: 'int' object is not subscriptable; perhaps you missed a comma?
002 |   1[arr] = 2[arr]
003 | /home/main.py:7: SyntaxWarning: 'int' object is not subscriptable; perhaps you missed a comma?
004 |   print(f"{0[arr] = }")
005 | arr = [2, 4, 4, 5]
006 | 0[arr] = 2
sick hound
#

real C

dry mirage
rugged sparrow
#

@dry mirage ^ you also might like this one

dry mirage
#

thanks defender 😔

meager zinc
#

huh I didn't know windows flagged hack tools

arctic skiff
meager zinc
#

here's a different style of problem with a pretty obvious solution if you know the trick

while 1:
    try:
        for c in(i:=input('>>> ')):
            if ord(c)<256:raise Exception(f'Invalid Input {i!r}')
        print(repr(eval(i)))
    except Exception as e:print('Error: ', e)
#

(get arbritrary os.system)

fleet bridge
tepid matrix
#

how do you get builtins using "__builtins__" string?

#

generator frames arent allowed

arctic skiff
night quarryBOT
#

@arctic skiff :white_check_mark: Your 3.11 eval job has completed with return code 0.

<module 'builtins' (built-in)>
tepid matrix
arctic skiff
#

!epy print(locals()['__builtins__'])

night quarryBOT
#

@arctic skiff :white_check_mark: Your 3.11 eval job has completed with return code 0.

<module 'builtins' (built-in)>
tepid matrix
#

that too

tough willow
#

eval('__builtins__')?

tepid matrix
#

theres a lot of things ive tried

arctic skiff
#

You are randomly creating rules

tepid matrix
tough willow
#

say every rule

#

you didn't say

  • locals
  • globals
  • eval/exec
tepid matrix
#

thats why i didnt say

tough willow
#

no, they weren't

tepid matrix
#

u think eval isnt obvious?

tough willow
#

yes

tepid matrix
#

okay.

tough willow
#

i think i found a solution

#

!e

import random
print(
  getattr(random, '__builtins__').keys()
)
night quarryBOT
#

@tough willow :white_check_mark: Your 3.11 eval job has completed with return code 0.

{'__name__': 'builtins', '__doc__': "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.", '__package__': '', '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>, origin='built-in'), '__build_class__': <built-in function __build_class__>, '__import__': <built-in function __import__>, 'abs': <built-in function abs>, 'all': <built-in function all>, 'any': <built-in function any>, 'ascii': <built-in function ascii>, 'bin': <built-in function bin>, 'breakpoint': <built-in function breakpoint>, 'callable': <built-in function callable>, 'chr': <built-in function chr>, 'compile': <built-in function compile>, 'delattr': <built-in function delattr>, 'dir': <built-in function dir>, 'divmod': <built-in function divmod>, 'eval': <built-in function eval>, 'exec': <built-in function exec>, 'format': <built-in function format>, 
... (truncated - too long)

Full output: https://paste.pythondiscord.com/nipajoduva.txt?noredirect

tough willow
#

!e

import random
print(
  getattr(random, '__builtins__').keys()
)
night quarryBOT
#

@tough willow :white_check_mark: Your 3.11 eval job has completed with return code 0.

dict_keys(['__name__', '__doc__', '__package__', '__loader__', '__spec__', '__build_class__', '__import__', 'abs', 'all', 'any', 'ascii', 'bin', 'breakpoint', 'callable', 'chr', 'compile', 'delattr', 'dir', 'divmod', 'eval', 'exec', 'format', 'getattr', 'globals', 'hasattr', 'hash', 'hex', 'id', 'input', 'isinstance', 'issubclass', 'iter', 'aiter', 'len', 'locals', 'max', 'min', 'next', 'anext', 'oct', 'ord', 'pow', 'print', 'repr', 'round', 'setattr', 'sorted', 'sum', 'vars', 'None', 'Ellipsis', 'NotImplemented', 'False', 'True', 'bool', 'memoryview', 'bytearray', 'bytes', 'classmethod', 'complex', 'dict', 'enumerate', 'filter', 'float', 'frozenset', 'property', 'int', 'list', 'map', 'object', 'range', 'reversed', 'set', 'slice', 'staticmethod', 'str', 'super', 'tuple', 'type', 'zip', '__debug__', 'BaseException', 'BaseExceptionGroup', 'Exception', 'GeneratorExit', 'KeyboardInterrupt', 'SystemExit', 'ArithmeticError', 'AssertionError', 'AttributeError', 'BufferError', 'EOFError', 'Imp
... (truncated - too long)

Full output: https://paste.pythondiscord.com/apixeroteh.txt?noredirect

arctic skiff
#

!epy print(__import__('__hello__').__getattribute__('__builtins__'))

night quarryBOT
#

@arctic skiff :white_check_mark: Your 3.11 eval job has completed with return code 0.

{'__name__': 'builtins', '__doc__': "Built-in functions, exceptions, and other objects.\n\nNoteworthy: None is the `nil' object; Ellipsis represents `...' in slices.", '__package__': '', '__loader__': <class '_frozen_importlib.BuiltinImporter'>, '__spec__': ModuleSpec(name='builtins', loader=<class '_frozen_importlib.BuiltinImporter'>, origin='built-in'), '__build_class__': <built-in function __build_class__>, '__import__': <built-in function __import__>, 'abs': <built-in function abs>, 'all': <built-in function all>, 'any': <built-in function any>, 'ascii': <built-in function ascii>, 'bin': <built-in function bin>, 'breakpoint': <built-in function breakpoint>, 'callable': <built-in function callable>, 'chr': <built-in function chr>, 'compile': <built-in function compile>, 'delattr': <built-in function delattr>, 'dir': <built-in function dir>, 'divmod': <built-in function divmod>, 'eval': <built-in function eval>, 'exec': <built-in function exec>, 'format': <built-in function format>, 
... (truncated - too long)

Full output: https://paste.pythondiscord.com/yeruwehuhi.txt?noredirect

tough willow
#

works too

#

@tepid matrix

#

using getattr on a module

arctic skiff
#

Incoming text from @tepid matrix

No getattr

tough willow
#

true

tepid matrix
#

getattr isnt blocked

#

thanks

tough willow
#

np

#

should work on any module that doesn't overwrite _builtins_ and doesnt have __all__ i think

#

oh

arctic skiff
#

!epy print(__file__)

night quarryBOT
#

@arctic skiff :white_check_mark: Your 3.11 eval job has completed with return code 0.

/home/main.py
arctic skiff
#

!epy import os with open(__file__) as f: a=f.read() if not os.path.isfile('/home/_'): with open('/home/_', 'x'):... exec(a) print('hi')

night quarryBOT
#

@arctic skiff :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | hi
002 | hi
rugged sparrow
sour panther
low lynx
#

!e @unique heath

import sys
def print(*a,sep=" ",end="\n",file=sys.stdout,flush=0):file.write(sep.join(map(str,a))+end);flush and file.flush()

print(123, 'abc', print, sep='sep')
with open('test.txt', 'w') as file:
    print(123, 456, sep='abc', end='a', file=file)
night quarryBOT
#

@low lynx :white_check_mark: Your 3.11 eval job has completed with return code 0.

123sepabcsep<function print at 0x7f7089e844a0>

test.txt

123abc456a
unique heath
#

!e

print(len('''import sys
def print(*a,sep=" ",end="\n",file=sys.stdout,flush=0):file.write(sep.join(map(str,a))+end);flush and file.flush()'''))````
night quarryBOT
#

@unique heath :white_check_mark: Your 3.11 eval job has completed with return code 0.

124
unique heath
#

huh

winter basin
#

Is there anyone who wants to donate me the nitro

#

?

languid hare
fleet bridge
#

yeah, it should be "\\n" in this code that counts length of other code

low lynx
#

in actual code it would just be the one character wouldn't it?

fleet bridge
#

yes, but in source code it is two characters long

low lynx
#

in golfing we've always counted newlines as one character haven't we?

rugged sparrow
#

I typically wrap the code in a raw string so that it doesn't perform escapes

fleet bridge
quartz wave
serene garnet
#

hmmm

#

can't i use walrus operator like

#
[a,b,c]:=[1,2,3]
sick hound
#

you cant

languid hare
#

mhm

#

not allowed

#

!e

([a, b, c] := [1, 2, 3])
night quarryBOT
#

@languid hare :x: Your 3.11 eval job has completed with return code 1.

001 |   File "/home/main.py", line 1
002 |     ([a, b, c] := [1, 2, 3])
003 |      ^^^^^^^^^
004 | SyntaxError: cannot use assignment expressions with list
serene garnet
#

i have to golf my code

#

but it fails

#
l=lambda:sorted(map(int,input().split()));a,b,c=d=l()
print('YNEOS'[d!=l()or a*a+b*b!=c*c::2])
#

it's a code that inputs each side length of two triangles and determine if it is possible to produce two triangles of given side length, by cutting a rectangle.

fleet bridge
#

i think this: d!=l() will always be True
because d is an int and l() is a list of ints

#

oh wait

#

it is a,b,c=d=l() not a,b,c,d=l()

fleet bridge
# languid hare not allowed

are there any plans to remove these restrictions from walrus?
i think it can work as regular assignment, but it also returns whatever left right side evaluates to

languid hare
#

In the future, if such
use-cases are found, the grammar can be expanded.

#

personally im not too sure why they seemed to be against iterable unpacking in the first place

#

in the example they gave the issue to me seems to be more of an ambiguity in the grammar, whether
x, y := 3, 4 should be a 3-tuple that also assigns y, or should it be a 2-tuple which performs a destructuring assignment

#

of course that goes away when you use parenthesis like a sane person
(x, y) := (3, 4)

next flame
# serene garnet but it fails

works for me

>>> l=lambda:sorted(map(int,input().split()));a,b,c=d=l();print('YNEOS'[d!=l()or a*a+b*b!=c*c::2])
3 4 5
3 4 5
YES
>>> l=lambda:sorted(map(int,input().split()));a,b,c=d=l();print('YNEOS'[d!=l()or a*a+b*b!=c*c::2])
1 2 3
1 2 3
NO
rugged sparrow
#

!e really weird way of running code on interpreter exit: ```py
def onexit(func, cache=[]):
def gen():
try:yield
except:
func()
raise
g = gen()
g.send(None)
cache.append(g)

@onexit
def _():
print('this ran when the process exited')

print('I ran first')

night quarryBOT
#

@rugged sparrow :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | I ran first
002 | this ran when the process exited
quartz wave
#

!e ```py
def gen(f):
try:yield
except:f();raise
def onexit(f,c=[]):g=gen(f);g.send(c.append(g))

onexit(lambda:print(globals()))
print('a')

night quarryBOT
#

@quartz wave :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | a
002 | {'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x7f880d33f750>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins'>, 'gen': <function gen at 0x7f880d2e44a0>, 'onexit': <function onexit at 0x7f880d2e4680>}
quartz wave
#

nice

rugged sparrow
#

ye

#

it works because tp_finalize for generators calls gen_close, which in turn sets PyExc_GeneratorExit as the active exception and then calls gen_send. so if you have a bare try catch you catch the GeneratorExit which lets you run code as the generator is freed

#

(just have to make sure to re-raise the GeneratorExit otherwise you get a RuntimeError)

tepid matrix
tepid matrix
quartz wave
tepid matrix
#

i need to learn more esopy

sick hound
#

!e print("Hello!")

tough willow
#

#bot-commands @sick hound

next flame
quartz wave
low lynx
#

why do you need to send something to g?

rugged sparrow
low lynx
#

!e

def onexit(func, cache=[]):
 def gen():
   try:yield
   except:
     func()
     raise
 g = gen()
 cache.append(g)

@onexit
def _():
 print('this ran when the process exited')

print('I ran first')
night quarryBOT
#

@low lynx :white_check_mark: Your 3.11 eval job has completed with return code 0.

I ran first
low lynx
#

huh

rugged sparrow
#

The generator has to be in the middle of executing. That's what the .send is for

#

If a generator is never started, it doesn't have to be closed

#

*technically this is not quite true, gen_close is still called, but if the generator is not currently inside a try block it cannot catch the GeneratorExit exception, and if it has never been used (by .send or otherwise) then it cannot be inside a try block

rugged sparrow
# tepid matrix do u know how this works cuz all im seeing is a bunch of gibberish

gen returns a generator when called, and you can sort of imagine that to be a function that can be paused. Calling .send executes everything until the first yield. Then the generator is stored in the cache list. When the generator is garage collected (at the end of the program or if the cache is cleared), the interpreter raises a GeneratorExit exception inside of it at the current point of execution. Because it is sitting at the first yield (inside a try/except block) that GeneratorExit exception is caught, func is called, and then the GeneratorExit is re-raised by the argument-less raise.

dry mirage
rugged sparrow
#

As long as cache is held by onexit it won't get picked up. But on interpreter exit it gets cleared out

low lynx
#

hmm

#

!e

def onexit(func, cache=[]):
 def gen():
   try:yield
   except:
     func()
     raise
 g = gen()
 next(g)
 cache.append(g)

@onexit
def _():
 print('this ran when the process exited')

print('I ran first')
night quarryBOT
#

@low lynx :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | I ran first
002 | this ran when the process exited
sick hound
#

sys._exit trolling

astral rover
#

I thought it was os._exit

sick hound
#

oh it seems to be

tepid matrix
rugged sparrow
tepid matrix
#

i try to find books on this topic and i cant

rugged sparrow
#

i spent a lot of time reading the cpython source code and looking for edge cases

#

also lots of trial and error

tepid matrix
#

it would help if i knew c

rugged sparrow
#

yea, C is def a good language to know for messing with the low level stuff like this

gentle plover
#

Python really is fun ```py
def onexit(func, /, *, _cache=[]):
cache.append(type("", (), {"del": lambda s, *, **__: func()})())

rugged sparrow
gentle plover
#

replace type with [].__class__.__class__

rugged sparrow
#

*builtins or dunders

gentle plover
#

ah

sly ibex
#

hey guys am i wrong or there is a way using f string to avoid math.ceil

rugged sparrow
#

!e you could just print(1.3.__ceil__())

night quarryBOT
#

@rugged sparrow :white_check_mark: Your 3.11 eval job has completed with return code 0.

2
sly ibex
#

nooo haa

#

but

#

i was saying something.... shorter

#

using the f string thereisn't a way?

rugged sparrow
#

fstrings round to nearest

sly ibex
#

f{number:d}

#

something like that

#

oh

#

nearest

#

!e
print(int(f'{1.8:.0f}'))

night quarryBOT
#

@sly ibex :white_check_mark: Your 3.11 eval job has completed with return code 0.

2
rugged sparrow
#

!e py print(f'{1.3 = :.0f}') print(f'{1.8 = :.0f}')

night quarryBOT
#

@rugged sparrow :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | 1.3 = 1
002 | 1.8 = 2
sly ibex
#

!e
import math
print(math.ceil(1.3))
print(math.ceil(1.8))

night quarryBOT
#

@sly ibex :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | 2
002 | 2
sly ibex
#

ohhh

#

@rugged sparrow

#

watch this way
int(a+.99)

fleet bridge
#

yeah this method works (if you somehow got 1-1/inf)

#

it also works with modular arithmetics
for example, you want to round 7 up modulo 3. You can do (7+(3-1))//3*3 and get 9

rugged sparrow
night quarryBOT
#

@rugged sparrow :white_check_mark: Your 3.11 eval job has completed with return code 0.

1
fleet bridge
#

what is the shortest way to get smallest positive value?

#
>>> import sys;eps=sys.float_info.epsilon
>>> ceil = lambda x: int(x + 1 - eps)
>>> ceil(1.0)
1
>>> ceil(1.1)
2
>>> ceil(1.00000001)
2
>>> ceil(1.000000000001)
2
>>> ceil(1.00000000000000001)
1
#

idk if this will work with big values

earnest wing
#

float_info.epsilon is slightly different semantically but should accidentally give the correct result for numbers close to 1

#

math.nextafter is closest to what you want I think

meager zinc
#

!e ```py
import sys;eps=sys.float_info.epsilon
print(f"{1e-128:.128f}")
print(f"{eps:.128f}")
print(1e-128 > 0)
print(eps > 0)

night quarryBOT
#

@meager zinc :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | 0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
002 | 0.00000000000000022204460492503130808472633361816406250000000000000000000000000000000000000000000000000000000000000000000000000000
003 | True
004 | True
meager zinc
#

!e noting this:

for i in range(500,1,-1):
    if (v := eval(f"1e-{i}")) != 0:
        print(v, v > 0)
        break
night quarryBOT
#

@meager zinc :white_check_mark: Your 3.11 eval job has completed with return code 0.

1e-323 True
vast wave
night quarryBOT
#

@vast wave :white_check_mark: Your 3.11 eval job has completed with return code 0.

1e-10
unique heath
#

!e dropping this here to help the obfooscators

print(__builtins__.eval(__builtins__.__dir__().__getitem__(__import__.__dir__().__len__().__sub__(______:=(___:=(_:=__name__.__getitem__((__:=__name__.__class__().__len__()))).__add__(_).__len__()).__mul__((____:=___.__mul__(___))).__add__((_____:=(_:=__name__.__getitem__(__)).__add__(_).__add__(_).__len__()))))))```
night quarryBOT
#

@unique heath :white_check_mark: Your 3.11 eval job has completed with return code 0.

<built-in function eval>
low lynx
#

ah yes

#

first thing to do when getting eval

#

is to get eval from builtins

vast wave
night quarryBOT
#

@vast wave :white_check_mark: Your 3.11 eval job has completed with return code 0.

<built-in function eval>
night quarryBOT
#

@unique heath :white_check_mark: Your 3.11 eval job has completed with return code 0.

<built-in function eval>
vast wave
quartz wave
#

#python-discussion message i think ```py
for p in[eval('[f"{1950+%d*10}s",%%d,[]]'%i%x)for i,x in enumerate("5x5NExB9GEpydZwCtGa2BP 1kjfmZ8JGrDA5YrBVVZLRx 0eMhi5F148eRenCW2C48nL 4DgEvihGmE3r5wUHUS7Wwv 2eh6kA7m2vUSalEBnKs5yC 1GOVol2EIX1sPDjspcHOtG 5RI8VCGELpG38SCVZBiGs0 4L6k3WNfixzwFI92ba0dqr".split())]:
R=requests.get(f"https://api.spotify.com/v1/playlists/{p[1]}/tracks?limit=50",headers={"Authorization":f"Bearer {access_token}"});print("Playlist: "+p[0])
for t in R.json()["items"]:s=t["track"];a=s["album"];z=a["release_date"];s["name"]in p[2]and int(z.split("-")[0])<2020and a["release_date_precision"]in{"year","month","day"}and print(f"{p[0]}: {s['name']} ({z})")

sick hound
#

i am so glad i don't see code like this everydya

sick hound
#

that's some nice code wym

rugged owl
#

beautiful

jolly sky
#

wtf is "code gore"

sick hound
#

no clue myself

rugged owl
rugged owl
sick hound
#

I wonder if there's code porn

#

like food porn

#

when the food looks extremely good

#

is there one for codd

#

code

eager ore
kindred raft
sharp glacier
pearl yarrow
#

Pretty sure there's code porn somewhere

cloud fossil
#

Don't go look for it, it might be unsafe code

rugged owl
cloud fossil
#

Bad practices everywhere

#

Man why do so many professional unadvised technical terms apply for this

sick hound
sick hound
wheat river
#

properly aligned

versed eagle
#

the source for GNU Hello

sharp glacier
sick hound
#

lol probably

sick hound
#

good

keen thicket
#

Hello my esoterism enthusiasts, I am happy to announce that for the first time ever, the International Obfuscated Python Code Competition is accepting submissions. This contest is similar to the IOCCC (International Obfuscated C Code Contest), and is intended to reward creative and perverse code style. A short bio about the judges and more details about the logistics of the competition will be released on April 30th, with submissions ending August 30th, 2023. Answers to FAQs as well as more information about the rules can be found on the website: pyobfusc.com

versed eagle
#

#

can that get pinned

#

so it doesn't get buried

keen thicket
#

@sharp kestrel if you could pin it that would be great--this channel seems to have a decent amount of activity

karmic pumice
#
def J():A='Safe at last';B='Run away';C='\n';D='';E='doo-doo';F=['Baby','Mommy','Daddy','Grandma','Grandpa',"Let's go hunt",B,A,"It's the end"];G={B:'(ah!)',A:'(phew)'};H='Shark';I='';D=[f"{3*f'{A} {H if B<=4 else I}, {E}, {E}{C}'}{A} {H if B<=4 else G[A]if(A:=F[B])in G.keys()else I}{C*2}"for B in range(len(F))];return D;print(J())
viscid latch
#

What?

finite blaze
earnest wing
keen thicket
keen thicket
#

is this the baby shark song lyrics generator?

karmic pumice
# keen thicket this doesn't produce any output when I run it?

I guess this is a small problem either on the bot side or on my side. For me it works, but the bot doesnt run it, maybe because the call to the functions is on the same line as declaration 🤷
editing it to

def J():A='Safe at last';B='Run away';C='\n';D='';E='doo-doo';F=['Baby','Mommy','Daddy','Grandma','Grandpa',"Let's go hunt",B,A,"It's the end"];G={B:'(ah!)',A:'(phew)'};H='Shark';I='';D=[f"{3*f'{A} {H if B<=4 else I}, {E}, {E}{C}'}{A} {H if B<=4 else G[A]if(A:=F[B])in G.keys()else I}{C*2}"for B in range(len(F))];return D;
print(J())

actually makes it work on the bot

keen thicket
#

Oh yep! putting it on the new line makes it work for me in repl.it

karmic pumice
#

well its weird af for me now, i'm trying to run it by myself but it generates the wrong thing for some reason lmao

#

havent used python in a long time and just came back for some simple scripting so decided to do some funny esoteric

keen thicket
#

yeah I feel you haha

#

I think you just need to strip some newlines

#

or use ''.join()

karmic pumice
keen thicket
#

I'm getting more than that

#

hmm

karmic pumice
keen thicket
#

oh yeah lol that's an old footgun

karmic pumice
#
def a():
    b = "\n"
    c = ""
    d = "doo-doo"
    e = ["Baby", "Mommy", "Daddy", "Grandma", "Grandpa", "Let's go hunt", "Run away", "Safe at last", "It's the end"]
    f = {"Run away": "(ah!)", "Safe at last": "(phew)"}
    i = "Shark"
    j = ""
    c = [f"{3 * f'{e[g]} {i if g <= 4 else j}, {d}, {d}{b}'}{e[g]} {i if g <= 4 else (f[e[g]] if (h:=e[g]) in f.keys() else j)}{b*2}" for g in range(len(e))]
    return "".join(c)

print(a())

so thats the more ,,readable" version

keen thicket
#

you can also change the "in f.keys()" to just "in f"

#

also if you wanted to change all those assignments to walrus operators you could use a lambda and save a few chars overall I think haha

karmic pumice
#

!e

def A():I='Safe at last';H='Run away';C='\n';A='';D='doo-doo';B=['Baby','Mommy','Daddy','Grandma','Grandpa',"Let's go hunt",H,I,"It's the end"];E={H:'(ah!)',I:'(phew)'};F='Shark';G='';A=[f"{3*f'{B[A]} {F if A<=4 else G}, {D}, {D}{C}'}{B[A]} {F if A<=4 else E[B[A]]if(H:=B[A])in E else G}{C*2}"for A in range(len(B))];return ''.join(A)
print(A())
night quarryBOT
#

@karmic pumice :white_check_mark: Your 3.11 eval job has completed with return code 0.

001 | Baby Shark, doo-doo, doo-doo
002 | Baby Shark, doo-doo, doo-doo
003 | Baby Shark, doo-doo, doo-doo
004 | Baby Shark
005 | 
006 | Mommy Shark, doo-doo, doo-doo
007 | Mommy Shark, doo-doo, doo-doo
008 | Mommy Shark, doo-doo, doo-doo
009 | Mommy Shark
010 | 
011 | Daddy Shark, doo-doo, doo-doo
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/bipojalupo.txt?noredirect

karmic pumice
#

fixed :d

keen thicket
#

hell yea

karmic pumice
#

fun way to get back into python lmao

keen thicket
#

yeah python is really fun for stuff like this

#

how do u get rid of those damn massive previews

karmic pumice
#

woah, these ones always impress me

keen thicket
#

wait it works tho

#

hahah

#

it got rid of the preview

karmic pumice
#

well this is supposed to be the correct way

keen thicket
#

OH

#

hahaha

#

I was confused

karmic pumice
#

yeah it looks impressive

keen thicket
#

thank god for the walrus operator hahah

#

I also made something that takes code that is just lambdas or basic expressions and reflows it into ascii art

#

it results in some really weird coding style

#

it can reflow itself so the main program kinda shows

#

how awkward it is to write like that

#

but you need to do that anyway for really intense whitespace transformation

karmic pumice
#

it doesnt even have list comps lemon_angrysad

keen thicket
#

damn wtf

#

list comps are like

#

the best thing about python haha

karmic pumice
#

yeah it was sad to not have list comprehensions in a language that even states on the docs that its similar to python syntax-wise

keen thicket
#

I mean most of the time when a language says they have python syntax they just mean it's easy to read

#

but most of the time they don't make it easy to write

karmic pumice
#

1 sec, ill share a small snippet

#
func Jump(move_axis: Vector2) -> void:
    if cur_jumps > 0:
        cur_jumps -= 1
        velocity.y = jump_velocity; velocity.x = move_axis.x * speed_limit_h
        var particle = preload("res://Scenes/JP.tscn").instantiate()
        particle.global_position = self.global_position - Vector2(0, -10)
        particle.rotation = PI / 4 * move_axis.x
        $"/root/World/Particles".add_child(particle)

func _physics_process(delta: float) -> void:
    var move_axis: Vector2 = Vector2(Input.get_axis("ui_left", "ui_right"), 0)
    
    if (is_on_floor()): cur_jumps = max_jumps
    if Input.is_action_just_pressed("ui_up"): Jump(move_axis)
    set_collision_mask_value(2, !Input.is_action_pressed("ui_down"))
    if !(is_on_floor()): velocity.y += gravity * delta
    
    if velocity.x == 0: velocity.x = move_axis.x * starting_velocity_h
    else:
        var accel_h = acceleration_h * move_axis.x * delta
        if sign(velocity.x) != sign(accel_h): accel_h *= 5.0;
        velocity.x = clamp(velocity.x + accel_h, -speed_limit_h, speed_limit_h)
    
    if !move_axis.x: velocity.x = move_toward(velocity.x, 0, delta * 100.0)
    move_and_slide()
#

(it isnt even supported in markdown yet, sadly)

keen thicket
#

wtf and they also have var?

karmic pumice
#

also, i was quite surprised that a language for a game engine doesnt have static typing for dictionary key and value types and for nested collection element types 💀

karmic pumice
keen thicket
#

to be fair, type hinting in python is really fucking hard

#

but they could surely implement that on the engine side

karmic pumice
keen thicket
#

I mean I worked on PyMongo last year when it switched over to being fully type annotated

#

and it was a nightmare

#

easily 9 months of work

#

mypy is really opinionated, but also has a ton of weird edge cases and straight up doesn't support some things

karmic pumice
keen thicket
#

you constantly have to sprinkle in #type:ignore after everything